First stab at getting namespaces working with PPH. This change will
[official-gcc.git] / gcc / cp / decl.c
blob24339a438179c07b02cb1be74b079d57a2c826bf
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* Process declarations and symbol lookup for C++ front end.
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
28 /* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "flags.h"
37 #include "cp-tree.h"
38 #include "tree-iterator.h"
39 #include "tree-inline.h"
40 #include "decl.h"
41 #include "intl.h"
42 #include "output.h"
43 #include "toplev.h"
44 #include "hashtab.h"
45 #include "tm_p.h"
46 #include "target.h"
47 #include "c-family/c-common.h"
48 #include "c-family/c-objc.h"
49 #include "c-family/c-pragma.h"
50 #include "diagnostic.h"
51 #include "intl.h"
52 #include "debug.h"
53 #include "timevar.h"
54 #include "pointer-set.h"
55 #include "splay-tree.h"
56 #include "plugin.h"
57 #include "langhooks.h"
58 #include "pph.h"
60 /* Possible cases of bad specifiers type used by bad_specifiers. */
61 enum bad_spec_place {
62 BSP_VAR, /* variable */
63 BSP_PARM, /* parameter */
64 BSP_TYPE, /* type */
65 BSP_FIELD /* field */
68 static tree grokparms (tree parmlist, tree *);
69 static const char *redeclaration_error_message (tree, tree);
71 static int decl_jump_unsafe (tree);
72 static void require_complete_types_for_parms (tree);
73 static int ambi_op_p (enum tree_code);
74 static int unary_op_p (enum tree_code);
75 static void push_local_name (tree);
76 static tree grok_reference_init (tree, tree, tree, tree *);
77 static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
78 int, int, tree);
79 static void record_unknown_type (tree, const char *);
80 static tree builtin_function_1 (tree, tree, bool);
81 static tree build_library_fn_1 (tree, enum tree_code, tree);
82 static int member_function_or_else (tree, tree, enum overload_flags);
83 static void bad_specifiers (tree, enum bad_spec_place, int, int, int, int,
84 int);
85 static void check_for_uninitialized_const_var (tree);
86 static hashval_t typename_hash (const void *);
87 static int typename_compare (const void *, const void *);
88 static tree local_variable_p_walkfn (tree *, int *, void *);
89 static tree record_builtin_java_type (const char *, int);
90 static const char *tag_name (enum tag_types);
91 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
92 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
93 static void maybe_deduce_size_from_array_init (tree, tree);
94 static void layout_var_decl (tree);
95 static tree check_initializer (tree, tree, int, tree *);
96 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
97 static void save_function_data (tree);
98 static void copy_type_enum (tree , tree);
99 static void check_function_type (tree, tree);
100 static void finish_constructor_body (void);
101 static void begin_destructor_body (void);
102 static void finish_destructor_body (void);
103 static void record_key_method_defined (tree);
104 static tree create_array_type_for_decl (tree, tree, tree);
105 static tree get_atexit_node (void);
106 static tree get_dso_handle_node (void);
107 static tree start_cleanup_fn (void);
108 static void end_cleanup_fn (void);
109 static tree cp_make_fname_decl (location_t, tree, int);
110 static void initialize_predefined_identifiers (void);
111 static tree check_special_function_return_type
112 (special_function_kind, tree, tree);
113 static tree push_cp_library_fn (enum tree_code, tree);
114 static tree build_cp_library_fn (tree, enum tree_code, tree);
115 static void store_parm_decls (tree);
116 static void initialize_local_var (tree, tree);
117 static void expand_static_init (tree, tree);
119 /* The following symbols are subsumed in the cp_global_trees array, and
120 listed here individually for documentation purposes.
122 C++ extensions
123 tree wchar_decl_node;
125 tree vtable_entry_type;
126 tree delta_type_node;
127 tree __t_desc_type_node;
129 tree class_type_node;
130 tree unknown_type_node;
132 Array type `vtable_entry_type[]'
134 tree vtbl_type_node;
135 tree vtbl_ptr_type_node;
137 Namespaces,
139 tree std_node;
140 tree abi_node;
142 A FUNCTION_DECL which can call `abort'. Not necessarily the
143 one that the user will declare, but sufficient to be called
144 by routines that want to abort the program.
146 tree abort_fndecl;
148 The FUNCTION_DECL for the default `::operator delete'.
150 tree global_delete_fndecl;
152 Used by RTTI
153 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
154 tree tinfo_var_id; */
156 tree cp_global_trees[CPTI_MAX];
158 /* Indicates that there is a type value in some namespace, although
159 that is not necessarily in scope at the moment. */
161 tree global_type_node;
163 /* The node that holds the "name" of the global scope. */
164 tree global_scope_name;
166 #define local_names cp_function_chain->x_local_names
168 /* A list of objects which have constructors or destructors
169 which reside in the global scope. The decl is stored in
170 the TREE_VALUE slot and the initializer is stored
171 in the TREE_PURPOSE slot. */
172 tree static_aggregates;
174 /* -- end of C++ */
176 /* A node for the integer constant 2. */
178 tree integer_two_node;
180 /* Used only for jumps to as-yet undefined labels, since jumps to
181 defined labels can have their validity checked immediately. */
183 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
184 struct named_label_use_entry *next;
185 /* The binding level to which this entry is *currently* attached.
186 This is initially the binding level in which the goto appeared,
187 but is modified as scopes are closed. */
188 struct cp_binding_level *binding_level;
189 /* The head of the names list that was current when the goto appeared,
190 or the inner scope popped. These are the decls that will *not* be
191 skipped when jumping to the label. */
192 tree names_in_scope;
193 /* The location of the goto, for error reporting. */
194 location_t o_goto_locus;
195 /* True if an OpenMP structured block scope has been closed since
196 the goto appeared. This means that the branch from the label will
197 illegally exit an OpenMP scope. */
198 bool in_omp_scope;
201 /* A list of all LABEL_DECLs in the function that have names. Here so
202 we can clear out their names' definitions at the end of the
203 function, and so we can check the validity of jumps to these labels. */
205 struct GTY(()) named_label_entry {
206 /* The decl itself. */
207 tree label_decl;
209 /* The binding level to which the label is *currently* attached.
210 This is initially set to the binding level in which the label
211 is defined, but is modified as scopes are closed. */
212 struct cp_binding_level *binding_level;
213 /* The head of the names list that was current when the label was
214 defined, or the inner scope popped. These are the decls that will
215 be skipped when jumping to the label. */
216 tree names_in_scope;
217 /* A vector of all decls from all binding levels that would be
218 crossed by a backward branch to the label. */
219 VEC(tree,gc) *bad_decls;
221 /* A list of uses of the label, before the label is defined. */
222 struct named_label_use_entry *uses;
224 /* The following bits are set after the label is defined, and are
225 updated as scopes are popped. They indicate that a backward jump
226 to the label will illegally enter a scope of the given flavor. */
227 bool in_try_scope;
228 bool in_catch_scope;
229 bool in_omp_scope;
232 #define named_labels cp_function_chain->x_named_labels
234 /* The number of function bodies which we are currently processing.
235 (Zero if we are at namespace scope, one inside the body of a
236 function, two inside the body of a function in a local class, etc.) */
237 int function_depth;
239 /* To avoid unwanted recursion, finish_function defers all mark_used calls
240 encountered during its execution until it finishes. */
241 bool defer_mark_used_calls;
242 VEC(tree, gc) *deferred_mark_used_calls;
244 /* States indicating how grokdeclarator() should handle declspecs marked
245 with __attribute__((deprecated)). An object declared as
246 __attribute__((deprecated)) suppresses warnings of uses of other
247 deprecated items. */
248 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
251 /* A list of VAR_DECLs whose type was incomplete at the time the
252 variable was declared. */
254 typedef struct GTY(()) incomplete_var_d {
255 tree decl;
256 tree incomplete_type;
257 } incomplete_var;
259 DEF_VEC_O(incomplete_var);
260 DEF_VEC_ALLOC_O(incomplete_var,gc);
262 static GTY(()) VEC(incomplete_var,gc) *incomplete_vars;
264 /* Returns the kind of template specialization we are currently
265 processing, given that it's declaration contained N_CLASS_SCOPES
266 explicit scope qualifications. */
268 tmpl_spec_kind
269 current_tmpl_spec_kind (int n_class_scopes)
271 int n_template_parm_scopes = 0;
272 int seen_specialization_p = 0;
273 int innermost_specialization_p = 0;
274 struct cp_binding_level *b;
276 /* Scan through the template parameter scopes. */
277 for (b = current_binding_level;
278 b->kind == sk_template_parms;
279 b = b->level_chain)
281 /* If we see a specialization scope inside a parameter scope,
282 then something is wrong. That corresponds to a declaration
283 like:
285 template <class T> template <> ...
287 which is always invalid since [temp.expl.spec] forbids the
288 specialization of a class member template if the enclosing
289 class templates are not explicitly specialized as well. */
290 if (b->explicit_spec_p)
292 if (n_template_parm_scopes == 0)
293 innermost_specialization_p = 1;
294 else
295 seen_specialization_p = 1;
297 else if (seen_specialization_p == 1)
298 return tsk_invalid_member_spec;
300 ++n_template_parm_scopes;
303 /* Handle explicit instantiations. */
304 if (processing_explicit_instantiation)
306 if (n_template_parm_scopes != 0)
307 /* We've seen a template parameter list during an explicit
308 instantiation. For example:
310 template <class T> template void f(int);
312 This is erroneous. */
313 return tsk_invalid_expl_inst;
314 else
315 return tsk_expl_inst;
318 if (n_template_parm_scopes < n_class_scopes)
319 /* We've not seen enough template headers to match all the
320 specialized classes present. For example:
322 template <class T> void R<T>::S<T>::f(int);
324 This is invalid; there needs to be one set of template
325 parameters for each class. */
326 return tsk_insufficient_parms;
327 else if (n_template_parm_scopes == n_class_scopes)
328 /* We're processing a non-template declaration (even though it may
329 be a member of a template class.) For example:
331 template <class T> void S<T>::f(int);
333 The `class T' matches the `S<T>', leaving no template headers
334 corresponding to the `f'. */
335 return tsk_none;
336 else if (n_template_parm_scopes > n_class_scopes + 1)
337 /* We've got too many template headers. For example:
339 template <> template <class T> void f (T);
341 There need to be more enclosing classes. */
342 return tsk_excessive_parms;
343 else
344 /* This must be a template. It's of the form:
346 template <class T> template <class U> void S<T>::f(U);
348 This is a specialization if the innermost level was a
349 specialization; otherwise it's just a definition of the
350 template. */
351 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
354 /* Exit the current scope. */
356 void
357 finish_scope (void)
359 poplevel (0, 0, 0);
362 /* When a label goes out of scope, check to see if that label was used
363 in a valid manner, and issue any appropriate warnings or errors. */
365 static void
366 pop_label (tree label, tree old_value)
368 if (!processing_template_decl)
370 if (DECL_INITIAL (label) == NULL_TREE)
372 location_t location;
374 error ("label %q+D used but not defined", label);
375 location = input_location; /* FIXME want (input_filename, (line)0) */
376 /* Avoid crashing later. */
377 define_label (location, DECL_NAME (label));
379 else
380 warn_for_unused_label (label);
383 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
386 /* At the end of a function, all labels declared within the function
387 go out of scope. BLOCK is the top-level block for the
388 function. */
390 static int
391 pop_labels_1 (void **slot, void *data)
393 struct named_label_entry *ent = (struct named_label_entry *) *slot;
394 tree block = (tree) data;
396 pop_label (ent->label_decl, NULL_TREE);
398 /* Put the labels into the "variables" of the top-level block,
399 so debugger can see them. */
400 DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
401 BLOCK_VARS (block) = ent->label_decl;
403 htab_clear_slot (named_labels, slot);
405 return 1;
408 static void
409 pop_labels (tree block)
411 if (named_labels)
413 htab_traverse (named_labels, pop_labels_1, block);
414 named_labels = NULL;
418 /* At the end of a block with local labels, restore the outer definition. */
420 static void
421 pop_local_label (tree label, tree old_value)
423 struct named_label_entry dummy;
424 void **slot;
426 pop_label (label, old_value);
428 dummy.label_decl = label;
429 slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
430 htab_clear_slot (named_labels, slot);
433 /* The following two routines are used to interface to Objective-C++.
434 The binding level is purposely treated as an opaque type. */
436 void *
437 objc_get_current_scope (void)
439 return current_binding_level;
442 /* The following routine is used by the NeXT-style SJLJ exceptions;
443 variables get marked 'volatile' so as to not be clobbered by
444 _setjmp()/_longjmp() calls. All variables in the current scope,
445 as well as parent scopes up to (but not including) ENCLOSING_BLK
446 shall be thusly marked. */
448 void
449 objc_mark_locals_volatile (void *enclosing_blk)
451 struct cp_binding_level *scope;
453 for (scope = current_binding_level;
454 scope && scope != enclosing_blk;
455 scope = scope->level_chain)
457 tree decl;
459 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
460 objc_volatilize_decl (decl);
462 /* Do not climb up past the current function. */
463 if (scope->kind == sk_function_parms)
464 break;
468 /* Update data for defined and undefined labels when leaving a scope. */
470 static int
471 poplevel_named_label_1 (void **slot, void *data)
473 struct named_label_entry *ent = (struct named_label_entry *) *slot;
474 struct cp_binding_level *bl = (struct cp_binding_level *) data;
475 struct cp_binding_level *obl = bl->level_chain;
477 if (ent->binding_level == bl)
479 tree decl;
481 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
482 TREE_LISTs representing OVERLOADs, so be careful. */
483 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
484 ? DECL_CHAIN (decl)
485 : TREE_CHAIN (decl)))
486 if (decl_jump_unsafe (decl))
487 VEC_safe_push (tree, gc, ent->bad_decls, decl);
489 ent->binding_level = obl;
490 ent->names_in_scope = obl->names;
491 switch (bl->kind)
493 case sk_try:
494 ent->in_try_scope = true;
495 break;
496 case sk_catch:
497 ent->in_catch_scope = true;
498 break;
499 case sk_omp:
500 ent->in_omp_scope = true;
501 break;
502 default:
503 break;
506 else if (ent->uses)
508 struct named_label_use_entry *use;
510 for (use = ent->uses; use ; use = use->next)
511 if (use->binding_level == bl)
513 use->binding_level = obl;
514 use->names_in_scope = obl->names;
515 if (bl->kind == sk_omp)
516 use->in_omp_scope = true;
520 return 1;
523 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
524 when errors were reported, except for -Werror-unused-but-set-*. */
525 static int unused_but_set_errorcount;
527 /* Exit a binding level.
528 Pop the level off, and restore the state of the identifier-decl mappings
529 that were in effect when this level was entered.
531 If KEEP == 1, this level had explicit declarations, so
532 and create a "block" (a BLOCK node) for the level
533 to record its declarations and subblocks for symbol table output.
535 If FUNCTIONBODY is nonzero, this level is the body of a function,
536 so create a block as if KEEP were set and also clear out all
537 label names.
539 If REVERSE is nonzero, reverse the order of decls before putting
540 them into the BLOCK. */
542 tree
543 poplevel (int keep, int reverse, int functionbody)
545 tree link;
546 /* The chain of decls was accumulated in reverse order.
547 Put it into forward order, just for cleanliness. */
548 tree decls;
549 tree subblocks;
550 tree block;
551 tree decl;
552 int leaving_for_scope;
553 scope_kind kind;
554 unsigned ix;
555 cp_label_binding *label_bind;
557 timevar_start (TV_NAME_LOOKUP);
558 restart:
560 block = NULL_TREE;
562 gcc_assert (current_binding_level->kind != sk_class);
564 if (current_binding_level->kind == sk_cleanup)
565 functionbody = 0;
566 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
568 gcc_assert (!VEC_length(cp_class_binding,
569 current_binding_level->class_shadowed));
571 /* We used to use KEEP == 2 to indicate that the new block should go
572 at the beginning of the list of blocks at this binding level,
573 rather than the end. This hack is no longer used. */
574 gcc_assert (keep == 0 || keep == 1);
576 if (current_binding_level->keep)
577 keep = 1;
579 /* Any uses of undefined labels, and any defined labels, now operate
580 under constraints of next binding contour. */
581 if (cfun && !functionbody && named_labels)
582 htab_traverse (named_labels, poplevel_named_label_1,
583 current_binding_level);
585 /* Get the decls in the order they were written.
586 Usually current_binding_level->names is in reverse order.
587 But parameter decls were previously put in forward order. */
589 if (reverse)
590 current_binding_level->names
591 = decls = nreverse (current_binding_level->names);
592 else
593 decls = current_binding_level->names;
595 /* If there were any declarations or structure tags in that level,
596 or if this level is a function body,
597 create a BLOCK to record them for the life of this function. */
598 block = NULL_TREE;
599 if (keep == 1 || functionbody)
600 block = make_node (BLOCK);
601 if (block != NULL_TREE)
603 BLOCK_VARS (block) = decls;
604 BLOCK_SUBBLOCKS (block) = subblocks;
607 /* In each subblock, record that this is its superior. */
608 if (keep >= 0)
609 for (link = subblocks; link; link = BLOCK_CHAIN (link))
610 BLOCK_SUPERCONTEXT (link) = block;
612 /* We still support the old for-scope rules, whereby the variables
613 in a for-init statement were in scope after the for-statement
614 ended. We only use the new rules if flag_new_for_scope is
615 nonzero. */
616 leaving_for_scope
617 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
619 /* Before we remove the declarations first check for unused variables. */
620 if ((warn_unused_variable || warn_unused_but_set_variable)
621 && !processing_template_decl)
622 for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
623 if (TREE_CODE (decl) == VAR_DECL
624 && (! TREE_USED (decl) || !DECL_READ_P (decl))
625 && ! DECL_IN_SYSTEM_HEADER (decl)
626 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
628 if (! TREE_USED (decl))
629 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
630 else if (DECL_CONTEXT (decl) == current_function_decl
631 && TREE_TYPE (decl) != error_mark_node
632 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
633 && errorcount == unused_but_set_errorcount
634 && (!CLASS_TYPE_P (TREE_TYPE (decl))
635 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
637 warning (OPT_Wunused_but_set_variable,
638 "variable %q+D set but not used", decl);
639 unused_but_set_errorcount = errorcount;
643 /* Remove declarations for all the DECLs in this level. */
644 for (link = decls; link; link = TREE_CHAIN (link))
646 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
647 && DECL_NAME (link))
649 tree name = DECL_NAME (link);
650 cxx_binding *ob;
651 tree ns_binding;
653 ob = outer_binding (name,
654 IDENTIFIER_BINDING (name),
655 /*class_p=*/true);
656 if (!ob)
657 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
658 else
659 ns_binding = NULL_TREE;
661 if (ob && ob->scope == current_binding_level->level_chain)
662 /* We have something like:
664 int i;
665 for (int i; ;);
667 and we are leaving the `for' scope. There's no reason to
668 keep the binding of the inner `i' in this case. */
669 pop_binding (name, link);
670 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
671 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
672 /* Here, we have something like:
674 typedef int I;
676 void f () {
677 for (int I; ;);
680 We must pop the for-scope binding so we know what's a
681 type and what isn't. */
682 pop_binding (name, link);
683 else
685 /* Mark this VAR_DECL as dead so that we can tell we left it
686 there only for backward compatibility. */
687 DECL_DEAD_FOR_LOCAL (link) = 1;
689 /* Keep track of what should have happened when we
690 popped the binding. */
691 if (ob && ob->value)
693 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
694 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
697 /* Add it to the list of dead variables in the next
698 outermost binding to that we can remove these when we
699 leave that binding. */
700 VEC_safe_push (tree, gc,
701 current_binding_level->level_chain->dead_vars_from_for,
702 link);
704 /* Although we don't pop the cxx_binding, we do clear
705 its SCOPE since the scope is going away now. */
706 IDENTIFIER_BINDING (name)->scope
707 = current_binding_level->level_chain;
710 else
712 tree name;
714 /* Remove the binding. */
715 decl = link;
717 if (TREE_CODE (decl) == TREE_LIST)
718 decl = TREE_VALUE (decl);
719 name = decl;
721 if (TREE_CODE (name) == OVERLOAD)
722 name = OVL_FUNCTION (name);
724 gcc_assert (DECL_P (name));
725 pop_binding (DECL_NAME (name), decl);
729 /* Remove declarations for any `for' variables from inner scopes
730 that we kept around. */
731 FOR_EACH_VEC_ELT_REVERSE (tree, current_binding_level->dead_vars_from_for,
732 ix, decl)
733 pop_binding (DECL_NAME (decl), decl);
735 /* Restore the IDENTIFIER_TYPE_VALUEs. */
736 for (link = current_binding_level->type_shadowed;
737 link; link = TREE_CHAIN (link))
738 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
740 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
741 FOR_EACH_VEC_ELT_REVERSE (cp_label_binding,
742 current_binding_level->shadowed_labels,
743 ix, label_bind)
744 pop_local_label (label_bind->label, label_bind->prev_value);
746 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
747 list if a `using' declaration put them there. The debugging
748 back ends won't understand OVERLOAD, so we remove them here.
749 Because the BLOCK_VARS are (temporarily) shared with
750 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
751 popped all the bindings. */
752 if (block)
754 tree* d;
756 for (d = &BLOCK_VARS (block); *d; )
758 if (TREE_CODE (*d) == TREE_LIST)
759 *d = TREE_CHAIN (*d);
760 else
761 d = &DECL_CHAIN (*d);
765 /* If the level being exited is the top level of a function,
766 check over all the labels. */
767 if (functionbody)
769 /* Since this is the top level block of a function, the vars are
770 the function's parameters. Don't leave them in the BLOCK
771 because they are found in the FUNCTION_DECL instead. */
772 BLOCK_VARS (block) = 0;
773 pop_labels (block);
776 kind = current_binding_level->kind;
777 if (kind == sk_cleanup)
779 tree stmt;
781 /* If this is a temporary binding created for a cleanup, then we'll
782 have pushed a statement list level. Pop that, create a new
783 BIND_EXPR for the block, and insert it into the stream. */
784 stmt = pop_stmt_list (current_binding_level->statement_list);
785 stmt = c_build_bind_expr (input_location, block, stmt);
786 add_stmt (stmt);
789 leave_scope ();
790 if (functionbody)
792 /* The current function is being defined, so its DECL_INITIAL
793 should be error_mark_node. */
794 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
795 DECL_INITIAL (current_function_decl) = block;
797 else if (block)
798 current_binding_level->blocks
799 = chainon (current_binding_level->blocks, block);
801 /* If we did not make a block for the level just exited,
802 any blocks made for inner levels
803 (since they cannot be recorded as subblocks in that level)
804 must be carried forward so they will later become subblocks
805 of something else. */
806 else if (subblocks)
807 current_binding_level->blocks
808 = chainon (current_binding_level->blocks, subblocks);
810 /* Each and every BLOCK node created here in `poplevel' is important
811 (e.g. for proper debugging information) so if we created one
812 earlier, mark it as "used". */
813 if (block)
814 TREE_USED (block) = 1;
816 /* All temporary bindings created for cleanups are popped silently. */
817 if (kind == sk_cleanup)
818 goto restart;
820 timevar_stop (TV_NAME_LOOKUP);
821 return block;
824 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
825 itself, calling F for each. The DATA is passed to F as well. */
827 static int
828 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
830 int result = 0;
831 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
833 result |= (*f) (name_space, data);
835 for (; current; current = DECL_CHAIN (current))
836 result |= walk_namespaces_r (current, f, data);
838 return result;
841 /* Walk all the namespaces, calling F for each. The DATA is passed to
842 F as well. */
845 walk_namespaces (walk_namespaces_fn f, void* data)
847 return walk_namespaces_r (global_namespace, f, data);
850 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
851 DATA is non-NULL, this is the last time we will call
852 wrapup_global_declarations for this NAMESPACE. */
855 wrapup_globals_for_namespace (tree name_space, void* data)
857 struct cp_binding_level *level = NAMESPACE_LEVEL (name_space);
858 VEC(tree,gc) *statics = level->static_decls;
859 tree *vec = VEC_address (tree, statics);
860 int len = VEC_length (tree, statics);
861 int last_time = (data != 0);
863 if (last_time)
865 check_global_declarations (vec, len);
866 emit_debug_global_declarations (vec, len);
867 return 0;
870 /* Write out any globals that need to be output. */
871 return wrapup_global_declarations (vec, len);
875 /* In C++, you don't have to write `struct S' to refer to `S'; you
876 can just use `S'. We accomplish this by creating a TYPE_DECL as
877 if the user had written `typedef struct S S'. Create and return
878 the TYPE_DECL for TYPE. */
880 tree
881 create_implicit_typedef (tree name, tree type)
883 tree decl;
885 decl = build_decl (input_location, TYPE_DECL, name, type);
886 DECL_ARTIFICIAL (decl) = 1;
887 /* There are other implicit type declarations, like the one *within*
888 a class that allows you to write `S::S'. We must distinguish
889 amongst these. */
890 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
891 TYPE_NAME (type) = decl;
892 TYPE_STUB_DECL (type) = decl;
894 return decl;
897 /* Remember a local name for name-mangling purposes. */
899 static void
900 push_local_name (tree decl)
902 size_t i, nelts;
903 tree t, name;
905 timevar_start (TV_NAME_LOOKUP);
907 name = DECL_NAME (decl);
909 nelts = VEC_length (tree, local_names);
910 for (i = 0; i < nelts; i++)
912 t = VEC_index (tree, local_names, i);
913 if (DECL_NAME (t) == name)
915 if (!DECL_LANG_SPECIFIC (decl))
916 retrofit_lang_decl (decl);
917 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
918 if (DECL_LANG_SPECIFIC (t))
919 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
920 else
921 DECL_DISCRIMINATOR (decl) = 1;
923 VEC_replace (tree, local_names, i, decl);
924 timevar_stop (TV_NAME_LOOKUP);
925 return;
929 VEC_safe_push (tree, gc, local_names, decl);
930 timevar_stop (TV_NAME_LOOKUP);
933 /* Subroutine of duplicate_decls: return truthvalue of whether
934 or not types of these decls match.
936 For C++, we must compare the parameter list so that `int' can match
937 `int&' in a parameter position, but `int&' is not confused with
938 `const int&'. */
941 decls_match (tree newdecl, tree olddecl)
943 int types_match;
945 if (newdecl == olddecl)
946 return 1;
948 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
949 /* If the two DECLs are not even the same kind of thing, we're not
950 interested in their types. */
951 return 0;
953 if (TREE_CODE (newdecl) == FUNCTION_DECL)
955 tree f1 = TREE_TYPE (newdecl);
956 tree f2 = TREE_TYPE (olddecl);
957 tree p1 = TYPE_ARG_TYPES (f1);
958 tree p2 = TYPE_ARG_TYPES (f2);
960 /* Specializations of different templates are different functions
961 even if they have the same type. */
962 tree t1 = (DECL_USE_TEMPLATE (newdecl)
963 ? DECL_TI_TEMPLATE (newdecl)
964 : NULL_TREE);
965 tree t2 = (DECL_USE_TEMPLATE (olddecl)
966 ? DECL_TI_TEMPLATE (olddecl)
967 : NULL_TREE);
968 if (t1 != t2)
969 return 0;
971 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
972 && ! (DECL_EXTERN_C_P (newdecl)
973 && DECL_EXTERN_C_P (olddecl)))
974 return 0;
976 #ifdef NO_IMPLICIT_EXTERN_C
977 /* A new declaration doesn't match a built-in one unless it
978 is also extern "C". */
979 if (DECL_IS_BUILTIN (olddecl)
980 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
981 return 0;
982 #endif
984 if (TREE_CODE (f1) != TREE_CODE (f2))
985 return 0;
987 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
989 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
990 && (DECL_BUILT_IN (olddecl)
991 #ifndef NO_IMPLICIT_EXTERN_C
992 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
993 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
994 #endif
997 types_match = self_promoting_args_p (p1);
998 if (p1 == void_list_node)
999 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1001 #ifndef NO_IMPLICIT_EXTERN_C
1002 else if (!prototype_p (f1)
1003 && (DECL_EXTERN_C_P (olddecl)
1004 && DECL_IN_SYSTEM_HEADER (olddecl)
1005 && !DECL_CLASS_SCOPE_P (olddecl))
1006 && (DECL_EXTERN_C_P (newdecl)
1007 && DECL_IN_SYSTEM_HEADER (newdecl)
1008 && !DECL_CLASS_SCOPE_P (newdecl)))
1010 types_match = self_promoting_args_p (p2);
1011 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1013 #endif
1014 else
1015 types_match =
1016 compparms (p1, p2)
1017 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1018 || targetm.comp_type_attributes (TREE_TYPE (newdecl),
1019 TREE_TYPE (olddecl)) != 0);
1021 else
1022 types_match = 0;
1024 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1026 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
1027 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
1028 return 0;
1030 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1031 DECL_TEMPLATE_PARMS (olddecl)))
1032 return 0;
1034 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1035 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1036 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
1037 else
1038 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1039 DECL_TEMPLATE_RESULT (newdecl));
1041 else
1043 /* Need to check scope for variable declaration (VAR_DECL).
1044 For typedef (TYPE_DECL), scope is ignored. */
1045 if (TREE_CODE (newdecl) == VAR_DECL
1046 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1047 /* [dcl.link]
1048 Two declarations for an object with C language linkage
1049 with the same name (ignoring the namespace that qualify
1050 it) that appear in different namespace scopes refer to
1051 the same object. */
1052 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1053 return 0;
1055 if (TREE_TYPE (newdecl) == error_mark_node)
1056 types_match = TREE_TYPE (olddecl) == error_mark_node;
1057 else if (TREE_TYPE (olddecl) == NULL_TREE)
1058 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1059 else if (TREE_TYPE (newdecl) == NULL_TREE)
1060 types_match = 0;
1061 else
1062 types_match = comptypes (TREE_TYPE (newdecl),
1063 TREE_TYPE (olddecl),
1064 COMPARE_REDECLARATION);
1067 return types_match;
1070 /* If NEWDECL is `static' and an `extern' was seen previously,
1071 warn about it. OLDDECL is the previous declaration.
1073 Note that this does not apply to the C++ case of declaring
1074 a variable `extern const' and then later `const'.
1076 Don't complain about built-in functions, since they are beyond
1077 the user's control. */
1079 void
1080 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1082 if (TREE_CODE (newdecl) == TYPE_DECL
1083 || TREE_CODE (newdecl) == TEMPLATE_DECL
1084 || TREE_CODE (newdecl) == CONST_DECL
1085 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1086 return;
1088 /* Don't get confused by static member functions; that's a different
1089 use of `static'. */
1090 if (TREE_CODE (newdecl) == FUNCTION_DECL
1091 && DECL_STATIC_FUNCTION_P (newdecl))
1092 return;
1094 /* If the old declaration was `static', or the new one isn't, then
1095 then everything is OK. */
1096 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1097 return;
1099 /* It's OK to declare a builtin function as `static'. */
1100 if (TREE_CODE (olddecl) == FUNCTION_DECL
1101 && DECL_ARTIFICIAL (olddecl))
1102 return;
1104 permerror (input_location, "%qD was declared %<extern%> and later %<static%>", newdecl);
1105 permerror (input_location, "previous declaration of %q+D", olddecl);
1108 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1109 function templates. If their exception specifications do not
1110 match, issue a diagnostic. */
1112 static void
1113 check_redeclaration_exception_specification (tree new_decl,
1114 tree old_decl)
1116 tree new_type;
1117 tree old_type;
1118 tree new_exceptions;
1119 tree old_exceptions;
1121 new_type = TREE_TYPE (new_decl);
1122 new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1123 old_type = TREE_TYPE (old_decl);
1124 old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1126 /* [except.spec]
1128 If any declaration of a function has an exception-specification,
1129 all declarations, including the definition and an explicit
1130 specialization, of that function shall have an
1131 exception-specification with the same set of type-ids. */
1132 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1133 && ! DECL_IS_BUILTIN (old_decl)
1134 && flag_exceptions
1135 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1137 error ("declaration of %qF has a different exception specifier",
1138 new_decl);
1139 error ("from previous declaration %q+F", old_decl);
1143 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1144 Otherwise issue diagnostics. */
1146 static bool
1147 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1149 old_decl = STRIP_TEMPLATE (old_decl);
1150 new_decl = STRIP_TEMPLATE (new_decl);
1151 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1152 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1153 return true;
1154 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1155 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1156 return true;
1157 if (TREE_CODE (old_decl) == FUNCTION_DECL && DECL_BUILT_IN (old_decl))
1159 /* Hide a built-in declaration. */
1160 DECL_DECLARED_CONSTEXPR_P (old_decl)
1161 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1162 return true;
1164 error ("redeclaration %qD differs in %<constexpr%>", new_decl);
1165 error ("from previous declaration %q+D", old_decl);
1166 return false;
1169 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1170 && lookup_attribute ("gnu_inline", \
1171 DECL_ATTRIBUTES (fn)))
1173 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1174 If the redeclaration is invalid, a diagnostic is issued, and the
1175 error_mark_node is returned. Otherwise, OLDDECL is returned.
1177 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1178 returned.
1180 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1182 static tree
1183 duplicate_decls_internal (tree newdecl, tree olddecl, bool newdecl_is_friend)
1185 unsigned olddecl_uid = DECL_UID (olddecl);
1186 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1187 int new_defines_function = 0;
1188 tree new_template_info;
1190 if (newdecl == olddecl)
1191 return olddecl;
1193 types_match = decls_match (newdecl, olddecl);
1195 /* If either the type of the new decl or the type of the old decl is an
1196 error_mark_node, then that implies that we have already issued an
1197 error (earlier) for some bogus type specification, and in that case,
1198 it is rather pointless to harass the user with yet more error message
1199 about the same declaration, so just pretend the types match here. */
1200 if (TREE_TYPE (newdecl) == error_mark_node
1201 || TREE_TYPE (olddecl) == error_mark_node)
1202 return error_mark_node;
1204 if (DECL_P (olddecl)
1205 && TREE_CODE (newdecl) == FUNCTION_DECL
1206 && TREE_CODE (olddecl) == FUNCTION_DECL
1207 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1209 if (DECL_DECLARED_INLINE_P (newdecl)
1210 && DECL_UNINLINABLE (newdecl)
1211 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1212 /* Already warned elsewhere. */;
1213 else if (DECL_DECLARED_INLINE_P (olddecl)
1214 && DECL_UNINLINABLE (olddecl)
1215 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1216 /* Already warned. */;
1217 else if (DECL_DECLARED_INLINE_P (newdecl)
1218 && DECL_UNINLINABLE (olddecl)
1219 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1221 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1222 newdecl);
1223 warning (OPT_Wattributes, "previous declaration of %q+D "
1224 "with attribute noinline", olddecl);
1226 else if (DECL_DECLARED_INLINE_P (olddecl)
1227 && DECL_UNINLINABLE (newdecl)
1228 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1230 warning (OPT_Wattributes, "function %q+D redeclared with "
1231 "attribute noinline", newdecl);
1232 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1233 olddecl);
1237 /* Check for redeclaration and other discrepancies. */
1238 if (TREE_CODE (olddecl) == FUNCTION_DECL
1239 && DECL_ARTIFICIAL (olddecl))
1241 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1242 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1244 /* Avoid warnings redeclaring built-ins which have not been
1245 explicitly declared. */
1246 if (DECL_ANTICIPATED (olddecl))
1247 return NULL_TREE;
1249 /* If you declare a built-in or predefined function name as static,
1250 the old definition is overridden, but optionally warn this was a
1251 bad choice of name. */
1252 if (! TREE_PUBLIC (newdecl))
1254 warning (OPT_Wshadow,
1255 DECL_BUILT_IN (olddecl)
1256 ? G_("shadowing built-in function %q#D")
1257 : G_("shadowing library function %q#D"), olddecl);
1258 /* Discard the old built-in function. */
1259 return NULL_TREE;
1261 /* If the built-in is not ansi, then programs can override
1262 it even globally without an error. */
1263 else if (! DECL_BUILT_IN (olddecl))
1264 warning (0, "library function %q#D redeclared as non-function %q#D",
1265 olddecl, newdecl);
1266 else
1268 error ("declaration of %q#D", newdecl);
1269 error ("conflicts with built-in declaration %q#D",
1270 olddecl);
1272 return NULL_TREE;
1274 else if (!types_match)
1276 /* Avoid warnings redeclaring built-ins which have not been
1277 explicitly declared. */
1278 if (DECL_ANTICIPATED (olddecl))
1280 /* Deal with fileptr_type_node. FILE type is not known
1281 at the time we create the builtins. */
1282 tree t1, t2;
1284 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1285 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1286 t1 || t2;
1287 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1288 if (!t1 || !t2)
1289 break;
1290 else if (TREE_VALUE (t2) == fileptr_type_node)
1292 tree t = TREE_VALUE (t1);
1294 if (TREE_CODE (t) == POINTER_TYPE
1295 && TYPE_NAME (TREE_TYPE (t))
1296 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1297 == get_identifier ("FILE")
1298 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1300 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1302 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1303 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1304 types_match = decls_match (newdecl, olddecl);
1305 if (types_match)
1306 return duplicate_decls (newdecl, olddecl,
1307 newdecl_is_friend);
1308 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1311 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1312 break;
1314 else if ((DECL_EXTERN_C_P (newdecl)
1315 && DECL_EXTERN_C_P (olddecl))
1316 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1317 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1319 /* A near match; override the builtin. */
1321 if (TREE_PUBLIC (newdecl))
1323 warning (0, "new declaration %q#D", newdecl);
1324 warning (0, "ambiguates built-in declaration %q#D",
1325 olddecl);
1327 else
1328 warning (OPT_Wshadow,
1329 DECL_BUILT_IN (olddecl)
1330 ? G_("shadowing built-in function %q#D")
1331 : G_("shadowing library function %q#D"), olddecl);
1333 else
1334 /* Discard the old built-in function. */
1335 return NULL_TREE;
1337 /* Replace the old RTL to avoid problems with inlining. */
1338 COPY_DECL_RTL (newdecl, olddecl);
1340 /* Even if the types match, prefer the new declarations type for
1341 built-ins which have not been explicitly declared, for
1342 exception lists, etc... */
1343 else if (DECL_IS_BUILTIN (olddecl))
1345 tree type = TREE_TYPE (newdecl);
1346 tree attribs = (*targetm.merge_type_attributes)
1347 (TREE_TYPE (olddecl), type);
1349 type = cp_build_type_attribute_variant (type, attribs);
1350 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1353 /* If a function is explicitly declared "throw ()", propagate that to
1354 the corresponding builtin. */
1355 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1356 && DECL_ANTICIPATED (olddecl)
1357 && TREE_NOTHROW (newdecl)
1358 && !TREE_NOTHROW (olddecl)
1359 && built_in_decls [DECL_FUNCTION_CODE (olddecl)] != NULL_TREE
1360 && built_in_decls [DECL_FUNCTION_CODE (olddecl)] != olddecl
1361 && types_match)
1362 TREE_NOTHROW (built_in_decls [DECL_FUNCTION_CODE (olddecl)]) = 1;
1364 /* Whether or not the builtin can throw exceptions has no
1365 bearing on this declarator. */
1366 TREE_NOTHROW (olddecl) = 0;
1368 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1370 /* If a builtin function is redeclared as `static', merge
1371 the declarations, but make the original one static. */
1372 DECL_THIS_STATIC (olddecl) = 1;
1373 TREE_PUBLIC (olddecl) = 0;
1375 /* Make the old declaration consistent with the new one so
1376 that all remnants of the builtin-ness of this function
1377 will be banished. */
1378 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1379 COPY_DECL_RTL (newdecl, olddecl);
1382 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1384 /* C++ Standard, 3.3, clause 4:
1385 "[Note: a namespace name or a class template name must be unique
1386 in its declarative region (7.3.2, clause 14). ]" */
1387 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1388 && TREE_CODE (newdecl) != NAMESPACE_DECL
1389 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1390 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1391 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1392 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1394 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1395 && TREE_CODE (newdecl) != TYPE_DECL)
1396 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1397 && TREE_CODE (olddecl) != TYPE_DECL))
1399 /* We do nothing special here, because C++ does such nasty
1400 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1401 get shadowed, and know that if we need to find a TYPE_DECL
1402 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1403 slot of the identifier. */
1404 return NULL_TREE;
1407 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1408 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1409 || (TREE_CODE (olddecl) == FUNCTION_DECL
1410 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1411 return NULL_TREE;
1414 error ("%q#D redeclared as different kind of symbol", newdecl);
1415 if (TREE_CODE (olddecl) == TREE_LIST)
1416 olddecl = TREE_VALUE (olddecl);
1417 error ("previous declaration of %q+#D", olddecl);
1419 return error_mark_node;
1421 else if (!types_match)
1423 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1424 /* These are certainly not duplicate declarations; they're
1425 from different scopes. */
1426 return NULL_TREE;
1428 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1430 /* The name of a class template may not be declared to refer to
1431 any other template, class, function, object, namespace, value,
1432 or type in the same scope. */
1433 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1434 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1436 error ("declaration of template %q#D", newdecl);
1437 error ("conflicts with previous declaration %q+#D", olddecl);
1439 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1440 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1441 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1442 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1443 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1444 DECL_TEMPLATE_PARMS (olddecl))
1445 /* Template functions can be disambiguated by
1446 return type. */
1447 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1448 TREE_TYPE (TREE_TYPE (olddecl))))
1450 error ("new declaration %q#D", newdecl);
1451 error ("ambiguates old declaration %q+#D", olddecl);
1453 return NULL_TREE;
1455 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1457 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1459 error ("declaration of C function %q#D conflicts with",
1460 newdecl);
1461 error ("previous declaration %q+#D here", olddecl);
1462 return NULL_TREE;
1464 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1465 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1467 error ("new declaration %q#D", newdecl);
1468 error ("ambiguates old declaration %q+#D", olddecl);
1469 return error_mark_node;
1471 else
1472 return NULL_TREE;
1474 else
1476 error ("conflicting declaration %q#D", newdecl);
1477 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
1478 return error_mark_node;
1481 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1482 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1483 && (!DECL_TEMPLATE_INFO (newdecl)
1484 || (DECL_TI_TEMPLATE (newdecl)
1485 != DECL_TI_TEMPLATE (olddecl))))
1486 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1487 && (!DECL_TEMPLATE_INFO (olddecl)
1488 || (DECL_TI_TEMPLATE (olddecl)
1489 != DECL_TI_TEMPLATE (newdecl))))))
1490 /* It's OK to have a template specialization and a non-template
1491 with the same type, or to have specializations of two
1492 different templates with the same type. Note that if one is a
1493 specialization, and the other is an instantiation of the same
1494 template, that we do not exit at this point. That situation
1495 can occur if we instantiate a template class, and then
1496 specialize one of its methods. This situation is valid, but
1497 the declarations must be merged in the usual way. */
1498 return NULL_TREE;
1499 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1500 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1501 && !DECL_USE_TEMPLATE (newdecl))
1502 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1503 && !DECL_USE_TEMPLATE (olddecl))))
1504 /* One of the declarations is a template instantiation, and the
1505 other is not a template at all. That's OK. */
1506 return NULL_TREE;
1507 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1509 /* In [namespace.alias] we have:
1511 In a declarative region, a namespace-alias-definition can be
1512 used to redefine a namespace-alias declared in that declarative
1513 region to refer only to the namespace to which it already
1514 refers.
1516 Therefore, if we encounter a second alias directive for the same
1517 alias, we can just ignore the second directive. */
1518 if (DECL_NAMESPACE_ALIAS (newdecl)
1519 && (DECL_NAMESPACE_ALIAS (newdecl)
1520 == DECL_NAMESPACE_ALIAS (olddecl)))
1521 return olddecl;
1522 /* [namespace.alias]
1524 A namespace-name or namespace-alias shall not be declared as
1525 the name of any other entity in the same declarative region.
1526 A namespace-name defined at global scope shall not be
1527 declared as the name of any other entity in any global scope
1528 of the program. */
1529 error ("declaration of namespace %qD conflicts with", newdecl);
1530 error ("previous declaration of namespace %q+D here", olddecl);
1531 return error_mark_node;
1533 else
1535 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1536 if (errmsg)
1538 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1539 if (DECL_NAME (olddecl) != NULL_TREE)
1540 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1541 ? "%q+#D previously defined here"
1542 : "%q+#D previously declared here", olddecl);
1543 return error_mark_node;
1545 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1546 && DECL_INITIAL (olddecl) != NULL_TREE
1547 && !prototype_p (TREE_TYPE (olddecl))
1548 && prototype_p (TREE_TYPE (newdecl)))
1550 /* Prototype decl follows defn w/o prototype. */
1551 warning_at (input_location, 0, "prototype for %q+#D", newdecl);
1552 warning_at (DECL_SOURCE_LOCATION (olddecl), 0,
1553 "follows non-prototype definition here");
1555 else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1556 || TREE_CODE (olddecl) == VAR_DECL)
1557 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1559 /* [dcl.link]
1560 If two declarations of the same function or object
1561 specify different linkage-specifications ..., the program
1562 is ill-formed.... Except for functions with C++ linkage,
1563 a function declaration without a linkage specification
1564 shall not precede the first linkage specification for
1565 that function. A function can be declared without a
1566 linkage specification after an explicit linkage
1567 specification has been seen; the linkage explicitly
1568 specified in the earlier declaration is not affected by
1569 such a function declaration.
1571 DR 563 raises the question why the restrictions on
1572 functions should not also apply to objects. Older
1573 versions of G++ silently ignore the linkage-specification
1574 for this example:
1576 namespace N {
1577 extern int i;
1578 extern "C" int i;
1581 which is clearly wrong. Therefore, we now treat objects
1582 like functions. */
1583 if (current_lang_depth () == 0)
1585 /* There is no explicit linkage-specification, so we use
1586 the linkage from the previous declaration. */
1587 if (!DECL_LANG_SPECIFIC (newdecl))
1588 retrofit_lang_decl (newdecl);
1589 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1591 else
1593 error ("previous declaration of %q+#D with %qL linkage",
1594 olddecl, DECL_LANGUAGE (olddecl));
1595 error ("conflicts with new declaration with %qL linkage",
1596 DECL_LANGUAGE (newdecl));
1600 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1602 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1604 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1605 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1606 int i = 1;
1608 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1609 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1611 for (; t1 && t1 != void_list_node;
1612 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1613 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1615 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1616 TREE_PURPOSE (t2)))
1618 permerror (input_location, "default argument given for parameter %d of %q#D",
1619 i, newdecl);
1620 permerror (input_location, "after previous specification in %q+#D", olddecl);
1622 else
1624 error ("default argument given for parameter %d of %q#D",
1625 i, newdecl);
1626 error ("after previous specification in %q+#D",
1627 olddecl);
1633 /* Do not merge an implicit typedef with an explicit one. In:
1635 class A;
1637 typedef class A A __attribute__ ((foo));
1639 the attribute should apply only to the typedef. */
1640 if (TREE_CODE (olddecl) == TYPE_DECL
1641 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1642 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1643 return NULL_TREE;
1645 /* If new decl is `static' and an `extern' was seen previously,
1646 warn about it. */
1647 warn_extern_redeclared_static (newdecl, olddecl);
1649 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1650 return error_mark_node;
1652 /* We have committed to returning 1 at this point. */
1653 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1655 /* Now that functions must hold information normally held
1656 by field decls, there is extra work to do so that
1657 declaration information does not get destroyed during
1658 definition. */
1659 if (DECL_VINDEX (olddecl))
1660 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1661 if (DECL_CONTEXT (olddecl))
1662 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1663 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1664 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1665 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1666 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1667 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1668 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1669 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1670 SET_OVERLOADED_OPERATOR_CODE
1671 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1672 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1674 /* Optionally warn about more than one declaration for the same
1675 name, but don't warn about a function declaration followed by a
1676 definition. */
1677 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1678 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1679 /* Don't warn about extern decl followed by definition. */
1680 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1681 /* Don't warn about friends, let add_friend take care of it. */
1682 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)))
1684 warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1685 warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
1688 if (DECL_DELETED_FN (newdecl))
1690 error ("deleted definition of %qD", newdecl);
1691 error ("after previous declaration %q+D", olddecl);
1695 /* Deal with C++: must preserve virtual function table size. */
1696 if (TREE_CODE (olddecl) == TYPE_DECL)
1698 tree newtype = TREE_TYPE (newdecl);
1699 tree oldtype = TREE_TYPE (olddecl);
1701 if (newtype != error_mark_node && oldtype != error_mark_node
1702 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1703 CLASSTYPE_FRIEND_CLASSES (newtype)
1704 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1706 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1709 /* Copy all the DECL_... slots specified in the new decl
1710 except for any that we copy here from the old type. */
1711 DECL_ATTRIBUTES (newdecl)
1712 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1714 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1716 tree old_result;
1717 tree new_result;
1718 old_result = DECL_TEMPLATE_RESULT (olddecl);
1719 new_result = DECL_TEMPLATE_RESULT (newdecl);
1720 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1721 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1722 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1723 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1725 DECL_ATTRIBUTES (old_result)
1726 = (*targetm.merge_decl_attributes) (old_result, new_result);
1728 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1730 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1731 && DECL_INITIAL (new_result))
1733 if (DECL_INITIAL (old_result))
1734 DECL_UNINLINABLE (old_result) = 1;
1735 else
1736 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
1737 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
1738 DECL_NOT_REALLY_EXTERN (old_result)
1739 = DECL_NOT_REALLY_EXTERN (new_result);
1740 DECL_INTERFACE_KNOWN (old_result)
1741 = DECL_INTERFACE_KNOWN (new_result);
1742 DECL_DECLARED_INLINE_P (old_result)
1743 = DECL_DECLARED_INLINE_P (new_result);
1744 DECL_DISREGARD_INLINE_LIMITS (old_result)
1745 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1748 else
1750 DECL_DECLARED_INLINE_P (old_result)
1751 |= DECL_DECLARED_INLINE_P (new_result);
1752 DECL_DISREGARD_INLINE_LIMITS (old_result)
1753 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1754 check_redeclaration_exception_specification (newdecl, olddecl);
1758 /* If the new declaration is a definition, update the file and
1759 line information on the declaration, and also make
1760 the old declaration the same definition. */
1761 if (DECL_INITIAL (new_result) != NULL_TREE)
1763 DECL_SOURCE_LOCATION (olddecl)
1764 = DECL_SOURCE_LOCATION (old_result)
1765 = DECL_SOURCE_LOCATION (newdecl);
1766 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
1767 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1769 tree parm;
1770 DECL_ARGUMENTS (old_result)
1771 = DECL_ARGUMENTS (new_result);
1772 for (parm = DECL_ARGUMENTS (old_result); parm;
1773 parm = DECL_CHAIN (parm))
1774 DECL_CONTEXT (parm) = old_result;
1778 return olddecl;
1781 if (types_match)
1783 /* Automatically handles default parameters. */
1784 tree oldtype = TREE_TYPE (olddecl);
1785 tree newtype;
1787 /* Merge the data types specified in the two decls. */
1788 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1790 /* If merge_types produces a non-typedef type, just use the old type. */
1791 if (TREE_CODE (newdecl) == TYPE_DECL
1792 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1793 newtype = oldtype;
1795 if (TREE_CODE (newdecl) == VAR_DECL)
1797 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1798 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1799 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1800 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1801 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1802 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1804 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1805 if (DECL_LANG_SPECIFIC (olddecl)
1806 && CP_DECL_THREADPRIVATE_P (olddecl))
1808 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1809 if (!DECL_LANG_SPECIFIC (newdecl))
1810 retrofit_lang_decl (newdecl);
1812 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1813 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1817 /* Do this after calling `merge_types' so that default
1818 parameters don't confuse us. */
1819 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1820 check_redeclaration_exception_specification (newdecl, olddecl);
1821 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1823 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1824 check_default_args (newdecl);
1826 /* Lay the type out, unless already done. */
1827 if (! same_type_p (newtype, oldtype)
1828 && TREE_TYPE (newdecl) != error_mark_node
1829 && !(processing_template_decl && uses_template_parms (newdecl)))
1830 layout_type (TREE_TYPE (newdecl));
1832 if ((TREE_CODE (newdecl) == VAR_DECL
1833 || TREE_CODE (newdecl) == PARM_DECL
1834 || TREE_CODE (newdecl) == RESULT_DECL
1835 || TREE_CODE (newdecl) == FIELD_DECL
1836 || TREE_CODE (newdecl) == TYPE_DECL)
1837 && !(processing_template_decl && uses_template_parms (newdecl)))
1838 layout_decl (newdecl, 0);
1840 /* Merge the type qualifiers. */
1841 if (TREE_READONLY (newdecl))
1842 TREE_READONLY (olddecl) = 1;
1843 if (TREE_THIS_VOLATILE (newdecl))
1844 TREE_THIS_VOLATILE (olddecl) = 1;
1845 if (TREE_NOTHROW (newdecl))
1846 TREE_NOTHROW (olddecl) = 1;
1848 /* Merge deprecatedness. */
1849 if (TREE_DEPRECATED (newdecl))
1850 TREE_DEPRECATED (olddecl) = 1;
1852 /* Preserve function specific target and optimization options */
1853 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1855 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1856 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1857 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1858 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1860 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1861 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1862 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1863 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1866 /* Merge the initialization information. */
1867 if (DECL_INITIAL (newdecl) == NULL_TREE
1868 && DECL_INITIAL (olddecl) != NULL_TREE)
1870 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1871 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1872 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1874 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1875 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1879 /* Merge the section attribute.
1880 We want to issue an error if the sections conflict but that must be
1881 done later in decl_attributes since we are called before attributes
1882 are assigned. */
1883 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1884 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1886 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1888 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1889 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1890 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1891 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1892 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1893 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1894 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1895 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1896 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1897 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
1898 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
1899 /* Keep the old RTL. */
1900 COPY_DECL_RTL (olddecl, newdecl);
1902 else if (TREE_CODE (newdecl) == VAR_DECL
1903 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1905 /* Keep the old RTL. We cannot keep the old RTL if the old
1906 declaration was for an incomplete object and the new
1907 declaration is not since many attributes of the RTL will
1908 change. */
1909 COPY_DECL_RTL (olddecl, newdecl);
1912 /* If cannot merge, then use the new type and qualifiers,
1913 and don't preserve the old rtl. */
1914 else
1916 /* Clean out any memory we had of the old declaration. */
1917 tree oldstatic = value_member (olddecl, static_aggregates);
1918 if (oldstatic)
1919 TREE_VALUE (oldstatic) = error_mark_node;
1921 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1922 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1923 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1924 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1927 /* Merge the storage class information. */
1928 merge_weak (newdecl, olddecl);
1930 if (DECL_ONE_ONLY (olddecl))
1931 DECL_COMDAT_GROUP (newdecl) = DECL_COMDAT_GROUP (olddecl);
1933 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1934 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1935 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1936 if (! DECL_EXTERNAL (olddecl))
1937 DECL_EXTERNAL (newdecl) = 0;
1939 new_template_info = NULL_TREE;
1940 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1942 bool new_redefines_gnu_inline = false;
1944 if (new_defines_function
1945 && ((DECL_INTERFACE_KNOWN (olddecl)
1946 && TREE_CODE (olddecl) == FUNCTION_DECL)
1947 || (TREE_CODE (olddecl) == TEMPLATE_DECL
1948 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1949 == FUNCTION_DECL))))
1951 tree fn = olddecl;
1953 if (TREE_CODE (fn) == TEMPLATE_DECL)
1954 fn = DECL_TEMPLATE_RESULT (olddecl);
1956 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
1959 if (!new_redefines_gnu_inline)
1961 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1962 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1963 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1965 DECL_TEMPLATE_INSTANTIATED (newdecl)
1966 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1967 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
1969 /* If the OLDDECL is an instantiation and/or specialization,
1970 then the NEWDECL must be too. But, it may not yet be marked
1971 as such if the caller has created NEWDECL, but has not yet
1972 figured out that it is a redeclaration. */
1973 if (!DECL_USE_TEMPLATE (newdecl))
1974 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
1976 /* Don't really know how much of the language-specific
1977 values we should copy from old to new. */
1978 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
1979 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
1980 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1981 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
1983 if (LANG_DECL_HAS_MIN (newdecl))
1985 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
1986 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
1987 if (DECL_TEMPLATE_INFO (newdecl))
1988 new_template_info = DECL_TEMPLATE_INFO (newdecl);
1989 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
1991 /* Only functions have these fields. */
1992 if (TREE_CODE (newdecl) == FUNCTION_DECL
1993 || DECL_FUNCTION_TEMPLATE_P (newdecl))
1995 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
1996 olddecl_friend = DECL_FRIEND_P (olddecl);
1997 hidden_friend = (DECL_ANTICIPATED (olddecl)
1998 && DECL_HIDDEN_FRIEND_P (olddecl)
1999 && newdecl_is_friend);
2000 DECL_BEFRIENDING_CLASSES (newdecl)
2001 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2002 DECL_BEFRIENDING_CLASSES (olddecl));
2003 /* DECL_THUNKS is only valid for virtual functions,
2004 otherwise it is a DECL_FRIEND_CONTEXT. */
2005 if (DECL_VIRTUAL_P (newdecl))
2006 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
2008 /* Only variables have this field. */
2009 else if (TREE_CODE (newdecl) == VAR_DECL
2010 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2011 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2014 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2016 tree parm;
2018 /* Merge parameter attributes. */
2019 tree oldarg, newarg;
2020 for (oldarg = DECL_ARGUMENTS(olddecl),
2021 newarg = DECL_ARGUMENTS(newdecl);
2022 oldarg && newarg;
2023 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2024 DECL_ATTRIBUTES (newarg)
2025 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2026 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2029 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2030 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2032 /* If newdecl is not a specialization, then it is not a
2033 template-related function at all. And that means that we
2034 should have exited above, returning 0. */
2035 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2037 if (DECL_ODR_USED (olddecl))
2038 /* From [temp.expl.spec]:
2040 If a template, a member template or the member of a class
2041 template is explicitly specialized then that
2042 specialization shall be declared before the first use of
2043 that specialization that would cause an implicit
2044 instantiation to take place, in every translation unit in
2045 which such a use occurs. */
2046 error ("explicit specialization of %qD after first use",
2047 olddecl);
2049 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2051 /* Don't propagate visibility from the template to the
2052 specialization here. We'll do that in determine_visibility if
2053 appropriate. */
2054 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2056 /* [temp.expl.spec/14] We don't inline explicit specialization
2057 just because the primary template says so. */
2059 else if (new_defines_function && DECL_INITIAL (olddecl))
2061 /* Never inline re-defined extern inline functions.
2062 FIXME: this could be better handled by keeping both
2063 function as separate declarations. */
2064 DECL_UNINLINABLE (newdecl) = 1;
2066 else
2068 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
2069 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2071 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2073 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2074 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2076 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2077 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2078 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2079 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2082 /* Preserve abstractness on cloned [cd]tors. */
2083 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
2085 /* Update newdecl's parms to point at olddecl. */
2086 for (parm = DECL_ARGUMENTS (newdecl); parm;
2087 parm = DECL_CHAIN (parm))
2088 DECL_CONTEXT (parm) = olddecl;
2090 if (! types_match)
2092 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2093 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2094 COPY_DECL_RTL (newdecl, olddecl);
2096 if (! types_match || new_defines_function)
2098 /* These need to be copied so that the names are available.
2099 Note that if the types do match, we'll preserve inline
2100 info and other bits, but if not, we won't. */
2101 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2102 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2104 if (new_defines_function)
2105 /* If defining a function declared with other language
2106 linkage, use the previously declared language linkage. */
2107 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2108 else if (types_match)
2110 /* If redeclaring a builtin function, and not a definition,
2111 it stays built in. */
2112 if (DECL_BUILT_IN (olddecl))
2114 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2115 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2116 /* If we're keeping the built-in definition, keep the rtl,
2117 regardless of declaration matches. */
2118 COPY_DECL_RTL (olddecl, newdecl);
2121 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2122 /* Don't clear out the arguments if we're just redeclaring a
2123 function. */
2124 if (DECL_ARGUMENTS (olddecl))
2125 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2128 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2129 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2131 /* Now preserve various other info from the definition. */
2132 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2133 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2134 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2135 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2137 /* Warn about conflicting visibility specifications. */
2138 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2139 && DECL_VISIBILITY_SPECIFIED (newdecl)
2140 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2142 warning_at (input_location, OPT_Wattributes,
2143 "%q+D: visibility attribute ignored because it", newdecl);
2144 warning_at (DECL_SOURCE_LOCATION (olddecl), OPT_Wattributes,
2145 "conflicts with previous declaration here");
2147 /* Choose the declaration which specified visibility. */
2148 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2150 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2151 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2153 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2154 so keep this behavior. */
2155 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
2157 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2158 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2160 /* Likewise for DECL_USER_ALIGN and DECL_PACKED. */
2161 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2162 if (TREE_CODE (newdecl) == FIELD_DECL)
2163 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2165 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2166 with that from NEWDECL below. */
2167 if (DECL_LANG_SPECIFIC (olddecl))
2169 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2170 != DECL_LANG_SPECIFIC (newdecl));
2171 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2174 /* Merge the USED information. */
2175 if (TREE_USED (olddecl))
2176 TREE_USED (newdecl) = 1;
2177 else if (TREE_USED (newdecl))
2178 TREE_USED (olddecl) = 1;
2179 if (TREE_CODE (newdecl) == VAR_DECL)
2181 if (DECL_READ_P (olddecl))
2182 DECL_READ_P (newdecl) = 1;
2183 else if (DECL_READ_P (newdecl))
2184 DECL_READ_P (olddecl) = 1;
2186 if (DECL_PRESERVE_P (olddecl))
2187 DECL_PRESERVE_P (newdecl) = 1;
2188 else if (DECL_PRESERVE_P (newdecl))
2189 DECL_PRESERVE_P (olddecl) = 1;
2191 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2193 int function_size;
2195 function_size = sizeof (struct tree_decl_common);
2197 memcpy ((char *) olddecl + sizeof (struct tree_common),
2198 (char *) newdecl + sizeof (struct tree_common),
2199 function_size - sizeof (struct tree_common));
2201 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2202 (char *) newdecl + sizeof (struct tree_decl_common),
2203 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2204 if (new_template_info)
2205 /* If newdecl is a template instantiation, it is possible that
2206 the following sequence of events has occurred:
2208 o A friend function was declared in a class template. The
2209 class template was instantiated.
2211 o The instantiation of the friend declaration was
2212 recorded on the instantiation list, and is newdecl.
2214 o Later, however, instantiate_class_template called pushdecl
2215 on the newdecl to perform name injection. But, pushdecl in
2216 turn called duplicate_decls when it discovered that another
2217 declaration of a global function with the same name already
2218 existed.
2220 o Here, in duplicate_decls, we decided to clobber newdecl.
2222 If we're going to do that, we'd better make sure that
2223 olddecl, and not newdecl, is on the list of
2224 instantiations so that if we try to do the instantiation
2225 again we won't get the clobbered declaration. */
2226 reregister_specialization (newdecl,
2227 new_template_info,
2228 olddecl);
2230 else
2232 size_t size = tree_code_size (TREE_CODE (olddecl));
2233 memcpy ((char *) olddecl + sizeof (struct tree_common),
2234 (char *) newdecl + sizeof (struct tree_common),
2235 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2236 switch (TREE_CODE (olddecl))
2238 case LABEL_DECL:
2239 case VAR_DECL:
2240 case RESULT_DECL:
2241 case PARM_DECL:
2242 case FIELD_DECL:
2243 case TYPE_DECL:
2244 case CONST_DECL:
2246 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2247 (char *) newdecl + sizeof (struct tree_decl_common),
2248 size - sizeof (struct tree_decl_common)
2249 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2251 break;
2252 default:
2253 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2254 (char *) newdecl + sizeof (struct tree_decl_common),
2255 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2256 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2257 break;
2260 DECL_UID (olddecl) = olddecl_uid;
2261 if (olddecl_friend)
2262 DECL_FRIEND_P (olddecl) = 1;
2263 if (hidden_friend)
2265 DECL_ANTICIPATED (olddecl) = 1;
2266 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2269 /* NEWDECL contains the merged attribute lists.
2270 Update OLDDECL to be the same. */
2271 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2273 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2274 so that encode_section_info has a chance to look at the new decl
2275 flags and attributes. */
2276 if (DECL_RTL_SET_P (olddecl)
2277 && (TREE_CODE (olddecl) == FUNCTION_DECL
2278 || (TREE_CODE (olddecl) == VAR_DECL
2279 && TREE_STATIC (olddecl))))
2280 make_decl_rtl (olddecl);
2282 /* The NEWDECL will no longer be needed. Because every out-of-class
2283 declaration of a member results in a call to duplicate_decls,
2284 freeing these nodes represents in a significant savings. */
2285 ggc_free (newdecl);
2287 return olddecl;
2291 /* Wrapper for duplicate_decls_internal used by PPH support to
2292 decide whether NEWDECL or OLDDECL should be removed from the
2293 AST catching data structures. This is necessary when NEWDECL
2294 is a re-declaration of OLDDECL. */
2296 tree
2297 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
2299 tree gooddecl, baddecl;
2301 gooddecl = duplicate_decls_internal (newdecl, olddecl, newdecl_is_friend);
2303 /* If duplicate_decls_internal returns NULL, it means that NEWDECL
2304 is not a re-declaration of OLDDECL, so nothing needs to be done
2305 in that case. */
2306 if (gooddecl == NULL_TREE)
2307 return NULL_TREE;
2309 baddecl = (gooddecl == newdecl) ? olddecl : newdecl;
2310 if (lang_hooks.pph_uncatch_tree)
2311 lang_hooks.pph_uncatch_tree (baddecl);
2313 if (lang_hooks.pph_catch_tree)
2314 lang_hooks.pph_catch_tree (gooddecl);
2316 return gooddecl;
2319 /* Return zero if the declaration NEWDECL is valid
2320 when the declaration OLDDECL (assumed to be for the same name)
2321 has already been seen.
2322 Otherwise return an error message format string with a %s
2323 where the identifier should go. */
2325 static const char *
2326 redeclaration_error_message (tree newdecl, tree olddecl)
2328 if (TREE_CODE (newdecl) == TYPE_DECL)
2330 /* Because C++ can put things into name space for free,
2331 constructs like "typedef struct foo { ... } foo"
2332 would look like an erroneous redeclaration. */
2333 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2334 return NULL;
2335 else
2336 return G_("redefinition of %q#D");
2338 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2340 /* If this is a pure function, its olddecl will actually be
2341 the original initialization to `0' (which we force to call
2342 abort()). Don't complain about redefinition in this case. */
2343 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2344 && DECL_INITIAL (olddecl) == NULL_TREE)
2345 return NULL;
2347 /* If both functions come from different namespaces, this is not
2348 a redeclaration - this is a conflict with a used function. */
2349 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2350 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2351 && ! decls_match (olddecl, newdecl))
2352 return G_("%qD conflicts with used function");
2354 /* We'll complain about linkage mismatches in
2355 warn_extern_redeclared_static. */
2357 /* Defining the same name twice is no good. */
2358 if (DECL_INITIAL (olddecl) != NULL_TREE
2359 && DECL_INITIAL (newdecl) != NULL_TREE)
2361 if (DECL_NAME (olddecl) == NULL_TREE)
2362 return G_("%q#D not declared in class");
2363 else if (!GNU_INLINE_P (olddecl)
2364 || GNU_INLINE_P (newdecl))
2365 return G_("redefinition of %q#D");
2368 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2370 bool olda = GNU_INLINE_P (olddecl);
2371 bool newa = GNU_INLINE_P (newdecl);
2373 if (olda != newa)
2375 if (newa)
2376 return G_("%q+D redeclared inline with "
2377 "%<gnu_inline%> attribute");
2378 else
2379 return G_("%q+D redeclared inline without "
2380 "%<gnu_inline%> attribute");
2384 return NULL;
2386 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2388 tree nt, ot;
2390 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2392 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2393 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2394 return G_("redefinition of %q#D");
2395 return NULL;
2398 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2399 || (DECL_TEMPLATE_RESULT (newdecl)
2400 == DECL_TEMPLATE_RESULT (olddecl)))
2401 return NULL;
2403 nt = DECL_TEMPLATE_RESULT (newdecl);
2404 if (DECL_TEMPLATE_INFO (nt))
2405 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2406 ot = DECL_TEMPLATE_RESULT (olddecl);
2407 if (DECL_TEMPLATE_INFO (ot))
2408 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2409 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2410 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2411 return G_("redefinition of %q#D");
2413 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2415 bool olda = GNU_INLINE_P (ot);
2416 bool newa = GNU_INLINE_P (nt);
2418 if (olda != newa)
2420 if (newa)
2421 return G_("%q+D redeclared inline with "
2422 "%<gnu_inline%> attribute");
2423 else
2424 return G_("%q+D redeclared inline without "
2425 "%<gnu_inline%> attribute");
2429 /* Core issue #226 (C++0x):
2431 If a friend function template declaration specifies a
2432 default template-argument, that declaration shall be a
2433 definition and shall be the only declaration of the
2434 function template in the translation unit. */
2435 if ((cxx_dialect != cxx98)
2436 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2437 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2438 /*is_primary=*/1, /*is_partial=*/0,
2439 /*is_friend_decl=*/2))
2440 return G_("redeclaration of friend %q#D "
2441 "may not have default template arguments");
2443 return NULL;
2445 else if (TREE_CODE (newdecl) == VAR_DECL
2446 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2447 && (! DECL_LANG_SPECIFIC (olddecl)
2448 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2449 || DECL_THREAD_LOCAL_P (newdecl)))
2451 /* Only variables can be thread-local, and all declarations must
2452 agree on this property. */
2453 if (DECL_THREAD_LOCAL_P (newdecl))
2454 return G_("thread-local declaration of %q#D follows "
2455 "non-thread-local declaration");
2456 else
2457 return G_("non-thread-local declaration of %q#D follows "
2458 "thread-local declaration");
2460 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2462 /* The objects have been declared at namespace scope. If either
2463 is a member of an anonymous union, then this is an invalid
2464 redeclaration. For example:
2466 int i;
2467 union { int i; };
2469 is invalid. */
2470 if ((TREE_CODE (newdecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (newdecl))
2471 || (TREE_CODE (olddecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (olddecl)))
2472 return G_("redeclaration of %q#D");
2473 /* If at least one declaration is a reference, there is no
2474 conflict. For example:
2476 int i = 3;
2477 extern int i;
2479 is valid. */
2480 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2481 return NULL;
2482 /* Reject two definitions. */
2483 return G_("redefinition of %q#D");
2485 else
2487 /* Objects declared with block scope: */
2488 /* Reject two definitions, and reject a definition
2489 together with an external reference. */
2490 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2491 return G_("redeclaration of %q#D");
2492 return NULL;
2496 /* Hash and equality functions for the named_label table. */
2498 static hashval_t
2499 named_label_entry_hash (const void *data)
2501 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2502 return DECL_UID (ent->label_decl);
2505 static int
2506 named_label_entry_eq (const void *a, const void *b)
2508 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2509 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2510 return ent_a->label_decl == ent_b->label_decl;
2513 /* Create a new label, named ID. */
2515 static tree
2516 make_label_decl (tree id, int local_p)
2518 struct named_label_entry *ent;
2519 void **slot;
2520 tree decl;
2522 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2524 DECL_CONTEXT (decl) = current_function_decl;
2525 DECL_MODE (decl) = VOIDmode;
2526 C_DECLARED_LABEL_FLAG (decl) = local_p;
2528 /* Say where one reference is to the label, for the sake of the
2529 error if it is not defined. */
2530 DECL_SOURCE_LOCATION (decl) = input_location;
2532 /* Record the fact that this identifier is bound to this label. */
2533 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2535 /* Create the label htab for the function on demand. */
2536 if (!named_labels)
2537 named_labels = htab_create_ggc (13, named_label_entry_hash,
2538 named_label_entry_eq, NULL);
2540 /* Record this label on the list of labels used in this function.
2541 We do this before calling make_label_decl so that we get the
2542 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2543 ent = ggc_alloc_cleared_named_label_entry ();
2544 ent->label_decl = decl;
2546 slot = htab_find_slot (named_labels, ent, INSERT);
2547 gcc_assert (*slot == NULL);
2548 *slot = ent;
2550 return decl;
2553 /* Look for a label named ID in the current function. If one cannot
2554 be found, create one. (We keep track of used, but undefined,
2555 labels, and complain about them at the end of a function.) */
2557 static tree
2558 lookup_label_1 (tree id)
2560 tree decl;
2562 timevar_push (TV_NAME_LOOKUP);
2563 /* You can't use labels at global scope. */
2564 if (current_function_decl == NULL_TREE)
2566 error ("label %qE referenced outside of any function", id);
2567 return NULL_TREE;
2570 /* See if we've already got this label. */
2571 decl = IDENTIFIER_LABEL_VALUE (id);
2572 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2573 return decl;
2575 decl = make_label_decl (id, /*local_p=*/0);
2576 return decl;
2579 /* Wrapper for lookup_label_1. */
2581 tree
2582 lookup_label (tree id)
2584 tree ret;
2585 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2586 ret = lookup_label_1 (id);
2587 pph_catch_name_lookup (ret);
2588 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2589 return ret;
2593 /* Declare a local label named ID. */
2595 tree
2596 declare_local_label (tree id)
2598 tree decl;
2599 cp_label_binding *bind;
2601 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2602 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2603 bind = VEC_safe_push (cp_label_binding, gc,
2604 current_binding_level->shadowed_labels, NULL);
2605 bind->prev_value = IDENTIFIER_LABEL_VALUE (id);
2607 decl = make_label_decl (id, /*local_p=*/1);
2608 bind->label = decl;
2610 return decl;
2613 /* Returns nonzero if it is ill-formed to jump past the declaration of
2614 DECL. Returns 2 if it's also a real problem. */
2616 static int
2617 decl_jump_unsafe (tree decl)
2619 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
2620 with automatic storage duration is not in scope to a point where it is
2621 in scope is ill-formed unless the variable has scalar type, class type
2622 with a trivial default constructor and a trivial destructor, a
2623 cv-qualified version of one of these types, or an array of one of the
2624 preceding types and is declared without an initializer (8.5). */
2625 tree type = TREE_TYPE (decl);
2627 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2628 || type == error_mark_node)
2629 return 0;
2631 type = strip_array_types (type);
2633 if (type_has_nontrivial_default_init (TREE_TYPE (decl))
2634 || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2635 return 2;
2637 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2638 return 1;
2640 return 0;
2643 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2645 static void
2646 identify_goto (tree decl, const location_t *locus)
2648 if (decl)
2649 permerror (input_location, "jump to label %qD", decl);
2650 else
2651 permerror (input_location, "jump to case label");
2652 if (locus)
2653 permerror (*locus, " from here");
2656 /* Check that a single previously seen jump to a newly defined label
2657 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2658 the jump context; NAMES are the names in scope in LEVEL at the jump
2659 context; LOCUS is the source position of the jump or 0. Returns
2660 true if all is well. */
2662 static bool
2663 check_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names,
2664 bool exited_omp, const location_t *locus)
2666 struct cp_binding_level *b;
2667 bool identified = false, saw_eh = false, saw_omp = false;
2669 if (exited_omp)
2671 identify_goto (decl, locus);
2672 error (" exits OpenMP structured block");
2673 identified = saw_omp = true;
2676 for (b = current_binding_level; b ; b = b->level_chain)
2678 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2680 for (new_decls = b->names; new_decls != old_decls;
2681 new_decls = DECL_CHAIN (new_decls))
2683 int problem = decl_jump_unsafe (new_decls);
2684 if (! problem)
2685 continue;
2687 if (!identified)
2689 identify_goto (decl, locus);
2690 identified = true;
2692 if (problem > 1)
2693 error (" crosses initialization of %q+#D", new_decls);
2694 else
2695 permerror (input_location, " enters scope of %q+#D which has "
2696 "non-trivial destructor", new_decls);
2699 if (b == level)
2700 break;
2701 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2703 if (!identified)
2705 identify_goto (decl, locus);
2706 identified = true;
2708 if (b->kind == sk_try)
2709 error (" enters try block");
2710 else
2711 error (" enters catch block");
2712 saw_eh = true;
2714 if (b->kind == sk_omp && !saw_omp)
2716 if (!identified)
2718 identify_goto (decl, locus);
2719 identified = true;
2721 error (" enters OpenMP structured block");
2722 saw_omp = true;
2726 return !identified;
2729 static void
2730 check_previous_goto (tree decl, struct named_label_use_entry *use)
2732 check_previous_goto_1 (decl, use->binding_level,
2733 use->names_in_scope, use->in_omp_scope,
2734 &use->o_goto_locus);
2737 static bool
2738 check_switch_goto (struct cp_binding_level* level)
2740 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2743 /* Check that a new jump to a label DECL is OK. Called by
2744 finish_goto_stmt. */
2746 void
2747 check_goto (tree decl)
2749 struct named_label_entry *ent, dummy;
2750 bool saw_catch = false, identified = false;
2751 tree bad;
2752 unsigned ix;
2754 /* We can't know where a computed goto is jumping.
2755 So we assume that it's OK. */
2756 if (TREE_CODE (decl) != LABEL_DECL)
2757 return;
2759 /* We didn't record any information about this label when we created it,
2760 and there's not much point since it's trivial to analyze as a return. */
2761 if (decl == cdtor_label)
2762 return;
2764 dummy.label_decl = decl;
2765 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2766 gcc_assert (ent != NULL);
2768 /* If the label hasn't been defined yet, defer checking. */
2769 if (! DECL_INITIAL (decl))
2771 struct named_label_use_entry *new_use;
2773 /* Don't bother creating another use if the last goto had the
2774 same data, and will therefore create the same set of errors. */
2775 if (ent->uses
2776 && ent->uses->names_in_scope == current_binding_level->names)
2777 return;
2779 new_use = ggc_alloc_named_label_use_entry ();
2780 new_use->binding_level = current_binding_level;
2781 new_use->names_in_scope = current_binding_level->names;
2782 new_use->o_goto_locus = input_location;
2783 new_use->in_omp_scope = false;
2785 new_use->next = ent->uses;
2786 ent->uses = new_use;
2787 return;
2790 if (ent->in_try_scope || ent->in_catch_scope
2791 || ent->in_omp_scope || !VEC_empty (tree, ent->bad_decls))
2793 permerror (input_location, "jump to label %q+D", decl);
2794 permerror (input_location, " from here");
2795 identified = true;
2798 FOR_EACH_VEC_ELT (tree, ent->bad_decls, ix, bad)
2800 int u = decl_jump_unsafe (bad);
2802 if (u > 1 && DECL_ARTIFICIAL (bad))
2804 /* Can't skip init of __exception_info. */
2805 error_at (DECL_SOURCE_LOCATION (bad), " enters catch block");
2806 saw_catch = true;
2808 else if (u > 1)
2809 error (" skips initialization of %q+#D", bad);
2810 else
2811 permerror (input_location, " enters scope of %q+#D which has "
2812 "non-trivial destructor", bad);
2815 if (ent->in_try_scope)
2816 error (" enters try block");
2817 else if (ent->in_catch_scope && !saw_catch)
2818 error (" enters catch block");
2820 if (ent->in_omp_scope)
2821 error (" enters OpenMP structured block");
2822 else if (flag_openmp)
2824 struct cp_binding_level *b;
2825 for (b = current_binding_level; b ; b = b->level_chain)
2827 if (b == ent->binding_level)
2828 break;
2829 if (b->kind == sk_omp)
2831 if (!identified)
2833 permerror (input_location, "jump to label %q+D", decl);
2834 permerror (input_location, " from here");
2835 identified = true;
2837 error (" exits OpenMP structured block");
2838 break;
2844 /* Check that a return is ok wrt OpenMP structured blocks.
2845 Called by finish_return_stmt. Returns true if all is well. */
2847 bool
2848 check_omp_return (void)
2850 struct cp_binding_level *b;
2851 for (b = current_binding_level; b ; b = b->level_chain)
2852 if (b->kind == sk_omp)
2854 error ("invalid exit from OpenMP structured block");
2855 return false;
2857 return true;
2860 /* Define a label, specifying the location in the source file.
2861 Return the LABEL_DECL node for the label. */
2863 static tree
2864 define_label_1 (location_t location, tree name)
2866 struct named_label_entry *ent, dummy;
2867 struct cp_binding_level *p;
2868 tree decl;
2870 decl = lookup_label (name);
2872 dummy.label_decl = decl;
2873 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2874 gcc_assert (ent != NULL);
2876 /* After labels, make any new cleanups in the function go into their
2877 own new (temporary) binding contour. */
2878 for (p = current_binding_level;
2879 p->kind != sk_function_parms;
2880 p = p->level_chain)
2881 p->more_cleanups_ok = 0;
2883 if (name == get_identifier ("wchar_t"))
2884 permerror (input_location, "label named wchar_t");
2886 if (DECL_INITIAL (decl) != NULL_TREE)
2888 error ("duplicate label %qD", decl);
2889 return error_mark_node;
2891 else
2893 struct named_label_use_entry *use;
2895 /* Mark label as having been defined. */
2896 DECL_INITIAL (decl) = error_mark_node;
2897 /* Say where in the source. */
2898 DECL_SOURCE_LOCATION (decl) = location;
2900 ent->binding_level = current_binding_level;
2901 ent->names_in_scope = current_binding_level->names;
2903 for (use = ent->uses; use ; use = use->next)
2904 check_previous_goto (decl, use);
2905 ent->uses = NULL;
2908 return decl;
2911 /* Wrapper for define_label_1. */
2913 tree
2914 define_label (location_t location, tree name)
2916 tree ret;
2917 timevar_start (TV_NAME_LOOKUP);
2918 ret = define_label_1 (location, name);
2919 pph_catch_name_lookup (ret);
2920 timevar_stop (TV_NAME_LOOKUP);
2921 return ret;
2925 struct cp_switch
2927 struct cp_binding_level *level;
2928 struct cp_switch *next;
2929 /* The SWITCH_STMT being built. */
2930 tree switch_stmt;
2931 /* A splay-tree mapping the low element of a case range to the high
2932 element, or NULL_TREE if there is no high element. Used to
2933 determine whether or not a new case label duplicates an old case
2934 label. We need a tree, rather than simply a hash table, because
2935 of the GNU case range extension. */
2936 splay_tree cases;
2939 /* A stack of the currently active switch statements. The innermost
2940 switch statement is on the top of the stack. There is no need to
2941 mark the stack for garbage collection because it is only active
2942 during the processing of the body of a function, and we never
2943 collect at that point. */
2945 static struct cp_switch *switch_stack;
2947 /* Called right after a switch-statement condition is parsed.
2948 SWITCH_STMT is the switch statement being parsed. */
2950 void
2951 push_switch (tree switch_stmt)
2953 struct cp_switch *p = XNEW (struct cp_switch);
2954 p->level = current_binding_level;
2955 p->next = switch_stack;
2956 p->switch_stmt = switch_stmt;
2957 p->cases = splay_tree_new (case_compare, NULL, NULL);
2958 switch_stack = p;
2961 void
2962 pop_switch (void)
2964 struct cp_switch *cs = switch_stack;
2965 location_t switch_location;
2967 /* Emit warnings as needed. */
2968 switch_location = EXPR_LOC_OR_HERE (cs->switch_stmt);
2969 if (!processing_template_decl)
2970 c_do_switch_warnings (cs->cases, switch_location,
2971 SWITCH_STMT_TYPE (cs->switch_stmt),
2972 SWITCH_STMT_COND (cs->switch_stmt));
2974 splay_tree_delete (cs->cases);
2975 switch_stack = switch_stack->next;
2976 free (cs);
2979 /* Note that we've seen a definition of a case label, and complain if this
2980 is a bad place for one. */
2982 tree
2983 finish_case_label (location_t loc, tree low_value, tree high_value)
2985 tree cond, r;
2986 struct cp_binding_level *p;
2988 if (processing_template_decl)
2990 tree label;
2992 /* For templates, just add the case label; we'll do semantic
2993 analysis at instantiation-time. */
2994 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
2995 return add_stmt (build_case_label (loc, low_value, high_value, label));
2998 /* Find the condition on which this switch statement depends. */
2999 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3000 if (cond && TREE_CODE (cond) == TREE_LIST)
3001 cond = TREE_VALUE (cond);
3003 if (!check_switch_goto (switch_stack->level))
3004 return error_mark_node;
3006 if (low_value)
3007 low_value = cxx_constant_value (low_value);
3008 if (high_value)
3009 high_value = cxx_constant_value (high_value);
3011 r = c_add_case_label (loc, switch_stack->cases, cond,
3012 SWITCH_STMT_TYPE (switch_stack->switch_stmt),
3013 low_value, high_value);
3015 /* After labels, make any new cleanups in the function go into their
3016 own new (temporary) binding contour. */
3017 for (p = current_binding_level;
3018 p->kind != sk_function_parms;
3019 p = p->level_chain)
3020 p->more_cleanups_ok = 0;
3022 return r;
3025 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
3027 static hashval_t
3028 typename_hash (const void* k)
3030 hashval_t hash;
3031 const_tree const t = (const_tree) k;
3033 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3034 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
3036 return hash;
3039 typedef struct typename_info {
3040 tree scope;
3041 tree name;
3042 tree template_id;
3043 bool enum_p;
3044 bool class_p;
3045 } typename_info;
3047 /* Compare two TYPENAME_TYPEs. K1 is really of type `tree', K2 is
3048 really of type `typename_info*' */
3050 static int
3051 typename_compare (const void * k1, const void * k2)
3053 const_tree const t1 = (const_tree) k1;
3054 const typename_info *const t2 = (const typename_info *) k2;
3056 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
3057 && TYPE_CONTEXT (t1) == t2->scope
3058 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3059 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3060 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3063 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3064 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3066 Returns the new TYPENAME_TYPE. */
3068 static GTY ((param_is (union tree_node))) htab_t typename_htab;
3070 static tree
3071 build_typename_type (tree context, tree name, tree fullname,
3072 enum tag_types tag_type)
3074 tree t;
3075 tree d;
3076 typename_info ti;
3077 void **e;
3078 hashval_t hash;
3080 if (typename_htab == NULL)
3081 typename_htab = htab_create_ggc (61, &typename_hash,
3082 &typename_compare, NULL);
3084 ti.scope = FROB_CONTEXT (context);
3085 ti.name = name;
3086 ti.template_id = fullname;
3087 ti.enum_p = tag_type == enum_type;
3088 ti.class_p = (tag_type == class_type
3089 || tag_type == record_type
3090 || tag_type == union_type);
3091 hash = (htab_hash_pointer (ti.scope)
3092 ^ htab_hash_pointer (ti.name));
3094 /* See if we already have this type. */
3095 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
3096 if (*e)
3097 t = (tree) *e;
3098 else
3100 /* Build the TYPENAME_TYPE. */
3101 t = cxx_make_type (TYPENAME_TYPE);
3102 TYPE_CONTEXT (t) = ti.scope;
3103 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3104 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3105 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3107 /* Build the corresponding TYPE_DECL. */
3108 d = build_decl (input_location, TYPE_DECL, name, t);
3109 TYPE_NAME (TREE_TYPE (d)) = d;
3110 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3111 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3112 DECL_ARTIFICIAL (d) = 1;
3114 /* Store it in the hash table. */
3115 *e = t;
3117 /* TYPENAME_TYPEs must always be compared structurally, because
3118 they may or may not resolve down to another type depending on
3119 the currently open classes. */
3120 SET_TYPE_STRUCTURAL_EQUALITY (t);
3123 return t;
3126 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3127 provided to name the type. Returns an appropriate type, unless an
3128 error occurs, in which case error_mark_node is returned. If we
3129 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3130 return that, rather than the _TYPE it corresponds to, in other
3131 cases we look through the type decl. If TF_ERROR is set, complain
3132 about errors, otherwise be quiet. */
3134 tree
3135 make_typename_type (tree context, tree name, enum tag_types tag_type,
3136 tsubst_flags_t complain)
3138 tree fullname;
3139 tree t;
3140 bool want_template;
3142 if (name == error_mark_node
3143 || context == NULL_TREE
3144 || context == error_mark_node)
3145 return error_mark_node;
3147 if (TYPE_P (name))
3149 if (!(TYPE_LANG_SPECIFIC (name)
3150 && (CLASSTYPE_IS_TEMPLATE (name)
3151 || CLASSTYPE_USE_TEMPLATE (name))))
3152 name = TYPE_IDENTIFIER (name);
3153 else
3154 /* Create a TEMPLATE_ID_EXPR for the type. */
3155 name = build_nt (TEMPLATE_ID_EXPR,
3156 CLASSTYPE_TI_TEMPLATE (name),
3157 CLASSTYPE_TI_ARGS (name));
3159 else if (TREE_CODE (name) == TYPE_DECL)
3160 name = DECL_NAME (name);
3162 fullname = name;
3164 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3166 name = TREE_OPERAND (name, 0);
3167 if (TREE_CODE (name) == TEMPLATE_DECL)
3168 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3169 else if (TREE_CODE (name) == OVERLOAD)
3171 error ("%qD is not a type", name);
3172 return error_mark_node;
3175 if (TREE_CODE (name) == TEMPLATE_DECL)
3177 error ("%qD used without template parameters", name);
3178 return error_mark_node;
3180 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3181 gcc_assert (TYPE_P (context));
3183 if (!MAYBE_CLASS_TYPE_P (context))
3185 if (complain & tf_error)
3186 error ("%q#T is not a class", context);
3187 return error_mark_node;
3190 /* When the CONTEXT is a dependent type, NAME could refer to a
3191 dependent base class of CONTEXT. But look inside it anyway
3192 if CONTEXT is a currently open scope, in case it refers to a
3193 member of the current instantiation or a non-dependent base;
3194 lookup will stop when we hit a dependent base. */
3195 if (!dependent_scope_p (context))
3196 /* We should only set WANT_TYPE when we're a nested typename type.
3197 Then we can give better diagnostics if we find a non-type. */
3198 t = lookup_field (context, name, 2, /*want_type=*/true);
3199 else
3200 t = NULL_TREE;
3202 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3203 return build_typename_type (context, name, fullname, tag_type);
3205 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3207 if (!t)
3209 if (complain & tf_error)
3210 error (want_template ? "no class template named %q#T in %q#T"
3211 : "no type named %q#T in %q#T", name, context);
3212 return error_mark_node;
3215 /* Pull out the template from an injected-class-name (or multiple). */
3216 if (want_template)
3217 t = maybe_get_template_decl_from_type_decl (t);
3219 if (TREE_CODE (t) == TREE_LIST)
3221 if (complain & tf_error)
3223 error ("lookup of %qT in %qT is ambiguous", name, context);
3224 print_candidates (t);
3226 return error_mark_node;
3229 if (want_template && !DECL_CLASS_TEMPLATE_P (t))
3231 if (complain & tf_error)
3232 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3233 context, name, t);
3234 return error_mark_node;
3236 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3238 if (complain & tf_error)
3239 error ("%<typename %T::%D%> names %q#T, which is not a type",
3240 context, name, t);
3241 return error_mark_node;
3244 if (complain & tf_error)
3245 perform_or_defer_access_check (TYPE_BINFO (context), t, t);
3247 /* If we are currently parsing a template and if T is a typedef accessed
3248 through CONTEXT then we need to remember and check access of T at
3249 template instantiation time. */
3250 add_typedef_to_current_template_for_access_check (t, context, input_location);
3252 if (want_template)
3253 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3254 NULL_TREE, context,
3255 /*entering_scope=*/0,
3256 tf_warning_or_error | tf_user);
3258 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3259 t = TREE_TYPE (t);
3261 return t;
3264 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3265 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3266 in which case error_mark_node is returned.
3268 If PARM_LIST is non-NULL, also make sure that the template parameter
3269 list of TEMPLATE_DECL matches.
3271 If COMPLAIN zero, don't complain about any errors that occur. */
3273 tree
3274 make_unbound_class_template (tree context, tree name, tree parm_list,
3275 tsubst_flags_t complain)
3277 tree t;
3278 tree d;
3280 if (TYPE_P (name))
3281 name = TYPE_IDENTIFIER (name);
3282 else if (DECL_P (name))
3283 name = DECL_NAME (name);
3284 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3286 if (!dependent_type_p (context)
3287 || currently_open_class (context))
3289 tree tmpl = NULL_TREE;
3291 if (MAYBE_CLASS_TYPE_P (context))
3292 tmpl = lookup_field (context, name, 0, false);
3294 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3295 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3297 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
3299 if (complain & tf_error)
3300 error ("no class template named %q#T in %q#T", name, context);
3301 return error_mark_node;
3304 if (parm_list
3305 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3307 if (complain & tf_error)
3309 error ("template parameters do not match template");
3310 error ("%q+D declared here", tmpl);
3312 return error_mark_node;
3315 if (complain & tf_error)
3316 perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl);
3318 return tmpl;
3321 /* Build the UNBOUND_CLASS_TEMPLATE. */
3322 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3323 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3324 TREE_TYPE (t) = NULL_TREE;
3325 SET_TYPE_STRUCTURAL_EQUALITY (t);
3327 /* Build the corresponding TEMPLATE_DECL. */
3328 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3329 TYPE_NAME (TREE_TYPE (d)) = d;
3330 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3331 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3332 DECL_ARTIFICIAL (d) = 1;
3333 DECL_TEMPLATE_PARMS (d) = parm_list;
3335 return t;
3340 /* Push the declarations of builtin types into the namespace.
3341 RID_INDEX is the index of the builtin type in the array
3342 RID_POINTERS. NAME is the name used when looking up the builtin
3343 type. TYPE is the _TYPE node for the builtin type. */
3345 void
3346 record_builtin_type (enum rid rid_index,
3347 const char* name,
3348 tree type)
3350 tree rname = NULL_TREE, tname = NULL_TREE;
3351 tree tdecl = NULL_TREE;
3353 if ((int) rid_index < (int) RID_MAX)
3354 rname = ridpointers[(int) rid_index];
3355 if (name)
3356 tname = get_identifier (name);
3358 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3359 eliminated. Built-in types should not be looked up name; their
3360 names are keywords that the parser can recognize. However, there
3361 is code in c-common.c that uses identifier_global_value to look
3362 up built-in types by name. */
3363 if (tname)
3365 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3366 DECL_ARTIFICIAL (tdecl) = 1;
3367 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3369 if (rname)
3371 if (!tdecl)
3373 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3374 DECL_ARTIFICIAL (tdecl) = 1;
3376 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3379 if (!TYPE_NAME (type))
3380 TYPE_NAME (type) = tdecl;
3382 if (tdecl)
3383 debug_hooks->type_decl (tdecl, 0);
3386 /* Record one of the standard Java types.
3387 * Declare it as having the given NAME.
3388 * If SIZE > 0, it is the size of one of the integral types;
3389 * otherwise it is the negative of the size of one of the other types. */
3391 static tree
3392 record_builtin_java_type (const char* name, int size)
3394 tree type, decl;
3395 if (size > 0)
3397 type = build_nonstandard_integer_type (size, 0);
3398 type = build_distinct_type_copy (type);
3400 else if (size > -32)
3402 tree stype;
3403 /* "__java_char" or ""__java_boolean". */
3404 type = build_nonstandard_integer_type (-size, 1);
3405 type = build_distinct_type_copy (type);
3406 /* Get the signed type cached and attached to the unsigned type,
3407 so it doesn't get garbage-collected at "random" times,
3408 causing potential codegen differences out of different UIDs
3409 and different alias set numbers. */
3410 stype = build_nonstandard_integer_type (-size, 0);
3411 stype = build_distinct_type_copy (stype);
3412 TREE_CHAIN (type) = stype;
3413 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3415 else
3416 { /* "__java_float" or ""__java_double". */
3417 type = make_node (REAL_TYPE);
3418 TYPE_PRECISION (type) = - size;
3419 layout_type (type);
3421 record_builtin_type (RID_MAX, name, type);
3422 decl = TYPE_NAME (type);
3424 /* Suppress generate debug symbol entries for these types,
3425 since for normal C++ they are just clutter.
3426 However, push_lang_context undoes this if extern "Java" is seen. */
3427 DECL_IGNORED_P (decl) = 1;
3429 TYPE_FOR_JAVA (type) = 1;
3430 return type;
3433 /* Push a type into the namespace so that the back ends ignore it. */
3435 static void
3436 record_unknown_type (tree type, const char* name)
3438 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3439 TYPE_DECL, get_identifier (name), type));
3440 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3441 DECL_IGNORED_P (decl) = 1;
3442 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3443 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3444 TYPE_ALIGN (type) = 1;
3445 TYPE_USER_ALIGN (type) = 0;
3446 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3449 /* A string for which we should create an IDENTIFIER_NODE at
3450 startup. */
3452 typedef struct predefined_identifier
3454 /* The name of the identifier. */
3455 const char *const name;
3456 /* The place where the IDENTIFIER_NODE should be stored. */
3457 tree *const node;
3458 /* Nonzero if this is the name of a constructor or destructor. */
3459 const int ctor_or_dtor_p;
3460 } predefined_identifier;
3462 /* Create all the predefined identifiers. */
3464 static void
3465 initialize_predefined_identifiers (void)
3467 const predefined_identifier *pid;
3469 /* A table of identifiers to create at startup. */
3470 static const predefined_identifier predefined_identifiers[] = {
3471 { "C++", &lang_name_cplusplus, 0 },
3472 { "C", &lang_name_c, 0 },
3473 { "Java", &lang_name_java, 0 },
3474 /* Some of these names have a trailing space so that it is
3475 impossible for them to conflict with names written by users. */
3476 { "__ct ", &ctor_identifier, 1 },
3477 { "__base_ctor ", &base_ctor_identifier, 1 },
3478 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3479 { "__dt ", &dtor_identifier, 1 },
3480 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3481 { "__base_dtor ", &base_dtor_identifier, 1 },
3482 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3483 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3484 { "nelts", &nelts_identifier, 0 },
3485 { THIS_NAME, &this_identifier, 0 },
3486 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3487 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3488 { "_vptr", &vptr_identifier, 0 },
3489 { "__vtt_parm", &vtt_parm_identifier, 0 },
3490 { "::", &global_scope_name, 0 },
3491 { "std", &std_identifier, 0 },
3492 { NULL, NULL, 0 }
3495 for (pid = predefined_identifiers; pid->name; ++pid)
3497 *pid->node = get_identifier (pid->name);
3498 if (pid->ctor_or_dtor_p)
3499 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3503 /* Create the predefined scalar types of C,
3504 and some nodes representing standard constants (0, 1, (void *)0).
3505 Initialize the global binding level.
3506 Make definitions for built-in primitive functions. */
3508 void
3509 cxx_init_decl_processing (void)
3511 tree void_ftype;
3512 tree void_ftype_ptr;
3514 build_common_tree_nodes (flag_signed_char);
3516 /* Create all the identifiers we need. */
3517 initialize_predefined_identifiers ();
3519 /* Create the global variables. */
3520 push_to_top_level ();
3522 current_function_decl = NULL_TREE;
3523 current_binding_level = NULL;
3524 /* Enter the global namespace. */
3525 gcc_assert (global_namespace == NULL_TREE);
3526 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3527 void_type_node);
3528 DECL_CONTEXT (global_namespace) = build_translation_unit_decl (NULL_TREE);
3529 TREE_PUBLIC (global_namespace) = 1;
3530 begin_scope (sk_namespace, global_namespace);
3532 current_lang_name = NULL_TREE;
3534 if (flag_visibility_ms_compat)
3535 default_visibility = VISIBILITY_HIDDEN;
3537 /* Initially, C. */
3538 current_lang_name = lang_name_c;
3540 /* Create the `std' namespace. */
3541 push_namespace (std_identifier);
3542 std_node = current_namespace;
3543 pop_namespace ();
3545 c_common_nodes_and_builtins ();
3547 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3548 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3549 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3550 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3551 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3552 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3553 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3554 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3556 integer_two_node = build_int_cst (NULL_TREE, 2);
3558 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3559 truthvalue_type_node = boolean_type_node;
3560 truthvalue_false_node = boolean_false_node;
3561 truthvalue_true_node = boolean_true_node;
3563 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3564 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
3565 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
3567 #if 0
3568 record_builtin_type (RID_MAX, NULL, string_type_node);
3569 #endif
3571 delta_type_node = ptrdiff_type_node;
3572 vtable_index_type = ptrdiff_type_node;
3574 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3575 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
3576 void_ftype_ptr = build_function_type_list (void_type_node,
3577 ptr_type_node, NULL_TREE);
3578 void_ftype_ptr
3579 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3581 /* C++ extensions */
3583 unknown_type_node = make_node (LANG_TYPE);
3584 record_unknown_type (unknown_type_node, "unknown type");
3586 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3587 TREE_TYPE (unknown_type_node) = unknown_type_node;
3589 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3590 result. */
3591 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3592 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3594 init_list_type_node = make_node (LANG_TYPE);
3595 record_unknown_type (init_list_type_node, "init list");
3598 /* Make sure we get a unique function type, so we can give
3599 its pointer type a name. (This wins for gdb.) */
3600 tree vfunc_type = make_node (FUNCTION_TYPE);
3601 TREE_TYPE (vfunc_type) = integer_type_node;
3602 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3603 layout_type (vfunc_type);
3605 vtable_entry_type = build_pointer_type (vfunc_type);
3607 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3609 vtbl_type_node
3610 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3611 layout_type (vtbl_type_node);
3612 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3613 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3614 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3615 layout_type (vtbl_ptr_type_node);
3616 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3618 push_namespace (get_identifier ("__cxxabiv1"));
3619 abi_node = current_namespace;
3620 pop_namespace ();
3622 global_type_node = make_node (LANG_TYPE);
3623 record_unknown_type (global_type_node, "global type");
3625 /* Now, C++. */
3626 current_lang_name = lang_name_cplusplus;
3629 tree newtype, deltype;
3630 tree ptr_ftype_sizetype;
3631 tree new_eh_spec;
3633 ptr_ftype_sizetype
3634 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
3635 if (cxx_dialect == cxx98)
3637 tree bad_alloc_id;
3638 tree bad_alloc_type_node;
3639 tree bad_alloc_decl;
3641 push_namespace (std_identifier);
3642 bad_alloc_id = get_identifier ("bad_alloc");
3643 bad_alloc_type_node = make_class_type (RECORD_TYPE);
3644 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3645 bad_alloc_decl
3646 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3647 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3648 pop_namespace ();
3650 new_eh_spec
3651 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
3653 else
3654 new_eh_spec = noexcept_false_spec;
3656 newtype = build_exception_variant (ptr_ftype_sizetype, new_eh_spec);
3657 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3658 push_cp_library_fn (NEW_EXPR, newtype);
3659 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3660 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3661 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3663 nullptr_type_node = make_node (NULLPTR_TYPE);
3664 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
3665 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
3666 TYPE_UNSIGNED (nullptr_type_node) = 1;
3667 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
3668 SET_TYPE_MODE (nullptr_type_node, Pmode);
3669 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
3670 nullptr_node = build_int_cst (nullptr_type_node, 0);
3673 abort_fndecl
3674 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3676 /* Perform other language dependent initializations. */
3677 init_class_processing ();
3678 init_rtti_processing ();
3679 init_template_processing ();
3681 if (flag_exceptions)
3682 init_exception_processing ();
3684 if (! supports_one_only ())
3685 flag_weak = 0;
3687 make_fname_decl = cp_make_fname_decl;
3688 start_fname_decls ();
3690 /* Show we use EH for cleanups. */
3691 if (flag_exceptions)
3692 using_eh_for_cleanups ();
3695 /* Generate an initializer for a function naming variable from
3696 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3697 filled in with the type of the init. */
3699 tree
3700 cp_fname_init (const char* name, tree *type_p)
3702 tree domain = NULL_TREE;
3703 tree type;
3704 tree init = NULL_TREE;
3705 size_t length = 0;
3707 if (name)
3709 length = strlen (name);
3710 domain = build_index_type (size_int (length));
3711 init = build_string (length + 1, name);
3714 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3715 type = build_cplus_array_type (type, domain);
3717 *type_p = type;
3719 if (init)
3720 TREE_TYPE (init) = type;
3721 else
3722 init = error_mark_node;
3724 return init;
3727 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
3728 the decl, LOC is the location to give the decl, NAME is the
3729 initialization string and TYPE_DEP indicates whether NAME depended
3730 on the type of the function. We make use of that to detect
3731 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
3732 at the point of first use, so we mustn't push the decl now. */
3734 static tree
3735 cp_make_fname_decl (location_t loc, tree id, int type_dep)
3737 const char *const name = (type_dep && processing_template_decl
3738 ? NULL : fname_as_string (type_dep));
3739 tree type;
3740 tree init = cp_fname_init (name, &type);
3741 tree decl = build_decl (loc, VAR_DECL, id, type);
3743 if (name)
3744 free (CONST_CAST (char *, name));
3746 /* As we're using pushdecl_with_scope, we must set the context. */
3747 DECL_CONTEXT (decl) = current_function_decl;
3748 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3750 TREE_STATIC (decl) = 1;
3751 TREE_READONLY (decl) = 1;
3752 DECL_ARTIFICIAL (decl) = 1;
3754 TREE_USED (decl) = 1;
3756 if (current_function_decl)
3758 struct cp_binding_level *b = current_binding_level;
3759 if (b->kind == sk_function_parms)
3760 return error_mark_node;
3761 while (b->level_chain->kind != sk_function_parms)
3762 b = b->level_chain;
3763 pushdecl_with_scope (decl, b, /*is_friend=*/false);
3764 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
3765 LOOKUP_ONLYCONVERTING);
3767 else
3769 DECL_THIS_STATIC (decl) = true;
3770 pushdecl_top_level_and_finish (decl, init);
3773 return decl;
3776 static tree
3777 builtin_function_1 (tree decl, tree context, bool is_global)
3779 tree id = DECL_NAME (decl);
3780 const char *name = IDENTIFIER_POINTER (id);
3782 retrofit_lang_decl (decl);
3784 DECL_ARTIFICIAL (decl) = 1;
3785 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
3786 SET_DECL_LANGUAGE (decl, lang_c);
3787 /* Runtime library routines are, by definition, available in an
3788 external shared object. */
3789 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
3790 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3792 DECL_CONTEXT (decl) = context;
3794 if (is_global)
3795 pushdecl_top_level (decl);
3796 else
3797 pushdecl (decl);
3799 /* A function in the user's namespace should have an explicit
3800 declaration before it is used. Mark the built-in function as
3801 anticipated but not actually declared. */
3802 if (name[0] != '_' || name[1] != '_')
3803 DECL_ANTICIPATED (decl) = 1;
3804 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
3806 size_t len = strlen (name);
3808 /* Treat __*_chk fortification functions as anticipated as well,
3809 unless they are __builtin_*. */
3810 if (len > strlen ("___chk")
3811 && memcmp (name + len - strlen ("_chk"),
3812 "_chk", strlen ("_chk") + 1) == 0)
3813 DECL_ANTICIPATED (decl) = 1;
3816 return decl;
3819 tree
3820 cxx_builtin_function (tree decl)
3822 tree id = DECL_NAME (decl);
3823 const char *name = IDENTIFIER_POINTER (id);
3824 /* All builtins that don't begin with an '_' should additionally
3825 go in the 'std' namespace. */
3826 if (name[0] != '_')
3828 tree decl2 = copy_node(decl);
3829 push_namespace (std_identifier);
3830 builtin_function_1 (decl2, std_node, false);
3831 pop_namespace ();
3834 return builtin_function_1 (decl, NULL_TREE, false);
3837 /* Like cxx_builtin_function, but guarantee the function is added to the global
3838 scope. This is to allow function specific options to add new machine
3839 dependent builtins when the target ISA changes via attribute((target(...)))
3840 which saves space on program startup if the program does not use non-generic
3841 ISAs. */
3843 tree
3844 cxx_builtin_function_ext_scope (tree decl)
3847 tree id = DECL_NAME (decl);
3848 const char *name = IDENTIFIER_POINTER (id);
3849 /* All builtins that don't begin with an '_' should additionally
3850 go in the 'std' namespace. */
3851 if (name[0] != '_')
3853 tree decl2 = copy_node(decl);
3854 push_namespace (std_identifier);
3855 builtin_function_1 (decl2, std_node, true);
3856 pop_namespace ();
3859 return builtin_function_1 (decl, NULL_TREE, true);
3862 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
3863 function. Not called directly. */
3865 static tree
3866 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3868 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3869 DECL_EXTERNAL (fn) = 1;
3870 TREE_PUBLIC (fn) = 1;
3871 DECL_ARTIFICIAL (fn) = 1;
3872 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
3873 SET_DECL_LANGUAGE (fn, lang_c);
3874 /* Runtime library routines are, by definition, available in an
3875 external shared object. */
3876 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3877 DECL_VISIBILITY_SPECIFIED (fn) = 1;
3878 return fn;
3881 /* Returns the _DECL for a library function with C linkage.
3882 We assume that such functions never throw; if this is incorrect,
3883 callers should unset TREE_NOTHROW. */
3885 static tree
3886 build_library_fn (tree name, tree type)
3888 tree fn = build_library_fn_1 (name, ERROR_MARK, type);
3889 TREE_NOTHROW (fn) = 1;
3890 return fn;
3893 /* Returns the _DECL for a library function with C++ linkage. */
3895 static tree
3896 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
3898 tree fn = build_library_fn_1 (name, operator_code, type);
3899 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
3900 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
3901 SET_DECL_LANGUAGE (fn, lang_cplusplus);
3902 return fn;
3905 /* Like build_library_fn, but takes a C string instead of an
3906 IDENTIFIER_NODE. */
3908 tree
3909 build_library_fn_ptr (const char* name, tree type)
3911 return build_library_fn (get_identifier (name), type);
3914 /* Like build_cp_library_fn, but takes a C string instead of an
3915 IDENTIFIER_NODE. */
3917 tree
3918 build_cp_library_fn_ptr (const char* name, tree type)
3920 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
3923 /* Like build_library_fn, but also pushes the function so that we will
3924 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
3925 may throw exceptions listed in RAISES. */
3927 tree
3928 push_library_fn (tree name, tree type, tree raises)
3930 tree fn;
3932 if (raises)
3933 type = build_exception_variant (type, raises);
3935 fn = build_library_fn (name, type);
3936 pushdecl_top_level (fn);
3937 return fn;
3940 /* Like build_cp_library_fn, but also pushes the function so that it
3941 will be found by normal lookup. */
3943 static tree
3944 push_cp_library_fn (enum tree_code operator_code, tree type)
3946 tree fn = build_cp_library_fn (ansi_opname (operator_code),
3947 operator_code,
3948 type);
3949 pushdecl (fn);
3950 return fn;
3953 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3954 a FUNCTION_TYPE. */
3956 tree
3957 push_void_library_fn (tree name, tree parmtypes)
3959 tree type = build_function_type (void_type_node, parmtypes);
3960 return push_library_fn (name, type, NULL_TREE);
3963 /* Like push_library_fn, but also note that this function throws
3964 and does not return. Used for __throw_foo and the like. */
3966 tree
3967 push_throw_library_fn (tree name, tree type)
3969 tree fn = push_library_fn (name, type, NULL_TREE);
3970 TREE_THIS_VOLATILE (fn) = 1;
3971 TREE_NOTHROW (fn) = 0;
3972 return fn;
3975 /* When we call finish_struct for an anonymous union, we create
3976 default copy constructors and such. But, an anonymous union
3977 shouldn't have such things; this function undoes the damage to the
3978 anonymous union type T.
3980 (The reason that we create the synthesized methods is that we don't
3981 distinguish `union { int i; }' from `typedef union { int i; } U'.
3982 The first is an anonymous union; the second is just an ordinary
3983 union type.) */
3985 void
3986 fixup_anonymous_aggr (tree t)
3988 tree *q;
3990 /* Wipe out memory of synthesized methods. */
3991 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
3992 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3993 TYPE_HAS_COPY_CTOR (t) = 0;
3994 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
3995 TYPE_HAS_COPY_ASSIGN (t) = 0;
3996 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
3998 /* Splice the implicitly generated functions out of the TYPE_METHODS
3999 list. */
4000 q = &TYPE_METHODS (t);
4001 while (*q)
4003 if (DECL_ARTIFICIAL (*q))
4004 *q = TREE_CHAIN (*q);
4005 else
4006 q = &DECL_CHAIN (*q);
4009 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
4010 if (TYPE_METHODS (t))
4012 tree decl = TYPE_MAIN_DECL (t);
4014 if (TREE_CODE (t) != UNION_TYPE)
4015 error_at (DECL_SOURCE_LOCATION (decl),
4016 "an anonymous struct cannot have function members");
4017 else
4018 error_at (DECL_SOURCE_LOCATION (decl),
4019 "an anonymous union cannot have function members");
4022 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4023 assignment operators (because they cannot have these methods themselves).
4024 For anonymous unions this is already checked because they are not allowed
4025 in any union, otherwise we have to check it. */
4026 if (TREE_CODE (t) != UNION_TYPE)
4028 tree field, type;
4030 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4031 if (TREE_CODE (field) == FIELD_DECL)
4033 type = TREE_TYPE (field);
4034 if (CLASS_TYPE_P (type))
4036 if (TYPE_NEEDS_CONSTRUCTING (type))
4037 error ("member %q+#D with constructor not allowed "
4038 "in anonymous aggregate", field);
4039 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4040 error ("member %q+#D with destructor not allowed "
4041 "in anonymous aggregate", field);
4042 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4043 error ("member %q+#D with copy assignment operator "
4044 "not allowed in anonymous aggregate", field);
4050 /* Make sure that a declaration with no declarator is well-formed, i.e.
4051 just declares a tagged type or anonymous union.
4053 Returns the type declared; or NULL_TREE if none. */
4055 tree
4056 check_tag_decl (cp_decl_specifier_seq *declspecs)
4058 int saw_friend = declspecs->specs[(int)ds_friend] != 0;
4059 int saw_typedef = declspecs->specs[(int)ds_typedef] != 0;
4060 /* If a class, struct, or enum type is declared by the DECLSPECS
4061 (i.e, if a class-specifier, enum-specifier, or non-typename
4062 elaborated-type-specifier appears in the DECLSPECS),
4063 DECLARED_TYPE is set to the corresponding type. */
4064 tree declared_type = NULL_TREE;
4065 bool error_p = false;
4067 if (declspecs->multiple_types_p)
4068 error ("multiple types in one declaration");
4069 else if (declspecs->redefined_builtin_type)
4071 if (!in_system_header)
4072 permerror (input_location, "redeclaration of C++ built-in type %qT",
4073 declspecs->redefined_builtin_type);
4074 return NULL_TREE;
4077 if (declspecs->type
4078 && TYPE_P (declspecs->type)
4079 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4080 && MAYBE_CLASS_TYPE_P (declspecs->type))
4081 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4082 declared_type = declspecs->type;
4083 else if (declspecs->type == error_mark_node)
4084 error_p = true;
4085 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4086 permerror (input_location, "declaration does not declare anything");
4087 /* Check for an anonymous union. */
4088 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4089 && TYPE_ANONYMOUS_P (declared_type))
4091 /* 7/3 In a simple-declaration, the optional init-declarator-list
4092 can be omitted only when declaring a class (clause 9) or
4093 enumeration (7.2), that is, when the decl-specifier-seq contains
4094 either a class-specifier, an elaborated-type-specifier with
4095 a class-key (9.1), or an enum-specifier. In these cases and
4096 whenever a class-specifier or enum-specifier is present in the
4097 decl-specifier-seq, the identifiers in these specifiers are among
4098 the names being declared by the declaration (as class-name,
4099 enum-names, or enumerators, depending on the syntax). In such
4100 cases, and except for the declaration of an unnamed bit-field (9.6),
4101 the decl-specifier-seq shall introduce one or more names into the
4102 program, or shall redeclare a name introduced by a previous
4103 declaration. [Example:
4104 enum { }; // ill-formed
4105 typedef class { }; // ill-formed
4106 --end example] */
4107 if (saw_typedef)
4109 error ("missing type-name in typedef-declaration");
4110 return NULL_TREE;
4112 /* Anonymous unions are objects, so they can have specifiers. */;
4113 SET_ANON_AGGR_TYPE_P (declared_type);
4115 if (TREE_CODE (declared_type) != UNION_TYPE && !in_system_header)
4116 pedwarn (input_location, OPT_pedantic, "ISO C++ prohibits anonymous structs");
4119 else
4121 if (declspecs->specs[(int)ds_inline]
4122 || declspecs->specs[(int)ds_virtual])
4123 error ("%qs can only be specified for functions",
4124 declspecs->specs[(int)ds_inline]
4125 ? "inline" : "virtual");
4126 else if (saw_friend
4127 && (!current_class_type
4128 || current_scope () != current_class_type))
4129 error ("%<friend%> can only be specified inside a class");
4130 else if (declspecs->specs[(int)ds_explicit])
4131 error ("%<explicit%> can only be specified for constructors");
4132 else if (declspecs->storage_class)
4133 error ("a storage class can only be specified for objects "
4134 "and functions");
4135 else if (declspecs->specs[(int)ds_const]
4136 || declspecs->specs[(int)ds_volatile]
4137 || declspecs->specs[(int)ds_restrict]
4138 || declspecs->specs[(int)ds_thread])
4139 error ("qualifiers can only be specified for objects "
4140 "and functions");
4141 else if (saw_typedef)
4142 warning (0, "%<typedef%> was ignored in this declaration");
4143 else if (declspecs->specs[(int) ds_constexpr])
4144 error ("%<constexpr%> cannot be used for type declarations");
4147 return declared_type;
4150 /* Called when a declaration is seen that contains no names to declare.
4151 If its type is a reference to a structure, union or enum inherited
4152 from a containing scope, shadow that tag name for the current scope
4153 with a forward reference.
4154 If its type defines a new named structure or union
4155 or defines an enum, it is valid but we need not do anything here.
4156 Otherwise, it is an error.
4158 C++: may have to grok the declspecs to learn about static,
4159 complain for anonymous unions.
4161 Returns the TYPE declared -- or NULL_TREE if none. */
4163 tree
4164 shadow_tag (cp_decl_specifier_seq *declspecs)
4166 tree t = check_tag_decl (declspecs);
4168 if (!t)
4169 return NULL_TREE;
4171 if (declspecs->attributes)
4173 warning (0, "attribute ignored in declaration of %q+#T", t);
4174 warning (0, "attribute for %q+#T must follow the %qs keyword",
4175 t, class_key_or_enum_as_string (t));
4179 if (maybe_process_partial_specialization (t) == error_mark_node)
4180 return NULL_TREE;
4182 /* This is where the variables in an anonymous union are
4183 declared. An anonymous union declaration looks like:
4184 union { ... } ;
4185 because there is no declarator after the union, the parser
4186 sends that declaration here. */
4187 if (ANON_AGGR_TYPE_P (t))
4189 fixup_anonymous_aggr (t);
4191 if (TYPE_FIELDS (t))
4193 tree decl = grokdeclarator (/*declarator=*/NULL,
4194 declspecs, NORMAL, 0, NULL);
4195 finish_anon_union (decl);
4199 return t;
4202 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4204 tree
4205 groktypename (cp_decl_specifier_seq *type_specifiers,
4206 const cp_declarator *declarator,
4207 bool is_template_arg)
4209 tree attrs;
4210 tree type;
4211 enum decl_context context
4212 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4213 attrs = type_specifiers->attributes;
4214 type_specifiers->attributes = NULL_TREE;
4215 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4216 if (attrs && type != error_mark_node)
4218 if (CLASS_TYPE_P (type))
4219 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4220 "outside of definition", type);
4221 else if (MAYBE_CLASS_TYPE_P (type))
4222 /* A template type parameter or other dependent type. */
4223 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4224 "type %qT without an associated declaration", type);
4225 else
4226 cplus_decl_attributes (&type, attrs, 0);
4228 return type;
4231 /* Process a DECLARATOR for a function-scope variable declaration,
4232 namespace-scope variable declaration, or function declaration.
4233 (Function definitions go through start_function; class member
4234 declarations appearing in the body of the class go through
4235 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4236 If an error occurs, the error_mark_node is returned instead.
4238 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4239 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4240 for an explicitly defaulted function, or SD_DELETED for an explicitly
4241 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4242 implicitly initialized via a default constructor. ATTRIBUTES and
4243 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4244 *PUSHED_SCOPE_P is set to the scope entered in this function, if any; if
4245 set, the caller is responsible for calling pop_scope. */
4247 tree
4248 start_decl (const cp_declarator *declarator,
4249 cp_decl_specifier_seq *declspecs,
4250 int initialized,
4251 tree attributes,
4252 tree prefix_attributes,
4253 tree *pushed_scope_p)
4255 tree decl;
4256 tree context;
4257 bool was_public;
4258 int flags;
4259 bool alias;
4261 *pushed_scope_p = NULL_TREE;
4263 /* An object declared as __attribute__((deprecated)) suppresses
4264 warnings of uses of other deprecated items. */
4265 if (lookup_attribute ("deprecated", attributes))
4266 deprecated_state = DEPRECATED_SUPPRESS;
4268 attributes = chainon (attributes, prefix_attributes);
4270 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4271 &attributes);
4273 deprecated_state = DEPRECATED_NORMAL;
4275 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE
4276 || decl == error_mark_node)
4277 return error_mark_node;
4279 context = CP_DECL_CONTEXT (decl);
4280 if (context != global_namespace)
4281 *pushed_scope_p = push_scope (context);
4283 if (initialized)
4284 /* Is it valid for this decl to have an initializer at all?
4285 If not, set INITIALIZED to zero, which will indirectly
4286 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4287 switch (TREE_CODE (decl))
4289 case TYPE_DECL:
4290 error ("typedef %qD is initialized (use decltype instead)", decl);
4291 return error_mark_node;
4293 case FUNCTION_DECL:
4294 if (initialized == SD_DELETED)
4295 /* We'll handle the rest of the semantics later, but we need to
4296 set this now so it's visible to duplicate_decls. */
4297 DECL_DELETED_FN (decl) = 1;
4298 break;
4300 default:
4301 break;
4304 if (initialized)
4306 if (! toplevel_bindings_p ()
4307 && DECL_EXTERNAL (decl))
4308 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4309 decl);
4310 DECL_EXTERNAL (decl) = 0;
4311 if (toplevel_bindings_p ())
4312 TREE_STATIC (decl) = 1;
4314 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4316 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4317 record_key_method_defined (decl);
4319 /* If this is a typedef that names the class for linkage purposes
4320 (7.1.3p8), apply any attributes directly to the type. */
4321 if (TREE_CODE (decl) == TYPE_DECL
4322 && TAGGED_TYPE_P (TREE_TYPE (decl))
4323 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
4324 flags = ATTR_FLAG_TYPE_IN_PLACE;
4325 else
4326 flags = 0;
4328 /* Set attributes here so if duplicate decl, will have proper attributes. */
4329 cplus_decl_attributes (&decl, attributes, flags);
4331 /* Dllimported symbols cannot be defined. Static data members (which
4332 can be initialized in-class and dllimported) go through grokfield,
4333 not here, so we don't need to exclude those decls when checking for
4334 a definition. */
4335 if (initialized && DECL_DLLIMPORT_P (decl))
4337 error ("definition of %q#D is marked %<dllimport%>", decl);
4338 DECL_DLLIMPORT_P (decl) = 0;
4341 /* If #pragma weak was used, mark the decl weak now. */
4342 maybe_apply_pragma_weak (decl);
4344 if (TREE_CODE (decl) == FUNCTION_DECL
4345 && DECL_DECLARED_INLINE_P (decl)
4346 && DECL_UNINLINABLE (decl)
4347 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4348 warning (0, "inline function %q+D given attribute noinline", decl);
4350 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
4352 if (TREE_CODE (decl) == VAR_DECL)
4354 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
4355 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
4356 error ("%q#D is not a static member of %q#T", decl, context);
4357 else
4359 if (DECL_CONTEXT (field) != context)
4361 if (!same_type_p (DECL_CONTEXT (field), context))
4362 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
4363 "to be defined as %<%T::%D%>",
4364 DECL_CONTEXT (field), DECL_NAME (decl),
4365 context, DECL_NAME (decl));
4366 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
4368 if (processing_specialization
4369 && template_class_depth (context) == 0
4370 && CLASSTYPE_TEMPLATE_SPECIALIZATION (context))
4371 error ("template header not allowed in member definition "
4372 "of explicitly specialized class");
4373 /* Static data member are tricky; an in-class initialization
4374 still doesn't provide a definition, so the in-class
4375 declaration will have DECL_EXTERNAL set, but will have an
4376 initialization. Thus, duplicate_decls won't warn
4377 about this situation, and so we check here. */
4378 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
4379 error ("duplicate initialization of %qD", decl);
4380 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
4381 decl = field;
4382 if (declspecs->specs[(int) ds_constexpr]
4383 && !DECL_DECLARED_CONSTEXPR_P (field))
4384 error ("%qD declared %<constexpr%> outside its class", field);
4387 else
4389 tree field = check_classfn (context, decl,
4390 (processing_template_decl
4391 > template_class_depth (context))
4392 ? current_template_parms
4393 : NULL_TREE);
4394 if (field && field != error_mark_node
4395 && duplicate_decls (decl, field,
4396 /*newdecl_is_friend=*/false))
4397 decl = field;
4400 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
4401 DECL_IN_AGGR_P (decl) = 0;
4402 /* Do not mark DECL as an explicit specialization if it was not
4403 already marked as an instantiation; a declaration should
4404 never be marked as a specialization unless we know what
4405 template is being specialized. */
4406 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4408 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
4410 /* [temp.expl.spec] An explicit specialization of a static data
4411 member of a template is a definition if the declaration
4412 includes an initializer; otherwise, it is a declaration.
4414 We check for processing_specialization so this only applies
4415 to the new specialization syntax. */
4416 if (!initialized && processing_specialization)
4417 DECL_EXTERNAL (decl) = 1;
4420 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
4421 /* Aliases are definitions. */
4422 && !alias)
4423 permerror (input_location, "declaration of %q#D outside of class is not definition",
4424 decl);
4427 was_public = TREE_PUBLIC (decl);
4429 /* Enter this declaration into the symbol table. */
4430 decl = maybe_push_decl (decl);
4432 if (processing_template_decl)
4433 decl = push_template_decl (decl);
4434 if (decl == error_mark_node)
4435 return error_mark_node;
4437 /* Tell the back end to use or not use .common as appropriate. If we say
4438 -fconserve-space, we want this to save .data space, at the expense of
4439 wrong semantics. If we say -fno-conserve-space, we want this to
4440 produce errors about redefs; to do this we force variables into the
4441 data segment. */
4442 if (flag_conserve_space
4443 && TREE_CODE (decl) == VAR_DECL
4444 && TREE_PUBLIC (decl)
4445 && !DECL_THREAD_LOCAL_P (decl)
4446 && !have_global_bss_p ())
4447 DECL_COMMON (decl) = 1;
4449 if (TREE_CODE (decl) == VAR_DECL
4450 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
4451 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
4453 /* This is a const variable with implicit 'static'. Set
4454 DECL_THIS_STATIC so we can tell it from variables that are
4455 !TREE_PUBLIC because of the anonymous namespace. */
4456 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
4457 DECL_THIS_STATIC (decl) = 1;
4460 if (!processing_template_decl && TREE_CODE (decl) == VAR_DECL)
4461 start_decl_1 (decl, initialized);
4463 return decl;
4466 /* Process the declaration of a variable DECL. INITIALIZED is true
4467 iff DECL is explicitly initialized. (INITIALIZED is false if the
4468 variable is initialized via an implicitly-called constructor.)
4469 This function must be called for ordinary variables (including, for
4470 example, implicit instantiations of templates), but must not be
4471 called for template declarations. */
4473 void
4474 start_decl_1 (tree decl, bool initialized)
4476 tree type;
4477 bool complete_p;
4478 bool aggregate_definition_p;
4480 gcc_assert (!processing_template_decl);
4482 if (error_operand_p (decl))
4483 return;
4485 gcc_assert (TREE_CODE (decl) == VAR_DECL);
4487 type = TREE_TYPE (decl);
4488 complete_p = COMPLETE_TYPE_P (type);
4489 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
4491 /* If an explicit initializer is present, or if this is a definition
4492 of an aggregate, then we need a complete type at this point.
4493 (Scalars are always complete types, so there is nothing to
4494 check.) This code just sets COMPLETE_P; errors (if necessary)
4495 are issued below. */
4496 if ((initialized || aggregate_definition_p)
4497 && !complete_p
4498 && COMPLETE_TYPE_P (complete_type (type)))
4500 complete_p = true;
4501 /* We will not yet have set TREE_READONLY on DECL if the type
4502 was "const", but incomplete, before this point. But, now, we
4503 have a complete type, so we can try again. */
4504 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
4507 if (initialized)
4508 /* Is it valid for this decl to have an initializer at all? */
4510 /* Don't allow initializations for incomplete types except for
4511 arrays which might be completed by the initialization. */
4512 if (complete_p)
4513 ; /* A complete type is ok. */
4514 else if (type_uses_auto (type))
4515 ; /* An auto type is ok. */
4516 else if (TREE_CODE (type) != ARRAY_TYPE)
4518 error ("variable %q#D has initializer but incomplete type", decl);
4519 type = TREE_TYPE (decl) = error_mark_node;
4521 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
4523 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
4524 error ("elements of array %q#D have incomplete type", decl);
4525 /* else we already gave an error in start_decl. */
4528 else if (aggregate_definition_p && !complete_p)
4530 if (type_uses_auto (type))
4531 error ("declaration of %q#D has no initializer", decl);
4532 else
4533 error ("aggregate %q#D has incomplete type and cannot be defined",
4534 decl);
4535 /* Change the type so that assemble_variable will give
4536 DECL an rtl we can live with: (mem (const_int 0)). */
4537 type = TREE_TYPE (decl) = error_mark_node;
4540 /* Create a new scope to hold this declaration if necessary.
4541 Whether or not a new scope is necessary cannot be determined
4542 until after the type has been completed; if the type is a
4543 specialization of a class template it is not until after
4544 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4545 will be set correctly. */
4546 maybe_push_cleanup_level (type);
4549 /* Handle initialization of references. DECL, TYPE, and INIT have the
4550 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
4551 but will be set to a new CLEANUP_STMT if a temporary is created
4552 that must be destroyed subsequently.
4554 Returns an initializer expression to use to initialize DECL, or
4555 NULL if the initialization can be performed statically.
4557 Quotes on semantics can be found in ARM 8.4.3. */
4559 static tree
4560 grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
4562 tree tmp;
4564 if (init == NULL_TREE)
4566 if ((DECL_LANG_SPECIFIC (decl) == 0
4567 || DECL_IN_AGGR_P (decl) == 0)
4568 && ! DECL_THIS_EXTERN (decl))
4569 error ("%qD declared as reference but not initialized", decl);
4570 return NULL_TREE;
4573 if (TREE_CODE (init) == TREE_LIST)
4574 init = build_x_compound_expr_from_list (init, ELK_INIT,
4575 tf_warning_or_error);
4577 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
4578 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
4579 /* Note: default conversion is only called in very special cases. */
4580 init = decay_conversion (init);
4582 /* Convert INIT to the reference type TYPE. This may involve the
4583 creation of a temporary, whose lifetime must be the same as that
4584 of the reference. If so, a DECL_EXPR for the temporary will be
4585 added just after the DECL_EXPR for DECL. That's why we don't set
4586 DECL_INITIAL for local references (instead assigning to them
4587 explicitly); we need to allow the temporary to be initialized
4588 first. */
4589 tmp = initialize_reference (type, init, decl, cleanup, tf_warning_or_error);
4591 if (tmp == error_mark_node)
4592 return NULL_TREE;
4593 else if (tmp == NULL_TREE)
4595 error ("cannot initialize %qT from %qT", type, TREE_TYPE (init));
4596 return NULL_TREE;
4599 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
4600 return tmp;
4602 DECL_INITIAL (decl) = tmp;
4604 return NULL_TREE;
4607 /* Subroutine of check_initializer. We're initializing a DECL of
4608 std::initializer_list<T> TYPE from a braced-init-list INIT, and need to
4609 extend the lifetime of the underlying array to match that of the decl,
4610 just like for reference initialization. CLEANUP is as for
4611 grok_reference_init. */
4613 static tree
4614 build_init_list_var_init (tree decl, tree type, tree init, tree *array_init,
4615 tree *cleanup)
4617 tree aggr_init, array, arrtype;
4618 init = perform_implicit_conversion (type, init, tf_warning_or_error);
4619 if (error_operand_p (init))
4620 return error_mark_node;
4622 aggr_init = TARGET_EXPR_INITIAL (init);
4623 array = AGGR_INIT_EXPR_ARG (aggr_init, 1);
4624 arrtype = TREE_TYPE (array);
4625 STRIP_NOPS (array);
4626 gcc_assert (TREE_CODE (array) == ADDR_EXPR);
4627 array = TREE_OPERAND (array, 0);
4628 /* If the array is constant, finish_compound_literal already made it a
4629 static variable and we don't need to do anything here. */
4630 if (decl && TREE_CODE (array) == TARGET_EXPR)
4632 tree var = set_up_extended_ref_temp (decl, array, cleanup, array_init);
4633 var = build_address (var);
4634 var = convert (arrtype, var);
4635 AGGR_INIT_EXPR_ARG (aggr_init, 1) = var;
4637 return init;
4640 /* Designated initializers in arrays are not supported in GNU C++.
4641 The parser cannot detect this error since it does not know whether
4642 a given brace-enclosed initializer is for a class type or for an
4643 array. This function checks that CE does not use a designated
4644 initializer. If it does, an error is issued. Returns true if CE
4645 is valid, i.e., does not have a designated initializer. */
4647 static bool
4648 check_array_designated_initializer (const constructor_elt *ce)
4650 /* Designated initializers for array elements are not supported. */
4651 if (ce->index)
4653 /* The parser only allows identifiers as designated
4654 initializers. */
4655 if (ce->index == error_mark_node)
4656 error ("name used in a GNU-style designated "
4657 "initializer for an array");
4658 else if (TREE_CODE (ce->index) == INTEGER_CST)
4659 /* An index added by reshape_init. */
4660 return true;
4661 else
4663 gcc_assert (TREE_CODE (ce->index) == IDENTIFIER_NODE);
4664 error ("name %qD used in a GNU-style designated "
4665 "initializer for an array", ce->index);
4667 return false;
4670 return true;
4673 /* When parsing `int a[] = {1, 2};' we don't know the size of the
4674 array until we finish parsing the initializer. If that's the
4675 situation we're in, update DECL accordingly. */
4677 static void
4678 maybe_deduce_size_from_array_init (tree decl, tree init)
4680 tree type = TREE_TYPE (decl);
4682 if (TREE_CODE (type) == ARRAY_TYPE
4683 && TYPE_DOMAIN (type) == NULL_TREE
4684 && TREE_CODE (decl) != TYPE_DECL)
4686 /* do_default is really a C-ism to deal with tentative definitions.
4687 But let's leave it here to ease the eventual merge. */
4688 int do_default = !DECL_EXTERNAL (decl);
4689 tree initializer = init ? init : DECL_INITIAL (decl);
4690 int failure = 0;
4692 /* Check that there are no designated initializers in INIT, as
4693 those are not supported in GNU C++, and as the middle-end
4694 will crash if presented with a non-numeric designated
4695 initializer. */
4696 if (initializer && TREE_CODE (initializer) == CONSTRUCTOR)
4698 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initializer);
4699 constructor_elt *ce;
4700 HOST_WIDE_INT i;
4701 FOR_EACH_VEC_ELT (constructor_elt, v, i, ce)
4702 if (!check_array_designated_initializer (ce))
4703 failure = 1;
4706 if (!failure)
4708 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
4709 do_default);
4710 if (failure == 1)
4712 error ("initializer fails to determine size of %qD", decl);
4713 TREE_TYPE (decl) = error_mark_node;
4715 else if (failure == 2)
4717 if (do_default)
4719 error ("array size missing in %qD", decl);
4720 TREE_TYPE (decl) = error_mark_node;
4722 /* If a `static' var's size isn't known, make it extern as
4723 well as static, so it does not get allocated. If it's not
4724 `static', then don't mark it extern; finish_incomplete_decl
4725 will give it a default size and it will get allocated. */
4726 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4727 DECL_EXTERNAL (decl) = 1;
4729 else if (failure == 3)
4731 error ("zero-size array %qD", decl);
4732 TREE_TYPE (decl) = error_mark_node;
4736 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
4738 relayout_decl (decl);
4742 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
4743 any appropriate error messages regarding the layout. */
4745 static void
4746 layout_var_decl (tree decl)
4748 tree type;
4750 type = TREE_TYPE (decl);
4751 if (type == error_mark_node)
4752 return;
4754 /* If we haven't already layed out this declaration, do so now.
4755 Note that we must not call complete type for an external object
4756 because it's type might involve templates that we are not
4757 supposed to instantiate yet. (And it's perfectly valid to say
4758 `extern X x' for some incomplete type `X'.) */
4759 if (!DECL_EXTERNAL (decl))
4760 complete_type (type);
4761 if (!DECL_SIZE (decl)
4762 && TREE_TYPE (decl) != error_mark_node
4763 && (COMPLETE_TYPE_P (type)
4764 || (TREE_CODE (type) == ARRAY_TYPE
4765 && !TYPE_DOMAIN (type)
4766 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
4767 layout_decl (decl, 0);
4769 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
4771 /* An automatic variable with an incomplete type: that is an error.
4772 Don't talk about array types here, since we took care of that
4773 message in grokdeclarator. */
4774 error ("storage size of %qD isn%'t known", decl);
4775 TREE_TYPE (decl) = error_mark_node;
4777 #if 0
4778 /* Keep this code around in case we later want to control debug info
4779 based on whether a type is "used". (jason 1999-11-11) */
4781 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
4782 /* Let debugger know it should output info for this type. */
4783 note_debug_info_needed (ttype);
4785 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
4786 note_debug_info_needed (DECL_CONTEXT (decl));
4787 #endif
4789 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4790 && DECL_SIZE (decl) != NULL_TREE
4791 && ! TREE_CONSTANT (DECL_SIZE (decl)))
4793 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4794 constant_expression_warning (DECL_SIZE (decl));
4795 else
4797 error ("storage size of %qD isn%'t constant", decl);
4798 TREE_TYPE (decl) = error_mark_node;
4803 /* If a local static variable is declared in an inline function, or if
4804 we have a weak definition, we must endeavor to create only one
4805 instance of the variable at link-time. */
4807 void
4808 maybe_commonize_var (tree decl)
4810 /* Static data in a function with comdat linkage also has comdat
4811 linkage. */
4812 if (TREE_STATIC (decl)
4813 /* Don't mess with __FUNCTION__. */
4814 && ! DECL_ARTIFICIAL (decl)
4815 && DECL_FUNCTION_SCOPE_P (decl)
4816 && vague_linkage_p (DECL_CONTEXT (decl)))
4818 if (flag_weak)
4820 /* With weak symbols, we simply make the variable COMDAT;
4821 that will cause copies in multiple translations units to
4822 be merged. */
4823 comdat_linkage (decl);
4825 else
4827 if (DECL_INITIAL (decl) == NULL_TREE
4828 || DECL_INITIAL (decl) == error_mark_node)
4830 /* Without weak symbols, we can use COMMON to merge
4831 uninitialized variables. */
4832 TREE_PUBLIC (decl) = 1;
4833 DECL_COMMON (decl) = 1;
4835 else
4837 /* While for initialized variables, we must use internal
4838 linkage -- which means that multiple copies will not
4839 be merged. */
4840 TREE_PUBLIC (decl) = 0;
4841 DECL_COMMON (decl) = 0;
4842 warning_at (input_location, 0,
4843 "sorry: semantics of inline function static "
4844 "data %q+#D are wrong (you%'ll wind up "
4845 "with multiple copies)", decl);
4846 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4847 " you can work around this by removing "
4848 "the initializer");
4852 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
4853 /* Set it up again; we might have set DECL_INITIAL since the last
4854 time. */
4855 comdat_linkage (decl);
4858 /* Issue an error message if DECL is an uninitialized const variable. */
4860 static void
4861 check_for_uninitialized_const_var (tree decl)
4863 tree type = strip_array_types (TREE_TYPE (decl));
4865 /* ``Unless explicitly declared extern, a const object does not have
4866 external linkage and must be initialized. ($8.4; $12.1)'' ARM
4867 7.1.6 */
4868 if (TREE_CODE (decl) == VAR_DECL
4869 && TREE_CODE (type) != REFERENCE_TYPE
4870 && CP_TYPE_CONST_P (type)
4871 && (!TYPE_NEEDS_CONSTRUCTING (type)
4872 || !type_has_user_provided_default_constructor (type))
4873 && !DECL_INITIAL (decl))
4875 permerror (DECL_SOURCE_LOCATION (decl),
4876 "uninitialized const %qD", decl);
4878 if (CLASS_TYPE_P (type)
4879 && !type_has_user_provided_default_constructor (type))
4881 tree defaulted_ctor;
4883 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
4884 "%q#T has no user-provided default constructor", type);
4885 defaulted_ctor = in_class_defaulted_default_constructor (type);
4886 if (defaulted_ctor)
4887 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
4888 "constructor is not user-provided because it is "
4889 "explicitly defaulted in the class body");
4894 /* Structure holding the current initializer being processed by reshape_init.
4895 CUR is a pointer to the current element being processed, END is a pointer
4896 after the last element present in the initializer. */
4897 typedef struct reshape_iterator_t
4899 constructor_elt *cur;
4900 constructor_elt *end;
4901 } reshape_iter;
4903 static tree reshape_init_r (tree, reshape_iter *, bool);
4905 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
4906 returned is the next FIELD_DECL (possibly FIELD itself) that can be
4907 initialized. If there are no more such fields, the return value
4908 will be NULL. */
4910 tree
4911 next_initializable_field (tree field)
4913 while (field
4914 && (TREE_CODE (field) != FIELD_DECL
4915 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
4916 || DECL_ARTIFICIAL (field)))
4917 field = DECL_CHAIN (field);
4919 return field;
4922 /* Subroutine of reshape_init_array and reshape_init_vector, which does
4923 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
4924 INTEGER_CST representing the size of the array minus one (the maximum index),
4925 or NULL_TREE if the array was declared without specifying the size. D is
4926 the iterator within the constructor. */
4928 static tree
4929 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d)
4931 tree new_init;
4932 bool sized_array_p = (max_index != NULL_TREE);
4933 unsigned HOST_WIDE_INT max_index_cst = 0;
4934 unsigned HOST_WIDE_INT index;
4936 /* The initializer for an array is always a CONSTRUCTOR. */
4937 new_init = build_constructor (init_list_type_node, NULL);
4939 if (sized_array_p)
4941 /* Minus 1 is used for zero sized arrays. */
4942 if (integer_all_onesp (max_index))
4943 return new_init;
4945 if (host_integerp (max_index, 1))
4946 max_index_cst = tree_low_cst (max_index, 1);
4947 /* sizetype is sign extended, not zero extended. */
4948 else
4949 max_index_cst = tree_low_cst (fold_convert (size_type_node, max_index),
4953 /* Loop until there are no more initializers. */
4954 for (index = 0;
4955 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
4956 ++index)
4958 tree elt_init;
4960 check_array_designated_initializer (d->cur);
4961 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false);
4962 if (elt_init == error_mark_node)
4963 return error_mark_node;
4964 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
4965 size_int (index), elt_init);
4968 return new_init;
4971 /* Subroutine of reshape_init_r, processes the initializers for arrays.
4972 Parameters are the same of reshape_init_r. */
4974 static tree
4975 reshape_init_array (tree type, reshape_iter *d)
4977 tree max_index = NULL_TREE;
4979 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
4981 if (TYPE_DOMAIN (type))
4982 max_index = array_type_nelts (type);
4984 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4987 /* Subroutine of reshape_init_r, processes the initializers for vectors.
4988 Parameters are the same of reshape_init_r. */
4990 static tree
4991 reshape_init_vector (tree type, reshape_iter *d)
4993 tree max_index = NULL_TREE;
4995 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
4997 if (COMPOUND_LITERAL_P (d->cur->value))
4999 tree value = d->cur->value;
5000 if (!same_type_p (TREE_TYPE (value), type))
5002 error ("invalid type %qT as initializer for a vector of type %qT",
5003 TREE_TYPE (d->cur->value), type);
5004 value = error_mark_node;
5006 ++d->cur;
5007 return value;
5010 /* For a vector, we initialize it as an array of the appropriate size. */
5011 if (TREE_CODE (type) == VECTOR_TYPE)
5012 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5014 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
5017 /* Subroutine of reshape_init_r, processes the initializers for classes
5018 or union. Parameters are the same of reshape_init_r. */
5020 static tree
5021 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p)
5023 tree field;
5024 tree new_init;
5026 gcc_assert (CLASS_TYPE_P (type));
5028 /* The initializer for a class is always a CONSTRUCTOR. */
5029 new_init = build_constructor (init_list_type_node, NULL);
5030 field = next_initializable_field (TYPE_FIELDS (type));
5032 if (!field)
5034 /* [dcl.init.aggr]
5036 An initializer for an aggregate member that is an
5037 empty class shall have the form of an empty
5038 initializer-list {}. */
5039 if (!first_initializer_p)
5041 error ("initializer for %qT must be brace-enclosed", type);
5042 return error_mark_node;
5044 return new_init;
5047 /* Loop through the initializable fields, gathering initializers. */
5048 while (d->cur != d->end)
5050 tree field_init;
5052 /* Handle designated initializers, as an extension. */
5053 if (d->cur->index)
5055 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
5057 if (!field || TREE_CODE (field) != FIELD_DECL)
5059 error ("%qT has no non-static data member named %qD", type,
5060 d->cur->index);
5061 return error_mark_node;
5065 /* If we processed all the member of the class, we are done. */
5066 if (!field)
5067 break;
5069 field_init = reshape_init_r (TREE_TYPE (field), d,
5070 /*first_initializer_p=*/false);
5071 if (field_init == error_mark_node)
5072 return error_mark_node;
5074 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5076 /* [dcl.init.aggr]
5078 When a union is initialized with a brace-enclosed
5079 initializer, the braces shall only contain an
5080 initializer for the first member of the union. */
5081 if (TREE_CODE (type) == UNION_TYPE)
5082 break;
5084 field = next_initializable_field (DECL_CHAIN (field));
5087 return new_init;
5090 /* Subroutine of reshape_init, which processes a single initializer (part of
5091 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5092 iterator within the CONSTRUCTOR which points to the initializer to process.
5093 FIRST_INITIALIZER_P is true if this is the first initializer of the
5094 outermost CONSTRUCTOR node. */
5096 static tree
5097 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p)
5099 tree init = d->cur->value;
5101 if (error_operand_p (init))
5102 return error_mark_node;
5104 /* A non-aggregate type is always initialized with a single
5105 initializer. */
5106 if (!CP_AGGREGATE_TYPE_P (type))
5108 /* It is invalid to initialize a non-aggregate type with a
5109 brace-enclosed initializer before C++0x.
5110 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5111 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5112 a CONSTRUCTOR (with a record type). */
5113 if (TREE_CODE (init) == CONSTRUCTOR
5114 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5116 if (SCALAR_TYPE_P (type))
5118 error ("braces around scalar initializer for type %qT", type);
5119 init = error_mark_node;
5121 else
5122 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5125 d->cur++;
5126 return init;
5129 /* [dcl.init.aggr]
5131 All implicit type conversions (clause _conv_) are considered when
5132 initializing the aggregate member with an initializer from an
5133 initializer-list. If the initializer can initialize a member,
5134 the member is initialized. Otherwise, if the member is itself a
5135 non-empty subaggregate, brace elision is assumed and the
5136 initializer is considered for the initialization of the first
5137 member of the subaggregate. */
5138 if (TREE_CODE (init) != CONSTRUCTOR
5139 /* But don't try this for the first initializer, since that would be
5140 looking through the outermost braces; A a2 = { a1 }; is not a
5141 valid aggregate initialization. */
5142 && !first_initializer_p
5143 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5144 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL)))
5146 d->cur++;
5147 return init;
5150 /* [dcl.init.string]
5152 A char array (whether plain char, signed char, or unsigned char)
5153 can be initialized by a string-literal (optionally enclosed in
5154 braces); a wchar_t array can be initialized by a wide
5155 string-literal (optionally enclosed in braces). */
5156 if (TREE_CODE (type) == ARRAY_TYPE
5157 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5159 tree str_init = init;
5161 /* Strip one level of braces if and only if they enclose a single
5162 element (as allowed by [dcl.init.string]). */
5163 if (!first_initializer_p
5164 && TREE_CODE (str_init) == CONSTRUCTOR
5165 && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (str_init)) == 1)
5167 str_init = VEC_index (constructor_elt,
5168 CONSTRUCTOR_ELTS (str_init), 0)->value;
5171 /* If it's a string literal, then it's the initializer for the array
5172 as a whole. Otherwise, continue with normal initialization for
5173 array types (one value per array element). */
5174 if (TREE_CODE (str_init) == STRING_CST)
5176 d->cur++;
5177 return str_init;
5181 /* The following cases are about aggregates. If we are not within a full
5182 initializer already, and there is not a CONSTRUCTOR, it means that there
5183 is a missing set of braces (that is, we are processing the case for
5184 which reshape_init exists). */
5185 if (!first_initializer_p)
5187 if (TREE_CODE (init) == CONSTRUCTOR)
5189 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
5190 /* There is no need to reshape pointer-to-member function
5191 initializers, as they are always constructed correctly
5192 by the front end. */
5194 else if (COMPOUND_LITERAL_P (init))
5195 /* For a nested compound literal, there is no need to reshape since
5196 brace elision is not allowed. Even if we decided to allow it,
5197 we should add a call to reshape_init in finish_compound_literal,
5198 before calling digest_init, so changing this code would still
5199 not be necessary. */
5200 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
5201 else
5203 ++d->cur;
5204 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5205 return reshape_init (type, init);
5209 warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
5210 type);
5213 /* Dispatch to specialized routines. */
5214 if (CLASS_TYPE_P (type))
5215 return reshape_init_class (type, d, first_initializer_p);
5216 else if (TREE_CODE (type) == ARRAY_TYPE)
5217 return reshape_init_array (type, d);
5218 else if (TREE_CODE (type) == VECTOR_TYPE)
5219 return reshape_init_vector (type, d);
5220 else
5221 gcc_unreachable();
5224 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
5225 brace-enclosed aggregate initializer.
5227 INIT is the CONSTRUCTOR containing the list of initializers describing
5228 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
5229 It may not presently match the shape of the TYPE; for example:
5231 struct S { int a; int b; };
5232 struct S a[] = { 1, 2, 3, 4 };
5234 Here INIT will hold a VEC of four elements, rather than a
5235 VEC of two elements, each itself a VEC of two elements. This
5236 routine transforms INIT from the former form into the latter. The
5237 revised CONSTRUCTOR node is returned. */
5239 tree
5240 reshape_init (tree type, tree init)
5242 VEC(constructor_elt, gc) *v;
5243 reshape_iter d;
5244 tree new_init;
5246 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5248 v = CONSTRUCTOR_ELTS (init);
5250 /* An empty constructor does not need reshaping, and it is always a valid
5251 initializer. */
5252 if (VEC_empty (constructor_elt, v))
5253 return init;
5255 /* Recurse on this CONSTRUCTOR. */
5256 d.cur = VEC_index (constructor_elt, v, 0);
5257 d.end = d.cur + VEC_length (constructor_elt, v);
5259 new_init = reshape_init_r (type, &d, true);
5260 if (new_init == error_mark_node)
5261 return error_mark_node;
5263 /* Make sure all the element of the constructor were used. Otherwise,
5264 issue an error about exceeding initializers. */
5265 if (d.cur != d.end)
5266 error ("too many initializers for %qT", type);
5268 return new_init;
5271 /* Verify array initializer. Returns true if errors have been reported. */
5273 bool
5274 check_array_initializer (tree decl, tree type, tree init)
5276 tree element_type = TREE_TYPE (type);
5278 /* The array type itself need not be complete, because the
5279 initializer may tell us how many elements are in the array.
5280 But, the elements of the array must be complete. */
5281 if (!COMPLETE_TYPE_P (complete_type (element_type)))
5283 if (decl)
5284 error ("elements of array %q#D have incomplete type", decl);
5285 else
5286 error ("elements of array %q#T have incomplete type", type);
5287 return true;
5289 /* It is not valid to initialize a VLA. */
5290 if (init
5291 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
5292 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
5294 if (decl)
5295 error ("variable-sized object %qD may not be initialized", decl);
5296 else
5297 error ("variable-sized compound literal");
5298 return true;
5300 return false;
5303 /* Subroutine of check_initializer; args are passed down from that function.
5304 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
5306 static tree
5307 build_aggr_init_full_exprs (tree decl, tree init, int flags)
5310 int saved_stmts_are_full_exprs_p = 0;
5311 if (building_stmt_tree ())
5313 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
5314 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
5316 init = build_aggr_init (decl, init, flags, tf_warning_or_error);
5317 if (building_stmt_tree ())
5318 current_stmt_tree ()->stmts_are_full_exprs_p =
5319 saved_stmts_are_full_exprs_p;
5320 return init;
5323 /* Verify INIT (the initializer for DECL), and record the
5324 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
5325 grok_reference_init.
5327 If the return value is non-NULL, it is an expression that must be
5328 evaluated dynamically to initialize DECL. */
5330 static tree
5331 check_initializer (tree decl, tree init, int flags, tree *cleanup)
5333 tree type = TREE_TYPE (decl);
5334 tree init_code = NULL;
5335 tree extra_init = NULL_TREE;
5336 tree core_type;
5338 /* Things that are going to be initialized need to have complete
5339 type. */
5340 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
5342 if (type == error_mark_node)
5343 /* We will have already complained. */
5344 return NULL_TREE;
5346 if (TREE_CODE (type) == ARRAY_TYPE)
5348 if (check_array_initializer (decl, type, init))
5349 return NULL_TREE;
5351 else if (!COMPLETE_TYPE_P (type))
5353 error ("%qD has incomplete type", decl);
5354 TREE_TYPE (decl) = error_mark_node;
5355 return NULL_TREE;
5357 else
5358 /* There is no way to make a variable-sized class type in GNU C++. */
5359 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
5361 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
5363 int init_len = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init));
5364 if (SCALAR_TYPE_P (type))
5366 if (init_len == 0)
5368 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5369 init = build_zero_init (type, NULL_TREE, false);
5371 else if (init_len != 1)
5373 error ("scalar object %qD requires one element in initializer",
5374 decl);
5375 TREE_TYPE (decl) = error_mark_node;
5376 return NULL_TREE;
5381 if (TREE_CODE (decl) == CONST_DECL)
5383 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
5385 DECL_INITIAL (decl) = init;
5387 gcc_assert (init != NULL_TREE);
5388 init = NULL_TREE;
5390 else if (!init && DECL_REALLY_EXTERN (decl))
5392 else if (TREE_CODE (type) == REFERENCE_TYPE)
5393 init = grok_reference_init (decl, type, init, cleanup);
5394 else if (init || TYPE_NEEDS_CONSTRUCTING (type))
5396 if (!init)
5397 check_for_uninitialized_const_var (decl);
5398 /* Do not reshape constructors of vectors (they don't need to be
5399 reshaped. */
5400 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
5402 if (is_std_init_list (type))
5403 init = build_init_list_var_init (decl, type, init,
5404 &extra_init, cleanup);
5405 else if (TYPE_NON_AGGREGATE_CLASS (type))
5407 /* Don't reshape if the class has constructors. */
5408 if (cxx_dialect == cxx98)
5409 error ("in C++98 %qD must be initialized by constructor, "
5410 "not by %<{...}%>",
5411 decl);
5413 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_VECTOR_OPAQUE (type))
5415 error ("opaque vector types cannot be initialized");
5416 init = error_mark_node;
5418 else
5419 init = reshape_init (type, init);
5422 /* If DECL has an array type without a specific bound, deduce the
5423 array size from the initializer. */
5424 maybe_deduce_size_from_array_init (decl, init);
5425 type = TREE_TYPE (decl);
5426 if (type == error_mark_node)
5427 return NULL_TREE;
5429 if (TYPE_NEEDS_CONSTRUCTING (type)
5430 || (CLASS_TYPE_P (type)
5431 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init))))
5433 init_code = build_aggr_init_full_exprs (decl, init, flags);
5435 /* If this is a constexpr initializer, expand_default_init will
5436 have returned an INIT_EXPR rather than a CALL_EXPR. In that
5437 case, pull the initializer back out and pass it down into
5438 store_init_value. */
5439 while (TREE_CODE (init_code) == EXPR_STMT
5440 || TREE_CODE (init_code) == CONVERT_EXPR)
5441 init_code = TREE_OPERAND (init_code, 0);
5442 if (TREE_CODE (init_code) == INIT_EXPR)
5444 init = TREE_OPERAND (init_code, 1);
5445 init_code = NULL_TREE;
5446 /* Don't call digest_init; it's unnecessary and will complain
5447 about aggregate initialization of non-aggregate classes. */
5448 flags |= LOOKUP_ALREADY_DIGESTED;
5450 else if (DECL_DECLARED_CONSTEXPR_P (decl))
5452 /* Declared constexpr, but no suitable initializer; massage
5453 init appropriately so we can pass it into store_init_value
5454 for the error. */
5455 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
5456 init = finish_compound_literal (type, init);
5457 else if (CLASS_TYPE_P (type)
5458 && (!init || TREE_CODE (init) == TREE_LIST))
5460 init = build_functional_cast (type, init, tf_none);
5461 if (init != error_mark_node)
5462 TARGET_EXPR_DIRECT_INIT_P (init) = true;
5464 init_code = NULL_TREE;
5466 else
5467 init = NULL_TREE;
5470 if (init && TREE_CODE (init) != TREE_VEC)
5472 init_code = store_init_value (decl, init, flags);
5473 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
5474 && DECL_INITIAL (decl)
5475 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
5476 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
5477 warning (0, "array %qD initialized by parenthesized string literal %qE",
5478 decl, DECL_INITIAL (decl));
5479 init = NULL;
5482 else
5484 if (CLASS_TYPE_P (core_type = strip_array_types (type))
5485 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
5486 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
5487 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
5488 /*complain=*/true);
5490 check_for_uninitialized_const_var (decl);
5493 if (init && init != error_mark_node)
5494 init_code = build2 (INIT_EXPR, type, decl, init);
5496 if (extra_init)
5497 init_code = add_stmt_to_compound (extra_init, init_code);
5499 if (init_code && DECL_IN_AGGR_P (decl))
5501 static int explained = 0;
5503 if (cxx_dialect < cxx0x)
5504 error ("initializer invalid for static member with constructor");
5505 else
5506 error ("non-constant in-class initialization invalid for static "
5507 "member %qD", decl);
5508 if (!explained)
5510 error ("(an out of class initialization is required)");
5511 explained = 1;
5515 return init_code;
5518 /* If DECL is not a local variable, give it RTL. */
5520 static void
5521 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
5523 int toplev = toplevel_bindings_p ();
5524 int defer_p;
5525 const char *filename;
5527 /* Set the DECL_ASSEMBLER_NAME for the object. */
5528 if (asmspec)
5530 /* The `register' keyword, when used together with an
5531 asm-specification, indicates that the variable should be
5532 placed in a particular register. */
5533 if (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
5535 set_user_assembler_name (decl, asmspec);
5536 DECL_HARD_REGISTER (decl) = 1;
5538 else
5540 if (TREE_CODE (decl) == FUNCTION_DECL
5541 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5542 set_builtin_user_assembler_name (decl, asmspec);
5543 set_user_assembler_name (decl, asmspec);
5547 /* Handle non-variables up front. */
5548 if (TREE_CODE (decl) != VAR_DECL)
5550 rest_of_decl_compilation (decl, toplev, at_eof);
5551 return;
5554 /* If we see a class member here, it should be a static data
5555 member. */
5556 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
5558 gcc_assert (TREE_STATIC (decl));
5559 /* An in-class declaration of a static data member should be
5560 external; it is only a declaration, and not a definition. */
5561 if (init == NULL_TREE)
5562 gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
5565 /* We don't create any RTL for local variables. */
5566 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
5567 return;
5569 /* We defer emission of local statics until the corresponding
5570 DECL_EXPR is expanded. */
5571 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
5573 /* We try to defer namespace-scope static constants so that they are
5574 not emitted into the object file unnecessarily. */
5575 filename = input_filename;
5576 if (!DECL_VIRTUAL_P (decl)
5577 && TREE_READONLY (decl)
5578 && DECL_INITIAL (decl) != NULL_TREE
5579 && DECL_INITIAL (decl) != error_mark_node
5580 && filename != NULL
5581 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
5582 && toplev
5583 && !TREE_PUBLIC (decl))
5585 /* Fool with the linkage of static consts according to #pragma
5586 interface. */
5587 struct c_fileinfo *finfo = get_fileinfo (filename);
5588 if (!finfo->interface_unknown && !TREE_PUBLIC (decl))
5590 TREE_PUBLIC (decl) = 1;
5591 DECL_EXTERNAL (decl) = finfo->interface_only;
5594 defer_p = 1;
5596 /* Likewise for template instantiations. */
5597 else if (DECL_LANG_SPECIFIC (decl)
5598 && DECL_IMPLICIT_INSTANTIATION (decl))
5599 defer_p = 1;
5601 /* If we're not deferring, go ahead and assemble the variable. */
5602 if (!defer_p)
5603 rest_of_decl_compilation (decl, toplev, at_eof);
5606 /* walk_tree helper for wrap_temporary_cleanups, below. */
5608 static tree
5609 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
5611 if (TYPE_P (*stmt_p))
5613 *walk_subtrees = 0;
5614 return NULL_TREE;
5617 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
5619 tree guard = (tree)data;
5620 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
5622 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
5623 /* Tell honor_protect_cleanup_actions to handle this as a separate
5624 cleanup. */
5625 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
5627 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
5630 return NULL_TREE;
5633 /* We're initializing a local variable which has a cleanup GUARD. If there
5634 are any temporaries used in the initializer INIT of this variable, we
5635 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
5636 variable will be cleaned up properly if one of them throws.
5638 Unfortunately, there's no way to express this properly in terms of
5639 nesting, as the regions for the temporaries overlap the region for the
5640 variable itself; if there are two temporaries, the variable needs to be
5641 the first thing destroyed if either of them throws. However, we only
5642 want to run the variable's cleanup if it actually got constructed. So
5643 we need to guard the temporary cleanups with the variable's cleanup if
5644 they are run on the normal path, but not if they are run on the
5645 exceptional path. We implement this by telling
5646 honor_protect_cleanup_actions to strip the variable cleanup from the
5647 exceptional path. */
5649 static void
5650 wrap_temporary_cleanups (tree init, tree guard)
5652 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
5655 /* Generate code to initialize DECL (a local variable). */
5657 static void
5658 initialize_local_var (tree decl, tree init)
5660 tree type = TREE_TYPE (decl);
5661 tree cleanup;
5662 int already_used;
5664 gcc_assert (TREE_CODE (decl) == VAR_DECL
5665 || TREE_CODE (decl) == RESULT_DECL);
5666 gcc_assert (!TREE_STATIC (decl));
5668 if (DECL_SIZE (decl) == NULL_TREE)
5670 /* If we used it already as memory, it must stay in memory. */
5671 DECL_INITIAL (decl) = NULL_TREE;
5672 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5673 return;
5676 if (type == error_mark_node)
5677 return;
5679 /* Compute and store the initial value. */
5680 already_used = TREE_USED (decl) || TREE_USED (type);
5681 if (TREE_USED (type))
5682 DECL_READ_P (decl) = 1;
5684 /* Generate a cleanup, if necessary. */
5685 cleanup = cxx_maybe_build_cleanup (decl);
5687 /* Perform the initialization. */
5688 if (init)
5690 int saved_stmts_are_full_exprs_p;
5692 /* If we're only initializing a single object, guard the destructors
5693 of any temporaries used in its initializer with its destructor.
5694 This isn't right for arrays because each element initialization is
5695 a full-expression. */
5696 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
5697 wrap_temporary_cleanups (init, cleanup);
5699 gcc_assert (building_stmt_tree ());
5700 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
5701 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
5702 finish_expr_stmt (init);
5703 current_stmt_tree ()->stmts_are_full_exprs_p =
5704 saved_stmts_are_full_exprs_p;
5707 /* Set this to 0 so we can tell whether an aggregate which was
5708 initialized was ever used. Don't do this if it has a
5709 destructor, so we don't complain about the 'resource
5710 allocation is initialization' idiom. Now set
5711 attribute((unused)) on types so decls of that type will be
5712 marked used. (see TREE_USED, above.) */
5713 if (TYPE_NEEDS_CONSTRUCTING (type)
5714 && ! already_used
5715 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
5716 && DECL_NAME (decl))
5717 TREE_USED (decl) = 0;
5718 else if (already_used)
5719 TREE_USED (decl) = 1;
5721 if (cleanup)
5722 finish_decl_cleanup (decl, cleanup);
5725 /* DECL is a VAR_DECL for a compiler-generated variable with static
5726 storage duration (like a virtual table) whose initializer is a
5727 compile-time constant. Initialize the variable and provide it to the
5728 back end. */
5730 void
5731 initialize_artificial_var (tree decl, VEC(constructor_elt,gc) *v)
5733 tree init;
5734 gcc_assert (DECL_ARTIFICIAL (decl));
5735 init = build_constructor (TREE_TYPE (decl), v);
5736 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
5737 DECL_INITIAL (decl) = init;
5738 DECL_INITIALIZED_P (decl) = 1;
5739 determine_visibility (decl);
5740 layout_var_decl (decl);
5741 maybe_commonize_var (decl);
5742 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
5745 /* INIT is the initializer for a variable, as represented by the
5746 parser. Returns true iff INIT is value-dependent. */
5748 static bool
5749 value_dependent_init_p (tree init)
5751 if (TREE_CODE (init) == TREE_LIST)
5752 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
5753 return any_value_dependent_elements_p (init);
5754 else if (TREE_CODE (init) == CONSTRUCTOR)
5755 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
5757 VEC(constructor_elt, gc) *elts;
5758 size_t nelts;
5759 size_t i;
5761 elts = CONSTRUCTOR_ELTS (init);
5762 nelts = VEC_length (constructor_elt, elts);
5763 for (i = 0; i < nelts; ++i)
5764 if (value_dependent_init_p (VEC_index (constructor_elt,
5765 elts, i)->value))
5766 return true;
5768 else
5769 /* It must be a simple expression, e.g., int i = 3; */
5770 return value_dependent_expression_p (init);
5772 return false;
5775 /* Finish processing of a declaration;
5776 install its line number and initial value.
5777 If the length of an array type is not known before,
5778 it must be determined now, from the initial value, or it is an error.
5780 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
5781 true, then INIT is an integral constant expression.
5783 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
5784 if the (init) syntax was used. */
5786 void
5787 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
5788 tree asmspec_tree, int flags)
5790 tree type;
5791 tree cleanup;
5792 const char *asmspec = NULL;
5793 int was_readonly = 0;
5794 bool var_definition_p = false;
5795 int saved_processing_template_decl;
5796 tree auto_node;
5798 if (decl == error_mark_node)
5799 return;
5800 else if (! decl)
5802 if (init)
5803 error ("assignment (not initialization) in declaration");
5804 return;
5807 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
5808 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
5809 gcc_assert (TREE_CODE (decl) != PARM_DECL);
5811 type = TREE_TYPE (decl);
5812 if (type == error_mark_node)
5813 return;
5815 /* Assume no cleanup is required. */
5816 cleanup = NULL_TREE;
5817 saved_processing_template_decl = processing_template_decl;
5819 /* If a name was specified, get the string. */
5820 if (global_scope_p (current_binding_level))
5821 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5822 if (asmspec_tree && asmspec_tree != error_mark_node)
5823 asmspec = TREE_STRING_POINTER (asmspec_tree);
5825 if (current_class_type
5826 && CP_DECL_CONTEXT (decl) == current_class_type
5827 && TYPE_BEING_DEFINED (current_class_type)
5828 && (DECL_INITIAL (decl) || init))
5829 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
5831 auto_node = type_uses_auto (type);
5832 if (auto_node)
5834 tree d_init;
5835 if (init == NULL_TREE)
5837 error ("declaration of %q#D has no initializer", decl);
5838 TREE_TYPE (decl) = error_mark_node;
5839 return;
5841 d_init = init;
5842 if (TREE_CODE (d_init) == TREE_LIST)
5843 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
5844 tf_warning_or_error);
5845 d_init = resolve_nondeduced_context (d_init);
5846 if (describable_type (d_init))
5848 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init,
5849 auto_node);
5850 if (type == error_mark_node)
5851 return;
5855 if (TREE_CODE (decl) == FUNCTION_DECL)
5856 validate_constexpr_fundecl (decl);
5858 else if (!ensure_literal_type_for_constexpr_object (decl))
5859 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
5861 if (init && TREE_CODE (decl) == FUNCTION_DECL)
5863 tree clone;
5864 if (init == ridpointers[(int)RID_DELETE])
5866 /* FIXME check this is 1st decl. */
5867 DECL_DELETED_FN (decl) = 1;
5868 DECL_DECLARED_INLINE_P (decl) = 1;
5869 DECL_INITIAL (decl) = error_mark_node;
5870 FOR_EACH_CLONE (clone, decl)
5872 DECL_DELETED_FN (clone) = 1;
5873 DECL_DECLARED_INLINE_P (clone) = 1;
5874 DECL_INITIAL (clone) = error_mark_node;
5876 init = NULL_TREE;
5878 else if (init == ridpointers[(int)RID_DEFAULT])
5880 if (defaultable_fn_check (decl))
5881 DECL_DEFAULTED_FN (decl) = 1;
5882 else
5883 DECL_INITIAL (decl) = NULL_TREE;
5887 if (init && TREE_CODE (decl) == VAR_DECL)
5889 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
5890 if (init_const_expr_p)
5892 /* Set these flags now for templates. We'll update the flags in
5893 store_init_value for instantiations. */
5894 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5895 if (decl_maybe_constant_var_p (decl))
5896 TREE_CONSTANT (decl) = 1;
5900 if (processing_template_decl)
5902 bool type_dependent_p;
5904 /* Add this declaration to the statement-tree. */
5905 if (at_function_scope_p ())
5906 add_decl_expr (decl);
5908 type_dependent_p = dependent_type_p (type);
5910 if (check_for_bare_parameter_packs (init))
5912 init = NULL_TREE;
5913 DECL_INITIAL (decl) = NULL_TREE;
5916 /* Generally, initializers in templates are expanded when the
5917 template is instantiated. But, if DECL is a variable constant
5918 then it can be used in future constant expressions, so its value
5919 must be available. */
5920 if (!(init
5921 && DECL_CLASS_SCOPE_P (decl)
5922 /* We just set TREE_CONSTANT appropriately; see above. */
5923 && TREE_CONSTANT (decl)
5924 && !type_dependent_p
5925 /* FIXME non-value-dependent constant expression */
5926 && !value_dependent_init_p (init)))
5928 if (init)
5929 DECL_INITIAL (decl) = init;
5930 if (TREE_CODE (decl) == VAR_DECL
5931 && !DECL_PRETTY_FUNCTION_P (decl)
5932 && !type_dependent_p)
5933 maybe_deduce_size_from_array_init (decl, init);
5934 goto finish_end;
5937 if (TREE_CODE (init) == TREE_LIST)
5939 /* If the parenthesized-initializer form was used (e.g.,
5940 "int A<N>::i(X)"), then INIT will be a TREE_LIST of initializer
5941 arguments. (There is generally only one.) We convert them
5942 individually. */
5943 tree list = init;
5944 for (; list; list = TREE_CHAIN (list))
5946 tree elt = TREE_VALUE (list);
5947 TREE_VALUE (list) = fold_non_dependent_expr (elt);
5950 else
5951 init = fold_non_dependent_expr (init);
5952 processing_template_decl = 0;
5955 /* Take care of TYPE_DECLs up front. */
5956 if (TREE_CODE (decl) == TYPE_DECL)
5958 if (type != error_mark_node
5959 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
5961 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
5962 warning (0, "shadowing previous type declaration of %q#D", decl);
5963 set_identifier_type_value (DECL_NAME (decl), decl);
5966 /* If we have installed this as the canonical typedef for this
5967 type, and that type has not been defined yet, delay emitting
5968 the debug information for it, as we will emit it later. */
5969 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
5970 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
5971 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5973 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
5974 at_eof);
5975 goto finish_end;
5978 /* A reference will be modified here, as it is initialized. */
5979 if (! DECL_EXTERNAL (decl)
5980 && TREE_READONLY (decl)
5981 && TREE_CODE (type) == REFERENCE_TYPE)
5983 was_readonly = 1;
5984 TREE_READONLY (decl) = 0;
5987 if (TREE_CODE (decl) == VAR_DECL)
5989 /* Only variables with trivial initialization and destruction can
5990 have thread-local storage. */
5991 if (DECL_THREAD_LOCAL_P (decl)
5992 && (type_has_nontrivial_default_init (TREE_TYPE (decl))
5993 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
5994 error ("%qD cannot be thread-local because it has non-trivial "
5995 "type %qT", decl, TREE_TYPE (decl));
5996 /* If this is a local variable that will need a mangled name,
5997 register it now. We must do this before processing the
5998 initializer for the variable, since the initialization might
5999 require a guard variable, and since the mangled name of the
6000 guard variable will depend on the mangled name of this
6001 variable. */
6002 if (DECL_FUNCTION_SCOPE_P (decl)
6003 && TREE_STATIC (decl)
6004 && !DECL_ARTIFICIAL (decl))
6006 push_local_name (decl);
6007 if (DECL_CONSTRUCTOR_P (current_function_decl)
6008 || DECL_DESTRUCTOR_P (current_function_decl))
6009 /* Normally local_decls is populated during GIMPLE lowering,
6010 but [cd]tors are never actually compiled directly. We need
6011 to put statics on the list so we can deal with the label
6012 address extension. */
6013 add_local_decl (cfun, decl);
6016 /* Convert the initializer to the type of DECL, if we have not
6017 already initialized DECL. */
6018 if (!DECL_INITIALIZED_P (decl)
6019 /* If !DECL_EXTERNAL then DECL is being defined. In the
6020 case of a static data member initialized inside the
6021 class-specifier, there can be an initializer even if DECL
6022 is *not* defined. */
6023 && (!DECL_EXTERNAL (decl) || init))
6025 if (TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6027 tree jclass
6028 = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
6029 /* Allow libjava/prims.cc define primitive classes. */
6030 if (init != NULL_TREE
6031 || jclass == NULL_TREE
6032 || TREE_CODE (jclass) != TYPE_DECL
6033 || !POINTER_TYPE_P (TREE_TYPE (jclass))
6034 || !same_type_ignoring_top_level_qualifiers_p
6035 (type, TREE_TYPE (TREE_TYPE (jclass))))
6036 error ("Java object %qD not allocated with %<new%>", decl);
6037 init = NULL_TREE;
6039 init = check_initializer (decl, init, flags, &cleanup);
6040 /* Thread-local storage cannot be dynamically initialized. */
6041 if (DECL_THREAD_LOCAL_P (decl) && init)
6043 error ("%qD is thread-local and so cannot be dynamically "
6044 "initialized", decl);
6045 init = NULL_TREE;
6048 /* Check that the initializer for a static data member was a
6049 constant. Although we check in the parser that the
6050 initializer is an integral constant expression, we do not
6051 simplify division-by-zero at the point at which it
6052 occurs. Therefore, in:
6054 struct S { static const int i = 7 / 0; };
6056 we issue an error at this point. It would
6057 probably be better to forbid division by zero in
6058 integral constant expressions. */
6059 if (DECL_EXTERNAL (decl) && init)
6061 error ("%qD cannot be initialized by a non-constant expression"
6062 " when being declared", decl);
6063 DECL_INITIALIZED_IN_CLASS_P (decl) = 0;
6064 init = NULL_TREE;
6067 /* Handle:
6069 [dcl.init]
6071 The memory occupied by any object of static storage
6072 duration is zero-initialized at program startup before
6073 any other initialization takes place.
6075 We cannot create an appropriate initializer until after
6076 the type of DECL is finalized. If DECL_INITIAL is set,
6077 then the DECL is statically initialized, and any
6078 necessary zero-initialization has already been performed. */
6079 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
6080 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
6081 /*nelts=*/NULL_TREE,
6082 /*static_storage_p=*/true);
6083 /* Remember that the initialization for this variable has
6084 taken place. */
6085 DECL_INITIALIZED_P (decl) = 1;
6086 /* This declaration is the definition of this variable,
6087 unless we are initializing a static data member within
6088 the class specifier. */
6089 if (!DECL_EXTERNAL (decl))
6090 var_definition_p = true;
6092 /* If the variable has an array type, lay out the type, even if
6093 there is no initializer. It is valid to index through the
6094 array, and we must get TYPE_ALIGN set correctly on the array
6095 type. */
6096 else if (TREE_CODE (type) == ARRAY_TYPE)
6097 layout_type (type);
6099 if (!processing_template_decl
6100 && TREE_STATIC (decl)
6101 && !at_function_scope_p ()
6102 && current_function_decl == NULL)
6103 /* So decl is a global variable or a static member of a
6104 non local class. Record the types it uses
6105 so that we can decide later to emit debug info for them. */
6106 record_types_used_by_current_var_decl (decl);
6108 else if (TREE_CODE (decl) == FIELD_DECL
6109 && TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6110 error ("non-static data member %qD has Java class type", decl);
6112 /* Add this declaration to the statement-tree. This needs to happen
6113 after the call to check_initializer so that the DECL_EXPR for a
6114 reference temp is added before the DECL_EXPR for the reference itself. */
6115 if (at_function_scope_p ())
6116 add_decl_expr (decl);
6118 /* Let the middle end know about variables and functions -- but not
6119 static data members in uninstantiated class templates. */
6120 if (!saved_processing_template_decl
6121 && (TREE_CODE (decl) == VAR_DECL
6122 || TREE_CODE (decl) == FUNCTION_DECL))
6124 if (TREE_CODE (decl) == VAR_DECL)
6126 layout_var_decl (decl);
6127 maybe_commonize_var (decl);
6130 /* This needs to happen after the linkage is set. */
6131 determine_visibility (decl);
6133 if (var_definition_p && TREE_STATIC (decl))
6135 /* If a TREE_READONLY variable needs initialization
6136 at runtime, it is no longer readonly and we need to
6137 avoid MEM_READONLY_P being set on RTL created for it. */
6138 if (init)
6140 if (TREE_READONLY (decl))
6141 TREE_READONLY (decl) = 0;
6142 was_readonly = 0;
6144 else if (was_readonly)
6145 TREE_READONLY (decl) = 1;
6148 make_rtl_for_nonlocal_decl (decl, init, asmspec);
6150 /* Check for abstractness of the type. Notice that there is no
6151 need to strip array types here since the check for those types
6152 is already done within create_array_type_for_decl. */
6153 if (TREE_CODE (type) == FUNCTION_TYPE
6154 || TREE_CODE (type) == METHOD_TYPE)
6155 abstract_virtuals_error (decl, TREE_TYPE (type));
6156 else
6157 abstract_virtuals_error (decl, type);
6159 if (TREE_TYPE (decl) == error_mark_node)
6160 /* No initialization required. */
6162 else if (TREE_CODE (decl) == FUNCTION_DECL)
6164 if (init)
6166 if (init == ridpointers[(int)RID_DEFAULT])
6168 /* An out-of-class default definition is defined at
6169 the point where it is explicitly defaulted. */
6170 if (DECL_DELETED_FN (decl))
6171 maybe_explain_implicit_delete (decl);
6172 else if (DECL_INITIAL (decl) == error_mark_node)
6173 synthesize_method (decl);
6175 else
6176 error ("function %q#D is initialized like a variable", decl);
6178 /* else no initialization required. */
6180 else if (DECL_EXTERNAL (decl)
6181 && ! (DECL_LANG_SPECIFIC (decl)
6182 && DECL_NOT_REALLY_EXTERN (decl)))
6184 if (init)
6185 DECL_INITIAL (decl) = init;
6187 /* A variable definition. */
6188 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6189 /* Initialize the local variable. */
6190 initialize_local_var (decl, init);
6192 /* If a variable is defined, and then a subsequent
6193 definition with external linkage is encountered, we will
6194 get here twice for the same variable. We want to avoid
6195 calling expand_static_init more than once. For variables
6196 that are not static data members, we can call
6197 expand_static_init only when we actually process the
6198 initializer. It is not legal to redeclare a static data
6199 member, so this issue does not arise in that case. */
6200 else if (var_definition_p && TREE_STATIC (decl))
6201 expand_static_init (decl, init);
6204 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
6205 reference, insert it in the statement-tree now. */
6206 if (cleanup)
6207 push_cleanup (decl, cleanup, false);
6209 finish_end:
6210 processing_template_decl = saved_processing_template_decl;
6212 if (was_readonly)
6213 TREE_READONLY (decl) = 1;
6216 /* Returns a declaration for a VAR_DECL as if:
6218 extern "C" TYPE NAME;
6220 had been seen. Used to create compiler-generated global
6221 variables. */
6223 static tree
6224 declare_global_var (tree name, tree type)
6226 tree decl;
6228 push_to_top_level ();
6229 decl = build_decl (input_location, VAR_DECL, name, type);
6230 TREE_PUBLIC (decl) = 1;
6231 DECL_EXTERNAL (decl) = 1;
6232 DECL_ARTIFICIAL (decl) = 1;
6233 /* If the user has explicitly declared this variable (perhaps
6234 because the code we are compiling is part of a low-level runtime
6235 library), then it is possible that our declaration will be merged
6236 with theirs by pushdecl. */
6237 decl = pushdecl (decl);
6238 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
6239 pop_from_top_level ();
6241 return decl;
6244 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
6245 if "__cxa_atexit" is not being used) corresponding to the function
6246 to be called when the program exits. */
6248 static tree
6249 get_atexit_fn_ptr_type (void)
6251 tree fn_type;
6253 if (!atexit_fn_ptr_type_node)
6255 tree arg_type;
6256 if (flag_use_cxa_atexit
6257 && !targetm.cxx.use_atexit_for_cxa_atexit ())
6258 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
6259 arg_type = ptr_type_node;
6260 else
6261 /* The parameter to "atexit" is "void (*)(void)". */
6262 arg_type = NULL_TREE;
6264 fn_type = build_function_type_list (void_type_node,
6265 arg_type, NULL_TREE);
6266 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
6269 return atexit_fn_ptr_type_node;
6272 /* Returns a pointer to the `atexit' function. Note that if
6273 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
6274 `__cxa_atexit' function specified in the IA64 C++ ABI. */
6276 static tree
6277 get_atexit_node (void)
6279 tree atexit_fndecl;
6280 tree fn_type;
6281 tree fn_ptr_type;
6282 const char *name;
6283 bool use_aeabi_atexit;
6285 if (atexit_node)
6286 return atexit_node;
6288 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
6290 /* The declaration for `__cxa_atexit' is:
6292 int __cxa_atexit (void (*)(void *), void *, void *)
6294 We build up the argument types and then the function type
6295 itself. */
6296 tree argtype0, argtype1, argtype2;
6298 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
6299 /* First, build the pointer-to-function type for the first
6300 argument. */
6301 fn_ptr_type = get_atexit_fn_ptr_type ();
6302 /* Then, build the rest of the argument types. */
6303 argtype2 = ptr_type_node;
6304 if (use_aeabi_atexit)
6306 argtype1 = fn_ptr_type;
6307 argtype0 = ptr_type_node;
6309 else
6311 argtype1 = ptr_type_node;
6312 argtype0 = fn_ptr_type;
6314 /* And the final __cxa_atexit type. */
6315 fn_type = build_function_type_list (integer_type_node,
6316 argtype0, argtype1, argtype2,
6317 NULL_TREE);
6318 fn_ptr_type = build_pointer_type (fn_type);
6319 if (use_aeabi_atexit)
6320 name = "__aeabi_atexit";
6321 else
6322 name = "__cxa_atexit";
6324 else
6326 /* The declaration for `atexit' is:
6328 int atexit (void (*)());
6330 We build up the argument types and then the function type
6331 itself. */
6332 fn_ptr_type = get_atexit_fn_ptr_type ();
6333 /* Build the final atexit type. */
6334 fn_type = build_function_type_list (integer_type_node,
6335 fn_ptr_type, NULL_TREE);
6336 name = "atexit";
6339 /* Now, build the function declaration. */
6340 push_lang_context (lang_name_c);
6341 atexit_fndecl = build_library_fn_ptr (name, fn_type);
6342 mark_used (atexit_fndecl);
6343 pop_lang_context ();
6344 atexit_node = decay_conversion (atexit_fndecl);
6346 return atexit_node;
6349 /* Returns the __dso_handle VAR_DECL. */
6351 static tree
6352 get_dso_handle_node (void)
6354 if (dso_handle_node)
6355 return dso_handle_node;
6357 /* Declare the variable. */
6358 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
6359 ptr_type_node);
6361 return dso_handle_node;
6364 /* Begin a new function with internal linkage whose job will be simply
6365 to destroy some particular variable. */
6367 static GTY(()) int start_cleanup_cnt;
6369 static tree
6370 start_cleanup_fn (void)
6372 char name[32];
6373 tree fntype;
6374 tree fndecl;
6375 bool use_cxa_atexit = flag_use_cxa_atexit
6376 && !targetm.cxx.use_atexit_for_cxa_atexit ();
6378 push_to_top_level ();
6380 /* No need to mangle this. */
6381 push_lang_context (lang_name_c);
6383 /* Build the name of the function. */
6384 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
6385 /* Build the function declaration. */
6386 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
6387 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
6388 /* It's a function with internal linkage, generated by the
6389 compiler. */
6390 TREE_PUBLIC (fndecl) = 0;
6391 DECL_ARTIFICIAL (fndecl) = 1;
6392 /* Make the function `inline' so that it is only emitted if it is
6393 actually needed. It is unlikely that it will be inlined, since
6394 it is only called via a function pointer, but we avoid unnecessary
6395 emissions this way. */
6396 DECL_DECLARED_INLINE_P (fndecl) = 1;
6397 DECL_INTERFACE_KNOWN (fndecl) = 1;
6398 /* Build the parameter. */
6399 if (use_cxa_atexit)
6401 tree parmdecl;
6403 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
6404 DECL_CONTEXT (parmdecl) = fndecl;
6405 TREE_USED (parmdecl) = 1;
6406 DECL_READ_P (parmdecl) = 1;
6407 DECL_ARGUMENTS (fndecl) = parmdecl;
6410 pushdecl (fndecl);
6411 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
6413 pop_lang_context ();
6415 return current_function_decl;
6418 /* Finish the cleanup function begun by start_cleanup_fn. */
6420 static void
6421 end_cleanup_fn (void)
6423 expand_or_defer_fn (finish_function (0));
6425 pop_from_top_level ();
6428 /* Generate code to handle the destruction of DECL, an object with
6429 static storage duration. */
6431 tree
6432 register_dtor_fn (tree decl)
6434 tree cleanup;
6435 tree compound_stmt;
6436 tree fcall;
6437 tree type;
6438 bool use_dtor;
6439 tree arg0, arg1 = NULL_TREE, arg2 = NULL_TREE;
6441 type = TREE_TYPE (decl);
6442 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
6443 return void_zero_node;
6445 /* If we're using "__cxa_atexit" (or "__aeabi_atexit"), and DECL is
6446 a class object, we can just pass the destructor to
6447 "__cxa_atexit"; we don't have to build a temporary function to do
6448 the cleanup. */
6449 use_dtor = (flag_use_cxa_atexit
6450 && !targetm.cxx.use_atexit_for_cxa_atexit ()
6451 && CLASS_TYPE_P (type));
6452 if (use_dtor)
6454 int idx;
6456 /* Find the destructor. */
6457 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
6458 gcc_assert (idx >= 0);
6459 cleanup = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), idx);
6460 /* Make sure it is accessible. */
6461 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup);
6463 else
6465 /* Call build_cleanup before we enter the anonymous function so
6466 that any access checks will be done relative to the current
6467 scope, rather than the scope of the anonymous function. */
6468 build_cleanup (decl);
6470 /* Now start the function. */
6471 cleanup = start_cleanup_fn ();
6473 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
6474 to the original function, rather than the anonymous one. That
6475 will make the back end think that nested functions are in use,
6476 which causes confusion. */
6477 push_deferring_access_checks (dk_no_check);
6478 fcall = build_cleanup (decl);
6479 pop_deferring_access_checks ();
6481 /* Create the body of the anonymous function. */
6482 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
6483 finish_expr_stmt (fcall);
6484 finish_compound_stmt (compound_stmt);
6485 end_cleanup_fn ();
6488 /* Call atexit with the cleanup function. */
6489 mark_used (cleanup);
6490 cleanup = build_address (cleanup);
6491 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
6493 tree addr;
6495 if (use_dtor)
6497 /* We must convert CLEANUP to the type that "__cxa_atexit"
6498 expects. */
6499 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
6500 /* "__cxa_atexit" will pass the address of DECL to the
6501 cleanup function. */
6502 mark_used (decl);
6503 addr = build_address (decl);
6504 /* The declared type of the parameter to "__cxa_atexit" is
6505 "void *". For plain "T*", we could just let the
6506 machinery in cp_build_function_call convert it -- but if the
6507 type is "cv-qualified T *", then we need to convert it
6508 before passing it in, to avoid spurious errors. */
6509 addr = build_nop (ptr_type_node, addr);
6511 else
6512 /* Since the cleanup functions we build ignore the address
6513 they're given, there's no reason to pass the actual address
6514 in, and, in general, it's cheaper to pass NULL than any
6515 other value. */
6516 addr = null_pointer_node;
6517 arg2 = cp_build_addr_expr (get_dso_handle_node (),
6518 tf_warning_or_error);
6519 if (targetm.cxx.use_aeabi_atexit ())
6521 arg1 = cleanup;
6522 arg0 = addr;
6524 else
6526 arg1 = addr;
6527 arg0 = cleanup;
6530 else
6531 arg0 = cleanup;
6532 return cp_build_function_call_nary (get_atexit_node (), tf_warning_or_error,
6533 arg0, arg1, arg2, NULL_TREE);
6536 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
6537 is its initializer. Generate code to handle the construction
6538 and destruction of DECL. */
6540 static void
6541 expand_static_init (tree decl, tree init)
6543 gcc_assert (TREE_CODE (decl) == VAR_DECL);
6544 gcc_assert (TREE_STATIC (decl));
6546 /* Some variables require no dynamic initialization. */
6547 if (!init
6548 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
6549 return;
6551 if (DECL_FUNCTION_SCOPE_P (decl))
6553 /* Emit code to perform this initialization but once. */
6554 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
6555 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
6556 tree guard, guard_addr;
6557 tree acquire_fn, release_fn, abort_fn;
6558 tree flag, begin;
6560 /* Emit code to perform this initialization but once. This code
6561 looks like:
6563 static <type> guard;
6564 if (!guard.first_byte) {
6565 if (__cxa_guard_acquire (&guard)) {
6566 bool flag = false;
6567 try {
6568 // Do initialization.
6569 flag = true; __cxa_guard_release (&guard);
6570 // Register variable for destruction at end of program.
6571 } catch {
6572 if (!flag) __cxa_guard_abort (&guard);
6576 Note that the `flag' variable is only set to 1 *after* the
6577 initialization is complete. This ensures that an exception,
6578 thrown during the construction, will cause the variable to
6579 reinitialized when we pass through this code again, as per:
6581 [stmt.dcl]
6583 If the initialization exits by throwing an exception, the
6584 initialization is not complete, so it will be tried again
6585 the next time control enters the declaration.
6587 This process should be thread-safe, too; multiple threads
6588 should not be able to initialize the variable more than
6589 once. */
6591 /* Create the guard variable. */
6592 guard = get_guard (decl);
6594 /* This optimization isn't safe on targets with relaxed memory
6595 consistency. On such targets we force synchronization in
6596 __cxa_guard_acquire. */
6597 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
6599 /* Begin the conditional initialization. */
6600 if_stmt = begin_if_stmt ();
6601 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
6602 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
6605 if (flag_threadsafe_statics)
6607 guard_addr = build_address (guard);
6609 acquire_fn = get_identifier ("__cxa_guard_acquire");
6610 release_fn = get_identifier ("__cxa_guard_release");
6611 abort_fn = get_identifier ("__cxa_guard_abort");
6612 if (!get_global_value_if_present (acquire_fn, &acquire_fn))
6614 tree vfntype = build_function_type_list (void_type_node,
6615 TREE_TYPE (guard_addr),
6616 NULL_TREE);
6617 acquire_fn = push_library_fn
6618 (acquire_fn, build_function_type_list (integer_type_node,
6619 TREE_TYPE (guard_addr),
6620 NULL_TREE),
6621 NULL_TREE);
6622 release_fn = push_library_fn (release_fn, vfntype, NULL_TREE);
6623 abort_fn = push_library_fn (abort_fn, vfntype, NULL_TREE);
6625 else
6627 release_fn = identifier_global_value (release_fn);
6628 abort_fn = identifier_global_value (abort_fn);
6631 inner_if_stmt = begin_if_stmt ();
6632 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
6633 inner_if_stmt);
6635 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
6636 begin = get_target_expr (boolean_false_node);
6637 flag = TARGET_EXPR_SLOT (begin);
6639 TARGET_EXPR_CLEANUP (begin)
6640 = build3 (COND_EXPR, void_type_node, flag,
6641 void_zero_node,
6642 build_call_n (abort_fn, 1, guard_addr));
6643 CLEANUP_EH_ONLY (begin) = 1;
6645 /* Do the initialization itself. */
6646 init = add_stmt_to_compound (begin, init);
6647 init = add_stmt_to_compound
6648 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
6649 init = add_stmt_to_compound
6650 (init, build_call_n (release_fn, 1, guard_addr));
6652 else
6653 init = add_stmt_to_compound (init, set_guard (guard));
6655 /* Use atexit to register a function for destroying this static
6656 variable. */
6657 init = add_stmt_to_compound (init, register_dtor_fn (decl));
6659 finish_expr_stmt (init);
6661 if (flag_threadsafe_statics)
6663 finish_compound_stmt (inner_then_clause);
6664 finish_then_clause (inner_if_stmt);
6665 finish_if_stmt (inner_if_stmt);
6668 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
6670 finish_compound_stmt (then_clause);
6671 finish_then_clause (if_stmt);
6672 finish_if_stmt (if_stmt);
6675 else
6676 static_aggregates = tree_cons (init, decl, static_aggregates);
6680 /* Make TYPE a complete type based on INITIAL_VALUE.
6681 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6682 2 if there was no information (in which case assume 0 if DO_DEFAULT),
6683 3 if the initializer list is empty (in pedantic mode). */
6686 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
6688 int failure;
6689 tree type, elt_type;
6691 if (initial_value)
6693 unsigned HOST_WIDE_INT i;
6694 tree value;
6696 /* An array of character type can be initialized from a
6697 brace-enclosed string constant.
6699 FIXME: this code is duplicated from reshape_init. Probably
6700 we should just call reshape_init here? */
6701 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
6702 && TREE_CODE (initial_value) == CONSTRUCTOR
6703 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (initial_value)))
6705 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
6706 tree value = VEC_index (constructor_elt, v, 0)->value;
6708 if (TREE_CODE (value) == STRING_CST
6709 && VEC_length (constructor_elt, v) == 1)
6710 initial_value = value;
6713 /* If any of the elements are parameter packs, we can't actually
6714 complete this type now because the array size is dependent. */
6715 if (TREE_CODE (initial_value) == CONSTRUCTOR)
6717 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
6718 i, value)
6720 if (PACK_EXPANSION_P (value))
6721 return 0;
6726 failure = complete_array_type (ptype, initial_value, do_default);
6728 /* We can create the array before the element type is complete, which
6729 means that we didn't have these two bits set in the original type
6730 either. In completing the type, we are expected to propagate these
6731 bits. See also complete_type which does the same thing for arrays
6732 of fixed size. */
6733 type = *ptype;
6734 if (TYPE_DOMAIN (type))
6736 elt_type = TREE_TYPE (type);
6737 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
6738 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
6739 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
6742 return failure;
6745 /* Return zero if something is declared to be a member of type
6746 CTYPE when in the context of CUR_TYPE. STRING is the error
6747 message to print in that case. Otherwise, quietly return 1. */
6749 static int
6750 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
6752 if (ctype && ctype != cur_type)
6754 if (flags == DTOR_FLAG)
6755 error ("destructor for alien class %qT cannot be a member", ctype);
6756 else
6757 error ("constructor for alien class %qT cannot be a member", ctype);
6758 return 0;
6760 return 1;
6763 /* Subroutine of `grokdeclarator'. */
6765 /* Generate errors possibly applicable for a given set of specifiers.
6766 This is for ARM $7.1.2. */
6768 static void
6769 bad_specifiers (tree object,
6770 enum bad_spec_place type,
6771 int virtualp,
6772 int quals,
6773 int inlinep,
6774 int friendp,
6775 int raises)
6777 switch (type)
6779 case BSP_VAR:
6780 if (virtualp)
6781 error ("%qD declared as a %<virtual%> variable", object);
6782 if (inlinep)
6783 error ("%qD declared as an %<inline%> variable", object);
6784 if (quals)
6785 error ("%<const%> and %<volatile%> function specifiers on "
6786 "%qD invalid in variable declaration", object);
6787 break;
6788 case BSP_PARM:
6789 if (virtualp)
6790 error ("%qD declared as a %<virtual%> parameter", object);
6791 if (inlinep)
6792 error ("%qD declared as an %<inline%> parameter", object);
6793 if (quals)
6794 error ("%<const%> and %<volatile%> function specifiers on "
6795 "%qD invalid in parameter declaration", object);
6796 break;
6797 case BSP_TYPE:
6798 if (virtualp)
6799 error ("%qD declared as a %<virtual%> type", object);
6800 if (inlinep)
6801 error ("%qD declared as an %<inline%> type", object);
6802 if (quals)
6803 error ("%<const%> and %<volatile%> function specifiers on "
6804 "%qD invalid in type declaration", object);
6805 break;
6806 case BSP_FIELD:
6807 if (virtualp)
6808 error ("%qD declared as a %<virtual%> field", object);
6809 if (inlinep)
6810 error ("%qD declared as an %<inline%> field", object);
6811 if (quals)
6812 error ("%<const%> and %<volatile%> function specifiers on "
6813 "%qD invalid in field declaration", object);
6814 break;
6815 default:
6816 gcc_unreachable();
6818 if (friendp)
6819 error ("%q+D declared as a friend", object);
6820 if (raises
6821 && (TREE_CODE (object) == TYPE_DECL
6822 || (!TYPE_PTRFN_P (TREE_TYPE (object))
6823 && !TYPE_REFFN_P (TREE_TYPE (object))
6824 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
6825 error ("%q+D declared with an exception specification", object);
6828 /* DECL is a member function or static data member and is presently
6829 being defined. Check that the definition is taking place in a
6830 valid namespace. */
6832 static void
6833 check_class_member_definition_namespace (tree decl)
6835 /* These checks only apply to member functions and static data
6836 members. */
6837 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6838 || TREE_CODE (decl) == VAR_DECL);
6839 /* We check for problems with specializations in pt.c in
6840 check_specialization_namespace, where we can issue better
6841 diagnostics. */
6842 if (processing_specialization)
6843 return;
6844 /* There are no restrictions on the placement of
6845 explicit instantiations. */
6846 if (processing_explicit_instantiation)
6847 return;
6848 /* [class.mfct]
6850 A member function definition that appears outside of the
6851 class definition shall appear in a namespace scope enclosing
6852 the class definition.
6854 [class.static.data]
6856 The definition for a static data member shall appear in a
6857 namespace scope enclosing the member's class definition. */
6858 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
6859 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
6860 decl, DECL_CONTEXT (decl));
6863 /* Build a PARM_DECL for the "this" parameter. TYPE is the
6864 METHOD_TYPE for a non-static member function; QUALS are the
6865 cv-qualifiers that apply to the function. */
6867 tree
6868 build_this_parm (tree type, cp_cv_quals quals)
6870 tree this_type;
6871 tree qual_type;
6872 tree parm;
6873 cp_cv_quals this_quals;
6875 this_type = TREE_VALUE (TYPE_ARG_TYPES (type));
6876 /* The `this' parameter is implicitly `const'; it cannot be
6877 assigned to. */
6878 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
6879 qual_type = cp_build_qualified_type (this_type, this_quals);
6880 parm = build_artificial_parm (this_identifier, qual_type);
6881 cp_apply_type_quals_to_decl (this_quals, parm);
6882 return parm;
6885 /* CTYPE is class type, or null if non-class.
6886 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
6887 or METHOD_TYPE.
6888 DECLARATOR is the function's name.
6889 PARMS is a chain of PARM_DECLs for the function.
6890 VIRTUALP is truthvalue of whether the function is virtual or not.
6891 FLAGS are to be passed through to `grokclassfn'.
6892 QUALS are qualifiers indicating whether the function is `const'
6893 or `volatile'.
6894 RAISES is a list of exceptions that this function can raise.
6895 CHECK is 1 if we must find this method in CTYPE, 0 if we should
6896 not look, and -1 if we should not call `grokclassfn' at all.
6898 SFK is the kind of special function (if any) for the new function.
6900 Returns `NULL_TREE' if something goes wrong, after issuing
6901 applicable error messages. */
6903 static tree
6904 grokfndecl (tree ctype,
6905 tree type,
6906 tree declarator,
6907 tree parms,
6908 tree orig_declarator,
6909 int virtualp,
6910 enum overload_flags flags,
6911 cp_cv_quals quals,
6912 tree raises,
6913 int check,
6914 int friendp,
6915 int publicp,
6916 int inlinep,
6917 special_function_kind sfk,
6918 bool funcdef_flag,
6919 int template_count,
6920 tree in_namespace,
6921 tree* attrlist,
6922 location_t location)
6924 tree decl;
6925 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
6926 tree t;
6928 if (raises)
6929 type = build_exception_variant (type, raises);
6931 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
6933 /* If we have an explicit location, use it, otherwise use whatever
6934 build_lang_decl used (probably input_location). */
6935 if (location != UNKNOWN_LOCATION)
6936 DECL_SOURCE_LOCATION (decl) = location;
6938 if (TREE_CODE (type) == METHOD_TYPE)
6940 tree parm;
6941 parm = build_this_parm (type, quals);
6942 DECL_CHAIN (parm) = parms;
6943 parms = parm;
6945 DECL_ARGUMENTS (decl) = parms;
6946 for (t = parms; t; t = DECL_CHAIN (t))
6947 DECL_CONTEXT (t) = decl;
6948 /* Propagate volatile out from type to decl. */
6949 if (TYPE_VOLATILE (type))
6950 TREE_THIS_VOLATILE (decl) = 1;
6952 /* Setup decl according to sfk. */
6953 switch (sfk)
6955 case sfk_constructor:
6956 case sfk_copy_constructor:
6957 case sfk_move_constructor:
6958 DECL_CONSTRUCTOR_P (decl) = 1;
6959 break;
6960 case sfk_destructor:
6961 DECL_DESTRUCTOR_P (decl) = 1;
6962 break;
6963 default:
6964 break;
6967 /* If pointers to member functions use the least significant bit to
6968 indicate whether a function is virtual, ensure a pointer
6969 to this function will have that bit clear. */
6970 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6971 && TREE_CODE (type) == METHOD_TYPE
6972 && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT)
6973 DECL_ALIGN (decl) = 2 * BITS_PER_UNIT;
6975 if (friendp
6976 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
6978 if (funcdef_flag)
6979 error
6980 ("defining explicit specialization %qD in friend declaration",
6981 orig_declarator);
6982 else
6984 tree fns = TREE_OPERAND (orig_declarator, 0);
6985 tree args = TREE_OPERAND (orig_declarator, 1);
6987 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
6989 /* Something like `template <class T> friend void f<T>()'. */
6990 error ("invalid use of template-id %qD in declaration "
6991 "of primary template",
6992 orig_declarator);
6993 return NULL_TREE;
6997 /* A friend declaration of the form friend void f<>(). Record
6998 the information in the TEMPLATE_ID_EXPR. */
6999 SET_DECL_IMPLICIT_INSTANTIATION (decl);
7001 if (TREE_CODE (fns) == COMPONENT_REF)
7003 /* Due to bison parser ickiness, we will have already looked
7004 up an operator_name or PFUNCNAME within the current class
7005 (see template_id in parse.y). If the current class contains
7006 such a name, we'll get a COMPONENT_REF here. Undo that. */
7008 gcc_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
7009 == current_class_type);
7010 fns = TREE_OPERAND (fns, 1);
7012 gcc_assert (TREE_CODE (fns) == IDENTIFIER_NODE
7013 || TREE_CODE (fns) == OVERLOAD);
7014 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
7016 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
7017 if (TREE_PURPOSE (t)
7018 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
7020 error ("default arguments are not allowed in declaration "
7021 "of friend template specialization %qD",
7022 decl);
7023 return NULL_TREE;
7026 if (inlinep)
7028 error ("%<inline%> is not allowed in declaration of friend "
7029 "template specialization %qD",
7030 decl);
7031 return NULL_TREE;
7036 /* If this decl has namespace scope, set that up. */
7037 if (in_namespace)
7038 set_decl_namespace (decl, in_namespace, friendp);
7039 else if (!ctype)
7040 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
7042 /* `main' and builtins have implicit 'C' linkage. */
7043 if ((MAIN_NAME_P (declarator)
7044 || (IDENTIFIER_LENGTH (declarator) > 10
7045 && IDENTIFIER_POINTER (declarator)[0] == '_'
7046 && IDENTIFIER_POINTER (declarator)[1] == '_'
7047 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
7048 && current_lang_name == lang_name_cplusplus
7049 && ctype == NULL_TREE
7050 && DECL_FILE_SCOPE_P (decl))
7051 SET_DECL_LANGUAGE (decl, lang_c);
7053 /* Should probably propagate const out from type to decl I bet (mrs). */
7054 if (staticp)
7056 DECL_STATIC_FUNCTION_P (decl) = 1;
7057 DECL_CONTEXT (decl) = ctype;
7060 if (ctype)
7062 DECL_CONTEXT (decl) = ctype;
7063 if (funcdef_flag)
7064 check_class_member_definition_namespace (decl);
7067 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
7069 if (processing_template_decl)
7070 error ("cannot declare %<::main%> to be a template");
7071 if (inlinep)
7072 error ("cannot declare %<::main%> to be inline");
7073 if (!publicp)
7074 error ("cannot declare %<::main%> to be static");
7075 inlinep = 0;
7076 publicp = 1;
7079 /* Members of anonymous types and local classes have no linkage; make
7080 them internal. If a typedef is made later, this will be changed. */
7081 if (ctype && (TYPE_ANONYMOUS_P (ctype)
7082 || decl_function_context (TYPE_MAIN_DECL (ctype))))
7083 publicp = 0;
7085 if (publicp && cxx_dialect == cxx98)
7087 /* [basic.link]: A name with no linkage (notably, the name of a class
7088 or enumeration declared in a local scope) shall not be used to
7089 declare an entity with linkage.
7091 DR 757 relaxes this restriction for C++0x. */
7092 t = no_linkage_check (TREE_TYPE (decl),
7093 /*relaxed_p=*/false);
7094 if (t)
7096 if (TYPE_ANONYMOUS_P (t))
7098 if (DECL_EXTERN_C_P (decl))
7099 /* Allow this; it's pretty common in C. */;
7100 else
7102 permerror (input_location, "anonymous type with no linkage "
7103 "used to declare function %q#D with linkage",
7104 decl);
7105 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
7106 permerror (input_location, "%q+#D does not refer to the unqualified "
7107 "type, so it is not used for linkage",
7108 TYPE_NAME (t));
7111 else
7112 permerror (input_location, "type %qT with no linkage used to "
7113 "declare function %q#D with linkage", t, decl);
7117 TREE_PUBLIC (decl) = publicp;
7118 if (! publicp)
7120 DECL_INTERFACE_KNOWN (decl) = 1;
7121 DECL_NOT_REALLY_EXTERN (decl) = 1;
7124 /* If the declaration was declared inline, mark it as such. */
7125 if (inlinep)
7126 DECL_DECLARED_INLINE_P (decl) = 1;
7127 if (inlinep & 2)
7128 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7130 DECL_EXTERNAL (decl) = 1;
7131 if (quals && TREE_CODE (type) == FUNCTION_TYPE)
7133 error (ctype
7134 ? G_("static member function %qD cannot have cv-qualifier")
7135 : G_("non-member function %qD cannot have cv-qualifier"),
7136 decl);
7137 quals = TYPE_UNQUALIFIED;
7140 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
7141 && !grok_op_properties (decl, /*complain=*/true))
7142 return NULL_TREE;
7144 if (funcdef_flag)
7145 /* Make the init_value nonzero so pushdecl knows this is not
7146 tentative. error_mark_node is replaced later with the BLOCK. */
7147 DECL_INITIAL (decl) = error_mark_node;
7149 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
7150 TREE_NOTHROW (decl) = 1;
7152 /* Caller will do the rest of this. */
7153 if (check < 0)
7154 return decl;
7156 if (ctype != NULL_TREE)
7157 grokclassfn (ctype, decl, flags);
7159 decl = check_explicit_specialization (orig_declarator, decl,
7160 template_count,
7161 2 * funcdef_flag +
7162 4 * (friendp != 0));
7163 if (decl == error_mark_node)
7164 return NULL_TREE;
7166 if (attrlist)
7168 cplus_decl_attributes (&decl, *attrlist, 0);
7169 *attrlist = NULL_TREE;
7172 /* Check main's type after attributes have been applied. */
7173 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
7175 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
7176 integer_type_node))
7178 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
7179 tree newtype;
7180 error ("%<::main%> must return %<int%>");
7181 newtype = build_function_type (integer_type_node, oldtypeargs);
7182 TREE_TYPE (decl) = newtype;
7184 if (warn_main)
7185 check_main_parameter_types (decl);
7188 if (ctype != NULL_TREE
7189 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
7190 && check)
7192 tree old_decl = check_classfn (ctype, decl,
7193 (processing_template_decl
7194 > template_class_depth (ctype))
7195 ? current_template_parms
7196 : NULL_TREE);
7198 if (old_decl == error_mark_node)
7199 return NULL_TREE;
7201 if (old_decl)
7203 tree ok;
7204 tree pushed_scope;
7206 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
7207 /* Because grokfndecl is always supposed to return a
7208 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
7209 here. We depend on our callers to figure out that its
7210 really a template that's being returned. */
7211 old_decl = DECL_TEMPLATE_RESULT (old_decl);
7213 if (DECL_STATIC_FUNCTION_P (old_decl)
7214 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7215 /* Remove the `this' parm added by grokclassfn.
7216 XXX Isn't this done in start_function, too? */
7217 revert_static_member_fn (decl);
7218 if (DECL_ARTIFICIAL (old_decl))
7220 error ("definition of implicitly-declared %qD", old_decl);
7221 return NULL_TREE;
7224 /* Since we've smashed OLD_DECL to its
7225 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
7226 if (TREE_CODE (decl) == TEMPLATE_DECL)
7227 decl = DECL_TEMPLATE_RESULT (decl);
7229 /* Attempt to merge the declarations. This can fail, in
7230 the case of some invalid specialization declarations. */
7231 pushed_scope = push_scope (ctype);
7232 ok = duplicate_decls (decl, old_decl, friendp);
7233 if (pushed_scope)
7234 pop_scope (pushed_scope);
7235 if (!ok)
7237 error ("no %q#D member function declared in class %qT",
7238 decl, ctype);
7239 return NULL_TREE;
7241 return old_decl;
7245 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
7246 return NULL_TREE;
7248 if (ctype == NULL_TREE || check)
7249 return decl;
7251 if (virtualp)
7252 DECL_VIRTUAL_P (decl) = 1;
7254 return decl;
7257 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
7258 the linkage that DECL will receive in the object file. */
7260 static void
7261 set_linkage_for_static_data_member (tree decl)
7263 /* A static data member always has static storage duration and
7264 external linkage. Note that static data members are forbidden in
7265 local classes -- the only situation in which a class has
7266 non-external linkage. */
7267 TREE_PUBLIC (decl) = 1;
7268 TREE_STATIC (decl) = 1;
7269 /* For non-template classes, static data members are always put
7270 out in exactly those files where they are defined, just as
7271 with ordinary namespace-scope variables. */
7272 if (!processing_template_decl)
7273 DECL_INTERFACE_KNOWN (decl) = 1;
7276 /* Create a VAR_DECL named NAME with the indicated TYPE.
7278 If SCOPE is non-NULL, it is the class type or namespace containing
7279 the variable. If SCOPE is NULL, the variable should is created in
7280 the innermost enclosings scope. */
7282 static tree
7283 grokvardecl (tree type,
7284 tree name,
7285 const cp_decl_specifier_seq *declspecs,
7286 int initialized,
7287 int constp,
7288 tree scope)
7290 tree decl;
7291 tree explicit_scope;
7293 gcc_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE);
7295 /* Compute the scope in which to place the variable, but remember
7296 whether or not that scope was explicitly specified by the user. */
7297 explicit_scope = scope;
7298 if (!scope)
7300 /* An explicit "extern" specifier indicates a namespace-scope
7301 variable. */
7302 if (declspecs->storage_class == sc_extern)
7303 scope = current_decl_namespace ();
7304 else if (!at_function_scope_p ())
7305 scope = current_scope ();
7308 if (scope
7309 && (/* If the variable is a namespace-scope variable declared in a
7310 template, we need DECL_LANG_SPECIFIC. */
7311 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
7312 /* Similarly for namespace-scope variables with language linkage
7313 other than C++. */
7314 || (TREE_CODE (scope) == NAMESPACE_DECL
7315 && current_lang_name != lang_name_cplusplus)
7316 /* Similarly for static data members. */
7317 || TYPE_P (scope)))
7318 decl = build_lang_decl (VAR_DECL, name, type);
7319 else
7320 decl = build_decl (input_location, VAR_DECL, name, type);
7322 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
7323 set_decl_namespace (decl, explicit_scope, 0);
7324 else
7325 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
7327 if (declspecs->storage_class == sc_extern)
7329 DECL_THIS_EXTERN (decl) = 1;
7330 DECL_EXTERNAL (decl) = !initialized;
7333 if (DECL_CLASS_SCOPE_P (decl))
7335 set_linkage_for_static_data_member (decl);
7336 /* This function is only called with out-of-class definitions. */
7337 DECL_EXTERNAL (decl) = 0;
7338 check_class_member_definition_namespace (decl);
7340 /* At top level, either `static' or no s.c. makes a definition
7341 (perhaps tentative), and absence of `static' makes it public. */
7342 else if (toplevel_bindings_p ())
7344 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
7345 && (DECL_THIS_EXTERN (decl) || ! constp));
7346 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
7348 /* Not at top level, only `static' makes a static definition. */
7349 else
7351 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
7352 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
7355 if (declspecs->specs[(int)ds_thread])
7356 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
7358 /* If the type of the decl has no linkage, make sure that we'll
7359 notice that in mark_used. */
7360 if (cxx_dialect > cxx98
7361 && decl_linkage (decl) != lk_none
7362 && DECL_LANG_SPECIFIC (decl) == NULL
7363 && !DECL_EXTERN_C_P (decl)
7364 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
7365 retrofit_lang_decl (decl);
7367 if (TREE_PUBLIC (decl))
7369 /* [basic.link]: A name with no linkage (notably, the name of a class
7370 or enumeration declared in a local scope) shall not be used to
7371 declare an entity with linkage.
7373 DR 757 relaxes this restriction for C++0x. */
7374 tree t = (cxx_dialect > cxx98 ? NULL_TREE
7375 : no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false));
7376 if (t)
7378 if (TYPE_ANONYMOUS_P (t))
7380 if (DECL_EXTERN_C_P (decl))
7381 /* Allow this; it's pretty common in C. */
7383 else
7385 /* DRs 132, 319 and 389 seem to indicate types with
7386 no linkage can only be used to declare extern "C"
7387 entities. Since it's not always an error in the
7388 ISO C++ 90 Standard, we only issue a warning. */
7389 warning (0, "anonymous type with no linkage used to declare "
7390 "variable %q#D with linkage", decl);
7391 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
7392 warning (0, "%q+#D does not refer to the unqualified "
7393 "type, so it is not used for linkage",
7394 TYPE_NAME (t));
7397 else
7398 warning (0, "type %qT with no linkage used to declare variable "
7399 "%q#D with linkage", t, decl);
7402 else
7403 DECL_INTERFACE_KNOWN (decl) = 1;
7405 return decl;
7408 /* Create and return a canonical pointer to member function type, for
7409 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
7411 tree
7412 build_ptrmemfunc_type (tree type)
7414 tree field, fields;
7415 tree t;
7416 tree unqualified_variant = NULL_TREE;
7418 if (type == error_mark_node)
7419 return type;
7421 /* If a canonical type already exists for this type, use it. We use
7422 this method instead of type_hash_canon, because it only does a
7423 simple equality check on the list of field members. */
7425 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
7426 return t;
7428 /* Make sure that we always have the unqualified pointer-to-member
7429 type first. */
7430 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
7431 unqualified_variant
7432 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
7434 t = make_class_type (RECORD_TYPE);
7435 xref_basetypes (t, NULL_TREE);
7437 /* Let the front end know this is a pointer to member function... */
7438 TYPE_PTRMEMFUNC_FLAG (t) = 1;
7439 /* ... and not really a class type. */
7440 SET_CLASS_TYPE_P (t, 0);
7442 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
7443 fields = field;
7445 field = build_decl (input_location, FIELD_DECL, delta_identifier,
7446 delta_type_node);
7447 DECL_CHAIN (field) = fields;
7448 fields = field;
7450 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
7452 /* Zap out the name so that the back end will give us the debugging
7453 information for this anonymous RECORD_TYPE. */
7454 TYPE_NAME (t) = NULL_TREE;
7456 /* If this is not the unqualified form of this pointer-to-member
7457 type, set the TYPE_MAIN_VARIANT for this type to be the
7458 unqualified type. Since they are actually RECORD_TYPEs that are
7459 not variants of each other, we must do this manually.
7460 As we just built a new type there is no need to do yet another copy. */
7461 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
7463 int type_quals = cp_type_quals (type);
7464 TYPE_READONLY (t) = (type_quals & TYPE_QUAL_CONST) != 0;
7465 TYPE_VOLATILE (t) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
7466 TYPE_RESTRICT (t) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
7467 TYPE_MAIN_VARIANT (t) = unqualified_variant;
7468 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
7469 TYPE_NEXT_VARIANT (unqualified_variant) = t;
7470 TREE_TYPE (TYPE_BINFO (t)) = t;
7473 /* Cache this pointer-to-member type so that we can find it again
7474 later. */
7475 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
7477 if (TYPE_STRUCTURAL_EQUALITY_P (type))
7478 SET_TYPE_STRUCTURAL_EQUALITY (t);
7479 else if (TYPE_CANONICAL (type) != type)
7480 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
7482 return t;
7485 /* Create and return a pointer to data member type. */
7487 tree
7488 build_ptrmem_type (tree class_type, tree member_type)
7490 if (TREE_CODE (member_type) == METHOD_TYPE)
7492 cp_cv_quals quals = type_memfn_quals (member_type);
7493 member_type = build_memfn_type (member_type, class_type, quals);
7494 return build_ptrmemfunc_type (build_pointer_type (member_type));
7496 else
7498 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
7499 return build_offset_type (class_type, member_type);
7503 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
7504 Check to see that the definition is valid. Issue appropriate error
7505 messages. Return 1 if the definition is particularly bad, or 0
7506 otherwise. */
7509 check_static_variable_definition (tree decl, tree type)
7511 /* If DECL is declared constexpr, we'll do the appropriate checks
7512 in check_initializer. */
7513 if (DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
7514 return 0;
7515 else if (cxx_dialect >= cxx0x && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7517 if (literal_type_p (type))
7518 error ("%<constexpr%> needed for in-class initialization of static "
7519 "data member %q#D of non-integral type", decl);
7520 else
7521 error ("in-class initialization of static data member %q#D of "
7522 "non-literal type", decl);
7523 return 1;
7526 /* Motion 10 at San Diego: If a static const integral data member is
7527 initialized with an integral constant expression, the initializer
7528 may appear either in the declaration (within the class), or in
7529 the definition, but not both. If it appears in the class, the
7530 member is a member constant. The file-scope definition is always
7531 required. */
7532 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
7534 error ("invalid in-class initialization of static data member "
7535 "of non-integral type %qT",
7536 type);
7537 return 1;
7539 else if (!CP_TYPE_CONST_P (type))
7540 error ("ISO C++ forbids in-class initialization of non-const "
7541 "static member %qD",
7542 decl);
7543 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7544 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids initialization of member constant "
7545 "%qD of non-integral type %qT", decl, type);
7547 return 0;
7550 /* Given the SIZE (i.e., number of elements) in an array, compute an
7551 appropriate index type for the array. If non-NULL, NAME is the
7552 name of the thing being declared. */
7554 tree
7555 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
7557 tree type;
7558 tree itype;
7559 tree osize = size;
7560 tree abi_1_itype = NULL_TREE;
7562 if (error_operand_p (size))
7563 return error_mark_node;
7565 type = TREE_TYPE (size);
7566 /* type_dependent_expression_p? */
7567 if (!dependent_type_p (type))
7569 mark_rvalue_use (size);
7571 if (cxx_dialect < cxx0x && TREE_CODE (size) == NOP_EXPR
7572 && TREE_SIDE_EFFECTS (size))
7573 /* In C++98, we mark a non-constant array bound with a magic
7574 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
7575 else
7577 size = fold_non_dependent_expr (size);
7579 if (CLASS_TYPE_P (type)
7580 && CLASSTYPE_LITERAL_P (type))
7582 size = build_expr_type_conversion (WANT_INT, size, true);
7583 if (size == error_mark_node)
7584 return error_mark_node;
7585 type = TREE_TYPE (size);
7586 /* We didn't support this case in GCC 3.2, so don't bother
7587 trying to model it now in ABI v1. */
7588 abi_1_itype = error_mark_node;
7591 size = maybe_constant_value (size);
7592 if (!TREE_CONSTANT (size))
7593 size = osize;
7596 if (error_operand_p (size))
7597 return error_mark_node;
7599 /* The array bound must be an integer type. */
7600 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
7602 if (!(complain & tf_error))
7603 return error_mark_node;
7604 if (name)
7605 error ("size of array %qD has non-integral type %qT", name, type);
7606 else
7607 error ("size of array has non-integral type %qT", type);
7608 size = integer_one_node;
7609 type = TREE_TYPE (size);
7613 /* A type is dependent if it is...an array type constructed from any
7614 dependent type or whose size is specified by a constant expression
7615 that is value-dependent. */
7616 /* We can only call value_dependent_expression_p on integral constant
7617 expressions; treat non-constant expressions as dependent, too. */
7618 if (processing_template_decl
7619 && (dependent_type_p (type)
7620 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
7622 /* We cannot do any checking for a SIZE that isn't known to be
7623 constant. Just build the index type and mark that it requires
7624 structural equality checks. */
7625 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
7626 size, integer_one_node));
7627 TYPE_DEPENDENT_P (itype) = 1;
7628 TYPE_DEPENDENT_P_VALID (itype) = 1;
7629 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7630 return itype;
7633 if (!abi_version_at_least (2) && processing_template_decl
7634 && abi_1_itype == NULL_TREE)
7635 /* For abi-1, we handled all instances in templates the same way,
7636 even when they were non-dependent. This affects the manglings
7637 produced. So, we do the normal checking for non-dependent
7638 sizes, but at the end we'll return the same type that abi-1
7639 would have, but with TYPE_CANONICAL set to the "right"
7640 value that the current ABI would provide. */
7641 abi_1_itype = build_index_type (build_min (MINUS_EXPR, sizetype,
7642 osize, integer_one_node));
7644 /* Normally, the array-bound will be a constant. */
7645 if (TREE_CODE (size) == INTEGER_CST)
7647 /* Check to see if the array bound overflowed. Make that an
7648 error, no matter how generous we're being. */
7649 constant_expression_error (size);
7651 /* An array must have a positive number of elements. */
7652 if (INT_CST_LT (size, integer_zero_node))
7654 if (!(complain & tf_error))
7655 return error_mark_node;
7656 if (name)
7657 error ("size of array %qD is negative", name);
7658 else
7659 error ("size of array is negative");
7660 size = integer_one_node;
7662 /* As an extension we allow zero-sized arrays. */
7663 else if (integer_zerop (size))
7665 if (!(complain & tf_error))
7666 /* We must fail if performing argument deduction (as
7667 indicated by the state of complain), so that
7668 another substitution can be found. */
7669 return error_mark_node;
7670 else if (in_system_header)
7671 /* Allow them in system headers because glibc uses them. */;
7672 else if (name)
7673 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids zero-size array %qD", name);
7674 else
7675 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids zero-size array");
7678 else if (TREE_CONSTANT (size)
7679 /* We don't allow VLAs at non-function scopes, or during
7680 tentative template substitution. */
7681 || !at_function_scope_p () || !(complain & tf_error))
7683 if (!(complain & tf_error))
7684 return error_mark_node;
7685 /* `(int) &fn' is not a valid array bound. */
7686 if (name)
7687 error ("size of array %qD is not an integral constant-expression",
7688 name);
7689 else
7690 error ("size of array is not an integral constant-expression");
7691 size = integer_one_node;
7693 else if (pedantic && warn_vla != 0)
7695 if (name)
7696 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
7697 else
7698 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
7700 else if (warn_vla > 0)
7702 if (name)
7703 warning (OPT_Wvla,
7704 "variable length array %qD is used", name);
7705 else
7706 warning (OPT_Wvla,
7707 "variable length array is used");
7710 if (processing_template_decl && !TREE_CONSTANT (size))
7711 /* A variable sized array. */
7712 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
7713 else
7715 HOST_WIDE_INT saved_processing_template_decl;
7717 /* Compute the index of the largest element in the array. It is
7718 one less than the number of elements in the array. We save
7719 and restore PROCESSING_TEMPLATE_DECL so that computations in
7720 cp_build_binary_op will be appropriately folded. */
7721 saved_processing_template_decl = processing_template_decl;
7722 processing_template_decl = 0;
7723 itype = cp_build_binary_op (input_location,
7724 MINUS_EXPR,
7725 cp_convert (ssizetype, size),
7726 cp_convert (ssizetype, integer_one_node),
7727 tf_warning_or_error);
7728 itype = fold (itype);
7729 processing_template_decl = saved_processing_template_decl;
7731 if (!TREE_CONSTANT (itype))
7732 /* A variable sized array. */
7733 itype = variable_size (itype);
7734 /* Make sure that there was no overflow when creating to a signed
7735 index type. (For example, on a 32-bit machine, an array with
7736 size 2^32 - 1 is too big.) */
7737 else if (TREE_CODE (itype) == INTEGER_CST
7738 && TREE_OVERFLOW (itype))
7740 if (!(complain & tf_error))
7741 return error_mark_node;
7742 error ("overflow in array dimension");
7743 TREE_OVERFLOW (itype) = 0;
7747 /* Create and return the appropriate index type. */
7748 if (abi_1_itype && abi_1_itype != error_mark_node)
7750 tree t = build_index_type (itype);
7751 TYPE_CANONICAL (abi_1_itype) = TYPE_CANONICAL (t);
7752 itype = abi_1_itype;
7754 else
7755 itype = build_index_type (itype);
7757 /* If the index type were dependent, we would have returned early, so
7758 remember that it isn't. */
7759 TYPE_DEPENDENT_P (itype) = 0;
7760 TYPE_DEPENDENT_P_VALID (itype) = 1;
7761 return itype;
7764 /* Returns the scope (if any) in which the entity declared by
7765 DECLARATOR will be located. If the entity was declared with an
7766 unqualified name, NULL_TREE is returned. */
7768 tree
7769 get_scope_of_declarator (const cp_declarator *declarator)
7771 while (declarator && declarator->kind != cdk_id)
7772 declarator = declarator->declarator;
7774 /* If the declarator-id is a SCOPE_REF, the scope in which the
7775 declaration occurs is the first operand. */
7776 if (declarator
7777 && declarator->u.id.qualifying_scope)
7778 return declarator->u.id.qualifying_scope;
7780 /* Otherwise, the declarator is not a qualified name; the entity will
7781 be declared in the current scope. */
7782 return NULL_TREE;
7785 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
7786 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
7787 with this type. */
7789 static tree
7790 create_array_type_for_decl (tree name, tree type, tree size)
7792 tree itype = NULL_TREE;
7794 /* If things have already gone awry, bail now. */
7795 if (type == error_mark_node || size == error_mark_node)
7796 return error_mark_node;
7798 /* If there are some types which cannot be array elements,
7799 issue an error-message and return. */
7800 switch (TREE_CODE (type))
7802 case VOID_TYPE:
7803 if (name)
7804 error ("declaration of %qD as array of void", name);
7805 else
7806 error ("creating array of void");
7807 return error_mark_node;
7809 case FUNCTION_TYPE:
7810 if (name)
7811 error ("declaration of %qD as array of functions", name);
7812 else
7813 error ("creating array of functions");
7814 return error_mark_node;
7816 case REFERENCE_TYPE:
7817 if (name)
7818 error ("declaration of %qD as array of references", name);
7819 else
7820 error ("creating array of references");
7821 return error_mark_node;
7823 case METHOD_TYPE:
7824 if (name)
7825 error ("declaration of %qD as array of function members", name);
7826 else
7827 error ("creating array of function members");
7828 return error_mark_node;
7830 default:
7831 break;
7834 /* [dcl.array]
7836 The constant expressions that specify the bounds of the arrays
7837 can be omitted only for the first member of the sequence. */
7838 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
7840 if (name)
7841 error ("declaration of %qD as multidimensional array must "
7842 "have bounds for all dimensions except the first",
7843 name);
7844 else
7845 error ("multidimensional array must have bounds for all "
7846 "dimensions except the first");
7848 return error_mark_node;
7851 /* Figure out the index type for the array. */
7852 if (size)
7853 itype = compute_array_index_type (name, size, tf_warning_or_error);
7855 /* [dcl.array]
7856 T is called the array element type; this type shall not be [...] an
7857 abstract class type. */
7858 abstract_virtuals_error (name, type);
7860 return build_cplus_array_type (type, itype);
7863 /* Check that it's OK to declare a function with the indicated TYPE.
7864 SFK indicates the kind of special function (if any) that this
7865 function is. OPTYPE is the type given in a conversion operator
7866 declaration, or the class type for a constructor/destructor.
7867 Returns the actual return type of the function; that
7868 may be different than TYPE if an error occurs, or for certain
7869 special functions. */
7871 static tree
7872 check_special_function_return_type (special_function_kind sfk,
7873 tree type,
7874 tree optype)
7876 switch (sfk)
7878 case sfk_constructor:
7879 if (type)
7880 error ("return type specification for constructor invalid");
7882 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
7883 type = build_pointer_type (optype);
7884 else
7885 type = void_type_node;
7886 break;
7888 case sfk_destructor:
7889 if (type)
7890 error ("return type specification for destructor invalid");
7891 /* We can't use the proper return type here because we run into
7892 problems with ambiguous bases and covariant returns.
7893 Java classes are left unchanged because (void *) isn't a valid
7894 Java type, and we don't want to change the Java ABI. */
7895 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
7896 type = build_pointer_type (void_type_node);
7897 else
7898 type = void_type_node;
7899 break;
7901 case sfk_conversion:
7902 if (type)
7903 error ("return type specified for %<operator %T%>", optype);
7904 type = optype;
7905 break;
7907 default:
7908 gcc_unreachable ();
7911 return type;
7914 /* A variable or data member (whose unqualified name is IDENTIFIER)
7915 has been declared with the indicated TYPE. If the TYPE is not
7916 acceptable, issue an error message and return a type to use for
7917 error-recovery purposes. */
7919 tree
7920 check_var_type (tree identifier, tree type)
7922 if (VOID_TYPE_P (type))
7924 if (!identifier)
7925 error ("unnamed variable or field declared void");
7926 else if (TREE_CODE (identifier) == IDENTIFIER_NODE)
7928 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
7929 error ("variable or field %qE declared void", identifier);
7931 else
7932 error ("variable or field declared void");
7933 type = error_mark_node;
7936 return type;
7939 /* Given declspecs and a declarator (abstract or otherwise), determine
7940 the name and type of the object declared and construct a DECL node
7941 for it.
7943 DECLSPECS points to the representation of declaration-specifier
7944 sequence that precedes declarator.
7946 DECL_CONTEXT says which syntactic context this declaration is in:
7947 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
7948 FUNCDEF for a function definition. Like NORMAL but a few different
7949 error messages in each case. Return value may be zero meaning
7950 this definition is too screwy to try to parse.
7951 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
7952 handle member functions (which have FIELD context).
7953 Return value may be zero meaning this definition is too screwy to
7954 try to parse.
7955 PARM for a parameter declaration (either within a function prototype
7956 or before a function body). Make a PARM_DECL, or return void_type_node.
7957 TPARM for a template parameter declaration.
7958 CATCHPARM for a parameter declaration before a catch clause.
7959 TYPENAME if for a typename (in a cast or sizeof).
7960 Don't make a DECL node; just return the ..._TYPE node.
7961 FIELD for a struct or union field; make a FIELD_DECL.
7962 BITFIELD for a field with specified width.
7964 INITIALIZED is as for start_decl.
7966 ATTRLIST is a pointer to the list of attributes, which may be NULL
7967 if there are none; *ATTRLIST may be modified if attributes from inside
7968 the declarator should be applied to the declaration.
7970 When this function is called, scoping variables (such as
7971 CURRENT_CLASS_TYPE) should reflect the scope in which the
7972 declaration occurs, not the scope in which the new declaration will
7973 be placed. For example, on:
7975 void S::f() { ... }
7977 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
7978 should not be `S'.
7980 Returns a DECL (if a declarator is present), a TYPE (if there is no
7981 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
7982 error occurs. */
7984 tree
7985 grokdeclarator (const cp_declarator *declarator,
7986 const cp_decl_specifier_seq *declspecs,
7987 enum decl_context decl_context,
7988 int initialized,
7989 tree* attrlist)
7991 tree type = NULL_TREE;
7992 int longlong = 0;
7993 int explicit_int128 = 0;
7994 int virtualp, explicitp, friendp, inlinep, staticp;
7995 int explicit_int = 0;
7996 int explicit_char = 0;
7997 int defaulted_int = 0;
7998 tree dependent_name = NULL_TREE;
8000 tree typedef_decl = NULL_TREE;
8001 const char *name = NULL;
8002 tree typedef_type = NULL_TREE;
8003 /* True if this declarator is a function definition. */
8004 bool funcdef_flag = false;
8005 cp_declarator_kind innermost_code = cdk_error;
8006 int bitfield = 0;
8007 #if 0
8008 /* See the code below that used this. */
8009 tree decl_attr = NULL_TREE;
8010 #endif
8012 /* Keep track of what sort of function is being processed
8013 so that we can warn about default return values, or explicit
8014 return values which do not match prescribed defaults. */
8015 special_function_kind sfk = sfk_none;
8017 tree dname = NULL_TREE;
8018 tree ctor_return_type = NULL_TREE;
8019 enum overload_flags flags = NO_SPECIAL;
8020 /* cv-qualifiers that apply to the declarator, for a declaration of
8021 a member function. */
8022 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
8023 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
8024 int type_quals;
8025 tree raises = NULL_TREE;
8026 int template_count = 0;
8027 tree returned_attrs = NULL_TREE;
8028 tree parms = NULL_TREE;
8029 const cp_declarator *id_declarator;
8030 /* The unqualified name of the declarator; either an
8031 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
8032 tree unqualified_id;
8033 /* The class type, if any, in which this entity is located,
8034 or NULL_TREE if none. Note that this value may be different from
8035 the current class type; for example if an attempt is made to declare
8036 "A::f" inside "B", this value will be "A". */
8037 tree ctype = current_class_type;
8038 /* The NAMESPACE_DECL for the namespace in which this entity is
8039 located. If an unqualified name is used to declare the entity,
8040 this value will be NULL_TREE, even if the entity is located at
8041 namespace scope. */
8042 tree in_namespace = NULL_TREE;
8043 cp_storage_class storage_class;
8044 bool unsigned_p, signed_p, short_p, long_p, thread_p;
8045 bool type_was_error_mark_node = false;
8046 bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
8047 bool template_type_arg = false;
8048 bool template_parm_flag = false;
8049 bool constexpr_p = declspecs->specs[(int) ds_constexpr];
8050 const char *errmsg;
8052 signed_p = declspecs->specs[(int)ds_signed];
8053 unsigned_p = declspecs->specs[(int)ds_unsigned];
8054 short_p = declspecs->specs[(int)ds_short];
8055 long_p = declspecs->specs[(int)ds_long];
8056 longlong = declspecs->specs[(int)ds_long] >= 2;
8057 explicit_int128 = declspecs->explicit_int128_p;
8058 thread_p = declspecs->specs[(int)ds_thread];
8060 if (decl_context == FUNCDEF)
8061 funcdef_flag = true, decl_context = NORMAL;
8062 else if (decl_context == MEMFUNCDEF)
8063 funcdef_flag = true, decl_context = FIELD;
8064 else if (decl_context == BITFIELD)
8065 bitfield = 1, decl_context = FIELD;
8066 else if (decl_context == TEMPLATE_TYPE_ARG)
8067 template_type_arg = true, decl_context = TYPENAME;
8068 else if (decl_context == TPARM)
8069 template_parm_flag = true, decl_context = PARM;
8071 if (initialized > 1)
8072 funcdef_flag = true;
8074 /* Look inside a declarator for the name being declared
8075 and get it as a string, for an error message. */
8076 for (id_declarator = declarator;
8077 id_declarator;
8078 id_declarator = id_declarator->declarator)
8080 if (id_declarator->kind != cdk_id)
8081 innermost_code = id_declarator->kind;
8083 switch (id_declarator->kind)
8085 case cdk_function:
8086 if (id_declarator->declarator
8087 && id_declarator->declarator->kind == cdk_id)
8089 sfk = id_declarator->declarator->u.id.sfk;
8090 if (sfk == sfk_destructor)
8091 flags = DTOR_FLAG;
8093 break;
8095 case cdk_id:
8097 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
8098 tree decl = id_declarator->u.id.unqualified_name;
8099 if (!decl)
8100 break;
8101 if (qualifying_scope)
8103 if (at_function_scope_p ())
8105 /* [dcl.meaning]
8107 A declarator-id shall not be qualified except
8108 for ...
8110 None of the cases are permitted in block
8111 scope. */
8112 if (qualifying_scope == global_namespace)
8113 error ("invalid use of qualified-name %<::%D%>",
8114 decl);
8115 else if (TYPE_P (qualifying_scope))
8116 error ("invalid use of qualified-name %<%T::%D%>",
8117 qualifying_scope, decl);
8118 else
8119 error ("invalid use of qualified-name %<%D::%D%>",
8120 qualifying_scope, decl);
8121 return error_mark_node;
8123 else if (TYPE_P (qualifying_scope))
8125 ctype = qualifying_scope;
8126 if (innermost_code != cdk_function
8127 && current_class_type
8128 && !UNIQUELY_DERIVED_FROM_P (ctype,
8129 current_class_type))
8131 error ("type %qT is not derived from type %qT",
8132 ctype, current_class_type);
8133 return error_mark_node;
8136 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
8137 in_namespace = qualifying_scope;
8139 switch (TREE_CODE (decl))
8141 case BIT_NOT_EXPR:
8143 tree type;
8145 if (innermost_code != cdk_function)
8147 error ("declaration of %qD as non-function", decl);
8148 return error_mark_node;
8150 else if (!qualifying_scope
8151 && !(current_class_type && at_class_scope_p ()))
8153 error ("declaration of %qD as non-member", decl);
8154 return error_mark_node;
8157 type = TREE_OPERAND (decl, 0);
8158 if (TYPE_P (type))
8159 type = constructor_name (type);
8160 name = identifier_to_locale (IDENTIFIER_POINTER (type));
8161 dname = decl;
8163 break;
8165 case TEMPLATE_ID_EXPR:
8167 tree fns = TREE_OPERAND (decl, 0);
8169 dname = fns;
8170 if (TREE_CODE (dname) != IDENTIFIER_NODE)
8172 gcc_assert (is_overloaded_fn (dname));
8173 dname = DECL_NAME (get_first_fn (dname));
8176 /* Fall through. */
8178 case IDENTIFIER_NODE:
8179 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8180 dname = decl;
8182 if (C_IS_RESERVED_WORD (dname))
8184 error ("declarator-id missing; using reserved word %qD",
8185 dname);
8186 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
8188 else if (!IDENTIFIER_TYPENAME_P (dname))
8189 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
8190 else
8192 gcc_assert (flags == NO_SPECIAL);
8193 flags = TYPENAME_FLAG;
8194 ctor_return_type = TREE_TYPE (dname);
8195 sfk = sfk_conversion;
8196 if (is_typename_at_global_scope (dname))
8197 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
8198 else
8199 name = "<invalid operator>";
8201 break;
8203 default:
8204 gcc_unreachable ();
8206 break;
8209 case cdk_array:
8210 case cdk_pointer:
8211 case cdk_reference:
8212 case cdk_ptrmem:
8213 break;
8215 case cdk_error:
8216 return error_mark_node;
8218 default:
8219 gcc_unreachable ();
8221 if (id_declarator->kind == cdk_id)
8222 break;
8225 /* [dcl.fct.edf]
8227 The declarator in a function-definition shall have the form
8228 D1 ( parameter-declaration-clause) ... */
8229 if (funcdef_flag && innermost_code != cdk_function)
8231 error ("function definition does not declare parameters");
8232 return error_mark_node;
8235 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
8236 && innermost_code != cdk_function
8237 && ! (ctype && !declspecs->any_specifiers_p))
8239 error ("declaration of %qD as non-function", dname);
8240 return error_mark_node;
8243 /* Anything declared one level down from the top level
8244 must be one of the parameters of a function
8245 (because the body is at least two levels down). */
8247 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
8248 by not allowing C++ class definitions to specify their parameters
8249 with xdecls (must be spec.d in the parmlist).
8251 Since we now wait to push a class scope until we are sure that
8252 we are in a legitimate method context, we must set oldcname
8253 explicitly (since current_class_name is not yet alive).
8255 We also want to avoid calling this a PARM if it is in a namespace. */
8257 if (decl_context == NORMAL && !toplevel_bindings_p ())
8259 struct cp_binding_level *b = current_binding_level;
8260 current_binding_level = b->level_chain;
8261 if (current_binding_level != 0 && toplevel_bindings_p ())
8262 decl_context = PARM;
8263 current_binding_level = b;
8266 if (name == NULL)
8267 name = decl_context == PARM ? "parameter" : "type name";
8269 if (constexpr_p && declspecs->specs[(int)ds_typedef])
8271 error ("%<constexpr%> cannot appear in a typedef declaration");
8272 return error_mark_node;
8275 /* If there were multiple types specified in the decl-specifier-seq,
8276 issue an error message. */
8277 if (declspecs->multiple_types_p)
8279 error ("two or more data types in declaration of %qs", name);
8280 return error_mark_node;
8283 if (declspecs->conflicting_specifiers_p)
8285 error ("conflicting specifiers in declaration of %qs", name);
8286 return error_mark_node;
8289 /* Extract the basic type from the decl-specifier-seq. */
8290 type = declspecs->type;
8291 if (type == error_mark_node)
8293 type = NULL_TREE;
8294 type_was_error_mark_node = true;
8296 /* If the entire declaration is itself tagged as deprecated then
8297 suppress reports of deprecated items. */
8298 if (type && TREE_DEPRECATED (type)
8299 && deprecated_state != DEPRECATED_SUPPRESS)
8300 warn_deprecated_use (type, NULL_TREE);
8301 if (type && TREE_CODE (type) == TYPE_DECL)
8303 typedef_decl = type;
8304 type = TREE_TYPE (typedef_decl);
8305 if (TREE_DEPRECATED (type)
8306 && DECL_ARTIFICIAL (typedef_decl)
8307 && deprecated_state != DEPRECATED_SUPPRESS)
8308 warn_deprecated_use (type, NULL_TREE);
8310 /* No type at all: default to `int', and set DEFAULTED_INT
8311 because it was not a user-defined typedef. */
8312 if (type == NULL_TREE && (signed_p || unsigned_p || long_p || short_p))
8314 /* These imply 'int'. */
8315 type = integer_type_node;
8316 defaulted_int = 1;
8318 /* Gather flags. */
8319 explicit_int = declspecs->explicit_int_p;
8320 explicit_char = declspecs->explicit_char_p;
8322 #if 0
8323 /* See the code below that used this. */
8324 if (typedef_decl)
8325 decl_attr = DECL_ATTRIBUTES (typedef_decl);
8326 #endif
8327 typedef_type = type;
8330 if (sfk != sfk_conversion)
8331 ctor_return_type = ctype;
8333 if (sfk != sfk_none)
8334 type = check_special_function_return_type (sfk, type,
8335 ctor_return_type);
8336 else if (type == NULL_TREE)
8338 int is_main;
8340 explicit_int = -1;
8342 /* We handle `main' specially here, because 'main () { }' is so
8343 common. With no options, it is allowed. With -Wreturn-type,
8344 it is a warning. It is only an error with -pedantic-errors. */
8345 is_main = (funcdef_flag
8346 && dname && TREE_CODE (dname) == IDENTIFIER_NODE
8347 && MAIN_NAME_P (dname)
8348 && ctype == NULL_TREE
8349 && in_namespace == NULL_TREE
8350 && current_namespace == global_namespace);
8352 if (type_was_error_mark_node)
8353 /* We've already issued an error, don't complain more. */;
8354 else if (in_system_header || flag_ms_extensions)
8355 /* Allow it, sigh. */;
8356 else if (! is_main)
8357 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
8358 else if (pedantic)
8359 pedwarn (input_location, OPT_pedantic,
8360 "ISO C++ forbids declaration of %qs with no type", name);
8361 else
8362 warning (OPT_Wreturn_type,
8363 "ISO C++ forbids declaration of %qs with no type", name);
8365 type = integer_type_node;
8368 ctype = NULL_TREE;
8370 /* Now process the modifiers that were specified
8371 and check for invalid combinations. */
8373 /* Long double is a special combination. */
8374 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
8376 long_p = false;
8377 type = cp_build_qualified_type (long_double_type_node,
8378 cp_type_quals (type));
8381 /* Check all other uses of type modifiers. */
8383 if (unsigned_p || signed_p || long_p || short_p)
8385 int ok = 0;
8387 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
8388 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
8389 else if (signed_p && unsigned_p)
8390 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
8391 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
8392 error ("%<long long%> invalid for %qs", name);
8393 else if (explicit_int128 && TREE_CODE (type) != INTEGER_TYPE)
8394 error ("%<__int128%> invalid for %qs", name);
8395 else if (long_p && TREE_CODE (type) == REAL_TYPE)
8396 error ("%<long%> invalid for %qs", name);
8397 else if (short_p && TREE_CODE (type) == REAL_TYPE)
8398 error ("%<short%> invalid for %qs", name);
8399 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
8400 error ("%<long%> or %<short%> invalid for %qs", name);
8401 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_int128)
8402 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
8403 else if ((long_p || short_p) && explicit_char)
8404 error ("%<long%> or %<short%> specified with char for %qs", name);
8405 else if (long_p && short_p)
8406 error ("%<long%> and %<short%> specified together for %qs", name);
8407 else if (type == char16_type_node || type == char32_type_node)
8409 if (signed_p || unsigned_p)
8410 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
8411 else if (short_p || long_p)
8412 error ("%<short%> or %<long%> invalid for %qs", name);
8414 else
8416 ok = 1;
8417 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_int128 && pedantic)
8419 pedwarn (input_location, OPT_pedantic,
8420 "long, short, signed or unsigned used invalidly for %qs",
8421 name);
8422 if (flag_pedantic_errors)
8423 ok = 0;
8425 if (explicit_int128)
8427 if (int128_integer_type_node == NULL_TREE)
8429 error ("%<__int128%> is not supported by this target");
8430 ok = 0;
8432 else if (pedantic)
8434 pedwarn (input_location, OPT_pedantic,
8435 "ISO C++ does not support %<__int128%> for %qs",
8436 name);
8437 if (flag_pedantic_errors)
8438 ok = 0;
8443 /* Discard the type modifiers if they are invalid. */
8444 if (! ok)
8446 unsigned_p = false;
8447 signed_p = false;
8448 long_p = false;
8449 short_p = false;
8450 longlong = 0;
8451 explicit_int128 = false;
8455 /* Decide whether an integer type is signed or not.
8456 Optionally treat bitfields as signed by default. */
8457 if (unsigned_p
8458 /* [class.bit]
8460 It is implementation-defined whether a plain (neither
8461 explicitly signed or unsigned) char, short, int, or long
8462 bit-field is signed or unsigned.
8464 Naturally, we extend this to long long as well. Note that
8465 this does not include wchar_t. */
8466 || (bitfield && !flag_signed_bitfields
8467 && !signed_p
8468 /* A typedef for plain `int' without `signed' can be
8469 controlled just like plain `int', but a typedef for
8470 `signed int' cannot be so controlled. */
8471 && !(typedef_decl
8472 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
8473 && TREE_CODE (type) == INTEGER_TYPE
8474 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
8476 if (explicit_int128)
8477 type = int128_unsigned_type_node;
8478 else if (longlong)
8479 type = long_long_unsigned_type_node;
8480 else if (long_p)
8481 type = long_unsigned_type_node;
8482 else if (short_p)
8483 type = short_unsigned_type_node;
8484 else if (type == char_type_node)
8485 type = unsigned_char_type_node;
8486 else if (typedef_decl)
8487 type = unsigned_type_for (type);
8488 else
8489 type = unsigned_type_node;
8491 else if (signed_p && type == char_type_node)
8492 type = signed_char_type_node;
8493 else if (explicit_int128)
8494 type = int128_integer_type_node;
8495 else if (longlong)
8496 type = long_long_integer_type_node;
8497 else if (long_p)
8498 type = long_integer_type_node;
8499 else if (short_p)
8500 type = short_integer_type_node;
8502 if (declspecs->specs[(int)ds_complex])
8504 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
8505 error ("complex invalid for %qs", name);
8506 /* If we just have "complex", it is equivalent to
8507 "complex double", but if any modifiers at all are specified it is
8508 the complex form of TYPE. E.g, "complex short" is
8509 "complex short int". */
8510 else if (defaulted_int && ! longlong && ! explicit_int128
8511 && ! (long_p || short_p || signed_p || unsigned_p))
8512 type = complex_double_type_node;
8513 else if (type == integer_type_node)
8514 type = complex_integer_type_node;
8515 else if (type == float_type_node)
8516 type = complex_float_type_node;
8517 else if (type == double_type_node)
8518 type = complex_double_type_node;
8519 else if (type == long_double_type_node)
8520 type = complex_long_double_type_node;
8521 else
8522 type = build_complex_type (type);
8525 type_quals = TYPE_UNQUALIFIED;
8526 if (declspecs->specs[(int)ds_const])
8527 type_quals |= TYPE_QUAL_CONST;
8528 if (declspecs->specs[(int)ds_volatile])
8529 type_quals |= TYPE_QUAL_VOLATILE;
8530 if (declspecs->specs[(int)ds_restrict])
8531 type_quals |= TYPE_QUAL_RESTRICT;
8532 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
8533 error ("qualifiers are not allowed on declaration of %<operator %T%>",
8534 ctor_return_type);
8536 type_quals |= cp_type_quals (type);
8537 type = cp_build_qualified_type_real
8538 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
8539 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
8540 /* We might have ignored or rejected some of the qualifiers. */
8541 type_quals = cp_type_quals (type);
8543 staticp = 0;
8544 inlinep = !! declspecs->specs[(int)ds_inline];
8545 virtualp = !! declspecs->specs[(int)ds_virtual];
8546 explicitp = !! declspecs->specs[(int)ds_explicit];
8548 storage_class = declspecs->storage_class;
8549 if (storage_class == sc_static)
8550 staticp = 1 + (decl_context == FIELD);
8552 if (virtualp && staticp == 2)
8554 error ("member %qD cannot be declared both virtual and static", dname);
8555 storage_class = sc_none;
8556 staticp = 0;
8558 friendp = !! declspecs->specs[(int)ds_friend];
8560 if (dependent_name && !friendp)
8562 error ("%<%T::%D%> is not a valid declarator", ctype, dependent_name);
8563 return error_mark_node;
8566 /* Issue errors about use of storage classes for parameters. */
8567 if (decl_context == PARM)
8569 if (declspecs->specs[(int)ds_typedef])
8571 error ("typedef declaration invalid in parameter declaration");
8572 return error_mark_node;
8574 else if (template_parm_flag && storage_class != sc_none)
8576 error ("storage class specified for template parameter %qs", name);
8577 return error_mark_node;
8579 else if (storage_class == sc_static
8580 || storage_class == sc_extern
8581 || thread_p)
8582 error ("storage class specifiers invalid in parameter declarations");
8584 if (type_uses_auto (type))
8586 error ("parameter declared %<auto%>");
8587 type = error_mark_node;
8590 /* Function parameters cannot be constexpr. If we saw one, moan
8591 and pretend it wasn't there. */
8592 if (constexpr_p)
8594 error ("a parameter cannot be declared %<constexpr%>");
8595 constexpr_p = 0;
8599 /* Give error if `virtual' is used outside of class declaration. */
8600 if (virtualp
8601 && (current_class_name == NULL_TREE || decl_context != FIELD))
8603 error ("%<virtual%> outside class declaration");
8604 virtualp = 0;
8607 /* Static anonymous unions are dealt with here. */
8608 if (staticp && decl_context == TYPENAME
8609 && declspecs->type
8610 && ANON_AGGR_TYPE_P (declspecs->type))
8611 decl_context = FIELD;
8613 /* Warn about storage classes that are invalid for certain
8614 kinds of declarations (parameters, typenames, etc.). */
8615 if (thread_p
8616 && ((storage_class
8617 && storage_class != sc_extern
8618 && storage_class != sc_static)
8619 || declspecs->specs[(int)ds_typedef]))
8621 error ("multiple storage classes in declaration of %qs", name);
8622 thread_p = false;
8624 if (decl_context != NORMAL
8625 && ((storage_class != sc_none
8626 && storage_class != sc_mutable)
8627 || thread_p))
8629 if ((decl_context == PARM || decl_context == CATCHPARM)
8630 && (storage_class == sc_register
8631 || storage_class == sc_auto))
8633 else if (declspecs->specs[(int)ds_typedef])
8635 else if (decl_context == FIELD
8636 /* C++ allows static class elements. */
8637 && storage_class == sc_static)
8638 /* C++ also allows inlines and signed and unsigned elements,
8639 but in those cases we don't come in here. */
8641 else
8643 if (decl_context == FIELD)
8644 error ("storage class specified for %qs", name);
8645 else
8647 if (decl_context == PARM || decl_context == CATCHPARM)
8648 error ("storage class specified for parameter %qs", name);
8649 else
8650 error ("storage class specified for typename");
8652 if (storage_class == sc_register
8653 || storage_class == sc_auto
8654 || storage_class == sc_extern
8655 || thread_p)
8656 storage_class = sc_none;
8659 else if (storage_class == sc_extern && funcdef_flag
8660 && ! toplevel_bindings_p ())
8661 error ("nested function %qs declared %<extern%>", name);
8662 else if (toplevel_bindings_p ())
8664 if (storage_class == sc_auto)
8665 error ("top-level declaration of %qs specifies %<auto%>", name);
8667 else if (thread_p
8668 && storage_class != sc_extern
8669 && storage_class != sc_static)
8671 error ("function-scope %qs implicitly auto and declared %<__thread%>",
8672 name);
8673 thread_p = false;
8676 if (storage_class && friendp)
8678 error ("storage class specifiers invalid in friend function declarations");
8679 storage_class = sc_none;
8680 staticp = 0;
8683 if (!id_declarator)
8684 unqualified_id = NULL_TREE;
8685 else
8687 unqualified_id = id_declarator->u.id.unqualified_name;
8688 switch (TREE_CODE (unqualified_id))
8690 case BIT_NOT_EXPR:
8691 unqualified_id = TREE_OPERAND (unqualified_id, 0);
8692 if (TYPE_P (unqualified_id))
8693 unqualified_id = constructor_name (unqualified_id);
8694 break;
8696 case IDENTIFIER_NODE:
8697 case TEMPLATE_ID_EXPR:
8698 break;
8700 default:
8701 gcc_unreachable ();
8705 /* Determine the type of the entity declared by recurring on the
8706 declarator. */
8707 for (; declarator; declarator = declarator->declarator)
8709 const cp_declarator *inner_declarator;
8710 tree attrs;
8712 if (type == error_mark_node)
8713 return error_mark_node;
8715 attrs = declarator->attributes;
8716 if (attrs)
8718 int attr_flags;
8720 attr_flags = 0;
8721 if (declarator == NULL || declarator->kind == cdk_id)
8722 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
8723 if (declarator->kind == cdk_function)
8724 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
8725 if (declarator->kind == cdk_array)
8726 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
8727 returned_attrs = decl_attributes (&type,
8728 chainon (returned_attrs, attrs),
8729 attr_flags);
8732 if (declarator->kind == cdk_id)
8733 break;
8735 inner_declarator = declarator->declarator;
8737 switch (declarator->kind)
8739 case cdk_array:
8740 type = create_array_type_for_decl (dname, type,
8741 declarator->u.array.bounds);
8742 break;
8744 case cdk_function:
8746 tree arg_types;
8747 int funcdecl_p;
8749 /* Declaring a function type.
8750 Make sure we have a valid type for the function to return. */
8752 if (type_quals != TYPE_UNQUALIFIED)
8754 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
8755 warning (OPT_Wignored_qualifiers,
8756 "type qualifiers ignored on function return type");
8757 /* We now know that the TYPE_QUALS don't apply to the
8758 decl, but to its return type. */
8759 type_quals = TYPE_UNQUALIFIED;
8761 errmsg = targetm.invalid_return_type (type);
8762 if (errmsg)
8764 error (errmsg);
8765 type = integer_type_node;
8768 /* Error about some types functions can't return. */
8770 if (TREE_CODE (type) == FUNCTION_TYPE)
8772 error ("%qs declared as function returning a function", name);
8773 return error_mark_node;
8775 if (TREE_CODE (type) == ARRAY_TYPE)
8777 error ("%qs declared as function returning an array", name);
8778 return error_mark_node;
8781 /* Pick up type qualifiers which should be applied to `this'. */
8782 memfn_quals = declarator->u.function.qualifiers;
8784 /* Pick up the exception specifications. */
8785 raises = declarator->u.function.exception_specification;
8787 /* Say it's a definition only for the CALL_EXPR
8788 closest to the identifier. */
8789 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
8791 /* Handle a late-specified return type. */
8792 if (funcdecl_p)
8794 if (type_uses_auto (type))
8796 if (!declarator->u.function.late_return_type)
8798 error ("%qs function uses %<auto%> type specifier without"
8799 " late return type", name);
8800 return error_mark_node;
8802 else if (!is_auto (type))
8804 error ("%qs function with late return type has"
8805 " %qT as its type rather than plain %<auto%>",
8806 name, type);
8807 return error_mark_node;
8810 else if (declarator->u.function.late_return_type)
8812 error ("%qs function with late return type not declared"
8813 " with %<auto%> type specifier", name);
8814 return error_mark_node;
8817 type = splice_late_return_type
8818 (type, declarator->u.function.late_return_type);
8819 if (type == error_mark_node)
8820 return error_mark_node;
8822 if (ctype == NULL_TREE
8823 && decl_context == FIELD
8824 && funcdecl_p
8825 && (friendp == 0 || dname == current_class_name))
8826 ctype = current_class_type;
8828 if (ctype && (sfk == sfk_constructor
8829 || sfk == sfk_destructor))
8831 /* We are within a class's scope. If our declarator name
8832 is the same as the class name, and we are defining
8833 a function, then it is a constructor/destructor, and
8834 therefore returns a void type. */
8836 /* ISO C++ 12.4/2. A destructor may not be declared
8837 const or volatile. A destructor may not be
8838 static.
8840 ISO C++ 12.1. A constructor may not be declared
8841 const or volatile. A constructor may not be
8842 virtual. A constructor may not be static. */
8843 if (staticp == 2)
8844 error ((flags == DTOR_FLAG)
8845 ? "destructor cannot be static member function"
8846 : "constructor cannot be static member function");
8847 if (memfn_quals)
8849 error ((flags == DTOR_FLAG)
8850 ? "destructors may not be cv-qualified"
8851 : "constructors may not be cv-qualified");
8852 memfn_quals = TYPE_UNQUALIFIED;
8855 if (decl_context == FIELD
8856 && !member_function_or_else (ctype,
8857 current_class_type,
8858 flags))
8859 return error_mark_node;
8861 if (flags != DTOR_FLAG)
8863 /* It's a constructor. */
8864 if (explicitp == 1)
8865 explicitp = 2;
8866 if (virtualp)
8868 permerror (input_location, "constructors cannot be declared virtual");
8869 virtualp = 0;
8871 if (decl_context == FIELD
8872 && sfk != sfk_constructor)
8873 return error_mark_node;
8875 if (decl_context == FIELD)
8876 staticp = 0;
8878 else if (friendp)
8880 if (initialized)
8881 error ("can%'t initialize friend function %qs", name);
8882 if (virtualp)
8884 /* Cannot be both friend and virtual. */
8885 error ("virtual functions cannot be friends");
8886 friendp = 0;
8888 if (decl_context == NORMAL)
8889 error ("friend declaration not in class definition");
8890 if (current_function_decl && funcdef_flag)
8891 error ("can%'t define friend function %qs in a local "
8892 "class definition",
8893 name);
8895 else if (ctype && sfk == sfk_conversion)
8897 if (explicitp == 1)
8899 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
8900 explicitp = 2;
8904 arg_types = grokparms (declarator->u.function.parameters,
8905 &parms);
8907 if (inner_declarator
8908 && inner_declarator->kind == cdk_id
8909 && inner_declarator->u.id.sfk == sfk_destructor
8910 && arg_types != void_list_node)
8912 error ("destructors may not have parameters");
8913 arg_types = void_list_node;
8914 parms = NULL_TREE;
8917 type = build_function_type (type, arg_types);
8919 break;
8921 case cdk_pointer:
8922 case cdk_reference:
8923 case cdk_ptrmem:
8924 /* Filter out pointers-to-references and references-to-references.
8925 We can get these if a TYPE_DECL is used. */
8927 if (TREE_CODE (type) == REFERENCE_TYPE)
8929 if (declarator->kind != cdk_reference)
8931 error ("cannot declare pointer to %q#T", type);
8932 type = TREE_TYPE (type);
8935 /* In C++0x, we allow reference to reference declarations
8936 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
8937 and template type arguments [14.3.1/4 temp.arg.type]. The
8938 check for direct reference to reference declarations, which
8939 are still forbidden, occurs below. Reasoning behind the change
8940 can be found in DR106, DR540, and the rvalue reference
8941 proposals. */
8942 else if (cxx_dialect == cxx98)
8944 error ("cannot declare reference to %q#T", type);
8945 type = TREE_TYPE (type);
8948 else if (VOID_TYPE_P (type))
8950 if (declarator->kind == cdk_reference)
8951 error ("cannot declare reference to %q#T", type);
8952 else if (declarator->kind == cdk_ptrmem)
8953 error ("cannot declare pointer to %q#T member", type);
8956 /* We now know that the TYPE_QUALS don't apply to the decl,
8957 but to the target of the pointer. */
8958 type_quals = TYPE_UNQUALIFIED;
8960 if (declarator->kind == cdk_ptrmem
8961 && (TREE_CODE (type) == FUNCTION_TYPE
8962 || (memfn_quals && TREE_CODE (type) == METHOD_TYPE)))
8964 memfn_quals |= type_memfn_quals (type);
8965 type = build_memfn_type (type,
8966 declarator->u.pointer.class_type,
8967 memfn_quals);
8968 if (type == error_mark_node)
8969 return error_mark_node;
8970 memfn_quals = TYPE_UNQUALIFIED;
8973 if (TREE_CODE (type) == FUNCTION_TYPE
8974 && type_memfn_quals (type) != TYPE_UNQUALIFIED)
8975 error (declarator->kind == cdk_reference
8976 ? G_("cannot declare reference to qualified function type %qT")
8977 : G_("cannot declare pointer to qualified function type %qT"),
8978 type);
8980 /* When the pointed-to type involves components of variable size,
8981 care must be taken to ensure that the size evaluation code is
8982 emitted early enough to dominate all the possible later uses
8983 and late enough for the variables on which it depends to have
8984 been assigned.
8986 This is expected to happen automatically when the pointed-to
8987 type has a name/declaration of it's own, but special attention
8988 is required if the type is anonymous.
8990 We handle the NORMAL and FIELD contexts here by inserting a
8991 dummy statement that just evaluates the size at a safe point
8992 and ensures it is not deferred until e.g. within a deeper
8993 conditional context (c++/43555).
8995 We expect nothing to be needed here for PARM or TYPENAME.
8996 Evaluating the size at this point for TYPENAME would
8997 actually be incorrect, as we might be in the middle of an
8998 expression with side effects on the pointed-to type size
8999 "arguments" prior to the pointer declaration point and the
9000 size evaluation could end up prior to the side effects. */
9002 if (!TYPE_NAME (type)
9003 && (decl_context == NORMAL || decl_context == FIELD)
9004 && at_function_scope_p ()
9005 && variably_modified_type_p (type, NULL_TREE))
9006 finish_expr_stmt (TYPE_SIZE (type));
9008 if (declarator->kind == cdk_reference)
9010 /* In C++0x, the type we are creating a reference to might be
9011 a typedef which is itself a reference type. In that case,
9012 we follow the reference collapsing rules in
9013 [7.1.3/8 dcl.typedef] to create the final reference type:
9015 "If a typedef TD names a type that is a reference to a type
9016 T, an attempt to create the type 'lvalue reference to cv TD'
9017 creates the type 'lvalue reference to T,' while an attempt
9018 to create the type "rvalue reference to cv TD' creates the
9019 type TD."
9021 if (!VOID_TYPE_P (type))
9022 type = cp_build_reference_type
9023 ((TREE_CODE (type) == REFERENCE_TYPE
9024 ? TREE_TYPE (type) : type),
9025 (declarator->u.reference.rvalue_ref
9026 && (TREE_CODE(type) != REFERENCE_TYPE
9027 || TYPE_REF_IS_RVALUE (type))));
9029 /* In C++0x, we need this check for direct reference to
9030 reference declarations, which are forbidden by
9031 [8.3.2/5 dcl.ref]. Reference to reference declarations
9032 are only allowed indirectly through typedefs and template
9033 type arguments. Example:
9035 void foo(int & &); // invalid ref-to-ref decl
9037 typedef int & int_ref;
9038 void foo(int_ref &); // valid ref-to-ref decl
9040 if (inner_declarator && inner_declarator->kind == cdk_reference)
9041 error ("cannot declare reference to %q#T, which is not "
9042 "a typedef or a template type argument", type);
9044 else if (TREE_CODE (type) == METHOD_TYPE)
9045 type = build_ptrmemfunc_type (build_pointer_type (type));
9046 else if (declarator->kind == cdk_ptrmem)
9048 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
9049 != NAMESPACE_DECL);
9050 if (declarator->u.pointer.class_type == error_mark_node)
9051 /* We will already have complained. */
9052 type = error_mark_node;
9053 else
9054 type = build_ptrmem_type (declarator->u.pointer.class_type,
9055 type);
9057 else
9058 type = build_pointer_type (type);
9060 /* Process a list of type modifier keywords (such as
9061 const or volatile) that were given inside the `*' or `&'. */
9063 if (declarator->u.pointer.qualifiers)
9065 type
9066 = cp_build_qualified_type (type,
9067 declarator->u.pointer.qualifiers);
9068 type_quals = cp_type_quals (type);
9070 ctype = NULL_TREE;
9071 break;
9073 case cdk_error:
9074 break;
9076 default:
9077 gcc_unreachable ();
9081 /* A `constexpr' specifier used in an object declaration declares
9082 the object as `const'. */
9083 if (constexpr_p && innermost_code != cdk_function)
9085 if (type_quals & TYPE_QUAL_CONST)
9086 error ("both %<const%> and %<constexpr%> cannot be used here");
9087 if (type_quals & TYPE_QUAL_VOLATILE)
9088 error ("both %<volatile%> and %<constexpr%> cannot be used here");
9089 type_quals |= TYPE_QUAL_CONST;
9090 type = cp_build_qualified_type (type, type_quals);
9093 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
9094 && TREE_CODE (type) != FUNCTION_TYPE
9095 && TREE_CODE (type) != METHOD_TYPE)
9097 error ("template-id %qD used as a declarator",
9098 unqualified_id);
9099 unqualified_id = dname;
9102 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
9103 qualified with a class-name, turn it into a METHOD_TYPE, unless
9104 we know that the function is static. We take advantage of this
9105 opportunity to do other processing that pertains to entities
9106 explicitly declared to be class members. Note that if DECLARATOR
9107 is non-NULL, we know it is a cdk_id declarator; otherwise, we
9108 would not have exited the loop above. */
9109 if (declarator
9110 && declarator->u.id.qualifying_scope
9111 && TYPE_P (declarator->u.id.qualifying_scope))
9113 tree t;
9115 ctype = declarator->u.id.qualifying_scope;
9116 ctype = TYPE_MAIN_VARIANT (ctype);
9117 t = ctype;
9118 while (t != NULL_TREE && CLASS_TYPE_P (t))
9120 /* You're supposed to have one `template <...>' for every
9121 template class, but you don't need one for a full
9122 specialization. For example:
9124 template <class T> struct S{};
9125 template <> struct S<int> { void f(); };
9126 void S<int>::f () {}
9128 is correct; there shouldn't be a `template <>' for the
9129 definition of `S<int>::f'. */
9130 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)
9131 && !any_dependent_template_arguments_p (CLASSTYPE_TI_ARGS (t)))
9132 /* T is an explicit (not partial) specialization. All
9133 containing classes must therefore also be explicitly
9134 specialized. */
9135 break;
9136 if ((CLASSTYPE_USE_TEMPLATE (t) || CLASSTYPE_IS_TEMPLATE (t))
9137 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
9138 template_count += 1;
9140 t = TYPE_MAIN_DECL (t);
9141 t = DECL_CONTEXT (t);
9144 if (ctype == current_class_type)
9146 if (friendp)
9148 permerror (input_location, "member functions are implicitly friends of their class");
9149 friendp = 0;
9151 else
9152 permerror (declarator->id_loc,
9153 "extra qualification %<%T::%> on member %qs",
9154 ctype, name);
9156 else if (/* If the qualifying type is already complete, then we
9157 can skip the following checks. */
9158 !COMPLETE_TYPE_P (ctype)
9159 && (/* If the function is being defined, then
9160 qualifying type must certainly be complete. */
9161 funcdef_flag
9162 /* A friend declaration of "T::f" is OK, even if
9163 "T" is a template parameter. But, if this
9164 function is not a friend, the qualifying type
9165 must be a class. */
9166 || (!friendp && !CLASS_TYPE_P (ctype))
9167 /* For a declaration, the type need not be
9168 complete, if either it is dependent (since there
9169 is no meaningful definition of complete in that
9170 case) or the qualifying class is currently being
9171 defined. */
9172 || !(dependent_type_p (ctype)
9173 || currently_open_class (ctype)))
9174 /* Check that the qualifying type is complete. */
9175 && !complete_type_or_else (ctype, NULL_TREE))
9176 return error_mark_node;
9177 else if (TREE_CODE (type) == FUNCTION_TYPE)
9179 if (current_class_type
9180 && (!friendp || funcdef_flag))
9182 error (funcdef_flag
9183 ? "cannot define member function %<%T::%s%> within %<%T%>"
9184 : "cannot declare member function %<%T::%s%> within %<%T%>",
9185 ctype, name, current_class_type);
9186 return error_mark_node;
9189 else if (declspecs->specs[(int)ds_typedef]
9190 && current_class_type)
9192 error ("cannot declare member %<%T::%s%> within %qT",
9193 ctype, name, current_class_type);
9194 return error_mark_node;
9198 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
9199 ctype = current_class_type;
9201 /* A constexpr non-static member function is implicitly const. */
9202 if (constexpr_p && ctype && staticp == 0
9203 && TREE_CODE (type) == FUNCTION_TYPE
9204 && sfk != sfk_constructor && sfk != sfk_destructor)
9205 memfn_quals |= TYPE_QUAL_CONST;
9207 /* Now TYPE has the actual type. */
9209 if (returned_attrs)
9211 if (attrlist)
9212 *attrlist = chainon (returned_attrs, *attrlist);
9213 else
9214 attrlist = &returned_attrs;
9217 /* Handle parameter packs. */
9218 if (parameter_pack_p)
9220 if (decl_context == PARM)
9221 /* Turn the type into a pack expansion.*/
9222 type = make_pack_expansion (type);
9223 else
9224 error ("non-parameter %qs cannot be a parameter pack", name);
9227 /* Did array size calculations overflow? */
9229 if (TREE_CODE (type) == ARRAY_TYPE
9230 && COMPLETE_TYPE_P (type)
9231 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9232 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
9234 error ("size of array %qs is too large", name);
9235 /* If we proceed with the array type as it is, we'll eventually
9236 crash in tree_low_cst(). */
9237 type = error_mark_node;
9240 if ((decl_context == FIELD || decl_context == PARM)
9241 && !processing_template_decl
9242 && variably_modified_type_p (type, NULL_TREE))
9244 if (decl_context == FIELD)
9245 error ("data member may not have variably modified type %qT", type);
9246 else
9247 error ("parameter may not have variably modified type %qT", type);
9248 type = error_mark_node;
9251 if (explicitp == 1 || (explicitp && friendp))
9253 /* [dcl.fct.spec] The explicit specifier shall only be used in
9254 declarations of constructors within a class definition. */
9255 error ("only declarations of constructors can be %<explicit%>");
9256 explicitp = 0;
9259 if (storage_class == sc_mutable)
9261 if (decl_context != FIELD || friendp)
9263 error ("non-member %qs cannot be declared %<mutable%>", name);
9264 storage_class = sc_none;
9266 else if (decl_context == TYPENAME || declspecs->specs[(int)ds_typedef])
9268 error ("non-object member %qs cannot be declared %<mutable%>", name);
9269 storage_class = sc_none;
9271 else if (TREE_CODE (type) == FUNCTION_TYPE
9272 || TREE_CODE (type) == METHOD_TYPE)
9274 error ("function %qs cannot be declared %<mutable%>", name);
9275 storage_class = sc_none;
9277 else if (staticp)
9279 error ("static %qs cannot be declared %<mutable%>", name);
9280 storage_class = sc_none;
9282 else if (type_quals & TYPE_QUAL_CONST)
9284 error ("const %qs cannot be declared %<mutable%>", name);
9285 storage_class = sc_none;
9287 else if (TREE_CODE (type) == REFERENCE_TYPE)
9289 permerror (input_location, "reference %qs cannot be declared "
9290 "%<mutable%>", name);
9291 storage_class = sc_none;
9295 /* If this is declaring a typedef name, return a TYPE_DECL. */
9296 if (declspecs->specs[(int)ds_typedef] && decl_context != TYPENAME)
9298 tree decl;
9300 /* Note that the grammar rejects storage classes
9301 in typenames, fields or parameters. */
9302 if (current_lang_name == lang_name_java)
9303 TYPE_FOR_JAVA (type) = 1;
9305 /* This declaration:
9307 typedef void f(int) const;
9309 declares a function type which is not a member of any
9310 particular class, but which is cv-qualified; for
9311 example "f S::*" declares a pointer to a const-qualified
9312 member function of S. We record the cv-qualification in the
9313 function type. */
9314 if (memfn_quals && TREE_CODE (type) == FUNCTION_TYPE)
9316 type = apply_memfn_quals (type, memfn_quals);
9318 /* We have now dealt with these qualifiers. */
9319 memfn_quals = TYPE_UNQUALIFIED;
9322 if (decl_context == FIELD)
9323 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
9324 else
9325 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
9326 if (id_declarator && declarator->u.id.qualifying_scope) {
9327 error_at (DECL_SOURCE_LOCATION (decl),
9328 "typedef name may not be a nested-name-specifier");
9329 TREE_TYPE (decl) = error_mark_node;
9332 if (decl_context != FIELD)
9334 if (!current_function_decl)
9335 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
9336 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
9337 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
9338 (current_function_decl)))
9339 /* The TYPE_DECL is "abstract" because there will be
9340 clones of this constructor/destructor, and there will
9341 be copies of this TYPE_DECL generated in those
9342 clones. */
9343 DECL_ABSTRACT (decl) = 1;
9345 else if (constructor_name_p (unqualified_id, current_class_type))
9346 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
9347 "as enclosing class",
9348 unqualified_id);
9350 /* If the user declares "typedef struct {...} foo" then the
9351 struct will have an anonymous name. Fill that name in now.
9352 Nothing can refer to it, so nothing needs know about the name
9353 change. */
9354 if (type != error_mark_node
9355 && unqualified_id
9356 && TYPE_NAME (type)
9357 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9358 && TYPE_ANONYMOUS_P (type)
9359 && cp_type_quals (type) == TYPE_UNQUALIFIED)
9361 tree t;
9363 /* Replace the anonymous name with the real name everywhere. */
9364 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9366 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
9367 /* We do not rename the debug info representing the
9368 anonymous tagged type because the standard says in
9369 [dcl.typedef] that the naming applies only for
9370 linkage purposes. */
9371 /*debug_hooks->set_name (t, decl);*/
9372 TYPE_NAME (t) = decl;
9375 if (TYPE_LANG_SPECIFIC (type))
9376 TYPE_WAS_ANONYMOUS (type) = 1;
9378 /* If this is a typedef within a template class, the nested
9379 type is a (non-primary) template. The name for the
9380 template needs updating as well. */
9381 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
9382 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
9383 = TYPE_IDENTIFIER (type);
9385 /* Adjust linkage now that we aren't anonymous anymore. */
9386 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
9387 determine_visibility (TYPE_MAIN_DECL (type));
9389 /* FIXME remangle member functions; member functions of a
9390 type with external linkage have external linkage. */
9393 if (signed_p
9394 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
9395 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
9397 bad_specifiers (decl, BSP_TYPE, virtualp,
9398 memfn_quals != TYPE_UNQUALIFIED,
9399 inlinep, friendp, raises != NULL_TREE);
9401 return decl;
9404 /* Detect the case of an array type of unspecified size
9405 which came, as such, direct from a typedef name.
9406 We must copy the type, so that the array's domain can be
9407 individually set by the object's initializer. */
9409 if (type && typedef_type
9410 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9411 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
9412 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
9414 /* Detect where we're using a typedef of function type to declare a
9415 function. PARMS will not be set, so we must create it now. */
9417 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
9419 tree decls = NULL_TREE;
9420 tree args;
9422 for (args = TYPE_ARG_TYPES (type);
9423 args && args != void_list_node;
9424 args = TREE_CHAIN (args))
9426 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
9428 DECL_CHAIN (decl) = decls;
9429 decls = decl;
9432 parms = nreverse (decls);
9434 if (decl_context != TYPENAME)
9436 /* A cv-qualifier-seq shall only be part of the function type
9437 for a non-static member function. [8.3.5/4 dcl.fct] */
9438 if (type_memfn_quals (type) != TYPE_UNQUALIFIED
9439 && (current_class_type == NULL_TREE || staticp) )
9441 error (staticp
9442 ? G_("qualified function types cannot be used to "
9443 "declare static member functions")
9444 : G_("qualified function types cannot be used to "
9445 "declare free functions"));
9446 type = TYPE_MAIN_VARIANT (type);
9449 /* The qualifiers on the function type become the qualifiers on
9450 the non-static member function. */
9451 memfn_quals |= type_memfn_quals (type);
9452 type_quals = TYPE_UNQUALIFIED;
9456 /* If this is a type name (such as, in a cast or sizeof),
9457 compute the type and return it now. */
9459 if (decl_context == TYPENAME)
9461 /* Note that the grammar rejects storage classes
9462 in typenames, fields or parameters. */
9463 if (type_quals != TYPE_UNQUALIFIED)
9464 type_quals = TYPE_UNQUALIFIED;
9466 /* Special case: "friend class foo" looks like a TYPENAME context. */
9467 if (friendp)
9469 if (type_quals != TYPE_UNQUALIFIED)
9471 error ("type qualifiers specified for friend class declaration");
9472 type_quals = TYPE_UNQUALIFIED;
9474 if (inlinep)
9476 error ("%<inline%> specified for friend class declaration");
9477 inlinep = 0;
9480 if (!current_aggr)
9482 /* Don't allow friend declaration without a class-key. */
9483 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
9484 permerror (input_location, "template parameters cannot be friends");
9485 else if (TREE_CODE (type) == TYPENAME_TYPE)
9486 permerror (input_location, "friend declaration requires class-key, "
9487 "i.e. %<friend class %T::%D%>",
9488 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
9489 else
9490 permerror (input_location, "friend declaration requires class-key, "
9491 "i.e. %<friend %#T%>",
9492 type);
9495 /* Only try to do this stuff if we didn't already give up. */
9496 if (type != integer_type_node)
9498 /* A friendly class? */
9499 if (current_class_type)
9500 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
9501 /*complain=*/true);
9502 else
9503 error ("trying to make class %qT a friend of global scope",
9504 type);
9506 type = void_type_node;
9509 else if (memfn_quals)
9511 if (ctype == NULL_TREE
9512 && TREE_CODE (type) == METHOD_TYPE)
9513 ctype = TYPE_METHOD_BASETYPE (type);
9515 if (ctype)
9516 type = build_memfn_type (type, ctype, memfn_quals);
9517 /* Core issue #547: need to allow this in template type args. */
9518 else if (template_type_arg && TREE_CODE (type) == FUNCTION_TYPE)
9519 type = apply_memfn_quals (type, memfn_quals);
9520 else
9521 error ("invalid qualifiers on non-member function type");
9524 return type;
9526 else if (unqualified_id == NULL_TREE && decl_context != PARM
9527 && decl_context != CATCHPARM
9528 && TREE_CODE (type) != UNION_TYPE
9529 && ! bitfield)
9531 error ("abstract declarator %qT used as declaration", type);
9532 return error_mark_node;
9535 /* Only functions may be declared using an operator-function-id. */
9536 if (unqualified_id
9537 && IDENTIFIER_OPNAME_P (unqualified_id)
9538 && TREE_CODE (type) != FUNCTION_TYPE
9539 && TREE_CODE (type) != METHOD_TYPE)
9541 error ("declaration of %qD as non-function", unqualified_id);
9542 return error_mark_node;
9545 /* We don't check parameter types here because we can emit a better
9546 error message later. */
9547 if (decl_context != PARM)
9549 type = check_var_type (unqualified_id, type);
9550 if (type == error_mark_node)
9551 return error_mark_node;
9554 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
9555 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
9557 if (decl_context == PARM || decl_context == CATCHPARM)
9559 if (ctype || in_namespace)
9560 error ("cannot use %<::%> in parameter declaration");
9562 /* A parameter declared as an array of T is really a pointer to T.
9563 One declared as a function is really a pointer to a function.
9564 One declared as a member is really a pointer to member. */
9566 if (TREE_CODE (type) == ARRAY_TYPE)
9568 /* Transfer const-ness of array into that of type pointed to. */
9569 type = build_pointer_type (TREE_TYPE (type));
9570 type_quals = TYPE_UNQUALIFIED;
9572 else if (TREE_CODE (type) == FUNCTION_TYPE)
9573 type = build_pointer_type (type);
9576 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
9577 && !NEW_DELETE_OPNAME_P (unqualified_id))
9578 type = build_memfn_type (type, ctype, memfn_quals);
9581 tree decl;
9583 if (decl_context == PARM)
9585 decl = cp_build_parm_decl (unqualified_id, type);
9587 bad_specifiers (decl, BSP_PARM, virtualp,
9588 memfn_quals != TYPE_UNQUALIFIED,
9589 inlinep, friendp, raises != NULL_TREE);
9591 else if (decl_context == FIELD)
9593 /* The C99 flexible array extension. */
9594 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
9595 && TYPE_DOMAIN (type) == NULL_TREE)
9597 tree itype = compute_array_index_type (dname, integer_zero_node,
9598 tf_warning_or_error);
9599 type = build_cplus_array_type (TREE_TYPE (type), itype);
9602 if (type == error_mark_node)
9604 /* Happens when declaring arrays of sizes which
9605 are error_mark_node, for example. */
9606 decl = NULL_TREE;
9608 else if (in_namespace && !friendp)
9610 /* Something like struct S { int N::j; }; */
9611 error ("invalid use of %<::%>");
9612 return error_mark_node;
9614 else if (TREE_CODE (type) == FUNCTION_TYPE
9615 || TREE_CODE (type) == METHOD_TYPE)
9617 int publicp = 0;
9618 tree function_context;
9620 if (friendp == 0)
9622 /* This should never happen in pure C++ (the check
9623 could be an assert). It could happen in
9624 Objective-C++ if someone writes invalid code that
9625 uses a function declaration for an instance
9626 variable or property (instance variables and
9627 properties are parsed as FIELD_DECLs, but they are
9628 part of an Objective-C class, not a C++ class).
9629 That code is invalid and is caught by this
9630 check. */
9631 if (!ctype)
9633 error ("declaration of function %qD in invalid context",
9634 unqualified_id);
9635 return error_mark_node;
9638 /* ``A union may [ ... ] not [ have ] virtual functions.''
9639 ARM 9.5 */
9640 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
9642 error ("function %qD declared virtual inside a union",
9643 unqualified_id);
9644 return error_mark_node;
9647 if (NEW_DELETE_OPNAME_P (unqualified_id))
9649 if (virtualp)
9651 error ("%qD cannot be declared virtual, since it "
9652 "is always static",
9653 unqualified_id);
9654 virtualp = 0;
9659 /* Check that the name used for a destructor makes sense. */
9660 if (sfk == sfk_destructor)
9662 tree uqname = id_declarator->u.id.unqualified_name;
9664 if (!ctype)
9666 gcc_assert (friendp);
9667 error ("expected qualified name in friend declaration "
9668 "for destructor %qD", uqname);
9669 return error_mark_node;
9672 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
9674 error ("declaration of %qD as member of %qT",
9675 uqname, ctype);
9676 return error_mark_node;
9678 if (constexpr_p)
9680 error ("a destructor cannot be %<constexpr%>");
9681 return error_mark_node;
9684 else if (sfk == sfk_constructor && friendp && !ctype)
9686 error ("expected qualified name in friend declaration "
9687 "for constructor %qD",
9688 id_declarator->u.id.unqualified_name);
9689 return error_mark_node;
9692 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
9693 function_context = (ctype != NULL_TREE) ?
9694 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
9695 publicp = (! friendp || ! staticp)
9696 && function_context == NULL_TREE;
9697 decl = grokfndecl (ctype, type,
9698 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
9699 ? unqualified_id : dname,
9700 parms,
9701 unqualified_id,
9702 virtualp, flags, memfn_quals, raises,
9703 friendp ? -1 : 0, friendp, publicp,
9704 inlinep | (2 * constexpr_p),
9705 sfk,
9706 funcdef_flag, template_count, in_namespace,
9707 attrlist, declarator->id_loc);
9708 if (decl == NULL_TREE)
9709 return error_mark_node;
9710 #if 0
9711 /* This clobbers the attrs stored in `decl' from `attrlist'. */
9712 /* The decl and setting of decl_attr is also turned off. */
9713 decl = build_decl_attribute_variant (decl, decl_attr);
9714 #endif
9716 /* [class.conv.ctor]
9718 A constructor declared without the function-specifier
9719 explicit that can be called with a single parameter
9720 specifies a conversion from the type of its first
9721 parameter to the type of its class. Such a constructor
9722 is called a converting constructor. */
9723 if (explicitp == 2)
9724 DECL_NONCONVERTING_P (decl) = 1;
9726 else if (!staticp && !dependent_type_p (type)
9727 && !COMPLETE_TYPE_P (complete_type (type))
9728 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
9730 if (unqualified_id)
9731 error ("field %qD has incomplete type", unqualified_id);
9732 else
9733 error ("name %qT has incomplete type", type);
9735 /* If we're instantiating a template, tell them which
9736 instantiation made the field's type be incomplete. */
9737 if (current_class_type
9738 && TYPE_NAME (current_class_type)
9739 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
9740 && declspecs->type
9741 && declspecs->type == type)
9742 error (" in instantiation of template %qT",
9743 current_class_type);
9745 return error_mark_node;
9747 else
9749 if (friendp)
9751 error ("%qE is neither function nor member function; "
9752 "cannot be declared friend", unqualified_id);
9753 friendp = 0;
9755 decl = NULL_TREE;
9758 if (friendp)
9760 /* Friends are treated specially. */
9761 if (ctype == current_class_type)
9762 ; /* We already issued a permerror. */
9763 else if (decl && DECL_NAME (decl))
9765 if (template_class_depth (current_class_type) == 0)
9767 decl = check_explicit_specialization
9768 (unqualified_id, decl, template_count,
9769 2 * funcdef_flag + 4);
9770 if (decl == error_mark_node)
9771 return error_mark_node;
9774 DECL_DECLARED_CONSTEXPR_P (decl) = constexpr_p;
9775 decl = do_friend (ctype, unqualified_id, decl,
9776 *attrlist, flags,
9777 funcdef_flag);
9778 return decl;
9780 else
9781 return error_mark_node;
9784 /* Structure field. It may not be a function, except for C++. */
9786 if (decl == NULL_TREE)
9788 if (initialized)
9790 if (!staticp)
9792 /* An attempt is being made to initialize a non-static
9793 member. But, from [class.mem]:
9795 4 A member-declarator can contain a
9796 constant-initializer only if it declares a static
9797 member (_class.static_) of integral or enumeration
9798 type, see _class.static.data_.
9800 This used to be relatively common practice, but
9801 the rest of the compiler does not correctly
9802 handle the initialization unless the member is
9803 static so we make it static below. */
9804 if (cxx_dialect >= cxx0x)
9806 sorry ("non-static data member initializers");
9808 else
9810 permerror (input_location, "ISO C++ forbids initialization of member %qD",
9811 unqualified_id);
9812 permerror (input_location, "making %qD static", unqualified_id);
9813 staticp = 1;
9817 if (uses_template_parms (type))
9818 /* We'll check at instantiation time. */
9820 else if (constexpr_p)
9821 /* constexpr has the same requirements. */
9823 else if (check_static_variable_definition (unqualified_id,
9824 type))
9825 /* If we just return the declaration, crashes
9826 will sometimes occur. We therefore return
9827 void_type_node, as if this was a friend
9828 declaration, to cause callers to completely
9829 ignore this declaration. */
9830 return error_mark_node;
9833 if (staticp)
9835 /* C++ allows static class members. All other work
9836 for this is done by grokfield. */
9837 decl = build_lang_decl (VAR_DECL, unqualified_id, type);
9838 set_linkage_for_static_data_member (decl);
9839 /* Even if there is an in-class initialization, DECL
9840 is considered undefined until an out-of-class
9841 definition is provided. */
9842 DECL_EXTERNAL (decl) = 1;
9844 if (thread_p)
9845 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
9847 if (constexpr_p && !initialized)
9849 error ("constexpr static data member %qD must have an "
9850 "initializer", decl);
9851 constexpr_p = false;
9854 else
9856 if (constexpr_p)
9858 error ("non-static data member %qE declared %<constexpr%>",
9859 unqualified_id);
9860 constexpr_p = false;
9862 decl = build_decl (input_location,
9863 FIELD_DECL, unqualified_id, type);
9864 DECL_NONADDRESSABLE_P (decl) = bitfield;
9865 if (bitfield && !unqualified_id)
9866 TREE_NO_WARNING (decl) = 1;
9868 if (storage_class == sc_mutable)
9870 DECL_MUTABLE_P (decl) = 1;
9871 storage_class = sc_none;
9875 bad_specifiers (decl, BSP_FIELD, virtualp,
9876 memfn_quals != TYPE_UNQUALIFIED,
9877 inlinep, friendp, raises != NULL_TREE);
9880 else if (TREE_CODE (type) == FUNCTION_TYPE
9881 || TREE_CODE (type) == METHOD_TYPE)
9883 tree original_name;
9884 int publicp = 0;
9886 if (!unqualified_id)
9887 return error_mark_node;
9889 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
9890 original_name = dname;
9891 else
9892 original_name = unqualified_id;
9894 if (storage_class == sc_auto)
9895 error ("storage class %<auto%> invalid for function %qs", name);
9896 else if (storage_class == sc_register)
9897 error ("storage class %<register%> invalid for function %qs", name);
9898 else if (thread_p)
9899 error ("storage class %<__thread%> invalid for function %qs", name);
9901 /* Function declaration not at top level.
9902 Storage classes other than `extern' are not allowed
9903 and `extern' makes no difference. */
9904 if (! toplevel_bindings_p ()
9905 && (storage_class == sc_static
9906 || declspecs->specs[(int)ds_inline])
9907 && pedantic)
9909 if (storage_class == sc_static)
9910 pedwarn (input_location, OPT_pedantic,
9911 "%<static%> specified invalid for function %qs "
9912 "declared out of global scope", name);
9913 else
9914 pedwarn (input_location, OPT_pedantic,
9915 "%<inline%> specifier invalid for function %qs "
9916 "declared out of global scope", name);
9919 if (ctype != NULL_TREE
9920 && TREE_CODE (ctype) != NAMESPACE_DECL && !MAYBE_CLASS_TYPE_P (ctype))
9922 error ("%q#T is not a class or a namespace", ctype);
9923 ctype = NULL_TREE;
9926 if (ctype == NULL_TREE)
9928 if (virtualp)
9930 error ("virtual non-class function %qs", name);
9931 virtualp = 0;
9933 else if (sfk == sfk_constructor
9934 || sfk == sfk_destructor)
9936 error (funcdef_flag
9937 ? "%qs defined in a non-class scope"
9938 : "%qs declared in a non-class scope", name);
9939 sfk = sfk_none;
9943 /* Record presence of `static'. */
9944 publicp = (ctype != NULL_TREE
9945 || storage_class == sc_extern
9946 || storage_class != sc_static);
9948 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
9949 virtualp, flags, memfn_quals, raises,
9950 1, friendp,
9951 publicp, inlinep | (2 * constexpr_p), sfk,
9952 funcdef_flag,
9953 template_count, in_namespace, attrlist,
9954 declarator->id_loc);
9955 if (decl == NULL_TREE)
9956 return error_mark_node;
9958 if (staticp == 1)
9960 int invalid_static = 0;
9962 /* Don't allow a static member function in a class, and forbid
9963 declaring main to be static. */
9964 if (TREE_CODE (type) == METHOD_TYPE)
9966 permerror (input_location, "cannot declare member function %qD to have "
9967 "static linkage", decl);
9968 invalid_static = 1;
9970 else if (current_function_decl)
9972 /* FIXME need arm citation */
9973 error ("cannot declare static function inside another function");
9974 invalid_static = 1;
9977 if (invalid_static)
9979 staticp = 0;
9980 storage_class = sc_none;
9984 else
9986 /* It's a variable. */
9988 /* An uninitialized decl with `extern' is a reference. */
9989 decl = grokvardecl (type, unqualified_id,
9990 declspecs,
9991 initialized,
9992 (type_quals & TYPE_QUAL_CONST) != 0,
9993 ctype ? ctype : in_namespace);
9994 bad_specifiers (decl, BSP_VAR, virtualp,
9995 memfn_quals != TYPE_UNQUALIFIED,
9996 inlinep, friendp, raises != NULL_TREE);
9998 if (ctype)
10000 DECL_CONTEXT (decl) = ctype;
10001 if (staticp == 1)
10003 permerror (input_location, "%<static%> may not be used when defining "
10004 "(as opposed to declaring) a static data member");
10005 staticp = 0;
10006 storage_class = sc_none;
10008 if (storage_class == sc_register && TREE_STATIC (decl))
10010 error ("static member %qD declared %<register%>", decl);
10011 storage_class = sc_none;
10013 if (storage_class == sc_extern && pedantic)
10015 pedwarn (input_location, OPT_pedantic,
10016 "cannot explicitly declare member %q#D to have "
10017 "extern linkage", decl);
10018 storage_class = sc_none;
10021 else if (constexpr_p && DECL_EXTERNAL (decl))
10022 error ("declaration of constexpr variable %qD is not a definition",
10023 decl);
10026 if (storage_class == sc_extern && initialized && !funcdef_flag)
10028 if (toplevel_bindings_p ())
10030 /* It's common practice (and completely valid) to have a const
10031 be initialized and declared extern. */
10032 if (!(type_quals & TYPE_QUAL_CONST))
10033 warning (0, "%qs initialized and declared %<extern%>", name);
10035 else
10037 error ("%qs has both %<extern%> and initializer", name);
10038 return error_mark_node;
10042 /* Record `register' declaration for warnings on &
10043 and in case doing stupid register allocation. */
10045 if (storage_class == sc_register)
10046 DECL_REGISTER (decl) = 1;
10047 else if (storage_class == sc_extern)
10048 DECL_THIS_EXTERN (decl) = 1;
10049 else if (storage_class == sc_static)
10050 DECL_THIS_STATIC (decl) = 1;
10052 /* Don't forget constexprness. */
10053 if (constexpr_p)
10054 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10056 /* Record constancy and volatility on the DECL itself . There's
10057 no need to do this when processing a template; we'll do this
10058 for the instantiated declaration based on the type of DECL. */
10059 if (!processing_template_decl)
10060 cp_apply_type_quals_to_decl (type_quals, decl);
10062 return decl;
10066 /* Subroutine of start_function. Ensure that each of the parameter
10067 types (as listed in PARMS) is complete, as is required for a
10068 function definition. */
10070 static void
10071 require_complete_types_for_parms (tree parms)
10073 for (; parms; parms = DECL_CHAIN (parms))
10075 if (dependent_type_p (TREE_TYPE (parms)))
10076 continue;
10077 if (!VOID_TYPE_P (TREE_TYPE (parms))
10078 && complete_type_or_else (TREE_TYPE (parms), parms))
10080 relayout_decl (parms);
10081 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
10083 else
10084 /* grokparms or complete_type_or_else will have already issued
10085 an error. */
10086 TREE_TYPE (parms) = error_mark_node;
10090 /* Returns nonzero if T is a local variable. */
10093 local_variable_p (const_tree t)
10095 if ((TREE_CODE (t) == VAR_DECL
10096 /* A VAR_DECL with a context that is a _TYPE is a static data
10097 member. */
10098 && !TYPE_P (CP_DECL_CONTEXT (t))
10099 /* Any other non-local variable must be at namespace scope. */
10100 && !DECL_NAMESPACE_SCOPE_P (t))
10101 || (TREE_CODE (t) == PARM_DECL))
10102 return 1;
10104 return 0;
10107 /* Like local_variable_p, but suitable for use as a tree-walking
10108 function. */
10110 static tree
10111 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
10112 void *data ATTRIBUTE_UNUSED)
10114 if (local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
10115 return *tp;
10116 else if (TYPE_P (*tp))
10117 *walk_subtrees = 0;
10119 return NULL_TREE;
10123 /* Check that ARG, which is a default-argument expression for a
10124 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
10125 something goes wrong. DECL may also be a _TYPE node, rather than a
10126 DECL, if there is no DECL available. */
10128 tree
10129 check_default_argument (tree decl, tree arg)
10131 tree var;
10132 tree decl_type;
10134 if (TREE_CODE (arg) == DEFAULT_ARG)
10135 /* We get a DEFAULT_ARG when looking at an in-class declaration
10136 with a default argument. Ignore the argument for now; we'll
10137 deal with it after the class is complete. */
10138 return arg;
10140 if (TYPE_P (decl))
10142 decl_type = decl;
10143 decl = NULL_TREE;
10145 else
10146 decl_type = TREE_TYPE (decl);
10148 if (arg == error_mark_node
10149 || decl == error_mark_node
10150 || TREE_TYPE (arg) == error_mark_node
10151 || decl_type == error_mark_node)
10152 /* Something already went wrong. There's no need to check
10153 further. */
10154 return error_mark_node;
10156 /* [dcl.fct.default]
10158 A default argument expression is implicitly converted to the
10159 parameter type. */
10160 if (!TREE_TYPE (arg)
10161 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg, LOOKUP_NORMAL))
10163 if (decl)
10164 error ("default argument for %q#D has type %qT",
10165 decl, TREE_TYPE (arg));
10166 else
10167 error ("default argument for parameter of type %qT has type %qT",
10168 decl_type, TREE_TYPE (arg));
10170 return error_mark_node;
10173 /* [dcl.fct.default]
10175 Local variables shall not be used in default argument
10176 expressions.
10178 The keyword `this' shall not be used in a default argument of a
10179 member function. */
10180 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
10181 if (var)
10183 error ("default argument %qE uses local variable %qD", arg, var);
10184 return error_mark_node;
10187 /* All is well. */
10188 return arg;
10191 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
10193 static tree
10194 type_is_deprecated (tree type)
10196 enum tree_code code;
10197 if (TREE_DEPRECATED (type))
10198 return type;
10199 if (TYPE_NAME (type)
10200 && TREE_DEPRECATED (TYPE_NAME (type)))
10201 return type;
10203 /* Do warn about using typedefs to a deprecated class. */
10204 if (TAGGED_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
10205 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
10207 code = TREE_CODE (type);
10209 if (code == POINTER_TYPE || code == REFERENCE_TYPE
10210 || code == OFFSET_TYPE || code == FUNCTION_TYPE
10211 || code == METHOD_TYPE || code == ARRAY_TYPE)
10212 return type_is_deprecated (TREE_TYPE (type));
10214 if (TYPE_PTRMEMFUNC_P (type))
10215 return type_is_deprecated
10216 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
10218 return NULL_TREE;
10221 /* Decode the list of parameter types for a function type.
10222 Given the list of things declared inside the parens,
10223 return a list of types.
10225 If this parameter does not end with an ellipsis, we append
10226 void_list_node.
10228 *PARMS is set to the chain of PARM_DECLs created. */
10230 static tree
10231 grokparms (tree parmlist, tree *parms)
10233 tree result = NULL_TREE;
10234 tree decls = NULL_TREE;
10235 tree parm;
10236 int any_error = 0;
10238 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
10240 tree type = NULL_TREE;
10241 tree init = TREE_PURPOSE (parm);
10242 tree decl = TREE_VALUE (parm);
10243 const char *errmsg;
10245 if (parm == void_list_node)
10246 break;
10248 if (! decl || TREE_TYPE (decl) == error_mark_node)
10249 continue;
10251 type = TREE_TYPE (decl);
10252 if (VOID_TYPE_P (type))
10254 if (same_type_p (type, void_type_node)
10255 && DECL_SELF_REFERENCE_P (type)
10256 && !DECL_NAME (decl) && !result && TREE_CHAIN (parm) == void_list_node)
10257 /* this is a parmlist of `(void)', which is ok. */
10258 break;
10259 cxx_incomplete_type_error (decl, type);
10260 /* It's not a good idea to actually create parameters of
10261 type `void'; other parts of the compiler assume that a
10262 void type terminates the parameter list. */
10263 type = error_mark_node;
10264 TREE_TYPE (decl) = error_mark_node;
10267 if (type != error_mark_node
10268 && TYPE_FOR_JAVA (type)
10269 && MAYBE_CLASS_TYPE_P (type))
10271 error ("parameter %qD has Java class type", decl);
10272 type = error_mark_node;
10273 TREE_TYPE (decl) = error_mark_node;
10274 init = NULL_TREE;
10277 if (type != error_mark_node
10278 && (errmsg = targetm.invalid_parameter_type (type)))
10280 error (errmsg);
10281 type = error_mark_node;
10282 TREE_TYPE (decl) = error_mark_node;
10285 if (type != error_mark_node)
10287 if (deprecated_state != DEPRECATED_SUPPRESS)
10289 tree deptype = type_is_deprecated (type);
10290 if (deptype)
10291 warn_deprecated_use (deptype, NULL_TREE);
10294 /* Top-level qualifiers on the parameters are
10295 ignored for function types. */
10296 type = cp_build_qualified_type (type, 0);
10297 if (TREE_CODE (type) == METHOD_TYPE)
10299 error ("parameter %qD invalidly declared method type", decl);
10300 type = build_pointer_type (type);
10301 TREE_TYPE (decl) = type;
10303 else if (abstract_virtuals_error (decl, type))
10304 any_error = 1; /* Seems like a good idea. */
10305 else if (POINTER_TYPE_P (type))
10307 /* [dcl.fct]/6, parameter types cannot contain pointers
10308 (references) to arrays of unknown bound. */
10309 tree t = TREE_TYPE (type);
10310 int ptr = TYPE_PTR_P (type);
10312 while (1)
10314 if (TYPE_PTR_P (t))
10315 ptr = 1;
10316 else if (TREE_CODE (t) != ARRAY_TYPE)
10317 break;
10318 else if (!TYPE_DOMAIN (t))
10319 break;
10320 t = TREE_TYPE (t);
10322 if (TREE_CODE (t) == ARRAY_TYPE)
10323 error (ptr
10324 ? G_("parameter %qD includes pointer to array of "
10325 "unknown bound %qT")
10326 : G_("parameter %qD includes reference to array of "
10327 "unknown bound %qT"),
10328 decl, t);
10331 if (any_error)
10332 init = NULL_TREE;
10333 else if (init && !processing_template_decl)
10334 init = check_default_argument (decl, init);
10337 if (TREE_CODE (decl) == PARM_DECL
10338 && FUNCTION_PARAMETER_PACK_P (decl)
10339 && TREE_CHAIN (parm)
10340 && TREE_CHAIN (parm) != void_list_node)
10341 error ("parameter packs must be at the end of the parameter list");
10343 DECL_CHAIN (decl) = decls;
10344 decls = decl;
10345 result = tree_cons (init, type, result);
10347 decls = nreverse (decls);
10348 result = nreverse (result);
10349 if (parm)
10350 result = chainon (result, void_list_node);
10351 *parms = decls;
10353 return result;
10357 /* D is a constructor or overloaded `operator='.
10359 Let T be the class in which D is declared. Then, this function
10360 returns:
10362 -1 if D's is an ill-formed constructor or copy assignment operator
10363 whose first parameter is of type `T'.
10364 0 if D is not a copy constructor or copy assignment
10365 operator.
10366 1 if D is a copy constructor or copy assignment operator whose
10367 first parameter is a reference to non-const qualified T.
10368 2 if D is a copy constructor or copy assignment operator whose
10369 first parameter is a reference to const qualified T.
10371 This function can be used as a predicate. Positive values indicate
10372 a copy constructor and nonzero values indicate a copy assignment
10373 operator. */
10376 copy_fn_p (const_tree d)
10378 tree args;
10379 tree arg_type;
10380 int result = 1;
10382 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
10384 if (TREE_CODE (d) == TEMPLATE_DECL
10385 || (DECL_TEMPLATE_INFO (d)
10386 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
10387 /* Instantiations of template member functions are never copy
10388 functions. Note that member functions of templated classes are
10389 represented as template functions internally, and we must
10390 accept those as copy functions. */
10391 return 0;
10393 args = FUNCTION_FIRST_USER_PARMTYPE (d);
10394 if (!args)
10395 return 0;
10397 arg_type = TREE_VALUE (args);
10398 if (arg_type == error_mark_node)
10399 return 0;
10401 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
10403 /* Pass by value copy assignment operator. */
10404 result = -1;
10406 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
10407 && !TYPE_REF_IS_RVALUE (arg_type)
10408 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
10410 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
10411 result = 2;
10413 else
10414 return 0;
10416 args = TREE_CHAIN (args);
10418 if (args && args != void_list_node && !TREE_PURPOSE (args))
10419 /* There are more non-optional args. */
10420 return 0;
10422 return result;
10425 /* D is a constructor or overloaded `operator='.
10427 Let T be the class in which D is declared. Then, this function
10428 returns true when D is a move constructor or move assignment
10429 operator, false otherwise. */
10431 bool
10432 move_fn_p (const_tree d)
10434 tree args;
10435 tree arg_type;
10436 bool result = false;
10438 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
10440 if (cxx_dialect == cxx98)
10441 /* There are no move constructors if we are in C++98 mode. */
10442 return false;
10444 if (TREE_CODE (d) == TEMPLATE_DECL
10445 || (DECL_TEMPLATE_INFO (d)
10446 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
10447 /* Instantiations of template member functions are never copy
10448 functions. Note that member functions of templated classes are
10449 represented as template functions internally, and we must
10450 accept those as copy functions. */
10451 return 0;
10453 args = FUNCTION_FIRST_USER_PARMTYPE (d);
10454 if (!args)
10455 return 0;
10457 arg_type = TREE_VALUE (args);
10458 if (arg_type == error_mark_node)
10459 return 0;
10461 if (TREE_CODE (arg_type) == REFERENCE_TYPE
10462 && TYPE_REF_IS_RVALUE (arg_type)
10463 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
10464 DECL_CONTEXT (d)))
10465 result = true;
10467 args = TREE_CHAIN (args);
10469 if (args && args != void_list_node && !TREE_PURPOSE (args))
10470 /* There are more non-optional args. */
10471 return false;
10473 return result;
10476 /* Remember any special properties of member function DECL. */
10478 void
10479 grok_special_member_properties (tree decl)
10481 tree class_type;
10483 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
10484 return;
10486 class_type = DECL_CONTEXT (decl);
10487 if (DECL_CONSTRUCTOR_P (decl))
10489 int ctor = copy_fn_p (decl);
10491 if (!DECL_ARTIFICIAL (decl))
10492 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
10494 if (ctor > 0)
10496 /* [class.copy]
10498 A non-template constructor for class X is a copy
10499 constructor if its first parameter is of type X&, const
10500 X&, volatile X& or const volatile X&, and either there
10501 are no other parameters or else all other parameters have
10502 default arguments. */
10503 TYPE_HAS_COPY_CTOR (class_type) = 1;
10504 if (user_provided_p (decl))
10505 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
10506 if (ctor > 1)
10507 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
10509 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
10511 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
10512 if (user_provided_p (decl))
10513 TYPE_HAS_COMPLEX_DFLT (class_type) = 1;
10515 else if (move_fn_p (decl) && user_provided_p (decl))
10516 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
10517 else if (is_list_ctor (decl))
10518 TYPE_HAS_LIST_CTOR (class_type) = 1;
10520 if (DECL_DECLARED_CONSTEXPR_P (decl)
10521 && !copy_fn_p (decl) && !move_fn_p (decl))
10522 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
10524 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
10526 /* [class.copy]
10528 A non-template assignment operator for class X is a copy
10529 assignment operator if its parameter is of type X, X&, const
10530 X&, volatile X& or const volatile X&. */
10532 int assop = copy_fn_p (decl);
10534 if (assop)
10536 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
10537 if (user_provided_p (decl))
10538 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
10539 if (assop != 1)
10540 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
10542 else if (move_fn_p (decl) && user_provided_p (decl))
10543 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
10545 /* Destructors are handled in check_methods. */
10548 /* Check a constructor DECL has the correct form. Complains
10549 if the class has a constructor of the form X(X). */
10552 grok_ctor_properties (const_tree ctype, const_tree decl)
10554 int ctor_parm = copy_fn_p (decl);
10556 if (ctor_parm < 0)
10558 /* [class.copy]
10560 A declaration of a constructor for a class X is ill-formed if
10561 its first parameter is of type (optionally cv-qualified) X
10562 and either there are no other parameters or else all other
10563 parameters have default arguments.
10565 We *don't* complain about member template instantiations that
10566 have this form, though; they can occur as we try to decide
10567 what constructor to use during overload resolution. Since
10568 overload resolution will never prefer such a constructor to
10569 the non-template copy constructor (which is either explicitly
10570 or implicitly defined), there's no need to worry about their
10571 existence. Theoretically, they should never even be
10572 instantiated, but that's hard to forestall. */
10573 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
10574 ctype, ctype);
10575 return 0;
10578 return 1;
10581 /* An operator with this code is unary, but can also be binary. */
10583 static int
10584 ambi_op_p (enum tree_code code)
10586 return (code == INDIRECT_REF
10587 || code == ADDR_EXPR
10588 || code == UNARY_PLUS_EXPR
10589 || code == NEGATE_EXPR
10590 || code == PREINCREMENT_EXPR
10591 || code == PREDECREMENT_EXPR);
10594 /* An operator with this name can only be unary. */
10596 static int
10597 unary_op_p (enum tree_code code)
10599 return (code == TRUTH_NOT_EXPR
10600 || code == BIT_NOT_EXPR
10601 || code == COMPONENT_REF
10602 || code == TYPE_EXPR);
10605 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
10606 errors are issued for invalid declarations. */
10608 bool
10609 grok_op_properties (tree decl, bool complain)
10611 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10612 tree argtype;
10613 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
10614 tree name = DECL_NAME (decl);
10615 enum tree_code operator_code;
10616 int arity;
10617 bool ellipsis_p;
10618 tree class_type;
10620 /* Count the number of arguments and check for ellipsis. */
10621 for (argtype = argtypes, arity = 0;
10622 argtype && argtype != void_list_node;
10623 argtype = TREE_CHAIN (argtype))
10624 ++arity;
10625 ellipsis_p = !argtype;
10627 class_type = DECL_CONTEXT (decl);
10628 if (class_type && !CLASS_TYPE_P (class_type))
10629 class_type = NULL_TREE;
10631 if (DECL_CONV_FN_P (decl))
10632 operator_code = TYPE_EXPR;
10633 else
10636 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
10637 if (ansi_opname (CODE) == name) \
10639 operator_code = (CODE); \
10640 break; \
10642 else if (ansi_assopname (CODE) == name) \
10644 operator_code = (CODE); \
10645 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
10646 break; \
10649 #include "operators.def"
10650 #undef DEF_OPERATOR
10652 gcc_unreachable ();
10654 while (0);
10655 gcc_assert (operator_code != MAX_TREE_CODES);
10656 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
10658 if (class_type)
10659 switch (operator_code)
10661 case NEW_EXPR:
10662 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
10663 break;
10665 case DELETE_EXPR:
10666 TYPE_GETS_DELETE (class_type) |= 1;
10667 break;
10669 case VEC_NEW_EXPR:
10670 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
10671 break;
10673 case VEC_DELETE_EXPR:
10674 TYPE_GETS_DELETE (class_type) |= 2;
10675 break;
10677 default:
10678 break;
10681 /* [basic.std.dynamic.allocation]/1:
10683 A program is ill-formed if an allocation function is declared
10684 in a namespace scope other than global scope or declared static
10685 in global scope.
10687 The same also holds true for deallocation functions. */
10688 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
10689 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
10691 if (DECL_NAMESPACE_SCOPE_P (decl))
10693 if (CP_DECL_CONTEXT (decl) != global_namespace)
10695 error ("%qD may not be declared within a namespace", decl);
10696 return false;
10698 else if (!TREE_PUBLIC (decl))
10700 error ("%qD may not be declared as static", decl);
10701 return false;
10706 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
10708 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
10709 DECL_IS_OPERATOR_NEW (decl) = 1;
10711 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
10712 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
10713 else
10715 /* An operator function must either be a non-static member function
10716 or have at least one parameter of a class, a reference to a class,
10717 an enumeration, or a reference to an enumeration. 13.4.0.6 */
10718 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
10720 if (operator_code == TYPE_EXPR
10721 || operator_code == CALL_EXPR
10722 || operator_code == COMPONENT_REF
10723 || operator_code == ARRAY_REF
10724 || operator_code == NOP_EXPR)
10726 error ("%qD must be a nonstatic member function", decl);
10727 return false;
10729 else
10731 tree p;
10733 if (DECL_STATIC_FUNCTION_P (decl))
10735 error ("%qD must be either a non-static member "
10736 "function or a non-member function", decl);
10737 return false;
10740 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
10742 tree arg = non_reference (TREE_VALUE (p));
10743 if (arg == error_mark_node)
10744 return false;
10746 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
10747 because these checks are performed even on
10748 template functions. */
10749 if (MAYBE_CLASS_TYPE_P (arg)
10750 || TREE_CODE (arg) == ENUMERAL_TYPE)
10751 break;
10754 if (!p || p == void_list_node)
10756 if (complain)
10757 error ("%qD must have an argument of class or "
10758 "enumerated type", decl);
10759 return false;
10764 /* There are no restrictions on the arguments to an overloaded
10765 "operator ()". */
10766 if (operator_code == CALL_EXPR)
10767 return true;
10769 /* Warn about conversion operators that will never be used. */
10770 if (IDENTIFIER_TYPENAME_P (name)
10771 && ! DECL_TEMPLATE_INFO (decl)
10772 && warn_conversion
10773 /* Warn only declaring the function; there is no need to
10774 warn again about out-of-class definitions. */
10775 && class_type == current_class_type)
10777 tree t = TREE_TYPE (name);
10778 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
10780 if (ref)
10781 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
10783 if (TREE_CODE (t) == VOID_TYPE)
10784 warning (OPT_Wconversion,
10786 ? G_("conversion to a reference to void "
10787 "will never use a type conversion operator")
10788 : G_("conversion to void "
10789 "will never use a type conversion operator"));
10790 else if (class_type)
10792 if (t == class_type)
10793 warning (OPT_Wconversion,
10795 ? G_("conversion to a reference to the same type "
10796 "will never use a type conversion operator")
10797 : G_("conversion to the same type "
10798 "will never use a type conversion operator"));
10799 /* Don't force t to be complete here. */
10800 else if (MAYBE_CLASS_TYPE_P (t)
10801 && COMPLETE_TYPE_P (t)
10802 && DERIVED_FROM_P (t, class_type))
10803 warning (OPT_Wconversion,
10805 ? G_("conversion to a reference to a base class "
10806 "will never use a type conversion operator")
10807 : G_("conversion to a base class "
10808 "will never use a type conversion operator"));
10813 if (operator_code == COND_EXPR)
10815 /* 13.4.0.3 */
10816 error ("ISO C++ prohibits overloading operator ?:");
10817 return false;
10819 else if (ellipsis_p)
10821 error ("%qD must not have variable number of arguments", decl);
10822 return false;
10824 else if (ambi_op_p (operator_code))
10826 if (arity == 1)
10827 /* We pick the one-argument operator codes by default, so
10828 we don't have to change anything. */
10830 else if (arity == 2)
10832 /* If we thought this was a unary operator, we now know
10833 it to be a binary operator. */
10834 switch (operator_code)
10836 case INDIRECT_REF:
10837 operator_code = MULT_EXPR;
10838 break;
10840 case ADDR_EXPR:
10841 operator_code = BIT_AND_EXPR;
10842 break;
10844 case UNARY_PLUS_EXPR:
10845 operator_code = PLUS_EXPR;
10846 break;
10848 case NEGATE_EXPR:
10849 operator_code = MINUS_EXPR;
10850 break;
10852 case PREINCREMENT_EXPR:
10853 operator_code = POSTINCREMENT_EXPR;
10854 break;
10856 case PREDECREMENT_EXPR:
10857 operator_code = POSTDECREMENT_EXPR;
10858 break;
10860 default:
10861 gcc_unreachable ();
10864 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
10866 if ((operator_code == POSTINCREMENT_EXPR
10867 || operator_code == POSTDECREMENT_EXPR)
10868 && ! processing_template_decl
10869 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
10871 if (methodp)
10872 error ("postfix %qD must take %<int%> as its argument",
10873 decl);
10874 else
10875 error ("postfix %qD must take %<int%> as its second "
10876 "argument", decl);
10877 return false;
10880 else
10882 if (methodp)
10883 error ("%qD must take either zero or one argument", decl);
10884 else
10885 error ("%qD must take either one or two arguments", decl);
10886 return false;
10889 /* More Effective C++ rule 6. */
10890 if (warn_ecpp
10891 && (operator_code == POSTINCREMENT_EXPR
10892 || operator_code == POSTDECREMENT_EXPR
10893 || operator_code == PREINCREMENT_EXPR
10894 || operator_code == PREDECREMENT_EXPR))
10896 tree arg = TREE_VALUE (argtypes);
10897 tree ret = TREE_TYPE (TREE_TYPE (decl));
10898 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
10899 arg = TREE_TYPE (arg);
10900 arg = TYPE_MAIN_VARIANT (arg);
10901 if (operator_code == PREINCREMENT_EXPR
10902 || operator_code == PREDECREMENT_EXPR)
10904 if (TREE_CODE (ret) != REFERENCE_TYPE
10905 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
10906 arg))
10907 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
10908 build_reference_type (arg));
10910 else
10912 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
10913 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
10917 else if (unary_op_p (operator_code))
10919 if (arity != 1)
10921 if (methodp)
10922 error ("%qD must take %<void%>", decl);
10923 else
10924 error ("%qD must take exactly one argument", decl);
10925 return false;
10928 else /* if (binary_op_p (operator_code)) */
10930 if (arity != 2)
10932 if (methodp)
10933 error ("%qD must take exactly one argument", decl);
10934 else
10935 error ("%qD must take exactly two arguments", decl);
10936 return false;
10939 /* More Effective C++ rule 7. */
10940 if (warn_ecpp
10941 && (operator_code == TRUTH_ANDIF_EXPR
10942 || operator_code == TRUTH_ORIF_EXPR
10943 || operator_code == COMPOUND_EXPR))
10944 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
10945 decl);
10948 /* Effective C++ rule 23. */
10949 if (warn_ecpp
10950 && arity == 2
10951 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
10952 && (operator_code == PLUS_EXPR
10953 || operator_code == MINUS_EXPR
10954 || operator_code == TRUNC_DIV_EXPR
10955 || operator_code == MULT_EXPR
10956 || operator_code == TRUNC_MOD_EXPR)
10957 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
10958 warning (OPT_Weffc__, "%qD should return by value", decl);
10960 /* [over.oper]/8 */
10961 for (; argtypes && argtypes != void_list_node;
10962 argtypes = TREE_CHAIN (argtypes))
10963 if (TREE_PURPOSE (argtypes))
10965 TREE_PURPOSE (argtypes) = NULL_TREE;
10966 if (operator_code == POSTINCREMENT_EXPR
10967 || operator_code == POSTDECREMENT_EXPR)
10969 pedwarn (input_location, OPT_pedantic, "%qD cannot have default arguments",
10970 decl);
10972 else
10974 error ("%qD cannot have default arguments", decl);
10975 return false;
10979 return true;
10982 /* Return a string giving the keyword associate with CODE. */
10984 static const char *
10985 tag_name (enum tag_types code)
10987 switch (code)
10989 case record_type:
10990 return "struct";
10991 case class_type:
10992 return "class";
10993 case union_type:
10994 return "union";
10995 case enum_type:
10996 return "enum";
10997 case typename_type:
10998 return "typename";
10999 default:
11000 gcc_unreachable ();
11004 /* Name lookup in an elaborated-type-specifier (after the keyword
11005 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
11006 elaborated-type-specifier is invalid, issue a diagnostic and return
11007 error_mark_node; otherwise, return the *_TYPE to which it referred.
11008 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
11010 tree
11011 check_elaborated_type_specifier (enum tag_types tag_code,
11012 tree decl,
11013 bool allow_template_p)
11015 tree type;
11017 /* In the case of:
11019 struct S { struct S *p; };
11021 name lookup will find the TYPE_DECL for the implicit "S::S"
11022 typedef. Adjust for that here. */
11023 if (DECL_SELF_REFERENCE_P (decl))
11024 decl = TYPE_NAME (TREE_TYPE (decl));
11026 type = TREE_TYPE (decl);
11028 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
11029 is false for this case as well. */
11030 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11032 error ("using template type parameter %qT after %qs",
11033 type, tag_name (tag_code));
11034 return error_mark_node;
11036 /* [dcl.type.elab]
11038 If the identifier resolves to a typedef-name or a template
11039 type-parameter, the elaborated-type-specifier is ill-formed.
11041 In other words, the only legitimate declaration to use in the
11042 elaborated type specifier is the implicit typedef created when
11043 the type is declared. */
11044 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
11045 && !DECL_SELF_REFERENCE_P (decl)
11046 && tag_code != typename_type)
11048 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
11049 error ("%q+D has a previous declaration here", decl);
11050 return error_mark_node;
11052 else if (TREE_CODE (type) != RECORD_TYPE
11053 && TREE_CODE (type) != UNION_TYPE
11054 && tag_code != enum_type
11055 && tag_code != typename_type)
11057 error ("%qT referred to as %qs", type, tag_name (tag_code));
11058 error ("%q+T has a previous declaration here", type);
11059 return error_mark_node;
11061 else if (TREE_CODE (type) != ENUMERAL_TYPE
11062 && tag_code == enum_type)
11064 error ("%qT referred to as enum", type);
11065 error ("%q+T has a previous declaration here", type);
11066 return error_mark_node;
11068 else if (!allow_template_p
11069 && TREE_CODE (type) == RECORD_TYPE
11070 && CLASSTYPE_IS_TEMPLATE (type))
11072 /* If a class template appears as elaborated type specifier
11073 without a template header such as:
11075 template <class T> class C {};
11076 void f(class C); // No template header here
11078 then the required template argument is missing. */
11079 error ("template argument required for %<%s %T%>",
11080 tag_name (tag_code),
11081 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
11082 return error_mark_node;
11085 return type;
11088 /* Lookup NAME in elaborate type specifier in scope according to
11089 SCOPE and issue diagnostics if necessary.
11090 Return *_TYPE node upon success, NULL_TREE when the NAME is not
11091 found, and ERROR_MARK_NODE for type error. */
11093 static tree
11094 lookup_and_check_tag (enum tag_types tag_code, tree name,
11095 tag_scope scope, bool template_header_p)
11097 tree t;
11098 tree decl;
11099 if (scope == ts_global)
11101 /* First try ordinary name lookup, ignoring hidden class name
11102 injected via friend declaration. */
11103 decl = lookup_name_prefer_type (name, 2);
11104 /* If that fails, the name will be placed in the smallest
11105 non-class, non-function-prototype scope according to 3.3.1/5.
11106 We may already have a hidden name declared as friend in this
11107 scope. So lookup again but not ignoring hidden names.
11108 If we find one, that name will be made visible rather than
11109 creating a new tag. */
11110 if (!decl)
11111 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
11113 else
11114 decl = lookup_type_scope (name, scope);
11116 if (decl && DECL_CLASS_TEMPLATE_P (decl))
11117 decl = DECL_TEMPLATE_RESULT (decl);
11119 if (decl && TREE_CODE (decl) == TYPE_DECL)
11121 /* Look for invalid nested type:
11122 class C {
11123 class C {};
11124 }; */
11125 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
11127 error ("%qD has the same name as the class in which it is "
11128 "declared",
11129 decl);
11130 return error_mark_node;
11133 /* Two cases we need to consider when deciding if a class
11134 template is allowed as an elaborated type specifier:
11135 1. It is a self reference to its own class.
11136 2. It comes with a template header.
11138 For example:
11140 template <class T> class C {
11141 class C *c1; // DECL_SELF_REFERENCE_P is true
11142 class D;
11144 template <class U> class C; // template_header_p is true
11145 template <class T> class C<T>::D {
11146 class C *c2; // DECL_SELF_REFERENCE_P is true
11147 }; */
11149 t = check_elaborated_type_specifier (tag_code,
11150 decl,
11151 template_header_p
11152 | DECL_SELF_REFERENCE_P (decl));
11153 return t;
11155 else if (decl && TREE_CODE (decl) == TREE_LIST)
11157 error ("reference to %qD is ambiguous", name);
11158 print_candidates (decl);
11159 return error_mark_node;
11161 else
11162 return NULL_TREE;
11165 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
11166 Define the tag as a forward-reference if it is not defined.
11168 If a declaration is given, process it here, and report an error if
11169 multiple declarations are not identical.
11171 SCOPE is TS_CURRENT when this is also a definition. Only look in
11172 the current frame for the name (since C++ allows new names in any
11173 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
11174 declaration. Only look beginning from the current scope outward up
11175 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
11177 TEMPLATE_HEADER_P is true when this declaration is preceded by
11178 a set of template parameters. */
11180 static tree
11181 xref_tag_1 (enum tag_types tag_code, tree name,
11182 tag_scope scope, bool template_header_p)
11184 enum tree_code code;
11185 tree t;
11186 tree context = NULL_TREE;
11188 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
11190 switch (tag_code)
11192 case record_type:
11193 case class_type:
11194 code = RECORD_TYPE;
11195 break;
11196 case union_type:
11197 code = UNION_TYPE;
11198 break;
11199 case enum_type:
11200 code = ENUMERAL_TYPE;
11201 break;
11202 default:
11203 gcc_unreachable ();
11206 /* In case of anonymous name, xref_tag is only called to
11207 make type node and push name. Name lookup is not required. */
11208 if (ANON_AGGRNAME_P (name))
11209 t = NULL_TREE;
11210 else
11211 t = lookup_and_check_tag (tag_code, name,
11212 scope, template_header_p);
11214 if (t == error_mark_node)
11215 return error_mark_node;
11217 if (scope != ts_current && t && current_class_type
11218 && template_class_depth (current_class_type)
11219 && template_header_p)
11221 /* Since SCOPE is not TS_CURRENT, we are not looking at a
11222 definition of this tag. Since, in addition, we are currently
11223 processing a (member) template declaration of a template
11224 class, we must be very careful; consider:
11226 template <class X>
11227 struct S1
11229 template <class U>
11230 struct S2
11231 { template <class V>
11232 friend struct S1; };
11234 Here, the S2::S1 declaration should not be confused with the
11235 outer declaration. In particular, the inner version should
11236 have a template parameter of level 2, not level 1. This
11237 would be particularly important if the member declaration
11238 were instead:
11240 template <class V = U> friend struct S1;
11242 say, when we should tsubst into `U' when instantiating
11243 S2. On the other hand, when presented with:
11245 template <class T>
11246 struct S1 {
11247 template <class U>
11248 struct S2 {};
11249 template <class U>
11250 friend struct S2;
11253 we must find the inner binding eventually. We
11254 accomplish this by making sure that the new type we
11255 create to represent this declaration has the right
11256 TYPE_CONTEXT. */
11257 context = TYPE_CONTEXT (t);
11258 t = NULL_TREE;
11261 if (! t)
11263 /* If no such tag is yet defined, create a forward-reference node
11264 and record it as the "definition".
11265 When a real declaration of this type is found,
11266 the forward-reference will be altered into a real type. */
11267 if (code == ENUMERAL_TYPE)
11269 error ("use of enum %q#D without previous declaration", name);
11270 return error_mark_node;
11272 else
11274 t = make_class_type (code);
11275 TYPE_CONTEXT (t) = context;
11276 /* FIXME pph: creating incomplete class. */
11277 t = pushtag (name, t, scope);
11280 else
11282 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
11284 if (!redeclare_class_template (t, current_template_parms))
11285 return error_mark_node;
11287 else if (!processing_template_decl
11288 && CLASS_TYPE_P (t)
11289 && CLASSTYPE_IS_TEMPLATE (t))
11291 error ("redeclaration of %qT as a non-template", t);
11292 error ("previous declaration %q+D", t);
11293 return error_mark_node;
11296 /* Make injected friend class visible. */
11297 if (scope != ts_within_enclosing_non_class
11298 && hidden_name_p (TYPE_NAME (t)))
11300 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
11301 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
11303 if (TYPE_TEMPLATE_INFO (t))
11305 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
11306 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
11311 return t;
11314 /* Wrapper for xref_tag_1. */
11316 tree
11317 xref_tag (enum tag_types tag_code, tree name,
11318 tag_scope scope, bool template_header_p)
11320 tree ret;
11321 timevar_start (TV_NAME_LOOKUP);
11322 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
11323 pph_catch_name_lookup (ret);
11324 timevar_stop (TV_NAME_LOOKUP);
11325 return ret;
11329 tree
11330 xref_tag_from_type (tree old, tree id, tag_scope scope)
11332 enum tag_types tag_kind;
11334 if (TREE_CODE (old) == RECORD_TYPE)
11335 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
11336 else
11337 tag_kind = union_type;
11339 if (id == NULL_TREE)
11340 id = TYPE_IDENTIFIER (old);
11342 return xref_tag (tag_kind, id, scope, false);
11345 /* Create the binfo hierarchy for REF with (possibly NULL) base list
11346 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
11347 access_* node, and the TREE_VALUE is the type of the base-class.
11348 Non-NULL TREE_TYPE indicates virtual inheritance.
11350 Returns true if the binfo hierarchy was successfully created,
11351 false if an error was detected. */
11353 bool
11354 xref_basetypes (tree ref, tree base_list)
11356 tree *basep;
11357 tree binfo, base_binfo;
11358 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
11359 unsigned max_bases = 0; /* Maximum direct bases. */
11360 int i;
11361 tree default_access;
11362 tree igo_prev; /* Track Inheritance Graph Order. */
11364 if (ref == error_mark_node)
11365 return false;
11367 /* The base of a derived class is private by default, all others are
11368 public. */
11369 default_access = (TREE_CODE (ref) == RECORD_TYPE
11370 && CLASSTYPE_DECLARED_CLASS (ref)
11371 ? access_private_node : access_public_node);
11373 /* First, make sure that any templates in base-classes are
11374 instantiated. This ensures that if we call ourselves recursively
11375 we do not get confused about which classes are marked and which
11376 are not. */
11377 basep = &base_list;
11378 while (*basep)
11380 tree basetype = TREE_VALUE (*basep);
11382 if (!(processing_template_decl && uses_template_parms (basetype))
11383 && !complete_type_or_else (basetype, NULL))
11384 /* An incomplete type. Remove it from the list. */
11385 *basep = TREE_CHAIN (*basep);
11386 else
11388 max_bases++;
11389 if (TREE_TYPE (*basep))
11390 max_vbases++;
11391 if (CLASS_TYPE_P (basetype))
11392 max_vbases += VEC_length (tree, CLASSTYPE_VBASECLASSES (basetype));
11393 basep = &TREE_CHAIN (*basep);
11397 TYPE_MARKED_P (ref) = 1;
11399 /* The binfo slot should be empty, unless this is an (ill-formed)
11400 redefinition. */
11401 if (TYPE_BINFO (ref) && !TYPE_SIZE (ref))
11403 error ("redefinition of %q#T", ref);
11404 return false;
11407 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
11409 binfo = make_tree_binfo (max_bases);
11411 TYPE_BINFO (ref) = binfo;
11412 BINFO_OFFSET (binfo) = size_zero_node;
11413 BINFO_TYPE (binfo) = ref;
11415 /* Apply base-class info set up to the variants of this type. */
11416 fixup_type_variants (ref);
11418 if (max_bases)
11420 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, max_bases);
11421 /* An aggregate cannot have baseclasses. */
11422 CLASSTYPE_NON_AGGREGATE (ref) = 1;
11424 if (TREE_CODE (ref) == UNION_TYPE)
11426 error ("derived union %qT invalid", ref);
11427 return false;
11431 if (max_bases > 1)
11433 if (TYPE_FOR_JAVA (ref))
11435 error ("Java class %qT cannot have multiple bases", ref);
11436 return false;
11440 if (max_vbases)
11442 CLASSTYPE_VBASECLASSES (ref) = VEC_alloc (tree, gc, max_vbases);
11444 if (TYPE_FOR_JAVA (ref))
11446 error ("Java class %qT cannot have virtual bases", ref);
11447 return false;
11451 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
11453 tree access = TREE_PURPOSE (base_list);
11454 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
11455 tree basetype = TREE_VALUE (base_list);
11457 if (access == access_default_node)
11458 access = default_access;
11460 if (PACK_EXPANSION_P (basetype))
11461 basetype = PACK_EXPANSION_PATTERN (basetype);
11462 if (TREE_CODE (basetype) == TYPE_DECL)
11463 basetype = TREE_TYPE (basetype);
11464 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
11466 error ("base type %qT fails to be a struct or class type",
11467 basetype);
11468 return false;
11471 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
11472 TYPE_FOR_JAVA (ref) = 1;
11474 base_binfo = NULL_TREE;
11475 if (CLASS_TYPE_P (basetype) && !dependent_type_p (basetype))
11477 base_binfo = TYPE_BINFO (basetype);
11478 /* The original basetype could have been a typedef'd type. */
11479 basetype = BINFO_TYPE (base_binfo);
11481 /* Inherit flags from the base. */
11482 TYPE_HAS_NEW_OPERATOR (ref)
11483 |= TYPE_HAS_NEW_OPERATOR (basetype);
11484 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
11485 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
11486 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
11487 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
11488 CLASSTYPE_DIAMOND_SHAPED_P (ref)
11489 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
11490 CLASSTYPE_REPEATED_BASE_P (ref)
11491 |= CLASSTYPE_REPEATED_BASE_P (basetype);
11494 /* We must do this test after we've seen through a typedef
11495 type. */
11496 if (TYPE_MARKED_P (basetype))
11498 if (basetype == ref)
11499 error ("recursive type %qT undefined", basetype);
11500 else
11501 error ("duplicate base type %qT invalid", basetype);
11502 return false;
11505 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
11506 /* Regenerate the pack expansion for the bases. */
11507 basetype = make_pack_expansion (basetype);
11509 TYPE_MARKED_P (basetype) = 1;
11511 base_binfo = copy_binfo (base_binfo, basetype, ref,
11512 &igo_prev, via_virtual);
11513 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
11514 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
11516 BINFO_BASE_APPEND (binfo, base_binfo);
11517 BINFO_BASE_ACCESS_APPEND (binfo, access);
11520 if (VEC_space (tree, CLASSTYPE_VBASECLASSES (ref), 1))
11521 /* If we have space in the vbase vector, we must have shared at
11522 least one of them, and are therefore diamond shaped. */
11523 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
11525 /* Unmark all the types. */
11526 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11527 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
11528 TYPE_MARKED_P (ref) = 0;
11530 /* Now see if we have a repeated base type. */
11531 if (!CLASSTYPE_REPEATED_BASE_P (ref))
11533 for (base_binfo = binfo; base_binfo;
11534 base_binfo = TREE_CHAIN (base_binfo))
11536 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
11538 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
11539 break;
11541 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
11543 for (base_binfo = binfo; base_binfo;
11544 base_binfo = TREE_CHAIN (base_binfo))
11545 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
11546 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
11547 else
11548 break;
11551 return true;
11555 /* Copies the enum-related properties from type SRC to type DST.
11556 Used with the underlying type of an enum and the enum itself. */
11557 static void
11558 copy_type_enum (tree dst, tree src)
11560 TYPE_MIN_VALUE (dst) = TYPE_MIN_VALUE (src);
11561 TYPE_MAX_VALUE (dst) = TYPE_MAX_VALUE (src);
11562 TYPE_SIZE (dst) = TYPE_SIZE (src);
11563 TYPE_SIZE_UNIT (dst) = TYPE_SIZE_UNIT (src);
11564 SET_TYPE_MODE (dst, TYPE_MODE (src));
11565 TYPE_PRECISION (dst) = TYPE_PRECISION (src);
11566 TYPE_ALIGN (dst) = TYPE_ALIGN (src);
11567 TYPE_USER_ALIGN (dst) = TYPE_USER_ALIGN (src);
11568 TYPE_UNSIGNED (dst) = TYPE_UNSIGNED (src);
11571 /* Begin compiling the definition of an enumeration type.
11572 NAME is its name,
11574 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
11576 UNDERLYING_TYPE is the type that will be used as the storage for
11577 the enumeration type. This should be NULL_TREE if no storage type
11578 was specified.
11580 SCOPED_ENUM_P is true if this is a scoped enumeration type.
11582 if IS_NEW is not NULL, gets TRUE iff a new type is created.
11584 Returns the type object, as yet incomplete.
11585 Also records info about it so that build_enumerator
11586 may be used to declare the individual values as they are read. */
11588 tree
11589 start_enum (tree name, tree enumtype, tree underlying_type,
11590 bool scoped_enum_p, bool *is_new)
11592 tree prevtype = NULL_TREE;
11593 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
11595 if (is_new)
11596 *is_new = false;
11597 /* [C++0x dcl.enum]p5:
11599 If not explicitly specified, the underlying type of a scoped
11600 enumeration type is int. */
11601 if (!underlying_type && scoped_enum_p)
11602 underlying_type = integer_type_node;
11604 if (underlying_type)
11605 underlying_type = cv_unqualified (underlying_type);
11607 /* If this is the real definition for a previous forward reference,
11608 fill in the contents in the same object that used to be the
11609 forward reference. */
11610 if (!enumtype)
11611 enumtype = lookup_and_check_tag (enum_type, name,
11612 /*tag_scope=*/ts_current,
11613 /*template_header_p=*/false);
11615 /* In case of a template_decl, the only check that should be deferred
11616 to instantiation time is the comparison of underlying types. */
11617 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
11619 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
11621 error_at (input_location, "scoped/unscoped mismatch "
11622 "in enum %q#T", enumtype);
11623 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
11624 "previous definition here");
11625 enumtype = error_mark_node;
11627 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
11629 error_at (input_location, "underlying type mismatch "
11630 "in enum %q#T", enumtype);
11631 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
11632 "previous definition here");
11633 enumtype = error_mark_node;
11635 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
11636 && !dependent_type_p (underlying_type)
11637 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
11638 && !same_type_p (underlying_type,
11639 ENUM_UNDERLYING_TYPE (enumtype)))
11641 error_at (input_location, "different underlying type "
11642 "in enum %q#T", enumtype);
11643 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
11644 "previous definition here");
11645 underlying_type = NULL_TREE;
11649 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
11650 || processing_template_decl)
11652 /* In case of error, make a dummy enum to allow parsing to
11653 continue. */
11654 if (enumtype == error_mark_node)
11656 name = make_anon_name ();
11657 enumtype = NULL_TREE;
11660 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
11661 of an opaque enum, or an opaque enum of an already defined
11662 enumeration (C++0x only).
11663 In any other case, it'll be NULL_TREE. */
11664 if (!enumtype)
11666 if (is_new)
11667 *is_new = true;
11669 prevtype = enumtype;
11670 enumtype = cxx_make_type (ENUMERAL_TYPE);
11671 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
11672 if (enumtype == error_mark_node)
11673 return error_mark_node;
11675 /* The enum is considered opaque until the opening '{' of the
11676 enumerator list. */
11677 SET_OPAQUE_ENUM_P (enumtype, true);
11678 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
11681 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
11683 if (underlying_type)
11685 if (CP_INTEGRAL_TYPE_P (underlying_type))
11687 copy_type_enum (enumtype, underlying_type);
11688 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
11690 else if (dependent_type_p (underlying_type))
11691 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
11692 else
11693 error ("underlying type %<%T%> of %<%T%> must be an integral type",
11694 underlying_type, enumtype);
11697 /* If into a template class, the returned enum is always the first
11698 declaration (opaque or not) seen. This way all the references to
11699 this type will be to the same declaration. The following ones are used
11700 only to check for definition errors. */
11701 if (prevtype && processing_template_decl)
11702 return prevtype;
11703 else
11704 return enumtype;
11707 /* After processing and defining all the values of an enumeration type,
11708 install their decls in the enumeration type.
11709 ENUMTYPE is the type object. */
11711 void
11712 finish_enum_value_list (tree enumtype)
11714 tree values;
11715 tree underlying_type;
11716 tree decl;
11717 tree value;
11718 tree minnode, maxnode;
11719 tree t;
11721 bool fixed_underlying_type_p
11722 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
11724 /* We built up the VALUES in reverse order. */
11725 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
11727 /* For an enum defined in a template, just set the type of the values;
11728 all further processing is postponed until the template is
11729 instantiated. We need to set the type so that tsubst of a CONST_DECL
11730 works. */
11731 if (processing_template_decl)
11733 for (values = TYPE_VALUES (enumtype);
11734 values;
11735 values = TREE_CHAIN (values))
11736 TREE_TYPE (TREE_VALUE (values)) = enumtype;
11737 return;
11740 /* Determine the minimum and maximum values of the enumerators. */
11741 if (TYPE_VALUES (enumtype))
11743 minnode = maxnode = NULL_TREE;
11745 for (values = TYPE_VALUES (enumtype);
11746 values;
11747 values = TREE_CHAIN (values))
11749 decl = TREE_VALUE (values);
11751 /* [dcl.enum]: Following the closing brace of an enum-specifier,
11752 each enumerator has the type of its enumeration. Prior to the
11753 closing brace, the type of each enumerator is the type of its
11754 initializing value. */
11755 TREE_TYPE (decl) = enumtype;
11757 /* Update the minimum and maximum values, if appropriate. */
11758 value = DECL_INITIAL (decl);
11759 if (value == error_mark_node)
11760 value = integer_zero_node;
11761 /* Figure out what the minimum and maximum values of the
11762 enumerators are. */
11763 if (!minnode)
11764 minnode = maxnode = value;
11765 else if (tree_int_cst_lt (maxnode, value))
11766 maxnode = value;
11767 else if (tree_int_cst_lt (value, minnode))
11768 minnode = value;
11771 else
11772 /* [dcl.enum]
11774 If the enumerator-list is empty, the underlying type is as if
11775 the enumeration had a single enumerator with value 0. */
11776 minnode = maxnode = integer_zero_node;
11778 if (!fixed_underlying_type_p)
11780 /* Compute the number of bits require to represent all values of the
11781 enumeration. We must do this before the type of MINNODE and
11782 MAXNODE are transformed, since tree_int_cst_min_precision relies
11783 on the TREE_TYPE of the value it is passed. */
11784 bool unsignedp = tree_int_cst_sgn (minnode) >= 0;
11785 int lowprec = tree_int_cst_min_precision (minnode, unsignedp);
11786 int highprec = tree_int_cst_min_precision (maxnode, unsignedp);
11787 int precision = MAX (lowprec, highprec);
11788 unsigned int itk;
11789 bool use_short_enum;
11791 /* Determine the underlying type of the enumeration.
11793 [dcl.enum]
11795 The underlying type of an enumeration is an integral type that
11796 can represent all the enumerator values defined in the
11797 enumeration. It is implementation-defined which integral type is
11798 used as the underlying type for an enumeration except that the
11799 underlying type shall not be larger than int unless the value of
11800 an enumerator cannot fit in an int or unsigned int.
11802 We use "int" or an "unsigned int" as the underlying type, even if
11803 a smaller integral type would work, unless the user has
11804 explicitly requested that we use the smallest possible type. The
11805 user can request that for all enumerations with a command line
11806 flag, or for just one enumeration with an attribute. */
11808 use_short_enum = flag_short_enums
11809 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
11811 for (itk = (use_short_enum ? itk_char : itk_int);
11812 itk != itk_none;
11813 itk++)
11815 underlying_type = integer_types[itk];
11816 if (underlying_type != NULL_TREE
11817 && TYPE_PRECISION (underlying_type) >= precision
11818 && TYPE_UNSIGNED (underlying_type) == unsignedp)
11819 break;
11821 if (itk == itk_none)
11823 /* DR 377
11825 IF no integral type can represent all the enumerator values, the
11826 enumeration is ill-formed. */
11827 error ("no integral type can represent all of the enumerator values "
11828 "for %qT", enumtype);
11829 precision = TYPE_PRECISION (long_long_integer_type_node);
11830 underlying_type = integer_types[itk_unsigned_long_long];
11833 /* [dcl.enum]
11835 The value of sizeof() applied to an enumeration type, an object
11836 of an enumeration type, or an enumerator, is the value of sizeof()
11837 applied to the underlying type. */
11838 copy_type_enum (enumtype, underlying_type);
11840 /* Compute the minimum and maximum values for the type.
11842 [dcl.enum]
11844 For an enumeration where emin is the smallest enumerator and emax
11845 is the largest, the values of the enumeration are the values of the
11846 underlying type in the range bmin to bmax, where bmin and bmax are,
11847 respectively, the smallest and largest values of the smallest bit-
11848 field that can store emin and emax. */
11850 /* The middle-end currently assumes that types with TYPE_PRECISION
11851 narrower than their underlying type are suitably zero or sign
11852 extended to fill their mode. Similarly, it assumes that the front
11853 end assures that a value of a particular type must be within
11854 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
11856 We used to set these fields based on bmin and bmax, but that led
11857 to invalid assumptions like optimizing away bounds checking. So
11858 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
11859 TYPE_MAX_VALUE to the values for the mode above and only restrict
11860 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
11861 ENUM_UNDERLYING_TYPE (enumtype)
11862 = build_distinct_type_copy (underlying_type);
11863 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
11864 set_min_and_max_values_for_integral_type
11865 (ENUM_UNDERLYING_TYPE (enumtype), precision, unsignedp);
11867 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
11868 if (flag_strict_enums)
11869 set_min_and_max_values_for_integral_type (enumtype, precision,
11870 unsignedp);
11872 else
11873 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
11875 /* Convert each of the enumerators to the type of the underlying
11876 type of the enumeration. */
11877 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
11879 location_t saved_location;
11881 decl = TREE_VALUE (values);
11882 saved_location = input_location;
11883 input_location = DECL_SOURCE_LOCATION (decl);
11884 if (fixed_underlying_type_p)
11885 /* If the enumeration type has a fixed underlying type, we
11886 already checked all of the enumerator values. */
11887 value = DECL_INITIAL (decl);
11888 else
11889 value = perform_implicit_conversion (underlying_type,
11890 DECL_INITIAL (decl),
11891 tf_warning_or_error);
11892 input_location = saved_location;
11894 /* Do not clobber shared ints. */
11895 value = copy_node (value);
11897 TREE_TYPE (value) = enumtype;
11898 DECL_INITIAL (decl) = value;
11901 /* Fix up all variant types of this enum type. */
11902 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
11903 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
11905 /* Finish debugging output for this type. */
11906 rest_of_type_compilation (enumtype, namespace_bindings_p ());
11909 /* Finishes the enum type. This is called only the first time an
11910 enumeration is seen, be it opaque or odinary.
11911 ENUMTYPE is the type object. */
11913 void
11914 finish_enum (tree enumtype)
11916 if (processing_template_decl)
11918 if (at_function_scope_p ())
11919 add_stmt (build_min (TAG_DEFN, enumtype));
11920 return;
11923 /* Here there should not be any variants of this type. */
11924 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
11925 && !TYPE_NEXT_VARIANT (enumtype));
11928 /* Build and install a CONST_DECL for an enumeration constant of the
11929 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
11930 LOC is the location of NAME.
11931 Assignment of sequential values by default is handled here. */
11933 void
11934 build_enumerator (tree name, tree value, tree enumtype, location_t loc)
11936 tree decl;
11937 tree context;
11938 tree type;
11940 /* If the VALUE was erroneous, pretend it wasn't there; that will
11941 result in the enum being assigned the next value in sequence. */
11942 if (value == error_mark_node)
11943 value = NULL_TREE;
11945 /* Remove no-op casts from the value. */
11946 if (value)
11947 STRIP_TYPE_NOPS (value);
11949 if (! processing_template_decl)
11951 /* Validate and default VALUE. */
11952 if (value != NULL_TREE)
11954 value = cxx_constant_value (value);
11956 if (TREE_CODE (value) == INTEGER_CST
11957 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
11959 value = perform_integral_promotions (value);
11961 else
11963 error ("enumerator value for %qD is not an integer constant", name);
11964 value = NULL_TREE;
11968 /* Default based on previous value. */
11969 if (value == NULL_TREE)
11971 if (TYPE_VALUES (enumtype))
11973 HOST_WIDE_INT hi;
11974 unsigned HOST_WIDE_INT lo;
11975 tree prev_value;
11976 bool overflowed;
11978 /* The next value is the previous value plus one.
11979 add_double doesn't know the type of the target expression,
11980 so we must check with int_fits_type_p as well. */
11981 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
11982 if (error_operand_p (prev_value))
11983 value = error_mark_node;
11984 else
11986 overflowed = add_double (TREE_INT_CST_LOW (prev_value),
11987 TREE_INT_CST_HIGH (prev_value),
11988 1, 0, &lo, &hi);
11989 value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi);
11990 overflowed
11991 |= !int_fits_type_p (value, TREE_TYPE (prev_value));
11993 if (overflowed)
11995 error ("overflow in enumeration values at %qD", name);
11996 value = error_mark_node;
12000 else
12001 value = integer_zero_node;
12004 /* Remove no-op casts from the value. */
12005 STRIP_TYPE_NOPS (value);
12007 /* If the underlying type of the enum is fixed, check whether
12008 the enumerator values fits in the underlying type. If it
12009 does not fit, the program is ill-formed [C++0x dcl.enum]. */
12010 if (ENUM_UNDERLYING_TYPE (enumtype)
12011 && value
12012 && TREE_CODE (value) == INTEGER_CST
12013 && !int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
12015 error ("enumerator value %E is too large for underlying type %<%T%>",
12016 value, ENUM_UNDERLYING_TYPE (enumtype));
12018 /* Silently convert the value so that we can continue. */
12019 value = perform_implicit_conversion (ENUM_UNDERLYING_TYPE (enumtype),
12020 value, tf_none);
12024 /* C++ associates enums with global, function, or class declarations. */
12025 context = current_scope ();
12027 /* Build the actual enumeration constant. Note that the enumeration
12028 constants have the underlying type of the enum (if it is fixed)
12029 or the type of their initializer (if the underlying type of the
12030 enum is not fixed):
12032 [ C++0x dcl.enum ]
12034 If the underlying type is fixed, the type of each enumerator
12035 prior to the closing brace is the underlying type; if the
12036 initializing value of an enumerator cannot be represented by
12037 the underlying type, the program is ill-formed. If the
12038 underlying type is not fixed, the type of each enumerator is
12039 the type of its initializing value.
12041 If the underlying type is not fixed, it will be computed by
12042 finish_enum and we will reset the type of this enumerator. Of
12043 course, if we're processing a template, there may be no value. */
12044 type = value ? TREE_TYPE (value) : NULL_TREE;
12046 if (context && context == current_class_type)
12047 /* This enum declaration is local to the class. We need the full
12048 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
12049 decl = build_lang_decl_loc (loc, CONST_DECL, name, type);
12050 else
12051 /* It's a global enum, or it's local to a function. (Note local to
12052 a function could mean local to a class method. */
12053 decl = build_decl (loc, CONST_DECL, name, type);
12055 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
12056 TREE_CONSTANT (decl) = 1;
12057 TREE_READONLY (decl) = 1;
12058 DECL_INITIAL (decl) = value;
12060 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
12061 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
12062 on the TYPE_FIELDS list for `S'. (That's so that you can say
12063 things like `S::i' later.) */
12064 finish_member_declaration (decl);
12065 else
12066 pushdecl (decl);
12068 /* Add this enumeration constant to the list for this type. */
12069 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
12072 /* Look for an enumerator with the given NAME within the enumeration
12073 type ENUMTYPE. This routine is used primarily for qualified name
12074 lookup into an enumerator in C++0x, e.g.,
12076 enum class Color { Red, Green, Blue };
12078 Color color = Color::Red;
12080 Returns the value corresponding to the enumerator, or
12081 NULL_TREE if no such enumerator was found. */
12082 tree
12083 lookup_enumerator (tree enumtype, tree name)
12085 tree e;
12086 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
12088 e = purpose_member (name, TYPE_VALUES (enumtype));
12089 return e? TREE_VALUE (e) : NULL_TREE;
12093 /* We're defining DECL. Make sure that its type is OK. */
12095 static void
12096 check_function_type (tree decl, tree current_function_parms)
12098 tree fntype = TREE_TYPE (decl);
12099 tree return_type = complete_type (TREE_TYPE (fntype));
12101 /* In a function definition, arg types must be complete. */
12102 require_complete_types_for_parms (current_function_parms);
12104 if (dependent_type_p (return_type))
12105 return;
12106 if (!COMPLETE_OR_VOID_TYPE_P (return_type)
12107 || (TYPE_FOR_JAVA (return_type) && MAYBE_CLASS_TYPE_P (return_type)))
12109 tree args = TYPE_ARG_TYPES (fntype);
12111 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
12112 error ("return type %q#T is incomplete", return_type);
12113 else
12114 error ("return type has Java class type %q#T", return_type);
12116 /* Make it return void instead. */
12117 if (TREE_CODE (fntype) == METHOD_TYPE)
12118 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
12119 void_type_node,
12120 TREE_CHAIN (args));
12121 else
12122 fntype = build_function_type (void_type_node, args);
12123 fntype
12124 = build_exception_variant (fntype,
12125 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
12126 fntype = (cp_build_type_attribute_variant
12127 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
12128 TREE_TYPE (decl) = fntype;
12130 else
12131 abstract_virtuals_error (decl, TREE_TYPE (fntype));
12134 /* Create the FUNCTION_DECL for a function definition.
12135 DECLSPECS and DECLARATOR are the parts of the declaration;
12136 they describe the function's name and the type it returns,
12137 but twisted together in a fashion that parallels the syntax of C.
12139 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
12140 DECLARATOR is really the DECL for the function we are about to
12141 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
12142 indicating that the function is an inline defined in-class.
12144 This function creates a binding context for the function body
12145 as well as setting up the FUNCTION_DECL in current_function_decl.
12147 For C++, we must first check whether that datum makes any sense.
12148 For example, "class A local_a(1,2);" means that variable local_a
12149 is an aggregate of type A, which should have a constructor
12150 applied to it with the argument list [1, 2].
12152 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
12153 or may be a BLOCK if the function has been defined previously
12154 in this translation unit. On exit, DECL_INITIAL (decl1) will be
12155 error_mark_node if the function has never been defined, or
12156 a BLOCK if the function has been defined somewhere. */
12158 void
12159 start_preparsed_function (tree decl1, tree attrs, int flags)
12161 tree ctype = NULL_TREE;
12162 tree fntype;
12163 tree restype;
12164 int doing_friend = 0;
12165 struct cp_binding_level *bl;
12166 tree current_function_parms;
12167 struct c_fileinfo *finfo
12168 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
12169 bool honor_interface;
12171 /* Sanity check. */
12172 gcc_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE);
12173 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
12175 fntype = TREE_TYPE (decl1);
12176 if (TREE_CODE (fntype) == METHOD_TYPE)
12177 ctype = TYPE_METHOD_BASETYPE (fntype);
12179 /* ISO C++ 11.4/5. A friend function defined in a class is in
12180 the (lexical) scope of the class in which it is defined. */
12181 if (!ctype && DECL_FRIEND_P (decl1))
12183 ctype = DECL_FRIEND_CONTEXT (decl1);
12185 /* CTYPE could be null here if we're dealing with a template;
12186 for example, `inline friend float foo()' inside a template
12187 will have no CTYPE set. */
12188 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
12189 ctype = NULL_TREE;
12190 else
12191 doing_friend = 1;
12194 if (DECL_DECLARED_INLINE_P (decl1)
12195 && lookup_attribute ("noinline", attrs))
12196 warning (0, "inline function %q+D given attribute noinline", decl1);
12198 /* Handle gnu_inline attribute. */
12199 if (GNU_INLINE_P (decl1))
12201 DECL_EXTERNAL (decl1) = 1;
12202 DECL_NOT_REALLY_EXTERN (decl1) = 0;
12203 DECL_INTERFACE_KNOWN (decl1) = 1;
12204 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
12207 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
12208 /* This is a constructor, we must ensure that any default args
12209 introduced by this definition are propagated to the clones
12210 now. The clones are used directly in overload resolution. */
12211 adjust_clone_args (decl1);
12213 /* Sometimes we don't notice that a function is a static member, and
12214 build a METHOD_TYPE for it. Fix that up now. */
12215 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
12216 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
12218 revert_static_member_fn (decl1);
12219 ctype = NULL_TREE;
12222 /* Set up current_class_type, and enter the scope of the class, if
12223 appropriate. */
12224 if (ctype)
12225 push_nested_class (ctype);
12226 else if (DECL_STATIC_FUNCTION_P (decl1))
12227 push_nested_class (DECL_CONTEXT (decl1));
12229 /* Now that we have entered the scope of the class, we must restore
12230 the bindings for any template parameters surrounding DECL1, if it
12231 is an inline member template. (Order is important; consider the
12232 case where a template parameter has the same name as a field of
12233 the class.) It is not until after this point that
12234 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
12235 if (flags & SF_INCLASS_INLINE)
12236 maybe_begin_member_template_processing (decl1);
12238 /* Effective C++ rule 15. */
12239 if (warn_ecpp
12240 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
12241 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
12242 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
12244 /* Make the init_value nonzero so pushdecl knows this is not tentative.
12245 error_mark_node is replaced below (in poplevel) with the BLOCK. */
12246 if (!DECL_INITIAL (decl1))
12247 DECL_INITIAL (decl1) = error_mark_node;
12249 /* This function exists in static storage.
12250 (This does not mean `static' in the C sense!) */
12251 TREE_STATIC (decl1) = 1;
12253 /* We must call push_template_decl after current_class_type is set
12254 up. (If we are processing inline definitions after exiting a
12255 class scope, current_class_type will be NULL_TREE until set above
12256 by push_nested_class.) */
12257 if (processing_template_decl)
12259 /* FIXME: Handle error_mark_node more gracefully. */
12260 tree newdecl1 = push_template_decl (decl1);
12261 if (newdecl1 != error_mark_node)
12262 decl1 = newdecl1;
12265 /* We are now in the scope of the function being defined. */
12266 current_function_decl = decl1;
12268 /* Save the parm names or decls from this function's declarator
12269 where store_parm_decls will find them. */
12270 current_function_parms = DECL_ARGUMENTS (decl1);
12272 /* Make sure the parameter and return types are reasonable. When
12273 you declare a function, these types can be incomplete, but they
12274 must be complete when you define the function. */
12275 check_function_type (decl1, current_function_parms);
12277 /* Build the return declaration for the function. */
12278 restype = TREE_TYPE (fntype);
12279 if (DECL_RESULT (decl1) == NULL_TREE)
12281 tree resdecl;
12283 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
12284 DECL_ARTIFICIAL (resdecl) = 1;
12285 DECL_IGNORED_P (resdecl) = 1;
12286 DECL_RESULT (decl1) = resdecl;
12288 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
12291 /* Let the user know we're compiling this function. */
12292 announce_function (decl1);
12294 /* Record the decl so that the function name is defined.
12295 If we already have a decl for this name, and it is a FUNCTION_DECL,
12296 use the old decl. */
12297 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
12299 /* A specialization is not used to guide overload resolution. */
12300 if (!DECL_FUNCTION_MEMBER_P (decl1)
12301 && !(DECL_USE_TEMPLATE (decl1) &&
12302 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
12304 tree olddecl = pushdecl (decl1);
12306 if (olddecl == error_mark_node)
12307 /* If something went wrong when registering the declaration,
12308 use DECL1; we have to have a FUNCTION_DECL to use when
12309 parsing the body of the function. */
12311 else
12313 /* Otherwise, OLDDECL is either a previous declaration
12314 of the same function or DECL1 itself. */
12316 if (warn_missing_declarations
12317 && olddecl == decl1
12318 && !DECL_MAIN_P (decl1)
12319 && TREE_PUBLIC (decl1)
12320 && !DECL_DECLARED_INLINE_P (decl1))
12322 tree context;
12324 /* Check whether DECL1 is in an anonymous
12325 namespace. */
12326 for (context = DECL_CONTEXT (decl1);
12327 context;
12328 context = DECL_CONTEXT (context))
12330 if (TREE_CODE (context) == NAMESPACE_DECL
12331 && DECL_NAME (context) == NULL_TREE)
12332 break;
12335 if (context == NULL)
12336 warning (OPT_Wmissing_declarations,
12337 "no previous declaration for %q+D", decl1);
12340 decl1 = olddecl;
12343 else
12345 /* We need to set the DECL_CONTEXT. */
12346 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
12347 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
12349 fntype = TREE_TYPE (decl1);
12351 /* If #pragma weak applies, mark the decl appropriately now.
12352 The pragma only applies to global functions. Because
12353 determining whether or not the #pragma applies involves
12354 computing the mangled name for the declaration, we cannot
12355 apply the pragma until after we have merged this declaration
12356 with any previous declarations; if the original declaration
12357 has a linkage specification, that specification applies to
12358 the definition as well, and may affect the mangled name. */
12359 if (DECL_FILE_SCOPE_P (decl1))
12360 maybe_apply_pragma_weak (decl1);
12363 /* constexpr functions must have literal argument types and
12364 literal return type. */
12365 validate_constexpr_fundecl (decl1);
12367 /* Reset this in case the call to pushdecl changed it. */
12368 current_function_decl = decl1;
12370 gcc_assert (DECL_INITIAL (decl1));
12372 /* This function may already have been parsed, in which case just
12373 return; our caller will skip over the body without parsing. */
12374 if (DECL_INITIAL (decl1) != error_mark_node)
12375 return;
12377 /* Initialize RTL machinery. We cannot do this until
12378 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
12379 even when processing a template; this is how we get
12380 CFUN set up, and our per-function variables initialized.
12381 FIXME factor out the non-RTL stuff. */
12382 bl = current_binding_level;
12383 allocate_struct_function (decl1, processing_template_decl);
12385 /* Initialize the language data structures. Whenever we start
12386 a new function, we destroy temporaries in the usual way. */
12387 cfun->language = ggc_alloc_cleared_language_function ();
12388 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
12389 current_binding_level = bl;
12391 /* Even though we're inside a function body, we still don't want to
12392 call expand_expr to calculate the size of a variable-sized array.
12393 We haven't necessarily assigned RTL to all variables yet, so it's
12394 not safe to try to expand expressions involving them. */
12395 cfun->dont_save_pending_sizes_p = 1;
12397 /* Start the statement-tree, start the tree now. */
12398 DECL_SAVED_TREE (decl1) = push_stmt_list ();
12400 /* If we are (erroneously) defining a function that we have already
12401 defined before, wipe out what we knew before. */
12402 if (!DECL_PENDING_INLINE_P (decl1))
12403 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
12405 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
12407 /* We know that this was set up by `grokclassfn'. We do not
12408 wait until `store_parm_decls', since evil parse errors may
12409 never get us to that point. Here we keep the consistency
12410 between `current_class_type' and `current_class_ptr'. */
12411 tree t = DECL_ARGUMENTS (decl1);
12413 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
12414 gcc_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE);
12416 cp_function_chain->x_current_class_ref
12417 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
12418 cp_function_chain->x_current_class_ptr = t;
12420 /* Constructors and destructors need to know whether they're "in
12421 charge" of initializing virtual base classes. */
12422 t = DECL_CHAIN (t);
12423 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
12425 current_in_charge_parm = t;
12426 t = DECL_CHAIN (t);
12428 if (DECL_HAS_VTT_PARM_P (decl1))
12430 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
12431 current_vtt_parm = t;
12435 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
12436 /* Implicitly-defined methods (like the
12437 destructor for a class in which no destructor
12438 is explicitly declared) must not be defined
12439 until their definition is needed. So, we
12440 ignore interface specifications for
12441 compiler-generated functions. */
12442 && !DECL_ARTIFICIAL (decl1));
12444 if (DECL_INTERFACE_KNOWN (decl1))
12446 tree ctx = decl_function_context (decl1);
12448 if (DECL_NOT_REALLY_EXTERN (decl1))
12449 DECL_EXTERNAL (decl1) = 0;
12451 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
12452 && TREE_PUBLIC (ctx))
12453 /* This is a function in a local class in an extern inline
12454 function. */
12455 comdat_linkage (decl1);
12457 /* If this function belongs to an interface, it is public.
12458 If it belongs to someone else's interface, it is also external.
12459 This only affects inlines and template instantiations. */
12460 else if (!finfo->interface_unknown && honor_interface)
12462 if (DECL_DECLARED_INLINE_P (decl1)
12463 || DECL_TEMPLATE_INSTANTIATION (decl1)
12464 || processing_template_decl)
12466 DECL_EXTERNAL (decl1)
12467 = (finfo->interface_only
12468 || (DECL_DECLARED_INLINE_P (decl1)
12469 && ! flag_implement_inlines
12470 && !DECL_VINDEX (decl1)));
12472 /* For WIN32 we also want to put these in linkonce sections. */
12473 maybe_make_one_only (decl1);
12475 else
12476 DECL_EXTERNAL (decl1) = 0;
12477 DECL_INTERFACE_KNOWN (decl1) = 1;
12478 /* If this function is in an interface implemented in this file,
12479 make sure that the back end knows to emit this function
12480 here. */
12481 if (!DECL_EXTERNAL (decl1))
12482 mark_needed (decl1);
12484 else if (finfo->interface_unknown && finfo->interface_only
12485 && honor_interface)
12487 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
12488 interface, we will have both finfo->interface_unknown and
12489 finfo->interface_only set. In that case, we don't want to
12490 use the normal heuristics because someone will supply a
12491 #pragma implementation elsewhere, and deducing it here would
12492 produce a conflict. */
12493 comdat_linkage (decl1);
12494 DECL_EXTERNAL (decl1) = 0;
12495 DECL_INTERFACE_KNOWN (decl1) = 1;
12496 DECL_DEFER_OUTPUT (decl1) = 1;
12498 else
12500 /* This is a definition, not a reference.
12501 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
12502 if (!GNU_INLINE_P (decl1))
12503 DECL_EXTERNAL (decl1) = 0;
12505 if ((DECL_DECLARED_INLINE_P (decl1)
12506 || DECL_TEMPLATE_INSTANTIATION (decl1))
12507 && ! DECL_INTERFACE_KNOWN (decl1))
12508 DECL_DEFER_OUTPUT (decl1) = 1;
12509 else
12510 DECL_INTERFACE_KNOWN (decl1) = 1;
12513 /* Determine the ELF visibility attribute for the function. We must not
12514 do this before calling "pushdecl", as we must allow "duplicate_decls"
12515 to merge any attributes appropriately. We also need to wait until
12516 linkage is set. */
12517 if (!DECL_CLONED_FUNCTION_P (decl1))
12518 determine_visibility (decl1);
12520 begin_scope (sk_function_parms, decl1);
12522 ++function_depth;
12524 if (DECL_DESTRUCTOR_P (decl1)
12525 || (DECL_CONSTRUCTOR_P (decl1)
12526 && targetm.cxx.cdtor_returns_this ()))
12528 cdtor_label = build_decl (input_location,
12529 LABEL_DECL, NULL_TREE, NULL_TREE);
12530 DECL_CONTEXT (cdtor_label) = current_function_decl;
12533 start_fname_decls ();
12535 store_parm_decls (current_function_parms);
12539 /* Like start_preparsed_function, except that instead of a
12540 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
12542 Returns 1 on success. If the DECLARATOR is not suitable for a function
12543 (it defines a datum instead), we return 0, which tells
12544 yyparse to report a parse error. */
12547 start_function (cp_decl_specifier_seq *declspecs,
12548 const cp_declarator *declarator,
12549 tree attrs)
12551 tree decl1;
12553 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
12554 if (decl1 == error_mark_node)
12555 return 0;
12556 /* If the declarator is not suitable for a function definition,
12557 cause a syntax error. */
12558 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
12560 error ("invalid function declaration");
12561 return 0;
12564 if (DECL_MAIN_P (decl1))
12565 /* main must return int. grokfndecl should have corrected it
12566 (and issued a diagnostic) if the user got it wrong. */
12567 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
12568 integer_type_node));
12570 start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
12572 return 1;
12575 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
12576 FN. */
12578 static bool
12579 use_eh_spec_block (tree fn)
12581 return (flag_exceptions && flag_enforce_eh_specs
12582 && !processing_template_decl
12583 && !type_throw_all_p (TREE_TYPE (fn))
12584 /* We insert the EH_SPEC_BLOCK only in the original
12585 function; then, it is copied automatically to the
12586 clones. */
12587 && !DECL_CLONED_FUNCTION_P (fn)
12588 /* Implicitly-generated constructors and destructors have
12589 exception specifications. However, those specifications
12590 are the union of the possible exceptions specified by the
12591 constructors/destructors for bases and members, so no
12592 unallowed exception will ever reach this function. By
12593 not creating the EH_SPEC_BLOCK we save a little memory,
12594 and we avoid spurious warnings about unreachable
12595 code. */
12596 && !DECL_ARTIFICIAL (fn));
12599 /* Store the parameter declarations into the current function declaration.
12600 This is called after parsing the parameter declarations, before
12601 digesting the body of the function.
12603 Also install to binding contour return value identifier, if any. */
12605 static void
12606 store_parm_decls (tree current_function_parms)
12608 tree fndecl = current_function_decl;
12609 tree parm;
12611 /* This is a chain of any other decls that came in among the parm
12612 declarations. If a parm is declared with enum {foo, bar} x;
12613 then CONST_DECLs for foo and bar are put here. */
12614 tree nonparms = NULL_TREE;
12616 if (current_function_parms)
12618 /* This case is when the function was defined with an ANSI prototype.
12619 The parms already have decls, so we need not do anything here
12620 except record them as in effect
12621 and complain if any redundant old-style parm decls were written. */
12623 tree specparms = current_function_parms;
12624 tree next;
12626 /* Must clear this because it might contain TYPE_DECLs declared
12627 at class level. */
12628 current_binding_level->names = NULL;
12630 /* If we're doing semantic analysis, then we'll call pushdecl
12631 for each of these. We must do them in reverse order so that
12632 they end in the correct forward order. */
12633 specparms = nreverse (specparms);
12635 for (parm = specparms; parm; parm = next)
12637 next = DECL_CHAIN (parm);
12638 if (TREE_CODE (parm) == PARM_DECL)
12640 if (DECL_NAME (parm) == NULL_TREE
12641 || TREE_CODE (parm) != VOID_TYPE)
12642 pushdecl (parm);
12643 else
12644 error ("parameter %qD declared void", parm);
12646 else
12648 /* If we find an enum constant or a type tag,
12649 put it aside for the moment. */
12650 TREE_CHAIN (parm) = NULL_TREE;
12651 nonparms = chainon (nonparms, parm);
12655 /* Get the decls in their original chain order and record in the
12656 function. This is all and only the PARM_DECLs that were
12657 pushed into scope by the loop above. */
12658 DECL_ARGUMENTS (fndecl) = getdecls ();
12660 else
12661 DECL_ARGUMENTS (fndecl) = NULL_TREE;
12663 /* Now store the final chain of decls for the arguments
12664 as the decl-chain of the current lexical scope.
12665 Put the enumerators in as well, at the front so that
12666 DECL_ARGUMENTS is not modified. */
12667 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
12669 if (use_eh_spec_block (current_function_decl))
12670 current_eh_spec_block = begin_eh_spec_block ();
12674 /* We have finished doing semantic analysis on DECL, but have not yet
12675 generated RTL for its body. Save away our current state, so that
12676 when we want to generate RTL later we know what to do. */
12678 static void
12679 save_function_data (tree decl)
12681 struct language_function *f;
12683 /* Save the language-specific per-function data so that we can
12684 get it back when we really expand this function. */
12685 gcc_assert (!DECL_PENDING_INLINE_P (decl));
12687 /* Make a copy. */
12688 f = ggc_alloc_language_function ();
12689 memcpy (f, cp_function_chain, sizeof (struct language_function));
12690 DECL_SAVED_FUNCTION_DATA (decl) = f;
12692 /* Clear out the bits we don't need. */
12693 f->base.x_stmt_tree.x_cur_stmt_list = NULL_TREE;
12694 f->bindings = NULL;
12695 f->x_local_names = NULL;
12699 /* Set the return value of the constructor (if present). */
12701 static void
12702 finish_constructor_body (void)
12704 tree val;
12705 tree exprstmt;
12707 if (targetm.cxx.cdtor_returns_this ()
12708 && (! TYPE_FOR_JAVA (current_class_type)))
12710 /* Any return from a constructor will end up here. */
12711 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
12713 val = DECL_ARGUMENTS (current_function_decl);
12714 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
12715 DECL_RESULT (current_function_decl), val);
12716 /* Return the address of the object. */
12717 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
12718 add_stmt (exprstmt);
12722 /* Do all the processing for the beginning of a destructor; set up the
12723 vtable pointers and cleanups for bases and members. */
12725 static void
12726 begin_destructor_body (void)
12728 tree compound_stmt;
12730 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
12731 issued an error message. We still want to try to process the
12732 body of the function, but initialize_vtbl_ptrs will crash if
12733 TYPE_BINFO is NULL. */
12734 if (COMPLETE_TYPE_P (current_class_type))
12736 compound_stmt = begin_compound_stmt (0);
12737 /* Make all virtual function table pointers in non-virtual base
12738 classes point to CURRENT_CLASS_TYPE's virtual function
12739 tables. */
12740 initialize_vtbl_ptrs (current_class_ptr);
12741 finish_compound_stmt (compound_stmt);
12743 /* And insert cleanups for our bases and members so that they
12744 will be properly destroyed if we throw. */
12745 push_base_cleanups ();
12749 /* At the end of every destructor we generate code to delete the object if
12750 necessary. Do that now. */
12752 static void
12753 finish_destructor_body (void)
12755 tree exprstmt;
12757 /* Any return from a destructor will end up here; that way all base
12758 and member cleanups will be run when the function returns. */
12759 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
12761 /* In a virtual destructor, we must call delete. */
12762 if (DECL_VIRTUAL_P (current_function_decl))
12764 tree if_stmt;
12765 tree virtual_size = cxx_sizeof (current_class_type);
12767 /* [class.dtor]
12769 At the point of definition of a virtual destructor (including
12770 an implicit definition), non-placement operator delete shall
12771 be looked up in the scope of the destructor's class and if
12772 found shall be accessible and unambiguous. */
12773 exprstmt = build_op_delete_call(DELETE_EXPR, current_class_ptr,
12774 virtual_size,
12775 /*global_p=*/false,
12776 /*placement=*/NULL_TREE,
12777 /*alloc_fn=*/NULL_TREE);
12779 if_stmt = begin_if_stmt ();
12780 finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
12781 current_in_charge_parm,
12782 integer_one_node),
12783 if_stmt);
12784 finish_expr_stmt (exprstmt);
12785 finish_then_clause (if_stmt);
12786 finish_if_stmt (if_stmt);
12789 if (targetm.cxx.cdtor_returns_this ())
12791 tree val;
12793 val = DECL_ARGUMENTS (current_function_decl);
12794 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
12795 DECL_RESULT (current_function_decl), val);
12796 /* Return the address of the object. */
12797 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
12798 add_stmt (exprstmt);
12802 /* Do the necessary processing for the beginning of a function body, which
12803 in this case includes member-initializers, but not the catch clauses of
12804 a function-try-block. Currently, this means opening a binding level
12805 for the member-initializers (in a ctor) and member cleanups (in a dtor). */
12807 tree
12808 begin_function_body (void)
12810 tree stmt;
12812 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
12813 return NULL_TREE;
12815 if (processing_template_decl)
12816 /* Do nothing now. */;
12817 else
12818 /* Always keep the BLOCK node associated with the outermost pair of
12819 curly braces of a function. These are needed for correct
12820 operation of dwarfout.c. */
12821 keep_next_level (true);
12823 stmt = begin_compound_stmt (BCS_FN_BODY);
12825 if (processing_template_decl)
12826 /* Do nothing now. */;
12827 else if (DECL_DESTRUCTOR_P (current_function_decl))
12828 begin_destructor_body ();
12830 return stmt;
12833 /* Do the processing for the end of a function body. Currently, this means
12834 closing out the cleanups for fully-constructed bases and members, and in
12835 the case of the destructor, deleting the object if desired. Again, this
12836 is only meaningful for [cd]tors, since they are the only functions where
12837 there is a significant distinction between the main body and any
12838 function catch clauses. Handling, say, main() return semantics here
12839 would be wrong, as flowing off the end of a function catch clause for
12840 main() would also need to return 0. */
12842 void
12843 finish_function_body (tree compstmt)
12845 if (compstmt == NULL_TREE)
12846 return;
12848 /* Close the block. */
12849 finish_compound_stmt (compstmt);
12851 if (processing_template_decl)
12852 /* Do nothing now. */;
12853 else if (DECL_CONSTRUCTOR_P (current_function_decl))
12854 finish_constructor_body ();
12855 else if (DECL_DESTRUCTOR_P (current_function_decl))
12856 finish_destructor_body ();
12859 /* Given a function, returns the BLOCK corresponding to the outermost level
12860 of curly braces, skipping the artificial block created for constructor
12861 initializers. */
12863 tree
12864 outer_curly_brace_block (tree fndecl)
12866 tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl));
12867 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
12868 /* Skip the artificial function body block. */
12869 block = BLOCK_SUBBLOCKS (block);
12870 return block;
12873 /* If FNDECL is a class's key method, add the class to the list of
12874 keyed classes that should be emitted. */
12876 static void
12877 record_key_method_defined (tree fndecl)
12879 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
12880 && DECL_VIRTUAL_P (fndecl)
12881 && !processing_template_decl)
12883 tree fnclass = DECL_CONTEXT (fndecl);
12884 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
12885 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
12889 /* Subroutine of finish_function.
12890 Save the body of constexpr functions for possible
12891 future compile time evaluation. */
12893 static void
12894 maybe_save_function_definition (tree fun)
12896 if (!processing_template_decl
12897 && DECL_DECLARED_CONSTEXPR_P (fun)
12898 && !DECL_CLONED_FUNCTION_P (fun))
12899 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
12902 /* Finish up a function declaration and compile that function
12903 all the way to assembler language output. The free the storage
12904 for the function definition.
12906 FLAGS is a bitwise or of the following values:
12907 2 - INCLASS_INLINE
12908 We just finished processing the body of an in-class inline
12909 function definition. (This processing will have taken place
12910 after the class definition is complete.) */
12912 tree
12913 finish_function (int flags)
12915 tree fndecl = current_function_decl;
12916 tree fntype, ctype = NULL_TREE;
12917 int inclass_inline = (flags & 2) != 0;
12919 /* When we get some parse errors, we can end up without a
12920 current_function_decl, so cope. */
12921 if (fndecl == NULL_TREE)
12922 return error_mark_node;
12924 if (c_dialect_objc ())
12925 objc_finish_function ();
12927 gcc_assert (!defer_mark_used_calls);
12928 defer_mark_used_calls = true;
12930 record_key_method_defined (fndecl);
12932 fntype = TREE_TYPE (fndecl);
12934 /* TREE_READONLY (fndecl) = 1;
12935 This caused &foo to be of type ptr-to-const-function
12936 which then got a warning when stored in a ptr-to-function variable. */
12938 gcc_assert (building_stmt_tree ());
12939 /* The current function is being defined, so its DECL_INITIAL should
12940 be set, and unless there's a multiple definition, it should be
12941 error_mark_node. */
12942 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
12944 /* For a cloned function, we've already got all the code we need;
12945 there's no need to add any extra bits. */
12946 if (!DECL_CLONED_FUNCTION_P (fndecl))
12948 if (DECL_MAIN_P (current_function_decl))
12950 tree stmt;
12952 /* Make it so that `main' always returns 0 by default (or
12953 1 for VMS). */
12954 #if VMS_TARGET
12955 stmt = finish_return_stmt (integer_one_node);
12956 #else
12957 stmt = finish_return_stmt (integer_zero_node);
12958 #endif
12959 /* Hack. We don't want the middle-end to warn that this
12960 return is unreachable, so put the statement on the
12961 special line 0. */
12963 location_t linezero = linemap_line_start (line_table, 0, 1);
12964 SET_EXPR_LOCATION (stmt, linezero);
12968 if (use_eh_spec_block (current_function_decl))
12969 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
12970 (TREE_TYPE (current_function_decl)),
12971 current_eh_spec_block);
12974 /* If we're saving up tree structure, tie off the function now. */
12975 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
12977 finish_fname_decls ();
12979 /* If this function can't throw any exceptions, remember that. */
12980 if (!processing_template_decl
12981 && !cp_function_chain->can_throw
12982 && !flag_non_call_exceptions
12983 && !decl_replaceable_p (fndecl))
12984 TREE_NOTHROW (fndecl) = 1;
12986 /* This must come after expand_function_end because cleanups might
12987 have declarations (from inline functions) that need to go into
12988 this function's blocks. */
12990 /* If the current binding level isn't the outermost binding level
12991 for this function, either there is a bug, or we have experienced
12992 syntax errors and the statement tree is malformed. */
12993 if (current_binding_level->kind != sk_function_parms)
12995 /* Make sure we have already experienced errors. */
12996 gcc_assert (errorcount);
12998 /* Throw away the broken statement tree and extra binding
12999 levels. */
13000 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
13002 while (current_binding_level->kind != sk_function_parms)
13004 if (current_binding_level->kind == sk_class)
13005 pop_nested_class ();
13006 else
13007 poplevel (0, 0, 0);
13010 poplevel (1, 0, 1);
13012 /* Statements should always be full-expressions at the outermost set
13013 of curly braces for a function. */
13014 gcc_assert (stmts_are_full_exprs_p ());
13016 /* Save constexpr function body before it gets munged by
13017 the NRV transformation. */
13018 maybe_save_function_definition (fndecl);
13020 /* Set up the named return value optimization, if we can. Candidate
13021 variables are selected in check_return_expr. */
13022 if (current_function_return_value)
13024 tree r = current_function_return_value;
13025 tree outer;
13027 if (r != error_mark_node
13028 /* This is only worth doing for fns that return in memory--and
13029 simpler, since we don't have to worry about promoted modes. */
13030 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
13031 /* Only allow this for variables declared in the outer scope of
13032 the function so we know that their lifetime always ends with a
13033 return; see g++.dg/opt/nrv6.C. We could be more flexible if
13034 we were to do this optimization in tree-ssa. */
13035 && (outer = outer_curly_brace_block (fndecl))
13036 && chain_member (r, BLOCK_VARS (outer)))
13037 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
13039 current_function_return_value = NULL_TREE;
13042 /* Remember that we were in class scope. */
13043 if (current_class_name)
13044 ctype = current_class_type;
13046 /* Must mark the RESULT_DECL as being in this function. */
13047 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
13049 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
13050 to the FUNCTION_DECL node itself. */
13051 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
13053 /* Save away current state, if appropriate. */
13054 if (!processing_template_decl)
13055 save_function_data (fndecl);
13057 /* Complain if there's just no return statement. */
13058 if (warn_return_type
13059 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
13060 && !dependent_type_p (TREE_TYPE (fntype))
13061 && !current_function_returns_value && !current_function_returns_null
13062 /* Don't complain if we abort or throw. */
13063 && !current_function_returns_abnormally
13064 /* Don't complain if we are declared noreturn. */
13065 && !TREE_THIS_VOLATILE (fndecl)
13066 && !DECL_NAME (DECL_RESULT (fndecl))
13067 && !TREE_NO_WARNING (fndecl)
13068 /* Structor return values (if any) are set by the compiler. */
13069 && !DECL_CONSTRUCTOR_P (fndecl)
13070 && !DECL_DESTRUCTOR_P (fndecl))
13072 warning (OPT_Wreturn_type,
13073 "no return statement in function returning non-void");
13074 TREE_NO_WARNING (fndecl) = 1;
13077 /* Store the end of the function, so that we get good line number
13078 info for the epilogue. */
13079 cfun->function_end_locus = input_location;
13081 /* Complain about parameters that are only set, but never otherwise used. */
13082 if (warn_unused_but_set_parameter
13083 && !processing_template_decl
13084 && errorcount == unused_but_set_errorcount
13085 && !DECL_CLONED_FUNCTION_P (fndecl))
13087 tree decl;
13089 for (decl = DECL_ARGUMENTS (fndecl);
13090 decl;
13091 decl = DECL_CHAIN (decl))
13092 if (TREE_USED (decl)
13093 && TREE_CODE (decl) == PARM_DECL
13094 && !DECL_READ_P (decl)
13095 && DECL_NAME (decl)
13096 && !DECL_ARTIFICIAL (decl)
13097 && !TREE_NO_WARNING (decl)
13098 && !DECL_IN_SYSTEM_HEADER (decl)
13099 && TREE_TYPE (decl) != error_mark_node
13100 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13101 && (!CLASS_TYPE_P (TREE_TYPE (decl))
13102 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
13103 warning (OPT_Wunused_but_set_parameter,
13104 "parameter %q+D set but not used", decl);
13105 unused_but_set_errorcount = errorcount;
13108 /* Genericize before inlining. */
13109 if (!processing_template_decl)
13111 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
13112 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
13113 cp_genericize (fndecl);
13114 /* Clear out the bits we don't need. */
13115 f->x_current_class_ptr = NULL;
13116 f->x_current_class_ref = NULL;
13117 f->x_eh_spec_block = NULL;
13118 f->x_in_charge_parm = NULL;
13119 f->x_vtt_parm = NULL;
13120 f->x_return_value = NULL;
13121 f->bindings = NULL;
13122 f->extern_decl_map = NULL;
13124 /* Clear out the bits we don't need. */
13125 local_names = NULL;
13127 /* We're leaving the context of this function, so zap cfun. It's still in
13128 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
13129 set_cfun (NULL);
13130 current_function_decl = NULL;
13132 /* If this is an in-class inline definition, we may have to pop the
13133 bindings for the template parameters that we added in
13134 maybe_begin_member_template_processing when start_function was
13135 called. */
13136 if (inclass_inline)
13137 maybe_end_member_template_processing ();
13139 /* Leave the scope of the class. */
13140 if (ctype)
13141 pop_nested_class ();
13143 --function_depth;
13145 /* Clean up. */
13146 current_function_decl = NULL_TREE;
13148 defer_mark_used_calls = false;
13149 if (deferred_mark_used_calls)
13151 unsigned int i;
13152 tree decl;
13154 FOR_EACH_VEC_ELT (tree, deferred_mark_used_calls, i, decl)
13155 mark_used (decl);
13156 VEC_free (tree, gc, deferred_mark_used_calls);
13159 return fndecl;
13162 /* Create the FUNCTION_DECL for a function definition.
13163 DECLSPECS and DECLARATOR are the parts of the declaration;
13164 they describe the return type and the name of the function,
13165 but twisted together in a fashion that parallels the syntax of C.
13167 This function creates a binding context for the function body
13168 as well as setting up the FUNCTION_DECL in current_function_decl.
13170 Returns a FUNCTION_DECL on success.
13172 If the DECLARATOR is not suitable for a function (it defines a datum
13173 instead), we return 0, which tells yyparse to report a parse error.
13175 May return void_type_node indicating that this method is actually
13176 a friend. See grokfield for more details.
13178 Came here with a `.pushlevel' .
13180 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
13181 CHANGES TO CODE IN `grokfield'. */
13183 tree
13184 grokmethod (cp_decl_specifier_seq *declspecs,
13185 const cp_declarator *declarator, tree attrlist)
13187 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
13188 &attrlist);
13190 if (fndecl == error_mark_node)
13191 return error_mark_node;
13193 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
13195 error ("invalid member function declaration");
13196 return error_mark_node;
13199 if (attrlist)
13200 cplus_decl_attributes (&fndecl, attrlist, 0);
13202 /* Pass friends other than inline friend functions back. */
13203 if (fndecl == void_type_node)
13204 return fndecl;
13206 if (DECL_IN_AGGR_P (fndecl))
13208 if (DECL_CLASS_SCOPE_P (fndecl))
13209 error ("%qD is already defined in class %qT", fndecl,
13210 DECL_CONTEXT (fndecl));
13211 return error_mark_node;
13214 check_template_shadow (fndecl);
13216 DECL_DECLARED_INLINE_P (fndecl) = 1;
13217 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
13219 /* We process method specializations in finish_struct_1. */
13220 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
13222 fndecl = push_template_decl (fndecl);
13223 if (fndecl == error_mark_node)
13224 return fndecl;
13227 if (! DECL_FRIEND_P (fndecl))
13229 if (DECL_CHAIN (fndecl))
13231 fndecl = copy_node (fndecl);
13232 TREE_CHAIN (fndecl) = NULL_TREE;
13236 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
13238 DECL_IN_AGGR_P (fndecl) = 1;
13239 return fndecl;
13243 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
13244 we can lay it out later, when and if its type becomes complete. */
13246 void
13247 maybe_register_incomplete_var (tree var)
13249 gcc_assert (TREE_CODE (var) == VAR_DECL);
13251 /* Keep track of variables with incomplete types. */
13252 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
13253 && DECL_EXTERNAL (var))
13255 tree inner_type = TREE_TYPE (var);
13257 while (TREE_CODE (inner_type) == ARRAY_TYPE)
13258 inner_type = TREE_TYPE (inner_type);
13259 inner_type = TYPE_MAIN_VARIANT (inner_type);
13261 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
13262 /* RTTI TD entries are created while defining the type_info. */
13263 || (TYPE_LANG_SPECIFIC (inner_type)
13264 && TYPE_BEING_DEFINED (inner_type)))
13266 incomplete_var *iv
13267 = VEC_safe_push (incomplete_var, gc, incomplete_vars, NULL);
13268 iv->decl = var;
13269 iv->incomplete_type = inner_type;
13274 /* Called when a class type (given by TYPE) is defined. If there are
13275 any existing VAR_DECLs whose type has been completed by this
13276 declaration, update them now. */
13278 void
13279 complete_vars (tree type)
13281 unsigned ix;
13282 incomplete_var *iv;
13284 for (ix = 0; VEC_iterate (incomplete_var, incomplete_vars, ix, iv); )
13286 if (same_type_p (type, iv->incomplete_type))
13288 tree var = iv->decl;
13289 tree type = TREE_TYPE (var);
13290 /* Complete the type of the variable. The VAR_DECL itself
13291 will be laid out in expand_expr. */
13292 complete_type (type);
13293 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
13294 /* Remove this entry from the list. */
13295 VEC_unordered_remove (incomplete_var, incomplete_vars, ix);
13297 else
13298 ix++;
13301 /* Check for pending declarations which may have abstract type. */
13302 complete_type_check_abstract (type);
13305 /* If DECL is of a type which needs a cleanup, build and return an
13306 expression to perform that cleanup here. Return NULL_TREE if no
13307 cleanup need be done. */
13309 tree
13310 cxx_maybe_build_cleanup (tree decl)
13312 tree type;
13313 tree attr;
13314 tree cleanup;
13316 /* Assume no cleanup is required. */
13317 cleanup = NULL_TREE;
13319 if (error_operand_p (decl))
13320 return cleanup;
13322 /* Handle "__attribute__((cleanup))". We run the cleanup function
13323 before the destructor since the destructor is what actually
13324 terminates the lifetime of the object. */
13325 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
13326 if (attr)
13328 tree id;
13329 tree fn;
13330 tree arg;
13332 /* Get the name specified by the user for the cleanup function. */
13333 id = TREE_VALUE (TREE_VALUE (attr));
13334 /* Look up the name to find the cleanup function to call. It is
13335 important to use lookup_name here because that is what is
13336 used in c-common.c:handle_cleanup_attribute when performing
13337 initial checks on the attribute. Note that those checks
13338 include ensuring that the function found is not an overloaded
13339 function, or an object with an overloaded call operator,
13340 etc.; we can rely on the fact that the function found is an
13341 ordinary FUNCTION_DECL. */
13342 fn = lookup_name (id);
13343 arg = build_address (decl);
13344 mark_used (decl);
13345 cleanup = cp_build_function_call_nary (fn, tf_warning_or_error,
13346 arg, NULL_TREE);
13348 /* Handle ordinary C++ destructors. */
13349 type = TREE_TYPE (decl);
13350 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
13352 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
13353 bool has_vbases = (TREE_CODE (type) == RECORD_TYPE
13354 && CLASSTYPE_VBASECLASSES (type));
13355 tree addr;
13356 tree call;
13358 if (TREE_CODE (type) == ARRAY_TYPE)
13359 addr = decl;
13360 else
13361 addr = build_address (decl);
13363 /* Optimize for space over speed here. */
13364 if (!has_vbases || flag_expensive_optimizations)
13365 flags |= LOOKUP_NONVIRTUAL;
13367 call = build_delete (TREE_TYPE (addr), addr,
13368 sfk_complete_destructor, flags, 0);
13369 if (cleanup)
13370 cleanup = build_compound_expr (input_location, cleanup, call);
13371 else
13372 cleanup = call;
13375 return cleanup;
13378 /* When a stmt has been parsed, this function is called. */
13380 void
13381 finish_stmt (void)
13385 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
13386 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
13387 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
13389 tree
13390 static_fn_type (tree memfntype)
13392 tree fntype;
13393 tree args;
13395 if (TYPE_PTRMEMFUNC_P (memfntype))
13396 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
13397 if (POINTER_TYPE_P (memfntype)
13398 || TREE_CODE (memfntype) == FUNCTION_DECL)
13399 memfntype = TREE_TYPE (memfntype);
13400 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
13401 return memfntype;
13402 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
13403 args = TYPE_ARG_TYPES (memfntype);
13404 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
13405 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
13406 fntype = (cp_build_type_attribute_variant
13407 (fntype, TYPE_ATTRIBUTES (memfntype)));
13408 fntype = (build_exception_variant
13409 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
13410 return fntype;
13413 /* DECL was originally constructed as a non-static member function,
13414 but turned out to be static. Update it accordingly. */
13416 void
13417 revert_static_member_fn (tree decl)
13419 tree stype = static_fn_type (decl);
13421 if (type_memfn_quals (stype) != TYPE_UNQUALIFIED)
13423 error ("static member function %q#D declared with type qualifiers", decl);
13424 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED);
13426 TREE_TYPE (decl) = stype;
13428 if (DECL_ARGUMENTS (decl))
13429 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
13430 DECL_STATIC_FUNCTION_P (decl) = 1;
13433 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
13434 one of the language-independent trees. */
13436 enum cp_tree_node_structure_enum
13437 cp_tree_node_structure (union lang_tree_node * t)
13439 switch (TREE_CODE (&t->generic))
13441 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
13442 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
13443 case OVERLOAD: return TS_CP_OVERLOAD;
13444 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
13445 case PTRMEM_CST: return TS_CP_PTRMEM;
13446 case BASELINK: return TS_CP_BASELINK;
13447 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
13448 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
13449 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
13450 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
13451 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
13452 default: return TS_CP_GENERIC;
13456 /* Build the void_list_node (void_type_node having been created). */
13457 tree
13458 build_void_list_node (void)
13460 tree t = build_tree_list (NULL_TREE, void_type_node);
13461 return t;
13464 bool
13465 cp_missing_noreturn_ok_p (tree decl)
13467 /* A missing noreturn is ok for the `main' function. */
13468 return DECL_MAIN_P (decl);
13471 /* Return the COMDAT group into which DECL should be placed. */
13473 tree
13474 cxx_comdat_group (tree decl)
13476 tree name;
13478 /* Virtual tables, construction virtual tables, and virtual table
13479 tables all go in a single COMDAT group, named after the primary
13480 virtual table. */
13481 if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
13482 name = DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (DECL_CONTEXT (decl)));
13483 /* For all other DECLs, the COMDAT group is the mangled name of the
13484 declaration itself. */
13485 else
13487 while (DECL_THUNK_P (decl))
13489 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
13490 into the same section as the target function. In that case
13491 we must return target's name. */
13492 tree target = THUNK_TARGET (decl);
13493 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
13494 && DECL_SECTION_NAME (target) != NULL
13495 && DECL_ONE_ONLY (target))
13496 decl = target;
13497 else
13498 break;
13500 name = DECL_ASSEMBLER_NAME (decl);
13503 return name;
13506 #include "gt-cp-decl.h"