Merge from trunk rev 172662.
[official-gcc.git] / gcc / cp / decl.c
blob2b28f4cd3d2b32a64a07467e56913bbacaa44045
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 = block_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 = block_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 || 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 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 = CONSTRUCTOR_ELT (aggr_init, 0)->value;
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 CONSTRUCTOR_ELT (aggr_init, 0)->value = 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);
4966 if (!TREE_CONSTANT (elt_init))
4967 TREE_CONSTANT (new_init) = false;
4970 return new_init;
4973 /* Subroutine of reshape_init_r, processes the initializers for arrays.
4974 Parameters are the same of reshape_init_r. */
4976 static tree
4977 reshape_init_array (tree type, reshape_iter *d)
4979 tree max_index = NULL_TREE;
4981 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
4983 if (TYPE_DOMAIN (type))
4984 max_index = array_type_nelts (type);
4986 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4989 /* Subroutine of reshape_init_r, processes the initializers for vectors.
4990 Parameters are the same of reshape_init_r. */
4992 static tree
4993 reshape_init_vector (tree type, reshape_iter *d)
4995 tree max_index = NULL_TREE;
4997 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
4999 if (COMPOUND_LITERAL_P (d->cur->value))
5001 tree value = d->cur->value;
5002 if (!same_type_p (TREE_TYPE (value), type))
5004 error ("invalid type %qT as initializer for a vector of type %qT",
5005 TREE_TYPE (d->cur->value), type);
5006 value = error_mark_node;
5008 ++d->cur;
5009 return value;
5012 /* For a vector, we initialize it as an array of the appropriate size. */
5013 if (TREE_CODE (type) == VECTOR_TYPE)
5014 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5016 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
5019 /* Subroutine of reshape_init_r, processes the initializers for classes
5020 or union. Parameters are the same of reshape_init_r. */
5022 static tree
5023 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p)
5025 tree field;
5026 tree new_init;
5028 gcc_assert (CLASS_TYPE_P (type));
5030 /* The initializer for a class is always a CONSTRUCTOR. */
5031 new_init = build_constructor (init_list_type_node, NULL);
5032 field = next_initializable_field (TYPE_FIELDS (type));
5034 if (!field)
5036 /* [dcl.init.aggr]
5038 An initializer for an aggregate member that is an
5039 empty class shall have the form of an empty
5040 initializer-list {}. */
5041 if (!first_initializer_p)
5043 error ("initializer for %qT must be brace-enclosed", type);
5044 return error_mark_node;
5046 return new_init;
5049 /* Loop through the initializable fields, gathering initializers. */
5050 while (d->cur != d->end)
5052 tree field_init;
5054 /* Handle designated initializers, as an extension. */
5055 if (d->cur->index)
5057 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
5059 if (!field || TREE_CODE (field) != FIELD_DECL)
5061 error ("%qT has no non-static data member named %qD", type,
5062 d->cur->index);
5063 return error_mark_node;
5067 /* If we processed all the member of the class, we are done. */
5068 if (!field)
5069 break;
5071 field_init = reshape_init_r (TREE_TYPE (field), d,
5072 /*first_initializer_p=*/false);
5073 if (field_init == error_mark_node)
5074 return error_mark_node;
5076 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5078 /* [dcl.init.aggr]
5080 When a union is initialized with a brace-enclosed
5081 initializer, the braces shall only contain an
5082 initializer for the first member of the union. */
5083 if (TREE_CODE (type) == UNION_TYPE)
5084 break;
5086 field = next_initializable_field (DECL_CHAIN (field));
5089 return new_init;
5092 /* Subroutine of reshape_init, which processes a single initializer (part of
5093 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5094 iterator within the CONSTRUCTOR which points to the initializer to process.
5095 FIRST_INITIALIZER_P is true if this is the first initializer of the
5096 outermost CONSTRUCTOR node. */
5098 static tree
5099 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p)
5101 tree init = d->cur->value;
5103 if (error_operand_p (init))
5104 return error_mark_node;
5106 /* A non-aggregate type is always initialized with a single
5107 initializer. */
5108 if (!CP_AGGREGATE_TYPE_P (type))
5110 /* It is invalid to initialize a non-aggregate type with a
5111 brace-enclosed initializer before C++0x.
5112 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5113 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5114 a CONSTRUCTOR (with a record type). */
5115 if (TREE_CODE (init) == CONSTRUCTOR
5116 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5118 if (SCALAR_TYPE_P (type))
5120 error ("braces around scalar initializer for type %qT", type);
5121 init = error_mark_node;
5123 else
5124 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5127 d->cur++;
5128 return init;
5131 /* [dcl.init.aggr]
5133 All implicit type conversions (clause _conv_) are considered when
5134 initializing the aggregate member with an initializer from an
5135 initializer-list. If the initializer can initialize a member,
5136 the member is initialized. Otherwise, if the member is itself a
5137 non-empty subaggregate, brace elision is assumed and the
5138 initializer is considered for the initialization of the first
5139 member of the subaggregate. */
5140 if (TREE_CODE (init) != CONSTRUCTOR
5141 /* But don't try this for the first initializer, since that would be
5142 looking through the outermost braces; A a2 = { a1 }; is not a
5143 valid aggregate initialization. */
5144 && !first_initializer_p
5145 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5146 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL)))
5148 d->cur++;
5149 return init;
5152 /* [dcl.init.string]
5154 A char array (whether plain char, signed char, or unsigned char)
5155 can be initialized by a string-literal (optionally enclosed in
5156 braces); a wchar_t array can be initialized by a wide
5157 string-literal (optionally enclosed in braces). */
5158 if (TREE_CODE (type) == ARRAY_TYPE
5159 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5161 tree str_init = init;
5163 /* Strip one level of braces if and only if they enclose a single
5164 element (as allowed by [dcl.init.string]). */
5165 if (!first_initializer_p
5166 && TREE_CODE (str_init) == CONSTRUCTOR
5167 && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (str_init)) == 1)
5169 str_init = VEC_index (constructor_elt,
5170 CONSTRUCTOR_ELTS (str_init), 0)->value;
5173 /* If it's a string literal, then it's the initializer for the array
5174 as a whole. Otherwise, continue with normal initialization for
5175 array types (one value per array element). */
5176 if (TREE_CODE (str_init) == STRING_CST)
5178 d->cur++;
5179 return str_init;
5183 /* The following cases are about aggregates. If we are not within a full
5184 initializer already, and there is not a CONSTRUCTOR, it means that there
5185 is a missing set of braces (that is, we are processing the case for
5186 which reshape_init exists). */
5187 if (!first_initializer_p)
5189 if (TREE_CODE (init) == CONSTRUCTOR)
5191 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
5192 /* There is no need to reshape pointer-to-member function
5193 initializers, as they are always constructed correctly
5194 by the front end. */
5196 else if (COMPOUND_LITERAL_P (init))
5197 /* For a nested compound literal, there is no need to reshape since
5198 brace elision is not allowed. Even if we decided to allow it,
5199 we should add a call to reshape_init in finish_compound_literal,
5200 before calling digest_init, so changing this code would still
5201 not be necessary. */
5202 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
5203 else
5205 ++d->cur;
5206 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5207 return reshape_init (type, init);
5211 warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
5212 type);
5215 /* Dispatch to specialized routines. */
5216 if (CLASS_TYPE_P (type))
5217 return reshape_init_class (type, d, first_initializer_p);
5218 else if (TREE_CODE (type) == ARRAY_TYPE)
5219 return reshape_init_array (type, d);
5220 else if (TREE_CODE (type) == VECTOR_TYPE)
5221 return reshape_init_vector (type, d);
5222 else
5223 gcc_unreachable();
5226 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
5227 brace-enclosed aggregate initializer.
5229 INIT is the CONSTRUCTOR containing the list of initializers describing
5230 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
5231 It may not presently match the shape of the TYPE; for example:
5233 struct S { int a; int b; };
5234 struct S a[] = { 1, 2, 3, 4 };
5236 Here INIT will hold a VEC of four elements, rather than a
5237 VEC of two elements, each itself a VEC of two elements. This
5238 routine transforms INIT from the former form into the latter. The
5239 revised CONSTRUCTOR node is returned. */
5241 tree
5242 reshape_init (tree type, tree init)
5244 VEC(constructor_elt, gc) *v;
5245 reshape_iter d;
5246 tree new_init;
5248 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5250 v = CONSTRUCTOR_ELTS (init);
5252 /* An empty constructor does not need reshaping, and it is always a valid
5253 initializer. */
5254 if (VEC_empty (constructor_elt, v))
5255 return init;
5257 /* Recurse on this CONSTRUCTOR. */
5258 d.cur = VEC_index (constructor_elt, v, 0);
5259 d.end = d.cur + VEC_length (constructor_elt, v);
5261 new_init = reshape_init_r (type, &d, true);
5262 if (new_init == error_mark_node)
5263 return error_mark_node;
5265 /* Make sure all the element of the constructor were used. Otherwise,
5266 issue an error about exceeding initializers. */
5267 if (d.cur != d.end)
5268 error ("too many initializers for %qT", type);
5270 return new_init;
5273 /* Verify array initializer. Returns true if errors have been reported. */
5275 bool
5276 check_array_initializer (tree decl, tree type, tree init)
5278 tree element_type = TREE_TYPE (type);
5280 /* The array type itself need not be complete, because the
5281 initializer may tell us how many elements are in the array.
5282 But, the elements of the array must be complete. */
5283 if (!COMPLETE_TYPE_P (complete_type (element_type)))
5285 if (decl)
5286 error ("elements of array %q#D have incomplete type", decl);
5287 else
5288 error ("elements of array %q#T have incomplete type", type);
5289 return true;
5291 /* It is not valid to initialize a VLA. */
5292 if (init
5293 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
5294 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
5296 if (decl)
5297 error ("variable-sized object %qD may not be initialized", decl);
5298 else
5299 error ("variable-sized compound literal");
5300 return true;
5302 return false;
5305 /* Subroutine of check_initializer; args are passed down from that function.
5306 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
5308 static tree
5309 build_aggr_init_full_exprs (tree decl, tree init, int flags)
5312 int saved_stmts_are_full_exprs_p = 0;
5313 if (building_stmt_tree ())
5315 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
5316 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
5318 init = build_aggr_init (decl, init, flags, tf_warning_or_error);
5319 if (building_stmt_tree ())
5320 current_stmt_tree ()->stmts_are_full_exprs_p =
5321 saved_stmts_are_full_exprs_p;
5322 return init;
5325 /* Verify INIT (the initializer for DECL), and record the
5326 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
5327 grok_reference_init.
5329 If the return value is non-NULL, it is an expression that must be
5330 evaluated dynamically to initialize DECL. */
5332 static tree
5333 check_initializer (tree decl, tree init, int flags, tree *cleanup)
5335 tree type = TREE_TYPE (decl);
5336 tree init_code = NULL;
5337 tree extra_init = NULL_TREE;
5338 tree core_type;
5340 /* Things that are going to be initialized need to have complete
5341 type. */
5342 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
5344 if (type == error_mark_node)
5345 /* We will have already complained. */
5346 return NULL_TREE;
5348 if (TREE_CODE (type) == ARRAY_TYPE)
5350 if (check_array_initializer (decl, type, init))
5351 return NULL_TREE;
5353 else if (!COMPLETE_TYPE_P (type))
5355 error ("%qD has incomplete type", decl);
5356 TREE_TYPE (decl) = error_mark_node;
5357 return NULL_TREE;
5359 else
5360 /* There is no way to make a variable-sized class type in GNU C++. */
5361 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
5363 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
5365 int init_len = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init));
5366 if (SCALAR_TYPE_P (type))
5368 if (init_len == 0)
5370 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5371 init = build_zero_init (type, NULL_TREE, false);
5373 else if (init_len != 1)
5375 error ("scalar object %qD requires one element in initializer",
5376 decl);
5377 TREE_TYPE (decl) = error_mark_node;
5378 return NULL_TREE;
5383 if (TREE_CODE (decl) == CONST_DECL)
5385 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
5387 DECL_INITIAL (decl) = init;
5389 gcc_assert (init != NULL_TREE);
5390 init = NULL_TREE;
5392 else if (!init && DECL_REALLY_EXTERN (decl))
5394 else if (TREE_CODE (type) == REFERENCE_TYPE)
5395 init = grok_reference_init (decl, type, init, cleanup);
5396 else if (init || TYPE_NEEDS_CONSTRUCTING (type))
5398 if (!init)
5399 check_for_uninitialized_const_var (decl);
5400 /* Do not reshape constructors of vectors (they don't need to be
5401 reshaped. */
5402 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
5404 if (is_std_init_list (type))
5405 init = build_init_list_var_init (decl, type, init,
5406 &extra_init, cleanup);
5407 else if (TYPE_NON_AGGREGATE_CLASS (type))
5409 /* Don't reshape if the class has constructors. */
5410 if (cxx_dialect == cxx98)
5411 error ("in C++98 %qD must be initialized by constructor, "
5412 "not by %<{...}%>",
5413 decl);
5415 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_VECTOR_OPAQUE (type))
5417 error ("opaque vector types cannot be initialized");
5418 init = error_mark_node;
5420 else
5421 init = reshape_init (type, init);
5424 /* If DECL has an array type without a specific bound, deduce the
5425 array size from the initializer. */
5426 maybe_deduce_size_from_array_init (decl, init);
5427 type = TREE_TYPE (decl);
5428 if (type == error_mark_node)
5429 return NULL_TREE;
5431 if (TYPE_NEEDS_CONSTRUCTING (type)
5432 || (CLASS_TYPE_P (type)
5433 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init))))
5435 init_code = build_aggr_init_full_exprs (decl, init, flags);
5437 /* If this is a constexpr initializer, expand_default_init will
5438 have returned an INIT_EXPR rather than a CALL_EXPR. In that
5439 case, pull the initializer back out and pass it down into
5440 store_init_value. */
5441 while (TREE_CODE (init_code) == EXPR_STMT
5442 || TREE_CODE (init_code) == CONVERT_EXPR)
5443 init_code = TREE_OPERAND (init_code, 0);
5444 if (TREE_CODE (init_code) == INIT_EXPR)
5446 init = TREE_OPERAND (init_code, 1);
5447 init_code = NULL_TREE;
5448 /* Don't call digest_init; it's unnecessary and will complain
5449 about aggregate initialization of non-aggregate classes. */
5450 flags |= LOOKUP_ALREADY_DIGESTED;
5452 else if (DECL_DECLARED_CONSTEXPR_P (decl))
5454 /* Declared constexpr, but no suitable initializer; massage
5455 init appropriately so we can pass it into store_init_value
5456 for the error. */
5457 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
5458 init = finish_compound_literal (type, init,
5459 tf_warning_or_error);
5460 else if (CLASS_TYPE_P (type)
5461 && (!init || TREE_CODE (init) == TREE_LIST))
5463 init = build_functional_cast (type, init, tf_none);
5464 if (init != error_mark_node)
5465 TARGET_EXPR_DIRECT_INIT_P (init) = true;
5467 init_code = NULL_TREE;
5469 else
5470 init = NULL_TREE;
5473 if (init && TREE_CODE (init) != TREE_VEC)
5475 init_code = store_init_value (decl, init, flags);
5476 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
5477 && DECL_INITIAL (decl)
5478 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
5479 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
5480 warning (0, "array %qD initialized by parenthesized string literal %qE",
5481 decl, DECL_INITIAL (decl));
5482 init = NULL;
5485 else
5487 if (CLASS_TYPE_P (core_type = strip_array_types (type))
5488 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
5489 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
5490 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
5491 /*complain=*/true);
5493 check_for_uninitialized_const_var (decl);
5496 if (init && init != error_mark_node)
5497 init_code = build2 (INIT_EXPR, type, decl, init);
5499 if (extra_init)
5500 init_code = add_stmt_to_compound (extra_init, init_code);
5502 if (init_code && DECL_IN_AGGR_P (decl))
5504 static int explained = 0;
5506 if (cxx_dialect < cxx0x)
5507 error ("initializer invalid for static member with constructor");
5508 else
5509 error ("non-constant in-class initialization invalid for static "
5510 "member %qD", decl);
5511 if (!explained)
5513 error ("(an out of class initialization is required)");
5514 explained = 1;
5518 return init_code;
5521 /* If DECL is not a local variable, give it RTL. */
5523 static void
5524 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
5526 int toplev = toplevel_bindings_p ();
5527 int defer_p;
5528 const char *filename;
5530 /* Set the DECL_ASSEMBLER_NAME for the object. */
5531 if (asmspec)
5533 /* The `register' keyword, when used together with an
5534 asm-specification, indicates that the variable should be
5535 placed in a particular register. */
5536 if (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
5538 set_user_assembler_name (decl, asmspec);
5539 DECL_HARD_REGISTER (decl) = 1;
5541 else
5543 if (TREE_CODE (decl) == FUNCTION_DECL
5544 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5545 set_builtin_user_assembler_name (decl, asmspec);
5546 set_user_assembler_name (decl, asmspec);
5550 /* Handle non-variables up front. */
5551 if (TREE_CODE (decl) != VAR_DECL)
5553 rest_of_decl_compilation (decl, toplev, at_eof);
5554 return;
5557 /* If we see a class member here, it should be a static data
5558 member. */
5559 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
5561 gcc_assert (TREE_STATIC (decl));
5562 /* An in-class declaration of a static data member should be
5563 external; it is only a declaration, and not a definition. */
5564 if (init == NULL_TREE)
5565 gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
5568 /* We don't create any RTL for local variables. */
5569 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
5570 return;
5572 /* We defer emission of local statics until the corresponding
5573 DECL_EXPR is expanded. */
5574 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
5576 /* We try to defer namespace-scope static constants so that they are
5577 not emitted into the object file unnecessarily. */
5578 filename = input_filename;
5579 if (!DECL_VIRTUAL_P (decl)
5580 && TREE_READONLY (decl)
5581 && DECL_INITIAL (decl) != NULL_TREE
5582 && DECL_INITIAL (decl) != error_mark_node
5583 && filename != NULL
5584 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
5585 && toplev
5586 && !TREE_PUBLIC (decl))
5588 /* Fool with the linkage of static consts according to #pragma
5589 interface. */
5590 struct c_fileinfo *finfo = get_fileinfo (filename);
5591 if (!finfo->interface_unknown && !TREE_PUBLIC (decl))
5593 TREE_PUBLIC (decl) = 1;
5594 DECL_EXTERNAL (decl) = finfo->interface_only;
5597 defer_p = 1;
5599 /* Likewise for template instantiations. */
5600 else if (DECL_LANG_SPECIFIC (decl)
5601 && DECL_IMPLICIT_INSTANTIATION (decl))
5602 defer_p = 1;
5604 /* If we're not deferring, go ahead and assemble the variable. */
5605 if (!defer_p)
5606 rest_of_decl_compilation (decl, toplev, at_eof);
5609 /* walk_tree helper for wrap_temporary_cleanups, below. */
5611 static tree
5612 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
5614 if (TYPE_P (*stmt_p))
5616 *walk_subtrees = 0;
5617 return NULL_TREE;
5620 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
5622 tree guard = (tree)data;
5623 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
5625 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
5626 /* Tell honor_protect_cleanup_actions to handle this as a separate
5627 cleanup. */
5628 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
5630 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
5633 return NULL_TREE;
5636 /* We're initializing a local variable which has a cleanup GUARD. If there
5637 are any temporaries used in the initializer INIT of this variable, we
5638 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
5639 variable will be cleaned up properly if one of them throws.
5641 Unfortunately, there's no way to express this properly in terms of
5642 nesting, as the regions for the temporaries overlap the region for the
5643 variable itself; if there are two temporaries, the variable needs to be
5644 the first thing destroyed if either of them throws. However, we only
5645 want to run the variable's cleanup if it actually got constructed. So
5646 we need to guard the temporary cleanups with the variable's cleanup if
5647 they are run on the normal path, but not if they are run on the
5648 exceptional path. We implement this by telling
5649 honor_protect_cleanup_actions to strip the variable cleanup from the
5650 exceptional path. */
5652 static void
5653 wrap_temporary_cleanups (tree init, tree guard)
5655 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
5658 /* Generate code to initialize DECL (a local variable). */
5660 static void
5661 initialize_local_var (tree decl, tree init)
5663 tree type = TREE_TYPE (decl);
5664 tree cleanup;
5665 int already_used;
5667 gcc_assert (TREE_CODE (decl) == VAR_DECL
5668 || TREE_CODE (decl) == RESULT_DECL);
5669 gcc_assert (!TREE_STATIC (decl));
5671 if (DECL_SIZE (decl) == NULL_TREE)
5673 /* If we used it already as memory, it must stay in memory. */
5674 DECL_INITIAL (decl) = NULL_TREE;
5675 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5676 return;
5679 if (type == error_mark_node)
5680 return;
5682 /* Compute and store the initial value. */
5683 already_used = TREE_USED (decl) || TREE_USED (type);
5684 if (TREE_USED (type))
5685 DECL_READ_P (decl) = 1;
5687 /* Generate a cleanup, if necessary. */
5688 cleanup = cxx_maybe_build_cleanup (decl);
5690 /* Perform the initialization. */
5691 if (init)
5693 int saved_stmts_are_full_exprs_p;
5695 /* If we're only initializing a single object, guard the destructors
5696 of any temporaries used in its initializer with its destructor.
5697 This isn't right for arrays because each element initialization is
5698 a full-expression. */
5699 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
5700 wrap_temporary_cleanups (init, cleanup);
5702 gcc_assert (building_stmt_tree ());
5703 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
5704 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
5705 finish_expr_stmt (init);
5706 current_stmt_tree ()->stmts_are_full_exprs_p =
5707 saved_stmts_are_full_exprs_p;
5710 /* Set this to 0 so we can tell whether an aggregate which was
5711 initialized was ever used. Don't do this if it has a
5712 destructor, so we don't complain about the 'resource
5713 allocation is initialization' idiom. Now set
5714 attribute((unused)) on types so decls of that type will be
5715 marked used. (see TREE_USED, above.) */
5716 if (TYPE_NEEDS_CONSTRUCTING (type)
5717 && ! already_used
5718 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
5719 && DECL_NAME (decl))
5720 TREE_USED (decl) = 0;
5721 else if (already_used)
5722 TREE_USED (decl) = 1;
5724 if (cleanup)
5725 finish_decl_cleanup (decl, cleanup);
5728 /* DECL is a VAR_DECL for a compiler-generated variable with static
5729 storage duration (like a virtual table) whose initializer is a
5730 compile-time constant. Initialize the variable and provide it to the
5731 back end. */
5733 void
5734 initialize_artificial_var (tree decl, VEC(constructor_elt,gc) *v)
5736 tree init;
5737 gcc_assert (DECL_ARTIFICIAL (decl));
5738 init = build_constructor (TREE_TYPE (decl), v);
5739 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
5740 DECL_INITIAL (decl) = init;
5741 DECL_INITIALIZED_P (decl) = 1;
5742 determine_visibility (decl);
5743 layout_var_decl (decl);
5744 maybe_commonize_var (decl);
5745 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
5748 /* INIT is the initializer for a variable, as represented by the
5749 parser. Returns true iff INIT is value-dependent. */
5751 static bool
5752 value_dependent_init_p (tree init)
5754 if (TREE_CODE (init) == TREE_LIST)
5755 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
5756 return any_value_dependent_elements_p (init);
5757 else if (TREE_CODE (init) == CONSTRUCTOR)
5758 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
5760 VEC(constructor_elt, gc) *elts;
5761 size_t nelts;
5762 size_t i;
5764 elts = CONSTRUCTOR_ELTS (init);
5765 nelts = VEC_length (constructor_elt, elts);
5766 for (i = 0; i < nelts; ++i)
5767 if (value_dependent_init_p (VEC_index (constructor_elt,
5768 elts, i)->value))
5769 return true;
5771 else
5772 /* It must be a simple expression, e.g., int i = 3; */
5773 return value_dependent_expression_p (init);
5775 return false;
5778 /* Finish processing of a declaration;
5779 install its line number and initial value.
5780 If the length of an array type is not known before,
5781 it must be determined now, from the initial value, or it is an error.
5783 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
5784 true, then INIT is an integral constant expression.
5786 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
5787 if the (init) syntax was used. */
5789 void
5790 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
5791 tree asmspec_tree, int flags)
5793 tree type;
5794 tree cleanup;
5795 const char *asmspec = NULL;
5796 int was_readonly = 0;
5797 bool var_definition_p = false;
5798 int saved_processing_template_decl;
5799 tree auto_node;
5801 if (decl == error_mark_node)
5802 return;
5803 else if (! decl)
5805 if (init)
5806 error ("assignment (not initialization) in declaration");
5807 return;
5810 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
5811 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
5812 gcc_assert (TREE_CODE (decl) != PARM_DECL);
5814 type = TREE_TYPE (decl);
5815 if (type == error_mark_node)
5816 return;
5818 /* Assume no cleanup is required. */
5819 cleanup = NULL_TREE;
5820 saved_processing_template_decl = processing_template_decl;
5822 /* If a name was specified, get the string. */
5823 if (global_scope_p (current_binding_level))
5824 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5825 if (asmspec_tree && asmspec_tree != error_mark_node)
5826 asmspec = TREE_STRING_POINTER (asmspec_tree);
5828 if (current_class_type
5829 && CP_DECL_CONTEXT (decl) == current_class_type
5830 && TYPE_BEING_DEFINED (current_class_type)
5831 && (DECL_INITIAL (decl) || init))
5832 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
5834 auto_node = type_uses_auto (type);
5835 if (auto_node)
5837 tree d_init;
5838 if (init == NULL_TREE)
5840 error ("declaration of %q#D has no initializer", decl);
5841 TREE_TYPE (decl) = error_mark_node;
5842 return;
5844 d_init = init;
5845 if (TREE_CODE (d_init) == TREE_LIST)
5846 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
5847 tf_warning_or_error);
5848 d_init = resolve_nondeduced_context (d_init);
5849 if (describable_type (d_init))
5851 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init,
5852 auto_node);
5853 if (type == error_mark_node)
5854 return;
5858 if (TREE_CODE (decl) == FUNCTION_DECL
5859 /* For members, defer until finalize_literal_type_property. */
5860 && (!DECL_CLASS_SCOPE_P (decl)
5861 || !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))))
5862 validate_constexpr_fundecl (decl);
5864 else if (!ensure_literal_type_for_constexpr_object (decl))
5865 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
5867 if (init && TREE_CODE (decl) == FUNCTION_DECL)
5869 tree clone;
5870 if (init == ridpointers[(int)RID_DELETE])
5872 /* FIXME check this is 1st decl. */
5873 DECL_DELETED_FN (decl) = 1;
5874 DECL_DECLARED_INLINE_P (decl) = 1;
5875 DECL_INITIAL (decl) = error_mark_node;
5876 FOR_EACH_CLONE (clone, decl)
5878 DECL_DELETED_FN (clone) = 1;
5879 DECL_DECLARED_INLINE_P (clone) = 1;
5880 DECL_INITIAL (clone) = error_mark_node;
5882 init = NULL_TREE;
5884 else if (init == ridpointers[(int)RID_DEFAULT])
5886 if (defaultable_fn_check (decl))
5887 DECL_DEFAULTED_FN (decl) = 1;
5888 else
5889 DECL_INITIAL (decl) = NULL_TREE;
5893 if (init && TREE_CODE (decl) == VAR_DECL)
5895 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
5896 if (init_const_expr_p)
5898 /* Set these flags now for templates. We'll update the flags in
5899 store_init_value for instantiations. */
5900 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5901 if (decl_maybe_constant_var_p (decl))
5902 TREE_CONSTANT (decl) = 1;
5906 if (processing_template_decl)
5908 bool type_dependent_p;
5910 /* Add this declaration to the statement-tree. */
5911 if (at_function_scope_p ())
5912 add_decl_expr (decl);
5914 type_dependent_p = dependent_type_p (type);
5916 if (check_for_bare_parameter_packs (init))
5918 init = NULL_TREE;
5919 DECL_INITIAL (decl) = NULL_TREE;
5922 /* Generally, initializers in templates are expanded when the
5923 template is instantiated. But, if DECL is a variable constant
5924 then it can be used in future constant expressions, so its value
5925 must be available. */
5926 if (!(init
5927 && DECL_CLASS_SCOPE_P (decl)
5928 /* We just set TREE_CONSTANT appropriately; see above. */
5929 && TREE_CONSTANT (decl)
5930 && !type_dependent_p
5931 /* FIXME non-value-dependent constant expression */
5932 && !value_dependent_init_p (init)))
5934 if (init)
5935 DECL_INITIAL (decl) = init;
5936 if (TREE_CODE (decl) == VAR_DECL
5937 && !DECL_PRETTY_FUNCTION_P (decl)
5938 && !type_dependent_p)
5939 maybe_deduce_size_from_array_init (decl, init);
5940 goto finish_end;
5943 if (TREE_CODE (init) == TREE_LIST)
5945 /* If the parenthesized-initializer form was used (e.g.,
5946 "int A<N>::i(X)"), then INIT will be a TREE_LIST of initializer
5947 arguments. (There is generally only one.) We convert them
5948 individually. */
5949 tree list = init;
5950 for (; list; list = TREE_CHAIN (list))
5952 tree elt = TREE_VALUE (list);
5953 TREE_VALUE (list) = fold_non_dependent_expr (elt);
5956 else
5957 init = fold_non_dependent_expr (init);
5958 processing_template_decl = 0;
5961 /* Take care of TYPE_DECLs up front. */
5962 if (TREE_CODE (decl) == TYPE_DECL)
5964 if (type != error_mark_node
5965 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
5967 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
5968 warning (0, "shadowing previous type declaration of %q#D", decl);
5969 set_identifier_type_value (DECL_NAME (decl), decl);
5972 /* If we have installed this as the canonical typedef for this
5973 type, and that type has not been defined yet, delay emitting
5974 the debug information for it, as we will emit it later. */
5975 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
5976 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
5977 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5979 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
5980 at_eof);
5981 goto finish_end;
5984 /* A reference will be modified here, as it is initialized. */
5985 if (! DECL_EXTERNAL (decl)
5986 && TREE_READONLY (decl)
5987 && TREE_CODE (type) == REFERENCE_TYPE)
5989 was_readonly = 1;
5990 TREE_READONLY (decl) = 0;
5993 if (TREE_CODE (decl) == VAR_DECL)
5995 /* Only variables with trivial initialization and destruction can
5996 have thread-local storage. */
5997 if (DECL_THREAD_LOCAL_P (decl)
5998 && (type_has_nontrivial_default_init (TREE_TYPE (decl))
5999 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
6000 error ("%qD cannot be thread-local because it has non-trivial "
6001 "type %qT", decl, TREE_TYPE (decl));
6002 /* If this is a local variable that will need a mangled name,
6003 register it now. We must do this before processing the
6004 initializer for the variable, since the initialization might
6005 require a guard variable, and since the mangled name of the
6006 guard variable will depend on the mangled name of this
6007 variable. */
6008 if (DECL_FUNCTION_SCOPE_P (decl)
6009 && TREE_STATIC (decl)
6010 && !DECL_ARTIFICIAL (decl))
6012 push_local_name (decl);
6013 if (DECL_CONSTRUCTOR_P (current_function_decl)
6014 || DECL_DESTRUCTOR_P (current_function_decl))
6015 /* Normally local_decls is populated during GIMPLE lowering,
6016 but [cd]tors are never actually compiled directly. We need
6017 to put statics on the list so we can deal with the label
6018 address extension. */
6019 add_local_decl (cfun, decl);
6022 /* Convert the initializer to the type of DECL, if we have not
6023 already initialized DECL. */
6024 if (!DECL_INITIALIZED_P (decl)
6025 /* If !DECL_EXTERNAL then DECL is being defined. In the
6026 case of a static data member initialized inside the
6027 class-specifier, there can be an initializer even if DECL
6028 is *not* defined. */
6029 && (!DECL_EXTERNAL (decl) || init))
6031 if (TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6033 tree jclass
6034 = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
6035 /* Allow libjava/prims.cc define primitive classes. */
6036 if (init != NULL_TREE
6037 || jclass == NULL_TREE
6038 || TREE_CODE (jclass) != TYPE_DECL
6039 || !POINTER_TYPE_P (TREE_TYPE (jclass))
6040 || !same_type_ignoring_top_level_qualifiers_p
6041 (type, TREE_TYPE (TREE_TYPE (jclass))))
6042 error ("Java object %qD not allocated with %<new%>", decl);
6043 init = NULL_TREE;
6045 init = check_initializer (decl, init, flags, &cleanup);
6046 /* Thread-local storage cannot be dynamically initialized. */
6047 if (DECL_THREAD_LOCAL_P (decl) && init)
6049 error ("%qD is thread-local and so cannot be dynamically "
6050 "initialized", decl);
6051 init = NULL_TREE;
6054 /* Check that the initializer for a static data member was a
6055 constant. Although we check in the parser that the
6056 initializer is an integral constant expression, we do not
6057 simplify division-by-zero at the point at which it
6058 occurs. Therefore, in:
6060 struct S { static const int i = 7 / 0; };
6062 we issue an error at this point. It would
6063 probably be better to forbid division by zero in
6064 integral constant expressions. */
6065 if (DECL_EXTERNAL (decl) && init)
6067 error ("%qD cannot be initialized by a non-constant expression"
6068 " when being declared", decl);
6069 DECL_INITIALIZED_IN_CLASS_P (decl) = 0;
6070 init = NULL_TREE;
6073 /* Handle:
6075 [dcl.init]
6077 The memory occupied by any object of static storage
6078 duration is zero-initialized at program startup before
6079 any other initialization takes place.
6081 We cannot create an appropriate initializer until after
6082 the type of DECL is finalized. If DECL_INITIAL is set,
6083 then the DECL is statically initialized, and any
6084 necessary zero-initialization has already been performed. */
6085 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
6086 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
6087 /*nelts=*/NULL_TREE,
6088 /*static_storage_p=*/true);
6089 /* Remember that the initialization for this variable has
6090 taken place. */
6091 DECL_INITIALIZED_P (decl) = 1;
6092 /* This declaration is the definition of this variable,
6093 unless we are initializing a static data member within
6094 the class specifier. */
6095 if (!DECL_EXTERNAL (decl))
6096 var_definition_p = true;
6098 /* If the variable has an array type, lay out the type, even if
6099 there is no initializer. It is valid to index through the
6100 array, and we must get TYPE_ALIGN set correctly on the array
6101 type. */
6102 else if (TREE_CODE (type) == ARRAY_TYPE)
6103 layout_type (type);
6105 if (!processing_template_decl
6106 && TREE_STATIC (decl)
6107 && !at_function_scope_p ()
6108 && current_function_decl == NULL)
6109 /* So decl is a global variable or a static member of a
6110 non local class. Record the types it uses
6111 so that we can decide later to emit debug info for them. */
6112 record_types_used_by_current_var_decl (decl);
6114 else if (TREE_CODE (decl) == FIELD_DECL
6115 && TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6116 error ("non-static data member %qD has Java class type", decl);
6118 /* Add this declaration to the statement-tree. This needs to happen
6119 after the call to check_initializer so that the DECL_EXPR for a
6120 reference temp is added before the DECL_EXPR for the reference itself. */
6121 if (at_function_scope_p ())
6122 add_decl_expr (decl);
6124 /* Let the middle end know about variables and functions -- but not
6125 static data members in uninstantiated class templates. */
6126 if (!saved_processing_template_decl
6127 && (TREE_CODE (decl) == VAR_DECL
6128 || TREE_CODE (decl) == FUNCTION_DECL))
6130 if (TREE_CODE (decl) == VAR_DECL)
6132 layout_var_decl (decl);
6133 maybe_commonize_var (decl);
6136 /* This needs to happen after the linkage is set. */
6137 determine_visibility (decl);
6139 if (var_definition_p && TREE_STATIC (decl))
6141 /* If a TREE_READONLY variable needs initialization
6142 at runtime, it is no longer readonly and we need to
6143 avoid MEM_READONLY_P being set on RTL created for it. */
6144 if (init)
6146 if (TREE_READONLY (decl))
6147 TREE_READONLY (decl) = 0;
6148 was_readonly = 0;
6150 else if (was_readonly)
6151 TREE_READONLY (decl) = 1;
6154 make_rtl_for_nonlocal_decl (decl, init, asmspec);
6156 /* Check for abstractness of the type. Notice that there is no
6157 need to strip array types here since the check for those types
6158 is already done within create_array_type_for_decl. */
6159 if (TREE_CODE (type) == FUNCTION_TYPE
6160 || TREE_CODE (type) == METHOD_TYPE)
6161 abstract_virtuals_error (decl, TREE_TYPE (type));
6162 else
6163 abstract_virtuals_error (decl, type);
6165 if (TREE_TYPE (decl) == error_mark_node)
6166 /* No initialization required. */
6168 else if (TREE_CODE (decl) == FUNCTION_DECL)
6170 if (init)
6172 if (init == ridpointers[(int)RID_DEFAULT])
6174 /* An out-of-class default definition is defined at
6175 the point where it is explicitly defaulted. */
6176 if (DECL_DELETED_FN (decl))
6177 maybe_explain_implicit_delete (decl);
6178 else if (DECL_INITIAL (decl) == error_mark_node)
6179 synthesize_method (decl);
6181 else
6182 error ("function %q#D is initialized like a variable", decl);
6184 /* else no initialization required. */
6186 else if (DECL_EXTERNAL (decl)
6187 && ! (DECL_LANG_SPECIFIC (decl)
6188 && DECL_NOT_REALLY_EXTERN (decl)))
6190 if (init)
6191 DECL_INITIAL (decl) = init;
6193 /* A variable definition. */
6194 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6195 /* Initialize the local variable. */
6196 initialize_local_var (decl, init);
6198 /* If a variable is defined, and then a subsequent
6199 definition with external linkage is encountered, we will
6200 get here twice for the same variable. We want to avoid
6201 calling expand_static_init more than once. For variables
6202 that are not static data members, we can call
6203 expand_static_init only when we actually process the
6204 initializer. It is not legal to redeclare a static data
6205 member, so this issue does not arise in that case. */
6206 else if (var_definition_p && TREE_STATIC (decl))
6207 expand_static_init (decl, init);
6210 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
6211 reference, insert it in the statement-tree now. */
6212 if (cleanup)
6213 push_cleanup (decl, cleanup, false);
6215 finish_end:
6216 processing_template_decl = saved_processing_template_decl;
6218 if (was_readonly)
6219 TREE_READONLY (decl) = 1;
6222 /* Returns a declaration for a VAR_DECL as if:
6224 extern "C" TYPE NAME;
6226 had been seen. Used to create compiler-generated global
6227 variables. */
6229 static tree
6230 declare_global_var (tree name, tree type)
6232 tree decl;
6234 push_to_top_level ();
6235 decl = build_decl (input_location, VAR_DECL, name, type);
6236 TREE_PUBLIC (decl) = 1;
6237 DECL_EXTERNAL (decl) = 1;
6238 DECL_ARTIFICIAL (decl) = 1;
6239 /* If the user has explicitly declared this variable (perhaps
6240 because the code we are compiling is part of a low-level runtime
6241 library), then it is possible that our declaration will be merged
6242 with theirs by pushdecl. */
6243 decl = pushdecl (decl);
6244 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
6245 pop_from_top_level ();
6247 return decl;
6250 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
6251 if "__cxa_atexit" is not being used) corresponding to the function
6252 to be called when the program exits. */
6254 static tree
6255 get_atexit_fn_ptr_type (void)
6257 tree fn_type;
6259 if (!atexit_fn_ptr_type_node)
6261 tree arg_type;
6262 if (flag_use_cxa_atexit
6263 && !targetm.cxx.use_atexit_for_cxa_atexit ())
6264 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
6265 arg_type = ptr_type_node;
6266 else
6267 /* The parameter to "atexit" is "void (*)(void)". */
6268 arg_type = NULL_TREE;
6270 fn_type = build_function_type_list (void_type_node,
6271 arg_type, NULL_TREE);
6272 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
6275 return atexit_fn_ptr_type_node;
6278 /* Returns a pointer to the `atexit' function. Note that if
6279 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
6280 `__cxa_atexit' function specified in the IA64 C++ ABI. */
6282 static tree
6283 get_atexit_node (void)
6285 tree atexit_fndecl;
6286 tree fn_type;
6287 tree fn_ptr_type;
6288 const char *name;
6289 bool use_aeabi_atexit;
6291 if (atexit_node)
6292 return atexit_node;
6294 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
6296 /* The declaration for `__cxa_atexit' is:
6298 int __cxa_atexit (void (*)(void *), void *, void *)
6300 We build up the argument types and then the function type
6301 itself. */
6302 tree argtype0, argtype1, argtype2;
6304 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
6305 /* First, build the pointer-to-function type for the first
6306 argument. */
6307 fn_ptr_type = get_atexit_fn_ptr_type ();
6308 /* Then, build the rest of the argument types. */
6309 argtype2 = ptr_type_node;
6310 if (use_aeabi_atexit)
6312 argtype1 = fn_ptr_type;
6313 argtype0 = ptr_type_node;
6315 else
6317 argtype1 = ptr_type_node;
6318 argtype0 = fn_ptr_type;
6320 /* And the final __cxa_atexit type. */
6321 fn_type = build_function_type_list (integer_type_node,
6322 argtype0, argtype1, argtype2,
6323 NULL_TREE);
6324 fn_ptr_type = build_pointer_type (fn_type);
6325 if (use_aeabi_atexit)
6326 name = "__aeabi_atexit";
6327 else
6328 name = "__cxa_atexit";
6330 else
6332 /* The declaration for `atexit' is:
6334 int atexit (void (*)());
6336 We build up the argument types and then the function type
6337 itself. */
6338 fn_ptr_type = get_atexit_fn_ptr_type ();
6339 /* Build the final atexit type. */
6340 fn_type = build_function_type_list (integer_type_node,
6341 fn_ptr_type, NULL_TREE);
6342 name = "atexit";
6345 /* Now, build the function declaration. */
6346 push_lang_context (lang_name_c);
6347 atexit_fndecl = build_library_fn_ptr (name, fn_type);
6348 mark_used (atexit_fndecl);
6349 pop_lang_context ();
6350 atexit_node = decay_conversion (atexit_fndecl);
6352 return atexit_node;
6355 /* Returns the __dso_handle VAR_DECL. */
6357 static tree
6358 get_dso_handle_node (void)
6360 if (dso_handle_node)
6361 return dso_handle_node;
6363 /* Declare the variable. */
6364 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
6365 ptr_type_node);
6367 return dso_handle_node;
6370 /* Begin a new function with internal linkage whose job will be simply
6371 to destroy some particular variable. */
6373 static GTY(()) int start_cleanup_cnt;
6375 static tree
6376 start_cleanup_fn (void)
6378 char name[32];
6379 tree fntype;
6380 tree fndecl;
6381 bool use_cxa_atexit = flag_use_cxa_atexit
6382 && !targetm.cxx.use_atexit_for_cxa_atexit ();
6384 push_to_top_level ();
6386 /* No need to mangle this. */
6387 push_lang_context (lang_name_c);
6389 /* Build the name of the function. */
6390 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
6391 /* Build the function declaration. */
6392 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
6393 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
6394 /* It's a function with internal linkage, generated by the
6395 compiler. */
6396 TREE_PUBLIC (fndecl) = 0;
6397 DECL_ARTIFICIAL (fndecl) = 1;
6398 /* Make the function `inline' so that it is only emitted if it is
6399 actually needed. It is unlikely that it will be inlined, since
6400 it is only called via a function pointer, but we avoid unnecessary
6401 emissions this way. */
6402 DECL_DECLARED_INLINE_P (fndecl) = 1;
6403 DECL_INTERFACE_KNOWN (fndecl) = 1;
6404 /* Build the parameter. */
6405 if (use_cxa_atexit)
6407 tree parmdecl;
6409 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
6410 DECL_CONTEXT (parmdecl) = fndecl;
6411 TREE_USED (parmdecl) = 1;
6412 DECL_READ_P (parmdecl) = 1;
6413 DECL_ARGUMENTS (fndecl) = parmdecl;
6416 pushdecl (fndecl);
6417 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
6419 pop_lang_context ();
6421 return current_function_decl;
6424 /* Finish the cleanup function begun by start_cleanup_fn. */
6426 static void
6427 end_cleanup_fn (void)
6429 expand_or_defer_fn (finish_function (0));
6431 pop_from_top_level ();
6434 /* Generate code to handle the destruction of DECL, an object with
6435 static storage duration. */
6437 tree
6438 register_dtor_fn (tree decl)
6440 tree cleanup;
6441 tree compound_stmt;
6442 tree fcall;
6443 tree type;
6444 bool use_dtor;
6445 tree arg0, arg1 = NULL_TREE, arg2 = NULL_TREE;
6447 type = TREE_TYPE (decl);
6448 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
6449 return void_zero_node;
6451 /* If we're using "__cxa_atexit" (or "__aeabi_atexit"), and DECL is
6452 a class object, we can just pass the destructor to
6453 "__cxa_atexit"; we don't have to build a temporary function to do
6454 the cleanup. */
6455 use_dtor = (flag_use_cxa_atexit
6456 && !targetm.cxx.use_atexit_for_cxa_atexit ()
6457 && CLASS_TYPE_P (type));
6458 if (use_dtor)
6460 int idx;
6462 /* Find the destructor. */
6463 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
6464 gcc_assert (idx >= 0);
6465 cleanup = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), idx);
6466 /* Make sure it is accessible. */
6467 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup);
6469 else
6471 /* Call build_cleanup before we enter the anonymous function so
6472 that any access checks will be done relative to the current
6473 scope, rather than the scope of the anonymous function. */
6474 build_cleanup (decl);
6476 /* Now start the function. */
6477 cleanup = start_cleanup_fn ();
6479 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
6480 to the original function, rather than the anonymous one. That
6481 will make the back end think that nested functions are in use,
6482 which causes confusion. */
6483 push_deferring_access_checks (dk_no_check);
6484 fcall = build_cleanup (decl);
6485 pop_deferring_access_checks ();
6487 /* Create the body of the anonymous function. */
6488 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
6489 finish_expr_stmt (fcall);
6490 finish_compound_stmt (compound_stmt);
6491 end_cleanup_fn ();
6494 /* Call atexit with the cleanup function. */
6495 mark_used (cleanup);
6496 cleanup = build_address (cleanup);
6497 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
6499 tree addr;
6501 if (use_dtor)
6503 /* We must convert CLEANUP to the type that "__cxa_atexit"
6504 expects. */
6505 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
6506 /* "__cxa_atexit" will pass the address of DECL to the
6507 cleanup function. */
6508 mark_used (decl);
6509 addr = build_address (decl);
6510 /* The declared type of the parameter to "__cxa_atexit" is
6511 "void *". For plain "T*", we could just let the
6512 machinery in cp_build_function_call convert it -- but if the
6513 type is "cv-qualified T *", then we need to convert it
6514 before passing it in, to avoid spurious errors. */
6515 addr = build_nop (ptr_type_node, addr);
6517 else
6518 /* Since the cleanup functions we build ignore the address
6519 they're given, there's no reason to pass the actual address
6520 in, and, in general, it's cheaper to pass NULL than any
6521 other value. */
6522 addr = null_pointer_node;
6523 arg2 = cp_build_addr_expr (get_dso_handle_node (),
6524 tf_warning_or_error);
6525 if (targetm.cxx.use_aeabi_atexit ())
6527 arg1 = cleanup;
6528 arg0 = addr;
6530 else
6532 arg1 = addr;
6533 arg0 = cleanup;
6536 else
6537 arg0 = cleanup;
6538 return cp_build_function_call_nary (get_atexit_node (), tf_warning_or_error,
6539 arg0, arg1, arg2, NULL_TREE);
6542 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
6543 is its initializer. Generate code to handle the construction
6544 and destruction of DECL. */
6546 static void
6547 expand_static_init (tree decl, tree init)
6549 gcc_assert (TREE_CODE (decl) == VAR_DECL);
6550 gcc_assert (TREE_STATIC (decl));
6552 /* Some variables require no dynamic initialization. */
6553 if (!init
6554 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
6555 return;
6557 if (DECL_FUNCTION_SCOPE_P (decl))
6559 /* Emit code to perform this initialization but once. */
6560 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
6561 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
6562 tree guard, guard_addr;
6563 tree acquire_fn, release_fn, abort_fn;
6564 tree flag, begin;
6566 /* Emit code to perform this initialization but once. This code
6567 looks like:
6569 static <type> guard;
6570 if (!guard.first_byte) {
6571 if (__cxa_guard_acquire (&guard)) {
6572 bool flag = false;
6573 try {
6574 // Do initialization.
6575 flag = true; __cxa_guard_release (&guard);
6576 // Register variable for destruction at end of program.
6577 } catch {
6578 if (!flag) __cxa_guard_abort (&guard);
6582 Note that the `flag' variable is only set to 1 *after* the
6583 initialization is complete. This ensures that an exception,
6584 thrown during the construction, will cause the variable to
6585 reinitialized when we pass through this code again, as per:
6587 [stmt.dcl]
6589 If the initialization exits by throwing an exception, the
6590 initialization is not complete, so it will be tried again
6591 the next time control enters the declaration.
6593 This process should be thread-safe, too; multiple threads
6594 should not be able to initialize the variable more than
6595 once. */
6597 /* Create the guard variable. */
6598 guard = get_guard (decl);
6600 /* This optimization isn't safe on targets with relaxed memory
6601 consistency. On such targets we force synchronization in
6602 __cxa_guard_acquire. */
6603 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
6605 /* Begin the conditional initialization. */
6606 if_stmt = begin_if_stmt ();
6607 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
6608 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
6611 if (flag_threadsafe_statics)
6613 guard_addr = build_address (guard);
6615 acquire_fn = get_identifier ("__cxa_guard_acquire");
6616 release_fn = get_identifier ("__cxa_guard_release");
6617 abort_fn = get_identifier ("__cxa_guard_abort");
6618 if (!get_global_value_if_present (acquire_fn, &acquire_fn))
6620 tree vfntype = build_function_type_list (void_type_node,
6621 TREE_TYPE (guard_addr),
6622 NULL_TREE);
6623 acquire_fn = push_library_fn
6624 (acquire_fn, build_function_type_list (integer_type_node,
6625 TREE_TYPE (guard_addr),
6626 NULL_TREE),
6627 NULL_TREE);
6628 release_fn = push_library_fn (release_fn, vfntype, NULL_TREE);
6629 abort_fn = push_library_fn (abort_fn, vfntype, NULL_TREE);
6631 else
6633 release_fn = identifier_global_value (release_fn);
6634 abort_fn = identifier_global_value (abort_fn);
6637 inner_if_stmt = begin_if_stmt ();
6638 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
6639 inner_if_stmt);
6641 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
6642 begin = get_target_expr (boolean_false_node);
6643 flag = TARGET_EXPR_SLOT (begin);
6645 TARGET_EXPR_CLEANUP (begin)
6646 = build3 (COND_EXPR, void_type_node, flag,
6647 void_zero_node,
6648 build_call_n (abort_fn, 1, guard_addr));
6649 CLEANUP_EH_ONLY (begin) = 1;
6651 /* Do the initialization itself. */
6652 init = add_stmt_to_compound (begin, init);
6653 init = add_stmt_to_compound
6654 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
6655 init = add_stmt_to_compound
6656 (init, build_call_n (release_fn, 1, guard_addr));
6658 else
6659 init = add_stmt_to_compound (init, set_guard (guard));
6661 /* Use atexit to register a function for destroying this static
6662 variable. */
6663 init = add_stmt_to_compound (init, register_dtor_fn (decl));
6665 finish_expr_stmt (init);
6667 if (flag_threadsafe_statics)
6669 finish_compound_stmt (inner_then_clause);
6670 finish_then_clause (inner_if_stmt);
6671 finish_if_stmt (inner_if_stmt);
6674 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
6676 finish_compound_stmt (then_clause);
6677 finish_then_clause (if_stmt);
6678 finish_if_stmt (if_stmt);
6681 else
6682 static_aggregates = tree_cons (init, decl, static_aggregates);
6686 /* Make TYPE a complete type based on INITIAL_VALUE.
6687 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6688 2 if there was no information (in which case assume 0 if DO_DEFAULT),
6689 3 if the initializer list is empty (in pedantic mode). */
6692 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
6694 int failure;
6695 tree type, elt_type;
6697 if (initial_value)
6699 unsigned HOST_WIDE_INT i;
6700 tree value;
6702 /* An array of character type can be initialized from a
6703 brace-enclosed string constant.
6705 FIXME: this code is duplicated from reshape_init. Probably
6706 we should just call reshape_init here? */
6707 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
6708 && TREE_CODE (initial_value) == CONSTRUCTOR
6709 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (initial_value)))
6711 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
6712 tree value = VEC_index (constructor_elt, v, 0)->value;
6714 if (TREE_CODE (value) == STRING_CST
6715 && VEC_length (constructor_elt, v) == 1)
6716 initial_value = value;
6719 /* If any of the elements are parameter packs, we can't actually
6720 complete this type now because the array size is dependent. */
6721 if (TREE_CODE (initial_value) == CONSTRUCTOR)
6723 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
6724 i, value)
6726 if (PACK_EXPANSION_P (value))
6727 return 0;
6732 failure = complete_array_type (ptype, initial_value, do_default);
6734 /* We can create the array before the element type is complete, which
6735 means that we didn't have these two bits set in the original type
6736 either. In completing the type, we are expected to propagate these
6737 bits. See also complete_type which does the same thing for arrays
6738 of fixed size. */
6739 type = *ptype;
6740 if (TYPE_DOMAIN (type))
6742 elt_type = TREE_TYPE (type);
6743 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
6744 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
6745 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
6748 return failure;
6751 /* As above, but either give an error or reject zero-size arrays, depending
6752 on COMPLAIN. */
6755 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
6756 bool do_default, tsubst_flags_t complain)
6758 int failure;
6759 bool sfinae = !(complain & tf_error);
6760 /* In SFINAE context we can't be lenient about zero-size arrays. */
6761 if (sfinae)
6762 ++pedantic;
6763 failure = cp_complete_array_type (ptype, initial_value, do_default);
6764 if (sfinae)
6765 --pedantic;
6766 if (failure)
6768 if (sfinae)
6769 /* Not an error. */;
6770 else if (failure == 1)
6771 error ("initializer fails to determine size of %qT", *ptype);
6772 else if (failure == 2)
6774 if (do_default)
6775 error ("array size missing in %qT", *ptype);
6777 else if (failure == 3)
6778 error ("zero-size array %qT", *ptype);
6779 *ptype = error_mark_node;
6781 return failure;
6784 /* Return zero if something is declared to be a member of type
6785 CTYPE when in the context of CUR_TYPE. STRING is the error
6786 message to print in that case. Otherwise, quietly return 1. */
6788 static int
6789 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
6791 if (ctype && ctype != cur_type)
6793 if (flags == DTOR_FLAG)
6794 error ("destructor for alien class %qT cannot be a member", ctype);
6795 else
6796 error ("constructor for alien class %qT cannot be a member", ctype);
6797 return 0;
6799 return 1;
6802 /* Subroutine of `grokdeclarator'. */
6804 /* Generate errors possibly applicable for a given set of specifiers.
6805 This is for ARM $7.1.2. */
6807 static void
6808 bad_specifiers (tree object,
6809 enum bad_spec_place type,
6810 int virtualp,
6811 int quals,
6812 int inlinep,
6813 int friendp,
6814 int raises)
6816 switch (type)
6818 case BSP_VAR:
6819 if (virtualp)
6820 error ("%qD declared as a %<virtual%> variable", object);
6821 if (inlinep)
6822 error ("%qD declared as an %<inline%> variable", object);
6823 if (quals)
6824 error ("%<const%> and %<volatile%> function specifiers on "
6825 "%qD invalid in variable declaration", object);
6826 break;
6827 case BSP_PARM:
6828 if (virtualp)
6829 error ("%qD declared as a %<virtual%> parameter", object);
6830 if (inlinep)
6831 error ("%qD declared as an %<inline%> parameter", object);
6832 if (quals)
6833 error ("%<const%> and %<volatile%> function specifiers on "
6834 "%qD invalid in parameter declaration", object);
6835 break;
6836 case BSP_TYPE:
6837 if (virtualp)
6838 error ("%qD declared as a %<virtual%> type", object);
6839 if (inlinep)
6840 error ("%qD declared as an %<inline%> type", object);
6841 if (quals)
6842 error ("%<const%> and %<volatile%> function specifiers on "
6843 "%qD invalid in type declaration", object);
6844 break;
6845 case BSP_FIELD:
6846 if (virtualp)
6847 error ("%qD declared as a %<virtual%> field", object);
6848 if (inlinep)
6849 error ("%qD declared as an %<inline%> field", object);
6850 if (quals)
6851 error ("%<const%> and %<volatile%> function specifiers on "
6852 "%qD invalid in field declaration", object);
6853 break;
6854 default:
6855 gcc_unreachable();
6857 if (friendp)
6858 error ("%q+D declared as a friend", object);
6859 if (raises
6860 && (TREE_CODE (object) == TYPE_DECL
6861 || (!TYPE_PTRFN_P (TREE_TYPE (object))
6862 && !TYPE_REFFN_P (TREE_TYPE (object))
6863 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
6864 error ("%q+D declared with an exception specification", object);
6867 /* DECL is a member function or static data member and is presently
6868 being defined. Check that the definition is taking place in a
6869 valid namespace. */
6871 static void
6872 check_class_member_definition_namespace (tree decl)
6874 /* These checks only apply to member functions and static data
6875 members. */
6876 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6877 || TREE_CODE (decl) == VAR_DECL);
6878 /* We check for problems with specializations in pt.c in
6879 check_specialization_namespace, where we can issue better
6880 diagnostics. */
6881 if (processing_specialization)
6882 return;
6883 /* There are no restrictions on the placement of
6884 explicit instantiations. */
6885 if (processing_explicit_instantiation)
6886 return;
6887 /* [class.mfct]
6889 A member function definition that appears outside of the
6890 class definition shall appear in a namespace scope enclosing
6891 the class definition.
6893 [class.static.data]
6895 The definition for a static data member shall appear in a
6896 namespace scope enclosing the member's class definition. */
6897 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
6898 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
6899 decl, DECL_CONTEXT (decl));
6902 /* Build a PARM_DECL for the "this" parameter. TYPE is the
6903 METHOD_TYPE for a non-static member function; QUALS are the
6904 cv-qualifiers that apply to the function. */
6906 tree
6907 build_this_parm (tree type, cp_cv_quals quals)
6909 tree this_type;
6910 tree qual_type;
6911 tree parm;
6912 cp_cv_quals this_quals;
6914 this_type = TREE_VALUE (TYPE_ARG_TYPES (type));
6915 /* The `this' parameter is implicitly `const'; it cannot be
6916 assigned to. */
6917 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
6918 qual_type = cp_build_qualified_type (this_type, this_quals);
6919 parm = build_artificial_parm (this_identifier, qual_type);
6920 cp_apply_type_quals_to_decl (this_quals, parm);
6921 return parm;
6924 /* CTYPE is class type, or null if non-class.
6925 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
6926 or METHOD_TYPE.
6927 DECLARATOR is the function's name.
6928 PARMS is a chain of PARM_DECLs for the function.
6929 VIRTUALP is truthvalue of whether the function is virtual or not.
6930 FLAGS are to be passed through to `grokclassfn'.
6931 QUALS are qualifiers indicating whether the function is `const'
6932 or `volatile'.
6933 RAISES is a list of exceptions that this function can raise.
6934 CHECK is 1 if we must find this method in CTYPE, 0 if we should
6935 not look, and -1 if we should not call `grokclassfn' at all.
6937 SFK is the kind of special function (if any) for the new function.
6939 Returns `NULL_TREE' if something goes wrong, after issuing
6940 applicable error messages. */
6942 static tree
6943 grokfndecl (tree ctype,
6944 tree type,
6945 tree declarator,
6946 tree parms,
6947 tree orig_declarator,
6948 int virtualp,
6949 enum overload_flags flags,
6950 cp_cv_quals quals,
6951 tree raises,
6952 int check,
6953 int friendp,
6954 int publicp,
6955 int inlinep,
6956 special_function_kind sfk,
6957 bool funcdef_flag,
6958 int template_count,
6959 tree in_namespace,
6960 tree* attrlist,
6961 location_t location)
6963 tree decl;
6964 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
6965 tree t;
6967 if (raises)
6968 type = build_exception_variant (type, raises);
6970 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
6972 /* If we have an explicit location, use it, otherwise use whatever
6973 build_lang_decl used (probably input_location). */
6974 if (location != UNKNOWN_LOCATION)
6975 DECL_SOURCE_LOCATION (decl) = location;
6977 if (TREE_CODE (type) == METHOD_TYPE)
6979 tree parm;
6980 parm = build_this_parm (type, quals);
6981 DECL_CHAIN (parm) = parms;
6982 parms = parm;
6984 DECL_ARGUMENTS (decl) = parms;
6985 for (t = parms; t; t = DECL_CHAIN (t))
6986 DECL_CONTEXT (t) = decl;
6987 /* Propagate volatile out from type to decl. */
6988 if (TYPE_VOLATILE (type))
6989 TREE_THIS_VOLATILE (decl) = 1;
6991 /* Setup decl according to sfk. */
6992 switch (sfk)
6994 case sfk_constructor:
6995 case sfk_copy_constructor:
6996 case sfk_move_constructor:
6997 DECL_CONSTRUCTOR_P (decl) = 1;
6998 break;
6999 case sfk_destructor:
7000 DECL_DESTRUCTOR_P (decl) = 1;
7001 break;
7002 default:
7003 break;
7006 /* If pointers to member functions use the least significant bit to
7007 indicate whether a function is virtual, ensure a pointer
7008 to this function will have that bit clear. */
7009 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
7010 && TREE_CODE (type) == METHOD_TYPE
7011 && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT)
7012 DECL_ALIGN (decl) = 2 * BITS_PER_UNIT;
7014 if (friendp
7015 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
7017 if (funcdef_flag)
7018 error
7019 ("defining explicit specialization %qD in friend declaration",
7020 orig_declarator);
7021 else
7023 tree fns = TREE_OPERAND (orig_declarator, 0);
7024 tree args = TREE_OPERAND (orig_declarator, 1);
7026 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7028 /* Something like `template <class T> friend void f<T>()'. */
7029 error ("invalid use of template-id %qD in declaration "
7030 "of primary template",
7031 orig_declarator);
7032 return NULL_TREE;
7036 /* A friend declaration of the form friend void f<>(). Record
7037 the information in the TEMPLATE_ID_EXPR. */
7038 SET_DECL_IMPLICIT_INSTANTIATION (decl);
7040 if (TREE_CODE (fns) == COMPONENT_REF)
7042 /* Due to bison parser ickiness, we will have already looked
7043 up an operator_name or PFUNCNAME within the current class
7044 (see template_id in parse.y). If the current class contains
7045 such a name, we'll get a COMPONENT_REF here. Undo that. */
7047 gcc_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
7048 == current_class_type);
7049 fns = TREE_OPERAND (fns, 1);
7051 gcc_assert (TREE_CODE (fns) == IDENTIFIER_NODE
7052 || TREE_CODE (fns) == OVERLOAD);
7053 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
7055 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
7056 if (TREE_PURPOSE (t)
7057 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
7059 error ("default arguments are not allowed in declaration "
7060 "of friend template specialization %qD",
7061 decl);
7062 return NULL_TREE;
7065 if (inlinep)
7067 error ("%<inline%> is not allowed in declaration of friend "
7068 "template specialization %qD",
7069 decl);
7070 return NULL_TREE;
7075 /* If this decl has namespace scope, set that up. */
7076 if (in_namespace)
7077 set_decl_namespace (decl, in_namespace, friendp);
7078 else if (!ctype)
7079 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
7081 /* `main' and builtins have implicit 'C' linkage. */
7082 if ((MAIN_NAME_P (declarator)
7083 || (IDENTIFIER_LENGTH (declarator) > 10
7084 && IDENTIFIER_POINTER (declarator)[0] == '_'
7085 && IDENTIFIER_POINTER (declarator)[1] == '_'
7086 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
7087 && current_lang_name == lang_name_cplusplus
7088 && ctype == NULL_TREE
7089 && DECL_FILE_SCOPE_P (decl))
7090 SET_DECL_LANGUAGE (decl, lang_c);
7092 /* Should probably propagate const out from type to decl I bet (mrs). */
7093 if (staticp)
7095 DECL_STATIC_FUNCTION_P (decl) = 1;
7096 DECL_CONTEXT (decl) = ctype;
7099 if (ctype)
7101 DECL_CONTEXT (decl) = ctype;
7102 if (funcdef_flag)
7103 check_class_member_definition_namespace (decl);
7106 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
7108 if (processing_template_decl)
7109 error ("cannot declare %<::main%> to be a template");
7110 if (inlinep)
7111 error ("cannot declare %<::main%> to be inline");
7112 if (!publicp)
7113 error ("cannot declare %<::main%> to be static");
7114 inlinep = 0;
7115 publicp = 1;
7118 /* Members of anonymous types and local classes have no linkage; make
7119 them internal. If a typedef is made later, this will be changed. */
7120 if (ctype && (TYPE_ANONYMOUS_P (ctype)
7121 || decl_function_context (TYPE_MAIN_DECL (ctype))))
7122 publicp = 0;
7124 if (publicp && cxx_dialect == cxx98)
7126 /* [basic.link]: A name with no linkage (notably, the name of a class
7127 or enumeration declared in a local scope) shall not be used to
7128 declare an entity with linkage.
7130 DR 757 relaxes this restriction for C++0x. */
7131 t = no_linkage_check (TREE_TYPE (decl),
7132 /*relaxed_p=*/false);
7133 if (t)
7135 if (TYPE_ANONYMOUS_P (t))
7137 if (DECL_EXTERN_C_P (decl))
7138 /* Allow this; it's pretty common in C. */;
7139 else
7141 permerror (input_location, "anonymous type with no linkage "
7142 "used to declare function %q#D with linkage",
7143 decl);
7144 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
7145 permerror (input_location, "%q+#D does not refer to the unqualified "
7146 "type, so it is not used for linkage",
7147 TYPE_NAME (t));
7150 else
7151 permerror (input_location, "type %qT with no linkage used to "
7152 "declare function %q#D with linkage", t, decl);
7156 TREE_PUBLIC (decl) = publicp;
7157 if (! publicp)
7159 DECL_INTERFACE_KNOWN (decl) = 1;
7160 DECL_NOT_REALLY_EXTERN (decl) = 1;
7163 /* If the declaration was declared inline, mark it as such. */
7164 if (inlinep)
7165 DECL_DECLARED_INLINE_P (decl) = 1;
7166 if (inlinep & 2)
7167 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7169 DECL_EXTERNAL (decl) = 1;
7170 if (quals && TREE_CODE (type) == FUNCTION_TYPE)
7172 error (ctype
7173 ? G_("static member function %qD cannot have cv-qualifier")
7174 : G_("non-member function %qD cannot have cv-qualifier"),
7175 decl);
7176 quals = TYPE_UNQUALIFIED;
7179 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
7180 && !grok_op_properties (decl, /*complain=*/true))
7181 return NULL_TREE;
7183 if (funcdef_flag)
7184 /* Make the init_value nonzero so pushdecl knows this is not
7185 tentative. error_mark_node is replaced later with the BLOCK. */
7186 DECL_INITIAL (decl) = error_mark_node;
7188 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
7189 TREE_NOTHROW (decl) = 1;
7191 /* Caller will do the rest of this. */
7192 if (check < 0)
7193 return decl;
7195 if (ctype != NULL_TREE)
7196 grokclassfn (ctype, decl, flags);
7198 decl = check_explicit_specialization (orig_declarator, decl,
7199 template_count,
7200 2 * funcdef_flag +
7201 4 * (friendp != 0));
7202 if (decl == error_mark_node)
7203 return NULL_TREE;
7205 if (attrlist)
7207 cplus_decl_attributes (&decl, *attrlist, 0);
7208 *attrlist = NULL_TREE;
7211 /* Check main's type after attributes have been applied. */
7212 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
7214 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
7215 integer_type_node))
7217 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
7218 tree newtype;
7219 error ("%<::main%> must return %<int%>");
7220 newtype = build_function_type (integer_type_node, oldtypeargs);
7221 TREE_TYPE (decl) = newtype;
7223 if (warn_main)
7224 check_main_parameter_types (decl);
7227 if (ctype != NULL_TREE
7228 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
7229 && check)
7231 tree old_decl = check_classfn (ctype, decl,
7232 (processing_template_decl
7233 > template_class_depth (ctype))
7234 ? current_template_parms
7235 : NULL_TREE);
7237 if (old_decl == error_mark_node)
7238 return NULL_TREE;
7240 if (old_decl)
7242 tree ok;
7243 tree pushed_scope;
7245 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
7246 /* Because grokfndecl is always supposed to return a
7247 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
7248 here. We depend on our callers to figure out that its
7249 really a template that's being returned. */
7250 old_decl = DECL_TEMPLATE_RESULT (old_decl);
7252 if (DECL_STATIC_FUNCTION_P (old_decl)
7253 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7254 /* Remove the `this' parm added by grokclassfn.
7255 XXX Isn't this done in start_function, too? */
7256 revert_static_member_fn (decl);
7257 if (DECL_ARTIFICIAL (old_decl))
7259 error ("definition of implicitly-declared %qD", old_decl);
7260 return NULL_TREE;
7263 /* Since we've smashed OLD_DECL to its
7264 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
7265 if (TREE_CODE (decl) == TEMPLATE_DECL)
7266 decl = DECL_TEMPLATE_RESULT (decl);
7268 /* Attempt to merge the declarations. This can fail, in
7269 the case of some invalid specialization declarations. */
7270 pushed_scope = push_scope (ctype);
7271 ok = duplicate_decls (decl, old_decl, friendp);
7272 if (pushed_scope)
7273 pop_scope (pushed_scope);
7274 if (!ok)
7276 error ("no %q#D member function declared in class %qT",
7277 decl, ctype);
7278 return NULL_TREE;
7280 return old_decl;
7284 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
7285 return NULL_TREE;
7287 if (ctype == NULL_TREE || check)
7288 return decl;
7290 if (virtualp)
7291 DECL_VIRTUAL_P (decl) = 1;
7293 return decl;
7296 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
7297 the linkage that DECL will receive in the object file. */
7299 static void
7300 set_linkage_for_static_data_member (tree decl)
7302 /* A static data member always has static storage duration and
7303 external linkage. Note that static data members are forbidden in
7304 local classes -- the only situation in which a class has
7305 non-external linkage. */
7306 TREE_PUBLIC (decl) = 1;
7307 TREE_STATIC (decl) = 1;
7308 /* For non-template classes, static data members are always put
7309 out in exactly those files where they are defined, just as
7310 with ordinary namespace-scope variables. */
7311 if (!processing_template_decl)
7312 DECL_INTERFACE_KNOWN (decl) = 1;
7315 /* Create a VAR_DECL named NAME with the indicated TYPE.
7317 If SCOPE is non-NULL, it is the class type or namespace containing
7318 the variable. If SCOPE is NULL, the variable should is created in
7319 the innermost enclosings scope. */
7321 static tree
7322 grokvardecl (tree type,
7323 tree name,
7324 const cp_decl_specifier_seq *declspecs,
7325 int initialized,
7326 int constp,
7327 tree scope)
7329 tree decl;
7330 tree explicit_scope;
7332 gcc_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE);
7334 /* Compute the scope in which to place the variable, but remember
7335 whether or not that scope was explicitly specified by the user. */
7336 explicit_scope = scope;
7337 if (!scope)
7339 /* An explicit "extern" specifier indicates a namespace-scope
7340 variable. */
7341 if (declspecs->storage_class == sc_extern)
7342 scope = current_decl_namespace ();
7343 else if (!at_function_scope_p ())
7344 scope = current_scope ();
7347 if (scope
7348 && (/* If the variable is a namespace-scope variable declared in a
7349 template, we need DECL_LANG_SPECIFIC. */
7350 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
7351 /* Similarly for namespace-scope variables with language linkage
7352 other than C++. */
7353 || (TREE_CODE (scope) == NAMESPACE_DECL
7354 && current_lang_name != lang_name_cplusplus)
7355 /* Similarly for static data members. */
7356 || TYPE_P (scope)))
7357 decl = build_lang_decl (VAR_DECL, name, type);
7358 else
7359 decl = build_decl (input_location, VAR_DECL, name, type);
7361 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
7362 set_decl_namespace (decl, explicit_scope, 0);
7363 else
7364 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
7366 if (declspecs->storage_class == sc_extern)
7368 DECL_THIS_EXTERN (decl) = 1;
7369 DECL_EXTERNAL (decl) = !initialized;
7372 if (DECL_CLASS_SCOPE_P (decl))
7374 set_linkage_for_static_data_member (decl);
7375 /* This function is only called with out-of-class definitions. */
7376 DECL_EXTERNAL (decl) = 0;
7377 check_class_member_definition_namespace (decl);
7379 /* At top level, either `static' or no s.c. makes a definition
7380 (perhaps tentative), and absence of `static' makes it public. */
7381 else if (toplevel_bindings_p ())
7383 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
7384 && (DECL_THIS_EXTERN (decl) || ! constp));
7385 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
7387 /* Not at top level, only `static' makes a static definition. */
7388 else
7390 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
7391 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
7394 if (declspecs->specs[(int)ds_thread])
7395 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
7397 /* If the type of the decl has no linkage, make sure that we'll
7398 notice that in mark_used. */
7399 if (cxx_dialect > cxx98
7400 && decl_linkage (decl) != lk_none
7401 && DECL_LANG_SPECIFIC (decl) == NULL
7402 && !DECL_EXTERN_C_P (decl)
7403 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
7404 retrofit_lang_decl (decl);
7406 if (TREE_PUBLIC (decl))
7408 /* [basic.link]: A name with no linkage (notably, the name of a class
7409 or enumeration declared in a local scope) shall not be used to
7410 declare an entity with linkage.
7412 DR 757 relaxes this restriction for C++0x. */
7413 tree t = (cxx_dialect > cxx98 ? NULL_TREE
7414 : no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false));
7415 if (t)
7417 if (TYPE_ANONYMOUS_P (t))
7419 if (DECL_EXTERN_C_P (decl))
7420 /* Allow this; it's pretty common in C. */
7422 else
7424 /* DRs 132, 319 and 389 seem to indicate types with
7425 no linkage can only be used to declare extern "C"
7426 entities. Since it's not always an error in the
7427 ISO C++ 90 Standard, we only issue a warning. */
7428 warning (0, "anonymous type with no linkage used to declare "
7429 "variable %q#D with linkage", decl);
7430 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
7431 warning (0, "%q+#D does not refer to the unqualified "
7432 "type, so it is not used for linkage",
7433 TYPE_NAME (t));
7436 else
7437 warning (0, "type %qT with no linkage used to declare variable "
7438 "%q#D with linkage", t, decl);
7441 else
7442 DECL_INTERFACE_KNOWN (decl) = 1;
7444 return decl;
7447 /* Create and return a canonical pointer to member function type, for
7448 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
7450 tree
7451 build_ptrmemfunc_type (tree type)
7453 tree field, fields;
7454 tree t;
7455 tree unqualified_variant = NULL_TREE;
7457 if (type == error_mark_node)
7458 return type;
7460 /* If a canonical type already exists for this type, use it. We use
7461 this method instead of type_hash_canon, because it only does a
7462 simple equality check on the list of field members. */
7464 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
7465 return t;
7467 /* Make sure that we always have the unqualified pointer-to-member
7468 type first. */
7469 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
7470 unqualified_variant
7471 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
7473 t = make_class_type (RECORD_TYPE);
7474 xref_basetypes (t, NULL_TREE);
7476 /* Let the front end know this is a pointer to member function... */
7477 TYPE_PTRMEMFUNC_FLAG (t) = 1;
7478 /* ... and not really a class type. */
7479 SET_CLASS_TYPE_P (t, 0);
7481 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
7482 fields = field;
7484 field = build_decl (input_location, FIELD_DECL, delta_identifier,
7485 delta_type_node);
7486 DECL_CHAIN (field) = fields;
7487 fields = field;
7489 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
7491 /* Zap out the name so that the back end will give us the debugging
7492 information for this anonymous RECORD_TYPE. */
7493 TYPE_NAME (t) = NULL_TREE;
7495 /* If this is not the unqualified form of this pointer-to-member
7496 type, set the TYPE_MAIN_VARIANT for this type to be the
7497 unqualified type. Since they are actually RECORD_TYPEs that are
7498 not variants of each other, we must do this manually.
7499 As we just built a new type there is no need to do yet another copy. */
7500 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
7502 int type_quals = cp_type_quals (type);
7503 TYPE_READONLY (t) = (type_quals & TYPE_QUAL_CONST) != 0;
7504 TYPE_VOLATILE (t) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
7505 TYPE_RESTRICT (t) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
7506 TYPE_MAIN_VARIANT (t) = unqualified_variant;
7507 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
7508 TYPE_NEXT_VARIANT (unqualified_variant) = t;
7509 TREE_TYPE (TYPE_BINFO (t)) = t;
7512 /* Cache this pointer-to-member type so that we can find it again
7513 later. */
7514 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
7516 if (TYPE_STRUCTURAL_EQUALITY_P (type))
7517 SET_TYPE_STRUCTURAL_EQUALITY (t);
7518 else if (TYPE_CANONICAL (type) != type)
7519 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
7521 return t;
7524 /* Create and return a pointer to data member type. */
7526 tree
7527 build_ptrmem_type (tree class_type, tree member_type)
7529 if (TREE_CODE (member_type) == METHOD_TYPE)
7531 cp_cv_quals quals = type_memfn_quals (member_type);
7532 member_type = build_memfn_type (member_type, class_type, quals);
7533 return build_ptrmemfunc_type (build_pointer_type (member_type));
7535 else
7537 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
7538 return build_offset_type (class_type, member_type);
7542 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
7543 Check to see that the definition is valid. Issue appropriate error
7544 messages. Return 1 if the definition is particularly bad, or 0
7545 otherwise. */
7548 check_static_variable_definition (tree decl, tree type)
7550 /* If DECL is declared constexpr, we'll do the appropriate checks
7551 in check_initializer. */
7552 if (DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
7553 return 0;
7554 else if (cxx_dialect >= cxx0x && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7556 if (literal_type_p (type))
7557 error ("%<constexpr%> needed for in-class initialization of static "
7558 "data member %q#D of non-integral type", decl);
7559 else
7560 error ("in-class initialization of static data member %q#D of "
7561 "non-literal type", decl);
7562 return 1;
7565 /* Motion 10 at San Diego: If a static const integral data member is
7566 initialized with an integral constant expression, the initializer
7567 may appear either in the declaration (within the class), or in
7568 the definition, but not both. If it appears in the class, the
7569 member is a member constant. The file-scope definition is always
7570 required. */
7571 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
7573 error ("invalid in-class initialization of static data member "
7574 "of non-integral type %qT",
7575 type);
7576 return 1;
7578 else if (!CP_TYPE_CONST_P (type))
7579 error ("ISO C++ forbids in-class initialization of non-const "
7580 "static member %qD",
7581 decl);
7582 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7583 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids initialization of member constant "
7584 "%qD of non-integral type %qT", decl, type);
7586 return 0;
7589 /* Given the SIZE (i.e., number of elements) in an array, compute an
7590 appropriate index type for the array. If non-NULL, NAME is the
7591 name of the thing being declared. */
7593 tree
7594 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
7596 tree type;
7597 tree itype;
7598 tree osize = size;
7599 tree abi_1_itype = NULL_TREE;
7601 if (error_operand_p (size))
7602 return error_mark_node;
7604 type = TREE_TYPE (size);
7605 /* type_dependent_expression_p? */
7606 if (!dependent_type_p (type))
7608 mark_rvalue_use (size);
7610 if (cxx_dialect < cxx0x && TREE_CODE (size) == NOP_EXPR
7611 && TREE_SIDE_EFFECTS (size))
7612 /* In C++98, we mark a non-constant array bound with a magic
7613 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
7614 else
7616 size = fold_non_dependent_expr (size);
7618 if (CLASS_TYPE_P (type)
7619 && CLASSTYPE_LITERAL_P (type))
7621 size = build_expr_type_conversion (WANT_INT, size, true);
7622 if (size == error_mark_node)
7623 return error_mark_node;
7624 type = TREE_TYPE (size);
7625 /* We didn't support this case in GCC 3.2, so don't bother
7626 trying to model it now in ABI v1. */
7627 abi_1_itype = error_mark_node;
7630 size = maybe_constant_value (size);
7631 if (!TREE_CONSTANT (size))
7632 size = osize;
7635 if (error_operand_p (size))
7636 return error_mark_node;
7638 /* The array bound must be an integer type. */
7639 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
7641 if (!(complain & tf_error))
7642 return error_mark_node;
7643 if (name)
7644 error ("size of array %qD has non-integral type %qT", name, type);
7645 else
7646 error ("size of array has non-integral type %qT", type);
7647 size = integer_one_node;
7648 type = TREE_TYPE (size);
7652 /* A type is dependent if it is...an array type constructed from any
7653 dependent type or whose size is specified by a constant expression
7654 that is value-dependent. */
7655 /* We can only call value_dependent_expression_p on integral constant
7656 expressions; treat non-constant expressions as dependent, too. */
7657 if (processing_template_decl
7658 && (dependent_type_p (type)
7659 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
7661 /* We cannot do any checking for a SIZE that isn't known to be
7662 constant. Just build the index type and mark that it requires
7663 structural equality checks. */
7664 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
7665 size, integer_one_node));
7666 TYPE_DEPENDENT_P (itype) = 1;
7667 TYPE_DEPENDENT_P_VALID (itype) = 1;
7668 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7669 return itype;
7672 if (!abi_version_at_least (2) && processing_template_decl
7673 && abi_1_itype == NULL_TREE)
7674 /* For abi-1, we handled all instances in templates the same way,
7675 even when they were non-dependent. This affects the manglings
7676 produced. So, we do the normal checking for non-dependent
7677 sizes, but at the end we'll return the same type that abi-1
7678 would have, but with TYPE_CANONICAL set to the "right"
7679 value that the current ABI would provide. */
7680 abi_1_itype = build_index_type (build_min (MINUS_EXPR, sizetype,
7681 osize, integer_one_node));
7683 /* Normally, the array-bound will be a constant. */
7684 if (TREE_CODE (size) == INTEGER_CST)
7686 /* Check to see if the array bound overflowed. Make that an
7687 error, no matter how generous we're being. */
7688 constant_expression_error (size);
7690 /* An array must have a positive number of elements. */
7691 if (INT_CST_LT (size, integer_zero_node))
7693 if (!(complain & tf_error))
7694 return error_mark_node;
7695 if (name)
7696 error ("size of array %qD is negative", name);
7697 else
7698 error ("size of array is negative");
7699 size = integer_one_node;
7701 /* As an extension we allow zero-sized arrays. */
7702 else if (integer_zerop (size))
7704 if (!(complain & tf_error))
7705 /* We must fail if performing argument deduction (as
7706 indicated by the state of complain), so that
7707 another substitution can be found. */
7708 return error_mark_node;
7709 else if (in_system_header)
7710 /* Allow them in system headers because glibc uses them. */;
7711 else if (name)
7712 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids zero-size array %qD", name);
7713 else
7714 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids zero-size array");
7717 else if (TREE_CONSTANT (size)
7718 /* We don't allow VLAs at non-function scopes, or during
7719 tentative template substitution. */
7720 || !at_function_scope_p () || !(complain & tf_error))
7722 if (!(complain & tf_error))
7723 return error_mark_node;
7724 /* `(int) &fn' is not a valid array bound. */
7725 if (name)
7726 error ("size of array %qD is not an integral constant-expression",
7727 name);
7728 else
7729 error ("size of array is not an integral constant-expression");
7730 size = integer_one_node;
7732 else if (pedantic && warn_vla != 0)
7734 if (name)
7735 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
7736 else
7737 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
7739 else if (warn_vla > 0)
7741 if (name)
7742 warning (OPT_Wvla,
7743 "variable length array %qD is used", name);
7744 else
7745 warning (OPT_Wvla,
7746 "variable length array is used");
7749 if (processing_template_decl && !TREE_CONSTANT (size))
7750 /* A variable sized array. */
7751 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
7752 else
7754 HOST_WIDE_INT saved_processing_template_decl;
7756 /* Compute the index of the largest element in the array. It is
7757 one less than the number of elements in the array. We save
7758 and restore PROCESSING_TEMPLATE_DECL so that computations in
7759 cp_build_binary_op will be appropriately folded. */
7760 saved_processing_template_decl = processing_template_decl;
7761 processing_template_decl = 0;
7762 itype = cp_build_binary_op (input_location,
7763 MINUS_EXPR,
7764 cp_convert (ssizetype, size),
7765 cp_convert (ssizetype, integer_one_node),
7766 tf_warning_or_error);
7767 itype = fold (itype);
7768 processing_template_decl = saved_processing_template_decl;
7770 if (!TREE_CONSTANT (itype))
7772 /* A variable sized array. */
7773 if (TREE_SIDE_EFFECTS (itype))
7774 /* Use get_temp_regvar rather than variable_size here so that
7775 people walking expressions that use a variable of this type
7776 don't walk into this expression. */
7777 itype = get_temp_regvar (TREE_TYPE (itype), itype);
7778 else
7779 itype = variable_size (itype);
7781 /* Make sure that there was no overflow when creating to a signed
7782 index type. (For example, on a 32-bit machine, an array with
7783 size 2^32 - 1 is too big.) */
7784 else if (TREE_CODE (itype) == INTEGER_CST
7785 && TREE_OVERFLOW (itype))
7787 if (!(complain & tf_error))
7788 return error_mark_node;
7789 error ("overflow in array dimension");
7790 TREE_OVERFLOW (itype) = 0;
7794 /* Create and return the appropriate index type. */
7795 if (abi_1_itype && abi_1_itype != error_mark_node)
7797 tree t = build_index_type (itype);
7798 TYPE_CANONICAL (abi_1_itype) = TYPE_CANONICAL (t);
7799 itype = abi_1_itype;
7801 else
7802 itype = build_index_type (itype);
7804 /* If the index type were dependent, we would have returned early, so
7805 remember that it isn't. */
7806 TYPE_DEPENDENT_P (itype) = 0;
7807 TYPE_DEPENDENT_P_VALID (itype) = 1;
7808 return itype;
7811 /* Returns the scope (if any) in which the entity declared by
7812 DECLARATOR will be located. If the entity was declared with an
7813 unqualified name, NULL_TREE is returned. */
7815 tree
7816 get_scope_of_declarator (const cp_declarator *declarator)
7818 while (declarator && declarator->kind != cdk_id)
7819 declarator = declarator->declarator;
7821 /* If the declarator-id is a SCOPE_REF, the scope in which the
7822 declaration occurs is the first operand. */
7823 if (declarator
7824 && declarator->u.id.qualifying_scope)
7825 return declarator->u.id.qualifying_scope;
7827 /* Otherwise, the declarator is not a qualified name; the entity will
7828 be declared in the current scope. */
7829 return NULL_TREE;
7832 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
7833 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
7834 with this type. */
7836 static tree
7837 create_array_type_for_decl (tree name, tree type, tree size)
7839 tree itype = NULL_TREE;
7841 /* If things have already gone awry, bail now. */
7842 if (type == error_mark_node || size == error_mark_node)
7843 return error_mark_node;
7845 /* If there are some types which cannot be array elements,
7846 issue an error-message and return. */
7847 switch (TREE_CODE (type))
7849 case VOID_TYPE:
7850 if (name)
7851 error ("declaration of %qD as array of void", name);
7852 else
7853 error ("creating array of void");
7854 return error_mark_node;
7856 case FUNCTION_TYPE:
7857 if (name)
7858 error ("declaration of %qD as array of functions", name);
7859 else
7860 error ("creating array of functions");
7861 return error_mark_node;
7863 case REFERENCE_TYPE:
7864 if (name)
7865 error ("declaration of %qD as array of references", name);
7866 else
7867 error ("creating array of references");
7868 return error_mark_node;
7870 case METHOD_TYPE:
7871 if (name)
7872 error ("declaration of %qD as array of function members", name);
7873 else
7874 error ("creating array of function members");
7875 return error_mark_node;
7877 default:
7878 break;
7881 /* [dcl.array]
7883 The constant expressions that specify the bounds of the arrays
7884 can be omitted only for the first member of the sequence. */
7885 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
7887 if (name)
7888 error ("declaration of %qD as multidimensional array must "
7889 "have bounds for all dimensions except the first",
7890 name);
7891 else
7892 error ("multidimensional array must have bounds for all "
7893 "dimensions except the first");
7895 return error_mark_node;
7898 /* Figure out the index type for the array. */
7899 if (size)
7900 itype = compute_array_index_type (name, size, tf_warning_or_error);
7902 /* [dcl.array]
7903 T is called the array element type; this type shall not be [...] an
7904 abstract class type. */
7905 abstract_virtuals_error (name, type);
7907 return build_cplus_array_type (type, itype);
7910 /* Check that it's OK to declare a function with the indicated TYPE.
7911 SFK indicates the kind of special function (if any) that this
7912 function is. OPTYPE is the type given in a conversion operator
7913 declaration, or the class type for a constructor/destructor.
7914 Returns the actual return type of the function; that
7915 may be different than TYPE if an error occurs, or for certain
7916 special functions. */
7918 static tree
7919 check_special_function_return_type (special_function_kind sfk,
7920 tree type,
7921 tree optype)
7923 switch (sfk)
7925 case sfk_constructor:
7926 if (type)
7927 error ("return type specification for constructor invalid");
7929 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
7930 type = build_pointer_type (optype);
7931 else
7932 type = void_type_node;
7933 break;
7935 case sfk_destructor:
7936 if (type)
7937 error ("return type specification for destructor invalid");
7938 /* We can't use the proper return type here because we run into
7939 problems with ambiguous bases and covariant returns.
7940 Java classes are left unchanged because (void *) isn't a valid
7941 Java type, and we don't want to change the Java ABI. */
7942 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
7943 type = build_pointer_type (void_type_node);
7944 else
7945 type = void_type_node;
7946 break;
7948 case sfk_conversion:
7949 if (type)
7950 error ("return type specified for %<operator %T%>", optype);
7951 type = optype;
7952 break;
7954 default:
7955 gcc_unreachable ();
7958 return type;
7961 /* A variable or data member (whose unqualified name is IDENTIFIER)
7962 has been declared with the indicated TYPE. If the TYPE is not
7963 acceptable, issue an error message and return a type to use for
7964 error-recovery purposes. */
7966 tree
7967 check_var_type (tree identifier, tree type)
7969 if (VOID_TYPE_P (type))
7971 if (!identifier)
7972 error ("unnamed variable or field declared void");
7973 else if (TREE_CODE (identifier) == IDENTIFIER_NODE)
7975 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
7976 error ("variable or field %qE declared void", identifier);
7978 else
7979 error ("variable or field declared void");
7980 type = error_mark_node;
7983 return type;
7986 /* Given declspecs and a declarator (abstract or otherwise), determine
7987 the name and type of the object declared and construct a DECL node
7988 for it.
7990 DECLSPECS points to the representation of declaration-specifier
7991 sequence that precedes declarator.
7993 DECL_CONTEXT says which syntactic context this declaration is in:
7994 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
7995 FUNCDEF for a function definition. Like NORMAL but a few different
7996 error messages in each case. Return value may be zero meaning
7997 this definition is too screwy to try to parse.
7998 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
7999 handle member functions (which have FIELD context).
8000 Return value may be zero meaning this definition is too screwy to
8001 try to parse.
8002 PARM for a parameter declaration (either within a function prototype
8003 or before a function body). Make a PARM_DECL, or return void_type_node.
8004 TPARM for a template parameter declaration.
8005 CATCHPARM for a parameter declaration before a catch clause.
8006 TYPENAME if for a typename (in a cast or sizeof).
8007 Don't make a DECL node; just return the ..._TYPE node.
8008 FIELD for a struct or union field; make a FIELD_DECL.
8009 BITFIELD for a field with specified width.
8011 INITIALIZED is as for start_decl.
8013 ATTRLIST is a pointer to the list of attributes, which may be NULL
8014 if there are none; *ATTRLIST may be modified if attributes from inside
8015 the declarator should be applied to the declaration.
8017 When this function is called, scoping variables (such as
8018 CURRENT_CLASS_TYPE) should reflect the scope in which the
8019 declaration occurs, not the scope in which the new declaration will
8020 be placed. For example, on:
8022 void S::f() { ... }
8024 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
8025 should not be `S'.
8027 Returns a DECL (if a declarator is present), a TYPE (if there is no
8028 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
8029 error occurs. */
8031 tree
8032 grokdeclarator (const cp_declarator *declarator,
8033 const cp_decl_specifier_seq *declspecs,
8034 enum decl_context decl_context,
8035 int initialized,
8036 tree* attrlist)
8038 tree type = NULL_TREE;
8039 int longlong = 0;
8040 int explicit_int128 = 0;
8041 int virtualp, explicitp, friendp, inlinep, staticp;
8042 int explicit_int = 0;
8043 int explicit_char = 0;
8044 int defaulted_int = 0;
8045 tree dependent_name = NULL_TREE;
8047 tree typedef_decl = NULL_TREE;
8048 const char *name = NULL;
8049 tree typedef_type = NULL_TREE;
8050 /* True if this declarator is a function definition. */
8051 bool funcdef_flag = false;
8052 cp_declarator_kind innermost_code = cdk_error;
8053 int bitfield = 0;
8054 #if 0
8055 /* See the code below that used this. */
8056 tree decl_attr = NULL_TREE;
8057 #endif
8059 /* Keep track of what sort of function is being processed
8060 so that we can warn about default return values, or explicit
8061 return values which do not match prescribed defaults. */
8062 special_function_kind sfk = sfk_none;
8064 tree dname = NULL_TREE;
8065 tree ctor_return_type = NULL_TREE;
8066 enum overload_flags flags = NO_SPECIAL;
8067 /* cv-qualifiers that apply to the declarator, for a declaration of
8068 a member function. */
8069 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
8070 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
8071 int type_quals;
8072 tree raises = NULL_TREE;
8073 int template_count = 0;
8074 tree returned_attrs = NULL_TREE;
8075 tree parms = NULL_TREE;
8076 const cp_declarator *id_declarator;
8077 /* The unqualified name of the declarator; either an
8078 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
8079 tree unqualified_id;
8080 /* The class type, if any, in which this entity is located,
8081 or NULL_TREE if none. Note that this value may be different from
8082 the current class type; for example if an attempt is made to declare
8083 "A::f" inside "B", this value will be "A". */
8084 tree ctype = current_class_type;
8085 /* The NAMESPACE_DECL for the namespace in which this entity is
8086 located. If an unqualified name is used to declare the entity,
8087 this value will be NULL_TREE, even if the entity is located at
8088 namespace scope. */
8089 tree in_namespace = NULL_TREE;
8090 cp_storage_class storage_class;
8091 bool unsigned_p, signed_p, short_p, long_p, thread_p;
8092 bool type_was_error_mark_node = false;
8093 bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
8094 bool template_type_arg = false;
8095 bool template_parm_flag = false;
8096 bool constexpr_p = declspecs->specs[(int) ds_constexpr];
8097 const char *errmsg;
8099 signed_p = declspecs->specs[(int)ds_signed];
8100 unsigned_p = declspecs->specs[(int)ds_unsigned];
8101 short_p = declspecs->specs[(int)ds_short];
8102 long_p = declspecs->specs[(int)ds_long];
8103 longlong = declspecs->specs[(int)ds_long] >= 2;
8104 explicit_int128 = declspecs->explicit_int128_p;
8105 thread_p = declspecs->specs[(int)ds_thread];
8107 if (decl_context == FUNCDEF)
8108 funcdef_flag = true, decl_context = NORMAL;
8109 else if (decl_context == MEMFUNCDEF)
8110 funcdef_flag = true, decl_context = FIELD;
8111 else if (decl_context == BITFIELD)
8112 bitfield = 1, decl_context = FIELD;
8113 else if (decl_context == TEMPLATE_TYPE_ARG)
8114 template_type_arg = true, decl_context = TYPENAME;
8115 else if (decl_context == TPARM)
8116 template_parm_flag = true, decl_context = PARM;
8118 if (initialized > 1)
8119 funcdef_flag = true;
8121 /* Look inside a declarator for the name being declared
8122 and get it as a string, for an error message. */
8123 for (id_declarator = declarator;
8124 id_declarator;
8125 id_declarator = id_declarator->declarator)
8127 if (id_declarator->kind != cdk_id)
8128 innermost_code = id_declarator->kind;
8130 switch (id_declarator->kind)
8132 case cdk_function:
8133 if (id_declarator->declarator
8134 && id_declarator->declarator->kind == cdk_id)
8136 sfk = id_declarator->declarator->u.id.sfk;
8137 if (sfk == sfk_destructor)
8138 flags = DTOR_FLAG;
8140 break;
8142 case cdk_id:
8144 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
8145 tree decl = id_declarator->u.id.unqualified_name;
8146 if (!decl)
8147 break;
8148 if (qualifying_scope)
8150 if (at_function_scope_p ())
8152 /* [dcl.meaning]
8154 A declarator-id shall not be qualified except
8155 for ...
8157 None of the cases are permitted in block
8158 scope. */
8159 if (qualifying_scope == global_namespace)
8160 error ("invalid use of qualified-name %<::%D%>",
8161 decl);
8162 else if (TYPE_P (qualifying_scope))
8163 error ("invalid use of qualified-name %<%T::%D%>",
8164 qualifying_scope, decl);
8165 else
8166 error ("invalid use of qualified-name %<%D::%D%>",
8167 qualifying_scope, decl);
8168 return error_mark_node;
8170 else if (TYPE_P (qualifying_scope))
8172 ctype = qualifying_scope;
8173 if (innermost_code != cdk_function
8174 && current_class_type
8175 && !UNIQUELY_DERIVED_FROM_P (ctype,
8176 current_class_type))
8178 error ("type %qT is not derived from type %qT",
8179 ctype, current_class_type);
8180 return error_mark_node;
8183 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
8184 in_namespace = qualifying_scope;
8186 switch (TREE_CODE (decl))
8188 case BIT_NOT_EXPR:
8190 tree type;
8192 if (innermost_code != cdk_function)
8194 error ("declaration of %qD as non-function", decl);
8195 return error_mark_node;
8197 else if (!qualifying_scope
8198 && !(current_class_type && at_class_scope_p ()))
8200 error ("declaration of %qD as non-member", decl);
8201 return error_mark_node;
8204 type = TREE_OPERAND (decl, 0);
8205 if (TYPE_P (type))
8206 type = constructor_name (type);
8207 name = identifier_to_locale (IDENTIFIER_POINTER (type));
8208 dname = decl;
8210 break;
8212 case TEMPLATE_ID_EXPR:
8214 tree fns = TREE_OPERAND (decl, 0);
8216 dname = fns;
8217 if (TREE_CODE (dname) != IDENTIFIER_NODE)
8219 gcc_assert (is_overloaded_fn (dname));
8220 dname = DECL_NAME (get_first_fn (dname));
8223 /* Fall through. */
8225 case IDENTIFIER_NODE:
8226 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8227 dname = decl;
8229 if (C_IS_RESERVED_WORD (dname))
8231 error ("declarator-id missing; using reserved word %qD",
8232 dname);
8233 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
8235 else if (!IDENTIFIER_TYPENAME_P (dname))
8236 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
8237 else
8239 gcc_assert (flags == NO_SPECIAL);
8240 flags = TYPENAME_FLAG;
8241 ctor_return_type = TREE_TYPE (dname);
8242 sfk = sfk_conversion;
8243 if (is_typename_at_global_scope (dname))
8244 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
8245 else
8246 name = "<invalid operator>";
8248 break;
8250 default:
8251 gcc_unreachable ();
8253 break;
8256 case cdk_array:
8257 case cdk_pointer:
8258 case cdk_reference:
8259 case cdk_ptrmem:
8260 break;
8262 case cdk_error:
8263 return error_mark_node;
8265 default:
8266 gcc_unreachable ();
8268 if (id_declarator->kind == cdk_id)
8269 break;
8272 /* [dcl.fct.edf]
8274 The declarator in a function-definition shall have the form
8275 D1 ( parameter-declaration-clause) ... */
8276 if (funcdef_flag && innermost_code != cdk_function)
8278 error ("function definition does not declare parameters");
8279 return error_mark_node;
8282 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
8283 && innermost_code != cdk_function
8284 && ! (ctype && !declspecs->any_specifiers_p))
8286 error ("declaration of %qD as non-function", dname);
8287 return error_mark_node;
8290 /* Anything declared one level down from the top level
8291 must be one of the parameters of a function
8292 (because the body is at least two levels down). */
8294 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
8295 by not allowing C++ class definitions to specify their parameters
8296 with xdecls (must be spec.d in the parmlist).
8298 Since we now wait to push a class scope until we are sure that
8299 we are in a legitimate method context, we must set oldcname
8300 explicitly (since current_class_name is not yet alive).
8302 We also want to avoid calling this a PARM if it is in a namespace. */
8304 if (decl_context == NORMAL && !toplevel_bindings_p ())
8306 struct cp_binding_level *b = current_binding_level;
8307 current_binding_level = b->level_chain;
8308 if (current_binding_level != 0 && toplevel_bindings_p ())
8309 decl_context = PARM;
8310 current_binding_level = b;
8313 if (name == NULL)
8314 name = decl_context == PARM ? "parameter" : "type name";
8316 if (constexpr_p && declspecs->specs[(int)ds_typedef])
8318 error ("%<constexpr%> cannot appear in a typedef declaration");
8319 return error_mark_node;
8322 /* If there were multiple types specified in the decl-specifier-seq,
8323 issue an error message. */
8324 if (declspecs->multiple_types_p)
8326 error ("two or more data types in declaration of %qs", name);
8327 return error_mark_node;
8330 if (declspecs->conflicting_specifiers_p)
8332 error ("conflicting specifiers in declaration of %qs", name);
8333 return error_mark_node;
8336 /* Extract the basic type from the decl-specifier-seq. */
8337 type = declspecs->type;
8338 if (type == error_mark_node)
8340 type = NULL_TREE;
8341 type_was_error_mark_node = true;
8343 /* If the entire declaration is itself tagged as deprecated then
8344 suppress reports of deprecated items. */
8345 if (type && TREE_DEPRECATED (type)
8346 && deprecated_state != DEPRECATED_SUPPRESS)
8347 warn_deprecated_use (type, NULL_TREE);
8348 if (type && TREE_CODE (type) == TYPE_DECL)
8350 typedef_decl = type;
8351 type = TREE_TYPE (typedef_decl);
8352 if (TREE_DEPRECATED (type)
8353 && DECL_ARTIFICIAL (typedef_decl)
8354 && deprecated_state != DEPRECATED_SUPPRESS)
8355 warn_deprecated_use (type, NULL_TREE);
8357 /* No type at all: default to `int', and set DEFAULTED_INT
8358 because it was not a user-defined typedef. */
8359 if (type == NULL_TREE && (signed_p || unsigned_p || long_p || short_p))
8361 /* These imply 'int'. */
8362 type = integer_type_node;
8363 defaulted_int = 1;
8365 /* Gather flags. */
8366 explicit_int = declspecs->explicit_int_p;
8367 explicit_char = declspecs->explicit_char_p;
8369 #if 0
8370 /* See the code below that used this. */
8371 if (typedef_decl)
8372 decl_attr = DECL_ATTRIBUTES (typedef_decl);
8373 #endif
8374 typedef_type = type;
8377 if (sfk != sfk_conversion)
8378 ctor_return_type = ctype;
8380 if (sfk != sfk_none)
8381 type = check_special_function_return_type (sfk, type,
8382 ctor_return_type);
8383 else if (type == NULL_TREE)
8385 int is_main;
8387 explicit_int = -1;
8389 /* We handle `main' specially here, because 'main () { }' is so
8390 common. With no options, it is allowed. With -Wreturn-type,
8391 it is a warning. It is only an error with -pedantic-errors. */
8392 is_main = (funcdef_flag
8393 && dname && TREE_CODE (dname) == IDENTIFIER_NODE
8394 && MAIN_NAME_P (dname)
8395 && ctype == NULL_TREE
8396 && in_namespace == NULL_TREE
8397 && current_namespace == global_namespace);
8399 if (type_was_error_mark_node)
8400 /* We've already issued an error, don't complain more. */;
8401 else if (in_system_header || flag_ms_extensions)
8402 /* Allow it, sigh. */;
8403 else if (! is_main)
8404 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
8405 else if (pedantic)
8406 pedwarn (input_location, OPT_pedantic,
8407 "ISO C++ forbids declaration of %qs with no type", name);
8408 else
8409 warning (OPT_Wreturn_type,
8410 "ISO C++ forbids declaration of %qs with no type", name);
8412 type = integer_type_node;
8415 ctype = NULL_TREE;
8417 /* Now process the modifiers that were specified
8418 and check for invalid combinations. */
8420 /* Long double is a special combination. */
8421 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
8423 long_p = false;
8424 type = cp_build_qualified_type (long_double_type_node,
8425 cp_type_quals (type));
8428 /* Check all other uses of type modifiers. */
8430 if (unsigned_p || signed_p || long_p || short_p)
8432 int ok = 0;
8434 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
8435 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
8436 else if (signed_p && unsigned_p)
8437 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
8438 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
8439 error ("%<long long%> invalid for %qs", name);
8440 else if (explicit_int128 && TREE_CODE (type) != INTEGER_TYPE)
8441 error ("%<__int128%> invalid for %qs", name);
8442 else if (long_p && TREE_CODE (type) == REAL_TYPE)
8443 error ("%<long%> invalid for %qs", name);
8444 else if (short_p && TREE_CODE (type) == REAL_TYPE)
8445 error ("%<short%> invalid for %qs", name);
8446 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
8447 error ("%<long%> or %<short%> invalid for %qs", name);
8448 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_int128)
8449 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
8450 else if ((long_p || short_p) && explicit_char)
8451 error ("%<long%> or %<short%> specified with char for %qs", name);
8452 else if (long_p && short_p)
8453 error ("%<long%> and %<short%> specified together for %qs", name);
8454 else if (type == char16_type_node || type == char32_type_node)
8456 if (signed_p || unsigned_p)
8457 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
8458 else if (short_p || long_p)
8459 error ("%<short%> or %<long%> invalid for %qs", name);
8461 else
8463 ok = 1;
8464 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_int128 && pedantic)
8466 pedwarn (input_location, OPT_pedantic,
8467 "long, short, signed or unsigned used invalidly for %qs",
8468 name);
8469 if (flag_pedantic_errors)
8470 ok = 0;
8472 if (explicit_int128)
8474 if (int128_integer_type_node == NULL_TREE)
8476 error ("%<__int128%> is not supported by this target");
8477 ok = 0;
8479 else if (pedantic)
8481 pedwarn (input_location, OPT_pedantic,
8482 "ISO C++ does not support %<__int128%> for %qs",
8483 name);
8484 if (flag_pedantic_errors)
8485 ok = 0;
8490 /* Discard the type modifiers if they are invalid. */
8491 if (! ok)
8493 unsigned_p = false;
8494 signed_p = false;
8495 long_p = false;
8496 short_p = false;
8497 longlong = 0;
8498 explicit_int128 = false;
8502 /* Decide whether an integer type is signed or not.
8503 Optionally treat bitfields as signed by default. */
8504 if (unsigned_p
8505 /* [class.bit]
8507 It is implementation-defined whether a plain (neither
8508 explicitly signed or unsigned) char, short, int, or long
8509 bit-field is signed or unsigned.
8511 Naturally, we extend this to long long as well. Note that
8512 this does not include wchar_t. */
8513 || (bitfield && !flag_signed_bitfields
8514 && !signed_p
8515 /* A typedef for plain `int' without `signed' can be
8516 controlled just like plain `int', but a typedef for
8517 `signed int' cannot be so controlled. */
8518 && !(typedef_decl
8519 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
8520 && TREE_CODE (type) == INTEGER_TYPE
8521 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
8523 if (explicit_int128)
8524 type = int128_unsigned_type_node;
8525 else if (longlong)
8526 type = long_long_unsigned_type_node;
8527 else if (long_p)
8528 type = long_unsigned_type_node;
8529 else if (short_p)
8530 type = short_unsigned_type_node;
8531 else if (type == char_type_node)
8532 type = unsigned_char_type_node;
8533 else if (typedef_decl)
8534 type = unsigned_type_for (type);
8535 else
8536 type = unsigned_type_node;
8538 else if (signed_p && type == char_type_node)
8539 type = signed_char_type_node;
8540 else if (explicit_int128)
8541 type = int128_integer_type_node;
8542 else if (longlong)
8543 type = long_long_integer_type_node;
8544 else if (long_p)
8545 type = long_integer_type_node;
8546 else if (short_p)
8547 type = short_integer_type_node;
8549 if (declspecs->specs[(int)ds_complex])
8551 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
8552 error ("complex invalid for %qs", name);
8553 /* If we just have "complex", it is equivalent to
8554 "complex double", but if any modifiers at all are specified it is
8555 the complex form of TYPE. E.g, "complex short" is
8556 "complex short int". */
8557 else if (defaulted_int && ! longlong && ! explicit_int128
8558 && ! (long_p || short_p || signed_p || unsigned_p))
8559 type = complex_double_type_node;
8560 else if (type == integer_type_node)
8561 type = complex_integer_type_node;
8562 else if (type == float_type_node)
8563 type = complex_float_type_node;
8564 else if (type == double_type_node)
8565 type = complex_double_type_node;
8566 else if (type == long_double_type_node)
8567 type = complex_long_double_type_node;
8568 else
8569 type = build_complex_type (type);
8572 type_quals = TYPE_UNQUALIFIED;
8573 if (declspecs->specs[(int)ds_const])
8574 type_quals |= TYPE_QUAL_CONST;
8575 if (declspecs->specs[(int)ds_volatile])
8576 type_quals |= TYPE_QUAL_VOLATILE;
8577 if (declspecs->specs[(int)ds_restrict])
8578 type_quals |= TYPE_QUAL_RESTRICT;
8579 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
8580 error ("qualifiers are not allowed on declaration of %<operator %T%>",
8581 ctor_return_type);
8583 type_quals |= cp_type_quals (type);
8584 type = cp_build_qualified_type_real
8585 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
8586 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
8587 /* We might have ignored or rejected some of the qualifiers. */
8588 type_quals = cp_type_quals (type);
8590 staticp = 0;
8591 inlinep = !! declspecs->specs[(int)ds_inline];
8592 virtualp = !! declspecs->specs[(int)ds_virtual];
8593 explicitp = !! declspecs->specs[(int)ds_explicit];
8595 storage_class = declspecs->storage_class;
8596 if (storage_class == sc_static)
8597 staticp = 1 + (decl_context == FIELD);
8599 if (virtualp && staticp == 2)
8601 error ("member %qD cannot be declared both virtual and static", dname);
8602 storage_class = sc_none;
8603 staticp = 0;
8605 friendp = !! declspecs->specs[(int)ds_friend];
8607 if (dependent_name && !friendp)
8609 error ("%<%T::%D%> is not a valid declarator", ctype, dependent_name);
8610 return error_mark_node;
8613 /* Issue errors about use of storage classes for parameters. */
8614 if (decl_context == PARM)
8616 if (declspecs->specs[(int)ds_typedef])
8618 error ("typedef declaration invalid in parameter declaration");
8619 return error_mark_node;
8621 else if (template_parm_flag && storage_class != sc_none)
8623 error ("storage class specified for template parameter %qs", name);
8624 return error_mark_node;
8626 else if (storage_class == sc_static
8627 || storage_class == sc_extern
8628 || thread_p)
8629 error ("storage class specifiers invalid in parameter declarations");
8631 if (type_uses_auto (type))
8633 error ("parameter declared %<auto%>");
8634 type = error_mark_node;
8637 /* Function parameters cannot be constexpr. If we saw one, moan
8638 and pretend it wasn't there. */
8639 if (constexpr_p)
8641 error ("a parameter cannot be declared %<constexpr%>");
8642 constexpr_p = 0;
8646 /* Give error if `virtual' is used outside of class declaration. */
8647 if (virtualp
8648 && (current_class_name == NULL_TREE || decl_context != FIELD))
8650 error ("%<virtual%> outside class declaration");
8651 virtualp = 0;
8654 /* Static anonymous unions are dealt with here. */
8655 if (staticp && decl_context == TYPENAME
8656 && declspecs->type
8657 && ANON_AGGR_TYPE_P (declspecs->type))
8658 decl_context = FIELD;
8660 /* Warn about storage classes that are invalid for certain
8661 kinds of declarations (parameters, typenames, etc.). */
8662 if (thread_p
8663 && ((storage_class
8664 && storage_class != sc_extern
8665 && storage_class != sc_static)
8666 || declspecs->specs[(int)ds_typedef]))
8668 error ("multiple storage classes in declaration of %qs", name);
8669 thread_p = false;
8671 if (decl_context != NORMAL
8672 && ((storage_class != sc_none
8673 && storage_class != sc_mutable)
8674 || thread_p))
8676 if ((decl_context == PARM || decl_context == CATCHPARM)
8677 && (storage_class == sc_register
8678 || storage_class == sc_auto))
8680 else if (declspecs->specs[(int)ds_typedef])
8682 else if (decl_context == FIELD
8683 /* C++ allows static class elements. */
8684 && storage_class == sc_static)
8685 /* C++ also allows inlines and signed and unsigned elements,
8686 but in those cases we don't come in here. */
8688 else
8690 if (decl_context == FIELD)
8691 error ("storage class specified for %qs", name);
8692 else
8694 if (decl_context == PARM || decl_context == CATCHPARM)
8695 error ("storage class specified for parameter %qs", name);
8696 else
8697 error ("storage class specified for typename");
8699 if (storage_class == sc_register
8700 || storage_class == sc_auto
8701 || storage_class == sc_extern
8702 || thread_p)
8703 storage_class = sc_none;
8706 else if (storage_class == sc_extern && funcdef_flag
8707 && ! toplevel_bindings_p ())
8708 error ("nested function %qs declared %<extern%>", name);
8709 else if (toplevel_bindings_p ())
8711 if (storage_class == sc_auto)
8712 error ("top-level declaration of %qs specifies %<auto%>", name);
8714 else if (thread_p
8715 && storage_class != sc_extern
8716 && storage_class != sc_static)
8718 error ("function-scope %qs implicitly auto and declared %<__thread%>",
8719 name);
8720 thread_p = false;
8723 if (storage_class && friendp)
8725 error ("storage class specifiers invalid in friend function declarations");
8726 storage_class = sc_none;
8727 staticp = 0;
8730 if (!id_declarator)
8731 unqualified_id = NULL_TREE;
8732 else
8734 unqualified_id = id_declarator->u.id.unqualified_name;
8735 switch (TREE_CODE (unqualified_id))
8737 case BIT_NOT_EXPR:
8738 unqualified_id = TREE_OPERAND (unqualified_id, 0);
8739 if (TYPE_P (unqualified_id))
8740 unqualified_id = constructor_name (unqualified_id);
8741 break;
8743 case IDENTIFIER_NODE:
8744 case TEMPLATE_ID_EXPR:
8745 break;
8747 default:
8748 gcc_unreachable ();
8752 /* Determine the type of the entity declared by recurring on the
8753 declarator. */
8754 for (; declarator; declarator = declarator->declarator)
8756 const cp_declarator *inner_declarator;
8757 tree attrs;
8759 if (type == error_mark_node)
8760 return error_mark_node;
8762 attrs = declarator->attributes;
8763 if (attrs)
8765 int attr_flags;
8767 attr_flags = 0;
8768 if (declarator == NULL || declarator->kind == cdk_id)
8769 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
8770 if (declarator->kind == cdk_function)
8771 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
8772 if (declarator->kind == cdk_array)
8773 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
8774 returned_attrs = decl_attributes (&type,
8775 chainon (returned_attrs, attrs),
8776 attr_flags);
8779 if (declarator->kind == cdk_id)
8780 break;
8782 inner_declarator = declarator->declarator;
8784 switch (declarator->kind)
8786 case cdk_array:
8787 type = create_array_type_for_decl (dname, type,
8788 declarator->u.array.bounds);
8789 break;
8791 case cdk_function:
8793 tree arg_types;
8794 int funcdecl_p;
8796 /* Declaring a function type.
8797 Make sure we have a valid type for the function to return. */
8799 if (type_quals != TYPE_UNQUALIFIED)
8801 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
8802 warning (OPT_Wignored_qualifiers,
8803 "type qualifiers ignored on function return type");
8804 /* We now know that the TYPE_QUALS don't apply to the
8805 decl, but to its return type. */
8806 type_quals = TYPE_UNQUALIFIED;
8808 errmsg = targetm.invalid_return_type (type);
8809 if (errmsg)
8811 error (errmsg);
8812 type = integer_type_node;
8815 /* Error about some types functions can't return. */
8817 if (TREE_CODE (type) == FUNCTION_TYPE)
8819 error ("%qs declared as function returning a function", name);
8820 return error_mark_node;
8822 if (TREE_CODE (type) == ARRAY_TYPE)
8824 error ("%qs declared as function returning an array", name);
8825 return error_mark_node;
8828 /* Pick up type qualifiers which should be applied to `this'. */
8829 memfn_quals = declarator->u.function.qualifiers;
8831 /* Pick up the exception specifications. */
8832 raises = declarator->u.function.exception_specification;
8834 /* Say it's a definition only for the CALL_EXPR
8835 closest to the identifier. */
8836 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
8838 /* Handle a late-specified return type. */
8839 if (funcdecl_p)
8841 if (type_uses_auto (type))
8843 if (!declarator->u.function.late_return_type)
8845 error ("%qs function uses %<auto%> type specifier without"
8846 " late return type", name);
8847 return error_mark_node;
8849 else if (!is_auto (type))
8851 error ("%qs function with late return type has"
8852 " %qT as its type rather than plain %<auto%>",
8853 name, type);
8854 return error_mark_node;
8857 else if (declarator->u.function.late_return_type)
8859 error ("%qs function with late return type not declared"
8860 " with %<auto%> type specifier", name);
8861 return error_mark_node;
8864 type = splice_late_return_type
8865 (type, declarator->u.function.late_return_type);
8866 if (type == error_mark_node)
8867 return error_mark_node;
8869 if (ctype == NULL_TREE
8870 && decl_context == FIELD
8871 && funcdecl_p
8872 && (friendp == 0 || dname == current_class_name))
8873 ctype = current_class_type;
8875 if (ctype && (sfk == sfk_constructor
8876 || sfk == sfk_destructor))
8878 /* We are within a class's scope. If our declarator name
8879 is the same as the class name, and we are defining
8880 a function, then it is a constructor/destructor, and
8881 therefore returns a void type. */
8883 /* ISO C++ 12.4/2. A destructor may not be declared
8884 const or volatile. A destructor may not be
8885 static.
8887 ISO C++ 12.1. A constructor may not be declared
8888 const or volatile. A constructor may not be
8889 virtual. A constructor may not be static. */
8890 if (staticp == 2)
8891 error ((flags == DTOR_FLAG)
8892 ? "destructor cannot be static member function"
8893 : "constructor cannot be static member function");
8894 if (memfn_quals)
8896 error ((flags == DTOR_FLAG)
8897 ? "destructors may not be cv-qualified"
8898 : "constructors may not be cv-qualified");
8899 memfn_quals = TYPE_UNQUALIFIED;
8902 if (decl_context == FIELD
8903 && !member_function_or_else (ctype,
8904 current_class_type,
8905 flags))
8906 return error_mark_node;
8908 if (flags != DTOR_FLAG)
8910 /* It's a constructor. */
8911 if (explicitp == 1)
8912 explicitp = 2;
8913 if (virtualp)
8915 permerror (input_location, "constructors cannot be declared virtual");
8916 virtualp = 0;
8918 if (decl_context == FIELD
8919 && sfk != sfk_constructor)
8920 return error_mark_node;
8922 if (decl_context == FIELD)
8923 staticp = 0;
8925 else if (friendp)
8927 if (initialized)
8928 error ("can%'t initialize friend function %qs", name);
8929 if (virtualp)
8931 /* Cannot be both friend and virtual. */
8932 error ("virtual functions cannot be friends");
8933 friendp = 0;
8935 if (decl_context == NORMAL)
8936 error ("friend declaration not in class definition");
8937 if (current_function_decl && funcdef_flag)
8938 error ("can%'t define friend function %qs in a local "
8939 "class definition",
8940 name);
8942 else if (ctype && sfk == sfk_conversion)
8944 if (explicitp == 1)
8946 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
8947 explicitp = 2;
8951 arg_types = grokparms (declarator->u.function.parameters,
8952 &parms);
8954 if (inner_declarator
8955 && inner_declarator->kind == cdk_id
8956 && inner_declarator->u.id.sfk == sfk_destructor
8957 && arg_types != void_list_node)
8959 error ("destructors may not have parameters");
8960 arg_types = void_list_node;
8961 parms = NULL_TREE;
8964 type = build_function_type (type, arg_types);
8966 break;
8968 case cdk_pointer:
8969 case cdk_reference:
8970 case cdk_ptrmem:
8971 /* Filter out pointers-to-references and references-to-references.
8972 We can get these if a TYPE_DECL is used. */
8974 if (TREE_CODE (type) == REFERENCE_TYPE)
8976 if (declarator->kind != cdk_reference)
8978 error ("cannot declare pointer to %q#T", type);
8979 type = TREE_TYPE (type);
8982 /* In C++0x, we allow reference to reference declarations
8983 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
8984 and template type arguments [14.3.1/4 temp.arg.type]. The
8985 check for direct reference to reference declarations, which
8986 are still forbidden, occurs below. Reasoning behind the change
8987 can be found in DR106, DR540, and the rvalue reference
8988 proposals. */
8989 else if (cxx_dialect == cxx98)
8991 error ("cannot declare reference to %q#T", type);
8992 type = TREE_TYPE (type);
8995 else if (VOID_TYPE_P (type))
8997 if (declarator->kind == cdk_reference)
8998 error ("cannot declare reference to %q#T", type);
8999 else if (declarator->kind == cdk_ptrmem)
9000 error ("cannot declare pointer to %q#T member", type);
9003 /* We now know that the TYPE_QUALS don't apply to the decl,
9004 but to the target of the pointer. */
9005 type_quals = TYPE_UNQUALIFIED;
9007 if (declarator->kind == cdk_ptrmem
9008 && (TREE_CODE (type) == FUNCTION_TYPE
9009 || (memfn_quals && TREE_CODE (type) == METHOD_TYPE)))
9011 memfn_quals |= type_memfn_quals (type);
9012 type = build_memfn_type (type,
9013 declarator->u.pointer.class_type,
9014 memfn_quals);
9015 if (type == error_mark_node)
9016 return error_mark_node;
9017 memfn_quals = TYPE_UNQUALIFIED;
9020 if (TREE_CODE (type) == FUNCTION_TYPE
9021 && type_memfn_quals (type) != TYPE_UNQUALIFIED)
9022 error (declarator->kind == cdk_reference
9023 ? G_("cannot declare reference to qualified function type %qT")
9024 : G_("cannot declare pointer to qualified function type %qT"),
9025 type);
9027 /* When the pointed-to type involves components of variable size,
9028 care must be taken to ensure that the size evaluation code is
9029 emitted early enough to dominate all the possible later uses
9030 and late enough for the variables on which it depends to have
9031 been assigned.
9033 This is expected to happen automatically when the pointed-to
9034 type has a name/declaration of it's own, but special attention
9035 is required if the type is anonymous.
9037 We handle the NORMAL and FIELD contexts here by inserting a
9038 dummy statement that just evaluates the size at a safe point
9039 and ensures it is not deferred until e.g. within a deeper
9040 conditional context (c++/43555).
9042 We expect nothing to be needed here for PARM or TYPENAME.
9043 Evaluating the size at this point for TYPENAME would
9044 actually be incorrect, as we might be in the middle of an
9045 expression with side effects on the pointed-to type size
9046 "arguments" prior to the pointer declaration point and the
9047 size evaluation could end up prior to the side effects. */
9049 if (!TYPE_NAME (type)
9050 && (decl_context == NORMAL || decl_context == FIELD)
9051 && at_function_scope_p ()
9052 && variably_modified_type_p (type, NULL_TREE))
9053 finish_expr_stmt (TYPE_SIZE (type));
9055 if (declarator->kind == cdk_reference)
9057 /* In C++0x, the type we are creating a reference to might be
9058 a typedef which is itself a reference type. In that case,
9059 we follow the reference collapsing rules in
9060 [7.1.3/8 dcl.typedef] to create the final reference type:
9062 "If a typedef TD names a type that is a reference to a type
9063 T, an attempt to create the type 'lvalue reference to cv TD'
9064 creates the type 'lvalue reference to T,' while an attempt
9065 to create the type "rvalue reference to cv TD' creates the
9066 type TD."
9068 if (!VOID_TYPE_P (type))
9069 type = cp_build_reference_type
9070 ((TREE_CODE (type) == REFERENCE_TYPE
9071 ? TREE_TYPE (type) : type),
9072 (declarator->u.reference.rvalue_ref
9073 && (TREE_CODE(type) != REFERENCE_TYPE
9074 || TYPE_REF_IS_RVALUE (type))));
9076 /* In C++0x, we need this check for direct reference to
9077 reference declarations, which are forbidden by
9078 [8.3.2/5 dcl.ref]. Reference to reference declarations
9079 are only allowed indirectly through typedefs and template
9080 type arguments. Example:
9082 void foo(int & &); // invalid ref-to-ref decl
9084 typedef int & int_ref;
9085 void foo(int_ref &); // valid ref-to-ref decl
9087 if (inner_declarator && inner_declarator->kind == cdk_reference)
9088 error ("cannot declare reference to %q#T, which is not "
9089 "a typedef or a template type argument", type);
9091 else if (TREE_CODE (type) == METHOD_TYPE)
9092 type = build_ptrmemfunc_type (build_pointer_type (type));
9093 else if (declarator->kind == cdk_ptrmem)
9095 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
9096 != NAMESPACE_DECL);
9097 if (declarator->u.pointer.class_type == error_mark_node)
9098 /* We will already have complained. */
9099 type = error_mark_node;
9100 else
9101 type = build_ptrmem_type (declarator->u.pointer.class_type,
9102 type);
9104 else
9105 type = build_pointer_type (type);
9107 /* Process a list of type modifier keywords (such as
9108 const or volatile) that were given inside the `*' or `&'. */
9110 if (declarator->u.pointer.qualifiers)
9112 type
9113 = cp_build_qualified_type (type,
9114 declarator->u.pointer.qualifiers);
9115 type_quals = cp_type_quals (type);
9117 ctype = NULL_TREE;
9118 break;
9120 case cdk_error:
9121 break;
9123 default:
9124 gcc_unreachable ();
9128 /* A `constexpr' specifier used in an object declaration declares
9129 the object as `const'. */
9130 if (constexpr_p && innermost_code != cdk_function)
9132 if (type_quals & TYPE_QUAL_CONST)
9133 error ("both %<const%> and %<constexpr%> cannot be used here");
9134 if (type_quals & TYPE_QUAL_VOLATILE)
9135 error ("both %<volatile%> and %<constexpr%> cannot be used here");
9136 type_quals |= TYPE_QUAL_CONST;
9137 type = cp_build_qualified_type (type, type_quals);
9140 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
9141 && TREE_CODE (type) != FUNCTION_TYPE
9142 && TREE_CODE (type) != METHOD_TYPE)
9144 error ("template-id %qD used as a declarator",
9145 unqualified_id);
9146 unqualified_id = dname;
9149 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
9150 qualified with a class-name, turn it into a METHOD_TYPE, unless
9151 we know that the function is static. We take advantage of this
9152 opportunity to do other processing that pertains to entities
9153 explicitly declared to be class members. Note that if DECLARATOR
9154 is non-NULL, we know it is a cdk_id declarator; otherwise, we
9155 would not have exited the loop above. */
9156 if (declarator
9157 && declarator->u.id.qualifying_scope
9158 && TYPE_P (declarator->u.id.qualifying_scope))
9160 tree t;
9162 ctype = declarator->u.id.qualifying_scope;
9163 ctype = TYPE_MAIN_VARIANT (ctype);
9164 t = ctype;
9165 while (t != NULL_TREE && CLASS_TYPE_P (t))
9167 /* You're supposed to have one `template <...>' for every
9168 template class, but you don't need one for a full
9169 specialization. For example:
9171 template <class T> struct S{};
9172 template <> struct S<int> { void f(); };
9173 void S<int>::f () {}
9175 is correct; there shouldn't be a `template <>' for the
9176 definition of `S<int>::f'. */
9177 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)
9178 && !any_dependent_template_arguments_p (CLASSTYPE_TI_ARGS (t)))
9179 /* T is an explicit (not partial) specialization. All
9180 containing classes must therefore also be explicitly
9181 specialized. */
9182 break;
9183 if ((CLASSTYPE_USE_TEMPLATE (t) || CLASSTYPE_IS_TEMPLATE (t))
9184 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
9185 template_count += 1;
9187 t = TYPE_MAIN_DECL (t);
9188 t = DECL_CONTEXT (t);
9191 if (ctype == current_class_type)
9193 if (friendp)
9195 permerror (input_location, "member functions are implicitly friends of their class");
9196 friendp = 0;
9198 else
9199 permerror (declarator->id_loc,
9200 "extra qualification %<%T::%> on member %qs",
9201 ctype, name);
9203 else if (/* If the qualifying type is already complete, then we
9204 can skip the following checks. */
9205 !COMPLETE_TYPE_P (ctype)
9206 && (/* If the function is being defined, then
9207 qualifying type must certainly be complete. */
9208 funcdef_flag
9209 /* A friend declaration of "T::f" is OK, even if
9210 "T" is a template parameter. But, if this
9211 function is not a friend, the qualifying type
9212 must be a class. */
9213 || (!friendp && !CLASS_TYPE_P (ctype))
9214 /* For a declaration, the type need not be
9215 complete, if either it is dependent (since there
9216 is no meaningful definition of complete in that
9217 case) or the qualifying class is currently being
9218 defined. */
9219 || !(dependent_type_p (ctype)
9220 || currently_open_class (ctype)))
9221 /* Check that the qualifying type is complete. */
9222 && !complete_type_or_else (ctype, NULL_TREE))
9223 return error_mark_node;
9224 else if (TREE_CODE (type) == FUNCTION_TYPE)
9226 if (current_class_type
9227 && (!friendp || funcdef_flag))
9229 error (funcdef_flag
9230 ? "cannot define member function %<%T::%s%> within %<%T%>"
9231 : "cannot declare member function %<%T::%s%> within %<%T%>",
9232 ctype, name, current_class_type);
9233 return error_mark_node;
9236 else if (declspecs->specs[(int)ds_typedef]
9237 && current_class_type)
9239 error ("cannot declare member %<%T::%s%> within %qT",
9240 ctype, name, current_class_type);
9241 return error_mark_node;
9245 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
9246 ctype = current_class_type;
9248 /* A constexpr non-static member function is implicitly const. */
9249 if (constexpr_p && ctype && staticp == 0
9250 && TREE_CODE (type) == FUNCTION_TYPE
9251 && sfk != sfk_constructor && sfk != sfk_destructor)
9252 memfn_quals |= TYPE_QUAL_CONST;
9254 /* Now TYPE has the actual type. */
9256 if (returned_attrs)
9258 if (attrlist)
9259 *attrlist = chainon (returned_attrs, *attrlist);
9260 else
9261 attrlist = &returned_attrs;
9264 /* Handle parameter packs. */
9265 if (parameter_pack_p)
9267 if (decl_context == PARM)
9268 /* Turn the type into a pack expansion.*/
9269 type = make_pack_expansion (type);
9270 else
9271 error ("non-parameter %qs cannot be a parameter pack", name);
9274 /* Did array size calculations overflow? */
9276 if (TREE_CODE (type) == ARRAY_TYPE
9277 && COMPLETE_TYPE_P (type)
9278 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9279 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
9281 error ("size of array %qs is too large", name);
9282 /* If we proceed with the array type as it is, we'll eventually
9283 crash in tree_low_cst(). */
9284 type = error_mark_node;
9287 if ((decl_context == FIELD || decl_context == PARM)
9288 && !processing_template_decl
9289 && variably_modified_type_p (type, NULL_TREE))
9291 if (decl_context == FIELD)
9292 error ("data member may not have variably modified type %qT", type);
9293 else
9294 error ("parameter may not have variably modified type %qT", type);
9295 type = error_mark_node;
9298 if (explicitp == 1 || (explicitp && friendp))
9300 /* [dcl.fct.spec] The explicit specifier shall only be used in
9301 declarations of constructors within a class definition. */
9302 error ("only declarations of constructors can be %<explicit%>");
9303 explicitp = 0;
9306 if (storage_class == sc_mutable)
9308 if (decl_context != FIELD || friendp)
9310 error ("non-member %qs cannot be declared %<mutable%>", name);
9311 storage_class = sc_none;
9313 else if (decl_context == TYPENAME || declspecs->specs[(int)ds_typedef])
9315 error ("non-object member %qs cannot be declared %<mutable%>", name);
9316 storage_class = sc_none;
9318 else if (TREE_CODE (type) == FUNCTION_TYPE
9319 || TREE_CODE (type) == METHOD_TYPE)
9321 error ("function %qs cannot be declared %<mutable%>", name);
9322 storage_class = sc_none;
9324 else if (staticp)
9326 error ("static %qs cannot be declared %<mutable%>", name);
9327 storage_class = sc_none;
9329 else if (type_quals & TYPE_QUAL_CONST)
9331 error ("const %qs cannot be declared %<mutable%>", name);
9332 storage_class = sc_none;
9334 else if (TREE_CODE (type) == REFERENCE_TYPE)
9336 permerror (input_location, "reference %qs cannot be declared "
9337 "%<mutable%>", name);
9338 storage_class = sc_none;
9342 /* If this is declaring a typedef name, return a TYPE_DECL. */
9343 if (declspecs->specs[(int)ds_typedef] && decl_context != TYPENAME)
9345 tree decl;
9347 /* Note that the grammar rejects storage classes
9348 in typenames, fields or parameters. */
9349 if (current_lang_name == lang_name_java)
9350 TYPE_FOR_JAVA (type) = 1;
9352 /* This declaration:
9354 typedef void f(int) const;
9356 declares a function type which is not a member of any
9357 particular class, but which is cv-qualified; for
9358 example "f S::*" declares a pointer to a const-qualified
9359 member function of S. We record the cv-qualification in the
9360 function type. */
9361 if (memfn_quals && TREE_CODE (type) == FUNCTION_TYPE)
9363 type = apply_memfn_quals (type, memfn_quals);
9365 /* We have now dealt with these qualifiers. */
9366 memfn_quals = TYPE_UNQUALIFIED;
9369 if (decl_context == FIELD)
9370 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
9371 else
9372 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
9373 if (id_declarator && declarator->u.id.qualifying_scope) {
9374 error_at (DECL_SOURCE_LOCATION (decl),
9375 "typedef name may not be a nested-name-specifier");
9376 TREE_TYPE (decl) = error_mark_node;
9379 if (decl_context != FIELD)
9381 if (!current_function_decl)
9382 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
9383 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
9384 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
9385 (current_function_decl)))
9386 /* The TYPE_DECL is "abstract" because there will be
9387 clones of this constructor/destructor, and there will
9388 be copies of this TYPE_DECL generated in those
9389 clones. */
9390 DECL_ABSTRACT (decl) = 1;
9392 else if (constructor_name_p (unqualified_id, current_class_type))
9393 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
9394 "as enclosing class",
9395 unqualified_id);
9397 /* If the user declares "typedef struct {...} foo" then the
9398 struct will have an anonymous name. Fill that name in now.
9399 Nothing can refer to it, so nothing needs know about the name
9400 change. */
9401 if (type != error_mark_node
9402 && unqualified_id
9403 && TYPE_NAME (type)
9404 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9405 && TYPE_ANONYMOUS_P (type)
9406 && cp_type_quals (type) == TYPE_UNQUALIFIED)
9408 tree t;
9410 /* Replace the anonymous name with the real name everywhere. */
9411 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9413 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
9414 /* We do not rename the debug info representing the
9415 anonymous tagged type because the standard says in
9416 [dcl.typedef] that the naming applies only for
9417 linkage purposes. */
9418 /*debug_hooks->set_name (t, decl);*/
9419 TYPE_NAME (t) = decl;
9422 if (TYPE_LANG_SPECIFIC (type))
9423 TYPE_WAS_ANONYMOUS (type) = 1;
9425 /* If this is a typedef within a template class, the nested
9426 type is a (non-primary) template. The name for the
9427 template needs updating as well. */
9428 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
9429 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
9430 = TYPE_IDENTIFIER (type);
9432 /* Adjust linkage now that we aren't anonymous anymore. */
9433 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
9434 determine_visibility (TYPE_MAIN_DECL (type));
9436 /* FIXME remangle member functions; member functions of a
9437 type with external linkage have external linkage. */
9440 if (signed_p
9441 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
9442 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
9444 bad_specifiers (decl, BSP_TYPE, virtualp,
9445 memfn_quals != TYPE_UNQUALIFIED,
9446 inlinep, friendp, raises != NULL_TREE);
9448 return decl;
9451 /* Detect the case of an array type of unspecified size
9452 which came, as such, direct from a typedef name.
9453 We must copy the type, so that the array's domain can be
9454 individually set by the object's initializer. */
9456 if (type && typedef_type
9457 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9458 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
9459 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
9461 /* Detect where we're using a typedef of function type to declare a
9462 function. PARMS will not be set, so we must create it now. */
9464 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
9466 tree decls = NULL_TREE;
9467 tree args;
9469 for (args = TYPE_ARG_TYPES (type);
9470 args && args != void_list_node;
9471 args = TREE_CHAIN (args))
9473 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
9475 DECL_CHAIN (decl) = decls;
9476 decls = decl;
9479 parms = nreverse (decls);
9481 if (decl_context != TYPENAME)
9483 /* A cv-qualifier-seq shall only be part of the function type
9484 for a non-static member function. [8.3.5/4 dcl.fct] */
9485 if (type_memfn_quals (type) != TYPE_UNQUALIFIED
9486 && (current_class_type == NULL_TREE || staticp) )
9488 error (staticp
9489 ? G_("qualified function types cannot be used to "
9490 "declare static member functions")
9491 : G_("qualified function types cannot be used to "
9492 "declare free functions"));
9493 type = TYPE_MAIN_VARIANT (type);
9496 /* The qualifiers on the function type become the qualifiers on
9497 the non-static member function. */
9498 memfn_quals |= type_memfn_quals (type);
9499 type_quals = TYPE_UNQUALIFIED;
9503 /* If this is a type name (such as, in a cast or sizeof),
9504 compute the type and return it now. */
9506 if (decl_context == TYPENAME)
9508 /* Note that the grammar rejects storage classes
9509 in typenames, fields or parameters. */
9510 if (type_quals != TYPE_UNQUALIFIED)
9511 type_quals = TYPE_UNQUALIFIED;
9513 /* Special case: "friend class foo" looks like a TYPENAME context. */
9514 if (friendp)
9516 if (type_quals != TYPE_UNQUALIFIED)
9518 error ("type qualifiers specified for friend class declaration");
9519 type_quals = TYPE_UNQUALIFIED;
9521 if (inlinep)
9523 error ("%<inline%> specified for friend class declaration");
9524 inlinep = 0;
9527 if (!current_aggr)
9529 /* Don't allow friend declaration without a class-key. */
9530 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
9531 permerror (input_location, "template parameters cannot be friends");
9532 else if (TREE_CODE (type) == TYPENAME_TYPE)
9533 permerror (input_location, "friend declaration requires class-key, "
9534 "i.e. %<friend class %T::%D%>",
9535 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
9536 else
9537 permerror (input_location, "friend declaration requires class-key, "
9538 "i.e. %<friend %#T%>",
9539 type);
9542 /* Only try to do this stuff if we didn't already give up. */
9543 if (type != integer_type_node)
9545 /* A friendly class? */
9546 if (current_class_type)
9547 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
9548 /*complain=*/true);
9549 else
9550 error ("trying to make class %qT a friend of global scope",
9551 type);
9553 type = void_type_node;
9556 else if (memfn_quals)
9558 if (ctype == NULL_TREE
9559 && TREE_CODE (type) == METHOD_TYPE)
9560 ctype = TYPE_METHOD_BASETYPE (type);
9562 if (ctype)
9563 type = build_memfn_type (type, ctype, memfn_quals);
9564 /* Core issue #547: need to allow this in template type args. */
9565 else if (template_type_arg && TREE_CODE (type) == FUNCTION_TYPE)
9566 type = apply_memfn_quals (type, memfn_quals);
9567 else
9568 error ("invalid qualifiers on non-member function type");
9571 return type;
9573 else if (unqualified_id == NULL_TREE && decl_context != PARM
9574 && decl_context != CATCHPARM
9575 && TREE_CODE (type) != UNION_TYPE
9576 && ! bitfield)
9578 error ("abstract declarator %qT used as declaration", type);
9579 return error_mark_node;
9582 /* Only functions may be declared using an operator-function-id. */
9583 if (unqualified_id
9584 && IDENTIFIER_OPNAME_P (unqualified_id)
9585 && TREE_CODE (type) != FUNCTION_TYPE
9586 && TREE_CODE (type) != METHOD_TYPE)
9588 error ("declaration of %qD as non-function", unqualified_id);
9589 return error_mark_node;
9592 /* We don't check parameter types here because we can emit a better
9593 error message later. */
9594 if (decl_context != PARM)
9596 type = check_var_type (unqualified_id, type);
9597 if (type == error_mark_node)
9598 return error_mark_node;
9601 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
9602 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
9604 if (decl_context == PARM || decl_context == CATCHPARM)
9606 if (ctype || in_namespace)
9607 error ("cannot use %<::%> in parameter declaration");
9609 /* A parameter declared as an array of T is really a pointer to T.
9610 One declared as a function is really a pointer to a function.
9611 One declared as a member is really a pointer to member. */
9613 if (TREE_CODE (type) == ARRAY_TYPE)
9615 /* Transfer const-ness of array into that of type pointed to. */
9616 type = build_pointer_type (TREE_TYPE (type));
9617 type_quals = TYPE_UNQUALIFIED;
9619 else if (TREE_CODE (type) == FUNCTION_TYPE)
9620 type = build_pointer_type (type);
9623 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
9624 && !NEW_DELETE_OPNAME_P (unqualified_id))
9625 type = build_memfn_type (type, ctype, memfn_quals);
9628 tree decl;
9630 if (decl_context == PARM)
9632 decl = cp_build_parm_decl (unqualified_id, type);
9634 bad_specifiers (decl, BSP_PARM, virtualp,
9635 memfn_quals != TYPE_UNQUALIFIED,
9636 inlinep, friendp, raises != NULL_TREE);
9638 else if (decl_context == FIELD)
9640 /* The C99 flexible array extension. */
9641 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
9642 && TYPE_DOMAIN (type) == NULL_TREE)
9644 tree itype = compute_array_index_type (dname, integer_zero_node,
9645 tf_warning_or_error);
9646 type = build_cplus_array_type (TREE_TYPE (type), itype);
9649 if (type == error_mark_node)
9651 /* Happens when declaring arrays of sizes which
9652 are error_mark_node, for example. */
9653 decl = NULL_TREE;
9655 else if (in_namespace && !friendp)
9657 /* Something like struct S { int N::j; }; */
9658 error ("invalid use of %<::%>");
9659 return error_mark_node;
9661 else if (TREE_CODE (type) == FUNCTION_TYPE
9662 || TREE_CODE (type) == METHOD_TYPE)
9664 int publicp = 0;
9665 tree function_context;
9667 if (friendp == 0)
9669 /* This should never happen in pure C++ (the check
9670 could be an assert). It could happen in
9671 Objective-C++ if someone writes invalid code that
9672 uses a function declaration for an instance
9673 variable or property (instance variables and
9674 properties are parsed as FIELD_DECLs, but they are
9675 part of an Objective-C class, not a C++ class).
9676 That code is invalid and is caught by this
9677 check. */
9678 if (!ctype)
9680 error ("declaration of function %qD in invalid context",
9681 unqualified_id);
9682 return error_mark_node;
9685 /* ``A union may [ ... ] not [ have ] virtual functions.''
9686 ARM 9.5 */
9687 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
9689 error ("function %qD declared virtual inside a union",
9690 unqualified_id);
9691 return error_mark_node;
9694 if (NEW_DELETE_OPNAME_P (unqualified_id))
9696 if (virtualp)
9698 error ("%qD cannot be declared virtual, since it "
9699 "is always static",
9700 unqualified_id);
9701 virtualp = 0;
9706 /* Check that the name used for a destructor makes sense. */
9707 if (sfk == sfk_destructor)
9709 tree uqname = id_declarator->u.id.unqualified_name;
9711 if (!ctype)
9713 gcc_assert (friendp);
9714 error ("expected qualified name in friend declaration "
9715 "for destructor %qD", uqname);
9716 return error_mark_node;
9719 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
9721 error ("declaration of %qD as member of %qT",
9722 uqname, ctype);
9723 return error_mark_node;
9725 if (constexpr_p)
9727 error ("a destructor cannot be %<constexpr%>");
9728 return error_mark_node;
9731 else if (sfk == sfk_constructor && friendp && !ctype)
9733 error ("expected qualified name in friend declaration "
9734 "for constructor %qD",
9735 id_declarator->u.id.unqualified_name);
9736 return error_mark_node;
9739 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
9740 function_context = (ctype != NULL_TREE) ?
9741 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
9742 publicp = (! friendp || ! staticp)
9743 && function_context == NULL_TREE;
9744 decl = grokfndecl (ctype, type,
9745 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
9746 ? unqualified_id : dname,
9747 parms,
9748 unqualified_id,
9749 virtualp, flags, memfn_quals, raises,
9750 friendp ? -1 : 0, friendp, publicp,
9751 inlinep | (2 * constexpr_p),
9752 sfk,
9753 funcdef_flag, template_count, in_namespace,
9754 attrlist, declarator->id_loc);
9755 if (decl == NULL_TREE)
9756 return error_mark_node;
9757 #if 0
9758 /* This clobbers the attrs stored in `decl' from `attrlist'. */
9759 /* The decl and setting of decl_attr is also turned off. */
9760 decl = build_decl_attribute_variant (decl, decl_attr);
9761 #endif
9763 /* [class.conv.ctor]
9765 A constructor declared without the function-specifier
9766 explicit that can be called with a single parameter
9767 specifies a conversion from the type of its first
9768 parameter to the type of its class. Such a constructor
9769 is called a converting constructor. */
9770 if (explicitp == 2)
9771 DECL_NONCONVERTING_P (decl) = 1;
9773 else if (!staticp && !dependent_type_p (type)
9774 && !COMPLETE_TYPE_P (complete_type (type))
9775 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
9777 if (unqualified_id)
9778 error ("field %qD has incomplete type", unqualified_id);
9779 else
9780 error ("name %qT has incomplete type", type);
9782 /* If we're instantiating a template, tell them which
9783 instantiation made the field's type be incomplete. */
9784 if (current_class_type
9785 && TYPE_NAME (current_class_type)
9786 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
9787 && declspecs->type
9788 && declspecs->type == type)
9789 error (" in instantiation of template %qT",
9790 current_class_type);
9792 return error_mark_node;
9794 else
9796 if (friendp)
9798 error ("%qE is neither function nor member function; "
9799 "cannot be declared friend", unqualified_id);
9800 friendp = 0;
9802 decl = NULL_TREE;
9805 if (friendp)
9807 /* Friends are treated specially. */
9808 if (ctype == current_class_type)
9809 ; /* We already issued a permerror. */
9810 else if (decl && DECL_NAME (decl))
9812 if (template_class_depth (current_class_type) == 0)
9814 decl = check_explicit_specialization
9815 (unqualified_id, decl, template_count,
9816 2 * funcdef_flag + 4);
9817 if (decl == error_mark_node)
9818 return error_mark_node;
9821 DECL_DECLARED_CONSTEXPR_P (decl) = constexpr_p;
9822 decl = do_friend (ctype, unqualified_id, decl,
9823 *attrlist, flags,
9824 funcdef_flag);
9825 return decl;
9827 else
9828 return error_mark_node;
9831 /* Structure field. It may not be a function, except for C++. */
9833 if (decl == NULL_TREE)
9835 if (initialized)
9837 if (!staticp)
9839 /* An attempt is being made to initialize a non-static
9840 member. But, from [class.mem]:
9842 4 A member-declarator can contain a
9843 constant-initializer only if it declares a static
9844 member (_class.static_) of integral or enumeration
9845 type, see _class.static.data_.
9847 This used to be relatively common practice, but
9848 the rest of the compiler does not correctly
9849 handle the initialization unless the member is
9850 static so we make it static below. */
9851 if (cxx_dialect >= cxx0x)
9853 sorry ("non-static data member initializers");
9855 else
9857 permerror (input_location, "ISO C++ forbids initialization of member %qD",
9858 unqualified_id);
9859 permerror (input_location, "making %qD static", unqualified_id);
9860 staticp = 1;
9864 if (uses_template_parms (type))
9865 /* We'll check at instantiation time. */
9867 else if (constexpr_p)
9868 /* constexpr has the same requirements. */
9870 else if (check_static_variable_definition (unqualified_id,
9871 type))
9872 /* If we just return the declaration, crashes
9873 will sometimes occur. We therefore return
9874 void_type_node, as if this was a friend
9875 declaration, to cause callers to completely
9876 ignore this declaration. */
9877 return error_mark_node;
9880 if (staticp)
9882 /* C++ allows static class members. All other work
9883 for this is done by grokfield. */
9884 decl = build_lang_decl (VAR_DECL, unqualified_id, type);
9885 set_linkage_for_static_data_member (decl);
9886 /* Even if there is an in-class initialization, DECL
9887 is considered undefined until an out-of-class
9888 definition is provided. */
9889 DECL_EXTERNAL (decl) = 1;
9891 if (thread_p)
9892 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
9894 if (constexpr_p && !initialized)
9896 error ("constexpr static data member %qD must have an "
9897 "initializer", decl);
9898 constexpr_p = false;
9901 else
9903 if (constexpr_p)
9905 error ("non-static data member %qE declared %<constexpr%>",
9906 unqualified_id);
9907 constexpr_p = false;
9909 decl = build_decl (input_location,
9910 FIELD_DECL, unqualified_id, type);
9911 DECL_NONADDRESSABLE_P (decl) = bitfield;
9912 if (bitfield && !unqualified_id)
9913 TREE_NO_WARNING (decl) = 1;
9915 if (storage_class == sc_mutable)
9917 DECL_MUTABLE_P (decl) = 1;
9918 storage_class = sc_none;
9922 bad_specifiers (decl, BSP_FIELD, virtualp,
9923 memfn_quals != TYPE_UNQUALIFIED,
9924 inlinep, friendp, raises != NULL_TREE);
9927 else if (TREE_CODE (type) == FUNCTION_TYPE
9928 || TREE_CODE (type) == METHOD_TYPE)
9930 tree original_name;
9931 int publicp = 0;
9933 if (!unqualified_id)
9934 return error_mark_node;
9936 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
9937 original_name = dname;
9938 else
9939 original_name = unqualified_id;
9941 if (storage_class == sc_auto)
9942 error ("storage class %<auto%> invalid for function %qs", name);
9943 else if (storage_class == sc_register)
9944 error ("storage class %<register%> invalid for function %qs", name);
9945 else if (thread_p)
9946 error ("storage class %<__thread%> invalid for function %qs", name);
9948 /* Function declaration not at top level.
9949 Storage classes other than `extern' are not allowed
9950 and `extern' makes no difference. */
9951 if (! toplevel_bindings_p ()
9952 && (storage_class == sc_static
9953 || declspecs->specs[(int)ds_inline])
9954 && pedantic)
9956 if (storage_class == sc_static)
9957 pedwarn (input_location, OPT_pedantic,
9958 "%<static%> specified invalid for function %qs "
9959 "declared out of global scope", name);
9960 else
9961 pedwarn (input_location, OPT_pedantic,
9962 "%<inline%> specifier invalid for function %qs "
9963 "declared out of global scope", name);
9966 if (ctype != NULL_TREE
9967 && TREE_CODE (ctype) != NAMESPACE_DECL && !MAYBE_CLASS_TYPE_P (ctype))
9969 error ("%q#T is not a class or a namespace", ctype);
9970 ctype = NULL_TREE;
9973 if (ctype == NULL_TREE)
9975 if (virtualp)
9977 error ("virtual non-class function %qs", name);
9978 virtualp = 0;
9980 else if (sfk == sfk_constructor
9981 || sfk == sfk_destructor)
9983 error (funcdef_flag
9984 ? "%qs defined in a non-class scope"
9985 : "%qs declared in a non-class scope", name);
9986 sfk = sfk_none;
9990 /* Record presence of `static'. */
9991 publicp = (ctype != NULL_TREE
9992 || storage_class == sc_extern
9993 || storage_class != sc_static);
9995 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
9996 virtualp, flags, memfn_quals, raises,
9997 1, friendp,
9998 publicp, inlinep | (2 * constexpr_p), sfk,
9999 funcdef_flag,
10000 template_count, in_namespace, attrlist,
10001 declarator->id_loc);
10002 if (decl == NULL_TREE)
10003 return error_mark_node;
10005 if (staticp == 1)
10007 int invalid_static = 0;
10009 /* Don't allow a static member function in a class, and forbid
10010 declaring main to be static. */
10011 if (TREE_CODE (type) == METHOD_TYPE)
10013 permerror (input_location, "cannot declare member function %qD to have "
10014 "static linkage", decl);
10015 invalid_static = 1;
10017 else if (current_function_decl)
10019 /* FIXME need arm citation */
10020 error ("cannot declare static function inside another function");
10021 invalid_static = 1;
10024 if (invalid_static)
10026 staticp = 0;
10027 storage_class = sc_none;
10031 else
10033 /* It's a variable. */
10035 /* An uninitialized decl with `extern' is a reference. */
10036 decl = grokvardecl (type, unqualified_id,
10037 declspecs,
10038 initialized,
10039 (type_quals & TYPE_QUAL_CONST) != 0,
10040 ctype ? ctype : in_namespace);
10041 bad_specifiers (decl, BSP_VAR, virtualp,
10042 memfn_quals != TYPE_UNQUALIFIED,
10043 inlinep, friendp, raises != NULL_TREE);
10045 if (ctype)
10047 DECL_CONTEXT (decl) = ctype;
10048 if (staticp == 1)
10050 permerror (input_location, "%<static%> may not be used when defining "
10051 "(as opposed to declaring) a static data member");
10052 staticp = 0;
10053 storage_class = sc_none;
10055 if (storage_class == sc_register && TREE_STATIC (decl))
10057 error ("static member %qD declared %<register%>", decl);
10058 storage_class = sc_none;
10060 if (storage_class == sc_extern && pedantic)
10062 pedwarn (input_location, OPT_pedantic,
10063 "cannot explicitly declare member %q#D to have "
10064 "extern linkage", decl);
10065 storage_class = sc_none;
10068 else if (constexpr_p && DECL_EXTERNAL (decl))
10069 error ("declaration of constexpr variable %qD is not a definition",
10070 decl);
10073 if (storage_class == sc_extern && initialized && !funcdef_flag)
10075 if (toplevel_bindings_p ())
10077 /* It's common practice (and completely valid) to have a const
10078 be initialized and declared extern. */
10079 if (!(type_quals & TYPE_QUAL_CONST))
10080 warning (0, "%qs initialized and declared %<extern%>", name);
10082 else
10084 error ("%qs has both %<extern%> and initializer", name);
10085 return error_mark_node;
10089 /* Record `register' declaration for warnings on &
10090 and in case doing stupid register allocation. */
10092 if (storage_class == sc_register)
10093 DECL_REGISTER (decl) = 1;
10094 else if (storage_class == sc_extern)
10095 DECL_THIS_EXTERN (decl) = 1;
10096 else if (storage_class == sc_static)
10097 DECL_THIS_STATIC (decl) = 1;
10099 /* Don't forget constexprness. */
10100 if (constexpr_p)
10101 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10103 /* Record constancy and volatility on the DECL itself . There's
10104 no need to do this when processing a template; we'll do this
10105 for the instantiated declaration based on the type of DECL. */
10106 if (!processing_template_decl)
10107 cp_apply_type_quals_to_decl (type_quals, decl);
10109 return decl;
10113 /* Subroutine of start_function. Ensure that each of the parameter
10114 types (as listed in PARMS) is complete, as is required for a
10115 function definition. */
10117 static void
10118 require_complete_types_for_parms (tree parms)
10120 for (; parms; parms = DECL_CHAIN (parms))
10122 if (dependent_type_p (TREE_TYPE (parms)))
10123 continue;
10124 if (!VOID_TYPE_P (TREE_TYPE (parms))
10125 && complete_type_or_else (TREE_TYPE (parms), parms))
10127 relayout_decl (parms);
10128 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
10130 else
10131 /* grokparms or complete_type_or_else will have already issued
10132 an error. */
10133 TREE_TYPE (parms) = error_mark_node;
10137 /* Returns nonzero if T is a local variable. */
10140 local_variable_p (const_tree t)
10142 if ((TREE_CODE (t) == VAR_DECL
10143 /* A VAR_DECL with a context that is a _TYPE is a static data
10144 member. */
10145 && !TYPE_P (CP_DECL_CONTEXT (t))
10146 /* Any other non-local variable must be at namespace scope. */
10147 && !DECL_NAMESPACE_SCOPE_P (t))
10148 || (TREE_CODE (t) == PARM_DECL))
10149 return 1;
10151 return 0;
10154 /* Like local_variable_p, but suitable for use as a tree-walking
10155 function. */
10157 static tree
10158 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
10159 void *data ATTRIBUTE_UNUSED)
10161 if (local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
10162 return *tp;
10163 else if (TYPE_P (*tp))
10164 *walk_subtrees = 0;
10166 return NULL_TREE;
10170 /* Check that ARG, which is a default-argument expression for a
10171 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
10172 something goes wrong. DECL may also be a _TYPE node, rather than a
10173 DECL, if there is no DECL available. */
10175 tree
10176 check_default_argument (tree decl, tree arg)
10178 tree var;
10179 tree decl_type;
10181 if (TREE_CODE (arg) == DEFAULT_ARG)
10182 /* We get a DEFAULT_ARG when looking at an in-class declaration
10183 with a default argument. Ignore the argument for now; we'll
10184 deal with it after the class is complete. */
10185 return arg;
10187 if (TYPE_P (decl))
10189 decl_type = decl;
10190 decl = NULL_TREE;
10192 else
10193 decl_type = TREE_TYPE (decl);
10195 if (arg == error_mark_node
10196 || decl == error_mark_node
10197 || TREE_TYPE (arg) == error_mark_node
10198 || decl_type == error_mark_node)
10199 /* Something already went wrong. There's no need to check
10200 further. */
10201 return error_mark_node;
10203 /* [dcl.fct.default]
10205 A default argument expression is implicitly converted to the
10206 parameter type. */
10207 if (!TREE_TYPE (arg)
10208 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg, LOOKUP_NORMAL))
10210 if (decl)
10211 error ("default argument for %q#D has type %qT",
10212 decl, TREE_TYPE (arg));
10213 else
10214 error ("default argument for parameter of type %qT has type %qT",
10215 decl_type, TREE_TYPE (arg));
10217 return error_mark_node;
10220 /* [dcl.fct.default]
10222 Local variables shall not be used in default argument
10223 expressions.
10225 The keyword `this' shall not be used in a default argument of a
10226 member function. */
10227 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
10228 if (var)
10230 error ("default argument %qE uses local variable %qD", arg, var);
10231 return error_mark_node;
10234 /* All is well. */
10235 return arg;
10238 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
10240 static tree
10241 type_is_deprecated (tree type)
10243 enum tree_code code;
10244 if (TREE_DEPRECATED (type))
10245 return type;
10246 if (TYPE_NAME (type)
10247 && TREE_DEPRECATED (TYPE_NAME (type)))
10248 return type;
10250 /* Do warn about using typedefs to a deprecated class. */
10251 if (TAGGED_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
10252 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
10254 code = TREE_CODE (type);
10256 if (code == POINTER_TYPE || code == REFERENCE_TYPE
10257 || code == OFFSET_TYPE || code == FUNCTION_TYPE
10258 || code == METHOD_TYPE || code == ARRAY_TYPE)
10259 return type_is_deprecated (TREE_TYPE (type));
10261 if (TYPE_PTRMEMFUNC_P (type))
10262 return type_is_deprecated
10263 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
10265 return NULL_TREE;
10268 /* Decode the list of parameter types for a function type.
10269 Given the list of things declared inside the parens,
10270 return a list of types.
10272 If this parameter does not end with an ellipsis, we append
10273 void_list_node.
10275 *PARMS is set to the chain of PARM_DECLs created. */
10277 static tree
10278 grokparms (tree parmlist, tree *parms)
10280 tree result = NULL_TREE;
10281 tree decls = NULL_TREE;
10282 tree parm;
10283 int any_error = 0;
10285 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
10287 tree type = NULL_TREE;
10288 tree init = TREE_PURPOSE (parm);
10289 tree decl = TREE_VALUE (parm);
10290 const char *errmsg;
10292 if (parm == void_list_node)
10293 break;
10295 if (! decl || TREE_TYPE (decl) == error_mark_node)
10296 continue;
10298 type = TREE_TYPE (decl);
10299 if (VOID_TYPE_P (type))
10301 if (same_type_p (type, void_type_node)
10302 && DECL_SELF_REFERENCE_P (type)
10303 && !DECL_NAME (decl) && !result && TREE_CHAIN (parm) == void_list_node)
10304 /* this is a parmlist of `(void)', which is ok. */
10305 break;
10306 cxx_incomplete_type_error (decl, type);
10307 /* It's not a good idea to actually create parameters of
10308 type `void'; other parts of the compiler assume that a
10309 void type terminates the parameter list. */
10310 type = error_mark_node;
10311 TREE_TYPE (decl) = error_mark_node;
10314 if (type != error_mark_node
10315 && TYPE_FOR_JAVA (type)
10316 && MAYBE_CLASS_TYPE_P (type))
10318 error ("parameter %qD has Java class type", decl);
10319 type = error_mark_node;
10320 TREE_TYPE (decl) = error_mark_node;
10321 init = NULL_TREE;
10324 if (type != error_mark_node
10325 && (errmsg = targetm.invalid_parameter_type (type)))
10327 error (errmsg);
10328 type = error_mark_node;
10329 TREE_TYPE (decl) = error_mark_node;
10332 if (type != error_mark_node)
10334 if (deprecated_state != DEPRECATED_SUPPRESS)
10336 tree deptype = type_is_deprecated (type);
10337 if (deptype)
10338 warn_deprecated_use (deptype, NULL_TREE);
10341 /* Top-level qualifiers on the parameters are
10342 ignored for function types. */
10343 type = cp_build_qualified_type (type, 0);
10344 if (TREE_CODE (type) == METHOD_TYPE)
10346 error ("parameter %qD invalidly declared method type", decl);
10347 type = build_pointer_type (type);
10348 TREE_TYPE (decl) = type;
10350 else if (abstract_virtuals_error (decl, type))
10351 any_error = 1; /* Seems like a good idea. */
10352 else if (POINTER_TYPE_P (type))
10354 /* [dcl.fct]/6, parameter types cannot contain pointers
10355 (references) to arrays of unknown bound. */
10356 tree t = TREE_TYPE (type);
10357 int ptr = TYPE_PTR_P (type);
10359 while (1)
10361 if (TYPE_PTR_P (t))
10362 ptr = 1;
10363 else if (TREE_CODE (t) != ARRAY_TYPE)
10364 break;
10365 else if (!TYPE_DOMAIN (t))
10366 break;
10367 t = TREE_TYPE (t);
10369 if (TREE_CODE (t) == ARRAY_TYPE)
10370 error (ptr
10371 ? G_("parameter %qD includes pointer to array of "
10372 "unknown bound %qT")
10373 : G_("parameter %qD includes reference to array of "
10374 "unknown bound %qT"),
10375 decl, t);
10378 if (any_error)
10379 init = NULL_TREE;
10380 else if (init && !processing_template_decl)
10381 init = check_default_argument (decl, init);
10384 if (TREE_CODE (decl) == PARM_DECL
10385 && FUNCTION_PARAMETER_PACK_P (decl)
10386 && TREE_CHAIN (parm)
10387 && TREE_CHAIN (parm) != void_list_node)
10388 error ("parameter packs must be at the end of the parameter list");
10390 DECL_CHAIN (decl) = decls;
10391 decls = decl;
10392 result = tree_cons (init, type, result);
10394 decls = nreverse (decls);
10395 result = nreverse (result);
10396 if (parm)
10397 result = chainon (result, void_list_node);
10398 *parms = decls;
10400 return result;
10404 /* D is a constructor or overloaded `operator='.
10406 Let T be the class in which D is declared. Then, this function
10407 returns:
10409 -1 if D's is an ill-formed constructor or copy assignment operator
10410 whose first parameter is of type `T'.
10411 0 if D is not a copy constructor or copy assignment
10412 operator.
10413 1 if D is a copy constructor or copy assignment operator whose
10414 first parameter is a reference to non-const qualified T.
10415 2 if D is a copy constructor or copy assignment operator whose
10416 first parameter is a reference to const qualified T.
10418 This function can be used as a predicate. Positive values indicate
10419 a copy constructor and nonzero values indicate a copy assignment
10420 operator. */
10423 copy_fn_p (const_tree d)
10425 tree args;
10426 tree arg_type;
10427 int result = 1;
10429 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
10431 if (TREE_CODE (d) == TEMPLATE_DECL
10432 || (DECL_TEMPLATE_INFO (d)
10433 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
10434 /* Instantiations of template member functions are never copy
10435 functions. Note that member functions of templated classes are
10436 represented as template functions internally, and we must
10437 accept those as copy functions. */
10438 return 0;
10440 args = FUNCTION_FIRST_USER_PARMTYPE (d);
10441 if (!args)
10442 return 0;
10444 arg_type = TREE_VALUE (args);
10445 if (arg_type == error_mark_node)
10446 return 0;
10448 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
10450 /* Pass by value copy assignment operator. */
10451 result = -1;
10453 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
10454 && !TYPE_REF_IS_RVALUE (arg_type)
10455 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
10457 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
10458 result = 2;
10460 else
10461 return 0;
10463 args = TREE_CHAIN (args);
10465 if (args && args != void_list_node && !TREE_PURPOSE (args))
10466 /* There are more non-optional args. */
10467 return 0;
10469 return result;
10472 /* D is a constructor or overloaded `operator='.
10474 Let T be the class in which D is declared. Then, this function
10475 returns true when D is a move constructor or move assignment
10476 operator, false otherwise. */
10478 bool
10479 move_fn_p (const_tree d)
10481 tree args;
10482 tree arg_type;
10483 bool result = false;
10485 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
10487 if (cxx_dialect == cxx98)
10488 /* There are no move constructors if we are in C++98 mode. */
10489 return false;
10491 if (TREE_CODE (d) == TEMPLATE_DECL
10492 || (DECL_TEMPLATE_INFO (d)
10493 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
10494 /* Instantiations of template member functions are never copy
10495 functions. Note that member functions of templated classes are
10496 represented as template functions internally, and we must
10497 accept those as copy functions. */
10498 return 0;
10500 args = FUNCTION_FIRST_USER_PARMTYPE (d);
10501 if (!args)
10502 return 0;
10504 arg_type = TREE_VALUE (args);
10505 if (arg_type == error_mark_node)
10506 return 0;
10508 if (TREE_CODE (arg_type) == REFERENCE_TYPE
10509 && TYPE_REF_IS_RVALUE (arg_type)
10510 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
10511 DECL_CONTEXT (d)))
10512 result = true;
10514 args = TREE_CHAIN (args);
10516 if (args && args != void_list_node && !TREE_PURPOSE (args))
10517 /* There are more non-optional args. */
10518 return false;
10520 return result;
10523 /* Remember any special properties of member function DECL. */
10525 void
10526 grok_special_member_properties (tree decl)
10528 tree class_type;
10530 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
10531 return;
10533 class_type = DECL_CONTEXT (decl);
10534 if (DECL_CONSTRUCTOR_P (decl))
10536 int ctor = copy_fn_p (decl);
10538 if (!DECL_ARTIFICIAL (decl))
10539 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
10541 if (ctor > 0)
10543 /* [class.copy]
10545 A non-template constructor for class X is a copy
10546 constructor if its first parameter is of type X&, const
10547 X&, volatile X& or const volatile X&, and either there
10548 are no other parameters or else all other parameters have
10549 default arguments. */
10550 TYPE_HAS_COPY_CTOR (class_type) = 1;
10551 if (user_provided_p (decl))
10552 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
10553 if (ctor > 1)
10554 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
10556 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
10558 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
10559 if (user_provided_p (decl))
10560 TYPE_HAS_COMPLEX_DFLT (class_type) = 1;
10562 else if (move_fn_p (decl) && user_provided_p (decl))
10563 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
10564 else if (is_list_ctor (decl))
10565 TYPE_HAS_LIST_CTOR (class_type) = 1;
10567 if (DECL_DECLARED_CONSTEXPR_P (decl)
10568 && !copy_fn_p (decl) && !move_fn_p (decl))
10569 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
10571 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
10573 /* [class.copy]
10575 A non-template assignment operator for class X is a copy
10576 assignment operator if its parameter is of type X, X&, const
10577 X&, volatile X& or const volatile X&. */
10579 int assop = copy_fn_p (decl);
10581 if (assop)
10583 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
10584 if (user_provided_p (decl))
10585 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
10586 if (assop != 1)
10587 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
10589 else if (move_fn_p (decl) && user_provided_p (decl))
10590 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
10592 /* Destructors are handled in check_methods. */
10595 /* Check a constructor DECL has the correct form. Complains
10596 if the class has a constructor of the form X(X). */
10599 grok_ctor_properties (const_tree ctype, const_tree decl)
10601 int ctor_parm = copy_fn_p (decl);
10603 if (ctor_parm < 0)
10605 /* [class.copy]
10607 A declaration of a constructor for a class X is ill-formed if
10608 its first parameter is of type (optionally cv-qualified) X
10609 and either there are no other parameters or else all other
10610 parameters have default arguments.
10612 We *don't* complain about member template instantiations that
10613 have this form, though; they can occur as we try to decide
10614 what constructor to use during overload resolution. Since
10615 overload resolution will never prefer such a constructor to
10616 the non-template copy constructor (which is either explicitly
10617 or implicitly defined), there's no need to worry about their
10618 existence. Theoretically, they should never even be
10619 instantiated, but that's hard to forestall. */
10620 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
10621 ctype, ctype);
10622 return 0;
10625 return 1;
10628 /* An operator with this code is unary, but can also be binary. */
10630 static int
10631 ambi_op_p (enum tree_code code)
10633 return (code == INDIRECT_REF
10634 || code == ADDR_EXPR
10635 || code == UNARY_PLUS_EXPR
10636 || code == NEGATE_EXPR
10637 || code == PREINCREMENT_EXPR
10638 || code == PREDECREMENT_EXPR);
10641 /* An operator with this name can only be unary. */
10643 static int
10644 unary_op_p (enum tree_code code)
10646 return (code == TRUTH_NOT_EXPR
10647 || code == BIT_NOT_EXPR
10648 || code == COMPONENT_REF
10649 || code == TYPE_EXPR);
10652 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
10653 errors are issued for invalid declarations. */
10655 bool
10656 grok_op_properties (tree decl, bool complain)
10658 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10659 tree argtype;
10660 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
10661 tree name = DECL_NAME (decl);
10662 enum tree_code operator_code;
10663 int arity;
10664 bool ellipsis_p;
10665 tree class_type;
10667 /* Count the number of arguments and check for ellipsis. */
10668 for (argtype = argtypes, arity = 0;
10669 argtype && argtype != void_list_node;
10670 argtype = TREE_CHAIN (argtype))
10671 ++arity;
10672 ellipsis_p = !argtype;
10674 class_type = DECL_CONTEXT (decl);
10675 if (class_type && !CLASS_TYPE_P (class_type))
10676 class_type = NULL_TREE;
10678 if (DECL_CONV_FN_P (decl))
10679 operator_code = TYPE_EXPR;
10680 else
10683 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
10684 if (ansi_opname (CODE) == name) \
10686 operator_code = (CODE); \
10687 break; \
10689 else if (ansi_assopname (CODE) == name) \
10691 operator_code = (CODE); \
10692 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
10693 break; \
10696 #include "operators.def"
10697 #undef DEF_OPERATOR
10699 gcc_unreachable ();
10701 while (0);
10702 gcc_assert (operator_code != MAX_TREE_CODES);
10703 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
10705 if (class_type)
10706 switch (operator_code)
10708 case NEW_EXPR:
10709 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
10710 break;
10712 case DELETE_EXPR:
10713 TYPE_GETS_DELETE (class_type) |= 1;
10714 break;
10716 case VEC_NEW_EXPR:
10717 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
10718 break;
10720 case VEC_DELETE_EXPR:
10721 TYPE_GETS_DELETE (class_type) |= 2;
10722 break;
10724 default:
10725 break;
10728 /* [basic.std.dynamic.allocation]/1:
10730 A program is ill-formed if an allocation function is declared
10731 in a namespace scope other than global scope or declared static
10732 in global scope.
10734 The same also holds true for deallocation functions. */
10735 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
10736 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
10738 if (DECL_NAMESPACE_SCOPE_P (decl))
10740 if (CP_DECL_CONTEXT (decl) != global_namespace)
10742 error ("%qD may not be declared within a namespace", decl);
10743 return false;
10745 else if (!TREE_PUBLIC (decl))
10747 error ("%qD may not be declared as static", decl);
10748 return false;
10753 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
10755 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
10756 DECL_IS_OPERATOR_NEW (decl) = 1;
10758 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
10759 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
10760 else
10762 /* An operator function must either be a non-static member function
10763 or have at least one parameter of a class, a reference to a class,
10764 an enumeration, or a reference to an enumeration. 13.4.0.6 */
10765 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
10767 if (operator_code == TYPE_EXPR
10768 || operator_code == CALL_EXPR
10769 || operator_code == COMPONENT_REF
10770 || operator_code == ARRAY_REF
10771 || operator_code == NOP_EXPR)
10773 error ("%qD must be a nonstatic member function", decl);
10774 return false;
10776 else
10778 tree p;
10780 if (DECL_STATIC_FUNCTION_P (decl))
10782 error ("%qD must be either a non-static member "
10783 "function or a non-member function", decl);
10784 return false;
10787 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
10789 tree arg = non_reference (TREE_VALUE (p));
10790 if (arg == error_mark_node)
10791 return false;
10793 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
10794 because these checks are performed even on
10795 template functions. */
10796 if (MAYBE_CLASS_TYPE_P (arg)
10797 || TREE_CODE (arg) == ENUMERAL_TYPE)
10798 break;
10801 if (!p || p == void_list_node)
10803 if (complain)
10804 error ("%qD must have an argument of class or "
10805 "enumerated type", decl);
10806 return false;
10811 /* There are no restrictions on the arguments to an overloaded
10812 "operator ()". */
10813 if (operator_code == CALL_EXPR)
10814 return true;
10816 /* Warn about conversion operators that will never be used. */
10817 if (IDENTIFIER_TYPENAME_P (name)
10818 && ! DECL_TEMPLATE_INFO (decl)
10819 && warn_conversion
10820 /* Warn only declaring the function; there is no need to
10821 warn again about out-of-class definitions. */
10822 && class_type == current_class_type)
10824 tree t = TREE_TYPE (name);
10825 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
10827 if (ref)
10828 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
10830 if (TREE_CODE (t) == VOID_TYPE)
10831 warning (OPT_Wconversion,
10833 ? G_("conversion to a reference to void "
10834 "will never use a type conversion operator")
10835 : G_("conversion to void "
10836 "will never use a type conversion operator"));
10837 else if (class_type)
10839 if (t == class_type)
10840 warning (OPT_Wconversion,
10842 ? G_("conversion to a reference to the same type "
10843 "will never use a type conversion operator")
10844 : G_("conversion to the same type "
10845 "will never use a type conversion operator"));
10846 /* Don't force t to be complete here. */
10847 else if (MAYBE_CLASS_TYPE_P (t)
10848 && COMPLETE_TYPE_P (t)
10849 && DERIVED_FROM_P (t, class_type))
10850 warning (OPT_Wconversion,
10852 ? G_("conversion to a reference to a base class "
10853 "will never use a type conversion operator")
10854 : G_("conversion to a base class "
10855 "will never use a type conversion operator"));
10860 if (operator_code == COND_EXPR)
10862 /* 13.4.0.3 */
10863 error ("ISO C++ prohibits overloading operator ?:");
10864 return false;
10866 else if (ellipsis_p)
10868 error ("%qD must not have variable number of arguments", decl);
10869 return false;
10871 else if (ambi_op_p (operator_code))
10873 if (arity == 1)
10874 /* We pick the one-argument operator codes by default, so
10875 we don't have to change anything. */
10877 else if (arity == 2)
10879 /* If we thought this was a unary operator, we now know
10880 it to be a binary operator. */
10881 switch (operator_code)
10883 case INDIRECT_REF:
10884 operator_code = MULT_EXPR;
10885 break;
10887 case ADDR_EXPR:
10888 operator_code = BIT_AND_EXPR;
10889 break;
10891 case UNARY_PLUS_EXPR:
10892 operator_code = PLUS_EXPR;
10893 break;
10895 case NEGATE_EXPR:
10896 operator_code = MINUS_EXPR;
10897 break;
10899 case PREINCREMENT_EXPR:
10900 operator_code = POSTINCREMENT_EXPR;
10901 break;
10903 case PREDECREMENT_EXPR:
10904 operator_code = POSTDECREMENT_EXPR;
10905 break;
10907 default:
10908 gcc_unreachable ();
10911 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
10913 if ((operator_code == POSTINCREMENT_EXPR
10914 || operator_code == POSTDECREMENT_EXPR)
10915 && ! processing_template_decl
10916 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
10918 if (methodp)
10919 error ("postfix %qD must take %<int%> as its argument",
10920 decl);
10921 else
10922 error ("postfix %qD must take %<int%> as its second "
10923 "argument", decl);
10924 return false;
10927 else
10929 if (methodp)
10930 error ("%qD must take either zero or one argument", decl);
10931 else
10932 error ("%qD must take either one or two arguments", decl);
10933 return false;
10936 /* More Effective C++ rule 6. */
10937 if (warn_ecpp
10938 && (operator_code == POSTINCREMENT_EXPR
10939 || operator_code == POSTDECREMENT_EXPR
10940 || operator_code == PREINCREMENT_EXPR
10941 || operator_code == PREDECREMENT_EXPR))
10943 tree arg = TREE_VALUE (argtypes);
10944 tree ret = TREE_TYPE (TREE_TYPE (decl));
10945 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
10946 arg = TREE_TYPE (arg);
10947 arg = TYPE_MAIN_VARIANT (arg);
10948 if (operator_code == PREINCREMENT_EXPR
10949 || operator_code == PREDECREMENT_EXPR)
10951 if (TREE_CODE (ret) != REFERENCE_TYPE
10952 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
10953 arg))
10954 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
10955 build_reference_type (arg));
10957 else
10959 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
10960 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
10964 else if (unary_op_p (operator_code))
10966 if (arity != 1)
10968 if (methodp)
10969 error ("%qD must take %<void%>", decl);
10970 else
10971 error ("%qD must take exactly one argument", decl);
10972 return false;
10975 else /* if (binary_op_p (operator_code)) */
10977 if (arity != 2)
10979 if (methodp)
10980 error ("%qD must take exactly one argument", decl);
10981 else
10982 error ("%qD must take exactly two arguments", decl);
10983 return false;
10986 /* More Effective C++ rule 7. */
10987 if (warn_ecpp
10988 && (operator_code == TRUTH_ANDIF_EXPR
10989 || operator_code == TRUTH_ORIF_EXPR
10990 || operator_code == COMPOUND_EXPR))
10991 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
10992 decl);
10995 /* Effective C++ rule 23. */
10996 if (warn_ecpp
10997 && arity == 2
10998 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
10999 && (operator_code == PLUS_EXPR
11000 || operator_code == MINUS_EXPR
11001 || operator_code == TRUNC_DIV_EXPR
11002 || operator_code == MULT_EXPR
11003 || operator_code == TRUNC_MOD_EXPR)
11004 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
11005 warning (OPT_Weffc__, "%qD should return by value", decl);
11007 /* [over.oper]/8 */
11008 for (; argtypes && argtypes != void_list_node;
11009 argtypes = TREE_CHAIN (argtypes))
11010 if (TREE_PURPOSE (argtypes))
11012 TREE_PURPOSE (argtypes) = NULL_TREE;
11013 if (operator_code == POSTINCREMENT_EXPR
11014 || operator_code == POSTDECREMENT_EXPR)
11016 pedwarn (input_location, OPT_pedantic, "%qD cannot have default arguments",
11017 decl);
11019 else
11021 error ("%qD cannot have default arguments", decl);
11022 return false;
11026 return true;
11029 /* Return a string giving the keyword associate with CODE. */
11031 static const char *
11032 tag_name (enum tag_types code)
11034 switch (code)
11036 case record_type:
11037 return "struct";
11038 case class_type:
11039 return "class";
11040 case union_type:
11041 return "union";
11042 case enum_type:
11043 return "enum";
11044 case typename_type:
11045 return "typename";
11046 default:
11047 gcc_unreachable ();
11051 /* Name lookup in an elaborated-type-specifier (after the keyword
11052 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
11053 elaborated-type-specifier is invalid, issue a diagnostic and return
11054 error_mark_node; otherwise, return the *_TYPE to which it referred.
11055 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
11057 tree
11058 check_elaborated_type_specifier (enum tag_types tag_code,
11059 tree decl,
11060 bool allow_template_p)
11062 tree type;
11064 /* In the case of:
11066 struct S { struct S *p; };
11068 name lookup will find the TYPE_DECL for the implicit "S::S"
11069 typedef. Adjust for that here. */
11070 if (DECL_SELF_REFERENCE_P (decl))
11071 decl = TYPE_NAME (TREE_TYPE (decl));
11073 type = TREE_TYPE (decl);
11075 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
11076 is false for this case as well. */
11077 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11079 error ("using template type parameter %qT after %qs",
11080 type, tag_name (tag_code));
11081 return error_mark_node;
11083 /* [dcl.type.elab]
11085 If the identifier resolves to a typedef-name or a template
11086 type-parameter, the elaborated-type-specifier is ill-formed.
11088 In other words, the only legitimate declaration to use in the
11089 elaborated type specifier is the implicit typedef created when
11090 the type is declared. */
11091 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
11092 && !DECL_SELF_REFERENCE_P (decl)
11093 && tag_code != typename_type)
11095 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
11096 error ("%q+D has a previous declaration here", decl);
11097 return error_mark_node;
11099 else if (TREE_CODE (type) != RECORD_TYPE
11100 && TREE_CODE (type) != UNION_TYPE
11101 && tag_code != enum_type
11102 && tag_code != typename_type)
11104 error ("%qT referred to as %qs", type, tag_name (tag_code));
11105 error ("%q+T has a previous declaration here", type);
11106 return error_mark_node;
11108 else if (TREE_CODE (type) != ENUMERAL_TYPE
11109 && tag_code == enum_type)
11111 error ("%qT referred to as enum", type);
11112 error ("%q+T has a previous declaration here", type);
11113 return error_mark_node;
11115 else if (!allow_template_p
11116 && TREE_CODE (type) == RECORD_TYPE
11117 && CLASSTYPE_IS_TEMPLATE (type))
11119 /* If a class template appears as elaborated type specifier
11120 without a template header such as:
11122 template <class T> class C {};
11123 void f(class C); // No template header here
11125 then the required template argument is missing. */
11126 error ("template argument required for %<%s %T%>",
11127 tag_name (tag_code),
11128 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
11129 return error_mark_node;
11132 return type;
11135 /* Lookup NAME in elaborate type specifier in scope according to
11136 SCOPE and issue diagnostics if necessary.
11137 Return *_TYPE node upon success, NULL_TREE when the NAME is not
11138 found, and ERROR_MARK_NODE for type error. */
11140 static tree
11141 lookup_and_check_tag (enum tag_types tag_code, tree name,
11142 tag_scope scope, bool template_header_p)
11144 tree t;
11145 tree decl;
11146 if (scope == ts_global)
11148 /* First try ordinary name lookup, ignoring hidden class name
11149 injected via friend declaration. */
11150 decl = lookup_name_prefer_type (name, 2);
11151 /* If that fails, the name will be placed in the smallest
11152 non-class, non-function-prototype scope according to 3.3.1/5.
11153 We may already have a hidden name declared as friend in this
11154 scope. So lookup again but not ignoring hidden names.
11155 If we find one, that name will be made visible rather than
11156 creating a new tag. */
11157 if (!decl)
11158 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
11160 else
11161 decl = lookup_type_scope (name, scope);
11163 if (decl && DECL_CLASS_TEMPLATE_P (decl))
11164 decl = DECL_TEMPLATE_RESULT (decl);
11166 if (decl && TREE_CODE (decl) == TYPE_DECL)
11168 /* Look for invalid nested type:
11169 class C {
11170 class C {};
11171 }; */
11172 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
11174 error ("%qD has the same name as the class in which it is "
11175 "declared",
11176 decl);
11177 return error_mark_node;
11180 /* Two cases we need to consider when deciding if a class
11181 template is allowed as an elaborated type specifier:
11182 1. It is a self reference to its own class.
11183 2. It comes with a template header.
11185 For example:
11187 template <class T> class C {
11188 class C *c1; // DECL_SELF_REFERENCE_P is true
11189 class D;
11191 template <class U> class C; // template_header_p is true
11192 template <class T> class C<T>::D {
11193 class C *c2; // DECL_SELF_REFERENCE_P is true
11194 }; */
11196 t = check_elaborated_type_specifier (tag_code,
11197 decl,
11198 template_header_p
11199 | DECL_SELF_REFERENCE_P (decl));
11200 return t;
11202 else if (decl && TREE_CODE (decl) == TREE_LIST)
11204 error ("reference to %qD is ambiguous", name);
11205 print_candidates (decl);
11206 return error_mark_node;
11208 else
11209 return NULL_TREE;
11212 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
11213 Define the tag as a forward-reference if it is not defined.
11215 If a declaration is given, process it here, and report an error if
11216 multiple declarations are not identical.
11218 SCOPE is TS_CURRENT when this is also a definition. Only look in
11219 the current frame for the name (since C++ allows new names in any
11220 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
11221 declaration. Only look beginning from the current scope outward up
11222 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
11224 TEMPLATE_HEADER_P is true when this declaration is preceded by
11225 a set of template parameters. */
11227 static tree
11228 xref_tag_1 (enum tag_types tag_code, tree name,
11229 tag_scope scope, bool template_header_p)
11231 enum tree_code code;
11232 tree t;
11233 tree context = NULL_TREE;
11235 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
11237 switch (tag_code)
11239 case record_type:
11240 case class_type:
11241 code = RECORD_TYPE;
11242 break;
11243 case union_type:
11244 code = UNION_TYPE;
11245 break;
11246 case enum_type:
11247 code = ENUMERAL_TYPE;
11248 break;
11249 default:
11250 gcc_unreachable ();
11253 /* In case of anonymous name, xref_tag is only called to
11254 make type node and push name. Name lookup is not required. */
11255 if (ANON_AGGRNAME_P (name))
11256 t = NULL_TREE;
11257 else
11258 t = lookup_and_check_tag (tag_code, name,
11259 scope, template_header_p);
11261 if (t == error_mark_node)
11262 return error_mark_node;
11264 if (scope != ts_current && t && current_class_type
11265 && template_class_depth (current_class_type)
11266 && template_header_p)
11268 /* Since SCOPE is not TS_CURRENT, we are not looking at a
11269 definition of this tag. Since, in addition, we are currently
11270 processing a (member) template declaration of a template
11271 class, we must be very careful; consider:
11273 template <class X>
11274 struct S1
11276 template <class U>
11277 struct S2
11278 { template <class V>
11279 friend struct S1; };
11281 Here, the S2::S1 declaration should not be confused with the
11282 outer declaration. In particular, the inner version should
11283 have a template parameter of level 2, not level 1. This
11284 would be particularly important if the member declaration
11285 were instead:
11287 template <class V = U> friend struct S1;
11289 say, when we should tsubst into `U' when instantiating
11290 S2. On the other hand, when presented with:
11292 template <class T>
11293 struct S1 {
11294 template <class U>
11295 struct S2 {};
11296 template <class U>
11297 friend struct S2;
11300 we must find the inner binding eventually. We
11301 accomplish this by making sure that the new type we
11302 create to represent this declaration has the right
11303 TYPE_CONTEXT. */
11304 context = TYPE_CONTEXT (t);
11305 t = NULL_TREE;
11308 if (! t)
11310 /* If no such tag is yet defined, create a forward-reference node
11311 and record it as the "definition".
11312 When a real declaration of this type is found,
11313 the forward-reference will be altered into a real type. */
11314 if (code == ENUMERAL_TYPE)
11316 error ("use of enum %q#D without previous declaration", name);
11317 return error_mark_node;
11319 else
11321 t = make_class_type (code);
11322 TYPE_CONTEXT (t) = context;
11323 /* FIXME pph: creating incomplete class. */
11324 t = pushtag (name, t, scope);
11327 else
11329 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
11331 if (!redeclare_class_template (t, current_template_parms))
11332 return error_mark_node;
11334 else if (!processing_template_decl
11335 && CLASS_TYPE_P (t)
11336 && CLASSTYPE_IS_TEMPLATE (t))
11338 error ("redeclaration of %qT as a non-template", t);
11339 error ("previous declaration %q+D", t);
11340 return error_mark_node;
11343 /* Make injected friend class visible. */
11344 if (scope != ts_within_enclosing_non_class
11345 && hidden_name_p (TYPE_NAME (t)))
11347 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
11348 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
11350 if (TYPE_TEMPLATE_INFO (t))
11352 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
11353 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
11358 return t;
11361 /* Wrapper for xref_tag_1. */
11363 tree
11364 xref_tag (enum tag_types tag_code, tree name,
11365 tag_scope scope, bool template_header_p)
11367 tree ret;
11368 timevar_start (TV_NAME_LOOKUP);
11369 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
11370 pph_catch_name_lookup (ret);
11371 timevar_stop (TV_NAME_LOOKUP);
11372 return ret;
11376 tree
11377 xref_tag_from_type (tree old, tree id, tag_scope scope)
11379 enum tag_types tag_kind;
11381 if (TREE_CODE (old) == RECORD_TYPE)
11382 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
11383 else
11384 tag_kind = union_type;
11386 if (id == NULL_TREE)
11387 id = TYPE_IDENTIFIER (old);
11389 return xref_tag (tag_kind, id, scope, false);
11392 /* Create the binfo hierarchy for REF with (possibly NULL) base list
11393 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
11394 access_* node, and the TREE_VALUE is the type of the base-class.
11395 Non-NULL TREE_TYPE indicates virtual inheritance.
11397 Returns true if the binfo hierarchy was successfully created,
11398 false if an error was detected. */
11400 bool
11401 xref_basetypes (tree ref, tree base_list)
11403 tree *basep;
11404 tree binfo, base_binfo;
11405 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
11406 unsigned max_bases = 0; /* Maximum direct bases. */
11407 int i;
11408 tree default_access;
11409 tree igo_prev; /* Track Inheritance Graph Order. */
11411 if (ref == error_mark_node)
11412 return false;
11414 /* The base of a derived class is private by default, all others are
11415 public. */
11416 default_access = (TREE_CODE (ref) == RECORD_TYPE
11417 && CLASSTYPE_DECLARED_CLASS (ref)
11418 ? access_private_node : access_public_node);
11420 /* First, make sure that any templates in base-classes are
11421 instantiated. This ensures that if we call ourselves recursively
11422 we do not get confused about which classes are marked and which
11423 are not. */
11424 basep = &base_list;
11425 while (*basep)
11427 tree basetype = TREE_VALUE (*basep);
11429 if (!(processing_template_decl && uses_template_parms (basetype))
11430 && !complete_type_or_else (basetype, NULL))
11431 /* An incomplete type. Remove it from the list. */
11432 *basep = TREE_CHAIN (*basep);
11433 else
11435 max_bases++;
11436 if (TREE_TYPE (*basep))
11437 max_vbases++;
11438 if (CLASS_TYPE_P (basetype))
11439 max_vbases += VEC_length (tree, CLASSTYPE_VBASECLASSES (basetype));
11440 basep = &TREE_CHAIN (*basep);
11444 TYPE_MARKED_P (ref) = 1;
11446 /* The binfo slot should be empty, unless this is an (ill-formed)
11447 redefinition. */
11448 if (TYPE_BINFO (ref) && !TYPE_SIZE (ref))
11450 error ("redefinition of %q#T", ref);
11451 return false;
11454 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
11456 binfo = make_tree_binfo (max_bases);
11458 TYPE_BINFO (ref) = binfo;
11459 BINFO_OFFSET (binfo) = size_zero_node;
11460 BINFO_TYPE (binfo) = ref;
11462 /* Apply base-class info set up to the variants of this type. */
11463 fixup_type_variants (ref);
11465 if (max_bases)
11467 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, max_bases);
11468 /* An aggregate cannot have baseclasses. */
11469 CLASSTYPE_NON_AGGREGATE (ref) = 1;
11471 if (TREE_CODE (ref) == UNION_TYPE)
11473 error ("derived union %qT invalid", ref);
11474 return false;
11478 if (max_bases > 1)
11480 if (TYPE_FOR_JAVA (ref))
11482 error ("Java class %qT cannot have multiple bases", ref);
11483 return false;
11487 if (max_vbases)
11489 CLASSTYPE_VBASECLASSES (ref) = VEC_alloc (tree, gc, max_vbases);
11491 if (TYPE_FOR_JAVA (ref))
11493 error ("Java class %qT cannot have virtual bases", ref);
11494 return false;
11498 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
11500 tree access = TREE_PURPOSE (base_list);
11501 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
11502 tree basetype = TREE_VALUE (base_list);
11504 if (access == access_default_node)
11505 access = default_access;
11507 if (PACK_EXPANSION_P (basetype))
11508 basetype = PACK_EXPANSION_PATTERN (basetype);
11509 if (TREE_CODE (basetype) == TYPE_DECL)
11510 basetype = TREE_TYPE (basetype);
11511 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
11513 error ("base type %qT fails to be a struct or class type",
11514 basetype);
11515 return false;
11518 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
11519 TYPE_FOR_JAVA (ref) = 1;
11521 base_binfo = NULL_TREE;
11522 if (CLASS_TYPE_P (basetype) && !dependent_type_p (basetype))
11524 base_binfo = TYPE_BINFO (basetype);
11525 /* The original basetype could have been a typedef'd type. */
11526 basetype = BINFO_TYPE (base_binfo);
11528 /* Inherit flags from the base. */
11529 TYPE_HAS_NEW_OPERATOR (ref)
11530 |= TYPE_HAS_NEW_OPERATOR (basetype);
11531 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
11532 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
11533 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
11534 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
11535 CLASSTYPE_DIAMOND_SHAPED_P (ref)
11536 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
11537 CLASSTYPE_REPEATED_BASE_P (ref)
11538 |= CLASSTYPE_REPEATED_BASE_P (basetype);
11541 /* We must do this test after we've seen through a typedef
11542 type. */
11543 if (TYPE_MARKED_P (basetype))
11545 if (basetype == ref)
11546 error ("recursive type %qT undefined", basetype);
11547 else
11548 error ("duplicate base type %qT invalid", basetype);
11549 return false;
11552 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
11553 /* Regenerate the pack expansion for the bases. */
11554 basetype = make_pack_expansion (basetype);
11556 TYPE_MARKED_P (basetype) = 1;
11558 base_binfo = copy_binfo (base_binfo, basetype, ref,
11559 &igo_prev, via_virtual);
11560 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
11561 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
11563 BINFO_BASE_APPEND (binfo, base_binfo);
11564 BINFO_BASE_ACCESS_APPEND (binfo, access);
11567 if (VEC_space (tree, CLASSTYPE_VBASECLASSES (ref), 1))
11568 /* If we have space in the vbase vector, we must have shared at
11569 least one of them, and are therefore diamond shaped. */
11570 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
11572 /* Unmark all the types. */
11573 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11574 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
11575 TYPE_MARKED_P (ref) = 0;
11577 /* Now see if we have a repeated base type. */
11578 if (!CLASSTYPE_REPEATED_BASE_P (ref))
11580 for (base_binfo = binfo; base_binfo;
11581 base_binfo = TREE_CHAIN (base_binfo))
11583 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
11585 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
11586 break;
11588 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
11590 for (base_binfo = binfo; base_binfo;
11591 base_binfo = TREE_CHAIN (base_binfo))
11592 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
11593 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
11594 else
11595 break;
11598 return true;
11602 /* Copies the enum-related properties from type SRC to type DST.
11603 Used with the underlying type of an enum and the enum itself. */
11604 static void
11605 copy_type_enum (tree dst, tree src)
11607 TYPE_MIN_VALUE (dst) = TYPE_MIN_VALUE (src);
11608 TYPE_MAX_VALUE (dst) = TYPE_MAX_VALUE (src);
11609 TYPE_SIZE (dst) = TYPE_SIZE (src);
11610 TYPE_SIZE_UNIT (dst) = TYPE_SIZE_UNIT (src);
11611 SET_TYPE_MODE (dst, TYPE_MODE (src));
11612 TYPE_PRECISION (dst) = TYPE_PRECISION (src);
11613 TYPE_ALIGN (dst) = TYPE_ALIGN (src);
11614 TYPE_USER_ALIGN (dst) = TYPE_USER_ALIGN (src);
11615 TYPE_UNSIGNED (dst) = TYPE_UNSIGNED (src);
11618 /* Begin compiling the definition of an enumeration type.
11619 NAME is its name,
11621 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
11623 UNDERLYING_TYPE is the type that will be used as the storage for
11624 the enumeration type. This should be NULL_TREE if no storage type
11625 was specified.
11627 SCOPED_ENUM_P is true if this is a scoped enumeration type.
11629 if IS_NEW is not NULL, gets TRUE iff a new type is created.
11631 Returns the type object, as yet incomplete.
11632 Also records info about it so that build_enumerator
11633 may be used to declare the individual values as they are read. */
11635 tree
11636 start_enum (tree name, tree enumtype, tree underlying_type,
11637 bool scoped_enum_p, bool *is_new)
11639 tree prevtype = NULL_TREE;
11640 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
11642 if (is_new)
11643 *is_new = false;
11644 /* [C++0x dcl.enum]p5:
11646 If not explicitly specified, the underlying type of a scoped
11647 enumeration type is int. */
11648 if (!underlying_type && scoped_enum_p)
11649 underlying_type = integer_type_node;
11651 if (underlying_type)
11652 underlying_type = cv_unqualified (underlying_type);
11654 /* If this is the real definition for a previous forward reference,
11655 fill in the contents in the same object that used to be the
11656 forward reference. */
11657 if (!enumtype)
11658 enumtype = lookup_and_check_tag (enum_type, name,
11659 /*tag_scope=*/ts_current,
11660 /*template_header_p=*/false);
11662 /* In case of a template_decl, the only check that should be deferred
11663 to instantiation time is the comparison of underlying types. */
11664 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
11666 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
11668 error_at (input_location, "scoped/unscoped mismatch "
11669 "in enum %q#T", enumtype);
11670 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
11671 "previous definition here");
11672 enumtype = error_mark_node;
11674 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
11676 error_at (input_location, "underlying type mismatch "
11677 "in enum %q#T", enumtype);
11678 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
11679 "previous definition here");
11680 enumtype = error_mark_node;
11682 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
11683 && !dependent_type_p (underlying_type)
11684 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
11685 && !same_type_p (underlying_type,
11686 ENUM_UNDERLYING_TYPE (enumtype)))
11688 error_at (input_location, "different underlying type "
11689 "in enum %q#T", enumtype);
11690 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
11691 "previous definition here");
11692 underlying_type = NULL_TREE;
11696 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
11697 || processing_template_decl)
11699 /* In case of error, make a dummy enum to allow parsing to
11700 continue. */
11701 if (enumtype == error_mark_node)
11703 name = make_anon_name ();
11704 enumtype = NULL_TREE;
11707 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
11708 of an opaque enum, or an opaque enum of an already defined
11709 enumeration (C++0x only).
11710 In any other case, it'll be NULL_TREE. */
11711 if (!enumtype)
11713 if (is_new)
11714 *is_new = true;
11716 prevtype = enumtype;
11717 enumtype = cxx_make_type (ENUMERAL_TYPE);
11718 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
11719 if (enumtype == error_mark_node)
11720 return error_mark_node;
11722 /* The enum is considered opaque until the opening '{' of the
11723 enumerator list. */
11724 SET_OPAQUE_ENUM_P (enumtype, true);
11725 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
11728 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
11730 if (underlying_type)
11732 if (CP_INTEGRAL_TYPE_P (underlying_type))
11734 copy_type_enum (enumtype, underlying_type);
11735 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
11737 else if (dependent_type_p (underlying_type))
11738 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
11739 else
11740 error ("underlying type %<%T%> of %<%T%> must be an integral type",
11741 underlying_type, enumtype);
11744 /* If into a template class, the returned enum is always the first
11745 declaration (opaque or not) seen. This way all the references to
11746 this type will be to the same declaration. The following ones are used
11747 only to check for definition errors. */
11748 if (prevtype && processing_template_decl)
11749 return prevtype;
11750 else
11751 return enumtype;
11754 /* After processing and defining all the values of an enumeration type,
11755 install their decls in the enumeration type.
11756 ENUMTYPE is the type object. */
11758 void
11759 finish_enum_value_list (tree enumtype)
11761 tree values;
11762 tree underlying_type;
11763 tree decl;
11764 tree value;
11765 tree minnode, maxnode;
11766 tree t;
11768 bool fixed_underlying_type_p
11769 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
11771 /* We built up the VALUES in reverse order. */
11772 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
11774 /* For an enum defined in a template, just set the type of the values;
11775 all further processing is postponed until the template is
11776 instantiated. We need to set the type so that tsubst of a CONST_DECL
11777 works. */
11778 if (processing_template_decl)
11780 for (values = TYPE_VALUES (enumtype);
11781 values;
11782 values = TREE_CHAIN (values))
11783 TREE_TYPE (TREE_VALUE (values)) = enumtype;
11784 return;
11787 /* Determine the minimum and maximum values of the enumerators. */
11788 if (TYPE_VALUES (enumtype))
11790 minnode = maxnode = NULL_TREE;
11792 for (values = TYPE_VALUES (enumtype);
11793 values;
11794 values = TREE_CHAIN (values))
11796 decl = TREE_VALUE (values);
11798 /* [dcl.enum]: Following the closing brace of an enum-specifier,
11799 each enumerator has the type of its enumeration. Prior to the
11800 closing brace, the type of each enumerator is the type of its
11801 initializing value. */
11802 TREE_TYPE (decl) = enumtype;
11804 /* Update the minimum and maximum values, if appropriate. */
11805 value = DECL_INITIAL (decl);
11806 if (value == error_mark_node)
11807 value = integer_zero_node;
11808 /* Figure out what the minimum and maximum values of the
11809 enumerators are. */
11810 if (!minnode)
11811 minnode = maxnode = value;
11812 else if (tree_int_cst_lt (maxnode, value))
11813 maxnode = value;
11814 else if (tree_int_cst_lt (value, minnode))
11815 minnode = value;
11818 else
11819 /* [dcl.enum]
11821 If the enumerator-list is empty, the underlying type is as if
11822 the enumeration had a single enumerator with value 0. */
11823 minnode = maxnode = integer_zero_node;
11825 if (!fixed_underlying_type_p)
11827 /* Compute the number of bits require to represent all values of the
11828 enumeration. We must do this before the type of MINNODE and
11829 MAXNODE are transformed, since tree_int_cst_min_precision relies
11830 on the TREE_TYPE of the value it is passed. */
11831 bool unsignedp = tree_int_cst_sgn (minnode) >= 0;
11832 int lowprec = tree_int_cst_min_precision (minnode, unsignedp);
11833 int highprec = tree_int_cst_min_precision (maxnode, unsignedp);
11834 int precision = MAX (lowprec, highprec);
11835 unsigned int itk;
11836 bool use_short_enum;
11838 /* Determine the underlying type of the enumeration.
11840 [dcl.enum]
11842 The underlying type of an enumeration is an integral type that
11843 can represent all the enumerator values defined in the
11844 enumeration. It is implementation-defined which integral type is
11845 used as the underlying type for an enumeration except that the
11846 underlying type shall not be larger than int unless the value of
11847 an enumerator cannot fit in an int or unsigned int.
11849 We use "int" or an "unsigned int" as the underlying type, even if
11850 a smaller integral type would work, unless the user has
11851 explicitly requested that we use the smallest possible type. The
11852 user can request that for all enumerations with a command line
11853 flag, or for just one enumeration with an attribute. */
11855 use_short_enum = flag_short_enums
11856 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
11858 for (itk = (use_short_enum ? itk_char : itk_int);
11859 itk != itk_none;
11860 itk++)
11862 underlying_type = integer_types[itk];
11863 if (underlying_type != NULL_TREE
11864 && TYPE_PRECISION (underlying_type) >= precision
11865 && TYPE_UNSIGNED (underlying_type) == unsignedp)
11866 break;
11868 if (itk == itk_none)
11870 /* DR 377
11872 IF no integral type can represent all the enumerator values, the
11873 enumeration is ill-formed. */
11874 error ("no integral type can represent all of the enumerator values "
11875 "for %qT", enumtype);
11876 precision = TYPE_PRECISION (long_long_integer_type_node);
11877 underlying_type = integer_types[itk_unsigned_long_long];
11880 /* [dcl.enum]
11882 The value of sizeof() applied to an enumeration type, an object
11883 of an enumeration type, or an enumerator, is the value of sizeof()
11884 applied to the underlying type. */
11885 copy_type_enum (enumtype, underlying_type);
11887 /* Compute the minimum and maximum values for the type.
11889 [dcl.enum]
11891 For an enumeration where emin is the smallest enumerator and emax
11892 is the largest, the values of the enumeration are the values of the
11893 underlying type in the range bmin to bmax, where bmin and bmax are,
11894 respectively, the smallest and largest values of the smallest bit-
11895 field that can store emin and emax. */
11897 /* The middle-end currently assumes that types with TYPE_PRECISION
11898 narrower than their underlying type are suitably zero or sign
11899 extended to fill their mode. Similarly, it assumes that the front
11900 end assures that a value of a particular type must be within
11901 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
11903 We used to set these fields based on bmin and bmax, but that led
11904 to invalid assumptions like optimizing away bounds checking. So
11905 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
11906 TYPE_MAX_VALUE to the values for the mode above and only restrict
11907 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
11908 ENUM_UNDERLYING_TYPE (enumtype)
11909 = build_distinct_type_copy (underlying_type);
11910 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
11911 set_min_and_max_values_for_integral_type
11912 (ENUM_UNDERLYING_TYPE (enumtype), precision, unsignedp);
11914 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
11915 if (flag_strict_enums)
11916 set_min_and_max_values_for_integral_type (enumtype, precision,
11917 unsignedp);
11919 else
11920 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
11922 /* Convert each of the enumerators to the type of the underlying
11923 type of the enumeration. */
11924 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
11926 location_t saved_location;
11928 decl = TREE_VALUE (values);
11929 saved_location = input_location;
11930 input_location = DECL_SOURCE_LOCATION (decl);
11931 if (fixed_underlying_type_p)
11932 /* If the enumeration type has a fixed underlying type, we
11933 already checked all of the enumerator values. */
11934 value = DECL_INITIAL (decl);
11935 else
11936 value = perform_implicit_conversion (underlying_type,
11937 DECL_INITIAL (decl),
11938 tf_warning_or_error);
11939 input_location = saved_location;
11941 /* Do not clobber shared ints. */
11942 value = copy_node (value);
11944 TREE_TYPE (value) = enumtype;
11945 DECL_INITIAL (decl) = value;
11948 /* Fix up all variant types of this enum type. */
11949 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
11950 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
11952 /* Finish debugging output for this type. */
11953 rest_of_type_compilation (enumtype, namespace_bindings_p ());
11956 /* Finishes the enum type. This is called only the first time an
11957 enumeration is seen, be it opaque or odinary.
11958 ENUMTYPE is the type object. */
11960 void
11961 finish_enum (tree enumtype)
11963 if (processing_template_decl)
11965 if (at_function_scope_p ())
11966 add_stmt (build_min (TAG_DEFN, enumtype));
11967 return;
11970 /* Here there should not be any variants of this type. */
11971 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
11972 && !TYPE_NEXT_VARIANT (enumtype));
11975 /* Build and install a CONST_DECL for an enumeration constant of the
11976 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
11977 LOC is the location of NAME.
11978 Assignment of sequential values by default is handled here. */
11980 void
11981 build_enumerator (tree name, tree value, tree enumtype, location_t loc)
11983 tree decl;
11984 tree context;
11985 tree type;
11987 /* If the VALUE was erroneous, pretend it wasn't there; that will
11988 result in the enum being assigned the next value in sequence. */
11989 if (value == error_mark_node)
11990 value = NULL_TREE;
11992 /* Remove no-op casts from the value. */
11993 if (value)
11994 STRIP_TYPE_NOPS (value);
11996 if (! processing_template_decl)
11998 /* Validate and default VALUE. */
11999 if (value != NULL_TREE)
12001 value = cxx_constant_value (value);
12003 if (TREE_CODE (value) == INTEGER_CST
12004 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
12006 value = perform_integral_promotions (value);
12008 else
12010 error ("enumerator value for %qD is not an integer constant", name);
12011 value = NULL_TREE;
12015 /* Default based on previous value. */
12016 if (value == NULL_TREE)
12018 if (TYPE_VALUES (enumtype))
12020 HOST_WIDE_INT hi;
12021 unsigned HOST_WIDE_INT lo;
12022 tree prev_value;
12023 bool overflowed;
12025 /* The next value is the previous value plus one.
12026 add_double doesn't know the type of the target expression,
12027 so we must check with int_fits_type_p as well. */
12028 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
12029 if (error_operand_p (prev_value))
12030 value = error_mark_node;
12031 else
12033 overflowed = add_double (TREE_INT_CST_LOW (prev_value),
12034 TREE_INT_CST_HIGH (prev_value),
12035 1, 0, &lo, &hi);
12036 value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi);
12037 overflowed
12038 |= !int_fits_type_p (value, TREE_TYPE (prev_value));
12040 if (overflowed)
12042 error ("overflow in enumeration values at %qD", name);
12043 value = error_mark_node;
12047 else
12048 value = integer_zero_node;
12051 /* Remove no-op casts from the value. */
12052 STRIP_TYPE_NOPS (value);
12054 /* If the underlying type of the enum is fixed, check whether
12055 the enumerator values fits in the underlying type. If it
12056 does not fit, the program is ill-formed [C++0x dcl.enum]. */
12057 if (ENUM_UNDERLYING_TYPE (enumtype)
12058 && value
12059 && TREE_CODE (value) == INTEGER_CST
12060 && !int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
12062 error ("enumerator value %E is too large for underlying type %<%T%>",
12063 value, ENUM_UNDERLYING_TYPE (enumtype));
12065 /* Silently convert the value so that we can continue. */
12066 value = perform_implicit_conversion (ENUM_UNDERLYING_TYPE (enumtype),
12067 value, tf_none);
12071 /* C++ associates enums with global, function, or class declarations. */
12072 context = current_scope ();
12074 /* Build the actual enumeration constant. Note that the enumeration
12075 constants have the underlying type of the enum (if it is fixed)
12076 or the type of their initializer (if the underlying type of the
12077 enum is not fixed):
12079 [ C++0x dcl.enum ]
12081 If the underlying type is fixed, the type of each enumerator
12082 prior to the closing brace is the underlying type; if the
12083 initializing value of an enumerator cannot be represented by
12084 the underlying type, the program is ill-formed. If the
12085 underlying type is not fixed, the type of each enumerator is
12086 the type of its initializing value.
12088 If the underlying type is not fixed, it will be computed by
12089 finish_enum and we will reset the type of this enumerator. Of
12090 course, if we're processing a template, there may be no value. */
12091 type = value ? TREE_TYPE (value) : NULL_TREE;
12093 if (context && context == current_class_type)
12094 /* This enum declaration is local to the class. We need the full
12095 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
12096 decl = build_lang_decl_loc (loc, CONST_DECL, name, type);
12097 else
12098 /* It's a global enum, or it's local to a function. (Note local to
12099 a function could mean local to a class method. */
12100 decl = build_decl (loc, CONST_DECL, name, type);
12102 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
12103 TREE_CONSTANT (decl) = 1;
12104 TREE_READONLY (decl) = 1;
12105 DECL_INITIAL (decl) = value;
12107 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
12108 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
12109 on the TYPE_FIELDS list for `S'. (That's so that you can say
12110 things like `S::i' later.) */
12111 finish_member_declaration (decl);
12112 else
12113 pushdecl (decl);
12115 /* Add this enumeration constant to the list for this type. */
12116 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
12119 /* Look for an enumerator with the given NAME within the enumeration
12120 type ENUMTYPE. This routine is used primarily for qualified name
12121 lookup into an enumerator in C++0x, e.g.,
12123 enum class Color { Red, Green, Blue };
12125 Color color = Color::Red;
12127 Returns the value corresponding to the enumerator, or
12128 NULL_TREE if no such enumerator was found. */
12129 tree
12130 lookup_enumerator (tree enumtype, tree name)
12132 tree e;
12133 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
12135 e = purpose_member (name, TYPE_VALUES (enumtype));
12136 return e? TREE_VALUE (e) : NULL_TREE;
12140 /* We're defining DECL. Make sure that its type is OK. */
12142 static void
12143 check_function_type (tree decl, tree current_function_parms)
12145 tree fntype = TREE_TYPE (decl);
12146 tree return_type = complete_type (TREE_TYPE (fntype));
12148 /* In a function definition, arg types must be complete. */
12149 require_complete_types_for_parms (current_function_parms);
12151 if (dependent_type_p (return_type))
12152 return;
12153 if (!COMPLETE_OR_VOID_TYPE_P (return_type)
12154 || (TYPE_FOR_JAVA (return_type) && MAYBE_CLASS_TYPE_P (return_type)))
12156 tree args = TYPE_ARG_TYPES (fntype);
12158 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
12159 error ("return type %q#T is incomplete", return_type);
12160 else
12161 error ("return type has Java class type %q#T", return_type);
12163 /* Make it return void instead. */
12164 if (TREE_CODE (fntype) == METHOD_TYPE)
12165 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
12166 void_type_node,
12167 TREE_CHAIN (args));
12168 else
12169 fntype = build_function_type (void_type_node, args);
12170 fntype
12171 = build_exception_variant (fntype,
12172 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
12173 fntype = (cp_build_type_attribute_variant
12174 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
12175 TREE_TYPE (decl) = fntype;
12177 else
12178 abstract_virtuals_error (decl, TREE_TYPE (fntype));
12181 /* Create the FUNCTION_DECL for a function definition.
12182 DECLSPECS and DECLARATOR are the parts of the declaration;
12183 they describe the function's name and the type it returns,
12184 but twisted together in a fashion that parallels the syntax of C.
12186 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
12187 DECLARATOR is really the DECL for the function we are about to
12188 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
12189 indicating that the function is an inline defined in-class.
12191 This function creates a binding context for the function body
12192 as well as setting up the FUNCTION_DECL in current_function_decl.
12194 For C++, we must first check whether that datum makes any sense.
12195 For example, "class A local_a(1,2);" means that variable local_a
12196 is an aggregate of type A, which should have a constructor
12197 applied to it with the argument list [1, 2].
12199 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
12200 or may be a BLOCK if the function has been defined previously
12201 in this translation unit. On exit, DECL_INITIAL (decl1) will be
12202 error_mark_node if the function has never been defined, or
12203 a BLOCK if the function has been defined somewhere. */
12205 void
12206 start_preparsed_function (tree decl1, tree attrs, int flags)
12208 tree ctype = NULL_TREE;
12209 tree fntype;
12210 tree restype;
12211 int doing_friend = 0;
12212 struct cp_binding_level *bl;
12213 tree current_function_parms;
12214 struct c_fileinfo *finfo
12215 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
12216 bool honor_interface;
12218 /* Sanity check. */
12219 gcc_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE);
12220 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
12222 fntype = TREE_TYPE (decl1);
12223 if (TREE_CODE (fntype) == METHOD_TYPE)
12224 ctype = TYPE_METHOD_BASETYPE (fntype);
12226 /* ISO C++ 11.4/5. A friend function defined in a class is in
12227 the (lexical) scope of the class in which it is defined. */
12228 if (!ctype && DECL_FRIEND_P (decl1))
12230 ctype = DECL_FRIEND_CONTEXT (decl1);
12232 /* CTYPE could be null here if we're dealing with a template;
12233 for example, `inline friend float foo()' inside a template
12234 will have no CTYPE set. */
12235 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
12236 ctype = NULL_TREE;
12237 else
12238 doing_friend = 1;
12241 if (DECL_DECLARED_INLINE_P (decl1)
12242 && lookup_attribute ("noinline", attrs))
12243 warning (0, "inline function %q+D given attribute noinline", decl1);
12245 /* Handle gnu_inline attribute. */
12246 if (GNU_INLINE_P (decl1))
12248 DECL_EXTERNAL (decl1) = 1;
12249 DECL_NOT_REALLY_EXTERN (decl1) = 0;
12250 DECL_INTERFACE_KNOWN (decl1) = 1;
12251 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
12254 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
12255 /* This is a constructor, we must ensure that any default args
12256 introduced by this definition are propagated to the clones
12257 now. The clones are used directly in overload resolution. */
12258 adjust_clone_args (decl1);
12260 /* Sometimes we don't notice that a function is a static member, and
12261 build a METHOD_TYPE for it. Fix that up now. */
12262 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
12263 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
12265 revert_static_member_fn (decl1);
12266 ctype = NULL_TREE;
12269 /* Set up current_class_type, and enter the scope of the class, if
12270 appropriate. */
12271 if (ctype)
12272 push_nested_class (ctype);
12273 else if (DECL_STATIC_FUNCTION_P (decl1))
12274 push_nested_class (DECL_CONTEXT (decl1));
12276 /* Now that we have entered the scope of the class, we must restore
12277 the bindings for any template parameters surrounding DECL1, if it
12278 is an inline member template. (Order is important; consider the
12279 case where a template parameter has the same name as a field of
12280 the class.) It is not until after this point that
12281 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
12282 if (flags & SF_INCLASS_INLINE)
12283 maybe_begin_member_template_processing (decl1);
12285 /* Effective C++ rule 15. */
12286 if (warn_ecpp
12287 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
12288 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
12289 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
12291 /* Make the init_value nonzero so pushdecl knows this is not tentative.
12292 error_mark_node is replaced below (in poplevel) with the BLOCK. */
12293 if (!DECL_INITIAL (decl1))
12294 DECL_INITIAL (decl1) = error_mark_node;
12296 /* This function exists in static storage.
12297 (This does not mean `static' in the C sense!) */
12298 TREE_STATIC (decl1) = 1;
12300 /* We must call push_template_decl after current_class_type is set
12301 up. (If we are processing inline definitions after exiting a
12302 class scope, current_class_type will be NULL_TREE until set above
12303 by push_nested_class.) */
12304 if (processing_template_decl)
12306 /* FIXME: Handle error_mark_node more gracefully. */
12307 tree newdecl1 = push_template_decl (decl1);
12308 if (newdecl1 != error_mark_node)
12309 decl1 = newdecl1;
12312 /* We are now in the scope of the function being defined. */
12313 current_function_decl = decl1;
12315 /* Save the parm names or decls from this function's declarator
12316 where store_parm_decls will find them. */
12317 current_function_parms = DECL_ARGUMENTS (decl1);
12319 /* Make sure the parameter and return types are reasonable. When
12320 you declare a function, these types can be incomplete, but they
12321 must be complete when you define the function. */
12322 check_function_type (decl1, current_function_parms);
12324 /* Build the return declaration for the function. */
12325 restype = TREE_TYPE (fntype);
12326 if (DECL_RESULT (decl1) == NULL_TREE)
12328 tree resdecl;
12330 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
12331 DECL_ARTIFICIAL (resdecl) = 1;
12332 DECL_IGNORED_P (resdecl) = 1;
12333 DECL_RESULT (decl1) = resdecl;
12335 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
12338 /* Let the user know we're compiling this function. */
12339 announce_function (decl1);
12341 /* Record the decl so that the function name is defined.
12342 If we already have a decl for this name, and it is a FUNCTION_DECL,
12343 use the old decl. */
12344 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
12346 /* A specialization is not used to guide overload resolution. */
12347 if (!DECL_FUNCTION_MEMBER_P (decl1)
12348 && !(DECL_USE_TEMPLATE (decl1) &&
12349 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
12351 tree olddecl = pushdecl (decl1);
12353 if (olddecl == error_mark_node)
12354 /* If something went wrong when registering the declaration,
12355 use DECL1; we have to have a FUNCTION_DECL to use when
12356 parsing the body of the function. */
12358 else
12360 /* Otherwise, OLDDECL is either a previous declaration
12361 of the same function or DECL1 itself. */
12363 if (warn_missing_declarations
12364 && olddecl == decl1
12365 && !DECL_MAIN_P (decl1)
12366 && TREE_PUBLIC (decl1)
12367 && !DECL_DECLARED_INLINE_P (decl1))
12369 tree context;
12371 /* Check whether DECL1 is in an anonymous
12372 namespace. */
12373 for (context = DECL_CONTEXT (decl1);
12374 context;
12375 context = DECL_CONTEXT (context))
12377 if (TREE_CODE (context) == NAMESPACE_DECL
12378 && DECL_NAME (context) == NULL_TREE)
12379 break;
12382 if (context == NULL)
12383 warning (OPT_Wmissing_declarations,
12384 "no previous declaration for %q+D", decl1);
12387 decl1 = olddecl;
12390 else
12392 /* We need to set the DECL_CONTEXT. */
12393 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
12394 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
12396 fntype = TREE_TYPE (decl1);
12398 /* If #pragma weak applies, mark the decl appropriately now.
12399 The pragma only applies to global functions. Because
12400 determining whether or not the #pragma applies involves
12401 computing the mangled name for the declaration, we cannot
12402 apply the pragma until after we have merged this declaration
12403 with any previous declarations; if the original declaration
12404 has a linkage specification, that specification applies to
12405 the definition as well, and may affect the mangled name. */
12406 if (DECL_FILE_SCOPE_P (decl1))
12407 maybe_apply_pragma_weak (decl1);
12410 /* constexpr functions must have literal argument types and
12411 literal return type. */
12412 validate_constexpr_fundecl (decl1);
12414 /* Reset this in case the call to pushdecl changed it. */
12415 current_function_decl = decl1;
12417 gcc_assert (DECL_INITIAL (decl1));
12419 /* This function may already have been parsed, in which case just
12420 return; our caller will skip over the body without parsing. */
12421 if (DECL_INITIAL (decl1) != error_mark_node)
12422 return;
12424 /* Initialize RTL machinery. We cannot do this until
12425 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
12426 even when processing a template; this is how we get
12427 CFUN set up, and our per-function variables initialized.
12428 FIXME factor out the non-RTL stuff. */
12429 bl = current_binding_level;
12430 allocate_struct_function (decl1, processing_template_decl);
12432 /* Initialize the language data structures. Whenever we start
12433 a new function, we destroy temporaries in the usual way. */
12434 cfun->language = ggc_alloc_cleared_language_function ();
12435 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
12436 current_binding_level = bl;
12438 /* Even though we're inside a function body, we still don't want to
12439 call expand_expr to calculate the size of a variable-sized array.
12440 We haven't necessarily assigned RTL to all variables yet, so it's
12441 not safe to try to expand expressions involving them. */
12442 cfun->dont_save_pending_sizes_p = 1;
12444 /* Start the statement-tree, start the tree now. */
12445 DECL_SAVED_TREE (decl1) = push_stmt_list ();
12447 /* If we are (erroneously) defining a function that we have already
12448 defined before, wipe out what we knew before. */
12449 if (!DECL_PENDING_INLINE_P (decl1))
12450 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
12452 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
12454 /* We know that this was set up by `grokclassfn'. We do not
12455 wait until `store_parm_decls', since evil parse errors may
12456 never get us to that point. Here we keep the consistency
12457 between `current_class_type' and `current_class_ptr'. */
12458 tree t = DECL_ARGUMENTS (decl1);
12460 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
12461 gcc_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE);
12463 cp_function_chain->x_current_class_ref
12464 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
12465 cp_function_chain->x_current_class_ptr = t;
12467 /* Constructors and destructors need to know whether they're "in
12468 charge" of initializing virtual base classes. */
12469 t = DECL_CHAIN (t);
12470 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
12472 current_in_charge_parm = t;
12473 t = DECL_CHAIN (t);
12475 if (DECL_HAS_VTT_PARM_P (decl1))
12477 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
12478 current_vtt_parm = t;
12482 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
12483 /* Implicitly-defined methods (like the
12484 destructor for a class in which no destructor
12485 is explicitly declared) must not be defined
12486 until their definition is needed. So, we
12487 ignore interface specifications for
12488 compiler-generated functions. */
12489 && !DECL_ARTIFICIAL (decl1));
12491 if (DECL_INTERFACE_KNOWN (decl1))
12493 tree ctx = decl_function_context (decl1);
12495 if (DECL_NOT_REALLY_EXTERN (decl1))
12496 DECL_EXTERNAL (decl1) = 0;
12498 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
12499 && TREE_PUBLIC (ctx))
12500 /* This is a function in a local class in an extern inline
12501 function. */
12502 comdat_linkage (decl1);
12504 /* If this function belongs to an interface, it is public.
12505 If it belongs to someone else's interface, it is also external.
12506 This only affects inlines and template instantiations. */
12507 else if (!finfo->interface_unknown && honor_interface)
12509 if (DECL_DECLARED_INLINE_P (decl1)
12510 || DECL_TEMPLATE_INSTANTIATION (decl1)
12511 || processing_template_decl)
12513 DECL_EXTERNAL (decl1)
12514 = (finfo->interface_only
12515 || (DECL_DECLARED_INLINE_P (decl1)
12516 && ! flag_implement_inlines
12517 && !DECL_VINDEX (decl1)));
12519 /* For WIN32 we also want to put these in linkonce sections. */
12520 maybe_make_one_only (decl1);
12522 else
12523 DECL_EXTERNAL (decl1) = 0;
12524 DECL_INTERFACE_KNOWN (decl1) = 1;
12525 /* If this function is in an interface implemented in this file,
12526 make sure that the back end knows to emit this function
12527 here. */
12528 if (!DECL_EXTERNAL (decl1))
12529 mark_needed (decl1);
12531 else if (finfo->interface_unknown && finfo->interface_only
12532 && honor_interface)
12534 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
12535 interface, we will have both finfo->interface_unknown and
12536 finfo->interface_only set. In that case, we don't want to
12537 use the normal heuristics because someone will supply a
12538 #pragma implementation elsewhere, and deducing it here would
12539 produce a conflict. */
12540 comdat_linkage (decl1);
12541 DECL_EXTERNAL (decl1) = 0;
12542 DECL_INTERFACE_KNOWN (decl1) = 1;
12543 DECL_DEFER_OUTPUT (decl1) = 1;
12545 else
12547 /* This is a definition, not a reference.
12548 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
12549 if (!GNU_INLINE_P (decl1))
12550 DECL_EXTERNAL (decl1) = 0;
12552 if ((DECL_DECLARED_INLINE_P (decl1)
12553 || DECL_TEMPLATE_INSTANTIATION (decl1))
12554 && ! DECL_INTERFACE_KNOWN (decl1))
12555 DECL_DEFER_OUTPUT (decl1) = 1;
12556 else
12557 DECL_INTERFACE_KNOWN (decl1) = 1;
12560 /* Determine the ELF visibility attribute for the function. We must not
12561 do this before calling "pushdecl", as we must allow "duplicate_decls"
12562 to merge any attributes appropriately. We also need to wait until
12563 linkage is set. */
12564 if (!DECL_CLONED_FUNCTION_P (decl1))
12565 determine_visibility (decl1);
12567 begin_scope (sk_function_parms, decl1);
12569 ++function_depth;
12571 if (DECL_DESTRUCTOR_P (decl1)
12572 || (DECL_CONSTRUCTOR_P (decl1)
12573 && targetm.cxx.cdtor_returns_this ()))
12575 cdtor_label = build_decl (input_location,
12576 LABEL_DECL, NULL_TREE, NULL_TREE);
12577 DECL_CONTEXT (cdtor_label) = current_function_decl;
12580 start_fname_decls ();
12582 store_parm_decls (current_function_parms);
12586 /* Like start_preparsed_function, except that instead of a
12587 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
12589 Returns 1 on success. If the DECLARATOR is not suitable for a function
12590 (it defines a datum instead), we return 0, which tells
12591 yyparse to report a parse error. */
12594 start_function (cp_decl_specifier_seq *declspecs,
12595 const cp_declarator *declarator,
12596 tree attrs)
12598 tree decl1;
12600 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
12601 if (decl1 == error_mark_node)
12602 return 0;
12603 /* If the declarator is not suitable for a function definition,
12604 cause a syntax error. */
12605 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
12607 error ("invalid function declaration");
12608 return 0;
12611 if (DECL_MAIN_P (decl1))
12612 /* main must return int. grokfndecl should have corrected it
12613 (and issued a diagnostic) if the user got it wrong. */
12614 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
12615 integer_type_node));
12617 start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
12619 return 1;
12622 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
12623 FN. */
12625 static bool
12626 use_eh_spec_block (tree fn)
12628 return (flag_exceptions && flag_enforce_eh_specs
12629 && !processing_template_decl
12630 && !type_throw_all_p (TREE_TYPE (fn))
12631 /* We insert the EH_SPEC_BLOCK only in the original
12632 function; then, it is copied automatically to the
12633 clones. */
12634 && !DECL_CLONED_FUNCTION_P (fn)
12635 /* Implicitly-generated constructors and destructors have
12636 exception specifications. However, those specifications
12637 are the union of the possible exceptions specified by the
12638 constructors/destructors for bases and members, so no
12639 unallowed exception will ever reach this function. By
12640 not creating the EH_SPEC_BLOCK we save a little memory,
12641 and we avoid spurious warnings about unreachable
12642 code. */
12643 && !DECL_ARTIFICIAL (fn));
12646 /* Store the parameter declarations into the current function declaration.
12647 This is called after parsing the parameter declarations, before
12648 digesting the body of the function.
12650 Also install to binding contour return value identifier, if any. */
12652 static void
12653 store_parm_decls (tree current_function_parms)
12655 tree fndecl = current_function_decl;
12656 tree parm;
12658 /* This is a chain of any other decls that came in among the parm
12659 declarations. If a parm is declared with enum {foo, bar} x;
12660 then CONST_DECLs for foo and bar are put here. */
12661 tree nonparms = NULL_TREE;
12663 if (current_function_parms)
12665 /* This case is when the function was defined with an ANSI prototype.
12666 The parms already have decls, so we need not do anything here
12667 except record them as in effect
12668 and complain if any redundant old-style parm decls were written. */
12670 tree specparms = current_function_parms;
12671 tree next;
12673 /* Must clear this because it might contain TYPE_DECLs declared
12674 at class level. */
12675 current_binding_level->names = NULL;
12677 /* If we're doing semantic analysis, then we'll call pushdecl
12678 for each of these. We must do them in reverse order so that
12679 they end in the correct forward order. */
12680 specparms = nreverse (specparms);
12682 for (parm = specparms; parm; parm = next)
12684 next = DECL_CHAIN (parm);
12685 if (TREE_CODE (parm) == PARM_DECL)
12687 if (DECL_NAME (parm) == NULL_TREE
12688 || TREE_CODE (parm) != VOID_TYPE)
12689 pushdecl (parm);
12690 else
12691 error ("parameter %qD declared void", parm);
12693 else
12695 /* If we find an enum constant or a type tag,
12696 put it aside for the moment. */
12697 TREE_CHAIN (parm) = NULL_TREE;
12698 nonparms = chainon (nonparms, parm);
12702 /* Get the decls in their original chain order and record in the
12703 function. This is all and only the PARM_DECLs that were
12704 pushed into scope by the loop above. */
12705 DECL_ARGUMENTS (fndecl) = getdecls ();
12707 else
12708 DECL_ARGUMENTS (fndecl) = NULL_TREE;
12710 /* Now store the final chain of decls for the arguments
12711 as the decl-chain of the current lexical scope.
12712 Put the enumerators in as well, at the front so that
12713 DECL_ARGUMENTS is not modified. */
12714 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
12716 if (use_eh_spec_block (current_function_decl))
12717 current_eh_spec_block = begin_eh_spec_block ();
12721 /* We have finished doing semantic analysis on DECL, but have not yet
12722 generated RTL for its body. Save away our current state, so that
12723 when we want to generate RTL later we know what to do. */
12725 static void
12726 save_function_data (tree decl)
12728 struct language_function *f;
12730 /* Save the language-specific per-function data so that we can
12731 get it back when we really expand this function. */
12732 gcc_assert (!DECL_PENDING_INLINE_P (decl));
12734 /* Make a copy. */
12735 f = ggc_alloc_language_function ();
12736 memcpy (f, cp_function_chain, sizeof (struct language_function));
12737 DECL_SAVED_FUNCTION_DATA (decl) = f;
12739 /* Clear out the bits we don't need. */
12740 f->base.x_stmt_tree.x_cur_stmt_list = NULL_TREE;
12741 f->bindings = NULL;
12742 f->x_local_names = NULL;
12746 /* Set the return value of the constructor (if present). */
12748 static void
12749 finish_constructor_body (void)
12751 tree val;
12752 tree exprstmt;
12754 if (targetm.cxx.cdtor_returns_this ()
12755 && (! TYPE_FOR_JAVA (current_class_type)))
12757 /* Any return from a constructor will end up here. */
12758 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
12760 val = DECL_ARGUMENTS (current_function_decl);
12761 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
12762 DECL_RESULT (current_function_decl), val);
12763 /* Return the address of the object. */
12764 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
12765 add_stmt (exprstmt);
12769 /* Do all the processing for the beginning of a destructor; set up the
12770 vtable pointers and cleanups for bases and members. */
12772 static void
12773 begin_destructor_body (void)
12775 tree compound_stmt;
12777 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
12778 issued an error message. We still want to try to process the
12779 body of the function, but initialize_vtbl_ptrs will crash if
12780 TYPE_BINFO is NULL. */
12781 if (COMPLETE_TYPE_P (current_class_type))
12783 compound_stmt = begin_compound_stmt (0);
12784 /* Make all virtual function table pointers in non-virtual base
12785 classes point to CURRENT_CLASS_TYPE's virtual function
12786 tables. */
12787 initialize_vtbl_ptrs (current_class_ptr);
12788 finish_compound_stmt (compound_stmt);
12790 /* And insert cleanups for our bases and members so that they
12791 will be properly destroyed if we throw. */
12792 push_base_cleanups ();
12796 /* At the end of every destructor we generate code to delete the object if
12797 necessary. Do that now. */
12799 static void
12800 finish_destructor_body (void)
12802 tree exprstmt;
12804 /* Any return from a destructor will end up here; that way all base
12805 and member cleanups will be run when the function returns. */
12806 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
12808 /* In a virtual destructor, we must call delete. */
12809 if (DECL_VIRTUAL_P (current_function_decl))
12811 tree if_stmt;
12812 tree virtual_size = cxx_sizeof (current_class_type);
12814 /* [class.dtor]
12816 At the point of definition of a virtual destructor (including
12817 an implicit definition), non-placement operator delete shall
12818 be looked up in the scope of the destructor's class and if
12819 found shall be accessible and unambiguous. */
12820 exprstmt = build_op_delete_call(DELETE_EXPR, current_class_ptr,
12821 virtual_size,
12822 /*global_p=*/false,
12823 /*placement=*/NULL_TREE,
12824 /*alloc_fn=*/NULL_TREE);
12826 if_stmt = begin_if_stmt ();
12827 finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
12828 current_in_charge_parm,
12829 integer_one_node),
12830 if_stmt);
12831 finish_expr_stmt (exprstmt);
12832 finish_then_clause (if_stmt);
12833 finish_if_stmt (if_stmt);
12836 if (targetm.cxx.cdtor_returns_this ())
12838 tree val;
12840 val = DECL_ARGUMENTS (current_function_decl);
12841 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
12842 DECL_RESULT (current_function_decl), val);
12843 /* Return the address of the object. */
12844 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
12845 add_stmt (exprstmt);
12849 /* Do the necessary processing for the beginning of a function body, which
12850 in this case includes member-initializers, but not the catch clauses of
12851 a function-try-block. Currently, this means opening a binding level
12852 for the member-initializers (in a ctor) and member cleanups (in a dtor). */
12854 tree
12855 begin_function_body (void)
12857 tree stmt;
12859 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
12860 return NULL_TREE;
12862 if (processing_template_decl)
12863 /* Do nothing now. */;
12864 else
12865 /* Always keep the BLOCK node associated with the outermost pair of
12866 curly braces of a function. These are needed for correct
12867 operation of dwarfout.c. */
12868 keep_next_level (true);
12870 stmt = begin_compound_stmt (BCS_FN_BODY);
12872 if (processing_template_decl)
12873 /* Do nothing now. */;
12874 else if (DECL_DESTRUCTOR_P (current_function_decl))
12875 begin_destructor_body ();
12877 return stmt;
12880 /* Do the processing for the end of a function body. Currently, this means
12881 closing out the cleanups for fully-constructed bases and members, and in
12882 the case of the destructor, deleting the object if desired. Again, this
12883 is only meaningful for [cd]tors, since they are the only functions where
12884 there is a significant distinction between the main body and any
12885 function catch clauses. Handling, say, main() return semantics here
12886 would be wrong, as flowing off the end of a function catch clause for
12887 main() would also need to return 0. */
12889 void
12890 finish_function_body (tree compstmt)
12892 if (compstmt == NULL_TREE)
12893 return;
12895 /* Close the block. */
12896 finish_compound_stmt (compstmt);
12898 if (processing_template_decl)
12899 /* Do nothing now. */;
12900 else if (DECL_CONSTRUCTOR_P (current_function_decl))
12901 finish_constructor_body ();
12902 else if (DECL_DESTRUCTOR_P (current_function_decl))
12903 finish_destructor_body ();
12906 /* Given a function, returns the BLOCK corresponding to the outermost level
12907 of curly braces, skipping the artificial block created for constructor
12908 initializers. */
12910 tree
12911 outer_curly_brace_block (tree fndecl)
12913 tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl));
12914 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
12915 /* Skip the artificial function body block. */
12916 block = BLOCK_SUBBLOCKS (block);
12917 return block;
12920 /* If FNDECL is a class's key method, add the class to the list of
12921 keyed classes that should be emitted. */
12923 static void
12924 record_key_method_defined (tree fndecl)
12926 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
12927 && DECL_VIRTUAL_P (fndecl)
12928 && !processing_template_decl)
12930 tree fnclass = DECL_CONTEXT (fndecl);
12931 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
12932 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
12936 /* Subroutine of finish_function.
12937 Save the body of constexpr functions for possible
12938 future compile time evaluation. */
12940 static void
12941 maybe_save_function_definition (tree fun)
12943 if (!processing_template_decl
12944 && DECL_DECLARED_CONSTEXPR_P (fun)
12945 && !DECL_CLONED_FUNCTION_P (fun))
12946 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
12949 /* Finish up a function declaration and compile that function
12950 all the way to assembler language output. The free the storage
12951 for the function definition.
12953 FLAGS is a bitwise or of the following values:
12954 2 - INCLASS_INLINE
12955 We just finished processing the body of an in-class inline
12956 function definition. (This processing will have taken place
12957 after the class definition is complete.) */
12959 tree
12960 finish_function (int flags)
12962 tree fndecl = current_function_decl;
12963 tree fntype, ctype = NULL_TREE;
12964 int inclass_inline = (flags & 2) != 0;
12966 /* When we get some parse errors, we can end up without a
12967 current_function_decl, so cope. */
12968 if (fndecl == NULL_TREE)
12969 return error_mark_node;
12971 if (c_dialect_objc ())
12972 objc_finish_function ();
12974 gcc_assert (!defer_mark_used_calls);
12975 defer_mark_used_calls = true;
12977 record_key_method_defined (fndecl);
12979 fntype = TREE_TYPE (fndecl);
12981 /* TREE_READONLY (fndecl) = 1;
12982 This caused &foo to be of type ptr-to-const-function
12983 which then got a warning when stored in a ptr-to-function variable. */
12985 gcc_assert (building_stmt_tree ());
12986 /* The current function is being defined, so its DECL_INITIAL should
12987 be set, and unless there's a multiple definition, it should be
12988 error_mark_node. */
12989 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
12991 /* For a cloned function, we've already got all the code we need;
12992 there's no need to add any extra bits. */
12993 if (!DECL_CLONED_FUNCTION_P (fndecl))
12995 if (DECL_MAIN_P (current_function_decl))
12997 tree stmt;
12999 /* Make it so that `main' always returns 0 by default (or
13000 1 for VMS). */
13001 #if VMS_TARGET
13002 stmt = finish_return_stmt (integer_one_node);
13003 #else
13004 stmt = finish_return_stmt (integer_zero_node);
13005 #endif
13006 /* Hack. We don't want the middle-end to warn that this
13007 return is unreachable, so put the statement on the
13008 special line 0. */
13010 location_t linezero = linemap_line_start (line_table, 0, 1);
13011 SET_EXPR_LOCATION (stmt, linezero);
13015 if (use_eh_spec_block (current_function_decl))
13016 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
13017 (TREE_TYPE (current_function_decl)),
13018 current_eh_spec_block);
13021 /* If we're saving up tree structure, tie off the function now. */
13022 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
13024 finish_fname_decls ();
13026 /* If this function can't throw any exceptions, remember that. */
13027 if (!processing_template_decl
13028 && !cp_function_chain->can_throw
13029 && !flag_non_call_exceptions
13030 && !decl_replaceable_p (fndecl))
13031 TREE_NOTHROW (fndecl) = 1;
13033 /* This must come after expand_function_end because cleanups might
13034 have declarations (from inline functions) that need to go into
13035 this function's blocks. */
13037 /* If the current binding level isn't the outermost binding level
13038 for this function, either there is a bug, or we have experienced
13039 syntax errors and the statement tree is malformed. */
13040 if (current_binding_level->kind != sk_function_parms)
13042 /* Make sure we have already experienced errors. */
13043 gcc_assert (errorcount);
13045 /* Throw away the broken statement tree and extra binding
13046 levels. */
13047 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
13049 while (current_binding_level->kind != sk_function_parms)
13051 if (current_binding_level->kind == sk_class)
13052 pop_nested_class ();
13053 else
13054 poplevel (0, 0, 0);
13057 poplevel (1, 0, 1);
13059 /* Statements should always be full-expressions at the outermost set
13060 of curly braces for a function. */
13061 gcc_assert (stmts_are_full_exprs_p ());
13063 /* Save constexpr function body before it gets munged by
13064 the NRV transformation. */
13065 maybe_save_function_definition (fndecl);
13067 /* Set up the named return value optimization, if we can. Candidate
13068 variables are selected in check_return_expr. */
13069 if (current_function_return_value)
13071 tree r = current_function_return_value;
13072 tree outer;
13074 if (r != error_mark_node
13075 /* This is only worth doing for fns that return in memory--and
13076 simpler, since we don't have to worry about promoted modes. */
13077 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
13078 /* Only allow this for variables declared in the outer scope of
13079 the function so we know that their lifetime always ends with a
13080 return; see g++.dg/opt/nrv6.C. We could be more flexible if
13081 we were to do this optimization in tree-ssa. */
13082 && (outer = outer_curly_brace_block (fndecl))
13083 && chain_member (r, BLOCK_VARS (outer)))
13084 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
13086 current_function_return_value = NULL_TREE;
13089 /* Remember that we were in class scope. */
13090 if (current_class_name)
13091 ctype = current_class_type;
13093 /* Must mark the RESULT_DECL as being in this function. */
13094 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
13096 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
13097 to the FUNCTION_DECL node itself. */
13098 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
13100 /* Save away current state, if appropriate. */
13101 if (!processing_template_decl)
13102 save_function_data (fndecl);
13104 /* Complain if there's just no return statement. */
13105 if (warn_return_type
13106 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
13107 && !dependent_type_p (TREE_TYPE (fntype))
13108 && !current_function_returns_value && !current_function_returns_null
13109 /* Don't complain if we abort or throw. */
13110 && !current_function_returns_abnormally
13111 /* Don't complain if we are declared noreturn. */
13112 && !TREE_THIS_VOLATILE (fndecl)
13113 && !DECL_NAME (DECL_RESULT (fndecl))
13114 && !TREE_NO_WARNING (fndecl)
13115 /* Structor return values (if any) are set by the compiler. */
13116 && !DECL_CONSTRUCTOR_P (fndecl)
13117 && !DECL_DESTRUCTOR_P (fndecl))
13119 warning (OPT_Wreturn_type,
13120 "no return statement in function returning non-void");
13121 TREE_NO_WARNING (fndecl) = 1;
13124 /* Store the end of the function, so that we get good line number
13125 info for the epilogue. */
13126 cfun->function_end_locus = input_location;
13128 /* Complain about parameters that are only set, but never otherwise used. */
13129 if (warn_unused_but_set_parameter
13130 && !processing_template_decl
13131 && errorcount == unused_but_set_errorcount
13132 && !DECL_CLONED_FUNCTION_P (fndecl))
13134 tree decl;
13136 for (decl = DECL_ARGUMENTS (fndecl);
13137 decl;
13138 decl = DECL_CHAIN (decl))
13139 if (TREE_USED (decl)
13140 && TREE_CODE (decl) == PARM_DECL
13141 && !DECL_READ_P (decl)
13142 && DECL_NAME (decl)
13143 && !DECL_ARTIFICIAL (decl)
13144 && !TREE_NO_WARNING (decl)
13145 && !DECL_IN_SYSTEM_HEADER (decl)
13146 && TREE_TYPE (decl) != error_mark_node
13147 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13148 && (!CLASS_TYPE_P (TREE_TYPE (decl))
13149 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
13150 warning (OPT_Wunused_but_set_parameter,
13151 "parameter %q+D set but not used", decl);
13152 unused_but_set_errorcount = errorcount;
13155 /* Genericize before inlining. */
13156 if (!processing_template_decl)
13158 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
13159 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
13160 cp_genericize (fndecl);
13161 /* Clear out the bits we don't need. */
13162 f->x_current_class_ptr = NULL;
13163 f->x_current_class_ref = NULL;
13164 f->x_eh_spec_block = NULL;
13165 f->x_in_charge_parm = NULL;
13166 f->x_vtt_parm = NULL;
13167 f->x_return_value = NULL;
13168 f->bindings = NULL;
13169 f->extern_decl_map = NULL;
13171 /* Clear out the bits we don't need. */
13172 local_names = NULL;
13174 /* We're leaving the context of this function, so zap cfun. It's still in
13175 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
13176 set_cfun (NULL);
13177 current_function_decl = NULL;
13179 /* If this is an in-class inline definition, we may have to pop the
13180 bindings for the template parameters that we added in
13181 maybe_begin_member_template_processing when start_function was
13182 called. */
13183 if (inclass_inline)
13184 maybe_end_member_template_processing ();
13186 /* Leave the scope of the class. */
13187 if (ctype)
13188 pop_nested_class ();
13190 --function_depth;
13192 /* Clean up. */
13193 current_function_decl = NULL_TREE;
13195 defer_mark_used_calls = false;
13196 if (deferred_mark_used_calls)
13198 unsigned int i;
13199 tree decl;
13201 FOR_EACH_VEC_ELT (tree, deferred_mark_used_calls, i, decl)
13202 mark_used (decl);
13203 VEC_free (tree, gc, deferred_mark_used_calls);
13206 return fndecl;
13209 /* Create the FUNCTION_DECL for a function definition.
13210 DECLSPECS and DECLARATOR are the parts of the declaration;
13211 they describe the return type and the name of the function,
13212 but twisted together in a fashion that parallels the syntax of C.
13214 This function creates a binding context for the function body
13215 as well as setting up the FUNCTION_DECL in current_function_decl.
13217 Returns a FUNCTION_DECL on success.
13219 If the DECLARATOR is not suitable for a function (it defines a datum
13220 instead), we return 0, which tells yyparse to report a parse error.
13222 May return void_type_node indicating that this method is actually
13223 a friend. See grokfield for more details.
13225 Came here with a `.pushlevel' .
13227 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
13228 CHANGES TO CODE IN `grokfield'. */
13230 tree
13231 grokmethod (cp_decl_specifier_seq *declspecs,
13232 const cp_declarator *declarator, tree attrlist)
13234 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
13235 &attrlist);
13237 if (fndecl == error_mark_node)
13238 return error_mark_node;
13240 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
13242 error ("invalid member function declaration");
13243 return error_mark_node;
13246 if (attrlist)
13247 cplus_decl_attributes (&fndecl, attrlist, 0);
13249 /* Pass friends other than inline friend functions back. */
13250 if (fndecl == void_type_node)
13251 return fndecl;
13253 if (DECL_IN_AGGR_P (fndecl))
13255 if (DECL_CLASS_SCOPE_P (fndecl))
13256 error ("%qD is already defined in class %qT", fndecl,
13257 DECL_CONTEXT (fndecl));
13258 return error_mark_node;
13261 check_template_shadow (fndecl);
13263 DECL_DECLARED_INLINE_P (fndecl) = 1;
13264 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
13266 /* We process method specializations in finish_struct_1. */
13267 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
13269 fndecl = push_template_decl (fndecl);
13270 if (fndecl == error_mark_node)
13271 return fndecl;
13274 if (! DECL_FRIEND_P (fndecl))
13276 if (DECL_CHAIN (fndecl))
13278 fndecl = copy_node (fndecl);
13279 TREE_CHAIN (fndecl) = NULL_TREE;
13283 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
13285 DECL_IN_AGGR_P (fndecl) = 1;
13286 return fndecl;
13290 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
13291 we can lay it out later, when and if its type becomes complete. */
13293 void
13294 maybe_register_incomplete_var (tree var)
13296 gcc_assert (TREE_CODE (var) == VAR_DECL);
13298 /* Keep track of variables with incomplete types. */
13299 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
13300 && DECL_EXTERNAL (var))
13302 tree inner_type = TREE_TYPE (var);
13304 while (TREE_CODE (inner_type) == ARRAY_TYPE)
13305 inner_type = TREE_TYPE (inner_type);
13306 inner_type = TYPE_MAIN_VARIANT (inner_type);
13308 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
13309 /* RTTI TD entries are created while defining the type_info. */
13310 || (TYPE_LANG_SPECIFIC (inner_type)
13311 && TYPE_BEING_DEFINED (inner_type)))
13313 incomplete_var *iv
13314 = VEC_safe_push (incomplete_var, gc, incomplete_vars, NULL);
13315 iv->decl = var;
13316 iv->incomplete_type = inner_type;
13321 /* Called when a class type (given by TYPE) is defined. If there are
13322 any existing VAR_DECLs whose type has been completed by this
13323 declaration, update them now. */
13325 void
13326 complete_vars (tree type)
13328 unsigned ix;
13329 incomplete_var *iv;
13331 for (ix = 0; VEC_iterate (incomplete_var, incomplete_vars, ix, iv); )
13333 if (same_type_p (type, iv->incomplete_type))
13335 tree var = iv->decl;
13336 tree type = TREE_TYPE (var);
13337 /* Complete the type of the variable. The VAR_DECL itself
13338 will be laid out in expand_expr. */
13339 complete_type (type);
13340 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
13341 /* Remove this entry from the list. */
13342 VEC_unordered_remove (incomplete_var, incomplete_vars, ix);
13344 else
13345 ix++;
13348 /* Check for pending declarations which may have abstract type. */
13349 complete_type_check_abstract (type);
13352 /* If DECL is of a type which needs a cleanup, build and return an
13353 expression to perform that cleanup here. Return NULL_TREE if no
13354 cleanup need be done. */
13356 tree
13357 cxx_maybe_build_cleanup (tree decl)
13359 tree type;
13360 tree attr;
13361 tree cleanup;
13363 /* Assume no cleanup is required. */
13364 cleanup = NULL_TREE;
13366 if (error_operand_p (decl))
13367 return cleanup;
13369 /* Handle "__attribute__((cleanup))". We run the cleanup function
13370 before the destructor since the destructor is what actually
13371 terminates the lifetime of the object. */
13372 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
13373 if (attr)
13375 tree id;
13376 tree fn;
13377 tree arg;
13379 /* Get the name specified by the user for the cleanup function. */
13380 id = TREE_VALUE (TREE_VALUE (attr));
13381 /* Look up the name to find the cleanup function to call. It is
13382 important to use lookup_name here because that is what is
13383 used in c-common.c:handle_cleanup_attribute when performing
13384 initial checks on the attribute. Note that those checks
13385 include ensuring that the function found is not an overloaded
13386 function, or an object with an overloaded call operator,
13387 etc.; we can rely on the fact that the function found is an
13388 ordinary FUNCTION_DECL. */
13389 fn = lookup_name (id);
13390 arg = build_address (decl);
13391 mark_used (decl);
13392 cleanup = cp_build_function_call_nary (fn, tf_warning_or_error,
13393 arg, NULL_TREE);
13395 /* Handle ordinary C++ destructors. */
13396 type = TREE_TYPE (decl);
13397 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
13399 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
13400 bool has_vbases = (TREE_CODE (type) == RECORD_TYPE
13401 && CLASSTYPE_VBASECLASSES (type));
13402 tree addr;
13403 tree call;
13405 if (TREE_CODE (type) == ARRAY_TYPE)
13406 addr = decl;
13407 else
13408 addr = build_address (decl);
13410 /* Optimize for space over speed here. */
13411 if (!has_vbases || flag_expensive_optimizations)
13412 flags |= LOOKUP_NONVIRTUAL;
13414 call = build_delete (TREE_TYPE (addr), addr,
13415 sfk_complete_destructor, flags, 0);
13416 if (cleanup)
13417 cleanup = build_compound_expr (input_location, cleanup, call);
13418 else
13419 cleanup = call;
13422 return cleanup;
13425 /* When a stmt has been parsed, this function is called. */
13427 void
13428 finish_stmt (void)
13432 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
13433 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
13434 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
13436 tree
13437 static_fn_type (tree memfntype)
13439 tree fntype;
13440 tree args;
13442 if (TYPE_PTRMEMFUNC_P (memfntype))
13443 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
13444 if (POINTER_TYPE_P (memfntype)
13445 || TREE_CODE (memfntype) == FUNCTION_DECL)
13446 memfntype = TREE_TYPE (memfntype);
13447 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
13448 return memfntype;
13449 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
13450 args = TYPE_ARG_TYPES (memfntype);
13451 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
13452 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
13453 fntype = (cp_build_type_attribute_variant
13454 (fntype, TYPE_ATTRIBUTES (memfntype)));
13455 fntype = (build_exception_variant
13456 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
13457 return fntype;
13460 /* DECL was originally constructed as a non-static member function,
13461 but turned out to be static. Update it accordingly. */
13463 void
13464 revert_static_member_fn (tree decl)
13466 tree stype = static_fn_type (decl);
13468 if (type_memfn_quals (stype) != TYPE_UNQUALIFIED)
13470 error ("static member function %q#D declared with type qualifiers", decl);
13471 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED);
13473 TREE_TYPE (decl) = stype;
13475 if (DECL_ARGUMENTS (decl))
13476 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
13477 DECL_STATIC_FUNCTION_P (decl) = 1;
13480 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
13481 one of the language-independent trees. */
13483 enum cp_tree_node_structure_enum
13484 cp_tree_node_structure (union lang_tree_node * t)
13486 switch (TREE_CODE (&t->generic))
13488 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
13489 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
13490 case OVERLOAD: return TS_CP_OVERLOAD;
13491 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
13492 case PTRMEM_CST: return TS_CP_PTRMEM;
13493 case BASELINK: return TS_CP_BASELINK;
13494 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
13495 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
13496 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
13497 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
13498 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
13499 default: return TS_CP_GENERIC;
13503 /* Build the void_list_node (void_type_node having been created). */
13504 tree
13505 build_void_list_node (void)
13507 tree t = build_tree_list (NULL_TREE, void_type_node);
13508 return t;
13511 bool
13512 cp_missing_noreturn_ok_p (tree decl)
13514 /* A missing noreturn is ok for the `main' function. */
13515 return DECL_MAIN_P (decl);
13518 /* Return the COMDAT group into which DECL should be placed. */
13520 tree
13521 cxx_comdat_group (tree decl)
13523 tree name;
13525 /* Virtual tables, construction virtual tables, and virtual table
13526 tables all go in a single COMDAT group, named after the primary
13527 virtual table. */
13528 if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
13529 name = DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (DECL_CONTEXT (decl)));
13530 /* For all other DECLs, the COMDAT group is the mangled name of the
13531 declaration itself. */
13532 else
13534 while (DECL_THUNK_P (decl))
13536 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
13537 into the same section as the target function. In that case
13538 we must return target's name. */
13539 tree target = THUNK_TARGET (decl);
13540 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
13541 && DECL_SECTION_NAME (target) != NULL
13542 && DECL_ONE_ONLY (target))
13543 decl = target;
13544 else
13545 break;
13547 name = DECL_ASSEMBLER_NAME (decl);
13550 return name;
13553 #include "gt-cp-decl.h"