2010-07-27 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / gcc / cp / decl.c
blob3aaa20c0ed3823d778cdbcfead1f7b33b2031744
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
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-pragma.h"
49 #include "diagnostic.h"
50 #include "intl.h"
51 #include "debug.h"
52 #include "timevar.h"
53 #include "pointer-set.h"
54 #include "splay-tree.h"
55 #include "plugin.h"
57 /* Possible cases of bad specifiers type used by bad_specifiers. */
58 enum bad_spec_place {
59 BSP_VAR, /* variable */
60 BSP_PARM, /* parameter */
61 BSP_TYPE, /* type */
62 BSP_FIELD /* field */
65 static tree grokparms (tree parmlist, tree *);
66 static const char *redeclaration_error_message (tree, tree);
68 static int decl_jump_unsafe (tree);
69 static void require_complete_types_for_parms (tree);
70 static int ambi_op_p (enum tree_code);
71 static int unary_op_p (enum tree_code);
72 static void push_local_name (tree);
73 static tree grok_reference_init (tree, tree, tree, tree *);
74 static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
75 int, int, tree);
76 static void record_unknown_type (tree, const char *);
77 static tree builtin_function_1 (tree, tree, bool);
78 static tree build_library_fn_1 (tree, enum tree_code, tree);
79 static int member_function_or_else (tree, tree, enum overload_flags);
80 static void bad_specifiers (tree, enum bad_spec_place, int, int, int, int,
81 int);
82 static void check_for_uninitialized_const_var (tree);
83 static hashval_t typename_hash (const void *);
84 static int typename_compare (const void *, const void *);
85 static tree local_variable_p_walkfn (tree *, int *, void *);
86 static tree record_builtin_java_type (const char *, int);
87 static const char *tag_name (enum tag_types);
88 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
89 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
90 static void maybe_deduce_size_from_array_init (tree, tree);
91 static void layout_var_decl (tree);
92 static tree check_initializer (tree, tree, int, tree *);
93 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
94 static void save_function_data (tree);
95 static void check_function_type (tree, tree);
96 static void finish_constructor_body (void);
97 static void begin_destructor_body (void);
98 static void finish_destructor_body (void);
99 static void record_key_method_defined (tree);
100 static tree create_array_type_for_decl (tree, tree, tree);
101 static tree get_atexit_node (void);
102 static tree get_dso_handle_node (void);
103 static tree start_cleanup_fn (void);
104 static void end_cleanup_fn (void);
105 static tree cp_make_fname_decl (location_t, tree, int);
106 static void initialize_predefined_identifiers (void);
107 static tree check_special_function_return_type
108 (special_function_kind, tree, tree);
109 static tree push_cp_library_fn (enum tree_code, tree);
110 static tree build_cp_library_fn (tree, enum tree_code, tree);
111 static void store_parm_decls (tree);
112 static void initialize_local_var (tree, tree);
113 static void expand_static_init (tree, tree);
115 /* The following symbols are subsumed in the cp_global_trees array, and
116 listed here individually for documentation purposes.
118 C++ extensions
119 tree wchar_decl_node;
121 tree vtable_entry_type;
122 tree delta_type_node;
123 tree __t_desc_type_node;
125 tree class_type_node;
126 tree unknown_type_node;
128 Array type `vtable_entry_type[]'
130 tree vtbl_type_node;
131 tree vtbl_ptr_type_node;
133 Namespaces,
135 tree std_node;
136 tree abi_node;
138 A FUNCTION_DECL which can call `abort'. Not necessarily the
139 one that the user will declare, but sufficient to be called
140 by routines that want to abort the program.
142 tree abort_fndecl;
144 The FUNCTION_DECL for the default `::operator delete'.
146 tree global_delete_fndecl;
148 Used by RTTI
149 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
150 tree tinfo_var_id; */
152 tree cp_global_trees[CPTI_MAX];
154 /* Indicates that there is a type value in some namespace, although
155 that is not necessarily in scope at the moment. */
157 tree global_type_node;
159 /* The node that holds the "name" of the global scope. */
160 tree global_scope_name;
162 #define local_names cp_function_chain->x_local_names
164 /* A list of objects which have constructors or destructors
165 which reside in the global scope. The decl is stored in
166 the TREE_VALUE slot and the initializer is stored
167 in the TREE_PURPOSE slot. */
168 tree static_aggregates;
170 /* -- end of C++ */
172 /* A node for the integer constant 2. */
174 tree integer_two_node;
176 /* Used only for jumps to as-yet undefined labels, since jumps to
177 defined labels can have their validity checked immediately. */
179 struct GTY(()) named_label_use_entry {
180 struct named_label_use_entry *next;
181 /* The binding level to which this entry is *currently* attached.
182 This is initially the binding level in which the goto appeared,
183 but is modified as scopes are closed. */
184 struct cp_binding_level *binding_level;
185 /* The head of the names list that was current when the goto appeared,
186 or the inner scope popped. These are the decls that will *not* be
187 skipped when jumping to the label. */
188 tree names_in_scope;
189 /* The location of the goto, for error reporting. */
190 location_t o_goto_locus;
191 /* True if an OpenMP structured block scope has been closed since
192 the goto appeared. This means that the branch from the label will
193 illegally exit an OpenMP scope. */
194 bool in_omp_scope;
197 /* A list of all LABEL_DECLs in the function that have names. Here so
198 we can clear out their names' definitions at the end of the
199 function, and so we can check the validity of jumps to these labels. */
201 struct GTY(()) named_label_entry {
202 /* The decl itself. */
203 tree label_decl;
205 /* The binding level to which the label is *currently* attached.
206 This is initially set to the binding level in which the label
207 is defined, but is modified as scopes are closed. */
208 struct cp_binding_level *binding_level;
209 /* The head of the names list that was current when the label was
210 defined, or the inner scope popped. These are the decls that will
211 be skipped when jumping to the label. */
212 tree names_in_scope;
213 /* A vector of all decls from all binding levels that would be
214 crossed by a backward branch to the label. */
215 VEC(tree,gc) *bad_decls;
217 /* A list of uses of the label, before the label is defined. */
218 struct named_label_use_entry *uses;
220 /* The following bits are set after the label is defined, and are
221 updated as scopes are popped. They indicate that a backward jump
222 to the label will illegally enter a scope of the given flavor. */
223 bool in_try_scope;
224 bool in_catch_scope;
225 bool in_omp_scope;
228 #define named_labels cp_function_chain->x_named_labels
230 /* The number of function bodies which we are currently processing.
231 (Zero if we are at namespace scope, one inside the body of a
232 function, two inside the body of a function in a local class, etc.) */
233 int function_depth;
235 /* To avoid unwanted recursion, finish_function defers all mark_used calls
236 encountered during its execution until it finishes. */
237 bool defer_mark_used_calls;
238 VEC(tree, gc) *deferred_mark_used_calls;
240 /* States indicating how grokdeclarator() should handle declspecs marked
241 with __attribute__((deprecated)). An object declared as
242 __attribute__((deprecated)) suppresses warnings of uses of other
243 deprecated items. */
244 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
247 /* A list of VAR_DECLs whose type was incomplete at the time the
248 variable was declared. */
250 typedef struct GTY(()) incomplete_var_d {
251 tree decl;
252 tree incomplete_type;
253 } incomplete_var;
255 DEF_VEC_O(incomplete_var);
256 DEF_VEC_ALLOC_O(incomplete_var,gc);
258 static GTY(()) VEC(incomplete_var,gc) *incomplete_vars;
260 /* Returns the kind of template specialization we are currently
261 processing, given that it's declaration contained N_CLASS_SCOPES
262 explicit scope qualifications. */
264 tmpl_spec_kind
265 current_tmpl_spec_kind (int n_class_scopes)
267 int n_template_parm_scopes = 0;
268 int seen_specialization_p = 0;
269 int innermost_specialization_p = 0;
270 struct cp_binding_level *b;
272 /* Scan through the template parameter scopes. */
273 for (b = current_binding_level;
274 b->kind == sk_template_parms;
275 b = b->level_chain)
277 /* If we see a specialization scope inside a parameter scope,
278 then something is wrong. That corresponds to a declaration
279 like:
281 template <class T> template <> ...
283 which is always invalid since [temp.expl.spec] forbids the
284 specialization of a class member template if the enclosing
285 class templates are not explicitly specialized as well. */
286 if (b->explicit_spec_p)
288 if (n_template_parm_scopes == 0)
289 innermost_specialization_p = 1;
290 else
291 seen_specialization_p = 1;
293 else if (seen_specialization_p == 1)
294 return tsk_invalid_member_spec;
296 ++n_template_parm_scopes;
299 /* Handle explicit instantiations. */
300 if (processing_explicit_instantiation)
302 if (n_template_parm_scopes != 0)
303 /* We've seen a template parameter list during an explicit
304 instantiation. For example:
306 template <class T> template void f(int);
308 This is erroneous. */
309 return tsk_invalid_expl_inst;
310 else
311 return tsk_expl_inst;
314 if (n_template_parm_scopes < n_class_scopes)
315 /* We've not seen enough template headers to match all the
316 specialized classes present. For example:
318 template <class T> void R<T>::S<T>::f(int);
320 This is invalid; there needs to be one set of template
321 parameters for each class. */
322 return tsk_insufficient_parms;
323 else if (n_template_parm_scopes == n_class_scopes)
324 /* We're processing a non-template declaration (even though it may
325 be a member of a template class.) For example:
327 template <class T> void S<T>::f(int);
329 The `class T' matches the `S<T>', leaving no template headers
330 corresponding to the `f'. */
331 return tsk_none;
332 else if (n_template_parm_scopes > n_class_scopes + 1)
333 /* We've got too many template headers. For example:
335 template <> template <class T> void f (T);
337 There need to be more enclosing classes. */
338 return tsk_excessive_parms;
339 else
340 /* This must be a template. It's of the form:
342 template <class T> template <class U> void S<T>::f(U);
344 This is a specialization if the innermost level was a
345 specialization; otherwise it's just a definition of the
346 template. */
347 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
350 /* Exit the current scope. */
352 void
353 finish_scope (void)
355 poplevel (0, 0, 0);
358 /* When a label goes out of scope, check to see if that label was used
359 in a valid manner, and issue any appropriate warnings or errors. */
361 static void
362 pop_label (tree label, tree old_value)
364 if (!processing_template_decl)
366 if (DECL_INITIAL (label) == NULL_TREE)
368 location_t location;
370 error ("label %q+D used but not defined", label);
371 location = input_location; /* FIXME want (input_filename, (line)0) */
372 /* Avoid crashing later. */
373 define_label (location, DECL_NAME (label));
375 else
376 warn_for_unused_label (label);
379 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
382 /* At the end of a function, all labels declared within the function
383 go out of scope. BLOCK is the top-level block for the
384 function. */
386 static int
387 pop_labels_1 (void **slot, void *data)
389 struct named_label_entry *ent = (struct named_label_entry *) *slot;
390 tree block = (tree) data;
392 pop_label (ent->label_decl, NULL_TREE);
394 /* Put the labels into the "variables" of the top-level block,
395 so debugger can see them. */
396 DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
397 BLOCK_VARS (block) = ent->label_decl;
399 htab_clear_slot (named_labels, slot);
401 return 1;
404 static void
405 pop_labels (tree block)
407 if (named_labels)
409 htab_traverse (named_labels, pop_labels_1, block);
410 named_labels = NULL;
414 /* At the end of a block with local labels, restore the outer definition. */
416 static void
417 pop_local_label (tree label, tree old_value)
419 struct named_label_entry dummy;
420 void **slot;
422 pop_label (label, old_value);
424 dummy.label_decl = label;
425 slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
426 htab_clear_slot (named_labels, slot);
429 /* The following two routines are used to interface to Objective-C++.
430 The binding level is purposely treated as an opaque type. */
432 void *
433 objc_get_current_scope (void)
435 return current_binding_level;
438 /* The following routine is used by the NeXT-style SJLJ exceptions;
439 variables get marked 'volatile' so as to not be clobbered by
440 _setjmp()/_longjmp() calls. All variables in the current scope,
441 as well as parent scopes up to (but not including) ENCLOSING_BLK
442 shall be thusly marked. */
444 void
445 objc_mark_locals_volatile (void *enclosing_blk)
447 struct cp_binding_level *scope;
449 for (scope = current_binding_level;
450 scope && scope != enclosing_blk;
451 scope = scope->level_chain)
453 tree decl;
455 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
456 objc_volatilize_decl (decl);
458 /* Do not climb up past the current function. */
459 if (scope->kind == sk_function_parms)
460 break;
464 /* Update data for defined and undefined labels when leaving a scope. */
466 static int
467 poplevel_named_label_1 (void **slot, void *data)
469 struct named_label_entry *ent = (struct named_label_entry *) *slot;
470 struct cp_binding_level *bl = (struct cp_binding_level *) data;
471 struct cp_binding_level *obl = bl->level_chain;
473 if (ent->binding_level == bl)
475 tree decl;
477 for (decl = ent->names_in_scope; decl; decl = DECL_CHAIN (decl))
478 if (decl_jump_unsafe (decl))
479 VEC_safe_push (tree, gc, ent->bad_decls, decl);
481 ent->binding_level = obl;
482 ent->names_in_scope = obl->names;
483 switch (bl->kind)
485 case sk_try:
486 ent->in_try_scope = true;
487 break;
488 case sk_catch:
489 ent->in_catch_scope = true;
490 break;
491 case sk_omp:
492 ent->in_omp_scope = true;
493 break;
494 default:
495 break;
498 else if (ent->uses)
500 struct named_label_use_entry *use;
502 for (use = ent->uses; use ; use = use->next)
503 if (use->binding_level == bl)
505 use->binding_level = obl;
506 use->names_in_scope = obl->names;
507 if (bl->kind == sk_omp)
508 use->in_omp_scope = true;
512 return 1;
515 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
516 when errors were reported, except for -Werror-unused-but-set-*. */
517 static int unused_but_set_errorcount;
519 /* Exit a binding level.
520 Pop the level off, and restore the state of the identifier-decl mappings
521 that were in effect when this level was entered.
523 If KEEP == 1, this level had explicit declarations, so
524 and create a "block" (a BLOCK node) for the level
525 to record its declarations and subblocks for symbol table output.
527 If FUNCTIONBODY is nonzero, this level is the body of a function,
528 so create a block as if KEEP were set and also clear out all
529 label names.
531 If REVERSE is nonzero, reverse the order of decls before putting
532 them into the BLOCK. */
534 tree
535 poplevel (int keep, int reverse, int functionbody)
537 tree link;
538 /* The chain of decls was accumulated in reverse order.
539 Put it into forward order, just for cleanliness. */
540 tree decls;
541 tree subblocks;
542 tree block;
543 tree decl;
544 int leaving_for_scope;
545 scope_kind kind;
547 timevar_push (TV_NAME_LOOKUP);
548 restart:
550 block = NULL_TREE;
552 gcc_assert (current_binding_level->kind != sk_class);
554 if (current_binding_level->kind == sk_cleanup)
555 functionbody = 0;
556 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
558 gcc_assert (!VEC_length(cp_class_binding,
559 current_binding_level->class_shadowed));
561 /* We used to use KEEP == 2 to indicate that the new block should go
562 at the beginning of the list of blocks at this binding level,
563 rather than the end. This hack is no longer used. */
564 gcc_assert (keep == 0 || keep == 1);
566 if (current_binding_level->keep)
567 keep = 1;
569 /* Any uses of undefined labels, and any defined labels, now operate
570 under constraints of next binding contour. */
571 if (cfun && !functionbody && named_labels)
572 htab_traverse (named_labels, poplevel_named_label_1,
573 current_binding_level);
575 /* Get the decls in the order they were written.
576 Usually current_binding_level->names is in reverse order.
577 But parameter decls were previously put in forward order. */
579 if (reverse)
580 current_binding_level->names
581 = decls = nreverse (current_binding_level->names);
582 else
583 decls = current_binding_level->names;
585 /* If there were any declarations or structure tags in that level,
586 or if this level is a function body,
587 create a BLOCK to record them for the life of this function. */
588 block = NULL_TREE;
589 if (keep == 1 || functionbody)
590 block = make_node (BLOCK);
591 if (block != NULL_TREE)
593 BLOCK_VARS (block) = decls;
594 BLOCK_SUBBLOCKS (block) = subblocks;
597 /* In each subblock, record that this is its superior. */
598 if (keep >= 0)
599 for (link = subblocks; link; link = BLOCK_CHAIN (link))
600 BLOCK_SUPERCONTEXT (link) = block;
602 /* We still support the old for-scope rules, whereby the variables
603 in a for-init statement were in scope after the for-statement
604 ended. We only use the new rules if flag_new_for_scope is
605 nonzero. */
606 leaving_for_scope
607 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
609 /* Before we remove the declarations first check for unused variables. */
610 if ((warn_unused_variable || warn_unused_but_set_variable)
611 && !processing_template_decl)
612 for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
613 if (TREE_CODE (decl) == VAR_DECL
614 && (! TREE_USED (decl) || !DECL_READ_P (decl))
615 && ! DECL_IN_SYSTEM_HEADER (decl)
616 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
618 if (! TREE_USED (decl))
619 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
620 else if (DECL_CONTEXT (decl) == current_function_decl
621 && TREE_TYPE (decl) != error_mark_node
622 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
623 && errorcount == unused_but_set_errorcount
624 && (!CLASS_TYPE_P (TREE_TYPE (decl))
625 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
627 warning (OPT_Wunused_but_set_variable,
628 "variable %q+D set but not used", decl);
629 unused_but_set_errorcount = errorcount;
633 /* Remove declarations for all the DECLs in this level. */
634 for (link = decls; link; link = TREE_CHAIN (link))
636 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
637 && DECL_NAME (link))
639 tree name = DECL_NAME (link);
640 cxx_binding *ob;
641 tree ns_binding;
643 ob = outer_binding (name,
644 IDENTIFIER_BINDING (name),
645 /*class_p=*/true);
646 if (!ob)
647 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
648 else
649 ns_binding = NULL_TREE;
651 if (ob && ob->scope == current_binding_level->level_chain)
652 /* We have something like:
654 int i;
655 for (int i; ;);
657 and we are leaving the `for' scope. There's no reason to
658 keep the binding of the inner `i' in this case. */
659 pop_binding (name, link);
660 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
661 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
662 /* Here, we have something like:
664 typedef int I;
666 void f () {
667 for (int I; ;);
670 We must pop the for-scope binding so we know what's a
671 type and what isn't. */
672 pop_binding (name, link);
673 else
675 /* Mark this VAR_DECL as dead so that we can tell we left it
676 there only for backward compatibility. */
677 DECL_DEAD_FOR_LOCAL (link) = 1;
679 /* Keep track of what should have happened when we
680 popped the binding. */
681 if (ob && ob->value)
683 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
684 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
687 /* Add it to the list of dead variables in the next
688 outermost binding to that we can remove these when we
689 leave that binding. */
690 current_binding_level->level_chain->dead_vars_from_for
691 = tree_cons (NULL_TREE, link,
692 current_binding_level->level_chain->
693 dead_vars_from_for);
695 /* Although we don't pop the cxx_binding, we do clear
696 its SCOPE since the scope is going away now. */
697 IDENTIFIER_BINDING (name)->scope
698 = current_binding_level->level_chain;
701 else
703 tree name;
705 /* Remove the binding. */
706 decl = link;
708 if (TREE_CODE (decl) == TREE_LIST)
709 decl = TREE_VALUE (decl);
710 name = decl;
712 if (TREE_CODE (name) == OVERLOAD)
713 name = OVL_FUNCTION (name);
715 gcc_assert (DECL_P (name));
716 pop_binding (DECL_NAME (name), decl);
720 /* Remove declarations for any `for' variables from inner scopes
721 that we kept around. */
722 for (link = current_binding_level->dead_vars_from_for;
723 link; link = TREE_CHAIN (link))
724 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
726 /* Restore the IDENTIFIER_TYPE_VALUEs. */
727 for (link = current_binding_level->type_shadowed;
728 link; link = TREE_CHAIN (link))
729 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
731 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
732 for (link = current_binding_level->shadowed_labels;
733 link;
734 link = TREE_CHAIN (link))
735 pop_local_label (TREE_VALUE (link), TREE_PURPOSE (link));
737 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
738 list if a `using' declaration put them there. The debugging
739 back ends won't understand OVERLOAD, so we remove them here.
740 Because the BLOCK_VARS are (temporarily) shared with
741 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
742 popped all the bindings. */
743 if (block)
745 tree* d;
747 for (d = &BLOCK_VARS (block); *d; )
749 if (TREE_CODE (*d) == TREE_LIST)
750 *d = TREE_CHAIN (*d);
751 else
752 d = &DECL_CHAIN (*d);
756 /* If the level being exited is the top level of a function,
757 check over all the labels. */
758 if (functionbody)
760 /* Since this is the top level block of a function, the vars are
761 the function's parameters. Don't leave them in the BLOCK
762 because they are found in the FUNCTION_DECL instead. */
763 BLOCK_VARS (block) = 0;
764 pop_labels (block);
767 kind = current_binding_level->kind;
768 if (kind == sk_cleanup)
770 tree stmt;
772 /* If this is a temporary binding created for a cleanup, then we'll
773 have pushed a statement list level. Pop that, create a new
774 BIND_EXPR for the block, and insert it into the stream. */
775 stmt = pop_stmt_list (current_binding_level->statement_list);
776 stmt = c_build_bind_expr (input_location, block, stmt);
777 add_stmt (stmt);
780 leave_scope ();
781 if (functionbody)
783 /* The current function is being defined, so its DECL_INITIAL
784 should be error_mark_node. */
785 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
786 DECL_INITIAL (current_function_decl) = block;
788 else if (block)
789 current_binding_level->blocks
790 = chainon (current_binding_level->blocks, block);
792 /* If we did not make a block for the level just exited,
793 any blocks made for inner levels
794 (since they cannot be recorded as subblocks in that level)
795 must be carried forward so they will later become subblocks
796 of something else. */
797 else if (subblocks)
798 current_binding_level->blocks
799 = chainon (current_binding_level->blocks, subblocks);
801 /* Each and every BLOCK node created here in `poplevel' is important
802 (e.g. for proper debugging information) so if we created one
803 earlier, mark it as "used". */
804 if (block)
805 TREE_USED (block) = 1;
807 /* All temporary bindings created for cleanups are popped silently. */
808 if (kind == sk_cleanup)
809 goto restart;
811 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
814 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
815 itself, calling F for each. The DATA is passed to F as well. */
817 static int
818 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
820 int result = 0;
821 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
823 result |= (*f) (name_space, data);
825 for (; current; current = DECL_CHAIN (current))
826 result |= walk_namespaces_r (current, f, data);
828 return result;
831 /* Walk all the namespaces, calling F for each. The DATA is passed to
832 F as well. */
835 walk_namespaces (walk_namespaces_fn f, void* data)
837 return walk_namespaces_r (global_namespace, f, data);
840 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
841 DATA is non-NULL, this is the last time we will call
842 wrapup_global_declarations for this NAMESPACE. */
845 wrapup_globals_for_namespace (tree name_space, void* data)
847 struct cp_binding_level *level = NAMESPACE_LEVEL (name_space);
848 VEC(tree,gc) *statics = level->static_decls;
849 tree *vec = VEC_address (tree, statics);
850 int len = VEC_length (tree, statics);
851 int last_time = (data != 0);
853 if (last_time)
855 check_global_declarations (vec, len);
856 emit_debug_global_declarations (vec, len);
857 return 0;
860 /* Write out any globals that need to be output. */
861 return wrapup_global_declarations (vec, len);
865 /* In C++, you don't have to write `struct S' to refer to `S'; you
866 can just use `S'. We accomplish this by creating a TYPE_DECL as
867 if the user had written `typedef struct S S'. Create and return
868 the TYPE_DECL for TYPE. */
870 tree
871 create_implicit_typedef (tree name, tree type)
873 tree decl;
875 decl = build_decl (input_location, TYPE_DECL, name, type);
876 DECL_ARTIFICIAL (decl) = 1;
877 /* There are other implicit type declarations, like the one *within*
878 a class that allows you to write `S::S'. We must distinguish
879 amongst these. */
880 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
881 TYPE_NAME (type) = decl;
882 TYPE_STUB_DECL (type) = decl;
884 return decl;
887 /* Remember a local name for name-mangling purposes. */
889 static void
890 push_local_name (tree decl)
892 size_t i, nelts;
893 tree t, name;
895 timevar_push (TV_NAME_LOOKUP);
897 name = DECL_NAME (decl);
899 nelts = VEC_length (tree, local_names);
900 for (i = 0; i < nelts; i++)
902 t = VEC_index (tree, local_names, i);
903 if (DECL_NAME (t) == name)
905 if (!DECL_LANG_SPECIFIC (decl))
906 retrofit_lang_decl (decl);
907 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
908 if (DECL_LANG_SPECIFIC (t))
909 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
910 else
911 DECL_DISCRIMINATOR (decl) = 1;
913 VEC_replace (tree, local_names, i, decl);
914 timevar_pop (TV_NAME_LOOKUP);
915 return;
919 VEC_safe_push (tree, gc, local_names, decl);
920 timevar_pop (TV_NAME_LOOKUP);
923 /* Subroutine of duplicate_decls: return truthvalue of whether
924 or not types of these decls match.
926 For C++, we must compare the parameter list so that `int' can match
927 `int&' in a parameter position, but `int&' is not confused with
928 `const int&'. */
931 decls_match (tree newdecl, tree olddecl)
933 int types_match;
935 if (newdecl == olddecl)
936 return 1;
938 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
939 /* If the two DECLs are not even the same kind of thing, we're not
940 interested in their types. */
941 return 0;
943 if (TREE_CODE (newdecl) == FUNCTION_DECL)
945 tree f1 = TREE_TYPE (newdecl);
946 tree f2 = TREE_TYPE (olddecl);
947 tree p1 = TYPE_ARG_TYPES (f1);
948 tree p2 = TYPE_ARG_TYPES (f2);
950 /* Specializations of different templates are different functions
951 even if they have the same type. */
952 tree t1 = (DECL_USE_TEMPLATE (newdecl)
953 ? DECL_TI_TEMPLATE (newdecl)
954 : NULL_TREE);
955 tree t2 = (DECL_USE_TEMPLATE (olddecl)
956 ? DECL_TI_TEMPLATE (olddecl)
957 : NULL_TREE);
958 if (t1 != t2)
959 return 0;
961 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
962 && ! (DECL_EXTERN_C_P (newdecl)
963 && DECL_EXTERN_C_P (olddecl)))
964 return 0;
966 #ifdef NO_IMPLICIT_EXTERN_C
967 /* A new declaration doesn't match a built-in one unless it
968 is also extern "C". */
969 if (DECL_IS_BUILTIN (olddecl)
970 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
971 return 0;
972 #endif
974 if (TREE_CODE (f1) != TREE_CODE (f2))
975 return 0;
977 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
979 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
980 && (DECL_BUILT_IN (olddecl)
981 #ifndef NO_IMPLICIT_EXTERN_C
982 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
983 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
984 #endif
987 types_match = self_promoting_args_p (p1);
988 if (p1 == void_list_node)
989 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
991 #ifndef NO_IMPLICIT_EXTERN_C
992 else if (p1 == NULL_TREE
993 && (DECL_EXTERN_C_P (olddecl)
994 && DECL_IN_SYSTEM_HEADER (olddecl)
995 && !DECL_CLASS_SCOPE_P (olddecl))
996 && (DECL_EXTERN_C_P (newdecl)
997 && DECL_IN_SYSTEM_HEADER (newdecl)
998 && !DECL_CLASS_SCOPE_P (newdecl)))
1000 types_match = self_promoting_args_p (p2);
1001 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1003 #endif
1004 else
1005 types_match = compparms (p1, p2);
1007 else
1008 types_match = 0;
1010 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1012 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
1013 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
1014 return 0;
1016 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1017 DECL_TEMPLATE_PARMS (olddecl)))
1018 return 0;
1020 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1021 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1022 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
1023 else
1024 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1025 DECL_TEMPLATE_RESULT (newdecl));
1027 else
1029 /* Need to check scope for variable declaration (VAR_DECL).
1030 For typedef (TYPE_DECL), scope is ignored. */
1031 if (TREE_CODE (newdecl) == VAR_DECL
1032 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1033 /* [dcl.link]
1034 Two declarations for an object with C language linkage
1035 with the same name (ignoring the namespace that qualify
1036 it) that appear in different namespace scopes refer to
1037 the same object. */
1038 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1039 return 0;
1041 if (TREE_TYPE (newdecl) == error_mark_node)
1042 types_match = TREE_TYPE (olddecl) == error_mark_node;
1043 else if (TREE_TYPE (olddecl) == NULL_TREE)
1044 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1045 else if (TREE_TYPE (newdecl) == NULL_TREE)
1046 types_match = 0;
1047 else
1048 types_match = comptypes (TREE_TYPE (newdecl),
1049 TREE_TYPE (olddecl),
1050 COMPARE_REDECLARATION);
1053 return types_match;
1056 /* If NEWDECL is `static' and an `extern' was seen previously,
1057 warn about it. OLDDECL is the previous declaration.
1059 Note that this does not apply to the C++ case of declaring
1060 a variable `extern const' and then later `const'.
1062 Don't complain about built-in functions, since they are beyond
1063 the user's control. */
1065 void
1066 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1068 if (TREE_CODE (newdecl) == TYPE_DECL
1069 || TREE_CODE (newdecl) == TEMPLATE_DECL
1070 || TREE_CODE (newdecl) == CONST_DECL
1071 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1072 return;
1074 /* Don't get confused by static member functions; that's a different
1075 use of `static'. */
1076 if (TREE_CODE (newdecl) == FUNCTION_DECL
1077 && DECL_STATIC_FUNCTION_P (newdecl))
1078 return;
1080 /* If the old declaration was `static', or the new one isn't, then
1081 then everything is OK. */
1082 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1083 return;
1085 /* It's OK to declare a builtin function as `static'. */
1086 if (TREE_CODE (olddecl) == FUNCTION_DECL
1087 && DECL_ARTIFICIAL (olddecl))
1088 return;
1090 permerror (input_location, "%qD was declared %<extern%> and later %<static%>", newdecl);
1091 permerror (input_location, "previous declaration of %q+D", olddecl);
1094 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1095 function templates. If their exception specifications do not
1096 match, issue a diagnostic. */
1098 static void
1099 check_redeclaration_exception_specification (tree new_decl,
1100 tree old_decl)
1102 tree new_type;
1103 tree old_type;
1104 tree new_exceptions;
1105 tree old_exceptions;
1107 new_type = TREE_TYPE (new_decl);
1108 new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1109 old_type = TREE_TYPE (old_decl);
1110 old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1112 /* [except.spec]
1114 If any declaration of a function has an exception-specification,
1115 all declarations, including the definition and an explicit
1116 specialization, of that function shall have an
1117 exception-specification with the same set of type-ids. */
1118 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1119 && ! DECL_IS_BUILTIN (old_decl)
1120 && flag_exceptions
1121 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1123 error ("declaration of %qF has a different exception specifier",
1124 new_decl);
1125 error ("from previous declaration %q+F", old_decl);
1129 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1130 && lookup_attribute ("gnu_inline", \
1131 DECL_ATTRIBUTES (fn)))
1133 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1134 If the redeclaration is invalid, a diagnostic is issued, and the
1135 error_mark_node is returned. Otherwise, OLDDECL is returned.
1137 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1138 returned.
1140 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1142 tree
1143 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1145 unsigned olddecl_uid = DECL_UID (olddecl);
1146 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1147 int new_defines_function = 0;
1148 tree new_template_info;
1150 if (newdecl == olddecl)
1151 return olddecl;
1153 types_match = decls_match (newdecl, olddecl);
1155 /* If either the type of the new decl or the type of the old decl is an
1156 error_mark_node, then that implies that we have already issued an
1157 error (earlier) for some bogus type specification, and in that case,
1158 it is rather pointless to harass the user with yet more error message
1159 about the same declaration, so just pretend the types match here. */
1160 if (TREE_TYPE (newdecl) == error_mark_node
1161 || TREE_TYPE (olddecl) == error_mark_node)
1162 return error_mark_node;
1164 if (DECL_P (olddecl)
1165 && TREE_CODE (newdecl) == FUNCTION_DECL
1166 && TREE_CODE (olddecl) == FUNCTION_DECL
1167 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1169 if (DECL_DECLARED_INLINE_P (newdecl)
1170 && DECL_UNINLINABLE (newdecl)
1171 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1172 /* Already warned elsewhere. */;
1173 else if (DECL_DECLARED_INLINE_P (olddecl)
1174 && DECL_UNINLINABLE (olddecl)
1175 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1176 /* Already warned. */;
1177 else if (DECL_DECLARED_INLINE_P (newdecl)
1178 && DECL_UNINLINABLE (olddecl)
1179 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1181 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1182 newdecl);
1183 warning (OPT_Wattributes, "previous declaration of %q+D "
1184 "with attribute noinline", olddecl);
1186 else if (DECL_DECLARED_INLINE_P (olddecl)
1187 && DECL_UNINLINABLE (newdecl)
1188 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1190 warning (OPT_Wattributes, "function %q+D redeclared with "
1191 "attribute noinline", newdecl);
1192 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1193 olddecl);
1197 /* Check for redeclaration and other discrepancies. */
1198 if (TREE_CODE (olddecl) == FUNCTION_DECL
1199 && DECL_ARTIFICIAL (olddecl))
1201 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1202 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1204 /* Avoid warnings redeclaring built-ins which have not been
1205 explicitly declared. */
1206 if (DECL_ANTICIPATED (olddecl))
1207 return NULL_TREE;
1209 /* If you declare a built-in or predefined function name as static,
1210 the old definition is overridden, but optionally warn this was a
1211 bad choice of name. */
1212 if (! TREE_PUBLIC (newdecl))
1214 warning (OPT_Wshadow,
1215 DECL_BUILT_IN (olddecl)
1216 ? G_("shadowing built-in function %q#D")
1217 : G_("shadowing library function %q#D"), olddecl);
1218 /* Discard the old built-in function. */
1219 return NULL_TREE;
1221 /* If the built-in is not ansi, then programs can override
1222 it even globally without an error. */
1223 else if (! DECL_BUILT_IN (olddecl))
1224 warning (0, "library function %q#D redeclared as non-function %q#D",
1225 olddecl, newdecl);
1226 else
1228 error ("declaration of %q#D", newdecl);
1229 error ("conflicts with built-in declaration %q#D",
1230 olddecl);
1232 return NULL_TREE;
1234 else if (!types_match)
1236 /* Avoid warnings redeclaring built-ins which have not been
1237 explicitly declared. */
1238 if (DECL_ANTICIPATED (olddecl))
1240 /* Deal with fileptr_type_node. FILE type is not known
1241 at the time we create the builtins. */
1242 tree t1, t2;
1244 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1245 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1246 t1 || t2;
1247 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1248 if (!t1 || !t2)
1249 break;
1250 else if (TREE_VALUE (t2) == fileptr_type_node)
1252 tree t = TREE_VALUE (t1);
1254 if (TREE_CODE (t) == POINTER_TYPE
1255 && TYPE_NAME (TREE_TYPE (t))
1256 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1257 == get_identifier ("FILE")
1258 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1260 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1262 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1263 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1264 types_match = decls_match (newdecl, olddecl);
1265 if (types_match)
1266 return duplicate_decls (newdecl, olddecl,
1267 newdecl_is_friend);
1268 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1271 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1272 break;
1274 else if ((DECL_EXTERN_C_P (newdecl)
1275 && DECL_EXTERN_C_P (olddecl))
1276 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1277 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1279 /* A near match; override the builtin. */
1281 if (TREE_PUBLIC (newdecl))
1283 warning (0, "new declaration %q#D", newdecl);
1284 warning (0, "ambiguates built-in declaration %q#D",
1285 olddecl);
1287 else
1288 warning (OPT_Wshadow,
1289 DECL_BUILT_IN (olddecl)
1290 ? G_("shadowing built-in function %q#D")
1291 : G_("shadowing library function %q#D"), olddecl);
1293 else
1294 /* Discard the old built-in function. */
1295 return NULL_TREE;
1297 /* Replace the old RTL to avoid problems with inlining. */
1298 COPY_DECL_RTL (newdecl, olddecl);
1300 /* Even if the types match, prefer the new declarations type for
1301 built-ins which have not been explicitly declared, for
1302 exception lists, etc... */
1303 else if (DECL_IS_BUILTIN (olddecl))
1305 tree type = TREE_TYPE (newdecl);
1306 tree attribs = (*targetm.merge_type_attributes)
1307 (TREE_TYPE (olddecl), type);
1309 type = cp_build_type_attribute_variant (type, attribs);
1310 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1313 /* If a function is explicitly declared "throw ()", propagate that to
1314 the corresponding builtin. */
1315 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1316 && DECL_ANTICIPATED (olddecl)
1317 && TREE_NOTHROW (newdecl)
1318 && !TREE_NOTHROW (olddecl)
1319 && built_in_decls [DECL_FUNCTION_CODE (olddecl)] != NULL_TREE
1320 && built_in_decls [DECL_FUNCTION_CODE (olddecl)] != olddecl
1321 && types_match)
1322 TREE_NOTHROW (built_in_decls [DECL_FUNCTION_CODE (olddecl)]) = 1;
1324 /* Whether or not the builtin can throw exceptions has no
1325 bearing on this declarator. */
1326 TREE_NOTHROW (olddecl) = 0;
1328 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1330 /* If a builtin function is redeclared as `static', merge
1331 the declarations, but make the original one static. */
1332 DECL_THIS_STATIC (olddecl) = 1;
1333 TREE_PUBLIC (olddecl) = 0;
1335 /* Make the old declaration consistent with the new one so
1336 that all remnants of the builtin-ness of this function
1337 will be banished. */
1338 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1339 COPY_DECL_RTL (newdecl, olddecl);
1342 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1344 /* C++ Standard, 3.3, clause 4:
1345 "[Note: a namespace name or a class template name must be unique
1346 in its declarative region (7.3.2, clause 14). ]" */
1347 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1348 && TREE_CODE (newdecl) != NAMESPACE_DECL
1349 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1350 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1351 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1352 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1354 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1355 && TREE_CODE (newdecl) != TYPE_DECL)
1356 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1357 && TREE_CODE (olddecl) != TYPE_DECL))
1359 /* We do nothing special here, because C++ does such nasty
1360 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1361 get shadowed, and know that if we need to find a TYPE_DECL
1362 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1363 slot of the identifier. */
1364 return NULL_TREE;
1367 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1368 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1369 || (TREE_CODE (olddecl) == FUNCTION_DECL
1370 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1371 return NULL_TREE;
1374 error ("%q#D redeclared as different kind of symbol", newdecl);
1375 if (TREE_CODE (olddecl) == TREE_LIST)
1376 olddecl = TREE_VALUE (olddecl);
1377 error ("previous declaration of %q+#D", olddecl);
1379 return error_mark_node;
1381 else if (!types_match)
1383 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1384 /* These are certainly not duplicate declarations; they're
1385 from different scopes. */
1386 return NULL_TREE;
1388 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1390 /* The name of a class template may not be declared to refer to
1391 any other template, class, function, object, namespace, value,
1392 or type in the same scope. */
1393 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1394 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1396 error ("declaration of template %q#D", newdecl);
1397 error ("conflicts with previous declaration %q+#D", olddecl);
1399 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1400 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1401 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1402 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1403 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1404 DECL_TEMPLATE_PARMS (olddecl))
1405 /* Template functions can be disambiguated by
1406 return type. */
1407 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1408 TREE_TYPE (TREE_TYPE (olddecl))))
1410 error ("new declaration %q#D", newdecl);
1411 error ("ambiguates old declaration %q+#D", olddecl);
1413 return NULL_TREE;
1415 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1417 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1419 error ("declaration of C function %q#D conflicts with",
1420 newdecl);
1421 error ("previous declaration %q+#D here", olddecl);
1423 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1424 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1426 error ("new declaration %q#D", newdecl);
1427 error ("ambiguates old declaration %q+#D", olddecl);
1428 return error_mark_node;
1430 else
1431 return NULL_TREE;
1433 else
1435 error ("conflicting declaration %q#D", newdecl);
1436 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
1437 return error_mark_node;
1440 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1441 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1442 && (!DECL_TEMPLATE_INFO (newdecl)
1443 || (DECL_TI_TEMPLATE (newdecl)
1444 != DECL_TI_TEMPLATE (olddecl))))
1445 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1446 && (!DECL_TEMPLATE_INFO (olddecl)
1447 || (DECL_TI_TEMPLATE (olddecl)
1448 != DECL_TI_TEMPLATE (newdecl))))))
1449 /* It's OK to have a template specialization and a non-template
1450 with the same type, or to have specializations of two
1451 different templates with the same type. Note that if one is a
1452 specialization, and the other is an instantiation of the same
1453 template, that we do not exit at this point. That situation
1454 can occur if we instantiate a template class, and then
1455 specialize one of its methods. This situation is valid, but
1456 the declarations must be merged in the usual way. */
1457 return NULL_TREE;
1458 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1459 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1460 && !DECL_USE_TEMPLATE (newdecl))
1461 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1462 && !DECL_USE_TEMPLATE (olddecl))))
1463 /* One of the declarations is a template instantiation, and the
1464 other is not a template at all. That's OK. */
1465 return NULL_TREE;
1466 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1468 /* In [namespace.alias] we have:
1470 In a declarative region, a namespace-alias-definition can be
1471 used to redefine a namespace-alias declared in that declarative
1472 region to refer only to the namespace to which it already
1473 refers.
1475 Therefore, if we encounter a second alias directive for the same
1476 alias, we can just ignore the second directive. */
1477 if (DECL_NAMESPACE_ALIAS (newdecl)
1478 && (DECL_NAMESPACE_ALIAS (newdecl)
1479 == DECL_NAMESPACE_ALIAS (olddecl)))
1480 return olddecl;
1481 /* [namespace.alias]
1483 A namespace-name or namespace-alias shall not be declared as
1484 the name of any other entity in the same declarative region.
1485 A namespace-name defined at global scope shall not be
1486 declared as the name of any other entity in any global scope
1487 of the program. */
1488 error ("declaration of namespace %qD conflicts with", newdecl);
1489 error ("previous declaration of namespace %q+D here", olddecl);
1490 return error_mark_node;
1492 else
1494 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1495 if (errmsg)
1497 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1498 if (DECL_NAME (olddecl) != NULL_TREE)
1499 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1500 ? "%q+#D previously defined here"
1501 : "%q+#D previously declared here", olddecl);
1502 return error_mark_node;
1504 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1505 && DECL_INITIAL (olddecl) != NULL_TREE
1506 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1507 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1509 /* Prototype decl follows defn w/o prototype. */
1510 warning_at (input_location, 0, "prototype for %q+#D", newdecl);
1511 warning_at (DECL_SOURCE_LOCATION (olddecl), 0,
1512 "follows non-prototype definition here");
1514 else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1515 || TREE_CODE (olddecl) == VAR_DECL)
1516 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1518 /* [dcl.link]
1519 If two declarations of the same function or object
1520 specify different linkage-specifications ..., the program
1521 is ill-formed.... Except for functions with C++ linkage,
1522 a function declaration without a linkage specification
1523 shall not precede the first linkage specification for
1524 that function. A function can be declared without a
1525 linkage specification after an explicit linkage
1526 specification has been seen; the linkage explicitly
1527 specified in the earlier declaration is not affected by
1528 such a function declaration.
1530 DR 563 raises the question why the restrictions on
1531 functions should not also apply to objects. Older
1532 versions of G++ silently ignore the linkage-specification
1533 for this example:
1535 namespace N {
1536 extern int i;
1537 extern "C" int i;
1540 which is clearly wrong. Therefore, we now treat objects
1541 like functions. */
1542 if (current_lang_depth () == 0)
1544 /* There is no explicit linkage-specification, so we use
1545 the linkage from the previous declaration. */
1546 if (!DECL_LANG_SPECIFIC (newdecl))
1547 retrofit_lang_decl (newdecl);
1548 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1550 else
1552 error ("previous declaration of %q+#D with %qL linkage",
1553 olddecl, DECL_LANGUAGE (olddecl));
1554 error ("conflicts with new declaration with %qL linkage",
1555 DECL_LANGUAGE (newdecl));
1559 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1561 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1563 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1564 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1565 int i = 1;
1567 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1568 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1570 for (; t1 && t1 != void_list_node;
1571 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1572 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1574 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1575 TREE_PURPOSE (t2)))
1577 permerror (input_location, "default argument given for parameter %d of %q#D",
1578 i, newdecl);
1579 permerror (input_location, "after previous specification in %q+#D", olddecl);
1581 else
1583 error ("default argument given for parameter %d of %q#D",
1584 i, newdecl);
1585 error ("after previous specification in %q+#D",
1586 olddecl);
1592 /* Do not merge an implicit typedef with an explicit one. In:
1594 class A;
1596 typedef class A A __attribute__ ((foo));
1598 the attribute should apply only to the typedef. */
1599 if (TREE_CODE (olddecl) == TYPE_DECL
1600 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1601 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1602 return NULL_TREE;
1604 /* If new decl is `static' and an `extern' was seen previously,
1605 warn about it. */
1606 warn_extern_redeclared_static (newdecl, olddecl);
1608 /* We have committed to returning 1 at this point. */
1609 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1611 /* Now that functions must hold information normally held
1612 by field decls, there is extra work to do so that
1613 declaration information does not get destroyed during
1614 definition. */
1615 if (DECL_VINDEX (olddecl))
1616 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1617 if (DECL_CONTEXT (olddecl))
1618 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1619 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1620 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1621 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1622 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1623 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1624 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1625 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1626 SET_OVERLOADED_OPERATOR_CODE
1627 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1628 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1630 /* Optionally warn about more than one declaration for the same
1631 name, but don't warn about a function declaration followed by a
1632 definition. */
1633 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1634 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1635 /* Don't warn about extern decl followed by definition. */
1636 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1637 /* Don't warn about friends, let add_friend take care of it. */
1638 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)))
1640 warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1641 warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
1644 if (DECL_DELETED_FN (newdecl))
1646 error ("deleted definition of %qD", newdecl);
1647 error ("after previous declaration %q+D", olddecl);
1651 /* Deal with C++: must preserve virtual function table size. */
1652 if (TREE_CODE (olddecl) == TYPE_DECL)
1654 tree newtype = TREE_TYPE (newdecl);
1655 tree oldtype = TREE_TYPE (olddecl);
1657 if (newtype != error_mark_node && oldtype != error_mark_node
1658 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1659 CLASSTYPE_FRIEND_CLASSES (newtype)
1660 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1662 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1665 /* Copy all the DECL_... slots specified in the new decl
1666 except for any that we copy here from the old type. */
1667 DECL_ATTRIBUTES (newdecl)
1668 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1670 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1672 tree old_result;
1673 tree new_result;
1674 old_result = DECL_TEMPLATE_RESULT (olddecl);
1675 new_result = DECL_TEMPLATE_RESULT (newdecl);
1676 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1677 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1678 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1679 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1681 DECL_ATTRIBUTES (old_result)
1682 = (*targetm.merge_decl_attributes) (old_result, new_result);
1684 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1686 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1687 && DECL_INITIAL (new_result))
1689 if (DECL_INITIAL (old_result))
1690 DECL_UNINLINABLE (old_result) = 1;
1691 else
1692 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
1693 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
1694 DECL_NOT_REALLY_EXTERN (old_result)
1695 = DECL_NOT_REALLY_EXTERN (new_result);
1696 DECL_INTERFACE_KNOWN (old_result)
1697 = DECL_INTERFACE_KNOWN (new_result);
1698 DECL_DECLARED_INLINE_P (old_result)
1699 = DECL_DECLARED_INLINE_P (new_result);
1700 DECL_DISREGARD_INLINE_LIMITS (old_result)
1701 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1704 else
1706 DECL_DECLARED_INLINE_P (old_result)
1707 |= DECL_DECLARED_INLINE_P (new_result);
1708 DECL_DISREGARD_INLINE_LIMITS (old_result)
1709 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1710 check_redeclaration_exception_specification (newdecl, olddecl);
1714 /* If the new declaration is a definition, update the file and
1715 line information on the declaration, and also make
1716 the old declaration the same definition. */
1717 if (DECL_INITIAL (new_result) != NULL_TREE)
1719 DECL_SOURCE_LOCATION (olddecl)
1720 = DECL_SOURCE_LOCATION (old_result)
1721 = DECL_SOURCE_LOCATION (newdecl);
1722 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
1723 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1725 tree parm;
1726 DECL_ARGUMENTS (old_result)
1727 = DECL_ARGUMENTS (new_result);
1728 for (parm = DECL_ARGUMENTS (old_result); parm;
1729 parm = DECL_CHAIN (parm))
1730 DECL_CONTEXT (parm) = old_result;
1734 return olddecl;
1737 if (types_match)
1739 /* Automatically handles default parameters. */
1740 tree oldtype = TREE_TYPE (olddecl);
1741 tree newtype;
1743 /* Merge the data types specified in the two decls. */
1744 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1746 /* If merge_types produces a non-typedef type, just use the old type. */
1747 if (TREE_CODE (newdecl) == TYPE_DECL
1748 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1749 newtype = oldtype;
1751 if (TREE_CODE (newdecl) == VAR_DECL)
1753 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1754 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1755 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1756 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1757 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1758 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1760 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1761 if (DECL_LANG_SPECIFIC (olddecl)
1762 && CP_DECL_THREADPRIVATE_P (olddecl))
1764 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1765 if (!DECL_LANG_SPECIFIC (newdecl))
1766 retrofit_lang_decl (newdecl);
1768 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1769 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1773 /* Do this after calling `merge_types' so that default
1774 parameters don't confuse us. */
1775 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1776 check_redeclaration_exception_specification (newdecl, olddecl);
1777 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1779 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1780 check_default_args (newdecl);
1782 /* Lay the type out, unless already done. */
1783 if (! same_type_p (newtype, oldtype)
1784 && TREE_TYPE (newdecl) != error_mark_node
1785 && !(processing_template_decl && uses_template_parms (newdecl)))
1786 layout_type (TREE_TYPE (newdecl));
1788 if ((TREE_CODE (newdecl) == VAR_DECL
1789 || TREE_CODE (newdecl) == PARM_DECL
1790 || TREE_CODE (newdecl) == RESULT_DECL
1791 || TREE_CODE (newdecl) == FIELD_DECL
1792 || TREE_CODE (newdecl) == TYPE_DECL)
1793 && !(processing_template_decl && uses_template_parms (newdecl)))
1794 layout_decl (newdecl, 0);
1796 /* Merge the type qualifiers. */
1797 if (TREE_READONLY (newdecl))
1798 TREE_READONLY (olddecl) = 1;
1799 if (TREE_THIS_VOLATILE (newdecl))
1800 TREE_THIS_VOLATILE (olddecl) = 1;
1801 if (TREE_NOTHROW (newdecl))
1802 TREE_NOTHROW (olddecl) = 1;
1804 /* Merge deprecatedness. */
1805 if (TREE_DEPRECATED (newdecl))
1806 TREE_DEPRECATED (olddecl) = 1;
1808 /* Preserve function specific target and optimization options */
1809 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1811 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1812 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1813 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1814 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1816 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1817 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1818 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1819 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1822 /* Merge the initialization information. */
1823 if (DECL_INITIAL (newdecl) == NULL_TREE
1824 && DECL_INITIAL (olddecl) != NULL_TREE)
1826 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1827 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1828 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1830 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1831 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1835 /* Merge the section attribute.
1836 We want to issue an error if the sections conflict but that must be
1837 done later in decl_attributes since we are called before attributes
1838 are assigned. */
1839 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1840 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1842 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1844 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1845 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1846 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1847 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1848 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1849 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1850 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1851 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1852 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1853 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
1854 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
1855 /* Keep the old RTL. */
1856 COPY_DECL_RTL (olddecl, newdecl);
1858 else if (TREE_CODE (newdecl) == VAR_DECL
1859 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1861 /* Keep the old RTL. We cannot keep the old RTL if the old
1862 declaration was for an incomplete object and the new
1863 declaration is not since many attributes of the RTL will
1864 change. */
1865 COPY_DECL_RTL (olddecl, newdecl);
1868 /* If cannot merge, then use the new type and qualifiers,
1869 and don't preserve the old rtl. */
1870 else
1872 /* Clean out any memory we had of the old declaration. */
1873 tree oldstatic = value_member (olddecl, static_aggregates);
1874 if (oldstatic)
1875 TREE_VALUE (oldstatic) = error_mark_node;
1877 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1878 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1879 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1880 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1883 /* Merge the storage class information. */
1884 merge_weak (newdecl, olddecl);
1886 if (DECL_ONE_ONLY (olddecl))
1887 DECL_COMDAT_GROUP (newdecl) = DECL_COMDAT_GROUP (olddecl);
1889 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1890 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1891 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1892 if (! DECL_EXTERNAL (olddecl))
1893 DECL_EXTERNAL (newdecl) = 0;
1895 new_template_info = NULL_TREE;
1896 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1898 bool new_redefines_gnu_inline = false;
1900 if (new_defines_function
1901 && ((DECL_INTERFACE_KNOWN (olddecl)
1902 && TREE_CODE (olddecl) == FUNCTION_DECL)
1903 || (TREE_CODE (olddecl) == TEMPLATE_DECL
1904 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1905 == FUNCTION_DECL))))
1907 tree fn = olddecl;
1909 if (TREE_CODE (fn) == TEMPLATE_DECL)
1910 fn = DECL_TEMPLATE_RESULT (olddecl);
1912 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
1915 if (!new_redefines_gnu_inline)
1917 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1918 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1919 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1921 DECL_TEMPLATE_INSTANTIATED (newdecl)
1922 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1923 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
1925 /* If the OLDDECL is an instantiation and/or specialization,
1926 then the NEWDECL must be too. But, it may not yet be marked
1927 as such if the caller has created NEWDECL, but has not yet
1928 figured out that it is a redeclaration. */
1929 if (!DECL_USE_TEMPLATE (newdecl))
1930 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
1932 /* Don't really know how much of the language-specific
1933 values we should copy from old to new. */
1934 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
1935 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
1936 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1937 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
1939 if (LANG_DECL_HAS_MIN (newdecl))
1941 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
1942 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
1943 if (DECL_TEMPLATE_INFO (newdecl))
1944 new_template_info = DECL_TEMPLATE_INFO (newdecl);
1945 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
1947 /* Only functions have these fields. */
1948 if (TREE_CODE (newdecl) == FUNCTION_DECL
1949 || DECL_FUNCTION_TEMPLATE_P (newdecl))
1951 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
1952 olddecl_friend = DECL_FRIEND_P (olddecl);
1953 hidden_friend = (DECL_ANTICIPATED (olddecl)
1954 && DECL_HIDDEN_FRIEND_P (olddecl)
1955 && newdecl_is_friend);
1956 DECL_BEFRIENDING_CLASSES (newdecl)
1957 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1958 DECL_BEFRIENDING_CLASSES (olddecl));
1959 /* DECL_THUNKS is only valid for virtual functions,
1960 otherwise it is a DECL_FRIEND_CONTEXT. */
1961 if (DECL_VIRTUAL_P (newdecl))
1962 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
1964 /* Only variables have this field. */
1965 else if (TREE_CODE (newdecl) == VAR_DECL
1966 && VAR_HAD_UNKNOWN_BOUND (olddecl))
1967 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
1970 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1972 tree parm;
1974 /* Merge parameter attributes. */
1975 tree oldarg, newarg;
1976 for (oldarg = DECL_ARGUMENTS(olddecl),
1977 newarg = DECL_ARGUMENTS(newdecl);
1978 oldarg && newarg;
1979 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
1980 DECL_ATTRIBUTES (newarg)
1981 = (*targetm.merge_decl_attributes) (oldarg, newarg);
1982 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
1985 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1986 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
1988 /* If newdecl is not a specialization, then it is not a
1989 template-related function at all. And that means that we
1990 should have exited above, returning 0. */
1991 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
1993 if (DECL_ODR_USED (olddecl))
1994 /* From [temp.expl.spec]:
1996 If a template, a member template or the member of a class
1997 template is explicitly specialized then that
1998 specialization shall be declared before the first use of
1999 that specialization that would cause an implicit
2000 instantiation to take place, in every translation unit in
2001 which such a use occurs. */
2002 error ("explicit specialization of %qD after first use",
2003 olddecl);
2005 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2007 /* Don't propagate visibility from the template to the
2008 specialization here. We'll do that in determine_visibility if
2009 appropriate. */
2010 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2012 /* [temp.expl.spec/14] We don't inline explicit specialization
2013 just because the primary template says so. */
2015 else if (new_defines_function && DECL_INITIAL (olddecl))
2017 /* Never inline re-defined extern inline functions.
2018 FIXME: this could be better handled by keeping both
2019 function as separate declarations. */
2020 DECL_UNINLINABLE (newdecl) = 1;
2022 else
2024 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
2025 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2027 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2029 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2030 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2032 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2033 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2034 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2035 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2038 /* Preserve abstractness on cloned [cd]tors. */
2039 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
2041 /* Update newdecl's parms to point at olddecl. */
2042 for (parm = DECL_ARGUMENTS (newdecl); parm;
2043 parm = DECL_CHAIN (parm))
2044 DECL_CONTEXT (parm) = olddecl;
2046 if (! types_match)
2048 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2049 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2050 COPY_DECL_RTL (newdecl, olddecl);
2052 if (! types_match || new_defines_function)
2054 /* These need to be copied so that the names are available.
2055 Note that if the types do match, we'll preserve inline
2056 info and other bits, but if not, we won't. */
2057 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2058 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2060 if (new_defines_function)
2061 /* If defining a function declared with other language
2062 linkage, use the previously declared language linkage. */
2063 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2064 else if (types_match)
2066 /* If redeclaring a builtin function, and not a definition,
2067 it stays built in. */
2068 if (DECL_BUILT_IN (olddecl))
2070 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2071 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2072 /* If we're keeping the built-in definition, keep the rtl,
2073 regardless of declaration matches. */
2074 COPY_DECL_RTL (olddecl, newdecl);
2077 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2078 /* Don't clear out the arguments if we're just redeclaring a
2079 function. */
2080 if (DECL_ARGUMENTS (olddecl))
2081 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2084 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2085 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2087 /* Now preserve various other info from the definition. */
2088 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2089 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2090 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2091 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2093 /* Warn about conflicting visibility specifications. */
2094 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2095 && DECL_VISIBILITY_SPECIFIED (newdecl)
2096 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2098 warning_at (input_location, OPT_Wattributes,
2099 "%q+D: visibility attribute ignored because it", newdecl);
2100 warning_at (DECL_SOURCE_LOCATION (olddecl), OPT_Wattributes,
2101 "conflicts with previous declaration here");
2103 /* Choose the declaration which specified visibility. */
2104 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2106 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2107 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2109 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2110 so keep this behavior. */
2111 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
2113 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2114 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2117 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2118 with that from NEWDECL below. */
2119 if (DECL_LANG_SPECIFIC (olddecl))
2121 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2122 != DECL_LANG_SPECIFIC (newdecl));
2123 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2126 /* Merge the USED information. */
2127 if (TREE_USED (olddecl))
2128 TREE_USED (newdecl) = 1;
2129 else if (TREE_USED (newdecl))
2130 TREE_USED (olddecl) = 1;
2131 if (TREE_CODE (newdecl) == VAR_DECL)
2133 if (DECL_READ_P (olddecl))
2134 DECL_READ_P (newdecl) = 1;
2135 else if (DECL_READ_P (newdecl))
2136 DECL_READ_P (olddecl) = 1;
2138 if (DECL_PRESERVE_P (olddecl))
2139 DECL_PRESERVE_P (newdecl) = 1;
2140 else if (DECL_PRESERVE_P (newdecl))
2141 DECL_PRESERVE_P (olddecl) = 1;
2143 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2145 int function_size;
2147 function_size = sizeof (struct tree_decl_common);
2149 memcpy ((char *) olddecl + sizeof (struct tree_common),
2150 (char *) newdecl + sizeof (struct tree_common),
2151 function_size - sizeof (struct tree_common));
2153 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2154 (char *) newdecl + sizeof (struct tree_decl_common),
2155 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2156 if (new_template_info)
2157 /* If newdecl is a template instantiation, it is possible that
2158 the following sequence of events has occurred:
2160 o A friend function was declared in a class template. The
2161 class template was instantiated.
2163 o The instantiation of the friend declaration was
2164 recorded on the instantiation list, and is newdecl.
2166 o Later, however, instantiate_class_template called pushdecl
2167 on the newdecl to perform name injection. But, pushdecl in
2168 turn called duplicate_decls when it discovered that another
2169 declaration of a global function with the same name already
2170 existed.
2172 o Here, in duplicate_decls, we decided to clobber newdecl.
2174 If we're going to do that, we'd better make sure that
2175 olddecl, and not newdecl, is on the list of
2176 instantiations so that if we try to do the instantiation
2177 again we won't get the clobbered declaration. */
2178 reregister_specialization (newdecl,
2179 new_template_info,
2180 olddecl);
2182 else
2184 size_t size = tree_code_size (TREE_CODE (olddecl));
2185 memcpy ((char *) olddecl + sizeof (struct tree_common),
2186 (char *) newdecl + sizeof (struct tree_common),
2187 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2188 switch (TREE_CODE (olddecl))
2190 case LABEL_DECL:
2191 case VAR_DECL:
2192 case RESULT_DECL:
2193 case PARM_DECL:
2194 case FIELD_DECL:
2195 case TYPE_DECL:
2196 case CONST_DECL:
2198 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2199 (char *) newdecl + sizeof (struct tree_decl_common),
2200 size - sizeof (struct tree_decl_common)
2201 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2203 break;
2204 default:
2205 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2206 (char *) newdecl + sizeof (struct tree_decl_common),
2207 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2208 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2209 break;
2212 DECL_UID (olddecl) = olddecl_uid;
2213 if (olddecl_friend)
2214 DECL_FRIEND_P (olddecl) = 1;
2215 if (hidden_friend)
2217 DECL_ANTICIPATED (olddecl) = 1;
2218 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2221 /* NEWDECL contains the merged attribute lists.
2222 Update OLDDECL to be the same. */
2223 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2225 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2226 so that encode_section_info has a chance to look at the new decl
2227 flags and attributes. */
2228 if (DECL_RTL_SET_P (olddecl)
2229 && (TREE_CODE (olddecl) == FUNCTION_DECL
2230 || (TREE_CODE (olddecl) == VAR_DECL
2231 && TREE_STATIC (olddecl))))
2232 make_decl_rtl (olddecl);
2234 /* The NEWDECL will no longer be needed. Because every out-of-class
2235 declaration of a member results in a call to duplicate_decls,
2236 freeing these nodes represents in a significant savings. */
2237 ggc_free (newdecl);
2239 return olddecl;
2242 /* Return zero if the declaration NEWDECL is valid
2243 when the declaration OLDDECL (assumed to be for the same name)
2244 has already been seen.
2245 Otherwise return an error message format string with a %s
2246 where the identifier should go. */
2248 static const char *
2249 redeclaration_error_message (tree newdecl, tree olddecl)
2251 if (TREE_CODE (newdecl) == TYPE_DECL)
2253 /* Because C++ can put things into name space for free,
2254 constructs like "typedef struct foo { ... } foo"
2255 would look like an erroneous redeclaration. */
2256 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2257 return NULL;
2258 else
2259 return G_("redefinition of %q#D");
2261 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2263 /* If this is a pure function, its olddecl will actually be
2264 the original initialization to `0' (which we force to call
2265 abort()). Don't complain about redefinition in this case. */
2266 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2267 && DECL_INITIAL (olddecl) == NULL_TREE)
2268 return NULL;
2270 /* If both functions come from different namespaces, this is not
2271 a redeclaration - this is a conflict with a used function. */
2272 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2273 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2274 && ! decls_match (olddecl, newdecl))
2275 return G_("%qD conflicts with used function");
2277 /* We'll complain about linkage mismatches in
2278 warn_extern_redeclared_static. */
2280 /* Defining the same name twice is no good. */
2281 if (DECL_INITIAL (olddecl) != NULL_TREE
2282 && DECL_INITIAL (newdecl) != NULL_TREE)
2284 if (DECL_NAME (olddecl) == NULL_TREE)
2285 return G_("%q#D not declared in class");
2286 else if (!GNU_INLINE_P (olddecl)
2287 || GNU_INLINE_P (newdecl))
2288 return G_("redefinition of %q#D");
2291 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2293 bool olda = GNU_INLINE_P (olddecl);
2294 bool newa = GNU_INLINE_P (newdecl);
2296 if (olda != newa)
2298 if (newa)
2299 return G_("%q+D redeclared inline with "
2300 "%<gnu_inline%> attribute");
2301 else
2302 return G_("%q+D redeclared inline without "
2303 "%<gnu_inline%> attribute");
2307 return NULL;
2309 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2311 tree nt, ot;
2313 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2315 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2316 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2317 return G_("redefinition of %q#D");
2318 return NULL;
2321 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2322 || (DECL_TEMPLATE_RESULT (newdecl)
2323 == DECL_TEMPLATE_RESULT (olddecl)))
2324 return NULL;
2326 nt = DECL_TEMPLATE_RESULT (newdecl);
2327 if (DECL_TEMPLATE_INFO (nt))
2328 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2329 ot = DECL_TEMPLATE_RESULT (olddecl);
2330 if (DECL_TEMPLATE_INFO (ot))
2331 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2332 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2333 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2334 return G_("redefinition of %q#D");
2336 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2338 bool olda = GNU_INLINE_P (ot);
2339 bool newa = GNU_INLINE_P (nt);
2341 if (olda != newa)
2343 if (newa)
2344 return G_("%q+D redeclared inline with "
2345 "%<gnu_inline%> attribute");
2346 else
2347 return G_("%q+D redeclared inline without "
2348 "%<gnu_inline%> attribute");
2352 /* Core issue #226 (C++0x):
2354 If a friend function template declaration specifies a
2355 default template-argument, that declaration shall be a
2356 definition and shall be the only declaration of the
2357 function template in the translation unit. */
2358 if ((cxx_dialect != cxx98)
2359 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2360 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2361 /*is_primary=*/1, /*is_partial=*/0,
2362 /*is_friend_decl=*/2))
2363 return G_("redeclaration of friend %q#D "
2364 "may not have default template arguments");
2366 return NULL;
2368 else if (TREE_CODE (newdecl) == VAR_DECL
2369 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2370 && (! DECL_LANG_SPECIFIC (olddecl)
2371 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2372 || DECL_THREAD_LOCAL_P (newdecl)))
2374 /* Only variables can be thread-local, and all declarations must
2375 agree on this property. */
2376 if (DECL_THREAD_LOCAL_P (newdecl))
2377 return G_("thread-local declaration of %q#D follows "
2378 "non-thread-local declaration");
2379 else
2380 return G_("non-thread-local declaration of %q#D follows "
2381 "thread-local declaration");
2383 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2385 /* The objects have been declared at namespace scope. If either
2386 is a member of an anonymous union, then this is an invalid
2387 redeclaration. For example:
2389 int i;
2390 union { int i; };
2392 is invalid. */
2393 if ((TREE_CODE (newdecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (newdecl))
2394 || (TREE_CODE (olddecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (olddecl)))
2395 return G_("redeclaration of %q#D");
2396 /* If at least one declaration is a reference, there is no
2397 conflict. For example:
2399 int i = 3;
2400 extern int i;
2402 is valid. */
2403 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2404 return NULL;
2405 /* Reject two definitions. */
2406 return G_("redefinition of %q#D");
2408 else
2410 /* Objects declared with block scope: */
2411 /* Reject two definitions, and reject a definition
2412 together with an external reference. */
2413 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2414 return G_("redeclaration of %q#D");
2415 return NULL;
2419 /* Hash and equality functions for the named_label table. */
2421 static hashval_t
2422 named_label_entry_hash (const void *data)
2424 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2425 return DECL_UID (ent->label_decl);
2428 static int
2429 named_label_entry_eq (const void *a, const void *b)
2431 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2432 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2433 return ent_a->label_decl == ent_b->label_decl;
2436 /* Create a new label, named ID. */
2438 static tree
2439 make_label_decl (tree id, int local_p)
2441 struct named_label_entry *ent;
2442 void **slot;
2443 tree decl;
2445 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2447 DECL_CONTEXT (decl) = current_function_decl;
2448 DECL_MODE (decl) = VOIDmode;
2449 C_DECLARED_LABEL_FLAG (decl) = local_p;
2451 /* Say where one reference is to the label, for the sake of the
2452 error if it is not defined. */
2453 DECL_SOURCE_LOCATION (decl) = input_location;
2455 /* Record the fact that this identifier is bound to this label. */
2456 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2458 /* Create the label htab for the function on demand. */
2459 if (!named_labels)
2460 named_labels = htab_create_ggc (13, named_label_entry_hash,
2461 named_label_entry_eq, NULL);
2463 /* Record this label on the list of labels used in this function.
2464 We do this before calling make_label_decl so that we get the
2465 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2466 ent = ggc_alloc_cleared_named_label_entry ();
2467 ent->label_decl = decl;
2469 slot = htab_find_slot (named_labels, ent, INSERT);
2470 gcc_assert (*slot == NULL);
2471 *slot = ent;
2473 return decl;
2476 /* Look for a label named ID in the current function. If one cannot
2477 be found, create one. (We keep track of used, but undefined,
2478 labels, and complain about them at the end of a function.) */
2480 tree
2481 lookup_label (tree id)
2483 tree decl;
2485 timevar_push (TV_NAME_LOOKUP);
2486 /* You can't use labels at global scope. */
2487 if (current_function_decl == NULL_TREE)
2489 error ("label %qE referenced outside of any function", id);
2490 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2493 /* See if we've already got this label. */
2494 decl = IDENTIFIER_LABEL_VALUE (id);
2495 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2496 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2498 decl = make_label_decl (id, /*local_p=*/0);
2499 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2502 /* Declare a local label named ID. */
2504 tree
2505 declare_local_label (tree id)
2507 tree decl, shadow;
2509 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2510 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2511 shadow = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2512 current_binding_level->shadowed_labels);
2513 current_binding_level->shadowed_labels = shadow;
2515 decl = make_label_decl (id, /*local_p=*/1);
2516 TREE_VALUE (shadow) = decl;
2518 return decl;
2521 /* Returns nonzero if it is ill-formed to jump past the declaration of
2522 DECL. Returns 2 if it's also a real problem. */
2524 static int
2525 decl_jump_unsafe (tree decl)
2527 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
2528 with automatic storage duration is not in scope to a point where it is
2529 in scope is ill-formed unless the variable has scalar type, class type
2530 with a trivial default constructor and a trivial destructor, a
2531 cv-qualified version of one of these types, or an array of one of the
2532 preceding types and is declared without an initializer (8.5). */
2533 tree type = TREE_TYPE (decl);
2535 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2536 || type == error_mark_node)
2537 return 0;
2539 type = strip_array_types (type);
2541 if (type_has_nontrivial_default_init (TREE_TYPE (decl))
2542 || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2543 return 2;
2545 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2546 return 1;
2548 return 0;
2551 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2553 static void
2554 identify_goto (tree decl, const location_t *locus)
2556 if (decl)
2557 permerror (input_location, "jump to label %qD", decl);
2558 else
2559 permerror (input_location, "jump to case label");
2560 if (locus)
2561 permerror (*locus, " from here");
2564 /* Check that a single previously seen jump to a newly defined label
2565 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2566 the jump context; NAMES are the names in scope in LEVEL at the jump
2567 context; LOCUS is the source position of the jump or 0. Returns
2568 true if all is well. */
2570 static bool
2571 check_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names,
2572 bool exited_omp, const location_t *locus)
2574 struct cp_binding_level *b;
2575 bool identified = false, saw_eh = false, saw_omp = false;
2577 if (exited_omp)
2579 identify_goto (decl, locus);
2580 error (" exits OpenMP structured block");
2581 identified = saw_omp = true;
2584 for (b = current_binding_level; b ; b = b->level_chain)
2586 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2588 for (new_decls = b->names; new_decls != old_decls;
2589 new_decls = DECL_CHAIN (new_decls))
2591 int problem = decl_jump_unsafe (new_decls);
2592 if (! problem)
2593 continue;
2595 if (!identified)
2597 identify_goto (decl, locus);
2598 identified = true;
2600 if (problem > 1)
2601 error (" crosses initialization of %q+#D", new_decls);
2602 else
2603 permerror (input_location, " enters scope of %q+#D which has "
2604 "non-trivial destructor", new_decls);
2607 if (b == level)
2608 break;
2609 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2611 if (!identified)
2613 identify_goto (decl, locus);
2614 identified = true;
2616 if (b->kind == sk_try)
2617 error (" enters try block");
2618 else
2619 error (" enters catch block");
2620 saw_eh = true;
2622 if (b->kind == sk_omp && !saw_omp)
2624 if (!identified)
2626 identify_goto (decl, locus);
2627 identified = true;
2629 error (" enters OpenMP structured block");
2630 saw_omp = true;
2634 return !identified;
2637 static void
2638 check_previous_goto (tree decl, struct named_label_use_entry *use)
2640 check_previous_goto_1 (decl, use->binding_level,
2641 use->names_in_scope, use->in_omp_scope,
2642 &use->o_goto_locus);
2645 static bool
2646 check_switch_goto (struct cp_binding_level* level)
2648 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2651 /* Check that a new jump to a label DECL is OK. Called by
2652 finish_goto_stmt. */
2654 void
2655 check_goto (tree decl)
2657 struct named_label_entry *ent, dummy;
2658 bool saw_catch = false, identified = false;
2659 tree bad;
2660 unsigned ix;
2662 /* We can't know where a computed goto is jumping.
2663 So we assume that it's OK. */
2664 if (TREE_CODE (decl) != LABEL_DECL)
2665 return;
2667 /* We didn't record any information about this label when we created it,
2668 and there's not much point since it's trivial to analyze as a return. */
2669 if (decl == cdtor_label)
2670 return;
2672 dummy.label_decl = decl;
2673 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2674 gcc_assert (ent != NULL);
2676 /* If the label hasn't been defined yet, defer checking. */
2677 if (! DECL_INITIAL (decl))
2679 struct named_label_use_entry *new_use;
2681 /* Don't bother creating another use if the last goto had the
2682 same data, and will therefore create the same set of errors. */
2683 if (ent->uses
2684 && ent->uses->names_in_scope == current_binding_level->names)
2685 return;
2687 new_use = ggc_alloc_named_label_use_entry ();
2688 new_use->binding_level = current_binding_level;
2689 new_use->names_in_scope = current_binding_level->names;
2690 new_use->o_goto_locus = input_location;
2691 new_use->in_omp_scope = false;
2693 new_use->next = ent->uses;
2694 ent->uses = new_use;
2695 return;
2698 if (ent->in_try_scope || ent->in_catch_scope
2699 || ent->in_omp_scope || !VEC_empty (tree, ent->bad_decls))
2701 permerror (input_location, "jump to label %q+D", decl);
2702 permerror (input_location, " from here");
2703 identified = true;
2706 for (ix = 0; VEC_iterate (tree, ent->bad_decls, ix, bad); ix++)
2708 int u = decl_jump_unsafe (bad);
2710 if (u > 1 && DECL_ARTIFICIAL (bad))
2712 /* Can't skip init of __exception_info. */
2713 error_at (DECL_SOURCE_LOCATION (bad), " enters catch block");
2714 saw_catch = true;
2716 else if (u > 1)
2717 error (" skips initialization of %q+#D", bad);
2718 else
2719 permerror (input_location, " enters scope of %q+#D which has "
2720 "non-trivial destructor", bad);
2723 if (ent->in_try_scope)
2724 error (" enters try block");
2725 else if (ent->in_catch_scope && !saw_catch)
2726 error (" enters catch block");
2728 if (ent->in_omp_scope)
2729 error (" enters OpenMP structured block");
2730 else if (flag_openmp)
2732 struct cp_binding_level *b;
2733 for (b = current_binding_level; b ; b = b->level_chain)
2735 if (b == ent->binding_level)
2736 break;
2737 if (b->kind == sk_omp)
2739 if (!identified)
2741 permerror (input_location, "jump to label %q+D", decl);
2742 permerror (input_location, " from here");
2743 identified = true;
2745 error (" exits OpenMP structured block");
2746 break;
2752 /* Check that a return is ok wrt OpenMP structured blocks.
2753 Called by finish_return_stmt. Returns true if all is well. */
2755 bool
2756 check_omp_return (void)
2758 struct cp_binding_level *b;
2759 for (b = current_binding_level; b ; b = b->level_chain)
2760 if (b->kind == sk_omp)
2762 error ("invalid exit from OpenMP structured block");
2763 return false;
2765 return true;
2768 /* Define a label, specifying the location in the source file.
2769 Return the LABEL_DECL node for the label. */
2771 tree
2772 define_label (location_t location, tree name)
2774 struct named_label_entry *ent, dummy;
2775 struct cp_binding_level *p;
2776 tree decl;
2778 timevar_push (TV_NAME_LOOKUP);
2780 decl = lookup_label (name);
2782 dummy.label_decl = decl;
2783 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2784 gcc_assert (ent != NULL);
2786 /* After labels, make any new cleanups in the function go into their
2787 own new (temporary) binding contour. */
2788 for (p = current_binding_level;
2789 p->kind != sk_function_parms;
2790 p = p->level_chain)
2791 p->more_cleanups_ok = 0;
2793 if (name == get_identifier ("wchar_t"))
2794 permerror (input_location, "label named wchar_t");
2796 if (DECL_INITIAL (decl) != NULL_TREE)
2798 error ("duplicate label %qD", decl);
2799 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2801 else
2803 struct named_label_use_entry *use;
2805 /* Mark label as having been defined. */
2806 DECL_INITIAL (decl) = error_mark_node;
2807 /* Say where in the source. */
2808 DECL_SOURCE_LOCATION (decl) = location;
2810 ent->binding_level = current_binding_level;
2811 ent->names_in_scope = current_binding_level->names;
2813 for (use = ent->uses; use ; use = use->next)
2814 check_previous_goto (decl, use);
2815 ent->uses = NULL;
2818 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2821 struct cp_switch
2823 struct cp_binding_level *level;
2824 struct cp_switch *next;
2825 /* The SWITCH_STMT being built. */
2826 tree switch_stmt;
2827 /* A splay-tree mapping the low element of a case range to the high
2828 element, or NULL_TREE if there is no high element. Used to
2829 determine whether or not a new case label duplicates an old case
2830 label. We need a tree, rather than simply a hash table, because
2831 of the GNU case range extension. */
2832 splay_tree cases;
2835 /* A stack of the currently active switch statements. The innermost
2836 switch statement is on the top of the stack. There is no need to
2837 mark the stack for garbage collection because it is only active
2838 during the processing of the body of a function, and we never
2839 collect at that point. */
2841 static struct cp_switch *switch_stack;
2843 /* Called right after a switch-statement condition is parsed.
2844 SWITCH_STMT is the switch statement being parsed. */
2846 void
2847 push_switch (tree switch_stmt)
2849 struct cp_switch *p = XNEW (struct cp_switch);
2850 p->level = current_binding_level;
2851 p->next = switch_stack;
2852 p->switch_stmt = switch_stmt;
2853 p->cases = splay_tree_new (case_compare, NULL, NULL);
2854 switch_stack = p;
2857 void
2858 pop_switch (void)
2860 struct cp_switch *cs = switch_stack;
2861 location_t switch_location;
2863 /* Emit warnings as needed. */
2864 if (EXPR_HAS_LOCATION (cs->switch_stmt))
2865 switch_location = EXPR_LOCATION (cs->switch_stmt);
2866 else
2867 switch_location = input_location;
2868 if (!processing_template_decl)
2869 c_do_switch_warnings (cs->cases, switch_location,
2870 SWITCH_STMT_TYPE (cs->switch_stmt),
2871 SWITCH_STMT_COND (cs->switch_stmt));
2873 splay_tree_delete (cs->cases);
2874 switch_stack = switch_stack->next;
2875 free (cs);
2878 /* Note that we've seen a definition of a case label, and complain if this
2879 is a bad place for one. */
2881 tree
2882 finish_case_label (location_t loc, tree low_value, tree high_value)
2884 tree cond, r;
2885 struct cp_binding_level *p;
2887 if (processing_template_decl)
2889 tree label;
2891 /* For templates, just add the case label; we'll do semantic
2892 analysis at instantiation-time. */
2893 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
2894 return add_stmt (build_case_label (loc, low_value, high_value, label));
2897 /* Find the condition on which this switch statement depends. */
2898 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
2899 if (cond && TREE_CODE (cond) == TREE_LIST)
2900 cond = TREE_VALUE (cond);
2902 if (!check_switch_goto (switch_stack->level))
2903 return error_mark_node;
2905 r = c_add_case_label (loc, switch_stack->cases, cond,
2906 SWITCH_STMT_TYPE (switch_stack->switch_stmt),
2907 low_value, high_value);
2909 /* After labels, make any new cleanups in the function go into their
2910 own new (temporary) binding contour. */
2911 for (p = current_binding_level;
2912 p->kind != sk_function_parms;
2913 p = p->level_chain)
2914 p->more_cleanups_ok = 0;
2916 return r;
2919 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2921 static hashval_t
2922 typename_hash (const void* k)
2924 hashval_t hash;
2925 const_tree const t = (const_tree) k;
2927 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2928 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
2930 return hash;
2933 typedef struct typename_info {
2934 tree scope;
2935 tree name;
2936 tree template_id;
2937 bool enum_p;
2938 bool class_p;
2939 } typename_info;
2941 /* Compare two TYPENAME_TYPEs. K1 is really of type `tree', K2 is
2942 really of type `typename_info*' */
2944 static int
2945 typename_compare (const void * k1, const void * k2)
2947 const_tree const t1 = (const_tree) k1;
2948 const typename_info *const t2 = (const typename_info *) k2;
2950 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2951 && TYPE_CONTEXT (t1) == t2->scope
2952 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
2953 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
2954 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
2957 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
2958 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
2960 Returns the new TYPENAME_TYPE. */
2962 static GTY ((param_is (union tree_node))) htab_t typename_htab;
2964 static tree
2965 build_typename_type (tree context, tree name, tree fullname,
2966 enum tag_types tag_type)
2968 tree t;
2969 tree d;
2970 typename_info ti;
2971 void **e;
2972 hashval_t hash;
2974 if (typename_htab == NULL)
2975 typename_htab = htab_create_ggc (61, &typename_hash,
2976 &typename_compare, NULL);
2978 ti.scope = FROB_CONTEXT (context);
2979 ti.name = name;
2980 ti.template_id = fullname;
2981 ti.enum_p = tag_type == enum_type;
2982 ti.class_p = (tag_type == class_type
2983 || tag_type == record_type
2984 || tag_type == union_type);
2985 hash = (htab_hash_pointer (ti.scope)
2986 ^ htab_hash_pointer (ti.name));
2988 /* See if we already have this type. */
2989 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
2990 if (*e)
2991 t = (tree) *e;
2992 else
2994 /* Build the TYPENAME_TYPE. */
2995 t = cxx_make_type (TYPENAME_TYPE);
2996 TYPE_CONTEXT (t) = ti.scope;
2997 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
2998 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
2999 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3001 /* Build the corresponding TYPE_DECL. */
3002 d = build_decl (input_location, TYPE_DECL, name, t);
3003 TYPE_NAME (TREE_TYPE (d)) = d;
3004 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3005 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3006 DECL_ARTIFICIAL (d) = 1;
3008 /* Store it in the hash table. */
3009 *e = t;
3011 /* TYPENAME_TYPEs must always be compared structurally, because
3012 they may or may not resolve down to another type depending on
3013 the currently open classes. */
3014 SET_TYPE_STRUCTURAL_EQUALITY (t);
3017 return t;
3020 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3021 provided to name the type. Returns an appropriate type, unless an
3022 error occurs, in which case error_mark_node is returned. If we
3023 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3024 return that, rather than the _TYPE it corresponds to, in other
3025 cases we look through the type decl. If TF_ERROR is set, complain
3026 about errors, otherwise be quiet. */
3028 tree
3029 make_typename_type (tree context, tree name, enum tag_types tag_type,
3030 tsubst_flags_t complain)
3032 tree fullname;
3033 tree t;
3034 bool want_template;
3036 if (name == error_mark_node
3037 || context == NULL_TREE
3038 || context == error_mark_node)
3039 return error_mark_node;
3041 if (TYPE_P (name))
3043 if (!(TYPE_LANG_SPECIFIC (name)
3044 && (CLASSTYPE_IS_TEMPLATE (name)
3045 || CLASSTYPE_USE_TEMPLATE (name))))
3046 name = TYPE_IDENTIFIER (name);
3047 else
3048 /* Create a TEMPLATE_ID_EXPR for the type. */
3049 name = build_nt (TEMPLATE_ID_EXPR,
3050 CLASSTYPE_TI_TEMPLATE (name),
3051 CLASSTYPE_TI_ARGS (name));
3053 else if (TREE_CODE (name) == TYPE_DECL)
3054 name = DECL_NAME (name);
3056 fullname = name;
3058 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3060 name = TREE_OPERAND (name, 0);
3061 if (TREE_CODE (name) == TEMPLATE_DECL)
3062 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3063 else if (TREE_CODE (name) == OVERLOAD)
3065 error ("%qD is not a type", name);
3066 return error_mark_node;
3069 if (TREE_CODE (name) == TEMPLATE_DECL)
3071 error ("%qD used without template parameters", name);
3072 return error_mark_node;
3074 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3075 gcc_assert (TYPE_P (context));
3077 if (!MAYBE_CLASS_TYPE_P (context))
3079 if (complain & tf_error)
3080 error ("%q#T is not a class", context);
3081 return error_mark_node;
3084 /* When the CONTEXT is a dependent type, NAME could refer to a
3085 dependent base class of CONTEXT. But look inside it anyway
3086 if CONTEXT is a currently open scope, in case it refers to a
3087 member of the current instantiation or a non-dependent base;
3088 lookup will stop when we hit a dependent base. */
3089 if (!dependent_scope_p (context))
3090 /* We should only set WANT_TYPE when we're a nested typename type.
3091 Then we can give better diagnostics if we find a non-type. */
3092 t = lookup_field (context, name, 2, /*want_type=*/true);
3093 else
3094 t = NULL_TREE;
3096 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3097 return build_typename_type (context, name, fullname, tag_type);
3099 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3101 if (!t)
3103 if (complain & tf_error)
3104 error (want_template ? "no class template named %q#T in %q#T"
3105 : "no type named %q#T in %q#T", name, context);
3106 return error_mark_node;
3109 /* Pull out the template from an injected-class-name (or multiple). */
3110 if (want_template)
3111 t = maybe_get_template_decl_from_type_decl (t);
3113 if (TREE_CODE (t) == TREE_LIST)
3115 if (complain & tf_error)
3117 error ("lookup of %qT in %qT is ambiguous", name, context);
3118 print_candidates (t);
3120 return error_mark_node;
3123 if (want_template && !DECL_CLASS_TEMPLATE_P (t))
3125 if (complain & tf_error)
3126 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3127 context, name, t);
3128 return error_mark_node;
3130 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3132 if (complain & tf_error)
3133 error ("%<typename %T::%D%> names %q#T, which is not a type",
3134 context, name, t);
3135 return error_mark_node;
3138 if (complain & tf_error)
3139 perform_or_defer_access_check (TYPE_BINFO (context), t, t);
3141 /* If we are currently parsing a template and if T is a typedef accessed
3142 through CONTEXT then we need to remember and check access of T at
3143 template instantiation time. */
3144 add_typedef_to_current_template_for_access_check (t, context, input_location);
3146 if (want_template)
3147 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3148 NULL_TREE, context,
3149 /*entering_scope=*/0,
3150 tf_warning_or_error | tf_user);
3152 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3153 t = TREE_TYPE (t);
3155 return t;
3158 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3159 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3160 in which case error_mark_node is returned.
3162 If PARM_LIST is non-NULL, also make sure that the template parameter
3163 list of TEMPLATE_DECL matches.
3165 If COMPLAIN zero, don't complain about any errors that occur. */
3167 tree
3168 make_unbound_class_template (tree context, tree name, tree parm_list,
3169 tsubst_flags_t complain)
3171 tree t;
3172 tree d;
3174 if (TYPE_P (name))
3175 name = TYPE_IDENTIFIER (name);
3176 else if (DECL_P (name))
3177 name = DECL_NAME (name);
3178 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3180 if (!dependent_type_p (context)
3181 || currently_open_class (context))
3183 tree tmpl = NULL_TREE;
3185 if (MAYBE_CLASS_TYPE_P (context))
3186 tmpl = lookup_field (context, name, 0, false);
3188 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
3190 if (complain & tf_error)
3191 error ("no class template named %q#T in %q#T", name, context);
3192 return error_mark_node;
3195 if (parm_list
3196 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3198 if (complain & tf_error)
3200 error ("template parameters do not match template");
3201 error ("%q+D declared here", tmpl);
3203 return error_mark_node;
3206 if (complain & tf_error)
3207 perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl);
3209 return tmpl;
3212 /* Build the UNBOUND_CLASS_TEMPLATE. */
3213 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3214 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3215 TREE_TYPE (t) = NULL_TREE;
3216 SET_TYPE_STRUCTURAL_EQUALITY (t);
3218 /* Build the corresponding TEMPLATE_DECL. */
3219 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3220 TYPE_NAME (TREE_TYPE (d)) = d;
3221 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3222 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3223 DECL_ARTIFICIAL (d) = 1;
3224 DECL_TEMPLATE_PARMS (d) = parm_list;
3226 return t;
3231 /* Push the declarations of builtin types into the namespace.
3232 RID_INDEX is the index of the builtin type in the array
3233 RID_POINTERS. NAME is the name used when looking up the builtin
3234 type. TYPE is the _TYPE node for the builtin type. */
3236 void
3237 record_builtin_type (enum rid rid_index,
3238 const char* name,
3239 tree type)
3241 tree rname = NULL_TREE, tname = NULL_TREE;
3242 tree tdecl = NULL_TREE;
3244 if ((int) rid_index < (int) RID_MAX)
3245 rname = ridpointers[(int) rid_index];
3246 if (name)
3247 tname = get_identifier (name);
3249 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3250 eliminated. Built-in types should not be looked up name; their
3251 names are keywords that the parser can recognize. However, there
3252 is code in c-common.c that uses identifier_global_value to look
3253 up built-in types by name. */
3254 if (tname)
3256 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3257 DECL_ARTIFICIAL (tdecl) = 1;
3258 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3260 if (rname)
3262 if (!tdecl)
3264 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3265 DECL_ARTIFICIAL (tdecl) = 1;
3267 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3270 if (!TYPE_NAME (type))
3271 TYPE_NAME (type) = tdecl;
3273 if (tdecl)
3274 debug_hooks->type_decl (tdecl, 0);
3277 /* Record one of the standard Java types.
3278 * Declare it as having the given NAME.
3279 * If SIZE > 0, it is the size of one of the integral types;
3280 * otherwise it is the negative of the size of one of the other types. */
3282 static tree
3283 record_builtin_java_type (const char* name, int size)
3285 tree type, decl;
3286 if (size > 0)
3287 type = build_nonstandard_integer_type (size, 0);
3288 else if (size > -32)
3290 tree stype;
3291 /* "__java_char" or ""__java_boolean". */
3292 type = build_nonstandard_integer_type (-size, 1);
3293 /* Get the signed type cached and attached to the unsigned type,
3294 so it doesn't get garbage-collected at "random" times,
3295 causing potential codegen differences out of different UIDs
3296 and different alias set numbers. */
3297 stype = build_nonstandard_integer_type (-size, 0);
3298 TREE_CHAIN (type) = stype;
3299 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3301 else
3302 { /* "__java_float" or ""__java_double". */
3303 type = make_node (REAL_TYPE);
3304 TYPE_PRECISION (type) = - size;
3305 layout_type (type);
3307 record_builtin_type (RID_MAX, name, type);
3308 decl = TYPE_NAME (type);
3310 /* Suppress generate debug symbol entries for these types,
3311 since for normal C++ they are just clutter.
3312 However, push_lang_context undoes this if extern "Java" is seen. */
3313 DECL_IGNORED_P (decl) = 1;
3315 TYPE_FOR_JAVA (type) = 1;
3316 return type;
3319 /* Push a type into the namespace so that the back ends ignore it. */
3321 static void
3322 record_unknown_type (tree type, const char* name)
3324 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3325 TYPE_DECL, get_identifier (name), type));
3326 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3327 DECL_IGNORED_P (decl) = 1;
3328 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3329 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3330 TYPE_ALIGN (type) = 1;
3331 TYPE_USER_ALIGN (type) = 0;
3332 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3335 /* A string for which we should create an IDENTIFIER_NODE at
3336 startup. */
3338 typedef struct predefined_identifier
3340 /* The name of the identifier. */
3341 const char *const name;
3342 /* The place where the IDENTIFIER_NODE should be stored. */
3343 tree *const node;
3344 /* Nonzero if this is the name of a constructor or destructor. */
3345 const int ctor_or_dtor_p;
3346 } predefined_identifier;
3348 /* Create all the predefined identifiers. */
3350 static void
3351 initialize_predefined_identifiers (void)
3353 const predefined_identifier *pid;
3355 /* A table of identifiers to create at startup. */
3356 static const predefined_identifier predefined_identifiers[] = {
3357 { "C++", &lang_name_cplusplus, 0 },
3358 { "C", &lang_name_c, 0 },
3359 { "Java", &lang_name_java, 0 },
3360 /* Some of these names have a trailing space so that it is
3361 impossible for them to conflict with names written by users. */
3362 { "__ct ", &ctor_identifier, 1 },
3363 { "__base_ctor ", &base_ctor_identifier, 1 },
3364 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3365 { "__dt ", &dtor_identifier, 1 },
3366 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3367 { "__base_dtor ", &base_dtor_identifier, 1 },
3368 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3369 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3370 { "nelts", &nelts_identifier, 0 },
3371 { THIS_NAME, &this_identifier, 0 },
3372 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3373 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3374 { "_vptr", &vptr_identifier, 0 },
3375 { "__vtt_parm", &vtt_parm_identifier, 0 },
3376 { "::", &global_scope_name, 0 },
3377 { "std", &std_identifier, 0 },
3378 { NULL, NULL, 0 }
3381 for (pid = predefined_identifiers; pid->name; ++pid)
3383 *pid->node = get_identifier (pid->name);
3384 if (pid->ctor_or_dtor_p)
3385 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3389 /* Create the predefined scalar types of C,
3390 and some nodes representing standard constants (0, 1, (void *)0).
3391 Initialize the global binding level.
3392 Make definitions for built-in primitive functions. */
3394 void
3395 cxx_init_decl_processing (void)
3397 tree void_ftype;
3398 tree void_ftype_ptr;
3400 build_common_tree_nodes (flag_signed_char);
3402 /* Create all the identifiers we need. */
3403 initialize_predefined_identifiers ();
3405 /* Create the global variables. */
3406 push_to_top_level ();
3408 current_function_decl = NULL_TREE;
3409 current_binding_level = NULL;
3410 /* Enter the global namespace. */
3411 gcc_assert (global_namespace == NULL_TREE);
3412 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3413 void_type_node);
3414 TREE_PUBLIC (global_namespace) = 1;
3415 begin_scope (sk_namespace, global_namespace);
3417 current_lang_name = NULL_TREE;
3419 if (flag_visibility_ms_compat)
3420 default_visibility = VISIBILITY_HIDDEN;
3422 /* Initially, C. */
3423 current_lang_name = lang_name_c;
3425 /* Create the `std' namespace. */
3426 push_namespace (std_identifier);
3427 std_node = current_namespace;
3428 pop_namespace ();
3430 c_common_nodes_and_builtins ();
3432 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3433 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3434 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3435 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3436 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3437 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3438 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3439 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3441 integer_two_node = build_int_cst (NULL_TREE, 2);
3443 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3444 truthvalue_type_node = boolean_type_node;
3445 truthvalue_false_node = boolean_false_node;
3446 truthvalue_true_node = boolean_true_node;
3448 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3449 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
3450 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
3452 #if 0
3453 record_builtin_type (RID_MAX, NULL, string_type_node);
3454 #endif
3456 delta_type_node = ptrdiff_type_node;
3457 vtable_index_type = ptrdiff_type_node;
3459 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3460 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
3461 void_ftype_ptr = build_function_type_list (void_type_node,
3462 ptr_type_node, NULL_TREE);
3463 void_ftype_ptr
3464 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3466 /* C++ extensions */
3468 unknown_type_node = make_node (LANG_TYPE);
3469 record_unknown_type (unknown_type_node, "unknown type");
3471 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3472 TREE_TYPE (unknown_type_node) = unknown_type_node;
3474 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3475 result. */
3476 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3477 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3479 init_list_type_node = make_node (LANG_TYPE);
3480 record_unknown_type (init_list_type_node, "init list");
3483 /* Make sure we get a unique function type, so we can give
3484 its pointer type a name. (This wins for gdb.) */
3485 tree vfunc_type = make_node (FUNCTION_TYPE);
3486 TREE_TYPE (vfunc_type) = integer_type_node;
3487 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3488 layout_type (vfunc_type);
3490 vtable_entry_type = build_pointer_type (vfunc_type);
3492 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3494 vtbl_type_node
3495 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3496 layout_type (vtbl_type_node);
3497 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3498 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3499 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3500 layout_type (vtbl_ptr_type_node);
3501 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3503 push_namespace (get_identifier ("__cxxabiv1"));
3504 abi_node = current_namespace;
3505 pop_namespace ();
3507 global_type_node = make_node (LANG_TYPE);
3508 record_unknown_type (global_type_node, "global type");
3510 /* Now, C++. */
3511 current_lang_name = lang_name_cplusplus;
3514 tree newtype, deltype;
3515 tree ptr_ftype_sizetype;
3516 tree new_eh_spec;
3518 ptr_ftype_sizetype
3519 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
3520 if (cxx_dialect == cxx98)
3522 tree bad_alloc_id;
3523 tree bad_alloc_type_node;
3524 tree bad_alloc_decl;
3526 push_namespace (std_identifier);
3527 bad_alloc_id = get_identifier ("bad_alloc");
3528 bad_alloc_type_node = make_class_type (RECORD_TYPE);
3529 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3530 bad_alloc_decl
3531 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3532 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3533 pop_namespace ();
3535 new_eh_spec
3536 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
3538 else
3539 new_eh_spec = noexcept_false_spec;
3541 newtype = build_exception_variant (ptr_ftype_sizetype, new_eh_spec);
3542 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3543 push_cp_library_fn (NEW_EXPR, newtype);
3544 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3545 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3546 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3548 nullptr_type_node = make_node (LANG_TYPE);
3549 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
3550 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
3551 TYPE_UNSIGNED (nullptr_type_node) = 1;
3552 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
3553 SET_TYPE_MODE (nullptr_type_node, Pmode);
3554 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
3555 nullptr_node = make_node (INTEGER_CST);
3556 TREE_TYPE (nullptr_node) = nullptr_type_node;
3559 abort_fndecl
3560 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3562 /* Perform other language dependent initializations. */
3563 init_class_processing ();
3564 init_rtti_processing ();
3565 init_template_processing ();
3567 if (flag_exceptions)
3568 init_exception_processing ();
3570 if (! supports_one_only ())
3571 flag_weak = 0;
3573 make_fname_decl = cp_make_fname_decl;
3574 start_fname_decls ();
3576 /* Show we use EH for cleanups. */
3577 if (flag_exceptions)
3578 using_eh_for_cleanups ();
3581 /* Generate an initializer for a function naming variable from
3582 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3583 filled in with the type of the init. */
3585 tree
3586 cp_fname_init (const char* name, tree *type_p)
3588 tree domain = NULL_TREE;
3589 tree type;
3590 tree init = NULL_TREE;
3591 size_t length = 0;
3593 if (name)
3595 length = strlen (name);
3596 domain = build_index_type (size_int (length));
3597 init = build_string (length + 1, name);
3600 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3601 type = build_cplus_array_type (type, domain);
3603 *type_p = type;
3605 if (init)
3606 TREE_TYPE (init) = type;
3607 else
3608 init = error_mark_node;
3610 return init;
3613 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
3614 the decl, LOC is the location to give the decl, NAME is the
3615 initialization string and TYPE_DEP indicates whether NAME depended
3616 on the type of the function. We make use of that to detect
3617 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
3618 at the point of first use, so we mustn't push the decl now. */
3620 static tree
3621 cp_make_fname_decl (location_t loc, tree id, int type_dep)
3623 const char *const name = (type_dep && processing_template_decl
3624 ? NULL : fname_as_string (type_dep));
3625 tree type;
3626 tree init = cp_fname_init (name, &type);
3627 tree decl = build_decl (loc, VAR_DECL, id, type);
3629 if (name)
3630 free (CONST_CAST (char *, name));
3632 /* As we're using pushdecl_with_scope, we must set the context. */
3633 DECL_CONTEXT (decl) = current_function_decl;
3634 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3636 TREE_STATIC (decl) = 1;
3637 TREE_READONLY (decl) = 1;
3638 DECL_ARTIFICIAL (decl) = 1;
3640 TREE_USED (decl) = 1;
3642 if (current_function_decl)
3644 struct cp_binding_level *b = current_binding_level;
3645 while (b->level_chain->kind != sk_function_parms)
3646 b = b->level_chain;
3647 pushdecl_with_scope (decl, b, /*is_friend=*/false);
3648 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
3649 LOOKUP_ONLYCONVERTING);
3651 else
3652 pushdecl_top_level_and_finish (decl, init);
3654 return decl;
3657 static tree
3658 builtin_function_1 (tree decl, tree context, bool is_global)
3660 tree id = DECL_NAME (decl);
3661 const char *name = IDENTIFIER_POINTER (id);
3663 retrofit_lang_decl (decl);
3665 DECL_ARTIFICIAL (decl) = 1;
3666 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
3667 SET_DECL_LANGUAGE (decl, lang_c);
3668 /* Runtime library routines are, by definition, available in an
3669 external shared object. */
3670 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
3671 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3673 DECL_CONTEXT (decl) = context;
3675 if (is_global)
3676 pushdecl_top_level (decl);
3677 else
3678 pushdecl (decl);
3680 /* A function in the user's namespace should have an explicit
3681 declaration before it is used. Mark the built-in function as
3682 anticipated but not actually declared. */
3683 if (name[0] != '_' || name[1] != '_')
3684 DECL_ANTICIPATED (decl) = 1;
3685 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
3687 size_t len = strlen (name);
3689 /* Treat __*_chk fortification functions as anticipated as well,
3690 unless they are __builtin_*. */
3691 if (len > strlen ("___chk")
3692 && memcmp (name + len - strlen ("_chk"),
3693 "_chk", strlen ("_chk") + 1) == 0)
3694 DECL_ANTICIPATED (decl) = 1;
3697 return decl;
3700 tree
3701 cxx_builtin_function (tree decl)
3703 tree id = DECL_NAME (decl);
3704 const char *name = IDENTIFIER_POINTER (id);
3705 /* All builtins that don't begin with an '_' should additionally
3706 go in the 'std' namespace. */
3707 if (name[0] != '_')
3709 tree decl2 = copy_node(decl);
3710 push_namespace (std_identifier);
3711 builtin_function_1 (decl2, std_node, false);
3712 pop_namespace ();
3715 return builtin_function_1 (decl, NULL_TREE, false);
3718 /* Like cxx_builtin_function, but guarantee the function is added to the global
3719 scope. This is to allow function specific options to add new machine
3720 dependent builtins when the target ISA changes via attribute((target(...)))
3721 which saves space on program startup if the program does not use non-generic
3722 ISAs. */
3724 tree
3725 cxx_builtin_function_ext_scope (tree decl)
3728 tree id = DECL_NAME (decl);
3729 const char *name = IDENTIFIER_POINTER (id);
3730 /* All builtins that don't begin with an '_' should additionally
3731 go in the 'std' namespace. */
3732 if (name[0] != '_')
3734 tree decl2 = copy_node(decl);
3735 push_namespace (std_identifier);
3736 builtin_function_1 (decl2, std_node, true);
3737 pop_namespace ();
3740 return builtin_function_1 (decl, NULL_TREE, true);
3743 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
3744 function. Not called directly. */
3746 static tree
3747 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3749 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3750 DECL_EXTERNAL (fn) = 1;
3751 TREE_PUBLIC (fn) = 1;
3752 DECL_ARTIFICIAL (fn) = 1;
3753 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
3754 SET_DECL_LANGUAGE (fn, lang_c);
3755 /* Runtime library routines are, by definition, available in an
3756 external shared object. */
3757 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3758 DECL_VISIBILITY_SPECIFIED (fn) = 1;
3759 return fn;
3762 /* Returns the _DECL for a library function with C linkage.
3763 We assume that such functions never throw; if this is incorrect,
3764 callers should unset TREE_NOTHROW. */
3766 static tree
3767 build_library_fn (tree name, tree type)
3769 tree fn = build_library_fn_1 (name, ERROR_MARK, type);
3770 TREE_NOTHROW (fn) = 1;
3771 return fn;
3774 /* Returns the _DECL for a library function with C++ linkage. */
3776 static tree
3777 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
3779 tree fn = build_library_fn_1 (name, operator_code, type);
3780 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
3781 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
3782 SET_DECL_LANGUAGE (fn, lang_cplusplus);
3783 return fn;
3786 /* Like build_library_fn, but takes a C string instead of an
3787 IDENTIFIER_NODE. */
3789 tree
3790 build_library_fn_ptr (const char* name, tree type)
3792 return build_library_fn (get_identifier (name), type);
3795 /* Like build_cp_library_fn, but takes a C string instead of an
3796 IDENTIFIER_NODE. */
3798 tree
3799 build_cp_library_fn_ptr (const char* name, tree type)
3801 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
3804 /* Like build_library_fn, but also pushes the function so that we will
3805 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
3806 may throw exceptions listed in RAISES. */
3808 tree
3809 push_library_fn (tree name, tree type, tree raises)
3811 tree fn;
3813 if (raises)
3814 type = build_exception_variant (type, raises);
3816 fn = build_library_fn (name, type);
3817 pushdecl_top_level (fn);
3818 return fn;
3821 /* Like build_cp_library_fn, but also pushes the function so that it
3822 will be found by normal lookup. */
3824 static tree
3825 push_cp_library_fn (enum tree_code operator_code, tree type)
3827 tree fn = build_cp_library_fn (ansi_opname (operator_code),
3828 operator_code,
3829 type);
3830 pushdecl (fn);
3831 return fn;
3834 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3835 a FUNCTION_TYPE. */
3837 tree
3838 push_void_library_fn (tree name, tree parmtypes)
3840 tree type = build_function_type (void_type_node, parmtypes);
3841 return push_library_fn (name, type, NULL_TREE);
3844 /* Like push_library_fn, but also note that this function throws
3845 and does not return. Used for __throw_foo and the like. */
3847 tree
3848 push_throw_library_fn (tree name, tree type)
3850 tree fn = push_library_fn (name, type, NULL_TREE);
3851 TREE_THIS_VOLATILE (fn) = 1;
3852 TREE_NOTHROW (fn) = 0;
3853 return fn;
3856 /* When we call finish_struct for an anonymous union, we create
3857 default copy constructors and such. But, an anonymous union
3858 shouldn't have such things; this function undoes the damage to the
3859 anonymous union type T.
3861 (The reason that we create the synthesized methods is that we don't
3862 distinguish `union { int i; }' from `typedef union { int i; } U'.
3863 The first is an anonymous union; the second is just an ordinary
3864 union type.) */
3866 void
3867 fixup_anonymous_aggr (tree t)
3869 tree *q;
3871 /* Wipe out memory of synthesized methods. */
3872 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
3873 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3874 TYPE_HAS_COPY_CTOR (t) = 0;
3875 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
3876 TYPE_HAS_COPY_ASSIGN (t) = 0;
3877 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
3879 /* Splice the implicitly generated functions out of the TYPE_METHODS
3880 list. */
3881 q = &TYPE_METHODS (t);
3882 while (*q)
3884 if (DECL_ARTIFICIAL (*q))
3885 *q = TREE_CHAIN (*q);
3886 else
3887 q = &DECL_CHAIN (*q);
3890 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
3891 if (TYPE_METHODS (t))
3893 tree decl = TYPE_MAIN_DECL (t);
3895 if (TREE_CODE (t) != UNION_TYPE)
3896 error_at (DECL_SOURCE_LOCATION (decl),
3897 "an anonymous struct cannot have function members");
3898 else
3899 error_at (DECL_SOURCE_LOCATION (decl),
3900 "an anonymous union cannot have function members");
3903 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3904 assignment operators (because they cannot have these methods themselves).
3905 For anonymous unions this is already checked because they are not allowed
3906 in any union, otherwise we have to check it. */
3907 if (TREE_CODE (t) != UNION_TYPE)
3909 tree field, type;
3911 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3912 if (TREE_CODE (field) == FIELD_DECL)
3914 type = TREE_TYPE (field);
3915 if (CLASS_TYPE_P (type))
3917 if (TYPE_NEEDS_CONSTRUCTING (type))
3918 error ("member %q+#D with constructor not allowed "
3919 "in anonymous aggregate", field);
3920 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3921 error ("member %q+#D with destructor not allowed "
3922 "in anonymous aggregate", field);
3923 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3924 error ("member %q+#D with copy assignment operator "
3925 "not allowed in anonymous aggregate", field);
3931 /* Make sure that a declaration with no declarator is well-formed, i.e.
3932 just declares a tagged type or anonymous union.
3934 Returns the type declared; or NULL_TREE if none. */
3936 tree
3937 check_tag_decl (cp_decl_specifier_seq *declspecs)
3939 int saw_friend = declspecs->specs[(int)ds_friend] != 0;
3940 int saw_typedef = declspecs->specs[(int)ds_typedef] != 0;
3941 /* If a class, struct, or enum type is declared by the DECLSPECS
3942 (i.e, if a class-specifier, enum-specifier, or non-typename
3943 elaborated-type-specifier appears in the DECLSPECS),
3944 DECLARED_TYPE is set to the corresponding type. */
3945 tree declared_type = NULL_TREE;
3946 bool error_p = false;
3948 if (declspecs->multiple_types_p)
3949 error ("multiple types in one declaration");
3950 else if (declspecs->redefined_builtin_type)
3952 if (!in_system_header)
3953 permerror (input_location, "redeclaration of C++ built-in type %qT",
3954 declspecs->redefined_builtin_type);
3955 return NULL_TREE;
3958 if (declspecs->type
3959 && TYPE_P (declspecs->type)
3960 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
3961 && MAYBE_CLASS_TYPE_P (declspecs->type))
3962 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
3963 declared_type = declspecs->type;
3964 else if (declspecs->type == error_mark_node)
3965 error_p = true;
3966 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
3967 permerror (input_location, "declaration does not declare anything");
3968 /* Check for an anonymous union. */
3969 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
3970 && TYPE_ANONYMOUS_P (declared_type))
3972 /* 7/3 In a simple-declaration, the optional init-declarator-list
3973 can be omitted only when declaring a class (clause 9) or
3974 enumeration (7.2), that is, when the decl-specifier-seq contains
3975 either a class-specifier, an elaborated-type-specifier with
3976 a class-key (9.1), or an enum-specifier. In these cases and
3977 whenever a class-specifier or enum-specifier is present in the
3978 decl-specifier-seq, the identifiers in these specifiers are among
3979 the names being declared by the declaration (as class-name,
3980 enum-names, or enumerators, depending on the syntax). In such
3981 cases, and except for the declaration of an unnamed bit-field (9.6),
3982 the decl-specifier-seq shall introduce one or more names into the
3983 program, or shall redeclare a name introduced by a previous
3984 declaration. [Example:
3985 enum { }; // ill-formed
3986 typedef class { }; // ill-formed
3987 --end example] */
3988 if (saw_typedef)
3990 error ("missing type-name in typedef-declaration");
3991 return NULL_TREE;
3993 /* Anonymous unions are objects, so they can have specifiers. */;
3994 SET_ANON_AGGR_TYPE_P (declared_type);
3996 if (TREE_CODE (declared_type) != UNION_TYPE && !in_system_header)
3997 pedwarn (input_location, OPT_pedantic, "ISO C++ prohibits anonymous structs");
4000 else
4002 if (declspecs->specs[(int)ds_inline]
4003 || declspecs->specs[(int)ds_virtual])
4004 error ("%qs can only be specified for functions",
4005 declspecs->specs[(int)ds_inline]
4006 ? "inline" : "virtual");
4007 else if (saw_friend
4008 && (!current_class_type
4009 || current_scope () != current_class_type))
4010 error ("%<friend%> can only be specified inside a class");
4011 else if (declspecs->specs[(int)ds_explicit])
4012 error ("%<explicit%> can only be specified for constructors");
4013 else if (declspecs->storage_class)
4014 error ("a storage class can only be specified for objects "
4015 "and functions");
4016 else if (declspecs->specs[(int)ds_const]
4017 || declspecs->specs[(int)ds_volatile]
4018 || declspecs->specs[(int)ds_restrict]
4019 || declspecs->specs[(int)ds_thread])
4020 error ("qualifiers can only be specified for objects "
4021 "and functions");
4022 else if (saw_typedef)
4023 warning (0, "%<typedef%> was ignored in this declaration");
4024 else if (declspecs->specs[(int) ds_constexpr])
4025 error ("%<constexpr> cannot be used for type declarations");
4028 return declared_type;
4031 /* Called when a declaration is seen that contains no names to declare.
4032 If its type is a reference to a structure, union or enum inherited
4033 from a containing scope, shadow that tag name for the current scope
4034 with a forward reference.
4035 If its type defines a new named structure or union
4036 or defines an enum, it is valid but we need not do anything here.
4037 Otherwise, it is an error.
4039 C++: may have to grok the declspecs to learn about static,
4040 complain for anonymous unions.
4042 Returns the TYPE declared -- or NULL_TREE if none. */
4044 tree
4045 shadow_tag (cp_decl_specifier_seq *declspecs)
4047 tree t = check_tag_decl (declspecs);
4049 if (!t)
4050 return NULL_TREE;
4052 if (declspecs->attributes)
4054 warning (0, "attribute ignored in declaration of %q+#T", t);
4055 warning (0, "attribute for %q+#T must follow the %qs keyword",
4056 t, class_key_or_enum_as_string (t));
4060 if (maybe_process_partial_specialization (t) == error_mark_node)
4061 return NULL_TREE;
4063 /* This is where the variables in an anonymous union are
4064 declared. An anonymous union declaration looks like:
4065 union { ... } ;
4066 because there is no declarator after the union, the parser
4067 sends that declaration here. */
4068 if (ANON_AGGR_TYPE_P (t))
4070 fixup_anonymous_aggr (t);
4072 if (TYPE_FIELDS (t))
4074 tree decl = grokdeclarator (/*declarator=*/NULL,
4075 declspecs, NORMAL, 0, NULL);
4076 finish_anon_union (decl);
4080 return t;
4083 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4085 tree
4086 groktypename (cp_decl_specifier_seq *type_specifiers,
4087 const cp_declarator *declarator,
4088 bool is_template_arg)
4090 tree attrs;
4091 tree type;
4092 enum decl_context context
4093 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4094 attrs = type_specifiers->attributes;
4095 type_specifiers->attributes = NULL_TREE;
4096 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4097 if (attrs && type != error_mark_node)
4099 if (CLASS_TYPE_P (type))
4100 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4101 "outside of definition", type);
4102 else if (MAYBE_CLASS_TYPE_P (type))
4103 /* A template type parameter or other dependent type. */
4104 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4105 "type %qT without an associated declaration", type);
4106 else
4107 cplus_decl_attributes (&type, attrs, 0);
4109 return type;
4112 /* Process a DECLARATOR for a function-scope variable declaration,
4113 namespace-scope variable declaration, or function declaration.
4114 (Function definitions go through start_function; class member
4115 declarations appearing in the body of the class go through
4116 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4117 If an error occurs, the error_mark_node is returned instead.
4119 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4120 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4121 for an explicitly defaulted function, or SD_DELETED for an explicitly
4122 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4123 implicitly initialized via a default constructor. ATTRIBUTES and
4124 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4125 *PUSHED_SCOPE_P is set to the scope entered in this function, if any; if
4126 set, the caller is responsible for calling pop_scope. */
4128 tree
4129 start_decl (const cp_declarator *declarator,
4130 cp_decl_specifier_seq *declspecs,
4131 int initialized,
4132 tree attributes,
4133 tree prefix_attributes,
4134 tree *pushed_scope_p)
4136 tree decl;
4137 tree context;
4138 bool was_public;
4139 int flags;
4140 bool alias;
4142 *pushed_scope_p = NULL_TREE;
4144 /* An object declared as __attribute__((deprecated)) suppresses
4145 warnings of uses of other deprecated items. */
4146 if (lookup_attribute ("deprecated", attributes))
4147 deprecated_state = DEPRECATED_SUPPRESS;
4149 attributes = chainon (attributes, prefix_attributes);
4151 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4152 &attributes);
4154 deprecated_state = DEPRECATED_NORMAL;
4156 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE
4157 || decl == error_mark_node)
4158 return error_mark_node;
4160 context = DECL_CONTEXT (decl);
4162 if (context)
4164 *pushed_scope_p = push_scope (context);
4166 /* We are only interested in class contexts, later. */
4167 if (TREE_CODE (context) == NAMESPACE_DECL)
4168 context = NULL_TREE;
4171 if (initialized)
4172 /* Is it valid for this decl to have an initializer at all?
4173 If not, set INITIALIZED to zero, which will indirectly
4174 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4175 switch (TREE_CODE (decl))
4177 case TYPE_DECL:
4178 error ("typedef %qD is initialized (use decltype instead)", decl);
4179 return error_mark_node;
4181 case FUNCTION_DECL:
4182 if (initialized == SD_DELETED)
4183 /* We'll handle the rest of the semantics later, but we need to
4184 set this now so it's visible to duplicate_decls. */
4185 DECL_DELETED_FN (decl) = 1;
4186 break;
4188 default:
4189 break;
4192 if (initialized)
4194 if (! toplevel_bindings_p ()
4195 && DECL_EXTERNAL (decl))
4196 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4197 decl);
4198 DECL_EXTERNAL (decl) = 0;
4199 if (toplevel_bindings_p ())
4200 TREE_STATIC (decl) = 1;
4202 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4204 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4205 record_key_method_defined (decl);
4207 /* If this is a typedef that names the class for linkage purposes
4208 (7.1.3p8), apply any attributes directly to the type. */
4209 if (TREE_CODE (decl) == TYPE_DECL
4210 && TAGGED_TYPE_P (TREE_TYPE (decl))
4211 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
4212 flags = ATTR_FLAG_TYPE_IN_PLACE;
4213 else
4214 flags = 0;
4216 /* Set attributes here so if duplicate decl, will have proper attributes. */
4217 cplus_decl_attributes (&decl, attributes, flags);
4219 /* Dllimported symbols cannot be defined. Static data members (which
4220 can be initialized in-class and dllimported) go through grokfield,
4221 not here, so we don't need to exclude those decls when checking for
4222 a definition. */
4223 if (initialized && DECL_DLLIMPORT_P (decl))
4225 error ("definition of %q#D is marked %<dllimport%>", decl);
4226 DECL_DLLIMPORT_P (decl) = 0;
4229 /* If #pragma weak was used, mark the decl weak now. */
4230 maybe_apply_pragma_weak (decl);
4232 if (TREE_CODE (decl) == FUNCTION_DECL
4233 && DECL_DECLARED_INLINE_P (decl)
4234 && DECL_UNINLINABLE (decl)
4235 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4236 warning (0, "inline function %q+D given attribute noinline", decl);
4238 if (context && COMPLETE_TYPE_P (complete_type (context)))
4240 if (TREE_CODE (decl) == VAR_DECL)
4242 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
4243 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
4244 error ("%q#D is not a static member of %q#T", decl, context);
4245 else
4247 if (DECL_CONTEXT (field) != context)
4249 if (!same_type_p (DECL_CONTEXT (field), context))
4250 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
4251 "to be defined as %<%T::%D%>",
4252 DECL_CONTEXT (field), DECL_NAME (decl),
4253 context, DECL_NAME (decl));
4254 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
4256 if (processing_specialization
4257 && template_class_depth (context) == 0
4258 && CLASSTYPE_TEMPLATE_SPECIALIZATION (context))
4259 error ("template header not allowed in member definition "
4260 "of explicitly specialized class");
4261 /* Static data member are tricky; an in-class initialization
4262 still doesn't provide a definition, so the in-class
4263 declaration will have DECL_EXTERNAL set, but will have an
4264 initialization. Thus, duplicate_decls won't warn
4265 about this situation, and so we check here. */
4266 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
4267 error ("duplicate initialization of %qD", decl);
4268 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
4269 decl = field;
4270 if (declspecs->specs[(int) ds_constexpr]
4271 && !DECL_DECLARED_CONSTEXPR_P (field))
4272 error ("%qD declared %<constexpr%> outside its class", field);
4275 else
4277 tree field = check_classfn (context, decl,
4278 (processing_template_decl
4279 > template_class_depth (context))
4280 ? current_template_parms
4281 : NULL_TREE);
4282 if (field && field != error_mark_node
4283 && duplicate_decls (decl, field,
4284 /*newdecl_is_friend=*/false))
4285 decl = field;
4288 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
4289 DECL_IN_AGGR_P (decl) = 0;
4290 /* Do not mark DECL as an explicit specialization if it was not
4291 already marked as an instantiation; a declaration should
4292 never be marked as a specialization unless we know what
4293 template is being specialized. */
4294 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4296 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
4298 /* [temp.expl.spec] An explicit specialization of a static data
4299 member of a template is a definition if the declaration
4300 includes an initializer; otherwise, it is a declaration.
4302 We check for processing_specialization so this only applies
4303 to the new specialization syntax. */
4304 if (!initialized && processing_specialization)
4305 DECL_EXTERNAL (decl) = 1;
4308 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
4309 /* Aliases are definitions. */
4310 && !alias)
4311 permerror (input_location, "declaration of %q#D outside of class is not definition",
4312 decl);
4314 if (!ensure_literal_type_for_constexpr_object (decl))
4315 return error_mark_node;
4318 was_public = TREE_PUBLIC (decl);
4320 /* Enter this declaration into the symbol table. */
4321 decl = maybe_push_decl (decl);
4323 if (processing_template_decl)
4324 decl = push_template_decl (decl);
4325 if (decl == error_mark_node)
4326 return error_mark_node;
4328 /* Tell the back end to use or not use .common as appropriate. If we say
4329 -fconserve-space, we want this to save .data space, at the expense of
4330 wrong semantics. If we say -fno-conserve-space, we want this to
4331 produce errors about redefs; to do this we force variables into the
4332 data segment. */
4333 if (flag_conserve_space
4334 && TREE_CODE (decl) == VAR_DECL
4335 && TREE_PUBLIC (decl)
4336 && !DECL_THREAD_LOCAL_P (decl)
4337 && !have_global_bss_p ())
4338 DECL_COMMON (decl) = 1;
4340 if (TREE_CODE (decl) == VAR_DECL
4341 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
4342 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
4344 /* This is a const variable with implicit 'static'. Set
4345 DECL_THIS_STATIC so we can tell it from variables that are
4346 !TREE_PUBLIC because of the anonymous namespace. */
4347 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)));
4348 DECL_THIS_STATIC (decl) = 1;
4351 if (!processing_template_decl && TREE_CODE (decl) == VAR_DECL)
4352 start_decl_1 (decl, initialized);
4354 return decl;
4357 /* Process the declaration of a variable DECL. INITIALIZED is true
4358 iff DECL is explicitly initialized. (INITIALIZED is false if the
4359 variable is initialized via an implicitly-called constructor.)
4360 This function must be called for ordinary variables (including, for
4361 example, implicit instantiations of templates), but must not be
4362 called for template declarations. */
4364 void
4365 start_decl_1 (tree decl, bool initialized)
4367 tree type;
4368 bool complete_p;
4369 bool aggregate_definition_p;
4371 gcc_assert (!processing_template_decl);
4373 if (error_operand_p (decl))
4374 return;
4376 gcc_assert (TREE_CODE (decl) == VAR_DECL);
4378 type = TREE_TYPE (decl);
4379 complete_p = COMPLETE_TYPE_P (type);
4380 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
4382 /* If an explicit initializer is present, or if this is a definition
4383 of an aggregate, then we need a complete type at this point.
4384 (Scalars are always complete types, so there is nothing to
4385 check.) This code just sets COMPLETE_P; errors (if necessary)
4386 are issued below. */
4387 if ((initialized || aggregate_definition_p)
4388 && !complete_p
4389 && COMPLETE_TYPE_P (complete_type (type)))
4391 complete_p = true;
4392 /* We will not yet have set TREE_READONLY on DECL if the type
4393 was "const", but incomplete, before this point. But, now, we
4394 have a complete type, so we can try again. */
4395 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
4398 if (initialized)
4399 /* Is it valid for this decl to have an initializer at all? */
4401 /* Don't allow initializations for incomplete types except for
4402 arrays which might be completed by the initialization. */
4403 if (complete_p)
4404 ; /* A complete type is ok. */
4405 else if (type_uses_auto (type))
4406 ; /* An auto type is ok. */
4407 else if (TREE_CODE (type) != ARRAY_TYPE)
4409 error ("variable %q#D has initializer but incomplete type", decl);
4410 type = TREE_TYPE (decl) = error_mark_node;
4412 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
4414 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
4415 error ("elements of array %q#D have incomplete type", decl);
4416 /* else we already gave an error in start_decl. */
4419 else if (aggregate_definition_p && !complete_p)
4421 if (type_uses_auto (type))
4422 error ("declaration of %q#D has no initializer", decl);
4423 else
4424 error ("aggregate %q#D has incomplete type and cannot be defined",
4425 decl);
4426 /* Change the type so that assemble_variable will give
4427 DECL an rtl we can live with: (mem (const_int 0)). */
4428 type = TREE_TYPE (decl) = error_mark_node;
4431 /* Create a new scope to hold this declaration if necessary.
4432 Whether or not a new scope is necessary cannot be determined
4433 until after the type has been completed; if the type is a
4434 specialization of a class template it is not until after
4435 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4436 will be set correctly. */
4437 maybe_push_cleanup_level (type);
4440 /* Handle initialization of references. DECL, TYPE, and INIT have the
4441 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
4442 but will be set to a new CLEANUP_STMT if a temporary is created
4443 that must be destroyed subsequently.
4445 Returns an initializer expression to use to initialize DECL, or
4446 NULL if the initialization can be performed statically.
4448 Quotes on semantics can be found in ARM 8.4.3. */
4450 static tree
4451 grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
4453 tree tmp;
4455 if (init == NULL_TREE)
4457 if ((DECL_LANG_SPECIFIC (decl) == 0
4458 || DECL_IN_AGGR_P (decl) == 0)
4459 && ! DECL_THIS_EXTERN (decl))
4460 error ("%qD declared as reference but not initialized", decl);
4461 return NULL_TREE;
4464 if (TREE_CODE (init) == TREE_LIST)
4465 init = build_x_compound_expr_from_list (init, ELK_INIT,
4466 tf_warning_or_error);
4468 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
4469 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
4470 /* Note: default conversion is only called in very special cases. */
4471 init = decay_conversion (init);
4473 /* Convert INIT to the reference type TYPE. This may involve the
4474 creation of a temporary, whose lifetime must be the same as that
4475 of the reference. If so, a DECL_EXPR for the temporary will be
4476 added just after the DECL_EXPR for DECL. That's why we don't set
4477 DECL_INITIAL for local references (instead assigning to them
4478 explicitly); we need to allow the temporary to be initialized
4479 first. */
4480 tmp = initialize_reference (type, init, decl, cleanup, tf_warning_or_error);
4482 if (tmp == error_mark_node)
4483 return NULL_TREE;
4484 else if (tmp == NULL_TREE)
4486 error ("cannot initialize %qT from %qT", type, TREE_TYPE (init));
4487 return NULL_TREE;
4490 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
4491 return tmp;
4493 DECL_INITIAL (decl) = tmp;
4495 return NULL_TREE;
4498 /* Subroutine of check_initializer. We're initializing a DECL of
4499 std::initializer_list<T> TYPE from a braced-init-list INIT, and need to
4500 extend the lifetime of the underlying array to match that of the decl,
4501 just like for reference initialization. CLEANUP is as for
4502 grok_reference_init. */
4504 static tree
4505 build_init_list_var_init (tree decl, tree type, tree init, tree *cleanup)
4507 tree aggr_init, array, arrtype;
4508 init = perform_implicit_conversion (type, init, tf_warning_or_error);
4509 if (error_operand_p (init))
4510 return error_mark_node;
4512 aggr_init = TARGET_EXPR_INITIAL (init);
4513 init = build2 (INIT_EXPR, type, decl, init);
4515 array = AGGR_INIT_EXPR_ARG (aggr_init, 1);
4516 arrtype = TREE_TYPE (array);
4517 STRIP_NOPS (array);
4518 gcc_assert (TREE_CODE (array) == ADDR_EXPR);
4519 array = TREE_OPERAND (array, 0);
4520 /* If the array is constant, finish_compound_literal already made it a
4521 static variable and we don't need to do anything here. */
4522 if (decl && TREE_CODE (array) == TARGET_EXPR)
4524 tree subinit;
4525 tree var = set_up_extended_ref_temp (decl, array, cleanup, &subinit);
4526 var = build_address (var);
4527 var = convert (arrtype, var);
4528 AGGR_INIT_EXPR_ARG (aggr_init, 1) = var;
4529 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
4531 return init;
4534 /* Designated initializers in arrays are not supported in GNU C++.
4535 The parser cannot detect this error since it does not know whether
4536 a given brace-enclosed initializer is for a class type or for an
4537 array. This function checks that CE does not use a designated
4538 initializer. If it does, an error is issued. Returns true if CE
4539 is valid, i.e., does not have a designated initializer. */
4541 static bool
4542 check_array_designated_initializer (const constructor_elt *ce)
4544 /* Designated initializers for array elements are not supported. */
4545 if (ce->index)
4547 /* The parser only allows identifiers as designated
4548 initializers. */
4549 if (ce->index == error_mark_node)
4550 error ("name used in a GNU-style designated "
4551 "initializer for an array");
4552 else
4554 gcc_assert (TREE_CODE (ce->index) == IDENTIFIER_NODE);
4555 error ("name %qD used in a GNU-style designated "
4556 "initializer for an array", ce->index);
4558 return false;
4561 return true;
4564 /* When parsing `int a[] = {1, 2};' we don't know the size of the
4565 array until we finish parsing the initializer. If that's the
4566 situation we're in, update DECL accordingly. */
4568 static void
4569 maybe_deduce_size_from_array_init (tree decl, tree init)
4571 tree type = TREE_TYPE (decl);
4573 if (TREE_CODE (type) == ARRAY_TYPE
4574 && TYPE_DOMAIN (type) == NULL_TREE
4575 && TREE_CODE (decl) != TYPE_DECL)
4577 /* do_default is really a C-ism to deal with tentative definitions.
4578 But let's leave it here to ease the eventual merge. */
4579 int do_default = !DECL_EXTERNAL (decl);
4580 tree initializer = init ? init : DECL_INITIAL (decl);
4581 int failure = 0;
4583 /* Check that there are no designated initializers in INIT, as
4584 those are not supported in GNU C++, and as the middle-end
4585 will crash if presented with a non-numeric designated
4586 initializer. */
4587 if (initializer && TREE_CODE (initializer) == CONSTRUCTOR)
4589 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initializer);
4590 constructor_elt *ce;
4591 HOST_WIDE_INT i;
4592 for (i = 0;
4593 VEC_iterate (constructor_elt, v, i, ce);
4594 ++i)
4595 if (!check_array_designated_initializer (ce))
4596 failure = 1;
4599 if (!failure)
4601 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
4602 do_default);
4603 if (failure == 1)
4605 error ("initializer fails to determine size of %qD", decl);
4606 TREE_TYPE (decl) = error_mark_node;
4608 else if (failure == 2)
4610 if (do_default)
4612 error ("array size missing in %qD", decl);
4613 TREE_TYPE (decl) = error_mark_node;
4615 /* If a `static' var's size isn't known, make it extern as
4616 well as static, so it does not get allocated. If it's not
4617 `static', then don't mark it extern; finish_incomplete_decl
4618 will give it a default size and it will get allocated. */
4619 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4620 DECL_EXTERNAL (decl) = 1;
4622 else if (failure == 3)
4624 error ("zero-size array %qD", decl);
4625 TREE_TYPE (decl) = error_mark_node;
4629 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
4631 relayout_decl (decl);
4635 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
4636 any appropriate error messages regarding the layout. */
4638 static void
4639 layout_var_decl (tree decl)
4641 tree type;
4643 type = TREE_TYPE (decl);
4644 if (type == error_mark_node)
4645 return;
4647 /* If we haven't already layed out this declaration, do so now.
4648 Note that we must not call complete type for an external object
4649 because it's type might involve templates that we are not
4650 supposed to instantiate yet. (And it's perfectly valid to say
4651 `extern X x' for some incomplete type `X'.) */
4652 if (!DECL_EXTERNAL (decl))
4653 complete_type (type);
4654 if (!DECL_SIZE (decl)
4655 && TREE_TYPE (decl) != error_mark_node
4656 && (COMPLETE_TYPE_P (type)
4657 || (TREE_CODE (type) == ARRAY_TYPE
4658 && !TYPE_DOMAIN (type)
4659 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
4660 layout_decl (decl, 0);
4662 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
4664 /* An automatic variable with an incomplete type: that is an error.
4665 Don't talk about array types here, since we took care of that
4666 message in grokdeclarator. */
4667 error ("storage size of %qD isn't known", decl);
4668 TREE_TYPE (decl) = error_mark_node;
4670 #if 0
4671 /* Keep this code around in case we later want to control debug info
4672 based on whether a type is "used". (jason 1999-11-11) */
4674 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
4675 /* Let debugger know it should output info for this type. */
4676 note_debug_info_needed (ttype);
4678 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
4679 note_debug_info_needed (DECL_CONTEXT (decl));
4680 #endif
4682 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4683 && DECL_SIZE (decl) != NULL_TREE
4684 && ! TREE_CONSTANT (DECL_SIZE (decl)))
4686 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4687 constant_expression_warning (DECL_SIZE (decl));
4688 else
4690 error ("storage size of %qD isn't constant", decl);
4691 TREE_TYPE (decl) = error_mark_node;
4696 /* If a local static variable is declared in an inline function, or if
4697 we have a weak definition, we must endeavor to create only one
4698 instance of the variable at link-time. */
4700 void
4701 maybe_commonize_var (tree decl)
4703 /* Static data in a function with comdat linkage also has comdat
4704 linkage. */
4705 if (TREE_STATIC (decl)
4706 /* Don't mess with __FUNCTION__. */
4707 && ! DECL_ARTIFICIAL (decl)
4708 && DECL_FUNCTION_SCOPE_P (decl)
4709 && vague_linkage_p (DECL_CONTEXT (decl)))
4711 if (flag_weak)
4713 /* With weak symbols, we simply make the variable COMDAT;
4714 that will cause copies in multiple translations units to
4715 be merged. */
4716 comdat_linkage (decl);
4718 else
4720 if (DECL_INITIAL (decl) == NULL_TREE
4721 || DECL_INITIAL (decl) == error_mark_node)
4723 /* Without weak symbols, we can use COMMON to merge
4724 uninitialized variables. */
4725 TREE_PUBLIC (decl) = 1;
4726 DECL_COMMON (decl) = 1;
4728 else
4730 /* While for initialized variables, we must use internal
4731 linkage -- which means that multiple copies will not
4732 be merged. */
4733 TREE_PUBLIC (decl) = 0;
4734 DECL_COMMON (decl) = 0;
4735 warning_at (input_location, 0,
4736 "sorry: semantics of inline function static "
4737 "data %q+#D are wrong (you'll wind up "
4738 "with multiple copies)", decl);
4739 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4740 " you can work around this by removing "
4741 "the initializer");
4745 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
4746 /* Set it up again; we might have set DECL_INITIAL since the last
4747 time. */
4748 comdat_linkage (decl);
4751 /* Issue an error message if DECL is an uninitialized const variable. */
4753 static void
4754 check_for_uninitialized_const_var (tree decl)
4756 tree type = strip_array_types (TREE_TYPE (decl));
4758 if (TREE_CODE (decl) == VAR_DECL && DECL_DECLARED_CONSTEXPR_P (decl)
4759 && DECL_INITIAL (decl) == NULL)
4760 error ("missing initializer for constexpr %qD", decl);
4762 /* ``Unless explicitly declared extern, a const object does not have
4763 external linkage and must be initialized. ($8.4; $12.1)'' ARM
4764 7.1.6 */
4765 else if (TREE_CODE (decl) == VAR_DECL
4766 && TREE_CODE (type) != REFERENCE_TYPE
4767 && CP_TYPE_CONST_P (type)
4768 && (!TYPE_NEEDS_CONSTRUCTING (type)
4769 || !type_has_user_provided_default_constructor (type))
4770 && !DECL_INITIAL (decl))
4772 permerror (DECL_SOURCE_LOCATION (decl),
4773 "uninitialized const %qD", decl);
4775 if (CLASS_TYPE_P (type)
4776 && !type_has_user_provided_default_constructor (type))
4778 tree defaulted_ctor;
4780 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
4781 "%q#T has no user-provided default constructor", type);
4782 defaulted_ctor = in_class_defaulted_default_constructor (type);
4783 if (defaulted_ctor)
4784 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
4785 "constructor is not user-provided because it is "
4786 "explicitly defaulted in the class body");
4791 /* Structure holding the current initializer being processed by reshape_init.
4792 CUR is a pointer to the current element being processed, END is a pointer
4793 after the last element present in the initializer. */
4794 typedef struct reshape_iterator_t
4796 constructor_elt *cur;
4797 constructor_elt *end;
4798 } reshape_iter;
4800 static tree reshape_init_r (tree, reshape_iter *, bool);
4802 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
4803 returned is the next FIELD_DECL (possibly FIELD itself) that can be
4804 initialized. If there are no more such fields, the return value
4805 will be NULL. */
4807 tree
4808 next_initializable_field (tree field)
4810 while (field
4811 && (TREE_CODE (field) != FIELD_DECL
4812 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
4813 || DECL_ARTIFICIAL (field)))
4814 field = DECL_CHAIN (field);
4816 return field;
4819 /* Subroutine of reshape_init_array and reshape_init_vector, which does
4820 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
4821 INTEGER_CST representing the size of the array minus one (the maximum index),
4822 or NULL_TREE if the array was declared without specifying the size. D is
4823 the iterator within the constructor. */
4825 static tree
4826 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d)
4828 tree new_init;
4829 bool sized_array_p = (max_index != NULL_TREE);
4830 unsigned HOST_WIDE_INT max_index_cst = 0;
4831 unsigned HOST_WIDE_INT index;
4833 /* The initializer for an array is always a CONSTRUCTOR. */
4834 new_init = build_constructor (init_list_type_node, NULL);
4836 if (sized_array_p)
4838 /* Minus 1 is used for zero sized arrays. */
4839 if (integer_all_onesp (max_index))
4840 return new_init;
4842 if (host_integerp (max_index, 1))
4843 max_index_cst = tree_low_cst (max_index, 1);
4844 /* sizetype is sign extended, not zero extended. */
4845 else
4846 max_index_cst = tree_low_cst (fold_convert (size_type_node, max_index),
4850 /* Loop until there are no more initializers. */
4851 for (index = 0;
4852 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
4853 ++index)
4855 tree elt_init;
4857 check_array_designated_initializer (d->cur);
4858 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false);
4859 if (elt_init == error_mark_node)
4860 return error_mark_node;
4861 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), NULL_TREE, elt_init);
4864 return new_init;
4867 /* Subroutine of reshape_init_r, processes the initializers for arrays.
4868 Parameters are the same of reshape_init_r. */
4870 static tree
4871 reshape_init_array (tree type, reshape_iter *d)
4873 tree max_index = NULL_TREE;
4875 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
4877 if (TYPE_DOMAIN (type))
4878 max_index = array_type_nelts (type);
4880 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4883 /* Subroutine of reshape_init_r, processes the initializers for vectors.
4884 Parameters are the same of reshape_init_r. */
4886 static tree
4887 reshape_init_vector (tree type, reshape_iter *d)
4889 tree max_index = NULL_TREE;
4890 tree rtype;
4892 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
4894 if (COMPOUND_LITERAL_P (d->cur->value))
4896 tree value = d->cur->value;
4897 if (!same_type_p (TREE_TYPE (value), type))
4899 error ("invalid type %qT as initializer for a vector of type %qT",
4900 TREE_TYPE (d->cur->value), type);
4901 value = error_mark_node;
4903 ++d->cur;
4904 return value;
4907 /* For a vector, the representation type is a struct
4908 containing a single member which is an array of the
4909 appropriate size. */
4910 rtype = TYPE_DEBUG_REPRESENTATION_TYPE (type);
4911 if (rtype && TYPE_DOMAIN (TREE_TYPE (TYPE_FIELDS (rtype))))
4912 max_index = array_type_nelts (TREE_TYPE (TYPE_FIELDS (rtype)));
4914 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4917 /* Subroutine of reshape_init_r, processes the initializers for classes
4918 or union. Parameters are the same of reshape_init_r. */
4920 static tree
4921 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p)
4923 tree field;
4924 tree new_init;
4926 gcc_assert (CLASS_TYPE_P (type));
4928 /* The initializer for a class is always a CONSTRUCTOR. */
4929 new_init = build_constructor (init_list_type_node, NULL);
4930 field = next_initializable_field (TYPE_FIELDS (type));
4932 if (!field)
4934 /* [dcl.init.aggr]
4936 An initializer for an aggregate member that is an
4937 empty class shall have the form of an empty
4938 initializer-list {}. */
4939 if (!first_initializer_p)
4941 error ("initializer for %qT must be brace-enclosed", type);
4942 return error_mark_node;
4944 return new_init;
4947 /* Loop through the initializable fields, gathering initializers. */
4948 while (d->cur != d->end)
4950 tree field_init;
4952 /* Handle designated initializers, as an extension. */
4953 if (d->cur->index)
4955 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
4957 if (!field || TREE_CODE (field) != FIELD_DECL)
4959 error ("%qT has no non-static data member named %qD", type,
4960 d->cur->index);
4961 return error_mark_node;
4965 /* If we processed all the member of the class, we are done. */
4966 if (!field)
4967 break;
4969 field_init = reshape_init_r (TREE_TYPE (field), d,
4970 /*first_initializer_p=*/false);
4971 if (field_init == error_mark_node)
4972 return error_mark_node;
4974 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
4976 /* [dcl.init.aggr]
4978 When a union is initialized with a brace-enclosed
4979 initializer, the braces shall only contain an
4980 initializer for the first member of the union. */
4981 if (TREE_CODE (type) == UNION_TYPE)
4982 break;
4984 field = next_initializable_field (DECL_CHAIN (field));
4987 return new_init;
4990 /* Subroutine of reshape_init, which processes a single initializer (part of
4991 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
4992 iterator within the CONSTRUCTOR which points to the initializer to process.
4993 FIRST_INITIALIZER_P is true if this is the first initializer of the
4994 outermost CONSTRUCTOR node. */
4996 static tree
4997 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p)
4999 tree init = d->cur->value;
5001 if (error_operand_p (init))
5002 return error_mark_node;
5004 /* A non-aggregate type is always initialized with a single
5005 initializer. */
5006 if (!CP_AGGREGATE_TYPE_P (type))
5008 /* It is invalid to initialize a non-aggregate type with a
5009 brace-enclosed initializer before C++0x.
5010 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5011 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5012 a CONSTRUCTOR (with a record type). */
5013 if (TREE_CODE (init) == CONSTRUCTOR
5014 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5016 if (SCALAR_TYPE_P (type))
5018 error ("braces around scalar initializer for type %qT", type);
5019 init = error_mark_node;
5021 else
5022 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5025 d->cur++;
5026 return init;
5029 /* [dcl.init.aggr]
5031 All implicit type conversions (clause _conv_) are considered when
5032 initializing the aggregate member with an initializer from an
5033 initializer-list. If the initializer can initialize a member,
5034 the member is initialized. Otherwise, if the member is itself a
5035 non-empty subaggregate, brace elision is assumed and the
5036 initializer is considered for the initialization of the first
5037 member of the subaggregate. */
5038 if (TREE_CODE (init) != CONSTRUCTOR
5039 /* But don't try this for the first initializer, since that would be
5040 looking through the outermost braces; A a2 = { a1 }; is not a
5041 valid aggregate initialization. */
5042 && !first_initializer_p
5043 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5044 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL)))
5046 d->cur++;
5047 return init;
5050 /* [dcl.init.string]
5052 A char array (whether plain char, signed char, or unsigned char)
5053 can be initialized by a string-literal (optionally enclosed in
5054 braces); a wchar_t array can be initialized by a wide
5055 string-literal (optionally enclosed in braces). */
5056 if (TREE_CODE (type) == ARRAY_TYPE
5057 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5059 tree str_init = init;
5061 /* Strip one level of braces if and only if they enclose a single
5062 element (as allowed by [dcl.init.string]). */
5063 if (!first_initializer_p
5064 && TREE_CODE (str_init) == CONSTRUCTOR
5065 && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (str_init)) == 1)
5067 str_init = VEC_index (constructor_elt,
5068 CONSTRUCTOR_ELTS (str_init), 0)->value;
5071 /* If it's a string literal, then it's the initializer for the array
5072 as a whole. Otherwise, continue with normal initialization for
5073 array types (one value per array element). */
5074 if (TREE_CODE (str_init) == STRING_CST)
5076 d->cur++;
5077 return str_init;
5081 /* The following cases are about aggregates. If we are not within a full
5082 initializer already, and there is not a CONSTRUCTOR, it means that there
5083 is a missing set of braces (that is, we are processing the case for
5084 which reshape_init exists). */
5085 if (!first_initializer_p)
5087 if (TREE_CODE (init) == CONSTRUCTOR)
5089 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
5090 /* There is no need to reshape pointer-to-member function
5091 initializers, as they are always constructed correctly
5092 by the front end. */
5094 else if (COMPOUND_LITERAL_P (init))
5095 /* For a nested compound literal, there is no need to reshape since
5096 brace elision is not allowed. Even if we decided to allow it,
5097 we should add a call to reshape_init in finish_compound_literal,
5098 before calling digest_init, so changing this code would still
5099 not be necessary. */
5100 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
5101 else
5103 ++d->cur;
5104 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5105 return reshape_init (type, init);
5109 warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
5110 type);
5113 /* Dispatch to specialized routines. */
5114 if (CLASS_TYPE_P (type))
5115 return reshape_init_class (type, d, first_initializer_p);
5116 else if (TREE_CODE (type) == ARRAY_TYPE)
5117 return reshape_init_array (type, d);
5118 else if (TREE_CODE (type) == VECTOR_TYPE)
5119 return reshape_init_vector (type, d);
5120 else
5121 gcc_unreachable();
5124 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
5125 brace-enclosed aggregate initializer.
5127 INIT is the CONSTRUCTOR containing the list of initializers describing
5128 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
5129 It may not presently match the shape of the TYPE; for example:
5131 struct S { int a; int b; };
5132 struct S a[] = { 1, 2, 3, 4 };
5134 Here INIT will hold a VEC of four elements, rather than a
5135 VEC of two elements, each itself a VEC of two elements. This
5136 routine transforms INIT from the former form into the latter. The
5137 revised CONSTRUCTOR node is returned. */
5139 tree
5140 reshape_init (tree type, tree init)
5142 VEC(constructor_elt, gc) *v;
5143 reshape_iter d;
5144 tree new_init;
5146 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5148 v = CONSTRUCTOR_ELTS (init);
5150 /* An empty constructor does not need reshaping, and it is always a valid
5151 initializer. */
5152 if (VEC_empty (constructor_elt, v))
5153 return init;
5155 /* Recurse on this CONSTRUCTOR. */
5156 d.cur = VEC_index (constructor_elt, v, 0);
5157 d.end = d.cur + VEC_length (constructor_elt, v);
5159 new_init = reshape_init_r (type, &d, true);
5160 if (new_init == error_mark_node)
5161 return error_mark_node;
5163 /* Make sure all the element of the constructor were used. Otherwise,
5164 issue an error about exceeding initializers. */
5165 if (d.cur != d.end)
5166 error ("too many initializers for %qT", type);
5168 return new_init;
5171 /* Verify array initializer. Returns true if errors have been reported. */
5173 bool
5174 check_array_initializer (tree decl, tree type, tree init)
5176 tree element_type = TREE_TYPE (type);
5178 /* The array type itself need not be complete, because the
5179 initializer may tell us how many elements are in the array.
5180 But, the elements of the array must be complete. */
5181 if (!COMPLETE_TYPE_P (complete_type (element_type)))
5183 if (decl)
5184 error ("elements of array %q#D have incomplete type", decl);
5185 else
5186 error ("elements of array %q#T have incomplete type", type);
5187 return true;
5189 /* It is not valid to initialize a VLA. */
5190 if (init
5191 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
5192 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
5194 if (decl)
5195 error ("variable-sized object %qD may not be initialized", decl);
5196 else
5197 error ("variable-sized compound literal");
5198 return true;
5200 return false;
5203 /* Subroutine of check_initializer; args are passed down from that function.
5204 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
5206 static tree
5207 build_aggr_init_full_exprs (tree decl, tree init, int flags)
5210 int saved_stmts_are_full_exprs_p = 0;
5211 if (building_stmt_tree ())
5213 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
5214 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
5216 init = build_aggr_init (decl, init, flags, tf_warning_or_error);
5217 if (building_stmt_tree ())
5218 current_stmt_tree ()->stmts_are_full_exprs_p =
5219 saved_stmts_are_full_exprs_p;
5220 return init;
5223 /* Verify INIT (the initializer for DECL), and record the
5224 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
5225 grok_reference_init.
5227 If the return value is non-NULL, it is an expression that must be
5228 evaluated dynamically to initialize DECL. */
5230 static tree
5231 check_initializer (tree decl, tree init, int flags, tree *cleanup)
5233 tree type = TREE_TYPE (decl);
5234 tree init_code = NULL;
5235 tree core_type;
5237 /* Things that are going to be initialized need to have complete
5238 type. */
5239 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
5241 if (type == error_mark_node)
5242 /* We will have already complained. */
5243 return NULL_TREE;
5245 if (TREE_CODE (type) == ARRAY_TYPE)
5247 if (check_array_initializer (decl, type, init))
5248 return NULL_TREE;
5250 else if (!COMPLETE_TYPE_P (type))
5252 error ("%qD has incomplete type", decl);
5253 TREE_TYPE (decl) = error_mark_node;
5254 return NULL_TREE;
5256 else
5257 /* There is no way to make a variable-sized class type in GNU C++. */
5258 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
5260 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
5262 int init_len = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init));
5263 if (SCALAR_TYPE_P (type))
5265 if (init_len == 0)
5267 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5268 init = build_zero_init (type, NULL_TREE, false);
5270 else if (init_len != 1)
5272 error ("scalar object %qD requires one element in initializer",
5273 decl);
5274 TREE_TYPE (decl) = error_mark_node;
5275 return NULL_TREE;
5280 if (TREE_CODE (decl) == CONST_DECL)
5282 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
5284 DECL_INITIAL (decl) = init;
5286 gcc_assert (init != NULL_TREE);
5287 init = NULL_TREE;
5289 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
5290 init = grok_reference_init (decl, type, init, cleanup);
5291 else if (init)
5293 /* Do not reshape constructors of vectors (they don't need to be
5294 reshaped. */
5295 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5297 if (is_std_init_list (type))
5298 return build_init_list_var_init (decl, type, init, cleanup);
5299 else if (TYPE_NON_AGGREGATE_CLASS (type))
5301 /* Don't reshape if the class has constructors. */
5302 if (cxx_dialect == cxx98)
5303 error ("in C++98 %qD must be initialized by constructor, "
5304 "not by %<{...}%>",
5305 decl);
5307 else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_VECTOR_OPAQUE (type))
5309 error ("opaque vector types cannot be initialized");
5310 init = error_mark_node;
5312 else
5313 init = reshape_init (type, init);
5316 /* If DECL has an array type without a specific bound, deduce the
5317 array size from the initializer. */
5318 maybe_deduce_size_from_array_init (decl, init);
5319 type = TREE_TYPE (decl);
5320 if (type == error_mark_node)
5321 return NULL_TREE;
5323 if (TYPE_NEEDS_CONSTRUCTING (type)
5324 || (CLASS_TYPE_P (type)
5325 && !BRACE_ENCLOSED_INITIALIZER_P (init)))
5326 return build_aggr_init_full_exprs (decl, init, flags);
5327 else if (TREE_CODE (init) != TREE_VEC)
5329 init_code = store_init_value (decl, init, flags);
5330 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
5331 && DECL_INITIAL (decl)
5332 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
5333 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
5334 warning (0, "array %qD initialized by parenthesized string literal %qE",
5335 decl, DECL_INITIAL (decl));
5336 init = NULL;
5339 else if (DECL_EXTERNAL (decl))
5341 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
5343 check_for_uninitialized_const_var (decl);
5344 return build_aggr_init_full_exprs (decl, init, flags);
5346 else if (MAYBE_CLASS_TYPE_P (core_type = strip_array_types (type)))
5348 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
5349 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
5350 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
5351 /*complain=*/true);
5353 check_for_uninitialized_const_var (decl);
5355 else
5356 check_for_uninitialized_const_var (decl);
5358 if (init && init != error_mark_node)
5359 init_code = build2 (INIT_EXPR, type, decl, init);
5361 return init_code;
5364 /* If DECL is not a local variable, give it RTL. */
5366 static void
5367 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
5369 int toplev = toplevel_bindings_p ();
5370 int defer_p;
5371 const char *filename;
5373 /* Set the DECL_ASSEMBLER_NAME for the object. */
5374 if (asmspec)
5376 /* The `register' keyword, when used together with an
5377 asm-specification, indicates that the variable should be
5378 placed in a particular register. */
5379 if (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
5381 set_user_assembler_name (decl, asmspec);
5382 DECL_HARD_REGISTER (decl) = 1;
5384 else
5386 if (TREE_CODE (decl) == FUNCTION_DECL
5387 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5388 set_builtin_user_assembler_name (decl, asmspec);
5389 set_user_assembler_name (decl, asmspec);
5393 /* Handle non-variables up front. */
5394 if (TREE_CODE (decl) != VAR_DECL)
5396 rest_of_decl_compilation (decl, toplev, at_eof);
5397 return;
5400 /* If we see a class member here, it should be a static data
5401 member. */
5402 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
5404 gcc_assert (TREE_STATIC (decl));
5405 /* An in-class declaration of a static data member should be
5406 external; it is only a declaration, and not a definition. */
5407 if (init == NULL_TREE)
5408 gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
5411 /* We don't create any RTL for local variables. */
5412 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
5413 return;
5415 /* We defer emission of local statics until the corresponding
5416 DECL_EXPR is expanded. */
5417 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
5419 /* We try to defer namespace-scope static constants so that they are
5420 not emitted into the object file unnecessarily. */
5421 filename = input_filename;
5422 if (!DECL_VIRTUAL_P (decl)
5423 && TREE_READONLY (decl)
5424 && DECL_INITIAL (decl) != NULL_TREE
5425 && DECL_INITIAL (decl) != error_mark_node
5426 && filename != NULL
5427 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
5428 && toplev
5429 && !TREE_PUBLIC (decl))
5431 /* Fool with the linkage of static consts according to #pragma
5432 interface. */
5433 struct c_fileinfo *finfo = get_fileinfo (filename);
5434 if (!finfo->interface_unknown && !TREE_PUBLIC (decl))
5436 TREE_PUBLIC (decl) = 1;
5437 DECL_EXTERNAL (decl) = finfo->interface_only;
5440 defer_p = 1;
5442 /* Likewise for template instantiations. */
5443 else if (DECL_LANG_SPECIFIC (decl)
5444 && DECL_IMPLICIT_INSTANTIATION (decl))
5445 defer_p = 1;
5447 /* If we're not deferring, go ahead and assemble the variable. */
5448 if (!defer_p)
5449 rest_of_decl_compilation (decl, toplev, at_eof);
5452 /* walk_tree helper for wrap_temporary_cleanups, below. */
5454 static tree
5455 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
5457 if (TYPE_P (*stmt_p))
5459 *walk_subtrees = 0;
5460 return NULL_TREE;
5463 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
5465 tree guard = (tree)data;
5466 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
5468 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
5469 /* Tell honor_protect_cleanup_actions to handle this as a separate
5470 cleanup. */
5471 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
5473 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
5476 return NULL_TREE;
5479 /* We're initializing a local variable which has a cleanup GUARD. If there
5480 are any temporaries used in the initializer INIT of this variable, we
5481 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
5482 variable will be cleaned up properly if one of them throws.
5484 Unfortunately, there's no way to express this properly in terms of
5485 nesting, as the regions for the temporaries overlap the region for the
5486 variable itself; if there are two temporaries, the variable needs to be
5487 the first thing destroyed if either of them throws. However, we only
5488 want to run the variable's cleanup if it actually got constructed. So
5489 we need to guard the temporary cleanups with the variable's cleanup if
5490 they are run on the normal path, but not if they are run on the
5491 exceptional path. We implement this by telling
5492 honor_protect_cleanup_actions to strip the variable cleanup from the
5493 exceptional path. */
5495 static void
5496 wrap_temporary_cleanups (tree init, tree guard)
5498 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
5501 /* Generate code to initialize DECL (a local variable). */
5503 static void
5504 initialize_local_var (tree decl, tree init)
5506 tree type = TREE_TYPE (decl);
5507 tree cleanup;
5508 int already_used;
5510 gcc_assert (TREE_CODE (decl) == VAR_DECL
5511 || TREE_CODE (decl) == RESULT_DECL);
5512 gcc_assert (!TREE_STATIC (decl));
5514 if (DECL_SIZE (decl) == NULL_TREE)
5516 /* If we used it already as memory, it must stay in memory. */
5517 DECL_INITIAL (decl) = NULL_TREE;
5518 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5519 return;
5522 if (type == error_mark_node)
5523 return;
5525 /* Compute and store the initial value. */
5526 already_used = TREE_USED (decl) || TREE_USED (type);
5527 if (TREE_USED (type))
5528 DECL_READ_P (decl) = 1;
5530 /* Generate a cleanup, if necessary. */
5531 cleanup = cxx_maybe_build_cleanup (decl);
5533 /* Perform the initialization. */
5534 if (init)
5536 int saved_stmts_are_full_exprs_p;
5538 /* If we're only initializing a single object, guard the destructors
5539 of any temporaries used in its initializer with its destructor.
5540 This isn't right for arrays because each element initialization is
5541 a full-expression. */
5542 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
5543 wrap_temporary_cleanups (init, cleanup);
5545 gcc_assert (building_stmt_tree ());
5546 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
5547 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
5548 finish_expr_stmt (init);
5549 current_stmt_tree ()->stmts_are_full_exprs_p =
5550 saved_stmts_are_full_exprs_p;
5553 /* Set this to 0 so we can tell whether an aggregate which was
5554 initialized was ever used. Don't do this if it has a
5555 destructor, so we don't complain about the 'resource
5556 allocation is initialization' idiom. Now set
5557 attribute((unused)) on types so decls of that type will be
5558 marked used. (see TREE_USED, above.) */
5559 if (TYPE_NEEDS_CONSTRUCTING (type)
5560 && ! already_used
5561 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
5562 && DECL_NAME (decl))
5563 TREE_USED (decl) = 0;
5564 else if (already_used)
5565 TREE_USED (decl) = 1;
5567 if (cleanup)
5568 finish_decl_cleanup (decl, cleanup);
5571 /* DECL is a VAR_DECL for a compiler-generated variable with static
5572 storage duration (like a virtual table) whose initializer is a
5573 compile-time constant. Initialize the variable and provide it to the
5574 back end. */
5576 void
5577 initialize_artificial_var (tree decl, VEC(constructor_elt,gc) *v)
5579 tree init;
5580 gcc_assert (DECL_ARTIFICIAL (decl));
5581 init = build_constructor (TREE_TYPE (decl), v);
5582 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
5583 DECL_INITIAL (decl) = init;
5584 DECL_INITIALIZED_P (decl) = 1;
5585 determine_visibility (decl);
5586 layout_var_decl (decl);
5587 maybe_commonize_var (decl);
5588 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
5591 /* INIT is the initializer for a variable, as represented by the
5592 parser. Returns true iff INIT is value-dependent. */
5594 static bool
5595 value_dependent_init_p (tree init)
5597 if (TREE_CODE (init) == TREE_LIST)
5598 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
5599 return any_value_dependent_elements_p (init);
5600 else if (TREE_CODE (init) == CONSTRUCTOR)
5601 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
5603 VEC(constructor_elt, gc) *elts;
5604 size_t nelts;
5605 size_t i;
5607 elts = CONSTRUCTOR_ELTS (init);
5608 nelts = VEC_length (constructor_elt, elts);
5609 for (i = 0; i < nelts; ++i)
5610 if (value_dependent_init_p (VEC_index (constructor_elt,
5611 elts, i)->value))
5612 return true;
5614 else
5615 /* It must be a simple expression, e.g., int i = 3; */
5616 return value_dependent_expression_p (init);
5618 return false;
5621 /* Finish processing of a declaration;
5622 install its line number and initial value.
5623 If the length of an array type is not known before,
5624 it must be determined now, from the initial value, or it is an error.
5626 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
5627 true, then INIT is an integral constant expression.
5629 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
5630 if the (init) syntax was used. */
5632 void
5633 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
5634 tree asmspec_tree, int flags)
5636 tree type;
5637 tree cleanup;
5638 const char *asmspec = NULL;
5639 int was_readonly = 0;
5640 bool var_definition_p = false;
5641 int saved_processing_template_decl;
5642 tree auto_node;
5644 if (decl == error_mark_node)
5645 return;
5646 else if (! decl)
5648 if (init)
5649 error ("assignment (not initialization) in declaration");
5650 return;
5653 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
5654 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
5655 gcc_assert (TREE_CODE (decl) != PARM_DECL);
5657 type = TREE_TYPE (decl);
5658 if (type == error_mark_node)
5659 return;
5661 /* Assume no cleanup is required. */
5662 cleanup = NULL_TREE;
5663 saved_processing_template_decl = processing_template_decl;
5665 /* If a name was specified, get the string. */
5666 if (global_scope_p (current_binding_level))
5667 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5668 if (asmspec_tree && asmspec_tree != error_mark_node)
5669 asmspec = TREE_STRING_POINTER (asmspec_tree);
5671 if (current_class_type
5672 && CP_DECL_CONTEXT (decl) == current_class_type
5673 && TYPE_BEING_DEFINED (current_class_type)
5674 && (DECL_INITIAL (decl) || init))
5675 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
5677 auto_node = type_uses_auto (type);
5678 if (auto_node)
5680 if (init == NULL_TREE)
5682 error ("declaration of %q#D has no initializer", decl);
5683 TREE_TYPE (decl) = error_mark_node;
5684 return;
5686 if (TREE_CODE (init) == TREE_LIST)
5687 init = build_x_compound_expr_from_list (init, ELK_INIT,
5688 tf_warning_or_error);
5689 if (describable_type (init))
5691 type = TREE_TYPE (decl) = do_auto_deduction (type, init, auto_node);
5692 if (type == error_mark_node)
5693 return;
5697 if (init && TREE_CODE (decl) == FUNCTION_DECL)
5699 tree clone;
5700 if (init == ridpointers[(int)RID_DELETE])
5702 /* FIXME check this is 1st decl. */
5703 DECL_DELETED_FN (decl) = 1;
5704 DECL_DECLARED_INLINE_P (decl) = 1;
5705 DECL_INITIAL (decl) = error_mark_node;
5706 FOR_EACH_CLONE (clone, decl)
5708 DECL_DELETED_FN (clone) = 1;
5709 DECL_DECLARED_INLINE_P (clone) = 1;
5710 DECL_INITIAL (clone) = error_mark_node;
5712 init = NULL_TREE;
5714 else if (init == ridpointers[(int)RID_DEFAULT])
5716 if (defaultable_fn_check (decl))
5717 DECL_DEFAULTED_FN (decl) = 1;
5718 else
5719 DECL_INITIAL (decl) = NULL_TREE;
5723 if (processing_template_decl)
5725 bool type_dependent_p;
5727 /* Add this declaration to the statement-tree. */
5728 if (at_function_scope_p ())
5729 add_decl_expr (decl);
5731 type_dependent_p = dependent_type_p (type);
5733 if (check_for_bare_parameter_packs (init))
5735 init = NULL_TREE;
5736 DECL_INITIAL (decl) = NULL_TREE;
5739 if (init && init_const_expr_p && TREE_CODE (decl) == VAR_DECL)
5741 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5742 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl))
5743 TREE_CONSTANT (decl) = 1;
5746 /* Generally, initializers in templates are expanded when the
5747 template is instantiated. But, if DECL is an integral
5748 constant static data member, then it can be used in future
5749 integral constant expressions, and its value must be
5750 available. */
5751 if (!(init
5752 && DECL_CLASS_SCOPE_P (decl)
5753 && DECL_INTEGRAL_CONSTANT_VAR_P (decl)
5754 && !type_dependent_p
5755 && !value_dependent_init_p (init)))
5757 if (init)
5758 DECL_INITIAL (decl) = init;
5759 if (TREE_CODE (decl) == VAR_DECL
5760 && !DECL_PRETTY_FUNCTION_P (decl)
5761 && !type_dependent_p)
5762 maybe_deduce_size_from_array_init (decl, init);
5763 goto finish_end;
5766 if (TREE_CODE (init) == TREE_LIST)
5768 /* If the parenthesized-initializer form was used (e.g.,
5769 "int A<N>::i(X)"), then INIT will be a TREE_LIST of initializer
5770 arguments. (There is generally only one.) We convert them
5771 individually. */
5772 tree list = init;
5773 for (; list; list = TREE_CHAIN (list))
5775 tree elt = TREE_VALUE (list);
5776 TREE_VALUE (list) = fold_non_dependent_expr (elt);
5779 else
5780 init = fold_non_dependent_expr (init);
5781 processing_template_decl = 0;
5784 /* Take care of TYPE_DECLs up front. */
5785 if (TREE_CODE (decl) == TYPE_DECL)
5787 if (type != error_mark_node
5788 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
5790 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
5791 warning (0, "shadowing previous type declaration of %q#D", decl);
5792 set_identifier_type_value (DECL_NAME (decl), decl);
5795 /* If we have installed this as the canonical typedef for this
5796 type, and that type has not been defined yet, delay emitting
5797 the debug information for it, as we will emit it later. */
5798 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
5799 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
5800 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5802 rest_of_decl_compilation (decl, DECL_CONTEXT (decl) == NULL_TREE,
5803 at_eof);
5804 goto finish_end;
5807 /* A reference will be modified here, as it is initialized. */
5808 if (! DECL_EXTERNAL (decl)
5809 && TREE_READONLY (decl)
5810 && TREE_CODE (type) == REFERENCE_TYPE)
5812 was_readonly = 1;
5813 TREE_READONLY (decl) = 0;
5816 if (TREE_CODE (decl) == VAR_DECL)
5818 /* Only variables with trivial initialization and destruction can
5819 have thread-local storage. */
5820 if (DECL_THREAD_LOCAL_P (decl)
5821 && (type_has_nontrivial_default_init (TREE_TYPE (decl))
5822 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
5823 error ("%qD cannot be thread-local because it has non-trivial "
5824 "type %qT", decl, TREE_TYPE (decl));
5825 /* If this is a local variable that will need a mangled name,
5826 register it now. We must do this before processing the
5827 initializer for the variable, since the initialization might
5828 require a guard variable, and since the mangled name of the
5829 guard variable will depend on the mangled name of this
5830 variable. */
5831 if (DECL_FUNCTION_SCOPE_P (decl)
5832 && TREE_STATIC (decl)
5833 && !DECL_ARTIFICIAL (decl))
5835 push_local_name (decl);
5836 if (DECL_CONSTRUCTOR_P (current_function_decl)
5837 || DECL_DESTRUCTOR_P (current_function_decl))
5838 /* Normally local_decls is populated during GIMPLE lowering,
5839 but [cd]tors are never actually compiled directly. We need
5840 to put statics on the list so we can deal with the label
5841 address extension. */
5842 add_local_decl (cfun, decl);
5845 /* Convert the initializer to the type of DECL, if we have not
5846 already initialized DECL. */
5847 if (!DECL_INITIALIZED_P (decl)
5848 /* If !DECL_EXTERNAL then DECL is being defined. In the
5849 case of a static data member initialized inside the
5850 class-specifier, there can be an initializer even if DECL
5851 is *not* defined. */
5852 && (!DECL_EXTERNAL (decl) || init))
5854 if (TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
5856 tree jclass
5857 = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
5858 /* Allow libjava/prims.cc define primitive classes. */
5859 if (init != NULL_TREE
5860 || jclass == NULL_TREE
5861 || TREE_CODE (jclass) != TYPE_DECL
5862 || !POINTER_TYPE_P (TREE_TYPE (jclass))
5863 || !same_type_ignoring_top_level_qualifiers_p
5864 (type, TREE_TYPE (TREE_TYPE (jclass))))
5865 error ("Java object %qD not allocated with %<new%>", decl);
5866 init = NULL_TREE;
5868 if (init)
5870 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
5871 if (init_const_expr_p)
5873 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5874 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl))
5875 TREE_CONSTANT (decl) = 1;
5878 init = check_initializer (decl, init, flags, &cleanup);
5879 /* Thread-local storage cannot be dynamically initialized. */
5880 if (DECL_THREAD_LOCAL_P (decl) && init)
5882 error ("%qD is thread-local and so cannot be dynamically "
5883 "initialized", decl);
5884 init = NULL_TREE;
5887 /* Check that the initializer for a static data member was a
5888 constant. Although we check in the parser that the
5889 initializer is an integral constant expression, we do not
5890 simplify division-by-zero at the point at which it
5891 occurs. Therefore, in:
5893 struct S { static const int i = 7 / 0; };
5895 we issue an error at this point. It would
5896 probably be better to forbid division by zero in
5897 integral constant expressions. */
5898 if (DECL_EXTERNAL (decl) && init)
5900 error ("%qD cannot be initialized by a non-constant expression"
5901 " when being declared", decl);
5902 DECL_INITIALIZED_IN_CLASS_P (decl) = 0;
5903 init = NULL_TREE;
5906 /* Handle:
5908 [dcl.init]
5910 The memory occupied by any object of static storage
5911 duration is zero-initialized at program startup before
5912 any other initialization takes place.
5914 We cannot create an appropriate initializer until after
5915 the type of DECL is finalized. If DECL_INITIAL is set,
5916 then the DECL is statically initialized, and any
5917 necessary zero-initialization has already been performed. */
5918 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
5919 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
5920 /*nelts=*/NULL_TREE,
5921 /*static_storage_p=*/true);
5922 /* Remember that the initialization for this variable has
5923 taken place. */
5924 DECL_INITIALIZED_P (decl) = 1;
5925 /* This declaration is the definition of this variable,
5926 unless we are initializing a static data member within
5927 the class specifier. */
5928 if (!DECL_EXTERNAL (decl))
5929 var_definition_p = true;
5931 /* If the variable has an array type, lay out the type, even if
5932 there is no initializer. It is valid to index through the
5933 array, and we must get TYPE_ALIGN set correctly on the array
5934 type. */
5935 else if (TREE_CODE (type) == ARRAY_TYPE)
5936 layout_type (type);
5938 if (!processing_template_decl
5939 && TREE_STATIC (decl)
5940 && !at_function_scope_p ()
5941 && current_function_decl == NULL)
5942 /* So decl is a global variable or a static member of a
5943 non local class. Record the types it uses
5944 so that we can decide later to emit debug info for them. */
5945 record_types_used_by_current_var_decl (decl);
5947 else if (TREE_CODE (decl) == FIELD_DECL
5948 && TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
5949 error ("non-static data member %qD has Java class type", decl);
5951 /* Add this declaration to the statement-tree. This needs to happen
5952 after the call to check_initializer so that the DECL_EXPR for a
5953 reference temp is added before the DECL_EXPR for the reference itself. */
5954 if (at_function_scope_p ())
5955 add_decl_expr (decl);
5957 /* Let the middle end know about variables and functions -- but not
5958 static data members in uninstantiated class templates. */
5959 if (!saved_processing_template_decl
5960 && (TREE_CODE (decl) == VAR_DECL
5961 || TREE_CODE (decl) == FUNCTION_DECL))
5963 if (TREE_CODE (decl) == VAR_DECL)
5965 layout_var_decl (decl);
5966 maybe_commonize_var (decl);
5969 /* This needs to happen after the linkage is set. */
5970 determine_visibility (decl);
5972 if (var_definition_p && TREE_STATIC (decl))
5974 /* If a TREE_READONLY variable needs initialization
5975 at runtime, it is no longer readonly and we need to
5976 avoid MEM_READONLY_P being set on RTL created for it. */
5977 if (init)
5979 if (TREE_READONLY (decl))
5980 TREE_READONLY (decl) = 0;
5981 was_readonly = 0;
5983 else if (was_readonly)
5984 TREE_READONLY (decl) = 1;
5987 make_rtl_for_nonlocal_decl (decl, init, asmspec);
5989 /* Check for abstractness of the type. Notice that there is no
5990 need to strip array types here since the check for those types
5991 is already done within create_array_type_for_decl. */
5992 if (TREE_CODE (type) == FUNCTION_TYPE
5993 || TREE_CODE (type) == METHOD_TYPE)
5994 abstract_virtuals_error (decl, TREE_TYPE (type));
5995 else
5996 abstract_virtuals_error (decl, type);
5998 if (TREE_TYPE (decl) == error_mark_node)
5999 /* No initialization required. */
6001 else if (TREE_CODE (decl) == FUNCTION_DECL)
6003 if (init)
6005 if (init == ridpointers[(int)RID_DEFAULT])
6007 /* An out-of-class default definition is defined at
6008 the point where it is explicitly defaulted. */
6009 if (DECL_INITIAL (decl) == error_mark_node)
6010 synthesize_method (decl);
6012 else
6013 error ("function %q#D is initialized like a variable", decl);
6015 /* else no initialization required. */
6017 else if (DECL_EXTERNAL (decl)
6018 && ! (DECL_LANG_SPECIFIC (decl)
6019 && DECL_NOT_REALLY_EXTERN (decl)))
6021 if (init)
6022 DECL_INITIAL (decl) = init;
6024 /* A variable definition. */
6025 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6026 /* Initialize the local variable. */
6027 initialize_local_var (decl, init);
6029 /* If a variable is defined, and then a subsequent
6030 definition with external linkage is encountered, we will
6031 get here twice for the same variable. We want to avoid
6032 calling expand_static_init more than once. For variables
6033 that are not static data members, we can call
6034 expand_static_init only when we actually process the
6035 initializer. It is not legal to redeclare a static data
6036 member, so this issue does not arise in that case. */
6037 else if (var_definition_p && TREE_STATIC (decl))
6038 expand_static_init (decl, init);
6041 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
6042 reference, insert it in the statement-tree now. */
6043 if (cleanup)
6044 push_cleanup (decl, cleanup, false);
6046 finish_end:
6047 processing_template_decl = saved_processing_template_decl;
6049 if (was_readonly)
6050 TREE_READONLY (decl) = 1;
6053 /* Returns a declaration for a VAR_DECL as if:
6055 extern "C" TYPE NAME;
6057 had been seen. Used to create compiler-generated global
6058 variables. */
6060 static tree
6061 declare_global_var (tree name, tree type)
6063 tree decl;
6065 push_to_top_level ();
6066 decl = build_decl (input_location, VAR_DECL, name, type);
6067 TREE_PUBLIC (decl) = 1;
6068 DECL_EXTERNAL (decl) = 1;
6069 DECL_ARTIFICIAL (decl) = 1;
6070 /* If the user has explicitly declared this variable (perhaps
6071 because the code we are compiling is part of a low-level runtime
6072 library), then it is possible that our declaration will be merged
6073 with theirs by pushdecl. */
6074 decl = pushdecl (decl);
6075 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
6076 pop_from_top_level ();
6078 return decl;
6081 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
6082 if "__cxa_atexit" is not being used) corresponding to the function
6083 to be called when the program exits. */
6085 static tree
6086 get_atexit_fn_ptr_type (void)
6088 tree fn_type;
6090 if (!atexit_fn_ptr_type_node)
6092 tree arg_type;
6093 if (flag_use_cxa_atexit
6094 && !targetm.cxx.use_atexit_for_cxa_atexit ())
6095 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
6096 arg_type = ptr_type_node;
6097 else
6098 /* The parameter to "atexit" is "void (*)(void)". */
6099 arg_type = NULL_TREE;
6101 fn_type = build_function_type_list (void_type_node,
6102 arg_type, NULL_TREE);
6103 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
6106 return atexit_fn_ptr_type_node;
6109 /* Returns a pointer to the `atexit' function. Note that if
6110 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
6111 `__cxa_atexit' function specified in the IA64 C++ ABI. */
6113 static tree
6114 get_atexit_node (void)
6116 tree atexit_fndecl;
6117 tree fn_type;
6118 tree fn_ptr_type;
6119 const char *name;
6120 bool use_aeabi_atexit;
6122 if (atexit_node)
6123 return atexit_node;
6125 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
6127 /* The declaration for `__cxa_atexit' is:
6129 int __cxa_atexit (void (*)(void *), void *, void *)
6131 We build up the argument types and then then function type
6132 itself. */
6133 tree argtype0, argtype1, argtype2;
6135 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
6136 /* First, build the pointer-to-function type for the first
6137 argument. */
6138 fn_ptr_type = get_atexit_fn_ptr_type ();
6139 /* Then, build the rest of the argument types. */
6140 argtype2 = ptr_type_node;
6141 if (use_aeabi_atexit)
6143 argtype1 = fn_ptr_type;
6144 argtype0 = ptr_type_node;
6146 else
6148 argtype1 = ptr_type_node;
6149 argtype0 = fn_ptr_type;
6151 /* And the final __cxa_atexit type. */
6152 fn_type = build_function_type_list (integer_type_node,
6153 argtype0, argtype1, argtype2,
6154 NULL_TREE);
6155 fn_ptr_type = build_pointer_type (fn_type);
6156 if (use_aeabi_atexit)
6157 name = "__aeabi_atexit";
6158 else
6159 name = "__cxa_atexit";
6161 else
6163 /* The declaration for `atexit' is:
6165 int atexit (void (*)());
6167 We build up the argument types and then then function type
6168 itself. */
6169 fn_ptr_type = get_atexit_fn_ptr_type ();
6170 /* Build the final atexit type. */
6171 fn_type = build_function_type_list (integer_type_node,
6172 fn_ptr_type, NULL_TREE);
6173 name = "atexit";
6176 /* Now, build the function declaration. */
6177 push_lang_context (lang_name_c);
6178 atexit_fndecl = build_library_fn_ptr (name, fn_type);
6179 mark_used (atexit_fndecl);
6180 pop_lang_context ();
6181 atexit_node = decay_conversion (atexit_fndecl);
6183 return atexit_node;
6186 /* Returns the __dso_handle VAR_DECL. */
6188 static tree
6189 get_dso_handle_node (void)
6191 if (dso_handle_node)
6192 return dso_handle_node;
6194 /* Declare the variable. */
6195 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
6196 ptr_type_node);
6198 return dso_handle_node;
6201 /* Begin a new function with internal linkage whose job will be simply
6202 to destroy some particular variable. */
6204 static GTY(()) int start_cleanup_cnt;
6206 static tree
6207 start_cleanup_fn (void)
6209 char name[32];
6210 tree fntype;
6211 tree fndecl;
6212 bool use_cxa_atexit = flag_use_cxa_atexit
6213 && !targetm.cxx.use_atexit_for_cxa_atexit ();
6215 push_to_top_level ();
6217 /* No need to mangle this. */
6218 push_lang_context (lang_name_c);
6220 /* Build the name of the function. */
6221 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
6222 /* Build the function declaration. */
6223 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
6224 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
6225 /* It's a function with internal linkage, generated by the
6226 compiler. */
6227 TREE_PUBLIC (fndecl) = 0;
6228 DECL_ARTIFICIAL (fndecl) = 1;
6229 /* Make the function `inline' so that it is only emitted if it is
6230 actually needed. It is unlikely that it will be inlined, since
6231 it is only called via a function pointer, but we avoid unnecessary
6232 emissions this way. */
6233 DECL_DECLARED_INLINE_P (fndecl) = 1;
6234 DECL_INTERFACE_KNOWN (fndecl) = 1;
6235 /* Build the parameter. */
6236 if (use_cxa_atexit)
6238 tree parmdecl;
6240 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
6241 DECL_CONTEXT (parmdecl) = fndecl;
6242 TREE_USED (parmdecl) = 1;
6243 DECL_READ_P (parmdecl) = 1;
6244 DECL_ARGUMENTS (fndecl) = parmdecl;
6247 pushdecl (fndecl);
6248 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
6250 pop_lang_context ();
6252 return current_function_decl;
6255 /* Finish the cleanup function begun by start_cleanup_fn. */
6257 static void
6258 end_cleanup_fn (void)
6260 expand_or_defer_fn (finish_function (0));
6262 pop_from_top_level ();
6265 /* Generate code to handle the destruction of DECL, an object with
6266 static storage duration. */
6268 tree
6269 register_dtor_fn (tree decl)
6271 tree cleanup;
6272 tree compound_stmt;
6273 tree fcall;
6274 tree type;
6275 bool use_dtor;
6276 tree arg0, arg1 = NULL_TREE, arg2 = NULL_TREE;
6278 type = TREE_TYPE (decl);
6279 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
6280 return void_zero_node;
6282 /* If we're using "__cxa_atexit" (or "__aeabi_atexit"), and DECL is
6283 a class object, we can just pass the destructor to
6284 "__cxa_atexit"; we don't have to build a temporary function to do
6285 the cleanup. */
6286 use_dtor = (flag_use_cxa_atexit
6287 && !targetm.cxx.use_atexit_for_cxa_atexit ()
6288 && CLASS_TYPE_P (type));
6289 if (use_dtor)
6291 int idx;
6293 /* Find the destructor. */
6294 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
6295 gcc_assert (idx >= 0);
6296 cleanup = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), idx);
6297 /* Make sure it is accessible. */
6298 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup);
6300 else
6302 /* Call build_cleanup before we enter the anonymous function so
6303 that any access checks will be done relative to the current
6304 scope, rather than the scope of the anonymous function. */
6305 build_cleanup (decl);
6307 /* Now start the function. */
6308 cleanup = start_cleanup_fn ();
6310 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
6311 to the original function, rather than the anonymous one. That
6312 will make the back end think that nested functions are in use,
6313 which causes confusion. */
6314 push_deferring_access_checks (dk_no_check);
6315 fcall = build_cleanup (decl);
6316 pop_deferring_access_checks ();
6318 /* Create the body of the anonymous function. */
6319 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
6320 finish_expr_stmt (fcall);
6321 finish_compound_stmt (compound_stmt);
6322 end_cleanup_fn ();
6325 /* Call atexit with the cleanup function. */
6326 mark_used (cleanup);
6327 cleanup = build_address (cleanup);
6328 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
6330 tree addr;
6332 if (use_dtor)
6334 /* We must convert CLEANUP to the type that "__cxa_atexit"
6335 expects. */
6336 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
6337 /* "__cxa_atexit" will pass the address of DECL to the
6338 cleanup function. */
6339 mark_used (decl);
6340 addr = build_address (decl);
6341 /* The declared type of the parameter to "__cxa_atexit" is
6342 "void *". For plain "T*", we could just let the
6343 machinery in cp_build_function_call convert it -- but if the
6344 type is "cv-qualified T *", then we need to convert it
6345 before passing it in, to avoid spurious errors. */
6346 addr = build_nop (ptr_type_node, addr);
6348 else
6349 /* Since the cleanup functions we build ignore the address
6350 they're given, there's no reason to pass the actual address
6351 in, and, in general, it's cheaper to pass NULL than any
6352 other value. */
6353 addr = null_pointer_node;
6354 arg2 = cp_build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0,
6355 tf_warning_or_error);
6356 if (targetm.cxx.use_aeabi_atexit ())
6358 arg1 = cleanup;
6359 arg0 = addr;
6361 else
6363 arg1 = addr;
6364 arg0 = cleanup;
6367 else
6368 arg0 = cleanup;
6369 return cp_build_function_call_nary (get_atexit_node (), tf_warning_or_error,
6370 arg0, arg1, arg2, NULL_TREE);
6373 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
6374 is its initializer. Generate code to handle the construction
6375 and destruction of DECL. */
6377 static void
6378 expand_static_init (tree decl, tree init)
6380 gcc_assert (TREE_CODE (decl) == VAR_DECL);
6381 gcc_assert (TREE_STATIC (decl));
6383 /* Some variables require no initialization. */
6384 if (!init
6385 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
6386 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
6387 return;
6389 if (DECL_FUNCTION_SCOPE_P (decl))
6391 /* Emit code to perform this initialization but once. */
6392 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
6393 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
6394 tree guard, guard_addr;
6395 tree acquire_fn, release_fn, abort_fn;
6396 tree flag, begin;
6398 /* Emit code to perform this initialization but once. This code
6399 looks like:
6401 static <type> guard;
6402 if (!guard.first_byte) {
6403 if (__cxa_guard_acquire (&guard)) {
6404 bool flag = false;
6405 try {
6406 // Do initialization.
6407 flag = true; __cxa_guard_release (&guard);
6408 // Register variable for destruction at end of program.
6409 } catch {
6410 if (!flag) __cxa_guard_abort (&guard);
6414 Note that the `flag' variable is only set to 1 *after* the
6415 initialization is complete. This ensures that an exception,
6416 thrown during the construction, will cause the variable to
6417 reinitialized when we pass through this code again, as per:
6419 [stmt.dcl]
6421 If the initialization exits by throwing an exception, the
6422 initialization is not complete, so it will be tried again
6423 the next time control enters the declaration.
6425 This process should be thread-safe, too; multiple threads
6426 should not be able to initialize the variable more than
6427 once. */
6429 /* Create the guard variable. */
6430 guard = get_guard (decl);
6432 /* This optimization isn't safe on targets with relaxed memory
6433 consistency. On such targets we force synchronization in
6434 __cxa_guard_acquire. */
6435 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
6437 /* Begin the conditional initialization. */
6438 if_stmt = begin_if_stmt ();
6439 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
6440 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
6443 if (flag_threadsafe_statics)
6445 guard_addr = build_address (guard);
6447 acquire_fn = get_identifier ("__cxa_guard_acquire");
6448 release_fn = get_identifier ("__cxa_guard_release");
6449 abort_fn = get_identifier ("__cxa_guard_abort");
6450 if (!get_global_value_if_present (acquire_fn, &acquire_fn))
6452 tree vfntype = build_function_type_list (void_type_node,
6453 TREE_TYPE (guard_addr),
6454 NULL_TREE);
6455 acquire_fn = push_library_fn
6456 (acquire_fn, build_function_type_list (integer_type_node,
6457 TREE_TYPE (guard_addr),
6458 NULL_TREE),
6459 NULL_TREE);
6460 release_fn = push_library_fn (release_fn, vfntype, NULL_TREE);
6461 abort_fn = push_library_fn (abort_fn, vfntype, NULL_TREE);
6463 else
6465 release_fn = identifier_global_value (release_fn);
6466 abort_fn = identifier_global_value (abort_fn);
6469 inner_if_stmt = begin_if_stmt ();
6470 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
6471 inner_if_stmt);
6473 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
6474 begin = get_target_expr (boolean_false_node);
6475 flag = TARGET_EXPR_SLOT (begin);
6477 TARGET_EXPR_CLEANUP (begin)
6478 = build3 (COND_EXPR, void_type_node, flag,
6479 void_zero_node,
6480 build_call_n (abort_fn, 1, guard_addr));
6481 CLEANUP_EH_ONLY (begin) = 1;
6483 /* Do the initialization itself. */
6484 init = add_stmt_to_compound (begin, init);
6485 init = add_stmt_to_compound
6486 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
6487 init = add_stmt_to_compound
6488 (init, build_call_n (release_fn, 1, guard_addr));
6490 else
6491 init = add_stmt_to_compound (init, set_guard (guard));
6493 /* Use atexit to register a function for destroying this static
6494 variable. */
6495 init = add_stmt_to_compound (init, register_dtor_fn (decl));
6497 finish_expr_stmt (init);
6499 if (flag_threadsafe_statics)
6501 finish_compound_stmt (inner_then_clause);
6502 finish_then_clause (inner_if_stmt);
6503 finish_if_stmt (inner_if_stmt);
6506 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
6508 finish_compound_stmt (then_clause);
6509 finish_then_clause (if_stmt);
6510 finish_if_stmt (if_stmt);
6513 else
6514 static_aggregates = tree_cons (init, decl, static_aggregates);
6518 /* Make TYPE a complete type based on INITIAL_VALUE.
6519 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6520 2 if there was no information (in which case assume 0 if DO_DEFAULT),
6521 3 if the initializer list is empty (in pedantic mode). */
6524 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
6526 int failure;
6527 tree type, elt_type;
6529 if (initial_value)
6531 unsigned HOST_WIDE_INT i;
6532 tree value;
6534 /* An array of character type can be initialized from a
6535 brace-enclosed string constant.
6537 FIXME: this code is duplicated from reshape_init. Probably
6538 we should just call reshape_init here? */
6539 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
6540 && TREE_CODE (initial_value) == CONSTRUCTOR
6541 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (initial_value)))
6543 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
6544 tree value = VEC_index (constructor_elt, v, 0)->value;
6546 if (TREE_CODE (value) == STRING_CST
6547 && VEC_length (constructor_elt, v) == 1)
6548 initial_value = value;
6551 /* If any of the elements are parameter packs, we can't actually
6552 complete this type now because the array size is dependent. */
6553 if (TREE_CODE (initial_value) == CONSTRUCTOR)
6555 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
6556 i, value)
6558 if (PACK_EXPANSION_P (value))
6559 return 0;
6564 failure = complete_array_type (ptype, initial_value, do_default);
6566 /* We can create the array before the element type is complete, which
6567 means that we didn't have these two bits set in the original type
6568 either. In completing the type, we are expected to propagate these
6569 bits. See also complete_type which does the same thing for arrays
6570 of fixed size. */
6571 type = *ptype;
6572 if (TYPE_DOMAIN (type))
6574 elt_type = TREE_TYPE (type);
6575 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
6576 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
6577 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
6580 return failure;
6583 /* Return zero if something is declared to be a member of type
6584 CTYPE when in the context of CUR_TYPE. STRING is the error
6585 message to print in that case. Otherwise, quietly return 1. */
6587 static int
6588 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
6590 if (ctype && ctype != cur_type)
6592 if (flags == DTOR_FLAG)
6593 error ("destructor for alien class %qT cannot be a member", ctype);
6594 else
6595 error ("constructor for alien class %qT cannot be a member", ctype);
6596 return 0;
6598 return 1;
6601 /* Subroutine of `grokdeclarator'. */
6603 /* Generate errors possibly applicable for a given set of specifiers.
6604 This is for ARM $7.1.2. */
6606 static void
6607 bad_specifiers (tree object,
6608 enum bad_spec_place type,
6609 int virtualp,
6610 int quals,
6611 int inlinep,
6612 int friendp,
6613 int raises)
6615 switch (type)
6617 case BSP_VAR:
6618 if (virtualp)
6619 error ("%qD declared as a %<virtual%> variable", object);
6620 if (inlinep)
6621 error ("%qD declared as an %<inline%> variable", object);
6622 if (quals)
6623 error ("%<const%> and %<volatile%> function specifiers on "
6624 "%qD invalid in variable declaration", object);
6625 break;
6626 case BSP_PARM:
6627 if (virtualp)
6628 error ("%qD declared as a %<virtual%> parameter", object);
6629 if (inlinep)
6630 error ("%qD declared as an %<inline%> parameter", object);
6631 if (quals)
6632 error ("%<const%> and %<volatile%> function specifiers on "
6633 "%qD invalid in parameter declaration", object);
6634 break;
6635 case BSP_TYPE:
6636 if (virtualp)
6637 error ("%qD declared as a %<virtual%> type", object);
6638 if (inlinep)
6639 error ("%qD declared as an %<inline%> type", object);
6640 if (quals)
6641 error ("%<const%> and %<volatile%> function specifiers on "
6642 "%qD invalid in type declaration", object);
6643 break;
6644 case BSP_FIELD:
6645 if (virtualp)
6646 error ("%qD declared as a %<virtual%> field", object);
6647 if (inlinep)
6648 error ("%qD declared as an %<inline%> field", object);
6649 if (quals)
6650 error ("%<const%> and %<volatile%> function specifiers on "
6651 "%qD invalid in field declaration", object);
6652 break;
6653 default:
6654 gcc_unreachable();
6656 if (friendp)
6657 error ("%q+D declared as a friend", object);
6658 if (raises
6659 && (TREE_CODE (object) == TYPE_DECL
6660 || (!TYPE_PTRFN_P (TREE_TYPE (object))
6661 && !TYPE_REFFN_P (TREE_TYPE (object))
6662 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
6663 error ("%q+D declared with an exception specification", object);
6666 /* DECL is a member function or static data member and is presently
6667 being defined. Check that the definition is taking place in a
6668 valid namespace. */
6670 static void
6671 check_class_member_definition_namespace (tree decl)
6673 /* These checks only apply to member functions and static data
6674 members. */
6675 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6676 || TREE_CODE (decl) == VAR_DECL);
6677 /* We check for problems with specializations in pt.c in
6678 check_specialization_namespace, where we can issue better
6679 diagnostics. */
6680 if (processing_specialization)
6681 return;
6682 /* There are no restrictions on the placement of
6683 explicit instantiations. */
6684 if (processing_explicit_instantiation)
6685 return;
6686 /* [class.mfct]
6688 A member function definition that appears outside of the
6689 class definition shall appear in a namespace scope enclosing
6690 the class definition.
6692 [class.static.data]
6694 The definition for a static data member shall appear in a
6695 namespace scope enclosing the member's class definition. */
6696 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
6697 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
6698 decl, DECL_CONTEXT (decl));
6701 /* Build a PARM_DECL for the "this" parameter. TYPE is the
6702 METHOD_TYPE for a non-static member function; QUALS are the
6703 cv-qualifiers that apply to the function. */
6705 tree
6706 build_this_parm (tree type, cp_cv_quals quals)
6708 tree this_type;
6709 tree qual_type;
6710 tree parm;
6711 cp_cv_quals this_quals;
6713 this_type = TREE_VALUE (TYPE_ARG_TYPES (type));
6714 /* The `this' parameter is implicitly `const'; it cannot be
6715 assigned to. */
6716 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
6717 qual_type = cp_build_qualified_type (this_type, this_quals);
6718 parm = build_artificial_parm (this_identifier, qual_type);
6719 cp_apply_type_quals_to_decl (this_quals, parm);
6720 return parm;
6723 /* CTYPE is class type, or null if non-class.
6724 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
6725 or METHOD_TYPE.
6726 DECLARATOR is the function's name.
6727 PARMS is a chain of PARM_DECLs for the function.
6728 VIRTUALP is truthvalue of whether the function is virtual or not.
6729 FLAGS are to be passed through to `grokclassfn'.
6730 QUALS are qualifiers indicating whether the function is `const'
6731 or `volatile'.
6732 RAISES is a list of exceptions that this function can raise.
6733 CHECK is 1 if we must find this method in CTYPE, 0 if we should
6734 not look, and -1 if we should not call `grokclassfn' at all.
6736 SFK is the kind of special function (if any) for the new function.
6738 Returns `NULL_TREE' if something goes wrong, after issuing
6739 applicable error messages. */
6741 static tree
6742 grokfndecl (tree ctype,
6743 tree type,
6744 tree declarator,
6745 tree parms,
6746 tree orig_declarator,
6747 int virtualp,
6748 enum overload_flags flags,
6749 cp_cv_quals quals,
6750 tree raises,
6751 int check,
6752 int friendp,
6753 int publicp,
6754 int inlinep,
6755 special_function_kind sfk,
6756 bool funcdef_flag,
6757 int template_count,
6758 tree in_namespace,
6759 tree* attrlist,
6760 location_t location)
6762 tree decl;
6763 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
6764 tree t;
6766 if (raises)
6767 type = build_exception_variant (type, raises);
6769 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
6771 /* If we have an explicit location, use it, otherwise use whatever
6772 build_lang_decl used (probably input_location). */
6773 if (location != UNKNOWN_LOCATION)
6774 DECL_SOURCE_LOCATION (decl) = location;
6776 if (TREE_CODE (type) == METHOD_TYPE)
6778 tree parm;
6779 parm = build_this_parm (type, quals);
6780 DECL_CHAIN (parm) = parms;
6781 parms = parm;
6783 DECL_ARGUMENTS (decl) = parms;
6784 for (t = parms; t; t = DECL_CHAIN (t))
6785 DECL_CONTEXT (t) = decl;
6786 /* Propagate volatile out from type to decl. */
6787 if (TYPE_VOLATILE (type))
6788 TREE_THIS_VOLATILE (decl) = 1;
6790 /* Setup decl according to sfk. */
6791 switch (sfk)
6793 case sfk_constructor:
6794 case sfk_copy_constructor:
6795 case sfk_move_constructor:
6796 DECL_CONSTRUCTOR_P (decl) = 1;
6797 break;
6798 case sfk_destructor:
6799 DECL_DESTRUCTOR_P (decl) = 1;
6800 break;
6801 default:
6802 break;
6805 /* If pointers to member functions use the least significant bit to
6806 indicate whether a function is virtual, ensure a pointer
6807 to this function will have that bit clear. */
6808 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6809 && TREE_CODE (type) == METHOD_TYPE
6810 && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT)
6811 DECL_ALIGN (decl) = 2 * BITS_PER_UNIT;
6813 if (friendp
6814 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
6816 if (funcdef_flag)
6817 error
6818 ("defining explicit specialization %qD in friend declaration",
6819 orig_declarator);
6820 else
6822 tree fns = TREE_OPERAND (orig_declarator, 0);
6823 tree args = TREE_OPERAND (orig_declarator, 1);
6825 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
6827 /* Something like `template <class T> friend void f<T>()'. */
6828 error ("invalid use of template-id %qD in declaration "
6829 "of primary template",
6830 orig_declarator);
6831 return NULL_TREE;
6835 /* A friend declaration of the form friend void f<>(). Record
6836 the information in the TEMPLATE_ID_EXPR. */
6837 SET_DECL_IMPLICIT_INSTANTIATION (decl);
6839 if (TREE_CODE (fns) == COMPONENT_REF)
6841 /* Due to bison parser ickiness, we will have already looked
6842 up an operator_name or PFUNCNAME within the current class
6843 (see template_id in parse.y). If the current class contains
6844 such a name, we'll get a COMPONENT_REF here. Undo that. */
6846 gcc_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
6847 == current_class_type);
6848 fns = TREE_OPERAND (fns, 1);
6850 gcc_assert (TREE_CODE (fns) == IDENTIFIER_NODE
6851 || TREE_CODE (fns) == OVERLOAD);
6852 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
6854 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
6855 if (TREE_PURPOSE (t)
6856 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
6858 error ("default arguments are not allowed in declaration "
6859 "of friend template specialization %qD",
6860 decl);
6861 return NULL_TREE;
6864 if (inlinep)
6866 error ("%<inline%> is not allowed in declaration of friend "
6867 "template specialization %qD",
6868 decl);
6869 return NULL_TREE;
6874 /* If this decl has namespace scope, set that up. */
6875 if (in_namespace)
6876 set_decl_namespace (decl, in_namespace, friendp);
6877 else if (!ctype)
6878 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
6880 /* `main' and builtins have implicit 'C' linkage. */
6881 if ((MAIN_NAME_P (declarator)
6882 || (IDENTIFIER_LENGTH (declarator) > 10
6883 && IDENTIFIER_POINTER (declarator)[0] == '_'
6884 && IDENTIFIER_POINTER (declarator)[1] == '_'
6885 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
6886 && current_lang_name == lang_name_cplusplus
6887 && ctype == NULL_TREE
6888 /* NULL_TREE means global namespace. */
6889 && DECL_CONTEXT (decl) == NULL_TREE)
6890 SET_DECL_LANGUAGE (decl, lang_c);
6892 /* Should probably propagate const out from type to decl I bet (mrs). */
6893 if (staticp)
6895 DECL_STATIC_FUNCTION_P (decl) = 1;
6896 DECL_CONTEXT (decl) = ctype;
6899 if (ctype)
6901 DECL_CONTEXT (decl) = ctype;
6902 if (funcdef_flag)
6903 check_class_member_definition_namespace (decl);
6906 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
6908 if (processing_template_decl)
6909 error ("cannot declare %<::main%> to be a template");
6910 if (inlinep)
6911 error ("cannot declare %<::main%> to be inline");
6912 if (!publicp)
6913 error ("cannot declare %<::main%> to be static");
6914 inlinep = 0;
6915 publicp = 1;
6918 /* Members of anonymous types and local classes have no linkage; make
6919 them internal. If a typedef is made later, this will be changed. */
6920 if (ctype && (TYPE_ANONYMOUS_P (ctype)
6921 || decl_function_context (TYPE_MAIN_DECL (ctype))))
6922 publicp = 0;
6924 if (publicp && cxx_dialect == cxx98)
6926 /* [basic.link]: A name with no linkage (notably, the name of a class
6927 or enumeration declared in a local scope) shall not be used to
6928 declare an entity with linkage.
6930 DR 757 relaxes this restriction for C++0x. */
6931 t = no_linkage_check (TREE_TYPE (decl),
6932 /*relaxed_p=*/false);
6933 if (t)
6935 if (TYPE_ANONYMOUS_P (t))
6937 if (DECL_EXTERN_C_P (decl))
6938 /* Allow this; it's pretty common in C. */;
6939 else
6941 permerror (input_location, "anonymous type with no linkage "
6942 "used to declare function %q#D with linkage",
6943 decl);
6944 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
6945 permerror (input_location, "%q+#D does not refer to the unqualified "
6946 "type, so it is not used for linkage",
6947 TYPE_NAME (t));
6950 else
6951 permerror (input_location, "type %qT with no linkage used to "
6952 "declare function %q#D with linkage", t, decl);
6956 TREE_PUBLIC (decl) = publicp;
6957 if (! publicp)
6959 DECL_INTERFACE_KNOWN (decl) = 1;
6960 DECL_NOT_REALLY_EXTERN (decl) = 1;
6963 /* If the declaration was declared inline, mark it as such. */
6964 if (inlinep)
6965 DECL_DECLARED_INLINE_P (decl) = 1;
6967 DECL_EXTERNAL (decl) = 1;
6968 if (quals && TREE_CODE (type) == FUNCTION_TYPE)
6970 error (ctype
6971 ? G_("static member function %qD cannot have cv-qualifier")
6972 : G_("non-member function %qD cannot have cv-qualifier"),
6973 decl);
6974 quals = TYPE_UNQUALIFIED;
6977 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
6978 && !grok_op_properties (decl, /*complain=*/true))
6979 return NULL_TREE;
6981 if (funcdef_flag)
6982 /* Make the init_value nonzero so pushdecl knows this is not
6983 tentative. error_mark_node is replaced later with the BLOCK. */
6984 DECL_INITIAL (decl) = error_mark_node;
6986 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
6987 TREE_NOTHROW (decl) = 1;
6989 /* Caller will do the rest of this. */
6990 if (check < 0)
6991 return decl;
6993 if (ctype != NULL_TREE)
6994 grokclassfn (ctype, decl, flags);
6996 decl = check_explicit_specialization (orig_declarator, decl,
6997 template_count,
6998 2 * funcdef_flag +
6999 4 * (friendp != 0));
7000 if (decl == error_mark_node)
7001 return NULL_TREE;
7003 if (attrlist)
7005 cplus_decl_attributes (&decl, *attrlist, 0);
7006 *attrlist = NULL_TREE;
7009 /* Check main's type after attributes have been applied. */
7010 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
7012 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
7013 integer_type_node))
7015 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
7016 tree newtype;
7017 error ("%<::main%> must return %<int%>");
7018 newtype = build_function_type (integer_type_node, oldtypeargs);
7019 TREE_TYPE (decl) = newtype;
7021 if (warn_main)
7022 check_main_parameter_types (decl);
7025 if (ctype != NULL_TREE
7026 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
7027 && check)
7029 tree old_decl = check_classfn (ctype, decl,
7030 (processing_template_decl
7031 > template_class_depth (ctype))
7032 ? current_template_parms
7033 : NULL_TREE);
7035 if (old_decl == error_mark_node)
7036 return NULL_TREE;
7038 if (old_decl)
7040 tree ok;
7041 tree pushed_scope;
7043 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
7044 /* Because grokfndecl is always supposed to return a
7045 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
7046 here. We depend on our callers to figure out that its
7047 really a template that's being returned. */
7048 old_decl = DECL_TEMPLATE_RESULT (old_decl);
7050 if (DECL_STATIC_FUNCTION_P (old_decl)
7051 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7052 /* Remove the `this' parm added by grokclassfn.
7053 XXX Isn't this done in start_function, too? */
7054 revert_static_member_fn (decl);
7055 if (DECL_ARTIFICIAL (old_decl))
7057 error ("definition of implicitly-declared %qD", old_decl);
7058 return NULL_TREE;
7061 /* Since we've smashed OLD_DECL to its
7062 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
7063 if (TREE_CODE (decl) == TEMPLATE_DECL)
7064 decl = DECL_TEMPLATE_RESULT (decl);
7066 /* Attempt to merge the declarations. This can fail, in
7067 the case of some invalid specialization declarations. */
7068 pushed_scope = push_scope (ctype);
7069 ok = duplicate_decls (decl, old_decl, friendp);
7070 if (pushed_scope)
7071 pop_scope (pushed_scope);
7072 if (!ok)
7074 error ("no %q#D member function declared in class %qT",
7075 decl, ctype);
7076 return NULL_TREE;
7078 return old_decl;
7082 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
7083 return NULL_TREE;
7085 if (ctype == NULL_TREE || check)
7086 return decl;
7088 if (virtualp)
7089 DECL_VIRTUAL_P (decl) = 1;
7091 return decl;
7094 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
7095 the linkage that DECL will receive in the object file. */
7097 static void
7098 set_linkage_for_static_data_member (tree decl)
7100 /* A static data member always has static storage duration and
7101 external linkage. Note that static data members are forbidden in
7102 local classes -- the only situation in which a class has
7103 non-external linkage. */
7104 TREE_PUBLIC (decl) = 1;
7105 TREE_STATIC (decl) = 1;
7106 /* For non-template classes, static data members are always put
7107 out in exactly those files where they are defined, just as
7108 with ordinary namespace-scope variables. */
7109 if (!processing_template_decl)
7110 DECL_INTERFACE_KNOWN (decl) = 1;
7113 /* Create a VAR_DECL named NAME with the indicated TYPE.
7115 If SCOPE is non-NULL, it is the class type or namespace containing
7116 the variable. If SCOPE is NULL, the variable should is created in
7117 the innermost enclosings scope. */
7119 static tree
7120 grokvardecl (tree type,
7121 tree name,
7122 const cp_decl_specifier_seq *declspecs,
7123 int initialized,
7124 int constp,
7125 tree scope)
7127 tree decl;
7128 tree explicit_scope;
7130 gcc_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE);
7132 /* Compute the scope in which to place the variable, but remember
7133 whether or not that scope was explicitly specified by the user. */
7134 explicit_scope = scope;
7135 if (!scope)
7137 /* An explicit "extern" specifier indicates a namespace-scope
7138 variable. */
7139 if (declspecs->storage_class == sc_extern)
7140 scope = current_decl_namespace ();
7141 else if (!at_function_scope_p ())
7142 scope = current_scope ();
7145 if (scope
7146 && (/* If the variable is a namespace-scope variable declared in a
7147 template, we need DECL_LANG_SPECIFIC. */
7148 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
7149 /* Similarly for namespace-scope variables with language linkage
7150 other than C++. */
7151 || (TREE_CODE (scope) == NAMESPACE_DECL
7152 && current_lang_name != lang_name_cplusplus)
7153 /* Similarly for static data members. */
7154 || TYPE_P (scope)))
7155 decl = build_lang_decl (VAR_DECL, name, type);
7156 else
7157 decl = build_decl (input_location, VAR_DECL, name, type);
7159 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
7160 set_decl_namespace (decl, explicit_scope, 0);
7161 else
7162 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
7164 if (declspecs->storage_class == sc_extern)
7166 DECL_THIS_EXTERN (decl) = 1;
7167 DECL_EXTERNAL (decl) = !initialized;
7170 if (DECL_CLASS_SCOPE_P (decl))
7172 set_linkage_for_static_data_member (decl);
7173 /* This function is only called with out-of-class definitions. */
7174 DECL_EXTERNAL (decl) = 0;
7175 check_class_member_definition_namespace (decl);
7177 /* At top level, either `static' or no s.c. makes a definition
7178 (perhaps tentative), and absence of `static' makes it public. */
7179 else if (toplevel_bindings_p ())
7181 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
7182 && (DECL_THIS_EXTERN (decl) || ! constp));
7183 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
7185 /* Not at top level, only `static' makes a static definition. */
7186 else
7188 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
7189 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
7192 if (declspecs->specs[(int)ds_thread])
7193 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
7195 /* If the type of the decl has no linkage, make sure that we'll
7196 notice that in mark_used. */
7197 if (cxx_dialect > cxx98
7198 && decl_linkage (decl) != lk_none
7199 && DECL_LANG_SPECIFIC (decl) == NULL
7200 && !DECL_EXTERN_C_P (decl)
7201 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
7202 retrofit_lang_decl (decl);
7204 if (TREE_PUBLIC (decl))
7206 /* [basic.link]: A name with no linkage (notably, the name of a class
7207 or enumeration declared in a local scope) shall not be used to
7208 declare an entity with linkage.
7210 DR 757 relaxes this restriction for C++0x. */
7211 tree t = (cxx_dialect > cxx98 ? NULL_TREE
7212 : no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false));
7213 if (t)
7215 if (TYPE_ANONYMOUS_P (t))
7217 if (DECL_EXTERN_C_P (decl))
7218 /* Allow this; it's pretty common in C. */
7220 else
7222 /* DRs 132, 319 and 389 seem to indicate types with
7223 no linkage can only be used to declare extern "C"
7224 entities. Since it's not always an error in the
7225 ISO C++ 90 Standard, we only issue a warning. */
7226 warning (0, "anonymous type with no linkage used to declare "
7227 "variable %q#D with linkage", decl);
7228 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
7229 warning (0, "%q+#D does not refer to the unqualified "
7230 "type, so it is not used for linkage",
7231 TYPE_NAME (t));
7234 else
7235 warning (0, "type %qT with no linkage used to declare variable "
7236 "%q#D with linkage", t, decl);
7239 else
7240 DECL_INTERFACE_KNOWN (decl) = 1;
7242 return decl;
7245 /* Create and return a canonical pointer to member function type, for
7246 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
7248 tree
7249 build_ptrmemfunc_type (tree type)
7251 tree field, fields;
7252 tree t;
7253 tree unqualified_variant = NULL_TREE;
7255 if (type == error_mark_node)
7256 return type;
7258 /* If a canonical type already exists for this type, use it. We use
7259 this method instead of type_hash_canon, because it only does a
7260 simple equality check on the list of field members. */
7262 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
7263 return t;
7265 /* Make sure that we always have the unqualified pointer-to-member
7266 type first. */
7267 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
7268 unqualified_variant
7269 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
7271 t = make_class_type (RECORD_TYPE);
7272 xref_basetypes (t, NULL_TREE);
7274 /* Let the front end know this is a pointer to member function... */
7275 TYPE_PTRMEMFUNC_FLAG (t) = 1;
7276 /* ... and not really a class type. */
7277 SET_CLASS_TYPE_P (t, 0);
7279 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
7280 fields = field;
7282 field = build_decl (input_location, FIELD_DECL, delta_identifier,
7283 delta_type_node);
7284 DECL_CHAIN (field) = fields;
7285 fields = field;
7287 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
7289 /* Zap out the name so that the back end will give us the debugging
7290 information for this anonymous RECORD_TYPE. */
7291 TYPE_NAME (t) = NULL_TREE;
7293 /* If this is not the unqualified form of this pointer-to-member
7294 type, set the TYPE_MAIN_VARIANT for this type to be the
7295 unqualified type. Since they are actually RECORD_TYPEs that are
7296 not variants of each other, we must do this manually.
7297 As we just built a new type there is no need to do yet another copy. */
7298 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
7300 int type_quals = cp_type_quals (type);
7301 TYPE_READONLY (t) = (type_quals & TYPE_QUAL_CONST) != 0;
7302 TYPE_VOLATILE (t) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
7303 TYPE_RESTRICT (t) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
7304 TYPE_MAIN_VARIANT (t) = unqualified_variant;
7305 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
7306 TYPE_NEXT_VARIANT (unqualified_variant) = t;
7307 TREE_TYPE (TYPE_BINFO (t)) = t;
7310 /* Cache this pointer-to-member type so that we can find it again
7311 later. */
7312 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
7314 if (TYPE_STRUCTURAL_EQUALITY_P (type))
7315 SET_TYPE_STRUCTURAL_EQUALITY (t);
7316 else if (TYPE_CANONICAL (type) != type)
7317 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
7319 return t;
7322 /* Create and return a pointer to data member type. */
7324 tree
7325 build_ptrmem_type (tree class_type, tree member_type)
7327 if (TREE_CODE (member_type) == METHOD_TYPE)
7329 cp_cv_quals quals = type_memfn_quals (member_type);
7330 member_type = build_memfn_type (member_type, class_type, quals);
7331 return build_ptrmemfunc_type (build_pointer_type (member_type));
7333 else
7335 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
7336 return build_offset_type (class_type, member_type);
7340 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
7341 Check to see that the definition is valid. Issue appropriate error
7342 messages. Return 1 if the definition is particularly bad, or 0
7343 otherwise. */
7346 check_static_variable_definition (tree decl, tree type)
7348 /* Motion 10 at San Diego: If a static const integral data member is
7349 initialized with an integral constant expression, the initializer
7350 may appear either in the declaration (within the class), or in
7351 the definition, but not both. If it appears in the class, the
7352 member is a member constant. The file-scope definition is always
7353 required. */
7354 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
7356 error ("invalid in-class initialization of static data member "
7357 "of non-integral type %qT",
7358 type);
7359 /* If we just return the declaration, crashes will sometimes
7360 occur. We therefore return void_type_node, as if this were a
7361 friend declaration, to cause callers to completely ignore
7362 this declaration. */
7363 return 1;
7365 else if (!CP_TYPE_CONST_P (type))
7366 error ("ISO C++ forbids in-class initialization of non-const "
7367 "static member %qD",
7368 decl);
7369 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7370 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids initialization of member constant "
7371 "%qD of non-integral type %qT", decl, type);
7373 return 0;
7376 /* Given the SIZE (i.e., number of elements) in an array, compute an
7377 appropriate index type for the array. If non-NULL, NAME is the
7378 name of the thing being declared. */
7380 tree
7381 compute_array_index_type (tree name, tree size)
7383 tree type;
7384 tree itype;
7385 tree abi_1_itype = NULL_TREE;
7387 if (error_operand_p (size))
7388 return error_mark_node;
7390 type = TREE_TYPE (size);
7391 /* The array bound must be an integer type. */
7392 if (!dependent_type_p (type) && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
7394 if (name)
7395 error ("size of array %qD has non-integral type %qT", name, type);
7396 else
7397 error ("size of array has non-integral type %qT", type);
7398 size = integer_one_node;
7399 type = TREE_TYPE (size);
7402 /* We can only call value_dependent_expression_p on integral constant
7403 expressions; the parser adds a dummy NOP_EXPR with TREE_SIDE_EFFECTS
7404 set if this isn't one. */
7405 if (processing_template_decl
7406 && (TREE_SIDE_EFFECTS (size) || value_dependent_expression_p (size)))
7408 /* We cannot do any checking for a SIZE that isn't known to be
7409 constant. Just build the index type and mark that it requires
7410 structural equality checks. */
7411 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
7412 size, integer_one_node));
7413 TYPE_DEPENDENT_P (itype) = 1;
7414 TYPE_DEPENDENT_P_VALID (itype) = 1;
7415 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7416 return itype;
7419 if (!abi_version_at_least (2) && processing_template_decl)
7420 /* For abi-1, we handled all instances in templates the same way,
7421 even when they were non-dependent. This affects the manglings
7422 produced. So, we do the normal checking for non-dependent
7423 sizes, but at the end we'll return the same type that abi-1
7424 would have, but with TYPE_CANONICAL set to the "right"
7425 value that the current ABI would provide. */
7426 abi_1_itype = build_index_type (build_min (MINUS_EXPR, sizetype,
7427 size, integer_one_node));
7429 /* The size might be the result of a cast. */
7430 STRIP_TYPE_NOPS (size);
7432 size = mark_rvalue_use (size);
7434 /* It might be a const variable or enumeration constant. */
7435 size = integral_constant_value (size);
7436 if (error_operand_p (size))
7437 return error_mark_node;
7439 /* Normally, the array-bound will be a constant. */
7440 if (TREE_CODE (size) == INTEGER_CST)
7442 /* Check to see if the array bound overflowed. Make that an
7443 error, no matter how generous we're being. */
7444 constant_expression_error (size);
7446 /* An array must have a positive number of elements. */
7447 if (INT_CST_LT (size, integer_zero_node))
7449 if (name)
7450 error ("size of array %qD is negative", name);
7451 else
7452 error ("size of array is negative");
7453 size = integer_one_node;
7455 /* As an extension we allow zero-sized arrays. We always allow
7456 them in system headers because glibc uses them. */
7457 else if (integer_zerop (size) && !in_system_header)
7459 if (name)
7460 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids zero-size array %qD", name);
7461 else
7462 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids zero-size array");
7465 else if (TREE_CONSTANT (size))
7467 /* `(int) &fn' is not a valid array bound. */
7468 if (name)
7469 error ("size of array %qD is not an integral constant-expression",
7470 name);
7471 else
7472 error ("size of array is not an integral constant-expression");
7473 size = integer_one_node;
7475 else if (pedantic && warn_vla != 0)
7477 if (name)
7478 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
7479 else
7480 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
7482 else if (warn_vla > 0)
7484 if (name)
7485 warning (OPT_Wvla,
7486 "variable length array %qD is used", name);
7487 else
7488 warning (OPT_Wvla,
7489 "variable length array is used");
7492 if (processing_template_decl && !TREE_CONSTANT (size))
7493 /* A variable sized array. */
7494 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
7495 else
7497 HOST_WIDE_INT saved_processing_template_decl;
7499 /* Compute the index of the largest element in the array. It is
7500 one less than the number of elements in the array. We save
7501 and restore PROCESSING_TEMPLATE_DECL so that computations in
7502 cp_build_binary_op will be appropriately folded. */
7503 saved_processing_template_decl = processing_template_decl;
7504 processing_template_decl = 0;
7505 itype = cp_build_binary_op (input_location,
7506 MINUS_EXPR,
7507 cp_convert (ssizetype, size),
7508 cp_convert (ssizetype, integer_one_node),
7509 tf_warning_or_error);
7510 itype = fold (itype);
7511 processing_template_decl = saved_processing_template_decl;
7513 if (!TREE_CONSTANT (itype))
7514 /* A variable sized array. */
7515 itype = variable_size (itype);
7516 /* Make sure that there was no overflow when creating to a signed
7517 index type. (For example, on a 32-bit machine, an array with
7518 size 2^32 - 1 is too big.) */
7519 else if (TREE_CODE (itype) == INTEGER_CST
7520 && TREE_OVERFLOW (itype))
7522 error ("overflow in array dimension");
7523 TREE_OVERFLOW (itype) = 0;
7527 /* Create and return the appropriate index type. */
7528 if (abi_1_itype)
7530 tree t = build_index_type (itype);
7531 TYPE_CANONICAL (abi_1_itype) = TYPE_CANONICAL (t);
7532 return abi_1_itype;
7534 else
7535 return build_index_type (itype);
7538 /* Returns the scope (if any) in which the entity declared by
7539 DECLARATOR will be located. If the entity was declared with an
7540 unqualified name, NULL_TREE is returned. */
7542 tree
7543 get_scope_of_declarator (const cp_declarator *declarator)
7545 while (declarator && declarator->kind != cdk_id)
7546 declarator = declarator->declarator;
7548 /* If the declarator-id is a SCOPE_REF, the scope in which the
7549 declaration occurs is the first operand. */
7550 if (declarator
7551 && declarator->u.id.qualifying_scope)
7552 return declarator->u.id.qualifying_scope;
7554 /* Otherwise, the declarator is not a qualified name; the entity will
7555 be declared in the current scope. */
7556 return NULL_TREE;
7559 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
7560 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
7561 with this type. */
7563 static tree
7564 create_array_type_for_decl (tree name, tree type, tree size)
7566 tree itype = NULL_TREE;
7568 /* If things have already gone awry, bail now. */
7569 if (type == error_mark_node || size == error_mark_node)
7570 return error_mark_node;
7572 /* If there are some types which cannot be array elements,
7573 issue an error-message and return. */
7574 switch (TREE_CODE (type))
7576 case VOID_TYPE:
7577 if (name)
7578 error ("declaration of %qD as array of void", name);
7579 else
7580 error ("creating array of void");
7581 return error_mark_node;
7583 case FUNCTION_TYPE:
7584 if (name)
7585 error ("declaration of %qD as array of functions", name);
7586 else
7587 error ("creating array of functions");
7588 return error_mark_node;
7590 case REFERENCE_TYPE:
7591 if (name)
7592 error ("declaration of %qD as array of references", name);
7593 else
7594 error ("creating array of references");
7595 return error_mark_node;
7597 case METHOD_TYPE:
7598 if (name)
7599 error ("declaration of %qD as array of function members", name);
7600 else
7601 error ("creating array of function members");
7602 return error_mark_node;
7604 default:
7605 break;
7608 /* [dcl.array]
7610 The constant expressions that specify the bounds of the arrays
7611 can be omitted only for the first member of the sequence. */
7612 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
7614 if (name)
7615 error ("declaration of %qD as multidimensional array must "
7616 "have bounds for all dimensions except the first",
7617 name);
7618 else
7619 error ("multidimensional array must have bounds for all "
7620 "dimensions except the first");
7622 return error_mark_node;
7625 /* Figure out the index type for the array. */
7626 if (size)
7627 itype = compute_array_index_type (name, size);
7629 /* [dcl.array]
7630 T is called the array element type; this type shall not be [...] an
7631 abstract class type. */
7632 abstract_virtuals_error (name, type);
7634 return build_cplus_array_type (type, itype);
7637 /* Check that it's OK to declare a function with the indicated TYPE.
7638 SFK indicates the kind of special function (if any) that this
7639 function is. OPTYPE is the type given in a conversion operator
7640 declaration, or the class type for a constructor/destructor.
7641 Returns the actual return type of the function; that
7642 may be different than TYPE if an error occurs, or for certain
7643 special functions. */
7645 static tree
7646 check_special_function_return_type (special_function_kind sfk,
7647 tree type,
7648 tree optype)
7650 switch (sfk)
7652 case sfk_constructor:
7653 if (type)
7654 error ("return type specification for constructor invalid");
7656 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
7657 type = build_pointer_type (optype);
7658 else
7659 type = void_type_node;
7660 break;
7662 case sfk_destructor:
7663 if (type)
7664 error ("return type specification for destructor invalid");
7665 /* We can't use the proper return type here because we run into
7666 problems with ambiguous bases and covariant returns.
7667 Java classes are left unchanged because (void *) isn't a valid
7668 Java type, and we don't want to change the Java ABI. */
7669 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
7670 type = build_pointer_type (void_type_node);
7671 else
7672 type = void_type_node;
7673 break;
7675 case sfk_conversion:
7676 if (type)
7677 error ("return type specified for %<operator %T%>", optype);
7678 type = optype;
7679 break;
7681 default:
7682 gcc_unreachable ();
7685 return type;
7688 /* A variable or data member (whose unqualified name is IDENTIFIER)
7689 has been declared with the indicated TYPE. If the TYPE is not
7690 acceptable, issue an error message and return a type to use for
7691 error-recovery purposes. */
7693 tree
7694 check_var_type (tree identifier, tree type)
7696 if (VOID_TYPE_P (type))
7698 if (!identifier)
7699 error ("unnamed variable or field declared void");
7700 else if (TREE_CODE (identifier) == IDENTIFIER_NODE)
7702 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
7703 error ("variable or field %qE declared void", identifier);
7705 else
7706 error ("variable or field declared void");
7707 type = error_mark_node;
7710 return type;
7713 /* Given declspecs and a declarator (abstract or otherwise), determine
7714 the name and type of the object declared and construct a DECL node
7715 for it.
7717 DECLSPECS points to the representation of declaration-specifier
7718 sequence that precedes declarator.
7720 DECL_CONTEXT says which syntactic context this declaration is in:
7721 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
7722 FUNCDEF for a function definition. Like NORMAL but a few different
7723 error messages in each case. Return value may be zero meaning
7724 this definition is too screwy to try to parse.
7725 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
7726 handle member functions (which have FIELD context).
7727 Return value may be zero meaning this definition is too screwy to
7728 try to parse.
7729 PARM for a parameter declaration (either within a function prototype
7730 or before a function body). Make a PARM_DECL, or return void_type_node.
7731 TPARM for a template parameter declaration.
7732 CATCHPARM for a parameter declaration before a catch clause.
7733 TYPENAME if for a typename (in a cast or sizeof).
7734 Don't make a DECL node; just return the ..._TYPE node.
7735 FIELD for a struct or union field; make a FIELD_DECL.
7736 BITFIELD for a field with specified width.
7738 INITIALIZED is as for start_decl.
7740 ATTRLIST is a pointer to the list of attributes, which may be NULL
7741 if there are none; *ATTRLIST may be modified if attributes from inside
7742 the declarator should be applied to the declaration.
7744 When this function is called, scoping variables (such as
7745 CURRENT_CLASS_TYPE) should reflect the scope in which the
7746 declaration occurs, not the scope in which the new declaration will
7747 be placed. For example, on:
7749 void S::f() { ... }
7751 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
7752 should not be `S'.
7754 Returns a DECL (if a declarator is present), a TYPE (if there is no
7755 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
7756 error occurs. */
7758 tree
7759 grokdeclarator (const cp_declarator *declarator,
7760 const cp_decl_specifier_seq *declspecs,
7761 enum decl_context decl_context,
7762 int initialized,
7763 tree* attrlist)
7765 tree type = NULL_TREE;
7766 int longlong = 0;
7767 int explicit_int128 = 0;
7768 int virtualp, explicitp, friendp, inlinep, staticp;
7769 int explicit_int = 0;
7770 int explicit_char = 0;
7771 int defaulted_int = 0;
7772 tree dependent_name = NULL_TREE;
7774 tree typedef_decl = NULL_TREE;
7775 const char *name = NULL;
7776 tree typedef_type = NULL_TREE;
7777 /* True if this declarator is a function definition. */
7778 bool funcdef_flag = false;
7779 cp_declarator_kind innermost_code = cdk_error;
7780 int bitfield = 0;
7781 #if 0
7782 /* See the code below that used this. */
7783 tree decl_attr = NULL_TREE;
7784 #endif
7786 /* Keep track of what sort of function is being processed
7787 so that we can warn about default return values, or explicit
7788 return values which do not match prescribed defaults. */
7789 special_function_kind sfk = sfk_none;
7791 tree dname = NULL_TREE;
7792 tree ctor_return_type = NULL_TREE;
7793 enum overload_flags flags = NO_SPECIAL;
7794 /* cv-qualifiers that apply to the declarator, for a declaration of
7795 a member function. */
7796 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
7797 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
7798 int type_quals;
7799 tree raises = NULL_TREE;
7800 int template_count = 0;
7801 tree returned_attrs = NULL_TREE;
7802 tree parms = NULL_TREE;
7803 const cp_declarator *id_declarator;
7804 /* The unqualified name of the declarator; either an
7805 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
7806 tree unqualified_id;
7807 /* The class type, if any, in which this entity is located,
7808 or NULL_TREE if none. Note that this value may be different from
7809 the current class type; for example if an attempt is made to declare
7810 "A::f" inside "B", this value will be "A". */
7811 tree ctype = current_class_type;
7812 /* The NAMESPACE_DECL for the namespace in which this entity is
7813 located. If an unqualified name is used to declare the entity,
7814 this value will be NULL_TREE, even if the entity is located at
7815 namespace scope. */
7816 tree in_namespace = NULL_TREE;
7817 cp_storage_class storage_class;
7818 bool unsigned_p, signed_p, short_p, long_p, thread_p;
7819 bool type_was_error_mark_node = false;
7820 bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
7821 bool template_type_arg = false;
7822 bool template_parm_flag = false;
7823 bool constexpr_p = declspecs->specs[(int) ds_constexpr];
7824 const char *errmsg;
7826 signed_p = declspecs->specs[(int)ds_signed];
7827 unsigned_p = declspecs->specs[(int)ds_unsigned];
7828 short_p = declspecs->specs[(int)ds_short];
7829 long_p = declspecs->specs[(int)ds_long];
7830 longlong = declspecs->specs[(int)ds_long] >= 2;
7831 explicit_int128 = declspecs->explicit_int128_p;
7832 thread_p = declspecs->specs[(int)ds_thread];
7834 if (decl_context == FUNCDEF)
7835 funcdef_flag = true, decl_context = NORMAL;
7836 else if (decl_context == MEMFUNCDEF)
7837 funcdef_flag = true, decl_context = FIELD;
7838 else if (decl_context == BITFIELD)
7839 bitfield = 1, decl_context = FIELD;
7840 else if (decl_context == TEMPLATE_TYPE_ARG)
7841 template_type_arg = true, decl_context = TYPENAME;
7842 else if (decl_context == TPARM)
7843 template_parm_flag = true, decl_context = PARM;
7845 if (initialized > 1)
7846 funcdef_flag = true;
7848 /* Look inside a declarator for the name being declared
7849 and get it as a string, for an error message. */
7850 for (id_declarator = declarator;
7851 id_declarator;
7852 id_declarator = id_declarator->declarator)
7854 if (id_declarator->kind != cdk_id)
7855 innermost_code = id_declarator->kind;
7857 switch (id_declarator->kind)
7859 case cdk_function:
7860 if (id_declarator->declarator
7861 && id_declarator->declarator->kind == cdk_id)
7863 sfk = id_declarator->declarator->u.id.sfk;
7864 if (sfk == sfk_destructor)
7865 flags = DTOR_FLAG;
7867 break;
7869 case cdk_id:
7871 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
7872 tree decl = id_declarator->u.id.unqualified_name;
7873 if (!decl)
7874 break;
7875 if (qualifying_scope)
7877 if (at_function_scope_p ())
7879 /* [dcl.meaning]
7881 A declarator-id shall not be qualified except
7882 for ...
7884 None of the cases are permitted in block
7885 scope. */
7886 if (qualifying_scope == global_namespace)
7887 error ("invalid use of qualified-name %<::%D%>",
7888 decl);
7889 else if (TYPE_P (qualifying_scope))
7890 error ("invalid use of qualified-name %<%T::%D%>",
7891 qualifying_scope, decl);
7892 else
7893 error ("invalid use of qualified-name %<%D::%D%>",
7894 qualifying_scope, decl);
7895 return error_mark_node;
7897 else if (TYPE_P (qualifying_scope))
7899 ctype = qualifying_scope;
7900 if (innermost_code != cdk_function
7901 && current_class_type
7902 && !UNIQUELY_DERIVED_FROM_P (ctype,
7903 current_class_type))
7905 error ("type %qT is not derived from type %qT",
7906 ctype, current_class_type);
7907 return error_mark_node;
7910 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
7911 in_namespace = qualifying_scope;
7913 switch (TREE_CODE (decl))
7915 case BIT_NOT_EXPR:
7917 tree type;
7919 if (innermost_code != cdk_function)
7921 error ("declaration of %qD as non-function", decl);
7922 return error_mark_node;
7924 else if (!qualifying_scope
7925 && !(current_class_type && at_class_scope_p ()))
7927 error ("declaration of %qD as non-member", decl);
7928 return error_mark_node;
7931 type = TREE_OPERAND (decl, 0);
7932 if (TYPE_P (type))
7933 type = constructor_name (type);
7934 name = identifier_to_locale (IDENTIFIER_POINTER (type));
7935 dname = decl;
7937 break;
7939 case TEMPLATE_ID_EXPR:
7941 tree fns = TREE_OPERAND (decl, 0);
7943 dname = fns;
7944 if (TREE_CODE (dname) != IDENTIFIER_NODE)
7946 gcc_assert (is_overloaded_fn (dname));
7947 dname = DECL_NAME (get_first_fn (dname));
7950 /* Fall through. */
7952 case IDENTIFIER_NODE:
7953 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7954 dname = decl;
7956 if (C_IS_RESERVED_WORD (dname))
7958 error ("declarator-id missing; using reserved word %qD",
7959 dname);
7960 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
7962 else if (!IDENTIFIER_TYPENAME_P (dname))
7963 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
7964 else
7966 gcc_assert (flags == NO_SPECIAL);
7967 flags = TYPENAME_FLAG;
7968 ctor_return_type = TREE_TYPE (dname);
7969 sfk = sfk_conversion;
7970 if (is_typename_at_global_scope (dname))
7971 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
7972 else
7973 name = "<invalid operator>";
7975 break;
7977 default:
7978 gcc_unreachable ();
7980 break;
7983 case cdk_array:
7984 case cdk_pointer:
7985 case cdk_reference:
7986 case cdk_ptrmem:
7987 break;
7989 case cdk_error:
7990 return error_mark_node;
7992 default:
7993 gcc_unreachable ();
7995 if (id_declarator->kind == cdk_id)
7996 break;
7999 /* [dcl.fct.edf]
8001 The declarator in a function-definition shall have the form
8002 D1 ( parameter-declaration-clause) ... */
8003 if (funcdef_flag && innermost_code != cdk_function)
8005 error ("function definition does not declare parameters");
8006 return error_mark_node;
8009 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
8010 && innermost_code != cdk_function
8011 && ! (ctype && !declspecs->any_specifiers_p))
8013 error ("declaration of %qD as non-function", dname);
8014 return error_mark_node;
8017 /* Anything declared one level down from the top level
8018 must be one of the parameters of a function
8019 (because the body is at least two levels down). */
8021 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
8022 by not allowing C++ class definitions to specify their parameters
8023 with xdecls (must be spec.d in the parmlist).
8025 Since we now wait to push a class scope until we are sure that
8026 we are in a legitimate method context, we must set oldcname
8027 explicitly (since current_class_name is not yet alive).
8029 We also want to avoid calling this a PARM if it is in a namespace. */
8031 if (decl_context == NORMAL && !toplevel_bindings_p ())
8033 struct cp_binding_level *b = current_binding_level;
8034 current_binding_level = b->level_chain;
8035 if (current_binding_level != 0 && toplevel_bindings_p ())
8036 decl_context = PARM;
8037 current_binding_level = b;
8040 if (name == NULL)
8041 name = decl_context == PARM ? "parameter" : "type name";
8043 /* If there were multiple types specified in the decl-specifier-seq,
8044 issue an error message. */
8045 if (declspecs->multiple_types_p)
8047 error ("two or more data types in declaration of %qs", name);
8048 return error_mark_node;
8051 if (declspecs->conflicting_specifiers_p)
8053 error ("conflicting specifiers in declaration of %qs", name);
8054 return error_mark_node;
8057 /* Extract the basic type from the decl-specifier-seq. */
8058 type = declspecs->type;
8059 if (type == error_mark_node)
8061 type = NULL_TREE;
8062 type_was_error_mark_node = true;
8064 /* If the entire declaration is itself tagged as deprecated then
8065 suppress reports of deprecated items. */
8066 if (type && TREE_DEPRECATED (type)
8067 && deprecated_state != DEPRECATED_SUPPRESS)
8068 warn_deprecated_use (type, NULL_TREE);
8069 if (type && TREE_CODE (type) == TYPE_DECL)
8071 typedef_decl = type;
8072 type = TREE_TYPE (typedef_decl);
8073 if (TREE_DEPRECATED (type)
8074 && DECL_ARTIFICIAL (typedef_decl)
8075 && deprecated_state != DEPRECATED_SUPPRESS)
8076 warn_deprecated_use (type, NULL_TREE);
8078 /* No type at all: default to `int', and set DEFAULTED_INT
8079 because it was not a user-defined typedef. */
8080 if (type == NULL_TREE && (signed_p || unsigned_p || long_p || short_p))
8082 /* These imply 'int'. */
8083 type = integer_type_node;
8084 defaulted_int = 1;
8086 /* Gather flags. */
8087 explicit_int = declspecs->explicit_int_p;
8088 explicit_char = declspecs->explicit_char_p;
8090 #if 0
8091 /* See the code below that used this. */
8092 if (typedef_decl)
8093 decl_attr = DECL_ATTRIBUTES (typedef_decl);
8094 #endif
8095 typedef_type = type;
8098 if (sfk != sfk_conversion)
8099 ctor_return_type = ctype;
8101 if (sfk != sfk_none)
8102 type = check_special_function_return_type (sfk, type,
8103 ctor_return_type);
8104 else if (type == NULL_TREE)
8106 int is_main;
8108 explicit_int = -1;
8110 /* We handle `main' specially here, because 'main () { }' is so
8111 common. With no options, it is allowed. With -Wreturn-type,
8112 it is a warning. It is only an error with -pedantic-errors. */
8113 is_main = (funcdef_flag
8114 && dname && MAIN_NAME_P (dname)
8115 && ctype == NULL_TREE
8116 && in_namespace == NULL_TREE
8117 && current_namespace == global_namespace);
8119 if (type_was_error_mark_node)
8120 /* We've already issued an error, don't complain more. */;
8121 else if (in_system_header || flag_ms_extensions)
8122 /* Allow it, sigh. */;
8123 else if (! is_main)
8124 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
8125 else if (pedantic)
8126 pedwarn (input_location, OPT_pedantic,
8127 "ISO C++ forbids declaration of %qs with no type", name);
8128 else
8129 warning (OPT_Wreturn_type,
8130 "ISO C++ forbids declaration of %qs with no type", name);
8132 type = integer_type_node;
8135 ctype = NULL_TREE;
8137 /* Now process the modifiers that were specified
8138 and check for invalid combinations. */
8140 /* Long double is a special combination. */
8141 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
8143 long_p = false;
8144 type = cp_build_qualified_type (long_double_type_node,
8145 cp_type_quals (type));
8148 /* Check all other uses of type modifiers. */
8150 if (unsigned_p || signed_p || long_p || short_p)
8152 int ok = 0;
8154 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
8155 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
8156 else if (signed_p && unsigned_p)
8157 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
8158 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
8159 error ("%<long long%> invalid for %qs", name);
8160 else if (explicit_int128 && TREE_CODE (type) != INTEGER_TYPE)
8161 error ("%<__int128%> invalid for %qs", name);
8162 else if (long_p && TREE_CODE (type) == REAL_TYPE)
8163 error ("%<long%> invalid for %qs", name);
8164 else if (short_p && TREE_CODE (type) == REAL_TYPE)
8165 error ("%<short%> invalid for %qs", name);
8166 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
8167 error ("%<long%> or %<short%> invalid for %qs", name);
8168 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_int128)
8169 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
8170 else if ((long_p || short_p) && explicit_char)
8171 error ("%<long%> or %<short%> specified with char for %qs", name);
8172 else if (long_p && short_p)
8173 error ("%<long%> and %<short%> specified together for %qs", name);
8174 else if (type == char16_type_node || type == char32_type_node)
8176 if (signed_p || unsigned_p)
8177 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
8178 else if (short_p || long_p)
8179 error ("%<short%> or %<long%> invalid for %qs", name);
8181 else
8183 ok = 1;
8184 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_int128 && pedantic)
8186 pedwarn (input_location, OPT_pedantic,
8187 "long, short, signed or unsigned used invalidly for %qs",
8188 name);
8189 if (flag_pedantic_errors)
8190 ok = 0;
8192 if (explicit_int128)
8194 if (int128_integer_type_node == NULL_TREE)
8196 error ("%<__int128%> is not supported by this target");
8197 ok = 0;
8199 else if (pedantic)
8201 pedwarn (input_location, OPT_pedantic,
8202 "ISO C++ does not support %<__int128%> for %qs",
8203 name);
8204 if (flag_pedantic_errors)
8205 ok = 0;
8210 /* Discard the type modifiers if they are invalid. */
8211 if (! ok)
8213 unsigned_p = false;
8214 signed_p = false;
8215 long_p = false;
8216 short_p = false;
8217 longlong = 0;
8218 explicit_int128 = false;
8222 /* Decide whether an integer type is signed or not.
8223 Optionally treat bitfields as signed by default. */
8224 if (unsigned_p
8225 /* [class.bit]
8227 It is implementation-defined whether a plain (neither
8228 explicitly signed or unsigned) char, short, int, or long
8229 bit-field is signed or unsigned.
8231 Naturally, we extend this to long long as well. Note that
8232 this does not include wchar_t. */
8233 || (bitfield && !flag_signed_bitfields
8234 && !signed_p
8235 /* A typedef for plain `int' without `signed' can be
8236 controlled just like plain `int', but a typedef for
8237 `signed int' cannot be so controlled. */
8238 && !(typedef_decl
8239 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
8240 && TREE_CODE (type) == INTEGER_TYPE
8241 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
8243 if (explicit_int128)
8244 type = int128_unsigned_type_node;
8245 else if (longlong)
8246 type = long_long_unsigned_type_node;
8247 else if (long_p)
8248 type = long_unsigned_type_node;
8249 else if (short_p)
8250 type = short_unsigned_type_node;
8251 else if (type == char_type_node)
8252 type = unsigned_char_type_node;
8253 else if (typedef_decl)
8254 type = unsigned_type_for (type);
8255 else
8256 type = unsigned_type_node;
8258 else if (signed_p && type == char_type_node)
8259 type = signed_char_type_node;
8260 else if (explicit_int128)
8261 type = int128_integer_type_node;
8262 else if (longlong)
8263 type = long_long_integer_type_node;
8264 else if (long_p)
8265 type = long_integer_type_node;
8266 else if (short_p)
8267 type = short_integer_type_node;
8269 if (declspecs->specs[(int)ds_complex])
8271 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
8272 error ("complex invalid for %qs", name);
8273 /* If we just have "complex", it is equivalent to
8274 "complex double", but if any modifiers at all are specified it is
8275 the complex form of TYPE. E.g, "complex short" is
8276 "complex short int". */
8277 else if (defaulted_int && ! longlong && ! explicit_int128
8278 && ! (long_p || short_p || signed_p || unsigned_p))
8279 type = complex_double_type_node;
8280 else if (type == integer_type_node)
8281 type = complex_integer_type_node;
8282 else if (type == float_type_node)
8283 type = complex_float_type_node;
8284 else if (type == double_type_node)
8285 type = complex_double_type_node;
8286 else if (type == long_double_type_node)
8287 type = complex_long_double_type_node;
8288 else
8289 type = build_complex_type (type);
8292 type_quals = TYPE_UNQUALIFIED;
8293 if (declspecs->specs[(int)ds_const])
8294 type_quals |= TYPE_QUAL_CONST;
8295 /* A `constexpr' specifier used in an object declaration declares
8296 the object as `const'. */
8297 if (constexpr_p)
8299 if (innermost_code == cdk_function)
8301 else if (declspecs->specs[(int)ds_const] != 0)
8302 error ("both %<const%> and %<constexpr%> cannot be used here");
8303 else
8304 type_quals |= TYPE_QUAL_CONST;
8306 if (declspecs->specs[(int)ds_volatile])
8307 type_quals |= TYPE_QUAL_VOLATILE;
8308 if (declspecs->specs[(int)ds_restrict])
8309 type_quals |= TYPE_QUAL_RESTRICT;
8310 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
8311 error ("qualifiers are not allowed on declaration of %<operator %T%>",
8312 ctor_return_type);
8314 type_quals |= cp_type_quals (type);
8315 type = cp_build_qualified_type_real
8316 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
8317 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
8318 /* We might have ignored or rejected some of the qualifiers. */
8319 type_quals = cp_type_quals (type);
8321 staticp = 0;
8322 inlinep = !! declspecs->specs[(int)ds_inline];
8323 virtualp = !! declspecs->specs[(int)ds_virtual];
8324 explicitp = !! declspecs->specs[(int)ds_explicit];
8326 storage_class = declspecs->storage_class;
8327 if (storage_class == sc_static)
8328 staticp = 1 + (decl_context == FIELD);
8330 if (virtualp && staticp == 2)
8332 error ("member %qD cannot be declared both virtual and static", dname);
8333 storage_class = sc_none;
8334 staticp = 0;
8336 friendp = !! declspecs->specs[(int)ds_friend];
8338 if (dependent_name && !friendp)
8340 error ("%<%T::%D%> is not a valid declarator", ctype, dependent_name);
8341 return error_mark_node;
8344 /* Issue errors about use of storage classes for parameters. */
8345 if (decl_context == PARM)
8347 if (declspecs->specs[(int)ds_typedef])
8349 error ("typedef declaration invalid in parameter declaration");
8350 return error_mark_node;
8352 else if (template_parm_flag && storage_class != sc_none)
8354 error ("storage class specified for template parameter %qs", name);
8355 return error_mark_node;
8357 else if (storage_class == sc_static
8358 || storage_class == sc_extern
8359 || thread_p)
8360 error ("storage class specifiers invalid in parameter declarations");
8362 if (type_uses_auto (type))
8364 error ("parameter declared %<auto%>");
8365 type = error_mark_node;
8368 /* Function parameters cannot be constexpr. If we saw one, moan
8369 and pretend it wasn't there. */
8370 if (constexpr_p)
8372 error ("a parameter cannot be declared %<constexpr%>");
8373 constexpr_p = 0;
8377 /* Give error if `virtual' is used outside of class declaration. */
8378 if (virtualp
8379 && (current_class_name == NULL_TREE || decl_context != FIELD))
8381 error ("%<virtual%> outside class declaration");
8382 virtualp = 0;
8385 /* Static anonymous unions are dealt with here. */
8386 if (staticp && decl_context == TYPENAME
8387 && declspecs->type
8388 && ANON_AGGR_TYPE_P (declspecs->type))
8389 decl_context = FIELD;
8391 /* Warn about storage classes that are invalid for certain
8392 kinds of declarations (parameters, typenames, etc.). */
8393 if (thread_p
8394 && ((storage_class
8395 && storage_class != sc_extern
8396 && storage_class != sc_static)
8397 || declspecs->specs[(int)ds_typedef]))
8399 error ("multiple storage classes in declaration of %qs", name);
8400 thread_p = false;
8402 if (decl_context != NORMAL
8403 && ((storage_class != sc_none
8404 && storage_class != sc_mutable)
8405 || thread_p))
8407 if ((decl_context == PARM || decl_context == CATCHPARM)
8408 && (storage_class == sc_register
8409 || storage_class == sc_auto))
8411 else if (declspecs->specs[(int)ds_typedef])
8413 else if (decl_context == FIELD
8414 /* C++ allows static class elements. */
8415 && storage_class == sc_static)
8416 /* C++ also allows inlines and signed and unsigned elements,
8417 but in those cases we don't come in here. */
8419 else
8421 if (decl_context == FIELD)
8422 error ("storage class specified for %qs", name);
8423 else
8425 if (decl_context == PARM || decl_context == CATCHPARM)
8426 error ("storage class specified for parameter %qs", name);
8427 else
8428 error ("storage class specified for typename");
8430 if (storage_class == sc_register
8431 || storage_class == sc_auto
8432 || storage_class == sc_extern
8433 || thread_p)
8434 storage_class = sc_none;
8437 else if (storage_class == sc_extern && funcdef_flag
8438 && ! toplevel_bindings_p ())
8439 error ("nested function %qs declared %<extern%>", name);
8440 else if (toplevel_bindings_p ())
8442 if (storage_class == sc_auto)
8443 error ("top-level declaration of %qs specifies %<auto%>", name);
8445 else if (thread_p
8446 && storage_class != sc_extern
8447 && storage_class != sc_static)
8449 error ("function-scope %qs implicitly auto and declared %<__thread%>",
8450 name);
8451 thread_p = false;
8454 if (storage_class && friendp)
8456 error ("storage class specifiers invalid in friend function declarations");
8457 storage_class = sc_none;
8458 staticp = 0;
8461 if (!id_declarator)
8462 unqualified_id = NULL_TREE;
8463 else
8465 unqualified_id = id_declarator->u.id.unqualified_name;
8466 switch (TREE_CODE (unqualified_id))
8468 case BIT_NOT_EXPR:
8469 unqualified_id = TREE_OPERAND (unqualified_id, 0);
8470 if (TYPE_P (unqualified_id))
8471 unqualified_id = constructor_name (unqualified_id);
8472 break;
8474 case IDENTIFIER_NODE:
8475 case TEMPLATE_ID_EXPR:
8476 break;
8478 default:
8479 gcc_unreachable ();
8483 /* Determine the type of the entity declared by recurring on the
8484 declarator. */
8485 for (; declarator; declarator = declarator->declarator)
8487 const cp_declarator *inner_declarator;
8488 tree attrs;
8490 if (type == error_mark_node)
8491 return error_mark_node;
8493 attrs = declarator->attributes;
8494 if (attrs)
8496 int attr_flags;
8498 attr_flags = 0;
8499 if (declarator == NULL || declarator->kind == cdk_id)
8500 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
8501 if (declarator->kind == cdk_function)
8502 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
8503 if (declarator->kind == cdk_array)
8504 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
8505 returned_attrs = decl_attributes (&type,
8506 chainon (returned_attrs, attrs),
8507 attr_flags);
8510 if (declarator->kind == cdk_id)
8511 break;
8513 inner_declarator = declarator->declarator;
8515 switch (declarator->kind)
8517 case cdk_array:
8518 type = create_array_type_for_decl (dname, type,
8519 declarator->u.array.bounds);
8520 break;
8522 case cdk_function:
8524 tree arg_types;
8525 int funcdecl_p;
8527 /* Declaring a function type.
8528 Make sure we have a valid type for the function to return. */
8530 if (type_quals != TYPE_UNQUALIFIED)
8532 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
8533 warning (OPT_Wignored_qualifiers,
8534 "type qualifiers ignored on function return type");
8535 /* We now know that the TYPE_QUALS don't apply to the
8536 decl, but to its return type. */
8537 type_quals = TYPE_UNQUALIFIED;
8539 errmsg = targetm.invalid_return_type (type);
8540 if (errmsg)
8542 error (errmsg);
8543 type = integer_type_node;
8546 /* Error about some types functions can't return. */
8548 if (TREE_CODE (type) == FUNCTION_TYPE)
8550 error ("%qs declared as function returning a function", name);
8551 return error_mark_node;
8553 if (TREE_CODE (type) == ARRAY_TYPE)
8555 error ("%qs declared as function returning an array", name);
8556 return error_mark_node;
8559 /* Pick up type qualifiers which should be applied to `this'. */
8560 memfn_quals = declarator->u.function.qualifiers;
8562 /* Pick up the exception specifications. */
8563 raises = declarator->u.function.exception_specification;
8565 /* Say it's a definition only for the CALL_EXPR
8566 closest to the identifier. */
8567 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
8569 /* Handle a late-specified return type. */
8570 if (funcdecl_p)
8572 if (type_uses_auto (type))
8574 if (!declarator->u.function.late_return_type)
8576 error ("%qs function uses %<auto%> type specifier without"
8577 " late return type", name);
8578 return error_mark_node;
8580 else if (!is_auto (type))
8582 error ("%qs function with late return type has"
8583 " %qT as its type rather than plain %<auto%>",
8584 name, type);
8585 return error_mark_node;
8588 else if (declarator->u.function.late_return_type)
8590 error ("%qs function with late return type not declared"
8591 " with %<auto%> type specifier", name);
8592 return error_mark_node;
8595 type = splice_late_return_type
8596 (type, declarator->u.function.late_return_type);
8597 if (type == error_mark_node)
8598 return error_mark_node;
8600 if (ctype == NULL_TREE
8601 && decl_context == FIELD
8602 && funcdecl_p
8603 && (friendp == 0 || dname == current_class_name))
8604 ctype = current_class_type;
8606 if (ctype && (sfk == sfk_constructor
8607 || sfk == sfk_destructor))
8609 /* We are within a class's scope. If our declarator name
8610 is the same as the class name, and we are defining
8611 a function, then it is a constructor/destructor, and
8612 therefore returns a void type. */
8614 /* ISO C++ 12.4/2. A destructor may not be declared
8615 const or volatile. A destructor may not be
8616 static.
8618 ISO C++ 12.1. A constructor may not be declared
8619 const or volatile. A constructor may not be
8620 virtual. A constructor may not be static. */
8621 if (staticp == 2)
8622 error ((flags == DTOR_FLAG)
8623 ? "destructor cannot be static member function"
8624 : "constructor cannot be static member function");
8625 if (memfn_quals)
8627 error ((flags == DTOR_FLAG)
8628 ? "destructors may not be cv-qualified"
8629 : "constructors may not be cv-qualified");
8630 memfn_quals = TYPE_UNQUALIFIED;
8633 if (decl_context == FIELD
8634 && !member_function_or_else (ctype,
8635 current_class_type,
8636 flags))
8637 return error_mark_node;
8639 if (flags != DTOR_FLAG)
8641 /* It's a constructor. */
8642 if (explicitp == 1)
8643 explicitp = 2;
8644 if (virtualp)
8646 permerror (input_location, "constructors cannot be declared virtual");
8647 virtualp = 0;
8649 if (decl_context == FIELD
8650 && sfk != sfk_constructor)
8651 return error_mark_node;
8653 if (decl_context == FIELD)
8654 staticp = 0;
8656 else if (friendp)
8658 if (initialized)
8659 error ("can't initialize friend function %qs", name);
8660 if (virtualp)
8662 /* Cannot be both friend and virtual. */
8663 error ("virtual functions cannot be friends");
8664 friendp = 0;
8666 if (decl_context == NORMAL)
8667 error ("friend declaration not in class definition");
8668 if (current_function_decl && funcdef_flag)
8669 error ("can't define friend function %qs in a local "
8670 "class definition",
8671 name);
8673 else if (ctype && sfk == sfk_conversion)
8675 if (explicitp == 1)
8677 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
8678 explicitp = 2;
8682 /* It is not allowed to use `constexpr' in a function
8683 declaration that is not a definition.
8684 That is too strict, though. */
8685 if (constexpr_p && !funcdef_flag)
8687 error ("the %<constexpr%> specifier cannot be used in "
8688 "a function declaration that is not a definition");
8689 constexpr_p = false;
8692 /* A constexpr non-static member function is implicitly const. */
8693 if (constexpr_p && decl_context == FIELD && staticp == 0
8694 && sfk != sfk_constructor && sfk != sfk_destructor)
8695 memfn_quals |= TYPE_QUAL_CONST;
8697 arg_types = grokparms (declarator->u.function.parameters,
8698 &parms);
8700 if (inner_declarator
8701 && inner_declarator->kind == cdk_id
8702 && inner_declarator->u.id.sfk == sfk_destructor
8703 && arg_types != void_list_node)
8705 error ("destructors may not have parameters");
8706 arg_types = void_list_node;
8707 parms = NULL_TREE;
8710 type = build_function_type (type, arg_types);
8712 break;
8714 case cdk_pointer:
8715 case cdk_reference:
8716 case cdk_ptrmem:
8717 /* Filter out pointers-to-references and references-to-references.
8718 We can get these if a TYPE_DECL is used. */
8720 if (TREE_CODE (type) == REFERENCE_TYPE)
8722 if (declarator->kind != cdk_reference)
8724 error ("cannot declare pointer to %q#T", type);
8725 type = TREE_TYPE (type);
8728 /* In C++0x, we allow reference to reference declarations
8729 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
8730 and template type arguments [14.3.1/4 temp.arg.type]. The
8731 check for direct reference to reference declarations, which
8732 are still forbidden, occurs below. Reasoning behind the change
8733 can be found in DR106, DR540, and the rvalue reference
8734 proposals. */
8735 else if (cxx_dialect == cxx98)
8737 error ("cannot declare reference to %q#T", type);
8738 type = TREE_TYPE (type);
8741 else if (VOID_TYPE_P (type))
8743 if (declarator->kind == cdk_reference)
8744 error ("cannot declare reference to %q#T", type);
8745 else if (declarator->kind == cdk_ptrmem)
8746 error ("cannot declare pointer to %q#T member", type);
8749 /* We now know that the TYPE_QUALS don't apply to the decl,
8750 but to the target of the pointer. */
8751 type_quals = TYPE_UNQUALIFIED;
8753 if (declarator->kind == cdk_ptrmem
8754 && (TREE_CODE (type) == FUNCTION_TYPE
8755 || (memfn_quals && TREE_CODE (type) == METHOD_TYPE)))
8757 memfn_quals |= type_memfn_quals (type);
8758 type = build_memfn_type (type,
8759 declarator->u.pointer.class_type,
8760 memfn_quals);
8761 memfn_quals = TYPE_UNQUALIFIED;
8764 if (TREE_CODE (type) == FUNCTION_TYPE
8765 && type_memfn_quals (type) != TYPE_UNQUALIFIED)
8766 error (declarator->kind == cdk_reference
8767 ? G_("cannot declare reference to qualified function type %qT")
8768 : G_("cannot declare pointer to qualified function type %qT"),
8769 type);
8771 /* When the pointed-to type involves components of variable size,
8772 care must be taken to ensure that the size evaluation code is
8773 emitted early enough to dominate all the possible later uses
8774 and late enough for the variables on which it depends to have
8775 been assigned.
8777 This is expected to happen automatically when the pointed-to
8778 type has a name/declaration of it's own, but special attention
8779 is required if the type is anonymous.
8781 We handle the NORMAL and FIELD contexts here by inserting a
8782 dummy statement that just evaluates the size at a safe point
8783 and ensures it is not deferred until e.g. within a deeper
8784 conditional context (c++/43555).
8786 We expect nothing to be needed here for PARM or TYPENAME.
8787 Evaluating the size at this point for TYPENAME would
8788 actually be incorrect, as we might be in the middle of an
8789 expression with side effects on the pointed-to type size
8790 "arguments" prior to the pointer declaration point and the
8791 size evaluation could end up prior to the side effects. */
8793 if (!TYPE_NAME (type)
8794 && (decl_context == NORMAL || decl_context == FIELD)
8795 && at_function_scope_p ()
8796 && variably_modified_type_p (type, NULL_TREE))
8797 finish_expr_stmt (TYPE_SIZE (type));
8799 if (declarator->kind == cdk_reference)
8801 /* In C++0x, the type we are creating a reference to might be
8802 a typedef which is itself a reference type. In that case,
8803 we follow the reference collapsing rules in
8804 [7.1.3/8 dcl.typedef] to create the final reference type:
8806 "If a typedef TD names a type that is a reference to a type
8807 T, an attempt to create the type 'lvalue reference to cv TD'
8808 creates the type 'lvalue reference to T,' while an attempt
8809 to create the type "rvalue reference to cv TD' creates the
8810 type TD."
8812 if (!VOID_TYPE_P (type))
8813 type = cp_build_reference_type
8814 ((TREE_CODE (type) == REFERENCE_TYPE
8815 ? TREE_TYPE (type) : type),
8816 (declarator->u.reference.rvalue_ref
8817 && (TREE_CODE(type) != REFERENCE_TYPE
8818 || TYPE_REF_IS_RVALUE (type))));
8820 /* In C++0x, we need this check for direct reference to
8821 reference declarations, which are forbidden by
8822 [8.3.2/5 dcl.ref]. Reference to reference declarations
8823 are only allowed indirectly through typedefs and template
8824 type arguments. Example:
8826 void foo(int & &); // invalid ref-to-ref decl
8828 typedef int & int_ref;
8829 void foo(int_ref &); // valid ref-to-ref decl
8831 if (inner_declarator && inner_declarator->kind == cdk_reference)
8832 error ("cannot declare reference to %q#T, which is not "
8833 "a typedef or a template type argument", type);
8835 else if (TREE_CODE (type) == METHOD_TYPE)
8836 type = build_ptrmemfunc_type (build_pointer_type (type));
8837 else if (declarator->kind == cdk_ptrmem)
8839 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
8840 != NAMESPACE_DECL);
8841 if (declarator->u.pointer.class_type == error_mark_node)
8842 /* We will already have complained. */
8843 type = error_mark_node;
8844 else
8845 type = build_ptrmem_type (declarator->u.pointer.class_type,
8846 type);
8848 else
8849 type = build_pointer_type (type);
8851 /* Process a list of type modifier keywords (such as
8852 const or volatile) that were given inside the `*' or `&'. */
8854 if (declarator->u.pointer.qualifiers)
8856 type
8857 = cp_build_qualified_type (type,
8858 declarator->u.pointer.qualifiers);
8859 type_quals = cp_type_quals (type);
8861 ctype = NULL_TREE;
8862 break;
8864 case cdk_error:
8865 break;
8867 default:
8868 gcc_unreachable ();
8872 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
8873 && TREE_CODE (type) != FUNCTION_TYPE
8874 && TREE_CODE (type) != METHOD_TYPE)
8876 error ("template-id %qD used as a declarator",
8877 unqualified_id);
8878 unqualified_id = dname;
8881 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
8882 qualified with a class-name, turn it into a METHOD_TYPE, unless
8883 we know that the function is static. We take advantage of this
8884 opportunity to do other processing that pertains to entities
8885 explicitly declared to be class members. Note that if DECLARATOR
8886 is non-NULL, we know it is a cdk_id declarator; otherwise, we
8887 would not have exited the loop above. */
8888 if (declarator
8889 && declarator->u.id.qualifying_scope
8890 && TYPE_P (declarator->u.id.qualifying_scope))
8892 tree t;
8894 ctype = declarator->u.id.qualifying_scope;
8895 ctype = TYPE_MAIN_VARIANT (ctype);
8896 t = ctype;
8897 while (t != NULL_TREE && CLASS_TYPE_P (t))
8899 /* You're supposed to have one `template <...>' for every
8900 template class, but you don't need one for a full
8901 specialization. For example:
8903 template <class T> struct S{};
8904 template <> struct S<int> { void f(); };
8905 void S<int>::f () {}
8907 is correct; there shouldn't be a `template <>' for the
8908 definition of `S<int>::f'. */
8909 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)
8910 && !any_dependent_template_arguments_p (CLASSTYPE_TI_ARGS (t)))
8911 /* T is an explicit (not partial) specialization. All
8912 containing classes must therefore also be explicitly
8913 specialized. */
8914 break;
8915 if ((CLASSTYPE_USE_TEMPLATE (t) || CLASSTYPE_IS_TEMPLATE (t))
8916 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
8917 template_count += 1;
8919 t = TYPE_MAIN_DECL (t);
8920 t = DECL_CONTEXT (t);
8923 if (ctype == current_class_type)
8925 if (friendp)
8927 permerror (input_location, "member functions are implicitly friends of their class");
8928 friendp = 0;
8930 else
8931 permerror (declarator->id_loc,
8932 "extra qualification %<%T::%> on member %qs",
8933 ctype, name);
8935 else if (/* If the qualifying type is already complete, then we
8936 can skip the following checks. */
8937 !COMPLETE_TYPE_P (ctype)
8938 && (/* If the function is being defined, then
8939 qualifying type must certainly be complete. */
8940 funcdef_flag
8941 /* A friend declaration of "T::f" is OK, even if
8942 "T" is a template parameter. But, if this
8943 function is not a friend, the qualifying type
8944 must be a class. */
8945 || (!friendp && !CLASS_TYPE_P (ctype))
8946 /* For a declaration, the type need not be
8947 complete, if either it is dependent (since there
8948 is no meaningful definition of complete in that
8949 case) or the qualifying class is currently being
8950 defined. */
8951 || !(dependent_type_p (ctype)
8952 || currently_open_class (ctype)))
8953 /* Check that the qualifying type is complete. */
8954 && !complete_type_or_else (ctype, NULL_TREE))
8955 return error_mark_node;
8956 else if (TREE_CODE (type) == FUNCTION_TYPE)
8958 tree sname = declarator->u.id.unqualified_name;
8960 if (current_class_type
8961 && (!friendp || funcdef_flag))
8963 error (funcdef_flag
8964 ? "cannot define member function %<%T::%s%> within %<%T%>"
8965 : "cannot declare member function %<%T::%s%> within %<%T%>",
8966 ctype, name, current_class_type);
8967 return error_mark_node;
8970 /* It is not permitted to define a member function outside ist
8971 membership class as `constexpr'. */
8972 if (constexpr_p)
8973 error ("a constexpr function cannot be defined "
8974 "outside of its class");
8976 if (TREE_CODE (sname) == IDENTIFIER_NODE
8977 && NEW_DELETE_OPNAME_P (sname))
8978 /* Overloaded operator new and operator delete
8979 are always static functions. */
8981 else
8982 type = build_memfn_type (type, ctype, memfn_quals);
8984 else if (declspecs->specs[(int)ds_typedef]
8985 && current_class_type)
8987 error ("cannot declare member %<%T::%s%> within %qT",
8988 ctype, name, current_class_type);
8989 return error_mark_node;
8993 /* Now TYPE has the actual type. */
8995 if (returned_attrs)
8997 if (attrlist)
8998 *attrlist = chainon (returned_attrs, *attrlist);
8999 else
9000 attrlist = &returned_attrs;
9003 /* Handle parameter packs. */
9004 if (parameter_pack_p)
9006 if (decl_context == PARM)
9007 /* Turn the type into a pack expansion.*/
9008 type = make_pack_expansion (type);
9009 else
9010 error ("non-parameter %qs cannot be a parameter pack", name);
9013 /* Did array size calculations overflow? */
9015 if (TREE_CODE (type) == ARRAY_TYPE
9016 && COMPLETE_TYPE_P (type)
9017 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
9018 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
9020 error ("size of array %qs is too large", name);
9021 /* If we proceed with the array type as it is, we'll eventually
9022 crash in tree_low_cst(). */
9023 type = error_mark_node;
9026 if ((decl_context == FIELD || decl_context == PARM)
9027 && !processing_template_decl
9028 && variably_modified_type_p (type, NULL_TREE))
9030 if (decl_context == FIELD)
9031 error ("data member may not have variably modified type %qT", type);
9032 else
9033 error ("parameter may not have variably modified type %qT", type);
9034 type = error_mark_node;
9037 if (explicitp == 1 || (explicitp && friendp))
9039 /* [dcl.fct.spec] The explicit specifier shall only be used in
9040 declarations of constructors within a class definition. */
9041 error ("only declarations of constructors can be %<explicit%>");
9042 explicitp = 0;
9045 if (storage_class == sc_mutable)
9047 if (decl_context != FIELD || friendp)
9049 error ("non-member %qs cannot be declared %<mutable%>", name);
9050 storage_class = sc_none;
9052 else if (decl_context == TYPENAME || declspecs->specs[(int)ds_typedef])
9054 error ("non-object member %qs cannot be declared %<mutable%>", name);
9055 storage_class = sc_none;
9057 else if (TREE_CODE (type) == FUNCTION_TYPE
9058 || TREE_CODE (type) == METHOD_TYPE)
9060 error ("function %qs cannot be declared %<mutable%>", name);
9061 storage_class = sc_none;
9063 else if (staticp)
9065 error ("static %qs cannot be declared %<mutable%>", name);
9066 storage_class = sc_none;
9068 else if (type_quals & TYPE_QUAL_CONST)
9070 error ("const %qs cannot be declared %<mutable%>", name);
9071 storage_class = sc_none;
9075 /* If this is declaring a typedef name, return a TYPE_DECL. */
9076 if (declspecs->specs[(int)ds_typedef] && decl_context != TYPENAME)
9078 tree decl;
9080 /* Note that the grammar rejects storage classes
9081 in typenames, fields or parameters. */
9082 if (current_lang_name == lang_name_java)
9083 TYPE_FOR_JAVA (type) = 1;
9085 /* This declaration:
9087 typedef void f(int) const;
9089 declares a function type which is not a member of any
9090 particular class, but which is cv-qualified; for
9091 example "f S::*" declares a pointer to a const-qualified
9092 member function of S. We record the cv-qualification in the
9093 function type. */
9094 if (memfn_quals && TREE_CODE (type) == FUNCTION_TYPE)
9096 type = apply_memfn_quals (type, memfn_quals);
9098 /* We have now dealt with these qualifiers. */
9099 memfn_quals = TYPE_UNQUALIFIED;
9102 if (decl_context == FIELD)
9103 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
9104 else
9105 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
9106 if (id_declarator && declarator->u.id.qualifying_scope) {
9107 error_at (DECL_SOURCE_LOCATION (decl),
9108 "typedef name may not be a nested-name-specifier");
9109 TREE_TYPE (decl) = error_mark_node;
9112 if (decl_context != FIELD)
9114 if (!current_function_decl)
9115 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
9116 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
9117 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
9118 (current_function_decl)))
9119 /* The TYPE_DECL is "abstract" because there will be
9120 clones of this constructor/destructor, and there will
9121 be copies of this TYPE_DECL generated in those
9122 clones. */
9123 DECL_ABSTRACT (decl) = 1;
9125 else if (constructor_name_p (unqualified_id, current_class_type))
9126 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
9127 "as enclosing class",
9128 unqualified_id);
9130 /* If the user declares "typedef struct {...} foo" then the
9131 struct will have an anonymous name. Fill that name in now.
9132 Nothing can refer to it, so nothing needs know about the name
9133 change. */
9134 if (type != error_mark_node
9135 && unqualified_id
9136 && TYPE_NAME (type)
9137 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9138 && TYPE_ANONYMOUS_P (type)
9139 && cp_type_quals (type) == TYPE_UNQUALIFIED)
9141 tree t;
9143 /* Replace the anonymous name with the real name everywhere. */
9144 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9146 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
9147 /* We do not rename the debug info representing the
9148 anonymous tagged type because the standard says in
9149 [dcl.typedef] that the naming applies only for
9150 linkage purposes. */
9151 /*debug_hooks->set_name (t, decl);*/
9152 TYPE_NAME (t) = decl;
9155 if (TYPE_LANG_SPECIFIC (type))
9156 TYPE_WAS_ANONYMOUS (type) = 1;
9158 /* If this is a typedef within a template class, the nested
9159 type is a (non-primary) template. The name for the
9160 template needs updating as well. */
9161 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
9162 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
9163 = TYPE_IDENTIFIER (type);
9165 /* Adjust linkage now that we aren't anonymous anymore. */
9166 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
9167 determine_visibility (TYPE_MAIN_DECL (type));
9169 /* FIXME remangle member functions; member functions of a
9170 type with external linkage have external linkage. */
9173 if (signed_p
9174 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
9175 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
9177 bad_specifiers (decl, BSP_TYPE, virtualp,
9178 memfn_quals != TYPE_UNQUALIFIED,
9179 inlinep, friendp, raises != NULL_TREE);
9181 return decl;
9184 /* Detect the case of an array type of unspecified size
9185 which came, as such, direct from a typedef name.
9186 We must copy the type, so that the array's domain can be
9187 individually set by the object's initializer. */
9189 if (type && typedef_type
9190 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9191 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
9192 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
9194 /* Detect where we're using a typedef of function type to declare a
9195 function. PARMS will not be set, so we must create it now. */
9197 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
9199 tree decls = NULL_TREE;
9200 tree args;
9202 for (args = TYPE_ARG_TYPES (type);
9203 args && args != void_list_node;
9204 args = TREE_CHAIN (args))
9206 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
9208 DECL_CHAIN (decl) = decls;
9209 decls = decl;
9212 parms = nreverse (decls);
9214 if (decl_context != TYPENAME)
9216 /* A cv-qualifier-seq shall only be part of the function type
9217 for a non-static member function. [8.3.5/4 dcl.fct] */
9218 if (type_memfn_quals (type) != TYPE_UNQUALIFIED
9219 && (current_class_type == NULL_TREE || staticp) )
9221 error (staticp
9222 ? G_("qualified function types cannot be used to "
9223 "declare static member functions")
9224 : G_("qualified function types cannot be used to "
9225 "declare free functions"));
9226 type = TYPE_MAIN_VARIANT (type);
9229 /* The qualifiers on the function type become the qualifiers on
9230 the non-static member function. */
9231 memfn_quals |= type_memfn_quals (type);
9232 type_quals = TYPE_UNQUALIFIED;
9236 /* If this is a type name (such as, in a cast or sizeof),
9237 compute the type and return it now. */
9239 if (decl_context == TYPENAME)
9241 /* Note that the grammar rejects storage classes
9242 in typenames, fields or parameters. */
9243 if (type_quals != TYPE_UNQUALIFIED)
9244 type_quals = TYPE_UNQUALIFIED;
9246 /* Special case: "friend class foo" looks like a TYPENAME context. */
9247 if (friendp)
9249 if (type_quals != TYPE_UNQUALIFIED)
9251 error ("type qualifiers specified for friend class declaration");
9252 type_quals = TYPE_UNQUALIFIED;
9254 if (inlinep)
9256 error ("%<inline%> specified for friend class declaration");
9257 inlinep = 0;
9260 if (!current_aggr)
9262 /* Don't allow friend declaration without a class-key. */
9263 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
9264 permerror (input_location, "template parameters cannot be friends");
9265 else if (TREE_CODE (type) == TYPENAME_TYPE)
9266 permerror (input_location, "friend declaration requires class-key, "
9267 "i.e. %<friend class %T::%D%>",
9268 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
9269 else
9270 permerror (input_location, "friend declaration requires class-key, "
9271 "i.e. %<friend %#T%>",
9272 type);
9275 /* Only try to do this stuff if we didn't already give up. */
9276 if (type != integer_type_node)
9278 /* A friendly class? */
9279 if (current_class_type)
9280 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
9281 /*complain=*/true);
9282 else
9283 error ("trying to make class %qT a friend of global scope",
9284 type);
9286 type = void_type_node;
9289 else if (memfn_quals)
9291 if (ctype == NULL_TREE
9292 && TREE_CODE (type) == METHOD_TYPE)
9293 ctype = TYPE_METHOD_BASETYPE (type);
9295 if (ctype)
9296 type = build_memfn_type (type, ctype, memfn_quals);
9297 /* Core issue #547: need to allow this in template type args. */
9298 else if (template_type_arg && TREE_CODE (type) == FUNCTION_TYPE)
9299 type = apply_memfn_quals (type, memfn_quals);
9300 else
9301 error ("invalid qualifiers on non-member function type");
9304 return type;
9306 else if (unqualified_id == NULL_TREE && decl_context != PARM
9307 && decl_context != CATCHPARM
9308 && TREE_CODE (type) != UNION_TYPE
9309 && ! bitfield)
9311 error ("abstract declarator %qT used as declaration", type);
9312 return error_mark_node;
9315 /* Only functions may be declared using an operator-function-id. */
9316 if (unqualified_id
9317 && IDENTIFIER_OPNAME_P (unqualified_id)
9318 && TREE_CODE (type) != FUNCTION_TYPE
9319 && TREE_CODE (type) != METHOD_TYPE)
9321 error ("declaration of %qD as non-function", unqualified_id);
9322 return error_mark_node;
9325 /* We don't check parameter types here because we can emit a better
9326 error message later. */
9327 if (decl_context != PARM)
9329 type = check_var_type (unqualified_id, type);
9330 if (type == error_mark_node)
9331 return error_mark_node;
9334 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
9335 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
9337 if (decl_context == PARM || decl_context == CATCHPARM)
9339 if (ctype || in_namespace)
9340 error ("cannot use %<::%> in parameter declaration");
9342 /* A parameter declared as an array of T is really a pointer to T.
9343 One declared as a function is really a pointer to a function.
9344 One declared as a member is really a pointer to member. */
9346 if (TREE_CODE (type) == ARRAY_TYPE)
9348 /* Transfer const-ness of array into that of type pointed to. */
9349 type = build_pointer_type (TREE_TYPE (type));
9350 type_quals = TYPE_UNQUALIFIED;
9352 else if (TREE_CODE (type) == FUNCTION_TYPE)
9353 type = build_pointer_type (type);
9357 tree decl;
9359 if (decl_context == PARM)
9361 decl = cp_build_parm_decl (unqualified_id, type);
9363 bad_specifiers (decl, BSP_PARM, virtualp,
9364 memfn_quals != TYPE_UNQUALIFIED,
9365 inlinep, friendp, raises != NULL_TREE);
9367 else if (decl_context == FIELD)
9369 /* The C99 flexible array extension. */
9370 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
9371 && TYPE_DOMAIN (type) == NULL_TREE)
9373 tree itype = compute_array_index_type (dname, integer_zero_node);
9374 type = build_cplus_array_type (TREE_TYPE (type), itype);
9377 if (type == error_mark_node)
9379 /* Happens when declaring arrays of sizes which
9380 are error_mark_node, for example. */
9381 decl = NULL_TREE;
9383 else if (in_namespace && !friendp)
9385 /* Something like struct S { int N::j; }; */
9386 error ("invalid use of %<::%>");
9387 return error_mark_node;
9389 else if (TREE_CODE (type) == FUNCTION_TYPE)
9391 int publicp = 0;
9392 tree function_context;
9394 if (friendp == 0)
9396 if (ctype == NULL_TREE)
9397 ctype = current_class_type;
9399 if (ctype == NULL_TREE)
9401 error ("can't make %qD into a method -- not in a class",
9402 unqualified_id);
9403 return error_mark_node;
9406 /* ``A union may [ ... ] not [ have ] virtual functions.''
9407 ARM 9.5 */
9408 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
9410 error ("function %qD declared virtual inside a union",
9411 unqualified_id);
9412 return error_mark_node;
9415 if (NEW_DELETE_OPNAME_P (unqualified_id))
9417 if (virtualp)
9419 error ("%qD cannot be declared virtual, since it "
9420 "is always static",
9421 unqualified_id);
9422 virtualp = 0;
9425 else if (staticp < 2)
9426 type = build_memfn_type (type, ctype, memfn_quals);
9429 /* Check that the name used for a destructor makes sense. */
9430 if (sfk == sfk_destructor)
9432 tree uqname = id_declarator->u.id.unqualified_name;
9434 if (!ctype)
9436 gcc_assert (friendp);
9437 error ("expected qualified name in friend declaration "
9438 "for destructor %qD", uqname);
9439 return error_mark_node;
9442 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
9444 error ("declaration of %qD as member of %qT",
9445 uqname, ctype);
9446 return error_mark_node;
9448 if (constexpr_p)
9449 error ("a destructor cannot be %<constexpr%>");
9451 else if (sfk == sfk_constructor && friendp)
9453 error ("expected qualified name in friend declaration "
9454 "for constructor %qD",
9455 id_declarator->u.id.unqualified_name);
9456 return error_mark_node;
9459 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
9460 function_context = (ctype != NULL_TREE) ?
9461 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
9462 publicp = (! friendp || ! staticp)
9463 && function_context == NULL_TREE;
9464 decl = grokfndecl (ctype, type,
9465 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
9466 ? unqualified_id : dname,
9467 parms,
9468 unqualified_id,
9469 virtualp, flags, memfn_quals, raises,
9470 friendp ? -1 : 0, friendp, publicp,
9471 inlinep || constexpr_p,
9472 sfk,
9473 funcdef_flag, template_count, in_namespace,
9474 attrlist, declarator->id_loc);
9475 if (decl == NULL_TREE)
9476 return error_mark_node;
9477 #if 0
9478 /* This clobbers the attrs stored in `decl' from `attrlist'. */
9479 /* The decl and setting of decl_attr is also turned off. */
9480 decl = build_decl_attribute_variant (decl, decl_attr);
9481 #endif
9483 /* [class.conv.ctor]
9485 A constructor declared without the function-specifier
9486 explicit that can be called with a single parameter
9487 specifies a conversion from the type of its first
9488 parameter to the type of its class. Such a constructor
9489 is called a converting constructor. */
9490 if (explicitp == 2)
9491 DECL_NONCONVERTING_P (decl) = 1;
9493 else if (TREE_CODE (type) == METHOD_TYPE)
9495 /* We only get here for friend declarations of
9496 members of other classes. */
9497 /* All method decls are public, so tell grokfndecl to set
9498 TREE_PUBLIC, also. */
9499 decl = grokfndecl (ctype, type,
9500 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
9501 ? unqualified_id : dname,
9502 parms,
9503 unqualified_id,
9504 virtualp, flags, memfn_quals, raises,
9505 friendp ? -1 : 0, friendp, 1, 0, sfk,
9506 funcdef_flag, template_count, in_namespace,
9507 attrlist,
9508 declarator->id_loc);
9509 if (decl == NULL_TREE)
9510 return error_mark_node;
9512 else if (!staticp && !dependent_type_p (type)
9513 && !COMPLETE_TYPE_P (complete_type (type))
9514 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
9516 if (unqualified_id)
9517 error ("field %qD has incomplete type", unqualified_id);
9518 else
9519 error ("name %qT has incomplete type", type);
9521 /* If we're instantiating a template, tell them which
9522 instantiation made the field's type be incomplete. */
9523 if (current_class_type
9524 && TYPE_NAME (current_class_type)
9525 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
9526 && declspecs->type
9527 && declspecs->type == type)
9528 error (" in instantiation of template %qT",
9529 current_class_type);
9531 return error_mark_node;
9533 else
9535 if (friendp)
9537 error ("%qE is neither function nor member function; "
9538 "cannot be declared friend", unqualified_id);
9539 friendp = 0;
9541 decl = NULL_TREE;
9544 if (friendp)
9546 /* Friends are treated specially. */
9547 if (ctype == current_class_type)
9548 ; /* We already issued a permerror. */
9549 else if (decl && DECL_NAME (decl))
9551 if (template_class_depth (current_class_type) == 0)
9553 decl = check_explicit_specialization
9554 (unqualified_id, decl, template_count,
9555 2 * funcdef_flag + 4);
9556 if (decl == error_mark_node)
9557 return error_mark_node;
9560 DECL_DECLARED_CONSTEXPR_P (decl) = constexpr_p;
9561 decl = do_friend (ctype, unqualified_id, decl,
9562 *attrlist, flags,
9563 funcdef_flag);
9564 return decl;
9566 else
9567 return error_mark_node;
9570 /* Structure field. It may not be a function, except for C++. */
9572 if (decl == NULL_TREE)
9574 if (initialized)
9576 if (!staticp)
9578 /* An attempt is being made to initialize a non-static
9579 member. But, from [class.mem]:
9581 4 A member-declarator can contain a
9582 constant-initializer only if it declares a static
9583 member (_class.static_) of integral or enumeration
9584 type, see _class.static.data_.
9586 This used to be relatively common practice, but
9587 the rest of the compiler does not correctly
9588 handle the initialization unless the member is
9589 static so we make it static below. */
9590 permerror (input_location, "ISO C++ forbids initialization of member %qD",
9591 unqualified_id);
9592 permerror (input_location, "making %qD static", unqualified_id);
9593 staticp = 1;
9596 if (uses_template_parms (type))
9597 /* We'll check at instantiation time. */
9599 else if (check_static_variable_definition (unqualified_id,
9600 type))
9601 /* If we just return the declaration, crashes
9602 will sometimes occur. We therefore return
9603 void_type_node, as if this was a friend
9604 declaration, to cause callers to completely
9605 ignore this declaration. */
9606 return error_mark_node;
9609 if (staticp)
9611 /* C++ allows static class members. All other work
9612 for this is done by grokfield. */
9613 decl = build_lang_decl (VAR_DECL, unqualified_id, type);
9614 set_linkage_for_static_data_member (decl);
9615 /* Even if there is an in-class initialization, DECL
9616 is considered undefined until an out-of-class
9617 definition is provided. */
9618 DECL_EXTERNAL (decl) = 1;
9620 if (thread_p)
9621 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
9623 else
9625 if (constexpr_p)
9626 error ("non-static data member %qE declared %<constexpr%>",
9627 unqualified_id);
9628 decl = build_decl (input_location,
9629 FIELD_DECL, unqualified_id, type);
9630 DECL_NONADDRESSABLE_P (decl) = bitfield;
9631 if (bitfield && !unqualified_id)
9632 TREE_NO_WARNING (decl) = 1;
9634 if (storage_class == sc_mutable)
9636 DECL_MUTABLE_P (decl) = 1;
9637 storage_class = sc_none;
9641 bad_specifiers (decl, BSP_FIELD, virtualp,
9642 memfn_quals != TYPE_UNQUALIFIED,
9643 inlinep, friendp, raises != NULL_TREE);
9646 else if (TREE_CODE (type) == FUNCTION_TYPE
9647 || TREE_CODE (type) == METHOD_TYPE)
9649 tree original_name;
9650 int publicp = 0;
9652 if (!unqualified_id)
9653 return error_mark_node;
9655 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
9656 original_name = dname;
9657 else
9658 original_name = unqualified_id;
9660 if (storage_class == sc_auto)
9661 error ("storage class %<auto%> invalid for function %qs", name);
9662 else if (storage_class == sc_register)
9663 error ("storage class %<register%> invalid for function %qs", name);
9664 else if (thread_p)
9665 error ("storage class %<__thread%> invalid for function %qs", name);
9667 /* Function declaration not at top level.
9668 Storage classes other than `extern' are not allowed
9669 and `extern' makes no difference. */
9670 if (! toplevel_bindings_p ()
9671 && (storage_class == sc_static
9672 || declspecs->specs[(int)ds_inline])
9673 && pedantic)
9675 if (storage_class == sc_static)
9676 pedwarn (input_location, OPT_pedantic,
9677 "%<static%> specified invalid for function %qs "
9678 "declared out of global scope", name);
9679 else
9680 pedwarn (input_location, OPT_pedantic,
9681 "%<inline%> specifier invalid for function %qs "
9682 "declared out of global scope", name);
9685 if (ctype != NULL_TREE
9686 && TREE_CODE (ctype) != NAMESPACE_DECL && !MAYBE_CLASS_TYPE_P (ctype))
9688 error ("%q#T is not a class or a namespace", ctype);
9689 ctype = NULL_TREE;
9692 if (ctype == NULL_TREE)
9694 if (virtualp)
9696 error ("virtual non-class function %qs", name);
9697 virtualp = 0;
9699 else if (sfk == sfk_constructor
9700 || sfk == sfk_destructor)
9702 error (funcdef_flag
9703 ? "%qs defined in a non-class scope"
9704 : "%qs declared in a non-class scope", name);
9705 sfk = sfk_none;
9708 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
9709 && !NEW_DELETE_OPNAME_P (original_name))
9710 type = build_method_type_directly (ctype,
9711 TREE_TYPE (type),
9712 TYPE_ARG_TYPES (type));
9714 /* Record presence of `static'. */
9715 publicp = (ctype != NULL_TREE
9716 || storage_class == sc_extern
9717 || storage_class != sc_static);
9719 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
9720 virtualp, flags, memfn_quals, raises,
9721 1, friendp,
9722 publicp, inlinep || constexpr_p, sfk, funcdef_flag,
9723 template_count, in_namespace, attrlist,
9724 declarator->id_loc);
9725 if (decl == NULL_TREE)
9726 return error_mark_node;
9728 if (staticp == 1)
9730 int invalid_static = 0;
9732 /* Don't allow a static member function in a class, and forbid
9733 declaring main to be static. */
9734 if (TREE_CODE (type) == METHOD_TYPE)
9736 permerror (input_location, "cannot declare member function %qD to have "
9737 "static linkage", decl);
9738 invalid_static = 1;
9740 else if (current_function_decl)
9742 /* FIXME need arm citation */
9743 error ("cannot declare static function inside another function");
9744 invalid_static = 1;
9747 if (invalid_static)
9749 staticp = 0;
9750 storage_class = sc_none;
9754 else
9756 /* It's a variable. */
9758 /* An uninitialized decl with `extern' is a reference. */
9759 decl = grokvardecl (type, unqualified_id,
9760 declspecs,
9761 initialized,
9762 (type_quals & TYPE_QUAL_CONST) != 0,
9763 ctype ? ctype : in_namespace);
9764 bad_specifiers (decl, BSP_VAR, virtualp,
9765 memfn_quals != TYPE_UNQUALIFIED,
9766 inlinep, friendp, raises != NULL_TREE);
9768 if (ctype)
9770 DECL_CONTEXT (decl) = ctype;
9771 if (staticp == 1)
9773 permerror (input_location, "%<static%> may not be used when defining "
9774 "(as opposed to declaring) a static data member");
9775 staticp = 0;
9776 storage_class = sc_none;
9778 if (storage_class == sc_register && TREE_STATIC (decl))
9780 error ("static member %qD declared %<register%>", decl);
9781 storage_class = sc_none;
9783 if (storage_class == sc_extern && pedantic)
9785 pedwarn (input_location, OPT_pedantic,
9786 "cannot explicitly declare member %q#D to have "
9787 "extern linkage", decl);
9788 storage_class = sc_none;
9793 if (storage_class == sc_extern && initialized && !funcdef_flag)
9795 if (toplevel_bindings_p ())
9797 /* It's common practice (and completely valid) to have a const
9798 be initialized and declared extern. */
9799 if (!(type_quals & TYPE_QUAL_CONST))
9800 warning (0, "%qs initialized and declared %<extern%>", name);
9802 else
9804 error ("%qs has both %<extern%> and initializer", name);
9805 return error_mark_node;
9809 /* Record `register' declaration for warnings on &
9810 and in case doing stupid register allocation. */
9812 if (storage_class == sc_register)
9813 DECL_REGISTER (decl) = 1;
9814 else if (storage_class == sc_extern)
9815 DECL_THIS_EXTERN (decl) = 1;
9816 else if (storage_class == sc_static)
9817 DECL_THIS_STATIC (decl) = 1;
9819 /* Don't forget constexprness. */
9820 if (VAR_OR_FUNCTION_DECL_P (decl))
9821 DECL_DECLARED_CONSTEXPR_P (decl) = constexpr_p;
9823 /* Record constancy and volatility on the DECL itself . There's
9824 no need to do this when processing a template; we'll do this
9825 for the instantiated declaration based on the type of DECL. */
9826 if (!processing_template_decl)
9827 cp_apply_type_quals_to_decl (type_quals, decl);
9829 return decl;
9833 /* Subroutine of start_function. Ensure that each of the parameter
9834 types (as listed in PARMS) is complete, as is required for a
9835 function definition. */
9837 static void
9838 require_complete_types_for_parms (tree parms)
9840 for (; parms; parms = DECL_CHAIN (parms))
9842 if (dependent_type_p (TREE_TYPE (parms)))
9843 continue;
9844 if (!VOID_TYPE_P (TREE_TYPE (parms))
9845 && complete_type_or_else (TREE_TYPE (parms), parms))
9847 relayout_decl (parms);
9848 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
9850 else
9851 /* grokparms or complete_type_or_else will have already issued
9852 an error. */
9853 TREE_TYPE (parms) = error_mark_node;
9857 /* Returns nonzero if T is a local variable. */
9860 local_variable_p (const_tree t)
9862 if ((TREE_CODE (t) == VAR_DECL
9863 /* A VAR_DECL with a context that is a _TYPE is a static data
9864 member. */
9865 && !TYPE_P (CP_DECL_CONTEXT (t))
9866 /* Any other non-local variable must be at namespace scope. */
9867 && !DECL_NAMESPACE_SCOPE_P (t))
9868 || (TREE_CODE (t) == PARM_DECL))
9869 return 1;
9871 return 0;
9874 /* Like local_variable_p, but suitable for use as a tree-walking
9875 function. */
9877 static tree
9878 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
9879 void *data ATTRIBUTE_UNUSED)
9881 if (local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
9882 return *tp;
9883 else if (TYPE_P (*tp))
9884 *walk_subtrees = 0;
9886 return NULL_TREE;
9890 /* Check that ARG, which is a default-argument expression for a
9891 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
9892 something goes wrong. DECL may also be a _TYPE node, rather than a
9893 DECL, if there is no DECL available. */
9895 tree
9896 check_default_argument (tree decl, tree arg)
9898 tree var;
9899 tree decl_type;
9901 if (TREE_CODE (arg) == DEFAULT_ARG)
9902 /* We get a DEFAULT_ARG when looking at an in-class declaration
9903 with a default argument. Ignore the argument for now; we'll
9904 deal with it after the class is complete. */
9905 return arg;
9907 if (TYPE_P (decl))
9909 decl_type = decl;
9910 decl = NULL_TREE;
9912 else
9913 decl_type = TREE_TYPE (decl);
9915 if (arg == error_mark_node
9916 || decl == error_mark_node
9917 || TREE_TYPE (arg) == error_mark_node
9918 || decl_type == error_mark_node)
9919 /* Something already went wrong. There's no need to check
9920 further. */
9921 return error_mark_node;
9923 /* [dcl.fct.default]
9925 A default argument expression is implicitly converted to the
9926 parameter type. */
9927 if (!TREE_TYPE (arg)
9928 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg, LOOKUP_NORMAL))
9930 if (decl)
9931 error ("default argument for %q#D has type %qT",
9932 decl, TREE_TYPE (arg));
9933 else
9934 error ("default argument for parameter of type %qT has type %qT",
9935 decl_type, TREE_TYPE (arg));
9937 return error_mark_node;
9940 /* [dcl.fct.default]
9942 Local variables shall not be used in default argument
9943 expressions.
9945 The keyword `this' shall not be used in a default argument of a
9946 member function. */
9947 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
9948 if (var)
9950 error ("default argument %qE uses local variable %qD", arg, var);
9951 return error_mark_node;
9954 /* All is well. */
9955 return arg;
9958 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
9960 static tree
9961 type_is_deprecated (tree type)
9963 enum tree_code code;
9964 if (TREE_DEPRECATED (type))
9965 return type;
9966 if (TYPE_NAME (type)
9967 && TREE_DEPRECATED (TYPE_NAME (type)))
9968 return type;
9970 /* Do warn about using typedefs to a deprecated class. */
9971 if (TAGGED_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
9972 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
9974 code = TREE_CODE (type);
9976 if (code == POINTER_TYPE || code == REFERENCE_TYPE
9977 || code == OFFSET_TYPE || code == FUNCTION_TYPE
9978 || code == METHOD_TYPE || code == ARRAY_TYPE)
9979 return type_is_deprecated (TREE_TYPE (type));
9981 if (TYPE_PTRMEMFUNC_P (type))
9982 return type_is_deprecated
9983 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
9985 return NULL_TREE;
9988 /* Decode the list of parameter types for a function type.
9989 Given the list of things declared inside the parens,
9990 return a list of types.
9992 If this parameter does not end with an ellipsis, we append
9993 void_list_node.
9995 *PARMS is set to the chain of PARM_DECLs created. */
9997 static tree
9998 grokparms (tree parmlist, tree *parms)
10000 tree result = NULL_TREE;
10001 tree decls = NULL_TREE;
10002 tree parm;
10003 int any_error = 0;
10005 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
10007 tree type = NULL_TREE;
10008 tree init = TREE_PURPOSE (parm);
10009 tree decl = TREE_VALUE (parm);
10010 const char *errmsg;
10012 if (parm == void_list_node)
10013 break;
10015 if (! decl || TREE_TYPE (decl) == error_mark_node)
10016 continue;
10018 type = TREE_TYPE (decl);
10019 if (VOID_TYPE_P (type))
10021 if (same_type_p (type, void_type_node)
10022 && DECL_SELF_REFERENCE_P (type)
10023 && !DECL_NAME (decl) && !result && TREE_CHAIN (parm) == void_list_node)
10024 /* this is a parmlist of `(void)', which is ok. */
10025 break;
10026 cxx_incomplete_type_error (decl, type);
10027 /* It's not a good idea to actually create parameters of
10028 type `void'; other parts of the compiler assume that a
10029 void type terminates the parameter list. */
10030 type = error_mark_node;
10031 TREE_TYPE (decl) = error_mark_node;
10034 if (type != error_mark_node
10035 && TYPE_FOR_JAVA (type)
10036 && MAYBE_CLASS_TYPE_P (type))
10038 error ("parameter %qD has Java class type", decl);
10039 type = error_mark_node;
10040 TREE_TYPE (decl) = error_mark_node;
10041 init = NULL_TREE;
10044 if (type != error_mark_node
10045 && (errmsg = targetm.invalid_parameter_type (type)))
10047 error (errmsg);
10048 type = error_mark_node;
10049 TREE_TYPE (decl) = error_mark_node;
10052 if (type != error_mark_node)
10054 if (deprecated_state != DEPRECATED_SUPPRESS)
10056 tree deptype = type_is_deprecated (type);
10057 if (deptype)
10058 warn_deprecated_use (deptype, NULL_TREE);
10061 /* Top-level qualifiers on the parameters are
10062 ignored for function types. */
10063 type = cp_build_qualified_type (type, 0);
10064 if (TREE_CODE (type) == METHOD_TYPE)
10066 error ("parameter %qD invalidly declared method type", decl);
10067 type = build_pointer_type (type);
10068 TREE_TYPE (decl) = type;
10070 else if (abstract_virtuals_error (decl, type))
10071 any_error = 1; /* Seems like a good idea. */
10072 else if (POINTER_TYPE_P (type))
10074 /* [dcl.fct]/6, parameter types cannot contain pointers
10075 (references) to arrays of unknown bound. */
10076 tree t = TREE_TYPE (type);
10077 int ptr = TYPE_PTR_P (type);
10079 while (1)
10081 if (TYPE_PTR_P (t))
10082 ptr = 1;
10083 else if (TREE_CODE (t) != ARRAY_TYPE)
10084 break;
10085 else if (!TYPE_DOMAIN (t))
10086 break;
10087 t = TREE_TYPE (t);
10089 if (TREE_CODE (t) == ARRAY_TYPE)
10090 error (ptr
10091 ? G_("parameter %qD includes pointer to array of "
10092 "unknown bound %qT")
10093 : G_("parameter %qD includes reference to array of "
10094 "unknown bound %qT"),
10095 decl, t);
10098 if (any_error)
10099 init = NULL_TREE;
10100 else if (init && !processing_template_decl)
10101 init = check_default_argument (decl, init);
10104 if (TREE_CODE (decl) == PARM_DECL
10105 && FUNCTION_PARAMETER_PACK_P (decl)
10106 && TREE_CHAIN (parm)
10107 && TREE_CHAIN (parm) != void_list_node)
10108 error ("parameter packs must be at the end of the parameter list");
10110 DECL_CHAIN (decl) = decls;
10111 decls = decl;
10112 result = tree_cons (init, type, result);
10114 decls = nreverse (decls);
10115 result = nreverse (result);
10116 if (parm)
10117 result = chainon (result, void_list_node);
10118 *parms = decls;
10120 return result;
10124 /* D is a constructor or overloaded `operator='.
10126 Let T be the class in which D is declared. Then, this function
10127 returns:
10129 -1 if D's is an ill-formed constructor or copy assignment operator
10130 whose first parameter is of type `T'.
10131 0 if D is not a copy constructor or copy assignment
10132 operator.
10133 1 if D is a copy constructor or copy assignment operator whose
10134 first parameter is a reference to non-const qualified T.
10135 2 if D is a copy constructor or copy assignment operator whose
10136 first parameter is a reference to const qualified T.
10138 This function can be used as a predicate. Positive values indicate
10139 a copy constructor and nonzero values indicate a copy assignment
10140 operator. */
10143 copy_fn_p (const_tree d)
10145 tree args;
10146 tree arg_type;
10147 int result = 1;
10149 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
10151 if (TREE_CODE (d) == TEMPLATE_DECL
10152 || (DECL_TEMPLATE_INFO (d)
10153 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
10154 /* Instantiations of template member functions are never copy
10155 functions. Note that member functions of templated classes are
10156 represented as template functions internally, and we must
10157 accept those as copy functions. */
10158 return 0;
10160 args = FUNCTION_FIRST_USER_PARMTYPE (d);
10161 if (!args)
10162 return 0;
10164 arg_type = TREE_VALUE (args);
10165 if (arg_type == error_mark_node)
10166 return 0;
10168 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
10170 /* Pass by value copy assignment operator. */
10171 result = -1;
10173 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
10174 && !TYPE_REF_IS_RVALUE (arg_type)
10175 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
10177 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
10178 result = 2;
10180 else
10181 return 0;
10183 args = TREE_CHAIN (args);
10185 if (args && args != void_list_node && !TREE_PURPOSE (args))
10186 /* There are more non-optional args. */
10187 return 0;
10189 return result;
10192 /* D is a constructor or overloaded `operator='.
10194 Let T be the class in which D is declared. Then, this function
10195 returns true when D is a move constructor or move assignment
10196 operator, false otherwise. */
10198 bool
10199 move_fn_p (const_tree d)
10201 tree args;
10202 tree arg_type;
10203 bool result = false;
10205 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
10207 if (cxx_dialect == cxx98)
10208 /* There are no move constructors if we are in C++98 mode. */
10209 return false;
10211 if (TREE_CODE (d) == TEMPLATE_DECL
10212 || (DECL_TEMPLATE_INFO (d)
10213 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
10214 /* Instantiations of template member functions are never copy
10215 functions. Note that member functions of templated classes are
10216 represented as template functions internally, and we must
10217 accept those as copy functions. */
10218 return 0;
10220 args = FUNCTION_FIRST_USER_PARMTYPE (d);
10221 if (!args)
10222 return 0;
10224 arg_type = TREE_VALUE (args);
10225 if (arg_type == error_mark_node)
10226 return 0;
10228 if (TREE_CODE (arg_type) == REFERENCE_TYPE
10229 && TYPE_REF_IS_RVALUE (arg_type)
10230 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
10231 DECL_CONTEXT (d)))
10232 result = true;
10234 args = TREE_CHAIN (args);
10236 if (args && args != void_list_node && !TREE_PURPOSE (args))
10237 /* There are more non-optional args. */
10238 return false;
10240 return result;
10243 /* Remember any special properties of member function DECL. */
10245 void
10246 grok_special_member_properties (tree decl)
10248 tree class_type;
10250 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
10251 return;
10253 class_type = DECL_CONTEXT (decl);
10254 if (DECL_CONSTRUCTOR_P (decl))
10256 int ctor = copy_fn_p (decl);
10258 if (!DECL_ARTIFICIAL (decl))
10259 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
10261 if (ctor > 0)
10263 /* [class.copy]
10265 A non-template constructor for class X is a copy
10266 constructor if its first parameter is of type X&, const
10267 X&, volatile X& or const volatile X&, and either there
10268 are no other parameters or else all other parameters have
10269 default arguments. */
10270 TYPE_HAS_COPY_CTOR (class_type) = 1;
10271 if (user_provided_p (decl))
10272 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
10273 if (ctor > 1)
10274 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
10276 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
10278 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
10279 if (user_provided_p (decl))
10280 TYPE_HAS_COMPLEX_DFLT (class_type) = 1;
10282 else if (move_fn_p (decl) && user_provided_p (decl))
10283 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
10284 else if (is_list_ctor (decl))
10285 TYPE_HAS_LIST_CTOR (class_type) = 1;
10287 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
10289 /* [class.copy]
10291 A non-template assignment operator for class X is a copy
10292 assignment operator if its parameter is of type X, X&, const
10293 X&, volatile X& or const volatile X&. */
10295 int assop = copy_fn_p (decl);
10297 if (assop)
10299 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
10300 if (user_provided_p (decl))
10301 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
10302 if (assop != 1)
10303 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
10305 else if (move_fn_p (decl) && user_provided_p (decl))
10306 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
10308 /* Destructors are handled in check_methods. */
10311 /* Check a constructor DECL has the correct form. Complains
10312 if the class has a constructor of the form X(X). */
10315 grok_ctor_properties (const_tree ctype, const_tree decl)
10317 int ctor_parm = copy_fn_p (decl);
10319 if (ctor_parm < 0)
10321 /* [class.copy]
10323 A declaration of a constructor for a class X is ill-formed if
10324 its first parameter is of type (optionally cv-qualified) X
10325 and either there are no other parameters or else all other
10326 parameters have default arguments.
10328 We *don't* complain about member template instantiations that
10329 have this form, though; they can occur as we try to decide
10330 what constructor to use during overload resolution. Since
10331 overload resolution will never prefer such a constructor to
10332 the non-template copy constructor (which is either explicitly
10333 or implicitly defined), there's no need to worry about their
10334 existence. Theoretically, they should never even be
10335 instantiated, but that's hard to forestall. */
10336 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
10337 ctype, ctype);
10338 return 0;
10341 return 1;
10344 /* An operator with this code is unary, but can also be binary. */
10346 static int
10347 ambi_op_p (enum tree_code code)
10349 return (code == INDIRECT_REF
10350 || code == ADDR_EXPR
10351 || code == UNARY_PLUS_EXPR
10352 || code == NEGATE_EXPR
10353 || code == PREINCREMENT_EXPR
10354 || code == PREDECREMENT_EXPR);
10357 /* An operator with this name can only be unary. */
10359 static int
10360 unary_op_p (enum tree_code code)
10362 return (code == TRUTH_NOT_EXPR
10363 || code == BIT_NOT_EXPR
10364 || code == COMPONENT_REF
10365 || code == TYPE_EXPR);
10368 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
10369 errors are issued for invalid declarations. */
10371 bool
10372 grok_op_properties (tree decl, bool complain)
10374 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10375 tree argtype;
10376 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
10377 tree name = DECL_NAME (decl);
10378 enum tree_code operator_code;
10379 int arity;
10380 bool ellipsis_p;
10381 tree class_type;
10383 /* Count the number of arguments and check for ellipsis. */
10384 for (argtype = argtypes, arity = 0;
10385 argtype && argtype != void_list_node;
10386 argtype = TREE_CHAIN (argtype))
10387 ++arity;
10388 ellipsis_p = !argtype;
10390 class_type = DECL_CONTEXT (decl);
10391 if (class_type && !CLASS_TYPE_P (class_type))
10392 class_type = NULL_TREE;
10394 if (DECL_CONV_FN_P (decl))
10395 operator_code = TYPE_EXPR;
10396 else
10399 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
10400 if (ansi_opname (CODE) == name) \
10402 operator_code = (CODE); \
10403 break; \
10405 else if (ansi_assopname (CODE) == name) \
10407 operator_code = (CODE); \
10408 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
10409 break; \
10412 #include "operators.def"
10413 #undef DEF_OPERATOR
10415 gcc_unreachable ();
10417 while (0);
10418 gcc_assert (operator_code != MAX_TREE_CODES);
10419 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
10421 if (class_type)
10422 switch (operator_code)
10424 case NEW_EXPR:
10425 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
10426 break;
10428 case DELETE_EXPR:
10429 TYPE_GETS_DELETE (class_type) |= 1;
10430 break;
10432 case VEC_NEW_EXPR:
10433 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
10434 break;
10436 case VEC_DELETE_EXPR:
10437 TYPE_GETS_DELETE (class_type) |= 2;
10438 break;
10440 default:
10441 break;
10444 /* [basic.std.dynamic.allocation]/1:
10446 A program is ill-formed if an allocation function is declared
10447 in a namespace scope other than global scope or declared static
10448 in global scope.
10450 The same also holds true for deallocation functions. */
10451 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
10452 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
10454 if (DECL_NAMESPACE_SCOPE_P (decl))
10456 if (CP_DECL_CONTEXT (decl) != global_namespace)
10458 error ("%qD may not be declared within a namespace", decl);
10459 return false;
10461 else if (!TREE_PUBLIC (decl))
10463 error ("%qD may not be declared as static", decl);
10464 return false;
10469 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
10471 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
10472 DECL_IS_OPERATOR_NEW (decl) = 1;
10474 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
10475 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
10476 else
10478 /* An operator function must either be a non-static member function
10479 or have at least one parameter of a class, a reference to a class,
10480 an enumeration, or a reference to an enumeration. 13.4.0.6 */
10481 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
10483 if (operator_code == TYPE_EXPR
10484 || operator_code == CALL_EXPR
10485 || operator_code == COMPONENT_REF
10486 || operator_code == ARRAY_REF
10487 || operator_code == NOP_EXPR)
10489 error ("%qD must be a nonstatic member function", decl);
10490 return false;
10492 else
10494 tree p;
10496 if (DECL_STATIC_FUNCTION_P (decl))
10498 error ("%qD must be either a non-static member "
10499 "function or a non-member function", decl);
10500 return false;
10503 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
10505 tree arg = non_reference (TREE_VALUE (p));
10506 if (arg == error_mark_node)
10507 return false;
10509 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
10510 because these checks are performed even on
10511 template functions. */
10512 if (MAYBE_CLASS_TYPE_P (arg)
10513 || TREE_CODE (arg) == ENUMERAL_TYPE)
10514 break;
10517 if (!p || p == void_list_node)
10519 if (complain)
10520 error ("%qD must have an argument of class or "
10521 "enumerated type", decl);
10522 return false;
10527 /* There are no restrictions on the arguments to an overloaded
10528 "operator ()". */
10529 if (operator_code == CALL_EXPR)
10530 return true;
10532 /* Warn about conversion operators that will never be used. */
10533 if (IDENTIFIER_TYPENAME_P (name)
10534 && ! DECL_TEMPLATE_INFO (decl)
10535 && warn_conversion
10536 /* Warn only declaring the function; there is no need to
10537 warn again about out-of-class definitions. */
10538 && class_type == current_class_type)
10540 tree t = TREE_TYPE (name);
10541 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
10543 if (ref)
10544 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
10546 if (TREE_CODE (t) == VOID_TYPE)
10547 warning (OPT_Wconversion,
10549 ? G_("conversion to a reference to void "
10550 "will never use a type conversion operator")
10551 : G_("conversion to void "
10552 "will never use a type conversion operator"));
10553 else if (class_type)
10555 if (t == class_type)
10556 warning (OPT_Wconversion,
10558 ? G_("conversion to a reference to the same type "
10559 "will never use a type conversion operator")
10560 : G_("conversion to the same type "
10561 "will never use a type conversion operator"));
10562 /* Don't force t to be complete here. */
10563 else if (MAYBE_CLASS_TYPE_P (t)
10564 && COMPLETE_TYPE_P (t)
10565 && DERIVED_FROM_P (t, class_type))
10566 warning (OPT_Wconversion,
10568 ? G_("conversion to a reference to a base class "
10569 "will never use a type conversion operator")
10570 : G_("conversion to a base class "
10571 "will never use a type conversion operator"));
10576 if (operator_code == COND_EXPR)
10578 /* 13.4.0.3 */
10579 error ("ISO C++ prohibits overloading operator ?:");
10580 return false;
10582 else if (ellipsis_p)
10584 error ("%qD must not have variable number of arguments", decl);
10585 return false;
10587 else if (ambi_op_p (operator_code))
10589 if (arity == 1)
10590 /* We pick the one-argument operator codes by default, so
10591 we don't have to change anything. */
10593 else if (arity == 2)
10595 /* If we thought this was a unary operator, we now know
10596 it to be a binary operator. */
10597 switch (operator_code)
10599 case INDIRECT_REF:
10600 operator_code = MULT_EXPR;
10601 break;
10603 case ADDR_EXPR:
10604 operator_code = BIT_AND_EXPR;
10605 break;
10607 case UNARY_PLUS_EXPR:
10608 operator_code = PLUS_EXPR;
10609 break;
10611 case NEGATE_EXPR:
10612 operator_code = MINUS_EXPR;
10613 break;
10615 case PREINCREMENT_EXPR:
10616 operator_code = POSTINCREMENT_EXPR;
10617 break;
10619 case PREDECREMENT_EXPR:
10620 operator_code = POSTDECREMENT_EXPR;
10621 break;
10623 default:
10624 gcc_unreachable ();
10627 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
10629 if ((operator_code == POSTINCREMENT_EXPR
10630 || operator_code == POSTDECREMENT_EXPR)
10631 && ! processing_template_decl
10632 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
10634 if (methodp)
10635 error ("postfix %qD must take %<int%> as its argument",
10636 decl);
10637 else
10638 error ("postfix %qD must take %<int%> as its second "
10639 "argument", decl);
10640 return false;
10643 else
10645 if (methodp)
10646 error ("%qD must take either zero or one argument", decl);
10647 else
10648 error ("%qD must take either one or two arguments", decl);
10649 return false;
10652 /* More Effective C++ rule 6. */
10653 if (warn_ecpp
10654 && (operator_code == POSTINCREMENT_EXPR
10655 || operator_code == POSTDECREMENT_EXPR
10656 || operator_code == PREINCREMENT_EXPR
10657 || operator_code == PREDECREMENT_EXPR))
10659 tree arg = TREE_VALUE (argtypes);
10660 tree ret = TREE_TYPE (TREE_TYPE (decl));
10661 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
10662 arg = TREE_TYPE (arg);
10663 arg = TYPE_MAIN_VARIANT (arg);
10664 if (operator_code == PREINCREMENT_EXPR
10665 || operator_code == PREDECREMENT_EXPR)
10667 if (TREE_CODE (ret) != REFERENCE_TYPE
10668 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
10669 arg))
10670 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
10671 build_reference_type (arg));
10673 else
10675 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
10676 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
10680 else if (unary_op_p (operator_code))
10682 if (arity != 1)
10684 if (methodp)
10685 error ("%qD must take %<void%>", decl);
10686 else
10687 error ("%qD must take exactly one argument", decl);
10688 return false;
10691 else /* if (binary_op_p (operator_code)) */
10693 if (arity != 2)
10695 if (methodp)
10696 error ("%qD must take exactly one argument", decl);
10697 else
10698 error ("%qD must take exactly two arguments", decl);
10699 return false;
10702 /* More Effective C++ rule 7. */
10703 if (warn_ecpp
10704 && (operator_code == TRUTH_ANDIF_EXPR
10705 || operator_code == TRUTH_ORIF_EXPR
10706 || operator_code == COMPOUND_EXPR))
10707 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
10708 decl);
10711 /* Effective C++ rule 23. */
10712 if (warn_ecpp
10713 && arity == 2
10714 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
10715 && (operator_code == PLUS_EXPR
10716 || operator_code == MINUS_EXPR
10717 || operator_code == TRUNC_DIV_EXPR
10718 || operator_code == MULT_EXPR
10719 || operator_code == TRUNC_MOD_EXPR)
10720 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
10721 warning (OPT_Weffc__, "%qD should return by value", decl);
10723 /* [over.oper]/8 */
10724 for (; argtypes && argtypes != void_list_node;
10725 argtypes = TREE_CHAIN (argtypes))
10726 if (TREE_PURPOSE (argtypes))
10728 TREE_PURPOSE (argtypes) = NULL_TREE;
10729 if (operator_code == POSTINCREMENT_EXPR
10730 || operator_code == POSTDECREMENT_EXPR)
10732 pedwarn (input_location, OPT_pedantic, "%qD cannot have default arguments",
10733 decl);
10735 else
10737 error ("%qD cannot have default arguments", decl);
10738 return false;
10742 return true;
10745 /* Return a string giving the keyword associate with CODE. */
10747 static const char *
10748 tag_name (enum tag_types code)
10750 switch (code)
10752 case record_type:
10753 return "struct";
10754 case class_type:
10755 return "class";
10756 case union_type:
10757 return "union";
10758 case enum_type:
10759 return "enum";
10760 case typename_type:
10761 return "typename";
10762 default:
10763 gcc_unreachable ();
10767 /* Name lookup in an elaborated-type-specifier (after the keyword
10768 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
10769 elaborated-type-specifier is invalid, issue a diagnostic and return
10770 error_mark_node; otherwise, return the *_TYPE to which it referred.
10771 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
10773 tree
10774 check_elaborated_type_specifier (enum tag_types tag_code,
10775 tree decl,
10776 bool allow_template_p)
10778 tree type;
10780 /* In the case of:
10782 struct S { struct S *p; };
10784 name lookup will find the TYPE_DECL for the implicit "S::S"
10785 typedef. Adjust for that here. */
10786 if (DECL_SELF_REFERENCE_P (decl))
10787 decl = TYPE_NAME (TREE_TYPE (decl));
10789 type = TREE_TYPE (decl);
10791 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
10792 is false for this case as well. */
10793 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10795 error ("using template type parameter %qT after %qs",
10796 type, tag_name (tag_code));
10797 return error_mark_node;
10799 /* [dcl.type.elab]
10801 If the identifier resolves to a typedef-name or a template
10802 type-parameter, the elaborated-type-specifier is ill-formed.
10804 In other words, the only legitimate declaration to use in the
10805 elaborated type specifier is the implicit typedef created when
10806 the type is declared. */
10807 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
10808 && !DECL_SELF_REFERENCE_P (decl)
10809 && tag_code != typename_type)
10811 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
10812 error ("%q+D has a previous declaration here", decl);
10813 return error_mark_node;
10815 else if (TREE_CODE (type) != RECORD_TYPE
10816 && TREE_CODE (type) != UNION_TYPE
10817 && tag_code != enum_type
10818 && tag_code != typename_type)
10820 error ("%qT referred to as %qs", type, tag_name (tag_code));
10821 error ("%q+T has a previous declaration here", type);
10822 return error_mark_node;
10824 else if (TREE_CODE (type) != ENUMERAL_TYPE
10825 && tag_code == enum_type)
10827 error ("%qT referred to as enum", type);
10828 error ("%q+T has a previous declaration here", type);
10829 return error_mark_node;
10831 else if (!allow_template_p
10832 && TREE_CODE (type) == RECORD_TYPE
10833 && CLASSTYPE_IS_TEMPLATE (type))
10835 /* If a class template appears as elaborated type specifier
10836 without a template header such as:
10838 template <class T> class C {};
10839 void f(class C); // No template header here
10841 then the required template argument is missing. */
10842 error ("template argument required for %<%s %T%>",
10843 tag_name (tag_code),
10844 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
10845 return error_mark_node;
10848 return type;
10851 /* Lookup NAME in elaborate type specifier in scope according to
10852 SCOPE and issue diagnostics if necessary.
10853 Return *_TYPE node upon success, NULL_TREE when the NAME is not
10854 found, and ERROR_MARK_NODE for type error. */
10856 static tree
10857 lookup_and_check_tag (enum tag_types tag_code, tree name,
10858 tag_scope scope, bool template_header_p)
10860 tree t;
10861 tree decl;
10862 if (scope == ts_global)
10864 /* First try ordinary name lookup, ignoring hidden class name
10865 injected via friend declaration. */
10866 decl = lookup_name_prefer_type (name, 2);
10867 /* If that fails, the name will be placed in the smallest
10868 non-class, non-function-prototype scope according to 3.3.1/5.
10869 We may already have a hidden name declared as friend in this
10870 scope. So lookup again but not ignoring hidden names.
10871 If we find one, that name will be made visible rather than
10872 creating a new tag. */
10873 if (!decl)
10874 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
10876 else
10877 decl = lookup_type_scope (name, scope);
10879 if (decl && DECL_CLASS_TEMPLATE_P (decl))
10880 decl = DECL_TEMPLATE_RESULT (decl);
10882 if (decl && TREE_CODE (decl) == TYPE_DECL)
10884 /* Look for invalid nested type:
10885 class C {
10886 class C {};
10887 }; */
10888 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
10890 error ("%qD has the same name as the class in which it is "
10891 "declared",
10892 decl);
10893 return error_mark_node;
10896 /* Two cases we need to consider when deciding if a class
10897 template is allowed as an elaborated type specifier:
10898 1. It is a self reference to its own class.
10899 2. It comes with a template header.
10901 For example:
10903 template <class T> class C {
10904 class C *c1; // DECL_SELF_REFERENCE_P is true
10905 class D;
10907 template <class U> class C; // template_header_p is true
10908 template <class T> class C<T>::D {
10909 class C *c2; // DECL_SELF_REFERENCE_P is true
10910 }; */
10912 t = check_elaborated_type_specifier (tag_code,
10913 decl,
10914 template_header_p
10915 | DECL_SELF_REFERENCE_P (decl));
10916 return t;
10918 else if (decl && TREE_CODE (decl) == TREE_LIST)
10920 error ("reference to %qD is ambiguous", name);
10921 print_candidates (decl);
10922 return error_mark_node;
10924 else
10925 return NULL_TREE;
10928 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
10929 Define the tag as a forward-reference if it is not defined.
10931 If a declaration is given, process it here, and report an error if
10932 multiple declarations are not identical.
10934 SCOPE is TS_CURRENT when this is also a definition. Only look in
10935 the current frame for the name (since C++ allows new names in any
10936 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
10937 declaration. Only look beginning from the current scope outward up
10938 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
10940 TEMPLATE_HEADER_P is true when this declaration is preceded by
10941 a set of template parameters. */
10943 tree
10944 xref_tag (enum tag_types tag_code, tree name,
10945 tag_scope scope, bool template_header_p)
10947 enum tree_code code;
10948 tree t;
10949 tree context = NULL_TREE;
10951 timevar_push (TV_NAME_LOOKUP);
10953 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
10955 switch (tag_code)
10957 case record_type:
10958 case class_type:
10959 code = RECORD_TYPE;
10960 break;
10961 case union_type:
10962 code = UNION_TYPE;
10963 break;
10964 case enum_type:
10965 code = ENUMERAL_TYPE;
10966 break;
10967 default:
10968 gcc_unreachable ();
10971 /* In case of anonymous name, xref_tag is only called to
10972 make type node and push name. Name lookup is not required. */
10973 if (ANON_AGGRNAME_P (name))
10974 t = NULL_TREE;
10975 else
10976 t = lookup_and_check_tag (tag_code, name,
10977 scope, template_header_p);
10979 if (t == error_mark_node)
10980 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
10982 if (scope != ts_current && t && current_class_type
10983 && template_class_depth (current_class_type)
10984 && template_header_p)
10986 /* Since SCOPE is not TS_CURRENT, we are not looking at a
10987 definition of this tag. Since, in addition, we are currently
10988 processing a (member) template declaration of a template
10989 class, we must be very careful; consider:
10991 template <class X>
10992 struct S1
10994 template <class U>
10995 struct S2
10996 { template <class V>
10997 friend struct S1; };
10999 Here, the S2::S1 declaration should not be confused with the
11000 outer declaration. In particular, the inner version should
11001 have a template parameter of level 2, not level 1. This
11002 would be particularly important if the member declaration
11003 were instead:
11005 template <class V = U> friend struct S1;
11007 say, when we should tsubst into `U' when instantiating
11008 S2. On the other hand, when presented with:
11010 template <class T>
11011 struct S1 {
11012 template <class U>
11013 struct S2 {};
11014 template <class U>
11015 friend struct S2;
11018 we must find the inner binding eventually. We
11019 accomplish this by making sure that the new type we
11020 create to represent this declaration has the right
11021 TYPE_CONTEXT. */
11022 context = TYPE_CONTEXT (t);
11023 t = NULL_TREE;
11026 if (! t)
11028 /* If no such tag is yet defined, create a forward-reference node
11029 and record it as the "definition".
11030 When a real declaration of this type is found,
11031 the forward-reference will be altered into a real type. */
11032 if (code == ENUMERAL_TYPE)
11034 error ("use of enum %q#D without previous declaration", name);
11035 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
11037 else
11039 t = make_class_type (code);
11040 TYPE_CONTEXT (t) = context;
11041 t = pushtag (name, t, scope);
11044 else
11046 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
11048 if (!redeclare_class_template (t, current_template_parms))
11049 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
11051 else if (!processing_template_decl
11052 && CLASS_TYPE_P (t)
11053 && CLASSTYPE_IS_TEMPLATE (t))
11055 error ("redeclaration of %qT as a non-template", t);
11056 error ("previous declaration %q+D", t);
11057 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
11060 /* Make injected friend class visible. */
11061 if (scope != ts_within_enclosing_non_class
11062 && hidden_name_p (TYPE_NAME (t)))
11064 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
11065 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
11067 if (TYPE_TEMPLATE_INFO (t))
11069 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
11070 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
11075 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
11078 tree
11079 xref_tag_from_type (tree old, tree id, tag_scope scope)
11081 enum tag_types tag_kind;
11083 if (TREE_CODE (old) == RECORD_TYPE)
11084 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
11085 else
11086 tag_kind = union_type;
11088 if (id == NULL_TREE)
11089 id = TYPE_IDENTIFIER (old);
11091 return xref_tag (tag_kind, id, scope, false);
11094 /* Create the binfo hierarchy for REF with (possibly NULL) base list
11095 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
11096 access_* node, and the TREE_VALUE is the type of the base-class.
11097 Non-NULL TREE_TYPE indicates virtual inheritance.
11099 Returns true if the binfo hierarchy was successfully created,
11100 false if an error was detected. */
11102 bool
11103 xref_basetypes (tree ref, tree base_list)
11105 tree *basep;
11106 tree binfo, base_binfo;
11107 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
11108 unsigned max_bases = 0; /* Maximum direct bases. */
11109 int i;
11110 tree default_access;
11111 tree igo_prev; /* Track Inheritance Graph Order. */
11113 if (ref == error_mark_node)
11114 return false;
11116 /* The base of a derived class is private by default, all others are
11117 public. */
11118 default_access = (TREE_CODE (ref) == RECORD_TYPE
11119 && CLASSTYPE_DECLARED_CLASS (ref)
11120 ? access_private_node : access_public_node);
11122 /* First, make sure that any templates in base-classes are
11123 instantiated. This ensures that if we call ourselves recursively
11124 we do not get confused about which classes are marked and which
11125 are not. */
11126 basep = &base_list;
11127 while (*basep)
11129 tree basetype = TREE_VALUE (*basep);
11131 if (!(processing_template_decl && uses_template_parms (basetype))
11132 && !complete_type_or_else (basetype, NULL))
11133 /* An incomplete type. Remove it from the list. */
11134 *basep = TREE_CHAIN (*basep);
11135 else
11137 max_bases++;
11138 if (TREE_TYPE (*basep))
11139 max_vbases++;
11140 if (CLASS_TYPE_P (basetype))
11141 max_vbases += VEC_length (tree, CLASSTYPE_VBASECLASSES (basetype));
11142 basep = &TREE_CHAIN (*basep);
11146 TYPE_MARKED_P (ref) = 1;
11148 /* The binfo slot should be empty, unless this is an (ill-formed)
11149 redefinition. */
11150 if (TYPE_BINFO (ref) && !TYPE_SIZE (ref))
11152 error ("redefinition of %q#T", ref);
11153 return false;
11156 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
11158 binfo = make_tree_binfo (max_bases);
11160 TYPE_BINFO (ref) = binfo;
11161 BINFO_OFFSET (binfo) = size_zero_node;
11162 BINFO_TYPE (binfo) = ref;
11164 /* Apply base-class info set up to the variants of this type. */
11165 fixup_type_variants (ref);
11167 if (max_bases)
11169 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, max_bases);
11170 /* An aggregate cannot have baseclasses. */
11171 CLASSTYPE_NON_AGGREGATE (ref) = 1;
11173 if (TREE_CODE (ref) == UNION_TYPE)
11175 error ("derived union %qT invalid", ref);
11176 return false;
11180 if (max_bases > 1)
11182 if (TYPE_FOR_JAVA (ref))
11184 error ("Java class %qT cannot have multiple bases", ref);
11185 return false;
11189 if (max_vbases)
11191 CLASSTYPE_VBASECLASSES (ref) = VEC_alloc (tree, gc, max_vbases);
11193 if (TYPE_FOR_JAVA (ref))
11195 error ("Java class %qT cannot have virtual bases", ref);
11196 return false;
11200 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
11202 tree access = TREE_PURPOSE (base_list);
11203 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
11204 tree basetype = TREE_VALUE (base_list);
11206 if (access == access_default_node)
11207 access = default_access;
11209 if (PACK_EXPANSION_P (basetype))
11210 basetype = PACK_EXPANSION_PATTERN (basetype);
11211 if (TREE_CODE (basetype) == TYPE_DECL)
11212 basetype = TREE_TYPE (basetype);
11213 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
11215 error ("base type %qT fails to be a struct or class type",
11216 basetype);
11217 return false;
11220 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
11221 TYPE_FOR_JAVA (ref) = 1;
11223 base_binfo = NULL_TREE;
11224 if (CLASS_TYPE_P (basetype) && !dependent_type_p (basetype))
11226 base_binfo = TYPE_BINFO (basetype);
11227 /* The original basetype could have been a typedef'd type. */
11228 basetype = BINFO_TYPE (base_binfo);
11230 /* Inherit flags from the base. */
11231 TYPE_HAS_NEW_OPERATOR (ref)
11232 |= TYPE_HAS_NEW_OPERATOR (basetype);
11233 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
11234 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
11235 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
11236 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
11237 CLASSTYPE_DIAMOND_SHAPED_P (ref)
11238 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
11239 CLASSTYPE_REPEATED_BASE_P (ref)
11240 |= CLASSTYPE_REPEATED_BASE_P (basetype);
11243 /* We must do this test after we've seen through a typedef
11244 type. */
11245 if (TYPE_MARKED_P (basetype))
11247 if (basetype == ref)
11248 error ("recursive type %qT undefined", basetype);
11249 else
11250 error ("duplicate base type %qT invalid", basetype);
11251 return false;
11254 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
11255 /* Regenerate the pack expansion for the bases. */
11256 basetype = make_pack_expansion (basetype);
11258 TYPE_MARKED_P (basetype) = 1;
11260 base_binfo = copy_binfo (base_binfo, basetype, ref,
11261 &igo_prev, via_virtual);
11262 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
11263 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
11265 BINFO_BASE_APPEND (binfo, base_binfo);
11266 BINFO_BASE_ACCESS_APPEND (binfo, access);
11269 if (VEC_space (tree, CLASSTYPE_VBASECLASSES (ref), 1))
11270 /* If we have space in the vbase vector, we must have shared at
11271 least one of them, and are therefore diamond shaped. */
11272 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
11274 /* Unmark all the types. */
11275 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11276 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
11277 TYPE_MARKED_P (ref) = 0;
11279 /* Now see if we have a repeated base type. */
11280 if (!CLASSTYPE_REPEATED_BASE_P (ref))
11282 for (base_binfo = binfo; base_binfo;
11283 base_binfo = TREE_CHAIN (base_binfo))
11285 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
11287 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
11288 break;
11290 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
11292 for (base_binfo = binfo; base_binfo;
11293 base_binfo = TREE_CHAIN (base_binfo))
11294 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
11295 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
11296 else
11297 break;
11300 return true;
11304 /* Begin compiling the definition of an enumeration type.
11305 NAME is its name,
11307 UNDERLYING_TYPE is the type that will be used as the storage for
11308 the enumeration type. This should be NULL_TREE if no storage type
11309 was specified.
11311 SCOPED_ENUM_P is true if this is a scoped enumeration type.
11313 Returns the type object, as yet incomplete.
11314 Also records info about it so that build_enumerator
11315 may be used to declare the individual values as they are read. */
11317 tree
11318 start_enum (tree name, tree underlying_type, bool scoped_enum_p)
11320 tree enumtype;
11322 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
11324 /* If this is the real definition for a previous forward reference,
11325 fill in the contents in the same object that used to be the
11326 forward reference. */
11328 enumtype = lookup_and_check_tag (enum_type, name,
11329 /*tag_scope=*/ts_current,
11330 /*template_header_p=*/false);
11332 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
11334 error_at (input_location, "multiple definition of %q#T", enumtype);
11335 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
11336 "previous definition here");
11337 /* Clear out TYPE_VALUES, and start again. */
11338 TYPE_VALUES (enumtype) = NULL_TREE;
11340 else
11342 /* In case of error, make a dummy enum to allow parsing to
11343 continue. */
11344 if (enumtype == error_mark_node)
11345 name = make_anon_name ();
11347 enumtype = cxx_make_type (ENUMERAL_TYPE);
11348 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
11351 if (enumtype == error_mark_node)
11352 return enumtype;
11354 if (scoped_enum_p)
11356 SET_SCOPED_ENUM_P (enumtype, 1);
11357 begin_scope (sk_scoped_enum, enumtype);
11359 /* [C++0x dcl.enum]p5:
11361 If not explicitly specified, the underlying type of a scoped
11362 enumeration type is int. */
11363 if (!underlying_type)
11364 underlying_type = integer_type_node;
11367 if (underlying_type)
11369 if (CP_INTEGRAL_TYPE_P (underlying_type))
11371 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (underlying_type);
11372 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (underlying_type);
11373 TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
11374 TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
11375 SET_TYPE_MODE (enumtype, TYPE_MODE (underlying_type));
11376 TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type);
11377 TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
11378 TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
11379 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (underlying_type);
11380 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
11382 else if (!dependent_type_p (underlying_type))
11383 error ("underlying type %<%T%> of %<%T%> must be an integral type",
11384 underlying_type, enumtype);
11387 return enumtype;
11390 /* After processing and defining all the values of an enumeration type,
11391 install their decls in the enumeration type and finish it off.
11392 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
11394 void
11395 finish_enum (tree enumtype)
11397 tree values;
11398 tree decl;
11399 tree minnode;
11400 tree maxnode;
11401 tree value;
11402 tree t;
11403 tree underlying_type = NULL_TREE;
11404 bool fixed_underlying_type_p
11405 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
11407 /* We built up the VALUES in reverse order. */
11408 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
11410 /* For an enum defined in a template, just set the type of the values;
11411 all further processing is postponed until the template is
11412 instantiated. We need to set the type so that tsubst of a CONST_DECL
11413 works. */
11414 if (processing_template_decl)
11416 for (values = TYPE_VALUES (enumtype);
11417 values;
11418 values = TREE_CHAIN (values))
11419 TREE_TYPE (TREE_VALUE (values)) = enumtype;
11420 if (at_function_scope_p ())
11421 add_stmt (build_min (TAG_DEFN, enumtype));
11422 if (SCOPED_ENUM_P (enumtype))
11423 finish_scope ();
11424 return;
11427 /* Determine the minimum and maximum values of the enumerators. */
11428 if (TYPE_VALUES (enumtype))
11430 minnode = maxnode = NULL_TREE;
11432 for (values = TYPE_VALUES (enumtype);
11433 values;
11434 values = TREE_CHAIN (values))
11436 decl = TREE_VALUE (values);
11438 /* [dcl.enum]: Following the closing brace of an enum-specifier,
11439 each enumerator has the type of its enumeration. Prior to the
11440 closing brace, the type of each enumerator is the type of its
11441 initializing value. */
11442 TREE_TYPE (decl) = enumtype;
11444 /* Update the minimum and maximum values, if appropriate. */
11445 value = DECL_INITIAL (decl);
11446 if (value == error_mark_node)
11447 value = integer_zero_node;
11448 /* Figure out what the minimum and maximum values of the
11449 enumerators are. */
11450 if (!minnode)
11451 minnode = maxnode = value;
11452 else if (tree_int_cst_lt (maxnode, value))
11453 maxnode = value;
11454 else if (tree_int_cst_lt (value, minnode))
11455 minnode = value;
11458 else
11459 /* [dcl.enum]
11461 If the enumerator-list is empty, the underlying type is as if
11462 the enumeration had a single enumerator with value 0. */
11463 minnode = maxnode = integer_zero_node;
11465 if (!fixed_underlying_type_p)
11467 /* Compute the number of bits require to represent all values of the
11468 enumeration. We must do this before the type of MINNODE and
11469 MAXNODE are transformed, since tree_int_cst_min_precision relies
11470 on the TREE_TYPE of the value it is passed. */
11471 bool unsignedp = tree_int_cst_sgn (minnode) >= 0;
11472 int lowprec = tree_int_cst_min_precision (minnode, unsignedp);
11473 int highprec = tree_int_cst_min_precision (maxnode, unsignedp);
11474 int precision = MAX (lowprec, highprec);
11475 unsigned int itk;
11476 bool use_short_enum;
11478 /* Determine the underlying type of the enumeration.
11480 [dcl.enum]
11482 The underlying type of an enumeration is an integral type that
11483 can represent all the enumerator values defined in the
11484 enumeration. It is implementation-defined which integral type is
11485 used as the underlying type for an enumeration except that the
11486 underlying type shall not be larger than int unless the value of
11487 an enumerator cannot fit in an int or unsigned int.
11489 We use "int" or an "unsigned int" as the underlying type, even if
11490 a smaller integral type would work, unless the user has
11491 explicitly requested that we use the smallest possible type. The
11492 user can request that for all enumerations with a command line
11493 flag, or for just one enumeration with an attribute. */
11495 use_short_enum = flag_short_enums
11496 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
11498 for (itk = (use_short_enum ? itk_char : itk_int);
11499 itk != itk_none;
11500 itk++)
11502 underlying_type = integer_types[itk];
11503 if (underlying_type != NULL_TREE
11504 && TYPE_PRECISION (underlying_type) >= precision
11505 && TYPE_UNSIGNED (underlying_type) == unsignedp)
11506 break;
11508 if (itk == itk_none)
11510 /* DR 377
11512 IF no integral type can represent all the enumerator values, the
11513 enumeration is ill-formed. */
11514 error ("no integral type can represent all of the enumerator values "
11515 "for %qT", enumtype);
11516 precision = TYPE_PRECISION (long_long_integer_type_node);
11517 underlying_type = integer_types[itk_unsigned_long_long];
11520 /* [dcl.enum]
11522 The value of sizeof() applied to an enumeration type, an object
11523 of an enumeration type, or an enumerator, is the value of sizeof()
11524 applied to the underlying type. */
11525 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (underlying_type);
11526 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (underlying_type);
11527 TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
11528 TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
11529 SET_TYPE_MODE (enumtype, TYPE_MODE (underlying_type));
11530 TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type);
11531 TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
11532 TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
11533 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (underlying_type);
11535 /* Compute the minimum and maximum values for the type.
11537 [dcl.enum]
11539 For an enumeration where emin is the smallest enumerator and emax
11540 is the largest, the values of the enumeration are the values of the
11541 underlying type in the range bmin to bmax, where bmin and bmax are,
11542 respectively, the smallest and largest values of the smallest bit-
11543 field that can store emin and emax. */
11545 /* The middle-end currently assumes that types with TYPE_PRECISION
11546 narrower than their underlying type are suitably zero or sign
11547 extended to fill their mode. Similarly, it assumes that the front
11548 end assures that a value of a particular type must be within
11549 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
11551 We used to set these fields based on bmin and bmax, but that led
11552 to invalid assumptions like optimizing away bounds checking. So
11553 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
11554 TYPE_MAX_VALUE to the values for the mode above and only restrict
11555 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
11556 ENUM_UNDERLYING_TYPE (enumtype)
11557 = build_distinct_type_copy (underlying_type);
11558 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
11559 set_min_and_max_values_for_integral_type
11560 (ENUM_UNDERLYING_TYPE (enumtype), precision, unsignedp);
11562 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
11563 if (flag_strict_enums)
11564 set_min_and_max_values_for_integral_type (enumtype, precision,
11565 unsignedp);
11567 else
11568 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
11570 /* Convert each of the enumerators to the type of the underlying
11571 type of the enumeration. */
11572 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
11574 location_t saved_location;
11576 decl = TREE_VALUE (values);
11577 saved_location = input_location;
11578 input_location = DECL_SOURCE_LOCATION (decl);
11579 if (fixed_underlying_type_p)
11580 /* If the enumeration type has a fixed underlying type, we
11581 already checked all of the enumerator values. */
11582 value = DECL_INITIAL (decl);
11583 else
11584 value = perform_implicit_conversion (underlying_type,
11585 DECL_INITIAL (decl),
11586 tf_warning_or_error);
11587 input_location = saved_location;
11589 /* Do not clobber shared ints. */
11590 value = copy_node (value);
11592 TREE_TYPE (value) = enumtype;
11593 DECL_INITIAL (decl) = value;
11596 /* Fix up all variant types of this enum type. */
11597 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
11599 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
11600 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
11601 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
11602 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
11603 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
11604 SET_TYPE_MODE (t, TYPE_MODE (enumtype));
11605 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
11606 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
11607 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
11608 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (enumtype);
11609 ENUM_UNDERLYING_TYPE (t) = ENUM_UNDERLYING_TYPE (enumtype);
11612 /* Finish up the scope of a scoped enumeration. */
11613 if (SCOPED_ENUM_P (enumtype))
11614 finish_scope ();
11616 /* Finish debugging output for this type. */
11617 rest_of_type_compilation (enumtype, namespace_bindings_p ());
11620 /* Build and install a CONST_DECL for an enumeration constant of the
11621 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
11622 Assignment of sequential values by default is handled here. */
11624 void
11625 build_enumerator (tree name, tree value, tree enumtype)
11627 tree decl;
11628 tree context;
11629 tree type;
11631 /* If the VALUE was erroneous, pretend it wasn't there; that will
11632 result in the enum being assigned the next value in sequence. */
11633 if (value == error_mark_node)
11634 value = NULL_TREE;
11636 /* Remove no-op casts from the value. */
11637 if (value)
11638 STRIP_TYPE_NOPS (value);
11640 if (! processing_template_decl)
11642 /* Validate and default VALUE. */
11643 if (value != NULL_TREE)
11645 value = integral_constant_value (value);
11647 if (TREE_CODE (value) == INTEGER_CST)
11649 value = perform_integral_promotions (value);
11650 constant_expression_warning (value);
11652 else
11654 error ("enumerator value for %qD is not an integer constant", name);
11655 value = NULL_TREE;
11659 /* Default based on previous value. */
11660 if (value == NULL_TREE)
11662 if (TYPE_VALUES (enumtype))
11664 HOST_WIDE_INT hi;
11665 unsigned HOST_WIDE_INT lo;
11666 tree prev_value;
11667 bool overflowed;
11669 /* The next value is the previous value plus one.
11670 add_double doesn't know the type of the target expression,
11671 so we must check with int_fits_type_p as well. */
11672 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
11673 if (error_operand_p (prev_value))
11674 value = error_mark_node;
11675 else
11677 overflowed = add_double (TREE_INT_CST_LOW (prev_value),
11678 TREE_INT_CST_HIGH (prev_value),
11679 1, 0, &lo, &hi);
11680 value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi);
11681 overflowed
11682 |= !int_fits_type_p (value, TREE_TYPE (prev_value));
11684 if (overflowed)
11686 error ("overflow in enumeration values at %qD", name);
11687 value = error_mark_node;
11691 else
11692 value = integer_zero_node;
11695 /* Remove no-op casts from the value. */
11696 STRIP_TYPE_NOPS (value);
11698 /* If the underlying type of the enum is fixed, check whether
11699 the enumerator values fits in the underlying type. If it
11700 does not fit, the program is ill-formed [C++0x dcl.enum]. */
11701 if (ENUM_UNDERLYING_TYPE (enumtype)
11702 && value
11703 && TREE_CODE (value) == INTEGER_CST
11704 && !int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
11706 error ("enumerator value %E is too large for underlying type %<%T%>",
11707 value, ENUM_UNDERLYING_TYPE (enumtype));
11709 /* Silently convert the value so that we can continue. */
11710 value = perform_implicit_conversion (ENUM_UNDERLYING_TYPE (enumtype),
11711 value, tf_none);
11715 /* C++ associates enums with global, function, or class declarations. */
11716 context = current_scope ();
11718 /* Build the actual enumeration constant. Note that the enumeration
11719 constants have the underlying type of the enum (if it is fixed)
11720 or the type of their initializer (if the underlying type of the
11721 enum is not fixed):
11723 [ C++0x dcl.enum ]
11725 If the underlying type is fixed, the type of each enumerator
11726 prior to the closing brace is the underlying type; if the
11727 initializing value of an enumerator cannot be represented by
11728 the underlying type, the program is ill-formed. If the
11729 underlying type is not fixed, the type of each enumerator is
11730 the type of its initializing value.
11732 If the underlying type is not fixed, it will be computed by
11733 finish_enum and we will reset the type of this enumerator. Of
11734 course, if we're processing a template, there may be no value. */
11735 type = value ? TREE_TYPE (value) : NULL_TREE;
11737 if (context && context == current_class_type)
11738 /* This enum declaration is local to the class. We need the full
11739 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
11740 decl = build_lang_decl (CONST_DECL, name, type);
11741 else
11742 /* It's a global enum, or it's local to a function. (Note local to
11743 a function could mean local to a class method. */
11744 decl = build_decl (input_location, CONST_DECL, name, type);
11746 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
11747 TREE_CONSTANT (decl) = 1;
11748 TREE_READONLY (decl) = 1;
11749 DECL_INITIAL (decl) = value;
11751 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
11752 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
11753 on the TYPE_FIELDS list for `S'. (That's so that you can say
11754 things like `S::i' later.) */
11755 finish_member_declaration (decl);
11756 else
11757 pushdecl (decl);
11759 /* Add this enumeration constant to the list for this type. */
11760 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
11763 /* Look for an enumerator with the given NAME within the enumeration
11764 type ENUMTYPE. This routine is used primarily for qualified name
11765 lookup into an enumerator in C++0x, e.g.,
11767 enum class Color { Red, Green, Blue };
11769 Color color = Color::Red;
11771 Returns the value corresponding to the enumerator, or
11772 NULL_TREE if no such enumerator was found. */
11773 tree
11774 lookup_enumerator (tree enumtype, tree name)
11776 tree e;
11777 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
11779 e = purpose_member (name, TYPE_VALUES (enumtype));
11780 return e? TREE_VALUE (e) : NULL_TREE;
11784 /* We're defining DECL. Make sure that its type is OK. */
11786 static void
11787 check_function_type (tree decl, tree current_function_parms)
11789 tree fntype = TREE_TYPE (decl);
11790 tree return_type = complete_type (TREE_TYPE (fntype));
11792 /* In a function definition, arg types must be complete. */
11793 require_complete_types_for_parms (current_function_parms);
11795 /* constexpr functions must have literal argument types and
11796 literal return type. */
11797 validate_constexpr_fundecl (decl);
11799 if (dependent_type_p (return_type))
11800 return;
11801 if (!COMPLETE_OR_VOID_TYPE_P (return_type)
11802 || (TYPE_FOR_JAVA (return_type) && MAYBE_CLASS_TYPE_P (return_type)))
11804 tree args = TYPE_ARG_TYPES (fntype);
11806 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
11807 error ("return type %q#T is incomplete", return_type);
11808 else
11809 error ("return type has Java class type %q#T", return_type);
11811 /* Make it return void instead. */
11812 if (TREE_CODE (fntype) == METHOD_TYPE)
11813 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
11814 void_type_node,
11815 TREE_CHAIN (args));
11816 else
11817 fntype = build_function_type (void_type_node, args);
11818 fntype
11819 = build_exception_variant (fntype,
11820 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
11821 fntype = (cp_build_type_attribute_variant
11822 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
11823 TREE_TYPE (decl) = fntype;
11825 else
11826 abstract_virtuals_error (decl, TREE_TYPE (fntype));
11829 /* Create the FUNCTION_DECL for a function definition.
11830 DECLSPECS and DECLARATOR are the parts of the declaration;
11831 they describe the function's name and the type it returns,
11832 but twisted together in a fashion that parallels the syntax of C.
11834 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
11835 DECLARATOR is really the DECL for the function we are about to
11836 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
11837 indicating that the function is an inline defined in-class.
11839 This function creates a binding context for the function body
11840 as well as setting up the FUNCTION_DECL in current_function_decl.
11842 For C++, we must first check whether that datum makes any sense.
11843 For example, "class A local_a(1,2);" means that variable local_a
11844 is an aggregate of type A, which should have a constructor
11845 applied to it with the argument list [1, 2].
11847 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
11848 or may be a BLOCK if the function has been defined previously
11849 in this translation unit. On exit, DECL_INITIAL (decl1) will be
11850 error_mark_node if the function has never been defined, or
11851 a BLOCK if the function has been defined somewhere. */
11853 void
11854 start_preparsed_function (tree decl1, tree attrs, int flags)
11856 tree ctype = NULL_TREE;
11857 tree fntype;
11858 tree restype;
11859 int doing_friend = 0;
11860 struct cp_binding_level *bl;
11861 tree current_function_parms;
11862 struct c_fileinfo *finfo
11863 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
11864 bool honor_interface;
11866 /* Sanity check. */
11867 gcc_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE);
11868 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
11870 fntype = TREE_TYPE (decl1);
11871 if (TREE_CODE (fntype) == METHOD_TYPE)
11872 ctype = TYPE_METHOD_BASETYPE (fntype);
11874 /* ISO C++ 11.4/5. A friend function defined in a class is in
11875 the (lexical) scope of the class in which it is defined. */
11876 if (!ctype && DECL_FRIEND_P (decl1))
11878 ctype = DECL_FRIEND_CONTEXT (decl1);
11880 /* CTYPE could be null here if we're dealing with a template;
11881 for example, `inline friend float foo()' inside a template
11882 will have no CTYPE set. */
11883 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
11884 ctype = NULL_TREE;
11885 else
11886 doing_friend = 1;
11889 if (DECL_DECLARED_INLINE_P (decl1)
11890 && lookup_attribute ("noinline", attrs))
11891 warning (0, "inline function %q+D given attribute noinline", decl1);
11893 /* Handle gnu_inline attribute. */
11894 if (GNU_INLINE_P (decl1))
11896 DECL_EXTERNAL (decl1) = 1;
11897 DECL_NOT_REALLY_EXTERN (decl1) = 0;
11898 DECL_INTERFACE_KNOWN (decl1) = 1;
11899 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
11902 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
11903 /* This is a constructor, we must ensure that any default args
11904 introduced by this definition are propagated to the clones
11905 now. The clones are used directly in overload resolution. */
11906 adjust_clone_args (decl1);
11908 /* Sometimes we don't notice that a function is a static member, and
11909 build a METHOD_TYPE for it. Fix that up now. */
11910 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
11911 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
11913 revert_static_member_fn (decl1);
11914 ctype = NULL_TREE;
11917 /* Set up current_class_type, and enter the scope of the class, if
11918 appropriate. */
11919 if (ctype)
11920 push_nested_class (ctype);
11921 else if (DECL_STATIC_FUNCTION_P (decl1))
11922 push_nested_class (DECL_CONTEXT (decl1));
11924 /* Now that we have entered the scope of the class, we must restore
11925 the bindings for any template parameters surrounding DECL1, if it
11926 is an inline member template. (Order is important; consider the
11927 case where a template parameter has the same name as a field of
11928 the class.) It is not until after this point that
11929 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
11930 if (flags & SF_INCLASS_INLINE)
11931 maybe_begin_member_template_processing (decl1);
11933 /* Effective C++ rule 15. */
11934 if (warn_ecpp
11935 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
11936 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
11937 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
11939 /* Make the init_value nonzero so pushdecl knows this is not tentative.
11940 error_mark_node is replaced below (in poplevel) with the BLOCK. */
11941 if (!DECL_INITIAL (decl1))
11942 DECL_INITIAL (decl1) = error_mark_node;
11944 /* This function exists in static storage.
11945 (This does not mean `static' in the C sense!) */
11946 TREE_STATIC (decl1) = 1;
11948 /* We must call push_template_decl after current_class_type is set
11949 up. (If we are processing inline definitions after exiting a
11950 class scope, current_class_type will be NULL_TREE until set above
11951 by push_nested_class.) */
11952 if (processing_template_decl)
11954 /* FIXME: Handle error_mark_node more gracefully. */
11955 tree newdecl1 = push_template_decl (decl1);
11956 if (newdecl1 != error_mark_node)
11957 decl1 = newdecl1;
11960 /* We are now in the scope of the function being defined. */
11961 current_function_decl = decl1;
11963 /* Save the parm names or decls from this function's declarator
11964 where store_parm_decls will find them. */
11965 current_function_parms = DECL_ARGUMENTS (decl1);
11967 /* Make sure the parameter and return types are reasonable. When
11968 you declare a function, these types can be incomplete, but they
11969 must be complete when you define the function. */
11970 check_function_type (decl1, current_function_parms);
11972 /* Build the return declaration for the function. */
11973 restype = TREE_TYPE (fntype);
11974 if (DECL_RESULT (decl1) == NULL_TREE)
11976 tree resdecl;
11978 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
11979 DECL_ARTIFICIAL (resdecl) = 1;
11980 DECL_IGNORED_P (resdecl) = 1;
11981 DECL_RESULT (decl1) = resdecl;
11983 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
11986 /* Let the user know we're compiling this function. */
11987 announce_function (decl1);
11989 /* Record the decl so that the function name is defined.
11990 If we already have a decl for this name, and it is a FUNCTION_DECL,
11991 use the old decl. */
11992 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
11994 /* A specialization is not used to guide overload resolution. */
11995 if (!DECL_FUNCTION_MEMBER_P (decl1)
11996 && !(DECL_USE_TEMPLATE (decl1) &&
11997 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
11999 tree olddecl = pushdecl (decl1);
12001 if (olddecl == error_mark_node)
12002 /* If something went wrong when registering the declaration,
12003 use DECL1; we have to have a FUNCTION_DECL to use when
12004 parsing the body of the function. */
12006 else
12008 /* Otherwise, OLDDECL is either a previous declaration
12009 of the same function or DECL1 itself. */
12011 if (warn_missing_declarations
12012 && olddecl == decl1
12013 && !DECL_MAIN_P (decl1)
12014 && TREE_PUBLIC (decl1)
12015 && !DECL_DECLARED_INLINE_P (decl1))
12017 tree context;
12019 /* Check whether DECL1 is in an anonymous
12020 namespace. */
12021 for (context = DECL_CONTEXT (decl1);
12022 context;
12023 context = DECL_CONTEXT (context))
12025 if (TREE_CODE (context) == NAMESPACE_DECL
12026 && DECL_NAME (context) == NULL_TREE)
12027 break;
12030 if (context == NULL)
12031 warning (OPT_Wmissing_declarations,
12032 "no previous declaration for %q+D", decl1);
12035 decl1 = olddecl;
12038 else
12040 /* We need to set the DECL_CONTEXT. */
12041 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
12042 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
12044 fntype = TREE_TYPE (decl1);
12046 /* If #pragma weak applies, mark the decl appropriately now.
12047 The pragma only applies to global functions. Because
12048 determining whether or not the #pragma applies involves
12049 computing the mangled name for the declaration, we cannot
12050 apply the pragma until after we have merged this declaration
12051 with any previous declarations; if the original declaration
12052 has a linkage specification, that specification applies to
12053 the definition as well, and may affect the mangled name. */
12054 if (!DECL_CONTEXT (decl1))
12055 maybe_apply_pragma_weak (decl1);
12058 /* Reset this in case the call to pushdecl changed it. */
12059 current_function_decl = decl1;
12061 gcc_assert (DECL_INITIAL (decl1));
12063 /* This function may already have been parsed, in which case just
12064 return; our caller will skip over the body without parsing. */
12065 if (DECL_INITIAL (decl1) != error_mark_node)
12066 return;
12068 /* Initialize RTL machinery. We cannot do this until
12069 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
12070 even when processing a template; this is how we get
12071 CFUN set up, and our per-function variables initialized.
12072 FIXME factor out the non-RTL stuff. */
12073 bl = current_binding_level;
12074 allocate_struct_function (decl1, processing_template_decl);
12076 /* Initialize the language data structures. Whenever we start
12077 a new function, we destroy temporaries in the usual way. */
12078 cfun->language = ggc_alloc_cleared_language_function ();
12079 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
12080 current_binding_level = bl;
12082 /* Even though we're inside a function body, we still don't want to
12083 call expand_expr to calculate the size of a variable-sized array.
12084 We haven't necessarily assigned RTL to all variables yet, so it's
12085 not safe to try to expand expressions involving them. */
12086 cfun->dont_save_pending_sizes_p = 1;
12088 /* Start the statement-tree, start the tree now. */
12089 DECL_SAVED_TREE (decl1) = push_stmt_list ();
12091 /* If we are (erroneously) defining a function that we have already
12092 defined before, wipe out what we knew before. */
12093 if (!DECL_PENDING_INLINE_P (decl1))
12094 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
12096 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
12098 /* We know that this was set up by `grokclassfn'. We do not
12099 wait until `store_parm_decls', since evil parse errors may
12100 never get us to that point. Here we keep the consistency
12101 between `current_class_type' and `current_class_ptr'. */
12102 tree t = DECL_ARGUMENTS (decl1);
12104 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
12105 gcc_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE);
12107 cp_function_chain->x_current_class_ref
12108 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
12109 cp_function_chain->x_current_class_ptr = t;
12111 /* Constructors and destructors need to know whether they're "in
12112 charge" of initializing virtual base classes. */
12113 t = DECL_CHAIN (t);
12114 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
12116 current_in_charge_parm = t;
12117 t = DECL_CHAIN (t);
12119 if (DECL_HAS_VTT_PARM_P (decl1))
12121 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
12122 current_vtt_parm = t;
12126 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
12127 /* Implicitly-defined methods (like the
12128 destructor for a class in which no destructor
12129 is explicitly declared) must not be defined
12130 until their definition is needed. So, we
12131 ignore interface specifications for
12132 compiler-generated functions. */
12133 && !DECL_ARTIFICIAL (decl1));
12135 if (DECL_INTERFACE_KNOWN (decl1))
12137 tree ctx = decl_function_context (decl1);
12139 if (DECL_NOT_REALLY_EXTERN (decl1))
12140 DECL_EXTERNAL (decl1) = 0;
12142 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
12143 && TREE_PUBLIC (ctx))
12144 /* This is a function in a local class in an extern inline
12145 function. */
12146 comdat_linkage (decl1);
12148 /* If this function belongs to an interface, it is public.
12149 If it belongs to someone else's interface, it is also external.
12150 This only affects inlines and template instantiations. */
12151 else if (!finfo->interface_unknown && honor_interface)
12153 if (DECL_DECLARED_INLINE_P (decl1)
12154 || DECL_TEMPLATE_INSTANTIATION (decl1)
12155 || processing_template_decl)
12157 DECL_EXTERNAL (decl1)
12158 = (finfo->interface_only
12159 || (DECL_DECLARED_INLINE_P (decl1)
12160 && ! flag_implement_inlines
12161 && !DECL_VINDEX (decl1)));
12163 /* For WIN32 we also want to put these in linkonce sections. */
12164 maybe_make_one_only (decl1);
12166 else
12167 DECL_EXTERNAL (decl1) = 0;
12168 DECL_INTERFACE_KNOWN (decl1) = 1;
12169 /* If this function is in an interface implemented in this file,
12170 make sure that the back end knows to emit this function
12171 here. */
12172 if (!DECL_EXTERNAL (decl1))
12173 mark_needed (decl1);
12175 else if (finfo->interface_unknown && finfo->interface_only
12176 && honor_interface)
12178 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
12179 interface, we will have both finfo->interface_unknown and
12180 finfo->interface_only set. In that case, we don't want to
12181 use the normal heuristics because someone will supply a
12182 #pragma implementation elsewhere, and deducing it here would
12183 produce a conflict. */
12184 comdat_linkage (decl1);
12185 DECL_EXTERNAL (decl1) = 0;
12186 DECL_INTERFACE_KNOWN (decl1) = 1;
12187 DECL_DEFER_OUTPUT (decl1) = 1;
12189 else
12191 /* This is a definition, not a reference.
12192 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
12193 if (!GNU_INLINE_P (decl1))
12194 DECL_EXTERNAL (decl1) = 0;
12196 if ((DECL_DECLARED_INLINE_P (decl1)
12197 || DECL_TEMPLATE_INSTANTIATION (decl1))
12198 && ! DECL_INTERFACE_KNOWN (decl1))
12199 DECL_DEFER_OUTPUT (decl1) = 1;
12200 else
12201 DECL_INTERFACE_KNOWN (decl1) = 1;
12204 /* Determine the ELF visibility attribute for the function. We must not
12205 do this before calling "pushdecl", as we must allow "duplicate_decls"
12206 to merge any attributes appropriately. We also need to wait until
12207 linkage is set. */
12208 if (!DECL_CLONED_FUNCTION_P (decl1))
12209 determine_visibility (decl1);
12211 begin_scope (sk_function_parms, decl1);
12213 ++function_depth;
12215 if (DECL_DESTRUCTOR_P (decl1)
12216 || (DECL_CONSTRUCTOR_P (decl1)
12217 && targetm.cxx.cdtor_returns_this ()))
12219 cdtor_label = build_decl (input_location,
12220 LABEL_DECL, NULL_TREE, NULL_TREE);
12221 DECL_CONTEXT (cdtor_label) = current_function_decl;
12224 start_fname_decls ();
12226 store_parm_decls (current_function_parms);
12230 /* Like start_preparsed_function, except that instead of a
12231 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
12233 Returns 1 on success. If the DECLARATOR is not suitable for a function
12234 (it defines a datum instead), we return 0, which tells
12235 yyparse to report a parse error. */
12238 start_function (cp_decl_specifier_seq *declspecs,
12239 const cp_declarator *declarator,
12240 tree attrs)
12242 tree decl1;
12244 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
12245 if (decl1 == error_mark_node)
12246 return 0;
12247 /* If the declarator is not suitable for a function definition,
12248 cause a syntax error. */
12249 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
12251 error ("invalid function declaration");
12252 return 0;
12255 if (DECL_MAIN_P (decl1))
12256 /* main must return int. grokfndecl should have corrected it
12257 (and issued a diagnostic) if the user got it wrong. */
12258 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
12259 integer_type_node));
12261 start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
12263 return 1;
12266 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
12267 FN. */
12269 static bool
12270 use_eh_spec_block (tree fn)
12272 return (flag_exceptions && flag_enforce_eh_specs
12273 && !processing_template_decl
12274 && !type_throw_all_p (TREE_TYPE (fn))
12275 /* We insert the EH_SPEC_BLOCK only in the original
12276 function; then, it is copied automatically to the
12277 clones. */
12278 && !DECL_CLONED_FUNCTION_P (fn)
12279 /* Implicitly-generated constructors and destructors have
12280 exception specifications. However, those specifications
12281 are the union of the possible exceptions specified by the
12282 constructors/destructors for bases and members, so no
12283 unallowed exception will ever reach this function. By
12284 not creating the EH_SPEC_BLOCK we save a little memory,
12285 and we avoid spurious warnings about unreachable
12286 code. */
12287 && !DECL_ARTIFICIAL (fn));
12290 /* Store the parameter declarations into the current function declaration.
12291 This is called after parsing the parameter declarations, before
12292 digesting the body of the function.
12294 Also install to binding contour return value identifier, if any. */
12296 static void
12297 store_parm_decls (tree current_function_parms)
12299 tree fndecl = current_function_decl;
12300 tree parm;
12302 /* This is a chain of any other decls that came in among the parm
12303 declarations. If a parm is declared with enum {foo, bar} x;
12304 then CONST_DECLs for foo and bar are put here. */
12305 tree nonparms = NULL_TREE;
12307 if (current_function_parms)
12309 /* This case is when the function was defined with an ANSI prototype.
12310 The parms already have decls, so we need not do anything here
12311 except record them as in effect
12312 and complain if any redundant old-style parm decls were written. */
12314 tree specparms = current_function_parms;
12315 tree next;
12317 /* Must clear this because it might contain TYPE_DECLs declared
12318 at class level. */
12319 current_binding_level->names = NULL;
12321 /* If we're doing semantic analysis, then we'll call pushdecl
12322 for each of these. We must do them in reverse order so that
12323 they end in the correct forward order. */
12324 specparms = nreverse (specparms);
12326 for (parm = specparms; parm; parm = next)
12328 next = DECL_CHAIN (parm);
12329 if (TREE_CODE (parm) == PARM_DECL)
12331 if (DECL_NAME (parm) == NULL_TREE
12332 || TREE_CODE (parm) != VOID_TYPE)
12333 pushdecl (parm);
12334 else
12335 error ("parameter %qD declared void", parm);
12337 else
12339 /* If we find an enum constant or a type tag,
12340 put it aside for the moment. */
12341 TREE_CHAIN (parm) = NULL_TREE;
12342 nonparms = chainon (nonparms, parm);
12346 /* Get the decls in their original chain order and record in the
12347 function. This is all and only the PARM_DECLs that were
12348 pushed into scope by the loop above. */
12349 DECL_ARGUMENTS (fndecl) = getdecls ();
12351 else
12352 DECL_ARGUMENTS (fndecl) = NULL_TREE;
12354 /* Now store the final chain of decls for the arguments
12355 as the decl-chain of the current lexical scope.
12356 Put the enumerators in as well, at the front so that
12357 DECL_ARGUMENTS is not modified. */
12358 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
12360 if (use_eh_spec_block (current_function_decl))
12361 current_eh_spec_block = begin_eh_spec_block ();
12365 /* We have finished doing semantic analysis on DECL, but have not yet
12366 generated RTL for its body. Save away our current state, so that
12367 when we want to generate RTL later we know what to do. */
12369 static void
12370 save_function_data (tree decl)
12372 struct language_function *f;
12374 /* Save the language-specific per-function data so that we can
12375 get it back when we really expand this function. */
12376 gcc_assert (!DECL_PENDING_INLINE_P (decl));
12378 /* Make a copy. */
12379 f = ggc_alloc_language_function ();
12380 memcpy (f, cp_function_chain, sizeof (struct language_function));
12381 DECL_SAVED_FUNCTION_DATA (decl) = f;
12383 /* Clear out the bits we don't need. */
12384 f->base.x_stmt_tree.x_cur_stmt_list = NULL_TREE;
12385 f->bindings = NULL;
12386 f->x_local_names = NULL;
12390 /* Set the return value of the constructor (if present). */
12392 static void
12393 finish_constructor_body (void)
12395 tree val;
12396 tree exprstmt;
12398 if (targetm.cxx.cdtor_returns_this ()
12399 && (! TYPE_FOR_JAVA (current_class_type)))
12401 /* Any return from a constructor will end up here. */
12402 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
12404 val = DECL_ARGUMENTS (current_function_decl);
12405 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
12406 DECL_RESULT (current_function_decl), val);
12407 /* Return the address of the object. */
12408 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
12409 add_stmt (exprstmt);
12413 /* Do all the processing for the beginning of a destructor; set up the
12414 vtable pointers and cleanups for bases and members. */
12416 static void
12417 begin_destructor_body (void)
12419 tree compound_stmt;
12421 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
12422 issued an error message. We still want to try to process the
12423 body of the function, but initialize_vtbl_ptrs will crash if
12424 TYPE_BINFO is NULL. */
12425 if (COMPLETE_TYPE_P (current_class_type))
12427 compound_stmt = begin_compound_stmt (0);
12428 /* Make all virtual function table pointers in non-virtual base
12429 classes point to CURRENT_CLASS_TYPE's virtual function
12430 tables. */
12431 initialize_vtbl_ptrs (current_class_ptr);
12432 finish_compound_stmt (compound_stmt);
12434 /* And insert cleanups for our bases and members so that they
12435 will be properly destroyed if we throw. */
12436 push_base_cleanups ();
12440 /* At the end of every destructor we generate code to delete the object if
12441 necessary. Do that now. */
12443 static void
12444 finish_destructor_body (void)
12446 tree exprstmt;
12448 /* Any return from a destructor will end up here; that way all base
12449 and member cleanups will be run when the function returns. */
12450 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
12452 /* In a virtual destructor, we must call delete. */
12453 if (DECL_VIRTUAL_P (current_function_decl))
12455 tree if_stmt;
12456 tree virtual_size = cxx_sizeof (current_class_type);
12458 /* [class.dtor]
12460 At the point of definition of a virtual destructor (including
12461 an implicit definition), non-placement operator delete shall
12462 be looked up in the scope of the destructor's class and if
12463 found shall be accessible and unambiguous. */
12464 exprstmt = build_op_delete_call(DELETE_EXPR, current_class_ptr,
12465 virtual_size,
12466 /*global_p=*/false,
12467 /*placement=*/NULL_TREE,
12468 /*alloc_fn=*/NULL_TREE);
12470 if_stmt = begin_if_stmt ();
12471 finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
12472 current_in_charge_parm,
12473 integer_one_node),
12474 if_stmt);
12475 finish_expr_stmt (exprstmt);
12476 finish_then_clause (if_stmt);
12477 finish_if_stmt (if_stmt);
12480 if (targetm.cxx.cdtor_returns_this ())
12482 tree val;
12484 val = DECL_ARGUMENTS (current_function_decl);
12485 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
12486 DECL_RESULT (current_function_decl), val);
12487 /* Return the address of the object. */
12488 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
12489 add_stmt (exprstmt);
12493 /* Do the necessary processing for the beginning of a function body, which
12494 in this case includes member-initializers, but not the catch clauses of
12495 a function-try-block. Currently, this means opening a binding level
12496 for the member-initializers (in a ctor) and member cleanups (in a dtor). */
12498 tree
12499 begin_function_body (void)
12501 tree stmt;
12503 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
12504 return NULL_TREE;
12506 if (processing_template_decl)
12507 /* Do nothing now. */;
12508 else
12509 /* Always keep the BLOCK node associated with the outermost pair of
12510 curly braces of a function. These are needed for correct
12511 operation of dwarfout.c. */
12512 keep_next_level (true);
12514 stmt = begin_compound_stmt (BCS_FN_BODY);
12516 if (processing_template_decl)
12517 /* Do nothing now. */;
12518 else if (DECL_DESTRUCTOR_P (current_function_decl))
12519 begin_destructor_body ();
12521 return stmt;
12524 /* Do the processing for the end of a function body. Currently, this means
12525 closing out the cleanups for fully-constructed bases and members, and in
12526 the case of the destructor, deleting the object if desired. Again, this
12527 is only meaningful for [cd]tors, since they are the only functions where
12528 there is a significant distinction between the main body and any
12529 function catch clauses. Handling, say, main() return semantics here
12530 would be wrong, as flowing off the end of a function catch clause for
12531 main() would also need to return 0. */
12533 void
12534 finish_function_body (tree compstmt)
12536 if (compstmt == NULL_TREE)
12537 return;
12539 /* Close the block. */
12540 finish_compound_stmt (compstmt);
12542 if (processing_template_decl)
12543 /* Do nothing now. */;
12544 else if (DECL_CONSTRUCTOR_P (current_function_decl))
12545 finish_constructor_body ();
12546 else if (DECL_DESTRUCTOR_P (current_function_decl))
12547 finish_destructor_body ();
12550 /* Given a function, returns the BLOCK corresponding to the outermost level
12551 of curly braces, skipping the artificial block created for constructor
12552 initializers. */
12554 tree
12555 outer_curly_brace_block (tree fndecl)
12557 tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl));
12558 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
12559 /* Skip the artificial function body block. */
12560 block = BLOCK_SUBBLOCKS (block);
12561 return block;
12564 /* If FNDECL is a class's key method, add the class to the list of
12565 keyed classes that should be emitted. */
12567 static void
12568 record_key_method_defined (tree fndecl)
12570 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
12571 && DECL_VIRTUAL_P (fndecl)
12572 && !processing_template_decl)
12574 tree fnclass = DECL_CONTEXT (fndecl);
12575 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
12576 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
12580 /* Finish up a function declaration and compile that function
12581 all the way to assembler language output. The free the storage
12582 for the function definition.
12584 FLAGS is a bitwise or of the following values:
12585 2 - INCLASS_INLINE
12586 We just finished processing the body of an in-class inline
12587 function definition. (This processing will have taken place
12588 after the class definition is complete.) */
12590 tree
12591 finish_function (int flags)
12593 tree fndecl = current_function_decl;
12594 tree fntype, ctype = NULL_TREE;
12595 int inclass_inline = (flags & 2) != 0;
12596 int nested;
12598 /* When we get some parse errors, we can end up without a
12599 current_function_decl, so cope. */
12600 if (fndecl == NULL_TREE)
12601 return error_mark_node;
12603 gcc_assert (!defer_mark_used_calls);
12604 defer_mark_used_calls = true;
12606 record_key_method_defined (fndecl);
12608 nested = function_depth > 1;
12609 fntype = TREE_TYPE (fndecl);
12611 /* TREE_READONLY (fndecl) = 1;
12612 This caused &foo to be of type ptr-to-const-function
12613 which then got a warning when stored in a ptr-to-function variable. */
12615 gcc_assert (building_stmt_tree ());
12616 /* The current function is being defined, so its DECL_INITIAL should
12617 be set, and unless there's a multiple definition, it should be
12618 error_mark_node. */
12619 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
12621 /* For a cloned function, we've already got all the code we need;
12622 there's no need to add any extra bits. */
12623 if (!DECL_CLONED_FUNCTION_P (fndecl))
12625 if (DECL_MAIN_P (current_function_decl))
12627 tree stmt;
12629 /* Make it so that `main' always returns 0 by default (or
12630 1 for VMS). */
12631 #if VMS_TARGET
12632 stmt = finish_return_stmt (integer_one_node);
12633 #else
12634 stmt = finish_return_stmt (integer_zero_node);
12635 #endif
12636 /* Hack. We don't want the middle-end to warn that this
12637 return is unreachable, so put the statement on the
12638 special line 0. */
12640 location_t linezero = linemap_line_start (line_table, 0, 1);
12641 SET_EXPR_LOCATION (stmt, linezero);
12645 if (use_eh_spec_block (current_function_decl))
12646 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
12647 (TREE_TYPE (current_function_decl)),
12648 current_eh_spec_block);
12651 /* If we're saving up tree structure, tie off the function now. */
12652 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
12654 finish_fname_decls ();
12656 /* If this function can't throw any exceptions, remember that. */
12657 if (!processing_template_decl
12658 && !cp_function_chain->can_throw
12659 && !flag_non_call_exceptions
12660 && !DECL_REPLACEABLE_P (fndecl))
12661 TREE_NOTHROW (fndecl) = 1;
12663 /* This must come after expand_function_end because cleanups might
12664 have declarations (from inline functions) that need to go into
12665 this function's blocks. */
12667 /* If the current binding level isn't the outermost binding level
12668 for this function, either there is a bug, or we have experienced
12669 syntax errors and the statement tree is malformed. */
12670 if (current_binding_level->kind != sk_function_parms)
12672 /* Make sure we have already experienced errors. */
12673 gcc_assert (errorcount);
12675 /* Throw away the broken statement tree and extra binding
12676 levels. */
12677 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
12679 while (current_binding_level->kind != sk_function_parms)
12681 if (current_binding_level->kind == sk_class)
12682 pop_nested_class ();
12683 else
12684 poplevel (0, 0, 0);
12687 poplevel (1, 0, 1);
12689 /* Statements should always be full-expressions at the outermost set
12690 of curly braces for a function. */
12691 gcc_assert (stmts_are_full_exprs_p ());
12693 /* Set up the named return value optimization, if we can. Candidate
12694 variables are selected in check_return_expr. */
12695 if (current_function_return_value)
12697 tree r = current_function_return_value;
12698 tree outer;
12700 if (r != error_mark_node
12701 /* This is only worth doing for fns that return in memory--and
12702 simpler, since we don't have to worry about promoted modes. */
12703 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
12704 /* Only allow this for variables declared in the outer scope of
12705 the function so we know that their lifetime always ends with a
12706 return; see g++.dg/opt/nrv6.C. We could be more flexible if
12707 we were to do this optimization in tree-ssa. */
12708 && (outer = outer_curly_brace_block (fndecl))
12709 && chain_member (r, BLOCK_VARS (outer)))
12710 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
12712 current_function_return_value = NULL_TREE;
12715 /* Remember that we were in class scope. */
12716 if (current_class_name)
12717 ctype = current_class_type;
12719 /* Must mark the RESULT_DECL as being in this function. */
12720 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
12722 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
12723 to the FUNCTION_DECL node itself. */
12724 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
12726 /* Save away current state, if appropriate. */
12727 if (!processing_template_decl)
12728 save_function_data (fndecl);
12730 /* Complain if there's just no return statement. */
12731 if (warn_return_type
12732 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
12733 && !dependent_type_p (TREE_TYPE (fntype))
12734 && !current_function_returns_value && !current_function_returns_null
12735 /* Don't complain if we abort or throw. */
12736 && !current_function_returns_abnormally
12737 /* Don't complain if we are declared noreturn. */
12738 && !TREE_THIS_VOLATILE (fndecl)
12739 && !DECL_NAME (DECL_RESULT (fndecl))
12740 && !TREE_NO_WARNING (fndecl)
12741 /* Structor return values (if any) are set by the compiler. */
12742 && !DECL_CONSTRUCTOR_P (fndecl)
12743 && !DECL_DESTRUCTOR_P (fndecl))
12745 warning (OPT_Wreturn_type,
12746 "no return statement in function returning non-void");
12747 TREE_NO_WARNING (fndecl) = 1;
12750 /* Store the end of the function, so that we get good line number
12751 info for the epilogue. */
12752 cfun->function_end_locus = input_location;
12754 /* Complain about parameters that are only set, but never otherwise used. */
12755 if (warn_unused_but_set_parameter
12756 && !processing_template_decl
12757 && errorcount == unused_but_set_errorcount
12758 && !DECL_CLONED_FUNCTION_P (fndecl))
12760 tree decl;
12762 for (decl = DECL_ARGUMENTS (fndecl);
12763 decl;
12764 decl = DECL_CHAIN (decl))
12765 if (TREE_USED (decl)
12766 && TREE_CODE (decl) == PARM_DECL
12767 && !DECL_READ_P (decl)
12768 && DECL_NAME (decl)
12769 && !DECL_ARTIFICIAL (decl)
12770 && !TREE_NO_WARNING (decl)
12771 && !DECL_IN_SYSTEM_HEADER (decl)
12772 && TREE_TYPE (decl) != error_mark_node
12773 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12774 && (!CLASS_TYPE_P (TREE_TYPE (decl))
12775 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
12776 warning (OPT_Wunused_but_set_parameter,
12777 "parameter %q+D set but not used", decl);
12778 unused_but_set_errorcount = errorcount;
12781 /* Genericize before inlining. */
12782 if (!processing_template_decl)
12784 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
12785 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
12786 cp_genericize (fndecl);
12787 /* Clear out the bits we don't need. */
12788 f->x_current_class_ptr = NULL;
12789 f->x_current_class_ref = NULL;
12790 f->x_eh_spec_block = NULL;
12791 f->x_in_charge_parm = NULL;
12792 f->x_vtt_parm = NULL;
12793 f->x_return_value = NULL;
12794 f->bindings = NULL;
12795 f->extern_decl_map = NULL;
12797 /* Clear out the bits we don't need. */
12798 local_names = NULL;
12800 /* We're leaving the context of this function, so zap cfun. It's still in
12801 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
12802 set_cfun (NULL);
12803 current_function_decl = NULL;
12805 /* If this is an in-class inline definition, we may have to pop the
12806 bindings for the template parameters that we added in
12807 maybe_begin_member_template_processing when start_function was
12808 called. */
12809 if (inclass_inline)
12810 maybe_end_member_template_processing ();
12812 /* Leave the scope of the class. */
12813 if (ctype)
12814 pop_nested_class ();
12816 --function_depth;
12818 /* Clean up. */
12819 if (! nested)
12820 /* Let the error reporting routines know that we're outside a
12821 function. For a nested function, this value is used in
12822 cxx_pop_function_context and then reset via pop_function_context. */
12823 current_function_decl = NULL_TREE;
12825 defer_mark_used_calls = false;
12826 if (deferred_mark_used_calls)
12828 unsigned int i;
12829 tree decl;
12831 for (i = 0; VEC_iterate (tree, deferred_mark_used_calls, i, decl); i++)
12832 mark_used (decl);
12833 VEC_free (tree, gc, deferred_mark_used_calls);
12836 return fndecl;
12839 /* Create the FUNCTION_DECL for a function definition.
12840 DECLSPECS and DECLARATOR are the parts of the declaration;
12841 they describe the return type and the name of the function,
12842 but twisted together in a fashion that parallels the syntax of C.
12844 This function creates a binding context for the function body
12845 as well as setting up the FUNCTION_DECL in current_function_decl.
12847 Returns a FUNCTION_DECL on success.
12849 If the DECLARATOR is not suitable for a function (it defines a datum
12850 instead), we return 0, which tells yyparse to report a parse error.
12852 May return void_type_node indicating that this method is actually
12853 a friend. See grokfield for more details.
12855 Came here with a `.pushlevel' .
12857 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
12858 CHANGES TO CODE IN `grokfield'. */
12860 tree
12861 grokmethod (cp_decl_specifier_seq *declspecs,
12862 const cp_declarator *declarator, tree attrlist)
12864 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
12865 &attrlist);
12867 if (fndecl == error_mark_node)
12868 return error_mark_node;
12870 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
12872 error ("invalid member function declaration");
12873 return error_mark_node;
12876 if (attrlist)
12877 cplus_decl_attributes (&fndecl, attrlist, 0);
12879 /* Pass friends other than inline friend functions back. */
12880 if (fndecl == void_type_node)
12881 return fndecl;
12883 if (DECL_IN_AGGR_P (fndecl))
12885 if (DECL_CONTEXT (fndecl)
12886 && TREE_CODE (DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
12887 error ("%qD is already defined in class %qT", fndecl,
12888 DECL_CONTEXT (fndecl));
12889 return error_mark_node;
12892 check_template_shadow (fndecl);
12894 DECL_DECLARED_INLINE_P (fndecl) = 1;
12895 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
12897 /* We process method specializations in finish_struct_1. */
12898 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
12900 fndecl = push_template_decl (fndecl);
12901 if (fndecl == error_mark_node)
12902 return fndecl;
12905 if (! DECL_FRIEND_P (fndecl))
12907 if (DECL_CHAIN (fndecl))
12909 fndecl = copy_node (fndecl);
12910 TREE_CHAIN (fndecl) = NULL_TREE;
12914 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
12916 DECL_IN_AGGR_P (fndecl) = 1;
12917 return fndecl;
12921 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
12922 we can lay it out later, when and if its type becomes complete. */
12924 void
12925 maybe_register_incomplete_var (tree var)
12927 gcc_assert (TREE_CODE (var) == VAR_DECL);
12929 /* Keep track of variables with incomplete types. */
12930 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
12931 && DECL_EXTERNAL (var))
12933 tree inner_type = TREE_TYPE (var);
12935 while (TREE_CODE (inner_type) == ARRAY_TYPE)
12936 inner_type = TREE_TYPE (inner_type);
12937 inner_type = TYPE_MAIN_VARIANT (inner_type);
12939 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
12940 /* RTTI TD entries are created while defining the type_info. */
12941 || (TYPE_LANG_SPECIFIC (inner_type)
12942 && TYPE_BEING_DEFINED (inner_type)))
12944 incomplete_var *iv
12945 = VEC_safe_push (incomplete_var, gc, incomplete_vars, NULL);
12946 iv->decl = var;
12947 iv->incomplete_type = inner_type;
12952 /* Called when a class type (given by TYPE) is defined. If there are
12953 any existing VAR_DECLs whose type has been completed by this
12954 declaration, update them now. */
12956 void
12957 complete_vars (tree type)
12959 unsigned ix;
12960 incomplete_var *iv;
12962 for (ix = 0; VEC_iterate (incomplete_var, incomplete_vars, ix, iv); )
12964 if (same_type_p (type, iv->incomplete_type))
12966 tree var = iv->decl;
12967 tree type = TREE_TYPE (var);
12968 /* Complete the type of the variable. The VAR_DECL itself
12969 will be laid out in expand_expr. */
12970 complete_type (type);
12971 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
12972 /* Remove this entry from the list. */
12973 VEC_unordered_remove (incomplete_var, incomplete_vars, ix);
12975 else
12976 ix++;
12979 /* Check for pending declarations which may have abstract type. */
12980 complete_type_check_abstract (type);
12983 /* If DECL is of a type which needs a cleanup, build and return an
12984 expression to perform that cleanup here. Return NULL_TREE if no
12985 cleanup need be done. */
12987 tree
12988 cxx_maybe_build_cleanup (tree decl)
12990 tree type;
12991 tree attr;
12992 tree cleanup;
12994 /* Assume no cleanup is required. */
12995 cleanup = NULL_TREE;
12997 if (error_operand_p (decl))
12998 return cleanup;
13000 /* Handle "__attribute__((cleanup))". We run the cleanup function
13001 before the destructor since the destructor is what actually
13002 terminates the lifetime of the object. */
13003 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
13004 if (attr)
13006 tree id;
13007 tree fn;
13008 tree arg;
13010 /* Get the name specified by the user for the cleanup function. */
13011 id = TREE_VALUE (TREE_VALUE (attr));
13012 /* Look up the name to find the cleanup function to call. It is
13013 important to use lookup_name here because that is what is
13014 used in c-common.c:handle_cleanup_attribute when performing
13015 initial checks on the attribute. Note that those checks
13016 include ensuring that the function found is not an overloaded
13017 function, or an object with an overloaded call operator,
13018 etc.; we can rely on the fact that the function found is an
13019 ordinary FUNCTION_DECL. */
13020 fn = lookup_name (id);
13021 arg = build_address (decl);
13022 mark_used (decl);
13023 cleanup = cp_build_function_call_nary (fn, tf_warning_or_error,
13024 arg, NULL_TREE);
13026 /* Handle ordinary C++ destructors. */
13027 type = TREE_TYPE (decl);
13028 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
13030 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
13031 bool has_vbases = (TREE_CODE (type) == RECORD_TYPE
13032 && CLASSTYPE_VBASECLASSES (type));
13033 tree addr;
13034 tree call;
13036 if (TREE_CODE (type) == ARRAY_TYPE)
13037 addr = decl;
13038 else
13039 addr = build_address (decl);
13041 /* Optimize for space over speed here. */
13042 if (!has_vbases || flag_expensive_optimizations)
13043 flags |= LOOKUP_NONVIRTUAL;
13045 call = build_delete (TREE_TYPE (addr), addr,
13046 sfk_complete_destructor, flags, 0);
13047 if (cleanup)
13048 cleanup = build_compound_expr (input_location, cleanup, call);
13049 else
13050 cleanup = call;
13053 return cleanup;
13056 /* When a stmt has been parsed, this function is called. */
13058 void
13059 finish_stmt (void)
13063 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
13064 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
13065 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
13067 tree
13068 static_fn_type (tree memfntype)
13070 tree fntype;
13071 tree args;
13073 if (TYPE_PTRMEMFUNC_P (memfntype))
13074 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
13075 if (POINTER_TYPE_P (memfntype)
13076 || TREE_CODE (memfntype) == FUNCTION_DECL)
13077 memfntype = TREE_TYPE (memfntype);
13078 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
13079 return memfntype;
13080 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
13081 args = TYPE_ARG_TYPES (memfntype);
13082 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
13083 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
13084 fntype = (cp_build_type_attribute_variant
13085 (fntype, TYPE_ATTRIBUTES (memfntype)));
13086 fntype = (build_exception_variant
13087 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
13088 return fntype;
13091 /* DECL was originally constructed as a non-static member function,
13092 but turned out to be static. Update it accordingly. */
13094 void
13095 revert_static_member_fn (tree decl)
13097 TREE_TYPE (decl) = static_fn_type (decl);
13099 if (cp_type_quals (TREE_TYPE (decl)) != TYPE_UNQUALIFIED)
13100 error ("static member function %q#D declared with type qualifiers", decl);
13102 if (DECL_ARGUMENTS (decl))
13103 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
13104 DECL_STATIC_FUNCTION_P (decl) = 1;
13107 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
13108 one of the language-independent trees. */
13110 enum cp_tree_node_structure_enum
13111 cp_tree_node_structure (union lang_tree_node * t)
13113 switch (TREE_CODE (&t->generic))
13115 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
13116 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
13117 case OVERLOAD: return TS_CP_OVERLOAD;
13118 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
13119 case PTRMEM_CST: return TS_CP_PTRMEM;
13120 case BASELINK: return TS_CP_BASELINK;
13121 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
13122 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
13123 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
13124 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
13125 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
13126 default: return TS_CP_GENERIC;
13130 /* Build the void_list_node (void_type_node having been created). */
13131 tree
13132 build_void_list_node (void)
13134 tree t = build_tree_list (NULL_TREE, void_type_node);
13135 return t;
13138 bool
13139 cp_missing_noreturn_ok_p (tree decl)
13141 /* A missing noreturn is ok for the `main' function. */
13142 return DECL_MAIN_P (decl);
13145 /* Return the COMDAT group into which DECL should be placed. */
13147 tree
13148 cxx_comdat_group (tree decl)
13150 tree name;
13152 /* Virtual tables, construction virtual tables, and virtual table
13153 tables all go in a single COMDAT group, named after the primary
13154 virtual table. */
13155 if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
13156 name = DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (DECL_CONTEXT (decl)));
13157 /* For all other DECLs, the COMDAT group is the mangled name of the
13158 declaration itself. */
13159 else
13161 while (DECL_THUNK_P (decl))
13163 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
13164 into the same section as the target function. In that case
13165 we must return target's name. */
13166 tree target = THUNK_TARGET (decl);
13167 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
13168 && DECL_SECTION_NAME (target) != NULL
13169 && DECL_ONE_ONLY (target))
13170 decl = target;
13171 else
13172 break;
13174 name = DECL_ASSEMBLER_NAME (decl);
13177 return name;
13180 #include "gt-cp-decl.h"