* config/i386/i386.h (GENERAL_REGNO_P): Use STACK_POINTER_REGNUM.
[official-gcc.git] / gcc / cp / decl.c
blob17b209a1f14a87ee3ef02a369c907c38b7bd0d20
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 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
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 "rtl.h"
37 #include "expr.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "output.h"
43 #include "except.h"
44 #include "toplev.h"
45 #include "hashtab.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "c-common.h"
49 #include "c-pragma.h"
50 #include "diagnostic.h"
51 #include "debug.h"
52 #include "timevar.h"
53 #include "tree-flow.h"
55 static tree grokparms (cp_parameter_declarator *, tree *);
56 static const char *redeclaration_error_message (tree, tree);
58 static int decl_jump_unsafe (tree);
59 static void require_complete_types_for_parms (tree);
60 static int ambi_op_p (enum tree_code);
61 static int unary_op_p (enum tree_code);
62 static void push_local_name (tree);
63 static tree grok_reference_init (tree, tree, tree, tree *);
64 static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
65 int, int, tree);
66 static void record_unknown_type (tree, const char *);
67 static tree builtin_function_1 (tree, tree);
68 static tree build_library_fn_1 (tree, enum tree_code, tree);
69 static int member_function_or_else (tree, tree, enum overload_flags);
70 static void bad_specifiers (tree, const char *, int, int, int, int,
71 int);
72 static void check_for_uninitialized_const_var (tree);
73 static hashval_t typename_hash (const void *);
74 static int typename_compare (const void *, const void *);
75 static tree local_variable_p_walkfn (tree *, int *, void *);
76 static tree record_builtin_java_type (const char *, int);
77 static const char *tag_name (enum tag_types);
78 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
79 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
80 static void maybe_deduce_size_from_array_init (tree, tree);
81 static void layout_var_decl (tree);
82 static void maybe_commonize_var (tree);
83 static tree check_initializer (tree, tree, int, tree *);
84 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
85 static void save_function_data (tree);
86 static void check_function_type (tree, tree);
87 static void finish_constructor_body (void);
88 static void begin_destructor_body (void);
89 static void finish_destructor_body (void);
90 static tree create_array_type_for_decl (tree, tree, tree);
91 static tree get_atexit_node (void);
92 static tree get_dso_handle_node (void);
93 static tree start_cleanup_fn (void);
94 static void end_cleanup_fn (void);
95 static tree cp_make_fname_decl (tree, int);
96 static void initialize_predefined_identifiers (void);
97 static tree check_special_function_return_type
98 (special_function_kind, tree, tree);
99 static tree push_cp_library_fn (enum tree_code, tree);
100 static tree build_cp_library_fn (tree, enum tree_code, tree);
101 static void store_parm_decls (tree);
102 static void initialize_local_var (tree, tree);
103 static void expand_static_init (tree, tree);
104 static tree next_initializable_field (tree);
106 /* The following symbols are subsumed in the cp_global_trees array, and
107 listed here individually for documentation purposes.
109 C++ extensions
110 tree wchar_decl_node;
112 tree vtable_entry_type;
113 tree delta_type_node;
114 tree __t_desc_type_node;
116 tree class_type_node;
117 tree unknown_type_node;
119 Array type `vtable_entry_type[]'
121 tree vtbl_type_node;
122 tree vtbl_ptr_type_node;
124 Namespaces,
126 tree std_node;
127 tree abi_node;
129 A FUNCTION_DECL which can call `abort'. Not necessarily the
130 one that the user will declare, but sufficient to be called
131 by routines that want to abort the program.
133 tree abort_fndecl;
135 The FUNCTION_DECL for the default `::operator delete'.
137 tree global_delete_fndecl;
139 Used by RTTI
140 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
141 tree tinfo_var_id; */
143 tree cp_global_trees[CPTI_MAX];
145 /* Indicates that there is a type value in some namespace, although
146 that is not necessarily in scope at the moment. */
148 tree global_type_node;
150 /* The node that holds the "name" of the global scope. */
151 tree global_scope_name;
153 #define local_names cp_function_chain->x_local_names
155 /* A list of objects which have constructors or destructors
156 which reside in the global scope. The decl is stored in
157 the TREE_VALUE slot and the initializer is stored
158 in the TREE_PURPOSE slot. */
159 tree static_aggregates;
161 /* -- end of C++ */
163 /* A node for the integer constants 2, and 3. */
165 tree integer_two_node, integer_three_node;
167 /* Used only for jumps to as-yet undefined labels, since jumps to
168 defined labels can have their validity checked immediately. */
170 struct named_label_use_entry GTY(())
172 struct named_label_use_entry *next;
173 /* The binding level to which this entry is *currently* attached.
174 This is initially the binding level in which the goto appeared,
175 but is modified as scopes are closed. */
176 struct cp_binding_level *binding_level;
177 /* The head of the names list that was current when the goto appeared,
178 or the inner scope popped. These are the decls that will *not* be
179 skipped when jumping to the label. */
180 tree names_in_scope;
181 /* The location of the goto, for error reporting. */
182 location_t o_goto_locus;
183 /* True if an OpenMP structured block scope has been closed since
184 the goto appeared. This means that the branch from the label will
185 illegally exit an OpenMP scope. */
186 bool in_omp_scope;
189 /* A list of all LABEL_DECLs in the function that have names. Here so
190 we can clear out their names' definitions at the end of the
191 function, and so we can check the validity of jumps to these labels. */
193 struct named_label_entry GTY(())
195 /* The decl itself. */
196 tree label_decl;
198 /* The binding level to which the label is *currently* attached.
199 This is initially set to the binding level in which the label
200 is defined, but is modified as scopes are closed. */
201 struct cp_binding_level *binding_level;
202 /* The head of the names list that was current when the label was
203 defined, or the inner scope popped. These are the decls that will
204 be skipped when jumping to the label. */
205 tree names_in_scope;
206 /* A tree list of all decls from all binding levels that would be
207 crossed by a backward branch to the label. */
208 tree bad_decls;
210 /* A list of uses of the label, before the label is defined. */
211 struct named_label_use_entry *uses;
213 /* The following bits are set after the label is defined, and are
214 updated as scopes are popped. They indicate that a backward jump
215 to the label will illegally enter a scope of the given flavor. */
216 bool in_try_scope;
217 bool in_catch_scope;
218 bool in_omp_scope;
221 #define named_labels cp_function_chain->x_named_labels
223 /* The number of function bodies which we are currently processing.
224 (Zero if we are at namespace scope, one inside the body of a
225 function, two inside the body of a function in a local class, etc.) */
226 int function_depth;
228 /* States indicating how grokdeclarator() should handle declspecs marked
229 with __attribute__((deprecated)). An object declared as
230 __attribute__((deprecated)) suppresses warnings of uses of other
231 deprecated items. */
233 enum deprecated_states {
234 DEPRECATED_NORMAL,
235 DEPRECATED_SUPPRESS
238 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
241 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
242 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
243 time the VAR_DECL was declared, the type was incomplete. */
245 static GTY(()) tree incomplete_vars;
247 /* Returns the kind of template specialization we are currently
248 processing, given that it's declaration contained N_CLASS_SCOPES
249 explicit scope qualifications. */
251 tmpl_spec_kind
252 current_tmpl_spec_kind (int n_class_scopes)
254 int n_template_parm_scopes = 0;
255 int seen_specialization_p = 0;
256 int innermost_specialization_p = 0;
257 struct cp_binding_level *b;
259 /* Scan through the template parameter scopes. */
260 for (b = current_binding_level;
261 b->kind == sk_template_parms;
262 b = b->level_chain)
264 /* If we see a specialization scope inside a parameter scope,
265 then something is wrong. That corresponds to a declaration
266 like:
268 template <class T> template <> ...
270 which is always invalid since [temp.expl.spec] forbids the
271 specialization of a class member template if the enclosing
272 class templates are not explicitly specialized as well. */
273 if (b->explicit_spec_p)
275 if (n_template_parm_scopes == 0)
276 innermost_specialization_p = 1;
277 else
278 seen_specialization_p = 1;
280 else if (seen_specialization_p == 1)
281 return tsk_invalid_member_spec;
283 ++n_template_parm_scopes;
286 /* Handle explicit instantiations. */
287 if (processing_explicit_instantiation)
289 if (n_template_parm_scopes != 0)
290 /* We've seen a template parameter list during an explicit
291 instantiation. For example:
293 template <class T> template void f(int);
295 This is erroneous. */
296 return tsk_invalid_expl_inst;
297 else
298 return tsk_expl_inst;
301 if (n_template_parm_scopes < n_class_scopes)
302 /* We've not seen enough template headers to match all the
303 specialized classes present. For example:
305 template <class T> void R<T>::S<T>::f(int);
307 This is invalid; there needs to be one set of template
308 parameters for each class. */
309 return tsk_insufficient_parms;
310 else if (n_template_parm_scopes == n_class_scopes)
311 /* We're processing a non-template declaration (even though it may
312 be a member of a template class.) For example:
314 template <class T> void S<T>::f(int);
316 The `class T' maches the `S<T>', leaving no template headers
317 corresponding to the `f'. */
318 return tsk_none;
319 else if (n_template_parm_scopes > n_class_scopes + 1)
320 /* We've got too many template headers. For example:
322 template <> template <class T> void f (T);
324 There need to be more enclosing classes. */
325 return tsk_excessive_parms;
326 else
327 /* This must be a template. It's of the form:
329 template <class T> template <class U> void S<T>::f(U);
331 This is a specialization if the innermost level was a
332 specialization; otherwise it's just a definition of the
333 template. */
334 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
337 /* Exit the current scope. */
339 void
340 finish_scope (void)
342 poplevel (0, 0, 0);
345 /* When a label goes out of scope, check to see if that label was used
346 in a valid manner, and issue any appropriate warnings or errors. */
348 static void
349 pop_label (tree label, tree old_value)
351 if (!processing_template_decl)
353 if (DECL_INITIAL (label) == NULL_TREE)
355 location_t location;
357 error ("label %q+D used but not defined", label);
358 #ifdef USE_MAPPED_LOCATION
359 location = input_location; /* FIXME want (input_filename, (line)0) */
360 #else
361 location.file = input_filename;
362 location.line = 0;
363 #endif
364 /* Avoid crashing later. */
365 define_label (location, DECL_NAME (label));
367 else if (!TREE_USED (label))
368 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
371 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
374 /* At the end of a function, all labels declared within the function
375 go out of scope. BLOCK is the top-level block for the
376 function. */
378 static int
379 pop_labels_1 (void **slot, void *data)
381 struct named_label_entry *ent = (struct named_label_entry *) *slot;
382 tree block = (tree) data;
384 pop_label (ent->label_decl, NULL_TREE);
386 /* Put the labels into the "variables" of the top-level block,
387 so debugger can see them. */
388 TREE_CHAIN (ent->label_decl) = BLOCK_VARS (block);
389 BLOCK_VARS (block) = ent->label_decl;
391 htab_clear_slot (named_labels, slot);
393 return 1;
396 static void
397 pop_labels (tree block)
399 if (named_labels)
401 htab_traverse (named_labels, pop_labels_1, block);
402 named_labels = NULL;
406 /* At the end of a block with local labels, restore the outer definition. */
408 static void
409 pop_local_label (tree label, tree old_value)
411 struct named_label_entry dummy;
412 void **slot;
414 pop_label (label, old_value);
416 dummy.label_decl = label;
417 slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
418 htab_clear_slot (named_labels, slot);
421 /* The following two routines are used to interface to Objective-C++.
422 The binding level is purposely treated as an opaque type. */
424 void *
425 objc_get_current_scope (void)
427 return current_binding_level;
430 /* The following routine is used by the NeXT-style SJLJ exceptions;
431 variables get marked 'volatile' so as to not be clobbered by
432 _setjmp()/_longjmp() calls. All variables in the current scope,
433 as well as parent scopes up to (but not including) ENCLOSING_BLK
434 shall be thusly marked. */
436 void
437 objc_mark_locals_volatile (void *enclosing_blk)
439 struct cp_binding_level *scope;
441 for (scope = current_binding_level;
442 scope && scope != enclosing_blk;
443 scope = scope->level_chain)
445 tree decl;
447 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
448 objc_volatilize_decl (decl);
450 /* Do not climb up past the current function. */
451 if (scope->kind == sk_function_parms)
452 break;
456 /* Update data for defined and undefined labels when leaving a scope. */
458 static int
459 poplevel_named_label_1 (void **slot, void *data)
461 struct named_label_entry *ent = (struct named_label_entry *) *slot;
462 struct cp_binding_level *bl = (struct cp_binding_level *) data;
463 struct cp_binding_level *obl = bl->level_chain;
465 if (ent->binding_level == bl)
467 tree decl;
469 for (decl = ent->names_in_scope; decl; decl = TREE_CHAIN (decl))
470 if (decl_jump_unsafe (decl))
471 ent->bad_decls = tree_cons (NULL, decl, ent->bad_decls);
473 ent->binding_level = obl;
474 ent->names_in_scope = obl->names;
475 switch (bl->kind)
477 case sk_try:
478 ent->in_try_scope = true;
479 break;
480 case sk_catch:
481 ent->in_catch_scope = true;
482 break;
483 case sk_omp:
484 ent->in_omp_scope = true;
485 break;
486 default:
487 break;
490 else if (ent->uses)
492 struct named_label_use_entry *use;
494 for (use = ent->uses; use ; use = use->next)
495 if (use->binding_level == bl)
497 use->binding_level = obl;
498 use->names_in_scope = obl->names;
499 if (bl->kind == sk_omp)
500 use->in_omp_scope = true;
504 return 1;
507 /* Exit a binding level.
508 Pop the level off, and restore the state of the identifier-decl mappings
509 that were in effect when this level was entered.
511 If KEEP == 1, this level had explicit declarations, so
512 and create a "block" (a BLOCK node) for the level
513 to record its declarations and subblocks for symbol table output.
515 If FUNCTIONBODY is nonzero, this level is the body of a function,
516 so create a block as if KEEP were set and also clear out all
517 label names.
519 If REVERSE is nonzero, reverse the order of decls before putting
520 them into the BLOCK. */
522 tree
523 poplevel (int keep, int reverse, int functionbody)
525 tree link;
526 /* The chain of decls was accumulated in reverse order.
527 Put it into forward order, just for cleanliness. */
528 tree decls;
529 int tmp = functionbody;
530 int real_functionbody;
531 tree subblocks;
532 tree block;
533 tree decl;
534 int leaving_for_scope;
535 scope_kind kind;
537 timevar_push (TV_NAME_LOOKUP);
538 restart:
540 block = NULL_TREE;
542 gcc_assert (current_binding_level->kind != sk_class);
544 real_functionbody = (current_binding_level->kind == sk_cleanup
545 ? ((functionbody = 0), tmp) : functionbody);
546 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
548 gcc_assert (!VEC_length(cp_class_binding,
549 current_binding_level->class_shadowed));
551 /* We used to use KEEP == 2 to indicate that the new block should go
552 at the beginning of the list of blocks at this binding level,
553 rather than the end. This hack is no longer used. */
554 gcc_assert (keep == 0 || keep == 1);
556 if (current_binding_level->keep)
557 keep = 1;
559 /* Any uses of undefined labels, and any defined labels, now operate
560 under constraints of next binding contour. */
561 if (cfun && !functionbody && named_labels)
562 htab_traverse (named_labels, poplevel_named_label_1,
563 current_binding_level);
565 /* Get the decls in the order they were written.
566 Usually current_binding_level->names is in reverse order.
567 But parameter decls were previously put in forward order. */
569 if (reverse)
570 current_binding_level->names
571 = decls = nreverse (current_binding_level->names);
572 else
573 decls = current_binding_level->names;
575 /* If there were any declarations or structure tags in that level,
576 or if this level is a function body,
577 create a BLOCK to record them for the life of this function. */
578 block = NULL_TREE;
579 if (keep == 1 || functionbody)
580 block = make_node (BLOCK);
581 if (block != NULL_TREE)
583 BLOCK_VARS (block) = decls;
584 BLOCK_SUBBLOCKS (block) = subblocks;
587 /* In each subblock, record that this is its superior. */
588 if (keep >= 0)
589 for (link = subblocks; link; link = TREE_CHAIN (link))
590 BLOCK_SUPERCONTEXT (link) = block;
592 /* We still support the old for-scope rules, whereby the variables
593 in a for-init statement were in scope after the for-statement
594 ended. We only use the new rules if flag_new_for_scope is
595 nonzero. */
596 leaving_for_scope
597 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
599 /* Before we remove the declarations first check for unused variables. */
600 if (warn_unused_variable
601 && !processing_template_decl)
602 for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
603 if (TREE_CODE (decl) == VAR_DECL
604 && ! TREE_USED (decl)
605 && ! DECL_IN_SYSTEM_HEADER (decl)
606 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
607 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
609 /* Remove declarations for all the DECLs in this level. */
610 for (link = decls; link; link = TREE_CHAIN (link))
612 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
613 && DECL_NAME (link))
615 tree name = DECL_NAME (link);
616 cxx_binding *ob;
617 tree ns_binding;
619 ob = outer_binding (name,
620 IDENTIFIER_BINDING (name),
621 /*class_p=*/true);
622 if (!ob)
623 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
624 else
625 ns_binding = NULL_TREE;
627 if (ob && ob->scope == current_binding_level->level_chain)
628 /* We have something like:
630 int i;
631 for (int i; ;);
633 and we are leaving the `for' scope. There's no reason to
634 keep the binding of the inner `i' in this case. */
635 pop_binding (name, link);
636 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
637 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
638 /* Here, we have something like:
640 typedef int I;
642 void f () {
643 for (int I; ;);
646 We must pop the for-scope binding so we know what's a
647 type and what isn't. */
648 pop_binding (name, link);
649 else
651 /* Mark this VAR_DECL as dead so that we can tell we left it
652 there only for backward compatibility. */
653 DECL_DEAD_FOR_LOCAL (link) = 1;
655 /* Keep track of what should have happened when we
656 popped the binding. */
657 if (ob && ob->value)
659 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
660 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
663 /* Add it to the list of dead variables in the next
664 outermost binding to that we can remove these when we
665 leave that binding. */
666 current_binding_level->level_chain->dead_vars_from_for
667 = tree_cons (NULL_TREE, link,
668 current_binding_level->level_chain->
669 dead_vars_from_for);
671 /* Although we don't pop the cxx_binding, we do clear
672 its SCOPE since the scope is going away now. */
673 IDENTIFIER_BINDING (name)->scope
674 = current_binding_level->level_chain;
677 else
679 tree name;
681 /* Remove the binding. */
682 decl = link;
684 if (TREE_CODE (decl) == TREE_LIST)
685 decl = TREE_VALUE (decl);
686 name = decl;
688 if (TREE_CODE (name) == OVERLOAD)
689 name = OVL_FUNCTION (name);
691 gcc_assert (DECL_P (name));
692 pop_binding (DECL_NAME (name), decl);
696 /* Remove declarations for any `for' variables from inner scopes
697 that we kept around. */
698 for (link = current_binding_level->dead_vars_from_for;
699 link; link = TREE_CHAIN (link))
700 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
702 /* Restore the IDENTIFIER_TYPE_VALUEs. */
703 for (link = current_binding_level->type_shadowed;
704 link; link = TREE_CHAIN (link))
705 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
707 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
708 for (link = current_binding_level->shadowed_labels;
709 link;
710 link = TREE_CHAIN (link))
711 pop_local_label (TREE_VALUE (link), TREE_PURPOSE (link));
713 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
714 list if a `using' declaration put them there. The debugging
715 back-ends won't understand OVERLOAD, so we remove them here.
716 Because the BLOCK_VARS are (temporarily) shared with
717 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
718 popped all the bindings. */
719 if (block)
721 tree* d;
723 for (d = &BLOCK_VARS (block); *d; )
725 if (TREE_CODE (*d) == TREE_LIST)
726 *d = TREE_CHAIN (*d);
727 else
728 d = &TREE_CHAIN (*d);
732 /* If the level being exited is the top level of a function,
733 check over all the labels. */
734 if (functionbody)
736 /* Since this is the top level block of a function, the vars are
737 the function's parameters. Don't leave them in the BLOCK
738 because they are found in the FUNCTION_DECL instead. */
739 BLOCK_VARS (block) = 0;
740 pop_labels (block);
743 kind = current_binding_level->kind;
744 if (kind == sk_cleanup)
746 tree stmt;
748 /* If this is a temporary binding created for a cleanup, then we'll
749 have pushed a statement list level. Pop that, create a new
750 BIND_EXPR for the block, and insert it into the stream. */
751 stmt = pop_stmt_list (current_binding_level->statement_list);
752 stmt = c_build_bind_expr (block, stmt);
753 add_stmt (stmt);
756 leave_scope ();
757 if (functionbody)
758 DECL_INITIAL (current_function_decl) = block;
759 else if (block)
760 current_binding_level->blocks
761 = chainon (current_binding_level->blocks, block);
763 /* If we did not make a block for the level just exited,
764 any blocks made for inner levels
765 (since they cannot be recorded as subblocks in that level)
766 must be carried forward so they will later become subblocks
767 of something else. */
768 else if (subblocks)
769 current_binding_level->blocks
770 = chainon (current_binding_level->blocks, subblocks);
772 /* Each and every BLOCK node created here in `poplevel' is important
773 (e.g. for proper debugging information) so if we created one
774 earlier, mark it as "used". */
775 if (block)
776 TREE_USED (block) = 1;
778 /* All temporary bindings created for cleanups are popped silently. */
779 if (kind == sk_cleanup)
780 goto restart;
782 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
785 /* Insert BLOCK at the end of the list of subblocks of the
786 current binding level. This is used when a BIND_EXPR is expanded,
787 to handle the BLOCK node inside the BIND_EXPR. */
789 void
790 insert_block (tree block)
792 TREE_USED (block) = 1;
793 current_binding_level->blocks
794 = chainon (current_binding_level->blocks, block);
797 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
798 itself, calling F for each. The DATA is passed to F as well. */
800 static int
801 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
803 int result = 0;
804 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
806 result |= (*f) (namespace, data);
808 for (; current; current = TREE_CHAIN (current))
809 result |= walk_namespaces_r (current, f, data);
811 return result;
814 /* Walk all the namespaces, calling F for each. The DATA is passed to
815 F as well. */
818 walk_namespaces (walk_namespaces_fn f, void* data)
820 return walk_namespaces_r (global_namespace, f, data);
823 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
824 DATA is non-NULL, this is the last time we will call
825 wrapup_global_declarations for this NAMESPACE. */
828 wrapup_globals_for_namespace (tree namespace, void* data)
830 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
831 VEC(tree,gc) *statics = level->static_decls;
832 tree *vec = VEC_address (tree, statics);
833 int len = VEC_length (tree, statics);
834 int last_time = (data != 0);
836 if (last_time)
838 check_global_declarations (vec, len);
839 emit_debug_global_declarations (vec, len);
840 return 0;
843 /* Write out any globals that need to be output. */
844 return wrapup_global_declarations (vec, len);
848 /* In C++, you don't have to write `struct S' to refer to `S'; you
849 can just use `S'. We accomplish this by creating a TYPE_DECL as
850 if the user had written `typedef struct S S'. Create and return
851 the TYPE_DECL for TYPE. */
853 tree
854 create_implicit_typedef (tree name, tree type)
856 tree decl;
858 decl = build_decl (TYPE_DECL, name, type);
859 DECL_ARTIFICIAL (decl) = 1;
860 /* There are other implicit type declarations, like the one *within*
861 a class that allows you to write `S::S'. We must distinguish
862 amongst these. */
863 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
864 TYPE_NAME (type) = decl;
866 return decl;
869 /* Remember a local name for name-mangling purposes. */
871 static void
872 push_local_name (tree decl)
874 size_t i, nelts;
875 tree t, name;
877 timevar_push (TV_NAME_LOOKUP);
879 name = DECL_NAME (decl);
881 nelts = VEC_length (tree, local_names);
882 for (i = 0; i < nelts; i++)
884 t = VEC_index (tree, local_names, i);
885 if (DECL_NAME (t) == name)
887 if (!DECL_LANG_SPECIFIC (decl))
888 retrofit_lang_decl (decl);
889 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
890 if (DECL_LANG_SPECIFIC (t))
891 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
892 else
893 DECL_DISCRIMINATOR (decl) = 1;
895 VEC_replace (tree, local_names, i, decl);
896 timevar_pop (TV_NAME_LOOKUP);
897 return;
901 VEC_safe_push (tree, gc, local_names, decl);
902 timevar_pop (TV_NAME_LOOKUP);
905 /* Subroutine of duplicate_decls: return truthvalue of whether
906 or not types of these decls match.
908 For C++, we must compare the parameter list so that `int' can match
909 `int&' in a parameter position, but `int&' is not confused with
910 `const int&'. */
913 decls_match (tree newdecl, tree olddecl)
915 int types_match;
917 if (newdecl == olddecl)
918 return 1;
920 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
921 /* If the two DECLs are not even the same kind of thing, we're not
922 interested in their types. */
923 return 0;
925 if (TREE_CODE (newdecl) == FUNCTION_DECL)
927 tree f1 = TREE_TYPE (newdecl);
928 tree f2 = TREE_TYPE (olddecl);
929 tree p1 = TYPE_ARG_TYPES (f1);
930 tree p2 = TYPE_ARG_TYPES (f2);
932 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
933 && ! (DECL_EXTERN_C_P (newdecl)
934 && DECL_EXTERN_C_P (olddecl)))
935 return 0;
937 if (TREE_CODE (f1) != TREE_CODE (f2))
938 return 0;
940 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
942 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
943 && (DECL_BUILT_IN (olddecl)
944 #ifndef NO_IMPLICIT_EXTERN_C
945 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
946 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
947 #endif
950 types_match = self_promoting_args_p (p1);
951 if (p1 == void_list_node)
952 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
954 #ifndef NO_IMPLICIT_EXTERN_C
955 else if (p1 == NULL_TREE
956 && (DECL_EXTERN_C_P (olddecl)
957 && DECL_IN_SYSTEM_HEADER (olddecl)
958 && !DECL_CLASS_SCOPE_P (olddecl))
959 && (DECL_EXTERN_C_P (newdecl)
960 && DECL_IN_SYSTEM_HEADER (newdecl)
961 && !DECL_CLASS_SCOPE_P (newdecl)))
963 types_match = self_promoting_args_p (p2);
964 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
966 #endif
967 else
968 types_match = compparms (p1, p2);
970 else
971 types_match = 0;
973 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
975 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
976 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
977 return 0;
979 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
980 DECL_TEMPLATE_PARMS (olddecl)))
981 return 0;
983 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
984 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
985 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
986 else
987 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
988 DECL_TEMPLATE_RESULT (newdecl));
990 else
992 /* Need to check scope for variable declaration (VAR_DECL).
993 For typedef (TYPE_DECL), scope is ignored. */
994 if (TREE_CODE (newdecl) == VAR_DECL
995 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
996 /* [dcl.link]
997 Two declarations for an object with C language linkage
998 with the same name (ignoring the namespace that qualify
999 it) that appear in different namespace scopes refer to
1000 the same object. */
1001 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1002 return 0;
1004 if (TREE_TYPE (newdecl) == error_mark_node)
1005 types_match = TREE_TYPE (olddecl) == error_mark_node;
1006 else if (TREE_TYPE (olddecl) == NULL_TREE)
1007 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1008 else if (TREE_TYPE (newdecl) == NULL_TREE)
1009 types_match = 0;
1010 else
1011 types_match = comptypes (TREE_TYPE (newdecl),
1012 TREE_TYPE (olddecl),
1013 COMPARE_REDECLARATION);
1016 return types_match;
1019 /* If NEWDECL is `static' and an `extern' was seen previously,
1020 warn about it. OLDDECL is the previous declaration.
1022 Note that this does not apply to the C++ case of declaring
1023 a variable `extern const' and then later `const'.
1025 Don't complain about built-in functions, since they are beyond
1026 the user's control. */
1028 void
1029 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1031 tree name;
1033 if (TREE_CODE (newdecl) == TYPE_DECL
1034 || TREE_CODE (newdecl) == TEMPLATE_DECL
1035 || TREE_CODE (newdecl) == CONST_DECL
1036 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1037 return;
1039 /* Don't get confused by static member functions; that's a different
1040 use of `static'. */
1041 if (TREE_CODE (newdecl) == FUNCTION_DECL
1042 && DECL_STATIC_FUNCTION_P (newdecl))
1043 return;
1045 /* If the old declaration was `static', or the new one isn't, then
1046 then everything is OK. */
1047 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1048 return;
1050 /* It's OK to declare a builtin function as `static'. */
1051 if (TREE_CODE (olddecl) == FUNCTION_DECL
1052 && DECL_ARTIFICIAL (olddecl))
1053 return;
1055 name = DECL_ASSEMBLER_NAME (newdecl);
1056 pedwarn ("%qD was declared %<extern%> and later %<static%>", newdecl);
1057 pedwarn ("previous declaration of %q+D", olddecl);
1060 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1061 function templates. If their exception specifications do not
1062 match, issue an a diagnostic. */
1064 static void
1065 check_redeclaration_exception_specification (tree new_decl,
1066 tree old_decl)
1068 tree new_type;
1069 tree old_type;
1070 tree new_exceptions;
1071 tree old_exceptions;
1073 new_type = TREE_TYPE (new_decl);
1074 new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1075 old_type = TREE_TYPE (old_decl);
1076 old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1078 /* [except.spec]
1080 If any declaration of a function has an exception-specification,
1081 all declarations, including the definition and an explicit
1082 specialization, of that function shall have an
1083 exception-specification with the same set of type-ids. */
1084 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1085 && ! DECL_IS_BUILTIN (old_decl)
1086 && flag_exceptions
1087 && !comp_except_specs (new_exceptions, old_exceptions,
1088 /*exact=*/true))
1090 error ("declaration of %qF throws different exceptions", new_decl);
1091 error ("from previous declaration %q+F", old_decl);
1095 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1096 If the redeclaration is invalid, a diagnostic is issued, and the
1097 error_mark_node is returned. Otherwise, OLDDECL is returned.
1099 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1100 returned.
1102 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1104 tree
1105 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1107 unsigned olddecl_uid = DECL_UID (olddecl);
1108 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1109 int new_defines_function = 0;
1110 tree new_template;
1112 if (newdecl == olddecl)
1113 return olddecl;
1115 types_match = decls_match (newdecl, olddecl);
1117 /* If either the type of the new decl or the type of the old decl is an
1118 error_mark_node, then that implies that we have already issued an
1119 error (earlier) for some bogus type specification, and in that case,
1120 it is rather pointless to harass the user with yet more error message
1121 about the same declaration, so just pretend the types match here. */
1122 if (TREE_TYPE (newdecl) == error_mark_node
1123 || TREE_TYPE (olddecl) == error_mark_node)
1124 return error_mark_node;
1126 if (DECL_P (olddecl)
1127 && TREE_CODE (newdecl) == FUNCTION_DECL
1128 && TREE_CODE (olddecl) == FUNCTION_DECL
1129 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1131 if (DECL_DECLARED_INLINE_P (newdecl)
1132 && DECL_UNINLINABLE (newdecl)
1133 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1134 /* Already warned elsewhere. */;
1135 else if (DECL_DECLARED_INLINE_P (olddecl)
1136 && DECL_UNINLINABLE (olddecl)
1137 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1138 /* Already warned. */;
1139 else if (DECL_DECLARED_INLINE_P (newdecl)
1140 && DECL_UNINLINABLE (olddecl)
1141 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1143 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1144 newdecl);
1145 warning (OPT_Wattributes, "previous declaration of %q+D "
1146 "with attribute noinline", olddecl);
1148 else if (DECL_DECLARED_INLINE_P (olddecl)
1149 && DECL_UNINLINABLE (newdecl)
1150 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1152 warning (OPT_Wattributes, "function %q+D redeclared with "
1153 "attribute noinline", newdecl);
1154 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1155 olddecl);
1159 /* Check for redeclaration and other discrepancies. */
1160 if (TREE_CODE (olddecl) == FUNCTION_DECL
1161 && DECL_ARTIFICIAL (olddecl))
1163 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1164 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1166 /* Avoid warnings redeclaring built-ins which have not been
1167 explicitly declared. */
1168 if (DECL_ANTICIPATED (olddecl))
1169 return NULL_TREE;
1171 /* If you declare a built-in or predefined function name as static,
1172 the old definition is overridden, but optionally warn this was a
1173 bad choice of name. */
1174 if (! TREE_PUBLIC (newdecl))
1176 warning (OPT_Wshadow, "shadowing %s function %q#D",
1177 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1178 olddecl);
1179 /* Discard the old built-in function. */
1180 return NULL_TREE;
1182 /* If the built-in is not ansi, then programs can override
1183 it even globally without an error. */
1184 else if (! DECL_BUILT_IN (olddecl))
1185 warning (0, "library function %q#D redeclared as non-function %q#D",
1186 olddecl, newdecl);
1187 else
1189 error ("declaration of %q#D", newdecl);
1190 error ("conflicts with built-in declaration %q#D",
1191 olddecl);
1193 return NULL_TREE;
1195 else if (!types_match)
1197 /* Avoid warnings redeclaring built-ins which have not been
1198 explicitly declared. */
1199 if (DECL_ANTICIPATED (olddecl))
1201 /* Deal with fileptr_type_node. FILE type is not known
1202 at the time we create the builtins. */
1203 tree t1, t2;
1205 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1206 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1207 t1 || t2;
1208 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1209 if (!t1 || !t2)
1210 break;
1211 else if (TREE_VALUE (t2) == fileptr_type_node)
1213 tree t = TREE_VALUE (t1);
1215 if (TREE_CODE (t) == POINTER_TYPE
1216 && TYPE_NAME (TREE_TYPE (t))
1217 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1218 == get_identifier ("FILE")
1219 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1221 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1223 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1224 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1225 types_match = decls_match (newdecl, olddecl);
1226 if (types_match)
1227 return duplicate_decls (newdecl, olddecl,
1228 newdecl_is_friend);
1229 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1232 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1233 break;
1235 else if ((DECL_EXTERN_C_P (newdecl)
1236 && DECL_EXTERN_C_P (olddecl))
1237 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1238 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1240 /* A near match; override the builtin. */
1242 if (TREE_PUBLIC (newdecl))
1244 warning (0, "new declaration %q#D", newdecl);
1245 warning (0, "ambiguates built-in declaration %q#D",
1246 olddecl);
1248 else
1249 warning (OPT_Wshadow, "shadowing %s function %q#D",
1250 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1251 olddecl);
1253 else
1254 /* Discard the old built-in function. */
1255 return NULL_TREE;
1257 /* Replace the old RTL to avoid problems with inlining. */
1258 COPY_DECL_RTL (newdecl, olddecl);
1260 /* Even if the types match, prefer the new declarations type for
1261 built-ins which have not been explicitly declared, for
1262 exception lists, etc... */
1263 else if (DECL_ANTICIPATED (olddecl))
1265 tree type = TREE_TYPE (newdecl);
1266 tree attribs = (*targetm.merge_type_attributes)
1267 (TREE_TYPE (olddecl), type);
1269 type = cp_build_type_attribute_variant (type, attribs);
1270 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1273 /* Whether or not the builtin can throw exceptions has no
1274 bearing on this declarator. */
1275 TREE_NOTHROW (olddecl) = 0;
1277 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1279 /* If a builtin function is redeclared as `static', merge
1280 the declarations, but make the original one static. */
1281 DECL_THIS_STATIC (olddecl) = 1;
1282 TREE_PUBLIC (olddecl) = 0;
1284 /* Make the old declaration consistent with the new one so
1285 that all remnants of the builtin-ness of this function
1286 will be banished. */
1287 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1288 COPY_DECL_RTL (newdecl, olddecl);
1291 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1293 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1294 && TREE_CODE (newdecl) != TYPE_DECL
1295 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
1296 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
1297 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1298 && TREE_CODE (olddecl) != TYPE_DECL
1299 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
1300 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1301 == TYPE_DECL))))
1303 /* We do nothing special here, because C++ does such nasty
1304 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1305 get shadowed, and know that if we need to find a TYPE_DECL
1306 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1307 slot of the identifier. */
1308 return NULL_TREE;
1311 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1312 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1313 || (TREE_CODE (olddecl) == FUNCTION_DECL
1314 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1315 return NULL_TREE;
1317 error ("%q#D redeclared as different kind of symbol", newdecl);
1318 if (TREE_CODE (olddecl) == TREE_LIST)
1319 olddecl = TREE_VALUE (olddecl);
1320 error ("previous declaration of %q+#D", olddecl);
1322 return error_mark_node;
1324 else if (!types_match)
1326 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1327 /* These are certainly not duplicate declarations; they're
1328 from different scopes. */
1329 return NULL_TREE;
1331 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1333 /* The name of a class template may not be declared to refer to
1334 any other template, class, function, object, namespace, value,
1335 or type in the same scope. */
1336 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1337 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1339 error ("declaration of template %q#D", newdecl);
1340 error ("conflicts with previous declaration %q+#D", olddecl);
1342 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1343 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1344 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1345 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1346 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1347 DECL_TEMPLATE_PARMS (olddecl))
1348 /* Template functions can be disambiguated by
1349 return type. */
1350 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1351 TREE_TYPE (TREE_TYPE (olddecl))))
1353 error ("new declaration %q#D", newdecl);
1354 error ("ambiguates old declaration %q+#D", olddecl);
1356 return NULL_TREE;
1358 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1360 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1362 error ("declaration of C function %q#D conflicts with",
1363 newdecl);
1364 error ("previous declaration %q+#D here", olddecl);
1366 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1367 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1369 error ("new declaration %q#D", newdecl);
1370 error ("ambiguates old declaration %q+#D", olddecl);
1371 return error_mark_node;
1373 else
1374 return NULL_TREE;
1376 else
1378 error ("conflicting declaration %q#D", newdecl);
1379 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
1380 return error_mark_node;
1383 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1384 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1385 && (!DECL_TEMPLATE_INFO (newdecl)
1386 || (DECL_TI_TEMPLATE (newdecl)
1387 != DECL_TI_TEMPLATE (olddecl))))
1388 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1389 && (!DECL_TEMPLATE_INFO (olddecl)
1390 || (DECL_TI_TEMPLATE (olddecl)
1391 != DECL_TI_TEMPLATE (newdecl))))))
1392 /* It's OK to have a template specialization and a non-template
1393 with the same type, or to have specializations of two
1394 different templates with the same type. Note that if one is a
1395 specialization, and the other is an instantiation of the same
1396 template, that we do not exit at this point. That situation
1397 can occur if we instantiate a template class, and then
1398 specialize one of its methods. This situation is valid, but
1399 the declarations must be merged in the usual way. */
1400 return NULL_TREE;
1401 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1402 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1403 && !DECL_USE_TEMPLATE (newdecl))
1404 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1405 && !DECL_USE_TEMPLATE (olddecl))))
1406 /* One of the declarations is a template instantiation, and the
1407 other is not a template at all. That's OK. */
1408 return NULL_TREE;
1409 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1411 /* In [namespace.alias] we have:
1413 In a declarative region, a namespace-alias-definition can be
1414 used to redefine a namespace-alias declared in that declarative
1415 region to refer only to the namespace to which it already
1416 refers.
1418 Therefore, if we encounter a second alias directive for the same
1419 alias, we can just ignore the second directive. */
1420 if (DECL_NAMESPACE_ALIAS (newdecl)
1421 && (DECL_NAMESPACE_ALIAS (newdecl)
1422 == DECL_NAMESPACE_ALIAS (olddecl)))
1423 return olddecl;
1424 /* [namespace.alias]
1426 A namespace-name or namespace-alias shall not be declared as
1427 the name of any other entity in the same declarative region.
1428 A namespace-name defined at global scope shall not be
1429 declared as the name of any other entity in any global scope
1430 of the program. */
1431 error ("declaration of namespace %qD conflicts with", newdecl);
1432 error ("previous declaration of namespace %q+D here", olddecl);
1433 return error_mark_node;
1435 else
1437 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1438 if (errmsg)
1440 error (errmsg, newdecl);
1441 if (DECL_NAME (olddecl) != NULL_TREE)
1442 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1443 ? "%q+#D previously defined here"
1444 : "%q+#D previously declared here", olddecl);
1445 return error_mark_node;
1447 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1448 && DECL_INITIAL (olddecl) != NULL_TREE
1449 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1450 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1452 /* Prototype decl follows defn w/o prototype. */
1453 warning (0, "prototype for %q+#D", newdecl);
1454 warning (0, "%Jfollows non-prototype definition here", olddecl);
1456 else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1457 || TREE_CODE (olddecl) == VAR_DECL)
1458 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1460 /* [dcl.link]
1461 If two declarations of the same function or object
1462 specify different linkage-specifications ..., the program
1463 is ill-formed.... Except for functions with C++ linkage,
1464 a function declaration without a linkage specification
1465 shall not precede the first linkage specification for
1466 that function. A function can be declared without a
1467 linkage specification after an explicit linkage
1468 specification has been seen; the linkage explicitly
1469 specified in the earlier declaration is not affected by
1470 such a function declaration.
1472 DR 563 raises the question why the restrictions on
1473 functions should not also apply to objects. Older
1474 versions of G++ silently ignore the linkage-specification
1475 for this example:
1477 namespace N {
1478 extern int i;
1479 extern "C" int i;
1482 which is clearly wrong. Therefore, we now treat objects
1483 like functions. */
1484 if (current_lang_depth () == 0)
1486 /* There is no explicit linkage-specification, so we use
1487 the linkage from the previous declaration. */
1488 if (!DECL_LANG_SPECIFIC (newdecl))
1489 retrofit_lang_decl (newdecl);
1490 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1492 else
1494 error ("previous declaration of %q+#D with %qL linkage",
1495 olddecl, DECL_LANGUAGE (olddecl));
1496 error ("conflicts with new declaration with %qL linkage",
1497 DECL_LANGUAGE (newdecl));
1501 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1503 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1505 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1506 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1507 int i = 1;
1509 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1510 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1512 for (; t1 && t1 != void_list_node;
1513 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1514 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1516 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1517 TREE_PURPOSE (t2)))
1519 pedwarn ("default argument given for parameter %d of %q#D",
1520 i, newdecl);
1521 pedwarn ("after previous specification in %q+#D", olddecl);
1523 else
1525 error ("default argument given for parameter %d of %q#D",
1526 i, newdecl);
1527 error ("after previous specification in %q+#D",
1528 olddecl);
1532 if (DECL_DECLARED_INLINE_P (newdecl)
1533 && ! DECL_DECLARED_INLINE_P (olddecl)
1534 && TREE_ADDRESSABLE (olddecl) && warn_inline)
1536 warning (0, "%q#D was used before it was declared inline", newdecl);
1537 warning (0, "%Jprevious non-inline declaration here", olddecl);
1542 /* Do not merge an implicit typedef with an explicit one. In:
1544 class A;
1546 typedef class A A __attribute__ ((foo));
1548 the attribute should apply only to the typedef. */
1549 if (TREE_CODE (olddecl) == TYPE_DECL
1550 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1551 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1552 return NULL_TREE;
1554 /* If new decl is `static' and an `extern' was seen previously,
1555 warn about it. */
1556 warn_extern_redeclared_static (newdecl, olddecl);
1558 /* We have committed to returning 1 at this point. */
1559 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1561 /* Now that functions must hold information normally held
1562 by field decls, there is extra work to do so that
1563 declaration information does not get destroyed during
1564 definition. */
1565 if (DECL_VINDEX (olddecl))
1566 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1567 if (DECL_CONTEXT (olddecl))
1568 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1569 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1570 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1571 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1572 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1573 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1574 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1575 SET_OVERLOADED_OPERATOR_CODE
1576 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1577 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1579 /* Optionally warn about more than one declaration for the same
1580 name, but don't warn about a function declaration followed by a
1581 definition. */
1582 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1583 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1584 /* Don't warn about extern decl followed by definition. */
1585 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1586 /* Don't warn about friends, let add_friend take care of it. */
1587 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)))
1589 warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1590 warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
1594 /* Deal with C++: must preserve virtual function table size. */
1595 if (TREE_CODE (olddecl) == TYPE_DECL)
1597 tree newtype = TREE_TYPE (newdecl);
1598 tree oldtype = TREE_TYPE (olddecl);
1600 if (newtype != error_mark_node && oldtype != error_mark_node
1601 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1602 CLASSTYPE_FRIEND_CLASSES (newtype)
1603 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1605 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1608 /* Copy all the DECL_... slots specified in the new decl
1609 except for any that we copy here from the old type. */
1610 DECL_ATTRIBUTES (newdecl)
1611 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1613 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1615 tree old_result;
1616 tree new_result;
1617 old_result = DECL_TEMPLATE_RESULT (olddecl);
1618 new_result = DECL_TEMPLATE_RESULT (newdecl);
1619 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1620 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1621 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1622 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1624 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1626 DECL_INLINE (old_result)
1627 |= DECL_INLINE (new_result);
1628 DECL_DECLARED_INLINE_P (old_result)
1629 |= DECL_DECLARED_INLINE_P (new_result);
1630 check_redeclaration_exception_specification (newdecl, olddecl);
1633 /* If the new declaration is a definition, update the file and
1634 line information on the declaration. */
1635 if (DECL_INITIAL (old_result) == NULL_TREE
1636 && DECL_INITIAL (new_result) != NULL_TREE)
1638 DECL_SOURCE_LOCATION (olddecl)
1639 = DECL_SOURCE_LOCATION (old_result)
1640 = DECL_SOURCE_LOCATION (newdecl);
1641 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1642 DECL_ARGUMENTS (old_result)
1643 = DECL_ARGUMENTS (new_result);
1646 return olddecl;
1649 if (types_match)
1651 /* Automatically handles default parameters. */
1652 tree oldtype = TREE_TYPE (olddecl);
1653 tree newtype;
1655 /* Merge the data types specified in the two decls. */
1656 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1658 /* If merge_types produces a non-typedef type, just use the old type. */
1659 if (TREE_CODE (newdecl) == TYPE_DECL
1660 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1661 newtype = oldtype;
1663 if (TREE_CODE (newdecl) == VAR_DECL)
1665 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1666 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1667 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1668 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1669 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1670 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1672 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1673 if (DECL_LANG_SPECIFIC (olddecl)
1674 && CP_DECL_THREADPRIVATE_P (olddecl))
1676 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1677 if (!DECL_LANG_SPECIFIC (newdecl))
1678 retrofit_lang_decl (newdecl);
1680 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1681 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1685 /* Do this after calling `merge_types' so that default
1686 parameters don't confuse us. */
1687 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1688 check_redeclaration_exception_specification (newdecl, olddecl);
1689 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1691 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1692 check_default_args (newdecl);
1694 /* Lay the type out, unless already done. */
1695 if (! same_type_p (newtype, oldtype)
1696 && TREE_TYPE (newdecl) != error_mark_node
1697 && !(processing_template_decl && uses_template_parms (newdecl)))
1698 layout_type (TREE_TYPE (newdecl));
1700 if ((TREE_CODE (newdecl) == VAR_DECL
1701 || TREE_CODE (newdecl) == PARM_DECL
1702 || TREE_CODE (newdecl) == RESULT_DECL
1703 || TREE_CODE (newdecl) == FIELD_DECL
1704 || TREE_CODE (newdecl) == TYPE_DECL)
1705 && !(processing_template_decl && uses_template_parms (newdecl)))
1706 layout_decl (newdecl, 0);
1708 /* Merge the type qualifiers. */
1709 if (TREE_READONLY (newdecl))
1710 TREE_READONLY (olddecl) = 1;
1711 if (TREE_THIS_VOLATILE (newdecl))
1712 TREE_THIS_VOLATILE (olddecl) = 1;
1713 if (TREE_NOTHROW (newdecl))
1714 TREE_NOTHROW (olddecl) = 1;
1716 /* Merge deprecatedness. */
1717 if (TREE_DEPRECATED (newdecl))
1718 TREE_DEPRECATED (olddecl) = 1;
1720 /* Merge the initialization information. */
1721 if (DECL_INITIAL (newdecl) == NULL_TREE
1722 && DECL_INITIAL (olddecl) != NULL_TREE)
1724 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1725 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1726 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
1727 && DECL_LANG_SPECIFIC (newdecl)
1728 && DECL_LANG_SPECIFIC (olddecl))
1730 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1731 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1735 /* Merge the section attribute.
1736 We want to issue an error if the sections conflict but that must be
1737 done later in decl_attributes since we are called before attributes
1738 are assigned. */
1739 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1740 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1742 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1744 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1745 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1746 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1747 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1748 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1749 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1750 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1751 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1752 /* Keep the old RTL. */
1753 COPY_DECL_RTL (olddecl, newdecl);
1755 else if (TREE_CODE (newdecl) == VAR_DECL
1756 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1758 /* Keep the old RTL. We cannot keep the old RTL if the old
1759 declaration was for an incomplete object and the new
1760 declaration is not since many attributes of the RTL will
1761 change. */
1762 COPY_DECL_RTL (olddecl, newdecl);
1765 /* If cannot merge, then use the new type and qualifiers,
1766 and don't preserve the old rtl. */
1767 else
1769 /* Clean out any memory we had of the old declaration. */
1770 tree oldstatic = value_member (olddecl, static_aggregates);
1771 if (oldstatic)
1772 TREE_VALUE (oldstatic) = error_mark_node;
1774 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1775 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1776 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1777 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1780 /* Merge the storage class information. */
1781 merge_weak (newdecl, olddecl);
1783 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
1784 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1785 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1786 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1787 if (! DECL_EXTERNAL (olddecl))
1788 DECL_EXTERNAL (newdecl) = 0;
1790 new_template = NULL_TREE;
1791 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1793 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1794 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1795 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1796 DECL_TEMPLATE_INSTANTIATED (newdecl)
1797 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1799 /* If the OLDDECL is an instantiation and/or specialization,
1800 then the NEWDECL must be too. But, it may not yet be marked
1801 as such if the caller has created NEWDECL, but has not yet
1802 figured out that it is a redeclaration. */
1803 if (!DECL_USE_TEMPLATE (newdecl))
1804 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
1806 /* Don't really know how much of the language-specific
1807 values we should copy from old to new. */
1808 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
1809 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
1810 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
1811 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
1812 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
1813 if (DECL_TEMPLATE_INFO (newdecl))
1814 new_template = DECL_TI_TEMPLATE (newdecl);
1815 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
1816 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1817 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
1818 olddecl_friend = DECL_FRIEND_P (olddecl);
1819 hidden_friend = (DECL_ANTICIPATED (olddecl)
1820 && DECL_HIDDEN_FRIEND_P (olddecl)
1821 && newdecl_is_friend);
1823 /* Only functions have DECL_BEFRIENDING_CLASSES. */
1824 if (TREE_CODE (newdecl) == FUNCTION_DECL
1825 || DECL_FUNCTION_TEMPLATE_P (newdecl))
1827 DECL_BEFRIENDING_CLASSES (newdecl)
1828 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1829 DECL_BEFRIENDING_CLASSES (olddecl));
1830 /* DECL_THUNKS is only valid for virtual functions,
1831 otherwise it is a DECL_FRIEND_CONTEXT. */
1832 if (DECL_VIRTUAL_P (newdecl))
1833 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
1837 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1839 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1840 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
1842 /* If newdecl is not a specialization, then it is not a
1843 template-related function at all. And that means that we
1844 should have exited above, returning 0. */
1845 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
1847 if (TREE_USED (olddecl))
1848 /* From [temp.expl.spec]:
1850 If a template, a member template or the member of a class
1851 template is explicitly specialized then that
1852 specialization shall be declared before the first use of
1853 that specialization that would cause an implicit
1854 instantiation to take place, in every translation unit in
1855 which such a use occurs. */
1856 error ("explicit specialization of %qD after first use",
1857 olddecl);
1859 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
1861 /* Don't propagate visibility from the template to the
1862 specialization here. We'll do that in determine_visibility if
1863 appropriate. */
1864 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
1866 /* [temp.expl.spec/14] We don't inline explicit specialization
1867 just because the primary template says so. */
1869 else
1871 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
1872 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
1874 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
1876 /* If either decl says `inline', this fn is inline, unless
1877 its definition was passed already. */
1878 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
1879 DECL_INLINE (olddecl) = 1;
1880 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
1882 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1883 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1886 /* Preserve abstractness on cloned [cd]tors. */
1887 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
1889 if (! types_match)
1891 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1892 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
1893 COPY_DECL_RTL (newdecl, olddecl);
1895 if (! types_match || new_defines_function)
1897 /* These need to be copied so that the names are available.
1898 Note that if the types do match, we'll preserve inline
1899 info and other bits, but if not, we won't. */
1900 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
1901 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
1903 if (new_defines_function)
1904 /* If defining a function declared with other language
1905 linkage, use the previously declared language linkage. */
1906 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1907 else if (types_match)
1909 /* If redeclaring a builtin function, and not a definition,
1910 it stays built in. */
1911 if (DECL_BUILT_IN (olddecl))
1913 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1914 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1915 /* If we're keeping the built-in definition, keep the rtl,
1916 regardless of declaration matches. */
1917 COPY_DECL_RTL (olddecl, newdecl);
1920 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1921 /* Don't clear out the arguments if we're redefining a function. */
1922 if (DECL_ARGUMENTS (olddecl))
1923 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1926 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1927 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
1929 /* Now preserve various other info from the definition. */
1930 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
1931 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
1932 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1933 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1935 /* Warn about conflicting visibility specifications. */
1936 if (DECL_VISIBILITY_SPECIFIED (olddecl)
1937 && DECL_VISIBILITY_SPECIFIED (newdecl)
1938 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1940 warning (OPT_Wattributes, "%q+D: visibility attribute ignored "
1941 "because it", newdecl);
1942 warning (OPT_Wattributes, "%Jconflicts with previous "
1943 "declaration here", olddecl);
1945 /* Choose the declaration which specified visibility. */
1946 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1948 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1949 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1951 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
1952 so keep this behavior. */
1953 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
1955 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
1956 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
1959 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
1960 with that from NEWDECL below. */
1961 if (DECL_LANG_SPECIFIC (olddecl))
1963 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
1964 != DECL_LANG_SPECIFIC (newdecl));
1965 ggc_free (DECL_LANG_SPECIFIC (olddecl));
1968 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1970 int function_size;
1972 function_size = sizeof (struct tree_decl_common);
1974 memcpy ((char *) olddecl + sizeof (struct tree_common),
1975 (char *) newdecl + sizeof (struct tree_common),
1976 function_size - sizeof (struct tree_common));
1978 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1979 (char *) newdecl + sizeof (struct tree_decl_common),
1980 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
1981 if (new_template)
1982 /* If newdecl is a template instantiation, it is possible that
1983 the following sequence of events has occurred:
1985 o A friend function was declared in a class template. The
1986 class template was instantiated.
1988 o The instantiation of the friend declaration was
1989 recorded on the instantiation list, and is newdecl.
1991 o Later, however, instantiate_class_template called pushdecl
1992 on the newdecl to perform name injection. But, pushdecl in
1993 turn called duplicate_decls when it discovered that another
1994 declaration of a global function with the same name already
1995 existed.
1997 o Here, in duplicate_decls, we decided to clobber newdecl.
1999 If we're going to do that, we'd better make sure that
2000 olddecl, and not newdecl, is on the list of
2001 instantiations so that if we try to do the instantiation
2002 again we won't get the clobbered declaration. */
2003 reregister_specialization (newdecl,
2004 new_template,
2005 olddecl);
2007 else
2009 size_t size = tree_code_size (TREE_CODE (olddecl));
2010 memcpy ((char *) olddecl + sizeof (struct tree_common),
2011 (char *) newdecl + sizeof (struct tree_common),
2012 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2013 switch (TREE_CODE (olddecl))
2015 case LABEL_DECL:
2016 case VAR_DECL:
2017 case RESULT_DECL:
2018 case PARM_DECL:
2019 case FIELD_DECL:
2020 case TYPE_DECL:
2021 case CONST_DECL:
2023 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2024 (char *) newdecl + sizeof (struct tree_decl_common),
2025 size - sizeof (struct tree_decl_common)
2026 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2028 break;
2029 default:
2030 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2031 (char *) newdecl + sizeof (struct tree_decl_common),
2032 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2033 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2034 break;
2037 DECL_UID (olddecl) = olddecl_uid;
2038 if (olddecl_friend)
2039 DECL_FRIEND_P (olddecl) = 1;
2040 if (hidden_friend)
2042 DECL_ANTICIPATED (olddecl) = 1;
2043 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2046 /* NEWDECL contains the merged attribute lists.
2047 Update OLDDECL to be the same. */
2048 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2050 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2051 so that encode_section_info has a chance to look at the new decl
2052 flags and attributes. */
2053 if (DECL_RTL_SET_P (olddecl)
2054 && (TREE_CODE (olddecl) == FUNCTION_DECL
2055 || (TREE_CODE (olddecl) == VAR_DECL
2056 && TREE_STATIC (olddecl))))
2057 make_decl_rtl (olddecl);
2059 /* The NEWDECL will no longer be needed. Because every out-of-class
2060 declaration of a member results in a call to duplicate_decls,
2061 freeing these nodes represents in a significant savings. */
2062 ggc_free (newdecl);
2064 return olddecl;
2067 /* Return zero if the declaration NEWDECL is valid
2068 when the declaration OLDDECL (assumed to be for the same name)
2069 has already been seen.
2070 Otherwise return an error message format string with a %s
2071 where the identifier should go. */
2073 static const char *
2074 redeclaration_error_message (tree newdecl, tree olddecl)
2076 if (TREE_CODE (newdecl) == TYPE_DECL)
2078 /* Because C++ can put things into name space for free,
2079 constructs like "typedef struct foo { ... } foo"
2080 would look like an erroneous redeclaration. */
2081 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2082 return NULL;
2083 else
2084 return "redefinition of %q#D";
2086 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2088 /* If this is a pure function, its olddecl will actually be
2089 the original initialization to `0' (which we force to call
2090 abort()). Don't complain about redefinition in this case. */
2091 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2092 && DECL_INITIAL (olddecl) == NULL_TREE)
2093 return NULL;
2095 /* If both functions come from different namespaces, this is not
2096 a redeclaration - this is a conflict with a used function. */
2097 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2098 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2099 && ! decls_match (olddecl, newdecl))
2100 return "%qD conflicts with used function";
2102 /* We'll complain about linkage mismatches in
2103 warn_extern_redeclared_static. */
2105 /* Defining the same name twice is no good. */
2106 if (DECL_INITIAL (olddecl) != NULL_TREE
2107 && DECL_INITIAL (newdecl) != NULL_TREE)
2109 if (DECL_NAME (olddecl) == NULL_TREE)
2110 return "%q#D not declared in class";
2111 else
2112 return "redefinition of %q#D";
2114 return NULL;
2116 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2118 tree nt, ot;
2120 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2122 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2123 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2124 return "redefinition of %q#D";
2125 return NULL;
2128 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2129 || (DECL_TEMPLATE_RESULT (newdecl)
2130 == DECL_TEMPLATE_RESULT (olddecl)))
2131 return NULL;
2133 nt = DECL_TEMPLATE_RESULT (newdecl);
2134 if (DECL_TEMPLATE_INFO (nt))
2135 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2136 ot = DECL_TEMPLATE_RESULT (olddecl);
2137 if (DECL_TEMPLATE_INFO (ot))
2138 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2139 if (DECL_INITIAL (nt) && DECL_INITIAL (ot))
2140 return "redefinition of %q#D";
2142 return NULL;
2144 else if (TREE_CODE (newdecl) == VAR_DECL
2145 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2146 && (! DECL_LANG_SPECIFIC (olddecl)
2147 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2148 || DECL_THREAD_LOCAL_P (newdecl)))
2150 /* Only variables can be thread-local, and all declarations must
2151 agree on this property. */
2152 if (DECL_THREAD_LOCAL_P (newdecl))
2153 return "thread-local declaration of %q#D follows "
2154 "non-thread-local declaration";
2155 else
2156 return "non-thread-local declaration of %q#D follows "
2157 "thread-local declaration";
2159 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2161 /* Objects declared at top level: */
2162 /* If at least one is a reference, it's ok. */
2163 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2164 return NULL;
2165 /* Reject two definitions. */
2166 return "redefinition of %q#D";
2168 else
2170 /* Objects declared with block scope: */
2171 /* Reject two definitions, and reject a definition
2172 together with an external reference. */
2173 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2174 return "redeclaration of %q#D";
2175 return NULL;
2179 /* Hash and equality functions for the named_label table. */
2181 static hashval_t
2182 named_label_entry_hash (const void *data)
2184 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2185 return DECL_UID (ent->label_decl);
2188 static int
2189 named_label_entry_eq (const void *a, const void *b)
2191 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2192 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2193 return ent_a->label_decl == ent_b->label_decl;
2196 /* Create a new label, named ID. */
2198 static tree
2199 make_label_decl (tree id, int local_p)
2201 struct named_label_entry *ent;
2202 void **slot;
2203 tree decl;
2205 decl = build_decl (LABEL_DECL, id, void_type_node);
2207 DECL_CONTEXT (decl) = current_function_decl;
2208 DECL_MODE (decl) = VOIDmode;
2209 C_DECLARED_LABEL_FLAG (decl) = local_p;
2211 /* Say where one reference is to the label, for the sake of the
2212 error if it is not defined. */
2213 DECL_SOURCE_LOCATION (decl) = input_location;
2215 /* Record the fact that this identifier is bound to this label. */
2216 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2218 /* Create the label htab for the function on demand. */
2219 if (!named_labels)
2220 named_labels = htab_create_ggc (13, named_label_entry_hash,
2221 named_label_entry_eq, NULL);
2223 /* Record this label on the list of labels used in this function.
2224 We do this before calling make_label_decl so that we get the
2225 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2226 ent = GGC_CNEW (struct named_label_entry);
2227 ent->label_decl = decl;
2229 slot = htab_find_slot (named_labels, ent, INSERT);
2230 gcc_assert (*slot == NULL);
2231 *slot = ent;
2233 return decl;
2236 /* Look for a label named ID in the current function. If one cannot
2237 be found, create one. (We keep track of used, but undefined,
2238 labels, and complain about them at the end of a function.) */
2240 tree
2241 lookup_label (tree id)
2243 tree decl;
2245 timevar_push (TV_NAME_LOOKUP);
2246 /* You can't use labels at global scope. */
2247 if (current_function_decl == NULL_TREE)
2249 error ("label %qE referenced outside of any function", id);
2250 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2253 /* See if we've already got this label. */
2254 decl = IDENTIFIER_LABEL_VALUE (id);
2255 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2256 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2258 decl = make_label_decl (id, /*local_p=*/0);
2259 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2262 /* Declare a local label named ID. */
2264 tree
2265 declare_local_label (tree id)
2267 tree decl, shadow;
2269 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2270 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2271 shadow = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2272 current_binding_level->shadowed_labels);
2273 current_binding_level->shadowed_labels = shadow;
2275 decl = make_label_decl (id, /*local_p=*/1);
2276 TREE_VALUE (shadow) = decl;
2278 return decl;
2281 /* Returns nonzero if it is ill-formed to jump past the declaration of
2282 DECL. Returns 2 if it's also a real problem. */
2284 static int
2285 decl_jump_unsafe (tree decl)
2287 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2288 || TREE_TYPE (decl) == error_mark_node)
2289 return 0;
2291 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
2292 || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2293 return 2;
2295 if (pod_type_p (TREE_TYPE (decl)))
2296 return 0;
2298 /* The POD stuff is just pedantry; why should it matter if the class
2299 contains a field of pointer to member type? */
2300 return 1;
2303 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2305 static void
2306 identify_goto (tree decl, const location_t *locus)
2308 if (decl)
2309 pedwarn ("jump to label %qD", decl);
2310 else
2311 pedwarn ("jump to case label");
2312 if (locus)
2313 pedwarn ("%H from here", locus);
2316 /* Check that a single previously seen jump to a newly defined label
2317 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2318 the jump context; NAMES are the names in scope in LEVEL at the jump
2319 context; LOCUS is the source position of the jump or 0. Returns
2320 true if all is well. */
2322 static bool
2323 check_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names,
2324 bool exited_omp, const location_t *locus)
2326 struct cp_binding_level *b;
2327 bool identified = false, saw_eh = false, saw_omp = false;
2329 if (exited_omp)
2331 identify_goto (decl, locus);
2332 error (" exits OpenMP structured block");
2333 identified = saw_omp = true;
2336 for (b = current_binding_level; b ; b = b->level_chain)
2338 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2340 for (new_decls = b->names; new_decls != old_decls;
2341 new_decls = TREE_CHAIN (new_decls))
2343 int problem = decl_jump_unsafe (new_decls);
2344 if (! problem)
2345 continue;
2347 if (!identified)
2349 identify_goto (decl, locus);
2350 identified = true;
2352 if (problem > 1)
2353 error (" crosses initialization of %q+#D", new_decls);
2354 else
2355 pedwarn (" enters scope of non-POD %q+#D", new_decls);
2358 if (b == level)
2359 break;
2360 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2362 if (!identified)
2364 identify_goto (decl, locus);
2365 identified = true;
2367 if (b->kind == sk_try)
2368 error (" enters try block");
2369 else
2370 error (" enters catch block");
2371 saw_eh = true;
2373 if (b->kind == sk_omp && !saw_omp)
2375 if (!identified)
2377 identify_goto (decl, locus);
2378 identified = true;
2380 error (" enters OpenMP structured block");
2381 saw_omp = true;
2385 return !identified;
2388 static void
2389 check_previous_goto (tree decl, struct named_label_use_entry *use)
2391 check_previous_goto_1 (decl, use->binding_level,
2392 use->names_in_scope, use->in_omp_scope,
2393 &use->o_goto_locus);
2396 static bool
2397 check_switch_goto (struct cp_binding_level* level)
2399 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2402 /* Check that a new jump to a label DECL is OK. Called by
2403 finish_goto_stmt. */
2405 void
2406 check_goto (tree decl)
2408 struct named_label_entry *ent, dummy;
2409 bool saw_catch = false, identified = false;
2410 tree bad;
2412 /* We can't know where a computed goto is jumping.
2413 So we assume that it's OK. */
2414 if (TREE_CODE (decl) != LABEL_DECL)
2415 return;
2417 /* We didn't record any information about this label when we created it,
2418 and there's not much point since it's trivial to analyze as a return. */
2419 if (decl == cdtor_label)
2420 return;
2422 dummy.label_decl = decl;
2423 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2424 gcc_assert (ent != NULL);
2426 /* If the label hasn't been defined yet, defer checking. */
2427 if (! DECL_INITIAL (decl))
2429 struct named_label_use_entry *new_use;
2431 /* Don't bother creating another use if the last goto had the
2432 same data, and will therefore create the same set of errors. */
2433 if (ent->uses
2434 && ent->uses->names_in_scope == current_binding_level->names)
2435 return;
2437 new_use = GGC_NEW (struct named_label_use_entry);
2438 new_use->binding_level = current_binding_level;
2439 new_use->names_in_scope = current_binding_level->names;
2440 new_use->o_goto_locus = input_location;
2441 new_use->in_omp_scope = false;
2443 new_use->next = ent->uses;
2444 ent->uses = new_use;
2445 return;
2448 if (ent->in_try_scope || ent->in_catch_scope
2449 || ent->in_omp_scope || ent->bad_decls)
2451 pedwarn ("jump to label %q+D", decl);
2452 pedwarn (" from here");
2453 identified = true;
2456 for (bad = ent->bad_decls; bad; bad = TREE_CHAIN (bad))
2458 tree b = TREE_VALUE (bad);
2459 int u = decl_jump_unsafe (b);
2461 if (u > 1 && DECL_ARTIFICIAL (b))
2463 /* Can't skip init of __exception_info. */
2464 error ("%J enters catch block", b);
2465 saw_catch = true;
2467 else if (u > 1)
2468 error (" skips initialization of %q+#D", b);
2469 else
2470 pedwarn (" enters scope of non-POD %q+#D", b);
2473 if (ent->in_try_scope)
2474 error (" enters try block");
2475 else if (ent->in_catch_scope && !saw_catch)
2476 error (" enters catch block");
2478 if (ent->in_omp_scope)
2479 error (" enters OpenMP structured block");
2480 else if (flag_openmp)
2482 struct cp_binding_level *b;
2483 for (b = current_binding_level; b ; b = b->level_chain)
2485 if (b == ent->binding_level)
2486 break;
2487 if (b->kind == sk_omp)
2489 if (!identified)
2491 pedwarn ("jump to label %q+D", decl);
2492 pedwarn (" from here");
2493 identified = true;
2495 error (" exits OpenMP structured block");
2496 break;
2502 /* Check that a return is ok wrt OpenMP structured blocks.
2503 Called by finish_return_stmt. Returns true if all is well. */
2505 bool
2506 check_omp_return (void)
2508 struct cp_binding_level *b;
2509 for (b = current_binding_level; b ; b = b->level_chain)
2510 if (b->kind == sk_omp)
2512 error ("invalid exit from OpenMP structured block");
2513 return false;
2515 return true;
2518 /* Define a label, specifying the location in the source file.
2519 Return the LABEL_DECL node for the label. */
2521 tree
2522 define_label (location_t location, tree name)
2524 struct named_label_entry *ent, dummy;
2525 struct cp_binding_level *p;
2526 tree decl;
2528 timevar_push (TV_NAME_LOOKUP);
2530 decl = lookup_label (name);
2532 dummy.label_decl = decl;
2533 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2534 gcc_assert (ent != NULL);
2536 /* After labels, make any new cleanups in the function go into their
2537 own new (temporary) binding contour. */
2538 for (p = current_binding_level;
2539 p->kind != sk_function_parms;
2540 p = p->level_chain)
2541 p->more_cleanups_ok = 0;
2543 if (name == get_identifier ("wchar_t"))
2544 pedwarn ("label named wchar_t");
2546 if (DECL_INITIAL (decl) != NULL_TREE)
2548 error ("duplicate label %qD", decl);
2549 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2551 else
2553 struct named_label_use_entry *use;
2555 /* Mark label as having been defined. */
2556 DECL_INITIAL (decl) = error_mark_node;
2557 /* Say where in the source. */
2558 DECL_SOURCE_LOCATION (decl) = location;
2560 ent->binding_level = current_binding_level;
2561 ent->names_in_scope = current_binding_level->names;
2563 for (use = ent->uses; use ; use = use->next)
2564 check_previous_goto (decl, use);
2565 ent->uses = NULL;
2568 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2571 struct cp_switch
2573 struct cp_binding_level *level;
2574 struct cp_switch *next;
2575 /* The SWITCH_STMT being built. */
2576 tree switch_stmt;
2577 /* A splay-tree mapping the low element of a case range to the high
2578 element, or NULL_TREE if there is no high element. Used to
2579 determine whether or not a new case label duplicates an old case
2580 label. We need a tree, rather than simply a hash table, because
2581 of the GNU case range extension. */
2582 splay_tree cases;
2585 /* A stack of the currently active switch statements. The innermost
2586 switch statement is on the top of the stack. There is no need to
2587 mark the stack for garbage collection because it is only active
2588 during the processing of the body of a function, and we never
2589 collect at that point. */
2591 static struct cp_switch *switch_stack;
2593 /* Called right after a switch-statement condition is parsed.
2594 SWITCH_STMT is the switch statement being parsed. */
2596 void
2597 push_switch (tree switch_stmt)
2599 struct cp_switch *p = XNEW (struct cp_switch);
2600 p->level = current_binding_level;
2601 p->next = switch_stack;
2602 p->switch_stmt = switch_stmt;
2603 p->cases = splay_tree_new (case_compare, NULL, NULL);
2604 switch_stack = p;
2607 void
2608 pop_switch (void)
2610 struct cp_switch *cs = switch_stack;
2611 location_t switch_location;
2613 /* Emit warnings as needed. */
2614 if (EXPR_HAS_LOCATION (cs->switch_stmt))
2615 switch_location = EXPR_LOCATION (cs->switch_stmt);
2616 else
2617 switch_location = input_location;
2618 if (!processing_template_decl)
2619 c_do_switch_warnings (cs->cases, switch_location,
2620 SWITCH_STMT_TYPE (cs->switch_stmt),
2621 SWITCH_STMT_COND (cs->switch_stmt));
2623 splay_tree_delete (cs->cases);
2624 switch_stack = switch_stack->next;
2625 free (cs);
2628 /* Note that we've seen a definition of a case label, and complain if this
2629 is a bad place for one. */
2631 tree
2632 finish_case_label (tree low_value, tree high_value)
2634 tree cond, r;
2635 struct cp_binding_level *p;
2637 if (processing_template_decl)
2639 tree label;
2641 /* For templates, just add the case label; we'll do semantic
2642 analysis at instantiation-time. */
2643 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2644 return add_stmt (build_case_label (low_value, high_value, label));
2647 /* Find the condition on which this switch statement depends. */
2648 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
2649 if (cond && TREE_CODE (cond) == TREE_LIST)
2650 cond = TREE_VALUE (cond);
2652 if (!check_switch_goto (switch_stack->level))
2653 return error_mark_node;
2655 r = c_add_case_label (switch_stack->cases, cond, TREE_TYPE (cond),
2656 low_value, high_value);
2658 /* After labels, make any new cleanups in the function go into their
2659 own new (temporary) binding contour. */
2660 for (p = current_binding_level;
2661 p->kind != sk_function_parms;
2662 p = p->level_chain)
2663 p->more_cleanups_ok = 0;
2665 return r;
2668 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2670 static hashval_t
2671 typename_hash (const void* k)
2673 hashval_t hash;
2674 tree t = (tree) k;
2676 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2677 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
2679 return hash;
2682 typedef struct typename_info {
2683 tree scope;
2684 tree name;
2685 tree template_id;
2686 bool enum_p;
2687 bool class_p;
2688 } typename_info;
2690 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
2692 static int
2693 typename_compare (const void * k1, const void * k2)
2695 tree t1;
2696 const typename_info *t2;
2698 t1 = (tree) k1;
2699 t2 = (const typename_info *) k2;
2701 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2702 && TYPE_CONTEXT (t1) == t2->scope
2703 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
2704 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
2705 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
2708 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
2709 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
2711 Returns the new TYPENAME_TYPE. */
2713 static GTY ((param_is (union tree_node))) htab_t typename_htab;
2715 static tree
2716 build_typename_type (tree context, tree name, tree fullname,
2717 enum tag_types tag_type)
2719 tree t;
2720 tree d;
2721 typename_info ti;
2722 void **e;
2723 hashval_t hash;
2725 if (typename_htab == NULL)
2726 typename_htab = htab_create_ggc (61, &typename_hash,
2727 &typename_compare, NULL);
2729 ti.scope = FROB_CONTEXT (context);
2730 ti.name = name;
2731 ti.template_id = fullname;
2732 ti.enum_p = tag_type == enum_type;
2733 ti.class_p = (tag_type == class_type
2734 || tag_type == record_type
2735 || tag_type == union_type);
2736 hash = (htab_hash_pointer (ti.scope)
2737 ^ htab_hash_pointer (ti.name));
2739 /* See if we already have this type. */
2740 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
2741 if (*e)
2742 t = (tree) *e;
2743 else
2745 /* Build the TYPENAME_TYPE. */
2746 t = make_aggr_type (TYPENAME_TYPE);
2747 TYPE_CONTEXT (t) = ti.scope;
2748 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
2749 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
2750 TYPENAME_IS_CLASS_P (t) = ti.class_p;
2752 /* Build the corresponding TYPE_DECL. */
2753 d = build_decl (TYPE_DECL, name, t);
2754 TYPE_NAME (TREE_TYPE (d)) = d;
2755 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2756 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2757 DECL_ARTIFICIAL (d) = 1;
2759 /* Store it in the hash table. */
2760 *e = t;
2763 return t;
2766 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
2767 provided to name the type. Returns an appropriate type, unless an
2768 error occurs, in which case error_mark_node is returned. If we
2769 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
2770 return that, rather than the _TYPE it corresponds to, in other
2771 cases we look through the type decl. If TF_ERROR is set, complain
2772 about errors, otherwise be quiet. */
2774 tree
2775 make_typename_type (tree context, tree name, enum tag_types tag_type,
2776 tsubst_flags_t complain)
2778 tree fullname;
2779 tree t;
2780 bool want_template;
2782 if (name == error_mark_node
2783 || context == NULL_TREE
2784 || context == error_mark_node)
2785 return error_mark_node;
2787 if (TYPE_P (name))
2789 if (!(TYPE_LANG_SPECIFIC (name)
2790 && (CLASSTYPE_IS_TEMPLATE (name)
2791 || CLASSTYPE_USE_TEMPLATE (name))))
2792 name = TYPE_IDENTIFIER (name);
2793 else
2794 /* Create a TEMPLATE_ID_EXPR for the type. */
2795 name = build_nt (TEMPLATE_ID_EXPR,
2796 CLASSTYPE_TI_TEMPLATE (name),
2797 CLASSTYPE_TI_ARGS (name));
2799 else if (TREE_CODE (name) == TYPE_DECL)
2800 name = DECL_NAME (name);
2802 fullname = name;
2804 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2806 name = TREE_OPERAND (name, 0);
2807 if (TREE_CODE (name) == TEMPLATE_DECL)
2808 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
2810 if (TREE_CODE (name) == TEMPLATE_DECL)
2812 error ("%qD used without template parameters", name);
2813 return error_mark_node;
2815 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2816 gcc_assert (TYPE_P (context));
2818 /* When the CONTEXT is a dependent type, NAME could refer to a
2819 dependent base class of CONTEXT. So we cannot peek inside it,
2820 even if CONTEXT is a currently open scope. */
2821 if (dependent_type_p (context))
2822 return build_typename_type (context, name, fullname, tag_type);
2824 if (!IS_AGGR_TYPE (context))
2826 if (complain & tf_error)
2827 error ("%q#T is not a class", context);
2828 return error_mark_node;
2831 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
2833 /* We should only set WANT_TYPE when we're a nested typename type.
2834 Then we can give better diagnostics if we find a non-type. */
2835 t = lookup_field (context, name, 0, /*want_type=*/true);
2836 if (!t)
2838 if (complain & tf_error)
2839 error (want_template ? "no class template named %q#T in %q#T"
2840 : "no type named %q#T in %q#T", name, context);
2841 return error_mark_node;
2844 if (want_template && !DECL_CLASS_TEMPLATE_P (t))
2846 if (complain & tf_error)
2847 error ("%<typename %T::%D%> names %q#T, which is not a class template",
2848 context, name, t);
2849 return error_mark_node;
2851 if (!want_template && TREE_CODE (t) != TYPE_DECL)
2853 if (complain & tf_error)
2854 error ("%<typename %T::%D%> names %q#T, which is not a type",
2855 context, name, t);
2856 return error_mark_node;
2859 if (complain & tf_error)
2860 perform_or_defer_access_check (TYPE_BINFO (context), t);
2862 if (want_template)
2863 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
2864 NULL_TREE, context,
2865 /*entering_scope=*/0,
2866 tf_warning_or_error | tf_user);
2868 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
2869 t = TREE_TYPE (t);
2871 return t;
2874 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
2875 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
2876 in which case error_mark_node is returned.
2878 If PARM_LIST is non-NULL, also make sure that the template parameter
2879 list of TEMPLATE_DECL matches.
2881 If COMPLAIN zero, don't complain about any errors that occur. */
2883 tree
2884 make_unbound_class_template (tree context, tree name, tree parm_list,
2885 tsubst_flags_t complain)
2887 tree t;
2888 tree d;
2890 if (TYPE_P (name))
2891 name = TYPE_IDENTIFIER (name);
2892 else if (DECL_P (name))
2893 name = DECL_NAME (name);
2894 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2896 if (!dependent_type_p (context)
2897 || currently_open_class (context))
2899 tree tmpl = NULL_TREE;
2901 if (IS_AGGR_TYPE (context))
2902 tmpl = lookup_field (context, name, 0, false);
2904 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
2906 if (complain & tf_error)
2907 error ("no class template named %q#T in %q#T", name, context);
2908 return error_mark_node;
2911 if (parm_list
2912 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
2914 if (complain & tf_error)
2916 error ("template parameters do not match template");
2917 error ("%q+D declared here", tmpl);
2919 return error_mark_node;
2922 if (complain & tf_error)
2923 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
2925 return tmpl;
2928 /* Build the UNBOUND_CLASS_TEMPLATE. */
2929 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
2930 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
2931 TREE_TYPE (t) = NULL_TREE;
2933 /* Build the corresponding TEMPLATE_DECL. */
2934 d = build_decl (TEMPLATE_DECL, name, t);
2935 TYPE_NAME (TREE_TYPE (d)) = d;
2936 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2937 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2938 DECL_ARTIFICIAL (d) = 1;
2939 DECL_TEMPLATE_PARMS (d) = parm_list;
2941 return t;
2946 /* Push the declarations of builtin types into the namespace.
2947 RID_INDEX is the index of the builtin type in the array
2948 RID_POINTERS. NAME is the name used when looking up the builtin
2949 type. TYPE is the _TYPE node for the builtin type. */
2951 void
2952 record_builtin_type (enum rid rid_index,
2953 const char* name,
2954 tree type)
2956 tree rname = NULL_TREE, tname = NULL_TREE;
2957 tree tdecl = NULL_TREE;
2959 if ((int) rid_index < (int) RID_MAX)
2960 rname = ridpointers[(int) rid_index];
2961 if (name)
2962 tname = get_identifier (name);
2964 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
2965 eliminated. Built-in types should not be looked up name; their
2966 names are keywords that the parser can recognize. However, there
2967 is code in c-common.c that uses identifier_global_value to look
2968 up built-in types by name. */
2969 if (tname)
2971 tdecl = build_decl (TYPE_DECL, tname, type);
2972 DECL_ARTIFICIAL (tdecl) = 1;
2973 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
2975 if (rname)
2977 if (!tdecl)
2979 tdecl = build_decl (TYPE_DECL, rname, type);
2980 DECL_ARTIFICIAL (tdecl) = 1;
2982 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
2985 if (!TYPE_NAME (type))
2986 TYPE_NAME (type) = tdecl;
2988 if (tdecl)
2989 debug_hooks->type_decl (tdecl, 0);
2992 /* Record one of the standard Java types.
2993 * Declare it as having the given NAME.
2994 * If SIZE > 0, it is the size of one of the integral types;
2995 * otherwise it is the negative of the size of one of the other types. */
2997 static tree
2998 record_builtin_java_type (const char* name, int size)
3000 tree type, decl;
3001 if (size > 0)
3002 type = make_signed_type (size);
3003 else if (size > -32)
3004 { /* "__java_char" or ""__java_boolean". */
3005 type = make_unsigned_type (-size);
3006 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3008 else
3009 { /* "__java_float" or ""__java_double". */
3010 type = make_node (REAL_TYPE);
3011 TYPE_PRECISION (type) = - size;
3012 layout_type (type);
3014 record_builtin_type (RID_MAX, name, type);
3015 decl = TYPE_NAME (type);
3017 /* Suppress generate debug symbol entries for these types,
3018 since for normal C++ they are just clutter.
3019 However, push_lang_context undoes this if extern "Java" is seen. */
3020 DECL_IGNORED_P (decl) = 1;
3022 TYPE_FOR_JAVA (type) = 1;
3023 return type;
3026 /* Push a type into the namespace so that the back-ends ignore it. */
3028 static void
3029 record_unknown_type (tree type, const char* name)
3031 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
3032 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3033 DECL_IGNORED_P (decl) = 1;
3034 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3035 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3036 TYPE_ALIGN (type) = 1;
3037 TYPE_USER_ALIGN (type) = 0;
3038 TYPE_MODE (type) = TYPE_MODE (void_type_node);
3041 /* A string for which we should create an IDENTIFIER_NODE at
3042 startup. */
3044 typedef struct predefined_identifier
3046 /* The name of the identifier. */
3047 const char *const name;
3048 /* The place where the IDENTIFIER_NODE should be stored. */
3049 tree *const node;
3050 /* Nonzero if this is the name of a constructor or destructor. */
3051 const int ctor_or_dtor_p;
3052 } predefined_identifier;
3054 /* Create all the predefined identifiers. */
3056 static void
3057 initialize_predefined_identifiers (void)
3059 const predefined_identifier *pid;
3061 /* A table of identifiers to create at startup. */
3062 static const predefined_identifier predefined_identifiers[] = {
3063 { "C++", &lang_name_cplusplus, 0 },
3064 { "C", &lang_name_c, 0 },
3065 { "Java", &lang_name_java, 0 },
3066 /* Some of these names have a trailing space so that it is
3067 impossible for them to conflict with names written by users. */
3068 { "__ct ", &ctor_identifier, 1 },
3069 { "__base_ctor ", &base_ctor_identifier, 1 },
3070 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3071 { "__dt ", &dtor_identifier, 1 },
3072 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3073 { "__base_dtor ", &base_dtor_identifier, 1 },
3074 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3075 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3076 { "nelts", &nelts_identifier, 0 },
3077 { THIS_NAME, &this_identifier, 0 },
3078 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3079 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3080 { "_vptr", &vptr_identifier, 0 },
3081 { "__vtt_parm", &vtt_parm_identifier, 0 },
3082 { "::", &global_scope_name, 0 },
3083 { "std", &std_identifier, 0 },
3084 { NULL, NULL, 0 }
3087 for (pid = predefined_identifiers; pid->name; ++pid)
3089 *pid->node = get_identifier (pid->name);
3090 if (pid->ctor_or_dtor_p)
3091 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3095 /* Create the predefined scalar types of C,
3096 and some nodes representing standard constants (0, 1, (void *)0).
3097 Initialize the global binding level.
3098 Make definitions for built-in primitive functions. */
3100 void
3101 cxx_init_decl_processing (void)
3103 tree void_ftype;
3104 tree void_ftype_ptr;
3106 build_common_tree_nodes (flag_signed_char, false);
3108 /* Create all the identifiers we need. */
3109 initialize_predefined_identifiers ();
3111 /* Create the global variables. */
3112 push_to_top_level ();
3114 current_function_decl = NULL_TREE;
3115 current_binding_level = NULL;
3116 /* Enter the global namespace. */
3117 gcc_assert (global_namespace == NULL_TREE);
3118 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3119 void_type_node);
3120 TREE_PUBLIC (global_namespace) = 1;
3121 begin_scope (sk_namespace, global_namespace);
3123 current_lang_name = NULL_TREE;
3125 /* Adjust various flags based on command-line settings. */
3126 if (!flag_permissive)
3127 flag_pedantic_errors = 1;
3128 if (!flag_no_inline)
3130 flag_inline_trees = 1;
3131 flag_no_inline = 1;
3133 if (flag_inline_functions)
3134 flag_inline_trees = 2;
3136 /* Force minimum function alignment if using the least significant
3137 bit of function pointers to store the virtual bit. */
3138 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
3139 && force_align_functions_log < 1)
3140 force_align_functions_log = 1;
3142 /* Initially, C. */
3143 current_lang_name = lang_name_c;
3145 /* Create the `std' namespace. */
3146 push_namespace (std_identifier);
3147 std_node = current_namespace;
3148 pop_namespace ();
3150 c_common_nodes_and_builtins ();
3152 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3153 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3154 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3155 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3156 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3157 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3158 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3159 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3161 integer_two_node = build_int_cst (NULL_TREE, 2);
3162 integer_three_node = build_int_cst (NULL_TREE, 3);
3164 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3165 truthvalue_type_node = boolean_type_node;
3166 truthvalue_false_node = boolean_false_node;
3167 truthvalue_true_node = boolean_true_node;
3169 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3171 #if 0
3172 record_builtin_type (RID_MAX, NULL, string_type_node);
3173 #endif
3175 delta_type_node = ptrdiff_type_node;
3176 vtable_index_type = ptrdiff_type_node;
3178 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3179 void_ftype = build_function_type (void_type_node, void_list_node);
3180 void_ftype_ptr = build_function_type (void_type_node,
3181 tree_cons (NULL_TREE,
3182 ptr_type_node,
3183 void_list_node));
3184 void_ftype_ptr
3185 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3187 /* C++ extensions */
3189 unknown_type_node = make_node (UNKNOWN_TYPE);
3190 record_unknown_type (unknown_type_node, "unknown type");
3192 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3193 TREE_TYPE (unknown_type_node) = unknown_type_node;
3195 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3196 result. */
3197 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3198 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3201 /* Make sure we get a unique function type, so we can give
3202 its pointer type a name. (This wins for gdb.) */
3203 tree vfunc_type = make_node (FUNCTION_TYPE);
3204 TREE_TYPE (vfunc_type) = integer_type_node;
3205 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3206 layout_type (vfunc_type);
3208 vtable_entry_type = build_pointer_type (vfunc_type);
3210 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3212 vtbl_type_node
3213 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3214 layout_type (vtbl_type_node);
3215 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3216 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3217 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3218 layout_type (vtbl_ptr_type_node);
3219 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3221 push_namespace (get_identifier ("__cxxabiv1"));
3222 abi_node = current_namespace;
3223 pop_namespace ();
3225 global_type_node = make_node (LANG_TYPE);
3226 record_unknown_type (global_type_node, "global type");
3228 /* Now, C++. */
3229 current_lang_name = lang_name_cplusplus;
3232 tree bad_alloc_id;
3233 tree bad_alloc_type_node;
3234 tree bad_alloc_decl;
3235 tree newtype, deltype;
3236 tree ptr_ftype_sizetype;
3238 push_namespace (std_identifier);
3239 bad_alloc_id = get_identifier ("bad_alloc");
3240 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
3241 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3242 bad_alloc_decl
3243 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3244 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3245 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
3246 pop_namespace ();
3248 ptr_ftype_sizetype
3249 = build_function_type (ptr_type_node,
3250 tree_cons (NULL_TREE,
3251 size_type_node,
3252 void_list_node));
3253 newtype = build_exception_variant
3254 (ptr_ftype_sizetype, add_exception_specifier
3255 (NULL_TREE, bad_alloc_type_node, -1));
3256 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3257 push_cp_library_fn (NEW_EXPR, newtype);
3258 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3259 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3260 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3263 abort_fndecl
3264 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3266 /* Perform other language dependent initializations. */
3267 init_class_processing ();
3268 init_rtti_processing ();
3270 if (flag_exceptions)
3271 init_exception_processing ();
3273 if (! supports_one_only ())
3274 flag_weak = 0;
3276 make_fname_decl = cp_make_fname_decl;
3277 start_fname_decls ();
3279 /* Show we use EH for cleanups. */
3280 if (flag_exceptions)
3281 using_eh_for_cleanups ();
3284 /* Generate an initializer for a function naming variable from
3285 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3286 filled in with the type of the init. */
3288 tree
3289 cp_fname_init (const char* name, tree *type_p)
3291 tree domain = NULL_TREE;
3292 tree type;
3293 tree init = NULL_TREE;
3294 size_t length = 0;
3296 if (name)
3298 length = strlen (name);
3299 domain = build_index_type (size_int (length));
3300 init = build_string (length + 1, name);
3303 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3304 type = build_cplus_array_type (type, domain);
3306 *type_p = type;
3308 if (init)
3309 TREE_TYPE (init) = type;
3310 else
3311 init = error_mark_node;
3313 return init;
3316 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
3317 decl, NAME is the initialization string and TYPE_DEP indicates whether
3318 NAME depended on the type of the function. We make use of that to detect
3319 __PRETTY_FUNCTION__ inside a template fn. This is being done
3320 lazily at the point of first use, so we mustn't push the decl now. */
3322 static tree
3323 cp_make_fname_decl (tree id, int type_dep)
3325 const char *const name = (type_dep && processing_template_decl
3326 ? NULL : fname_as_string (type_dep));
3327 tree type;
3328 tree init = cp_fname_init (name, &type);
3329 tree decl = build_decl (VAR_DECL, id, type);
3331 if (name)
3332 free ((char *) name);
3334 /* As we're using pushdecl_with_scope, we must set the context. */
3335 DECL_CONTEXT (decl) = current_function_decl;
3336 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3338 TREE_STATIC (decl) = 1;
3339 TREE_READONLY (decl) = 1;
3340 DECL_ARTIFICIAL (decl) = 1;
3342 TREE_USED (decl) = 1;
3344 if (current_function_decl)
3346 struct cp_binding_level *b = current_binding_level;
3347 while (b->level_chain->kind != sk_function_parms)
3348 b = b->level_chain;
3349 pushdecl_with_scope (decl, b, /*is_friend=*/false);
3350 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
3351 LOOKUP_ONLYCONVERTING);
3353 else
3354 pushdecl_top_level_and_finish (decl, init);
3356 return decl;
3359 static tree
3360 builtin_function_1 (tree decl, tree context)
3362 tree id = DECL_NAME (decl);
3363 const char *name = IDENTIFIER_POINTER (id);
3365 retrofit_lang_decl (decl);
3367 /* All nesting of C++ functions is lexical; there is never a "static
3368 chain" in the sense of GNU C nested functions. */
3369 DECL_NO_STATIC_CHAIN (decl) = 1;
3371 DECL_ARTIFICIAL (decl) = 1;
3372 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
3373 SET_DECL_LANGUAGE (decl, lang_c);
3374 /* Runtime library routines are, by definition, available in an
3375 external shared object. */
3376 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
3377 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3379 DECL_CONTEXT (decl) = context;
3381 pushdecl (decl);
3383 /* A function in the user's namespace should have an explicit
3384 declaration before it is used. Mark the built-in function as
3385 anticipated but not actually declared. */
3386 if (name[0] != '_' || name[1] != '_')
3387 DECL_ANTICIPATED (decl) = 1;
3389 return decl;
3392 tree
3393 cxx_builtin_function (tree decl)
3395 tree id = DECL_NAME (decl);
3396 const char *name = IDENTIFIER_POINTER (id);
3397 tree decl2 = copy_node(decl);
3398 /* All builtins that don't begin with an '_' should additionally
3399 go in the 'std' namespace. */
3400 if (name[0] != '_')
3402 push_namespace (std_identifier);
3403 builtin_function_1 (decl, std_node);
3404 pop_namespace ();
3407 return builtin_function_1 (decl2, NULL_TREE);
3410 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
3411 function. Not called directly. */
3413 static tree
3414 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3416 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3417 DECL_EXTERNAL (fn) = 1;
3418 TREE_PUBLIC (fn) = 1;
3419 DECL_ARTIFICIAL (fn) = 1;
3420 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
3421 SET_DECL_LANGUAGE (fn, lang_c);
3422 /* Runtime library routines are, by definition, available in an
3423 external shared object. */
3424 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3425 DECL_VISIBILITY_SPECIFIED (fn) = 1;
3426 return fn;
3429 /* Returns the _DECL for a library function with C linkage.
3430 We assume that such functions never throw; if this is incorrect,
3431 callers should unset TREE_NOTHROW. */
3433 tree
3434 build_library_fn (tree name, tree type)
3436 tree fn = build_library_fn_1 (name, ERROR_MARK, type);
3437 TREE_NOTHROW (fn) = 1;
3438 return fn;
3441 /* Returns the _DECL for a library function with C++ linkage. */
3443 static tree
3444 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
3446 tree fn = build_library_fn_1 (name, operator_code, type);
3447 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
3448 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
3449 SET_DECL_LANGUAGE (fn, lang_cplusplus);
3450 return fn;
3453 /* Like build_library_fn, but takes a C string instead of an
3454 IDENTIFIER_NODE. */
3456 tree
3457 build_library_fn_ptr (const char* name, tree type)
3459 return build_library_fn (get_identifier (name), type);
3462 /* Like build_cp_library_fn, but takes a C string instead of an
3463 IDENTIFIER_NODE. */
3465 tree
3466 build_cp_library_fn_ptr (const char* name, tree type)
3468 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
3471 /* Like build_library_fn, but also pushes the function so that we will
3472 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
3474 tree
3475 push_library_fn (tree name, tree type)
3477 tree fn = build_library_fn (name, type);
3478 pushdecl_top_level (fn);
3479 return fn;
3482 /* Like build_cp_library_fn, but also pushes the function so that it
3483 will be found by normal lookup. */
3485 static tree
3486 push_cp_library_fn (enum tree_code operator_code, tree type)
3488 tree fn = build_cp_library_fn (ansi_opname (operator_code),
3489 operator_code,
3490 type);
3491 pushdecl (fn);
3492 return fn;
3495 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3496 a FUNCTION_TYPE. */
3498 tree
3499 push_void_library_fn (tree name, tree parmtypes)
3501 tree type = build_function_type (void_type_node, parmtypes);
3502 return push_library_fn (name, type);
3505 /* Like push_library_fn, but also note that this function throws
3506 and does not return. Used for __throw_foo and the like. */
3508 tree
3509 push_throw_library_fn (tree name, tree type)
3511 tree fn = push_library_fn (name, type);
3512 TREE_THIS_VOLATILE (fn) = 1;
3513 TREE_NOTHROW (fn) = 0;
3514 return fn;
3517 /* When we call finish_struct for an anonymous union, we create
3518 default copy constructors and such. But, an anonymous union
3519 shouldn't have such things; this function undoes the damage to the
3520 anonymous union type T.
3522 (The reason that we create the synthesized methods is that we don't
3523 distinguish `union { int i; }' from `typedef union { int i; } U'.
3524 The first is an anonymous union; the second is just an ordinary
3525 union type.) */
3527 void
3528 fixup_anonymous_aggr (tree t)
3530 tree *q;
3532 /* Wipe out memory of synthesized methods. */
3533 TYPE_HAS_CONSTRUCTOR (t) = 0;
3534 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3535 TYPE_HAS_INIT_REF (t) = 0;
3536 TYPE_HAS_CONST_INIT_REF (t) = 0;
3537 TYPE_HAS_ASSIGN_REF (t) = 0;
3538 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
3540 /* Splice the implicitly generated functions out of the TYPE_METHODS
3541 list. */
3542 q = &TYPE_METHODS (t);
3543 while (*q)
3545 if (DECL_ARTIFICIAL (*q))
3546 *q = TREE_CHAIN (*q);
3547 else
3548 q = &TREE_CHAIN (*q);
3551 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
3552 if (TYPE_METHODS (t))
3553 error ("%Jan anonymous union cannot have function members",
3554 TYPE_MAIN_DECL (t));
3556 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3557 assignment operators (because they cannot have these methods themselves).
3558 For anonymous unions this is already checked because they are not allowed
3559 in any union, otherwise we have to check it. */
3560 if (TREE_CODE (t) != UNION_TYPE)
3562 tree field, type;
3564 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3565 if (TREE_CODE (field) == FIELD_DECL)
3567 type = TREE_TYPE (field);
3568 if (CLASS_TYPE_P (type))
3570 if (TYPE_NEEDS_CONSTRUCTING (type))
3571 error ("member %q+#D with constructor not allowed "
3572 "in anonymous aggregate", field);
3573 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3574 error ("member %q+#D with destructor not allowed "
3575 "in anonymous aggregate", field);
3576 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3577 error ("member %q+#D with copy assignment operator "
3578 "not allowed in anonymous aggregate", field);
3584 /* Make sure that a declaration with no declarator is well-formed, i.e.
3585 just declares a tagged type or anonymous union.
3587 Returns the type declared; or NULL_TREE if none. */
3589 tree
3590 check_tag_decl (cp_decl_specifier_seq *declspecs)
3592 int saw_friend = declspecs->specs[(int)ds_friend] != 0;
3593 int saw_typedef = declspecs->specs[(int)ds_typedef] != 0;
3594 /* If a class, struct, or enum type is declared by the DECLSPECS
3595 (i.e, if a class-specifier, enum-specifier, or non-typename
3596 elaborated-type-specifier appears in the DECLSPECS),
3597 DECLARED_TYPE is set to the corresponding type. */
3598 tree declared_type = NULL_TREE;
3599 bool error_p = false;
3601 if (declspecs->multiple_types_p)
3602 error ("multiple types in one declaration");
3603 else if (declspecs->redefined_builtin_type)
3605 if (!in_system_header)
3606 pedwarn ("redeclaration of C++ built-in type %qT",
3607 declspecs->redefined_builtin_type);
3608 return NULL_TREE;
3611 if (declspecs->type
3612 && TYPE_P (declspecs->type)
3613 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
3614 && IS_AGGR_TYPE (declspecs->type))
3615 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
3616 declared_type = declspecs->type;
3617 else if (declspecs->type == error_mark_node)
3618 error_p = true;
3619 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
3620 pedwarn ("declaration does not declare anything");
3621 /* Check for an anonymous union. */
3622 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
3623 && TYPE_ANONYMOUS_P (declared_type))
3625 /* 7/3 In a simple-declaration, the optional init-declarator-list
3626 can be omitted only when declaring a class (clause 9) or
3627 enumeration (7.2), that is, when the decl-specifier-seq contains
3628 either a class-specifier, an elaborated-type-specifier with
3629 a class-key (9.1), or an enum-specifier. In these cases and
3630 whenever a class-specifier or enum-specifier is present in the
3631 decl-specifier-seq, the identifiers in these specifiers are among
3632 the names being declared by the declaration (as class-name,
3633 enum-names, or enumerators, depending on the syntax). In such
3634 cases, and except for the declaration of an unnamed bit-field (9.6),
3635 the decl-specifier-seq shall introduce one or more names into the
3636 program, or shall redeclare a name introduced by a previous
3637 declaration. [Example:
3638 enum { }; // ill-formed
3639 typedef class { }; // ill-formed
3640 --end example] */
3641 if (saw_typedef)
3643 error ("missing type-name in typedef-declaration");
3644 return NULL_TREE;
3646 /* Anonymous unions are objects, so they can have specifiers. */;
3647 SET_ANON_AGGR_TYPE_P (declared_type);
3649 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
3650 && !in_system_header)
3651 pedwarn ("ISO C++ prohibits anonymous structs");
3654 else
3656 if (declspecs->specs[(int)ds_inline]
3657 || declspecs->specs[(int)ds_virtual])
3658 error ("%qs can only be specified for functions",
3659 declspecs->specs[(int)ds_inline]
3660 ? "inline" : "virtual");
3661 else if (saw_friend
3662 && (!current_class_type
3663 || current_scope () != current_class_type))
3664 error ("%<friend%> can only be specified inside a class");
3665 else if (declspecs->specs[(int)ds_explicit])
3666 error ("%<explicit%> can only be specified for constructors");
3667 else if (declspecs->storage_class)
3668 error ("a storage class can only be specified for objects "
3669 "and functions");
3670 else if (declspecs->specs[(int)ds_const]
3671 || declspecs->specs[(int)ds_volatile]
3672 || declspecs->specs[(int)ds_restrict]
3673 || declspecs->specs[(int)ds_thread])
3674 error ("qualifiers can only be specified for objects "
3675 "and functions");
3678 return declared_type;
3681 /* Called when a declaration is seen that contains no names to declare.
3682 If its type is a reference to a structure, union or enum inherited
3683 from a containing scope, shadow that tag name for the current scope
3684 with a forward reference.
3685 If its type defines a new named structure or union
3686 or defines an enum, it is valid but we need not do anything here.
3687 Otherwise, it is an error.
3689 C++: may have to grok the declspecs to learn about static,
3690 complain for anonymous unions.
3692 Returns the TYPE declared -- or NULL_TREE if none. */
3694 tree
3695 shadow_tag (cp_decl_specifier_seq *declspecs)
3697 tree t = check_tag_decl (declspecs);
3699 if (!t)
3700 return NULL_TREE;
3702 if (declspecs->attributes)
3704 warning (0, "attribute ignored in declaration of %q+#T", t);
3705 warning (0, "attribute for %q+#T must follow the %qs keyword",
3706 t, class_key_or_enum_as_string (t));
3710 if (maybe_process_partial_specialization (t) == error_mark_node)
3711 return NULL_TREE;
3713 /* This is where the variables in an anonymous union are
3714 declared. An anonymous union declaration looks like:
3715 union { ... } ;
3716 because there is no declarator after the union, the parser
3717 sends that declaration here. */
3718 if (ANON_AGGR_TYPE_P (t))
3720 fixup_anonymous_aggr (t);
3722 if (TYPE_FIELDS (t))
3724 tree decl = grokdeclarator (/*declarator=*/NULL,
3725 declspecs, NORMAL, 0, NULL);
3726 finish_anon_union (decl);
3730 return t;
3733 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3735 tree
3736 groktypename (cp_decl_specifier_seq *type_specifiers,
3737 const cp_declarator *declarator)
3739 tree attrs;
3740 tree type;
3741 attrs = type_specifiers->attributes;
3742 type_specifiers->attributes = NULL_TREE;
3743 type = grokdeclarator (declarator, type_specifiers, TYPENAME, 0, &attrs);
3744 if (attrs)
3745 cplus_decl_attributes (&type, attrs, 0);
3746 return type;
3749 /* Decode a declarator in an ordinary declaration or data definition.
3750 This is called as soon as the type information and variable name
3751 have been parsed, before parsing the initializer if any.
3752 Here we create the ..._DECL node, fill in its type,
3753 and put it on the list of decls for the current context.
3754 The ..._DECL node is returned as the value.
3756 Exception: for arrays where the length is not specified,
3757 the type is left null, to be filled in by `cp_finish_decl'.
3759 Function definitions do not come here; they go to start_function
3760 instead. However, external and forward declarations of functions
3761 do go through here. Structure field declarations are done by
3762 grokfield and not through here. */
3764 tree
3765 start_decl (const cp_declarator *declarator,
3766 cp_decl_specifier_seq *declspecs,
3767 int initialized,
3768 tree attributes,
3769 tree prefix_attributes,
3770 tree *pushed_scope_p)
3772 tree decl;
3773 tree type, tem;
3774 tree context;
3776 *pushed_scope_p = NULL_TREE;
3778 /* An object declared as __attribute__((deprecated)) suppresses
3779 warnings of uses of other deprecated items. */
3780 if (lookup_attribute ("deprecated", attributes))
3781 deprecated_state = DEPRECATED_SUPPRESS;
3783 attributes = chainon (attributes, prefix_attributes);
3785 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
3786 &attributes);
3788 deprecated_state = DEPRECATED_NORMAL;
3790 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE
3791 || decl == error_mark_node)
3792 return error_mark_node;
3794 type = TREE_TYPE (decl);
3796 context = DECL_CONTEXT (decl);
3798 if (context)
3800 *pushed_scope_p = push_scope (context);
3802 /* We are only interested in class contexts, later. */
3803 if (TREE_CODE (context) == NAMESPACE_DECL)
3804 context = NULL_TREE;
3807 if (initialized)
3808 /* Is it valid for this decl to have an initializer at all?
3809 If not, set INITIALIZED to zero, which will indirectly
3810 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
3811 switch (TREE_CODE (decl))
3813 case TYPE_DECL:
3814 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3815 return error_mark_node;
3817 case FUNCTION_DECL:
3818 error ("function %q#D is initialized like a variable", decl);
3819 return error_mark_node;
3821 default:
3822 break;
3825 if (initialized)
3827 if (! toplevel_bindings_p ()
3828 && DECL_EXTERNAL (decl))
3829 warning (0, "declaration of %q#D has %<extern%> and is initialized",
3830 decl);
3831 DECL_EXTERNAL (decl) = 0;
3832 if (toplevel_bindings_p ())
3833 TREE_STATIC (decl) = 1;
3836 /* Set attributes here so if duplicate decl, will have proper attributes. */
3837 cplus_decl_attributes (&decl, attributes, 0);
3839 /* Dllimported symbols cannot be defined. Static data members (which
3840 can be initialized in-class and dllimported) go through grokfield,
3841 not here, so we don't need to exclude those decls when checking for
3842 a definition. */
3843 if (initialized && DECL_DLLIMPORT_P (decl))
3845 error ("definition of %q#D is marked %<dllimport%>", decl);
3846 DECL_DLLIMPORT_P (decl) = 0;
3849 /* If #pragma weak was used, mark the decl weak now. */
3850 maybe_apply_pragma_weak (decl);
3852 if (TREE_CODE (decl) == FUNCTION_DECL
3853 && DECL_DECLARED_INLINE_P (decl)
3854 && DECL_UNINLINABLE (decl)
3855 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3856 warning (0, "inline function %q+D given attribute noinline", decl);
3858 if (context && COMPLETE_TYPE_P (complete_type (context)))
3860 if (TREE_CODE (decl) == VAR_DECL)
3862 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
3863 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
3864 error ("%q#D is not a static member of %q#T", decl, context);
3865 else
3867 if (DECL_CONTEXT (field) != context)
3869 if (!same_type_p (DECL_CONTEXT (field), context))
3870 pedwarn ("ISO C++ does not permit %<%T::%D%> "
3871 "to be defined as %<%T::%D%>",
3872 DECL_CONTEXT (field), DECL_NAME (decl),
3873 context, DECL_NAME (decl));
3874 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
3876 if (processing_specialization
3877 && template_class_depth (context) == 0
3878 && CLASSTYPE_TEMPLATE_SPECIALIZATION (context))
3879 error ("template header not allowed in member definition "
3880 "of explicitly specialized class");
3881 /* Static data member are tricky; an in-class initialization
3882 still doesn't provide a definition, so the in-class
3883 declaration will have DECL_EXTERNAL set, but will have an
3884 initialization. Thus, duplicate_decls won't warn
3885 about this situation, and so we check here. */
3886 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
3887 error ("duplicate initialization of %qD", decl);
3888 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
3889 decl = field;
3892 else
3894 tree field = check_classfn (context, decl,
3895 (processing_template_decl
3896 > template_class_depth (context))
3897 ? current_template_parms
3898 : NULL_TREE);
3899 if (field && duplicate_decls (decl, field,
3900 /*newdecl_is_friend=*/false))
3901 decl = field;
3904 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
3905 DECL_IN_AGGR_P (decl) = 0;
3906 /* Do not mark DECL as an explicit specialization if it was not
3907 already marked as an instantiation; a declaration should
3908 never be marked as a specialization unless we know what
3909 template is being specialized. */
3910 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3912 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
3914 /* [temp.expl.spec] An explicit specialization of a static data
3915 member of a template is a definition if the declaration
3916 includes an initializer; otherwise, it is a declaration.
3918 We check for processing_specialization so this only applies
3919 to the new specialization syntax. */
3920 if (!initialized && processing_specialization)
3921 DECL_EXTERNAL (decl) = 1;
3924 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
3925 pedwarn ("declaration of %q#D outside of class is not definition",
3926 decl);
3929 /* Enter this declaration into the symbol table. */
3930 tem = maybe_push_decl (decl);
3932 if (processing_template_decl)
3933 tem = push_template_decl (tem);
3934 if (tem == error_mark_node)
3935 return error_mark_node;
3937 /* Tell the back-end to use or not use .common as appropriate. If we say
3938 -fconserve-space, we want this to save .data space, at the expense of
3939 wrong semantics. If we say -fno-conserve-space, we want this to
3940 produce errors about redefs; to do this we force variables into the
3941 data segment. */
3942 if (flag_conserve_space
3943 && TREE_CODE (tem) == VAR_DECL
3944 && TREE_PUBLIC (tem)
3945 && !DECL_THREAD_LOCAL_P (tem)
3946 && !have_global_bss_p ())
3947 DECL_COMMON (tem) = 1;
3949 if (!processing_template_decl && TREE_CODE (tem) == VAR_DECL)
3950 start_decl_1 (tem, initialized);
3952 return tem;
3955 void
3956 start_decl_1 (tree decl, bool initialized)
3958 tree type;
3960 gcc_assert (!processing_template_decl);
3962 if (error_operand_p (decl))
3963 return;
3965 gcc_assert (TREE_CODE (decl) == VAR_DECL);
3966 type = TREE_TYPE (decl);
3968 if (initialized)
3969 /* Is it valid for this decl to have an initializer at all?
3970 If not, set INITIALIZED to zero, which will indirectly
3971 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
3973 /* Don't allow initializations for incomplete types except for
3974 arrays which might be completed by the initialization. */
3975 if (COMPLETE_TYPE_P (complete_type (type)))
3976 ; /* A complete type is ok. */
3977 else if (TREE_CODE (type) != ARRAY_TYPE)
3979 error ("variable %q#D has initializer but incomplete type", decl);
3980 initialized = 0;
3981 type = TREE_TYPE (decl) = error_mark_node;
3983 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
3985 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
3986 error ("elements of array %q#D have incomplete type", decl);
3987 /* else we already gave an error in start_decl. */
3988 initialized = 0;
3991 else if (IS_AGGR_TYPE (type)
3992 && ! DECL_EXTERNAL (decl))
3994 if (!COMPLETE_TYPE_P (complete_type (type)))
3996 error ("aggregate %q#D has incomplete type and cannot be defined",
3997 decl);
3998 /* Change the type so that assemble_variable will give
3999 DECL an rtl we can live with: (mem (const_int 0)). */
4000 type = TREE_TYPE (decl) = error_mark_node;
4002 else
4004 /* If any base type in the hierarchy of TYPE needs a constructor,
4005 then we set initialized to 1. This way any nodes which are
4006 created for the purposes of initializing this aggregate
4007 will live as long as it does. This is necessary for global
4008 aggregates which do not have their initializers processed until
4009 the end of the file. */
4010 initialized = TYPE_NEEDS_CONSTRUCTING (type);
4014 /* Create a new scope to hold this declaration if necessary.
4015 Whether or not a new scope is necessary cannot be determined
4016 until after the type has been completed; if the type is a
4017 specialization of a class template it is not until after
4018 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4019 will be set correctly. */
4020 maybe_push_cleanup_level (type);
4023 /* Handle initialization of references. DECL, TYPE, and INIT have the
4024 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
4025 but will be set to a new CLEANUP_STMT if a temporary is created
4026 that must be destroyed subsequently.
4028 Returns an initializer expression to use to initialize DECL, or
4029 NULL if the initialization can be performed statically.
4031 Quotes on semantics can be found in ARM 8.4.3. */
4033 static tree
4034 grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
4036 tree tmp;
4038 if (init == NULL_TREE)
4040 if ((DECL_LANG_SPECIFIC (decl) == 0
4041 || DECL_IN_AGGR_P (decl) == 0)
4042 && ! DECL_THIS_EXTERN (decl))
4043 error ("%qD declared as reference but not initialized", decl);
4044 return NULL_TREE;
4047 if (TREE_CODE (init) == CONSTRUCTOR)
4049 error ("ISO C++ forbids use of initializer list to "
4050 "initialize reference %qD", decl);
4051 return NULL_TREE;
4054 if (TREE_CODE (init) == TREE_LIST)
4055 init = build_x_compound_expr_from_list (init, "initializer");
4057 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
4058 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
4059 /* Note: default conversion is only called in very special cases. */
4060 init = decay_conversion (init);
4062 /* Convert INIT to the reference type TYPE. This may involve the
4063 creation of a temporary, whose lifetime must be the same as that
4064 of the reference. If so, a DECL_EXPR for the temporary will be
4065 added just after the DECL_EXPR for DECL. That's why we don't set
4066 DECL_INITIAL for local references (instead assigning to them
4067 explicitly); we need to allow the temporary to be initialized
4068 first. */
4069 tmp = initialize_reference (type, init, decl, cleanup);
4071 if (tmp == error_mark_node)
4072 return NULL_TREE;
4073 else if (tmp == NULL_TREE)
4075 error ("cannot initialize %qT from %qT", type, TREE_TYPE (init));
4076 return NULL_TREE;
4079 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
4080 return tmp;
4082 DECL_INITIAL (decl) = tmp;
4084 return NULL_TREE;
4087 /* When parsing `int a[] = {1, 2};' we don't know the size of the
4088 array until we finish parsing the initializer. If that's the
4089 situation we're in, update DECL accordingly. */
4091 static void
4092 maybe_deduce_size_from_array_init (tree decl, tree init)
4094 tree type = TREE_TYPE (decl);
4096 if (TREE_CODE (type) == ARRAY_TYPE
4097 && TYPE_DOMAIN (type) == NULL_TREE
4098 && TREE_CODE (decl) != TYPE_DECL)
4100 /* do_default is really a C-ism to deal with tentative definitions.
4101 But let's leave it here to ease the eventual merge. */
4102 int do_default = !DECL_EXTERNAL (decl);
4103 tree initializer = init ? init : DECL_INITIAL (decl);
4104 int failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
4105 do_default);
4107 if (failure == 1)
4109 error ("initializer fails to determine size of %qD", decl);
4110 TREE_TYPE (decl) = error_mark_node;
4112 else if (failure == 2)
4114 if (do_default)
4116 error ("array size missing in %qD", decl);
4117 TREE_TYPE (decl) = error_mark_node;
4119 /* If a `static' var's size isn't known, make it extern as
4120 well as static, so it does not get allocated. If it's not
4121 `static', then don't mark it extern; finish_incomplete_decl
4122 will give it a default size and it will get allocated. */
4123 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4124 DECL_EXTERNAL (decl) = 1;
4126 else if (failure == 3)
4128 error ("zero-size array %qD", decl);
4129 TREE_TYPE (decl) = error_mark_node;
4132 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
4134 layout_decl (decl, 0);
4138 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
4139 any appropriate error messages regarding the layout. */
4141 static void
4142 layout_var_decl (tree decl)
4144 tree type;
4146 type = TREE_TYPE (decl);
4147 if (type == error_mark_node)
4148 return;
4150 /* If we haven't already layed out this declaration, do so now.
4151 Note that we must not call complete type for an external object
4152 because it's type might involve templates that we are not
4153 supposed to instantiate yet. (And it's perfectly valid to say
4154 `extern X x' for some incomplete type `X'.) */
4155 if (!DECL_EXTERNAL (decl))
4156 complete_type (type);
4157 if (!DECL_SIZE (decl)
4158 && TREE_TYPE (decl) != error_mark_node
4159 && (COMPLETE_TYPE_P (type)
4160 || (TREE_CODE (type) == ARRAY_TYPE
4161 && !TYPE_DOMAIN (type)
4162 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
4163 layout_decl (decl, 0);
4165 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
4167 /* An automatic variable with an incomplete type: that is an error.
4168 Don't talk about array types here, since we took care of that
4169 message in grokdeclarator. */
4170 error ("storage size of %qD isn't known", decl);
4171 TREE_TYPE (decl) = error_mark_node;
4173 #if 0
4174 /* Keep this code around in case we later want to control debug info
4175 based on whether a type is "used". (jason 1999-11-11) */
4177 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
4178 /* Let debugger know it should output info for this type. */
4179 note_debug_info_needed (ttype);
4181 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
4182 note_debug_info_needed (DECL_CONTEXT (decl));
4183 #endif
4185 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4186 && DECL_SIZE (decl) != NULL_TREE
4187 && ! TREE_CONSTANT (DECL_SIZE (decl)))
4189 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4190 constant_expression_warning (DECL_SIZE (decl));
4191 else
4192 error ("storage size of %qD isn't constant", decl);
4196 /* If a local static variable is declared in an inline function, or if
4197 we have a weak definition, we must endeavor to create only one
4198 instance of the variable at link-time. */
4200 static void
4201 maybe_commonize_var (tree decl)
4203 /* Static data in a function with comdat linkage also has comdat
4204 linkage. */
4205 if (TREE_STATIC (decl)
4206 /* Don't mess with __FUNCTION__. */
4207 && ! DECL_ARTIFICIAL (decl)
4208 && DECL_FUNCTION_SCOPE_P (decl)
4209 /* Unfortunately, import_export_decl has not always been called
4210 before the function is processed, so we cannot simply check
4211 DECL_COMDAT. */
4212 && (DECL_COMDAT (DECL_CONTEXT (decl))
4213 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
4214 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
4215 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
4217 if (flag_weak)
4219 /* With weak symbols, we simply make the variable COMDAT;
4220 that will cause copies in multiple translations units to
4221 be merged. */
4222 comdat_linkage (decl);
4224 else
4226 if (DECL_INITIAL (decl) == NULL_TREE
4227 || DECL_INITIAL (decl) == error_mark_node)
4229 /* Without weak symbols, we can use COMMON to merge
4230 uninitialized variables. */
4231 TREE_PUBLIC (decl) = 1;
4232 DECL_COMMON (decl) = 1;
4234 else
4236 /* While for initialized variables, we must use internal
4237 linkage -- which means that multiple copies will not
4238 be merged. */
4239 TREE_PUBLIC (decl) = 0;
4240 DECL_COMMON (decl) = 0;
4241 warning (0, "sorry: semantics of inline function static "
4242 "data %q+#D are wrong (you'll wind up "
4243 "with multiple copies)", decl);
4244 warning (0, "%J you can work around this by removing "
4245 "the initializer",
4246 decl);
4250 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
4251 /* Set it up again; we might have set DECL_INITIAL since the last
4252 time. */
4253 comdat_linkage (decl);
4256 /* Issue an error message if DECL is an uninitialized const variable. */
4258 static void
4259 check_for_uninitialized_const_var (tree decl)
4261 tree type = TREE_TYPE (decl);
4263 /* ``Unless explicitly declared extern, a const object does not have
4264 external linkage and must be initialized. ($8.4; $12.1)'' ARM
4265 7.1.6 */
4266 if (TREE_CODE (decl) == VAR_DECL
4267 && TREE_CODE (type) != REFERENCE_TYPE
4268 && CP_TYPE_CONST_P (type)
4269 && !TYPE_NEEDS_CONSTRUCTING (type)
4270 && !DECL_INITIAL (decl))
4271 error ("uninitialized const %qD", decl);
4275 /* Structure holding the current initializer being processed by reshape_init.
4276 CUR is a pointer to the current element being processed, END is a pointer
4277 after the last element present in the initializer. */
4278 typedef struct reshape_iterator_t
4280 constructor_elt *cur;
4281 constructor_elt *end;
4282 } reshape_iter;
4284 static tree reshape_init_r (tree, reshape_iter *, bool);
4286 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
4287 returned is the next FIELD_DECL (possibly FIELD itself) that can be
4288 initialized. If there are no more such fields, the return value
4289 will be NULL. */
4291 static tree
4292 next_initializable_field (tree field)
4294 while (field
4295 && (TREE_CODE (field) != FIELD_DECL
4296 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
4297 || DECL_ARTIFICIAL (field)))
4298 field = TREE_CHAIN (field);
4300 return field;
4303 /* Subroutine of reshape_init_array and reshape_init_vector, which does
4304 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
4305 INTEGER_CST representing the size of the array minus one (the maximum index),
4306 or NULL_TREE if the array was declared without specifying the size. D is
4307 the iterator within the constructor. */
4309 static tree
4310 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d)
4312 tree new_init;
4313 bool sized_array_p = (max_index != NULL_TREE);
4314 unsigned HOST_WIDE_INT max_index_cst = 0;
4315 unsigned HOST_WIDE_INT index;
4317 /* The initializer for an array is always a CONSTRUCTOR. */
4318 new_init = build_constructor (NULL_TREE, NULL);
4320 if (sized_array_p)
4322 /* Minus 1 is used for zero sized arrays. */
4323 if (integer_all_onesp (max_index))
4324 return new_init;
4326 if (host_integerp (max_index, 1))
4327 max_index_cst = tree_low_cst (max_index, 1);
4328 /* sizetype is sign extended, not zero extended. */
4329 else
4330 max_index_cst = tree_low_cst (fold_convert (size_type_node, max_index),
4334 /* Loop until there are no more initializers. */
4335 for (index = 0;
4336 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
4337 ++index)
4339 tree elt_init;
4341 if (d->cur->index)
4343 /* Handle array designated initializers (GNU extension). */
4344 if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
4346 error ("name %qD used in a GNU-style designated "
4347 "initializer for an array", d->cur->index);
4349 else
4350 gcc_unreachable ();
4353 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false);
4354 if (elt_init == error_mark_node)
4355 return error_mark_node;
4356 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), NULL_TREE, elt_init);
4359 return new_init;
4362 /* Subroutine of reshape_init_r, processes the initializers for arrays.
4363 Parameters are the same of reshape_init_r. */
4365 static tree
4366 reshape_init_array (tree type, reshape_iter *d)
4368 tree max_index = NULL_TREE;
4370 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
4372 if (TYPE_DOMAIN (type))
4373 max_index = array_type_nelts (type);
4375 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4378 /* Subroutine of reshape_init_r, processes the initializers for vectors.
4379 Parameters are the same of reshape_init_r. */
4381 static tree
4382 reshape_init_vector (tree type, reshape_iter *d)
4384 tree max_index = NULL_TREE;
4385 tree rtype;
4387 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
4389 if (COMPOUND_LITERAL_P (d->cur->value))
4391 tree value = d->cur->value;
4392 if (!same_type_p (TREE_TYPE (value), type))
4394 error ("invalid type %qT as initializer for a vector of type %qT",
4395 TREE_TYPE (d->cur->value), type);
4396 value = error_mark_node;
4398 ++d->cur;
4399 return value;
4402 /* For a vector, the representation type is a struct
4403 containing a single member which is an array of the
4404 appropriate size. */
4405 rtype = TYPE_DEBUG_REPRESENTATION_TYPE (type);
4406 if (rtype && TYPE_DOMAIN (TREE_TYPE (TYPE_FIELDS (rtype))))
4407 max_index = array_type_nelts (TREE_TYPE (TYPE_FIELDS (rtype)));
4409 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4412 /* Subroutine of reshape_init_r, processes the initializers for classes
4413 or union. Parameters are the same of reshape_init_r. */
4415 static tree
4416 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p)
4418 tree field;
4419 tree new_init;
4421 gcc_assert (CLASS_TYPE_P (type));
4423 /* The initializer for a class is always a CONSTRUCTOR. */
4424 new_init = build_constructor (NULL_TREE, NULL);
4425 field = next_initializable_field (TYPE_FIELDS (type));
4427 if (!field)
4429 /* [dcl.init.aggr]
4431 An initializer for an aggregate member that is an
4432 empty class shall have the form of an empty
4433 initializer-list {}. */
4434 if (!first_initializer_p)
4436 error ("initializer for %qT must be brace-enclosed", type);
4437 return error_mark_node;
4439 return new_init;
4442 /* Loop through the initializable fields, gathering initializers. */
4443 while (d->cur != d->end)
4445 tree field_init;
4447 /* Handle designated initializers, as an extension. */
4448 if (d->cur->index)
4450 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
4452 if (!field || TREE_CODE (field) != FIELD_DECL)
4454 error ("%qT has no non-static data member named %qD", type,
4455 d->cur->index);
4456 return error_mark_node;
4460 /* If we processed all the member of the class, we are done. */
4461 if (!field)
4462 break;
4464 field_init = reshape_init_r (TREE_TYPE (field), d,
4465 /*first_initializer_p=*/false);
4466 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
4468 /* [dcl.init.aggr]
4470 When a union is initialized with a brace-enclosed
4471 initializer, the braces shall only contain an
4472 initializer for the first member of the union. */
4473 if (TREE_CODE (type) == UNION_TYPE)
4474 break;
4476 field = next_initializable_field (TREE_CHAIN (field));
4479 return new_init;
4482 /* Subroutine of reshape_init, which processes a single initializer (part of
4483 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
4484 iterator within the CONSTRUCTOR which points to the initializer to process.
4485 FIRST_INITIALIZER_P is true if this is the first initializer of the
4486 CONSTRUCTOR node. */
4488 static tree
4489 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p)
4491 tree init = d->cur->value;
4493 /* A non-aggregate type is always initialized with a single
4494 initializer. */
4495 if (!CP_AGGREGATE_TYPE_P (type))
4497 /* It is invalid to initialize a non-aggregate type with a
4498 brace-enclosed initializer.
4499 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
4500 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
4501 a CONSTRUCTOR (with a record type). */
4502 if (TREE_CODE (init) == CONSTRUCTOR
4503 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
4505 error ("braces around scalar initializer for type %qT", type);
4506 init = error_mark_node;
4509 d->cur++;
4510 return init;
4513 /* [dcl.init.aggr]
4515 All implicit type conversions (clause _conv_) are considered when
4516 initializing the aggregate member with an initializer from an
4517 initializer-list. If the initializer can initialize a member,
4518 the member is initialized. Otherwise, if the member is itself a
4519 non-empty subaggregate, brace elision is assumed and the
4520 initializer is considered for the initialization of the first
4521 member of the subaggregate. */
4522 if (TREE_CODE (init) != CONSTRUCTOR
4523 && can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL))
4525 d->cur++;
4526 return init;
4529 /* [dcl.init.string]
4531 A char array (whether plain char, signed char, or unsigned char)
4532 can be initialized by a string-literal (optionally enclosed in
4533 braces); a wchar_t array can be initialized by a wide
4534 string-literal (optionally enclosed in braces). */
4535 if (TREE_CODE (type) == ARRAY_TYPE
4536 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
4538 tree str_init = init;
4540 /* Strip one level of braces if and only if they enclose a single
4541 element (as allowed by [dcl.init.string]). */
4542 if (!first_initializer_p
4543 && TREE_CODE (str_init) == CONSTRUCTOR
4544 && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (str_init)) == 1)
4546 str_init = VEC_index (constructor_elt,
4547 CONSTRUCTOR_ELTS (str_init), 0)->value;
4550 /* If it's a string literal, then it's the initializer for the array
4551 as a whole. Otherwise, continue with normal initialization for
4552 array types (one value per array element). */
4553 if (TREE_CODE (str_init) == STRING_CST)
4555 d->cur++;
4556 return str_init;
4560 /* The following cases are about aggregates. If we are not within a full
4561 initializer already, and there is not a CONSTRUCTOR, it means that there
4562 is a missing set of braces (that is, we are processing the case for
4563 which reshape_init exists). */
4564 if (!first_initializer_p)
4566 if (TREE_CODE (init) == CONSTRUCTOR)
4568 /* For a nested compound literal, there is no need to reshape since
4569 brace elision is not allowed. Even if we decided to allow it,
4570 we should add a call to reshape_init in finish_compound_literal,
4571 before calling digest_init, so changing this code would still
4572 not be necessary. */
4573 if (!COMPOUND_LITERAL_P (init))
4575 ++d->cur;
4576 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
4577 return reshape_init (type, init);
4579 else
4580 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
4583 warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
4584 type);
4587 /* Dispatch to specialized routines. */
4588 if (CLASS_TYPE_P (type))
4589 return reshape_init_class (type, d, first_initializer_p);
4590 else if (TREE_CODE (type) == ARRAY_TYPE)
4591 return reshape_init_array (type, d);
4592 else if (TREE_CODE (type) == VECTOR_TYPE)
4593 return reshape_init_vector (type, d);
4594 else
4595 gcc_unreachable();
4598 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
4599 brace-enclosed aggregate initializer.
4601 INIT is the CONSTRUCTOR containing the list of initializers describing
4602 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
4603 It may not presently match the shape of the TYPE; for example:
4605 struct S { int a; int b; };
4606 struct S a[] = { 1, 2, 3, 4 };
4608 Here INIT will hold a VEC of four elements, rather than a
4609 VEC of two elements, each itself a VEC of two elements. This
4610 routine transforms INIT from the former form into the latter. The
4611 revised CONSTRUCTOR node is returned. */
4613 tree
4614 reshape_init (tree type, tree init)
4616 VEC(constructor_elt, gc) *v;
4617 reshape_iter d;
4618 tree new_init;
4620 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
4622 v = CONSTRUCTOR_ELTS (init);
4624 /* An empty constructor does not need reshaping, and it is always a valid
4625 initializer. */
4626 if (VEC_empty (constructor_elt, v))
4627 return init;
4629 /* Recurse on this CONSTRUCTOR. */
4630 d.cur = VEC_index (constructor_elt, v, 0);
4631 d.end = d.cur + VEC_length (constructor_elt, v);
4633 new_init = reshape_init_r (type, &d, true);
4634 if (new_init == error_mark_node)
4635 return error_mark_node;
4637 /* Make sure all the element of the constructor were used. Otherwise,
4638 issue an error about exceeding initializers. */
4639 if (d.cur != d.end)
4640 error ("too many initializers for %qT", type);
4642 return new_init;
4645 /* Verify INIT (the initializer for DECL), and record the
4646 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
4647 grok_reference_init.
4649 If the return value is non-NULL, it is an expression that must be
4650 evaluated dynamically to initialize DECL. */
4652 static tree
4653 check_initializer (tree decl, tree init, int flags, tree *cleanup)
4655 tree type = TREE_TYPE (decl);
4656 tree init_code = NULL;
4658 /* Things that are going to be initialized need to have complete
4659 type. */
4660 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
4662 if (type == error_mark_node)
4663 /* We will have already complained. */
4664 return NULL_TREE;
4666 if (TREE_CODE (type) == ARRAY_TYPE)
4668 tree element_type = TREE_TYPE (type);
4670 /* The array type itself need not be complete, because the
4671 initializer may tell us how many elements are in the array.
4672 But, the elements of the array must be complete. */
4673 if (!COMPLETE_TYPE_P (complete_type (element_type)))
4675 error ("elements of array %q#D have incomplete type", decl);
4676 return NULL_TREE;
4678 /* It is not valid to initialize an a VLA. */
4679 if (init
4680 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
4681 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
4683 error ("variable-sized object %qD may not be initialized", decl);
4684 return NULL_TREE;
4687 else if (!COMPLETE_TYPE_P (type))
4689 error ("%qD has incomplete type", decl);
4690 TREE_TYPE (decl) = error_mark_node;
4691 return NULL_TREE;
4693 else
4694 /* There is no way to make a variable-sized class type in GNU C++. */
4695 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
4697 if (!CP_AGGREGATE_TYPE_P (type)
4698 && init && BRACE_ENCLOSED_INITIALIZER_P (init)
4699 && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)) != 1)
4701 error ("scalar object %qD requires one element in initializer", decl);
4702 TREE_TYPE (decl) = error_mark_node;
4703 return NULL_TREE;
4706 if (TREE_CODE (decl) == CONST_DECL)
4708 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
4710 DECL_INITIAL (decl) = init;
4712 gcc_assert (init != NULL_TREE);
4713 init = NULL_TREE;
4715 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
4716 init = grok_reference_init (decl, type, init, cleanup);
4717 else if (init)
4719 /* Do not reshape constructors of vectors (they don't need to be
4720 reshaped. */
4721 if (TREE_CODE (init) == CONSTRUCTOR
4722 && !COMPOUND_LITERAL_P (init)
4723 && !TREE_TYPE (init)) /* ptrmemfunc */
4725 init = reshape_init (type, init);
4727 if ((*targetm.vector_opaque_p) (type))
4729 error ("opaque vector types cannot be initialized");
4730 init = error_mark_node;
4734 /* If DECL has an array type without a specific bound, deduce the
4735 array size from the initializer. */
4736 maybe_deduce_size_from_array_init (decl, init);
4737 type = TREE_TYPE (decl);
4738 if (type == error_mark_node)
4739 return NULL_TREE;
4741 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
4743 if (TREE_CODE (type) == ARRAY_TYPE)
4744 goto initialize_aggr;
4745 else if (TREE_CODE (init) == CONSTRUCTOR)
4747 if (TYPE_NON_AGGREGATE_CLASS (type))
4749 error ("%qD must be initialized by constructor, "
4750 "not by %<{...}%>",
4751 decl);
4752 init = error_mark_node;
4754 else
4755 goto dont_use_constructor;
4757 else
4759 int saved_stmts_are_full_exprs_p;
4761 initialize_aggr:
4762 saved_stmts_are_full_exprs_p = 0;
4763 if (building_stmt_tree ())
4765 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
4766 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4768 init = build_aggr_init (decl, init, flags);
4769 if (building_stmt_tree ())
4770 current_stmt_tree ()->stmts_are_full_exprs_p =
4771 saved_stmts_are_full_exprs_p;
4772 return init;
4775 else
4777 dont_use_constructor:
4778 if (TREE_CODE (init) != TREE_VEC)
4780 init_code = store_init_value (decl, init);
4781 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
4782 && DECL_INITIAL (decl)
4783 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
4784 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
4785 warning (0, "array %qD initialized by parenthesized string literal %qE",
4786 decl, DECL_INITIAL (decl));
4787 init = NULL;
4791 else if (DECL_EXTERNAL (decl))
4793 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
4794 goto initialize_aggr;
4795 else if (IS_AGGR_TYPE (type))
4797 tree core_type = strip_array_types (type);
4799 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
4800 error ("structure %qD with uninitialized const members", decl);
4801 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
4802 error ("structure %qD with uninitialized reference members", decl);
4804 check_for_uninitialized_const_var (decl);
4806 else
4807 check_for_uninitialized_const_var (decl);
4809 if (init && init != error_mark_node)
4810 init_code = build2 (INIT_EXPR, type, decl, init);
4812 return init_code;
4815 /* If DECL is not a local variable, give it RTL. */
4817 static void
4818 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
4820 int toplev = toplevel_bindings_p ();
4821 int defer_p;
4822 const char *filename;
4824 /* Set the DECL_ASSEMBLER_NAME for the object. */
4825 if (asmspec)
4827 /* The `register' keyword, when used together with an
4828 asm-specification, indicates that the variable should be
4829 placed in a particular register. */
4830 if (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
4832 set_user_assembler_name (decl, asmspec);
4833 DECL_HARD_REGISTER (decl) = 1;
4835 else
4837 if (TREE_CODE (decl) == FUNCTION_DECL
4838 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4839 set_builtin_user_assembler_name (decl, asmspec);
4840 set_user_assembler_name (decl, asmspec);
4844 /* Handle non-variables up front. */
4845 if (TREE_CODE (decl) != VAR_DECL)
4847 rest_of_decl_compilation (decl, toplev, at_eof);
4848 return;
4851 /* If we see a class member here, it should be a static data
4852 member. */
4853 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
4855 gcc_assert (TREE_STATIC (decl));
4856 /* An in-class declaration of a static data member should be
4857 external; it is only a declaration, and not a definition. */
4858 if (init == NULL_TREE)
4859 gcc_assert (DECL_EXTERNAL (decl));
4862 /* We don't create any RTL for local variables. */
4863 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
4864 return;
4866 /* We defer emission of local statics until the corresponding
4867 DECL_EXPR is expanded. */
4868 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
4870 /* We try to defer namespace-scope static constants so that they are
4871 not emitted into the object file unnecessarily. */
4872 filename = input_filename;
4873 if (!DECL_VIRTUAL_P (decl)
4874 && TREE_READONLY (decl)
4875 && DECL_INITIAL (decl) != NULL_TREE
4876 && DECL_INITIAL (decl) != error_mark_node
4877 && filename != NULL
4878 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
4879 && toplev
4880 && !TREE_PUBLIC (decl))
4882 /* Fool with the linkage of static consts according to #pragma
4883 interface. */
4884 struct c_fileinfo *finfo = get_fileinfo (filename);
4885 if (!finfo->interface_unknown && !TREE_PUBLIC (decl))
4887 TREE_PUBLIC (decl) = 1;
4888 DECL_EXTERNAL (decl) = finfo->interface_only;
4891 defer_p = 1;
4893 /* Likewise for template instantiations. */
4894 else if (DECL_LANG_SPECIFIC (decl)
4895 && DECL_IMPLICIT_INSTANTIATION (decl))
4896 defer_p = 1;
4898 /* If we're not deferring, go ahead and assemble the variable. */
4899 if (!defer_p)
4900 rest_of_decl_compilation (decl, toplev, at_eof);
4903 /* Generate code to initialize DECL (a local variable). */
4905 static void
4906 initialize_local_var (tree decl, tree init)
4908 tree type = TREE_TYPE (decl);
4909 tree cleanup;
4911 gcc_assert (TREE_CODE (decl) == VAR_DECL
4912 || TREE_CODE (decl) == RESULT_DECL);
4913 gcc_assert (!TREE_STATIC (decl));
4915 if (DECL_SIZE (decl) == NULL_TREE)
4917 /* If we used it already as memory, it must stay in memory. */
4918 DECL_INITIAL (decl) = NULL_TREE;
4919 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4922 if (DECL_SIZE (decl) && type != error_mark_node)
4924 int already_used;
4926 /* Compute and store the initial value. */
4927 already_used = TREE_USED (decl) || TREE_USED (type);
4929 /* Perform the initialization. */
4930 if (init)
4932 int saved_stmts_are_full_exprs_p;
4934 gcc_assert (building_stmt_tree ());
4935 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
4936 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4937 finish_expr_stmt (init);
4938 current_stmt_tree ()->stmts_are_full_exprs_p =
4939 saved_stmts_are_full_exprs_p;
4942 /* Set this to 0 so we can tell whether an aggregate which was
4943 initialized was ever used. Don't do this if it has a
4944 destructor, so we don't complain about the 'resource
4945 allocation is initialization' idiom. Now set
4946 attribute((unused)) on types so decls of that type will be
4947 marked used. (see TREE_USED, above.) */
4948 if (TYPE_NEEDS_CONSTRUCTING (type)
4949 && ! already_used
4950 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
4951 && DECL_NAME (decl))
4952 TREE_USED (decl) = 0;
4953 else if (already_used)
4954 TREE_USED (decl) = 1;
4957 /* Generate a cleanup, if necessary. */
4958 cleanup = cxx_maybe_build_cleanup (decl);
4959 if (DECL_SIZE (decl) && cleanup)
4960 finish_decl_cleanup (decl, cleanup);
4963 /* DECL is a VAR_DECL for a compiler-generated variable with static
4964 storage duration (like a virtual table) whose initializer is a
4965 compile-time constant. INIT must be either a TREE_LIST of values,
4966 or a CONSTRUCTOR. Initialize the variable and provide it to the
4967 back end. */
4969 void
4970 initialize_artificial_var (tree decl, tree init)
4972 gcc_assert (DECL_ARTIFICIAL (decl));
4973 if (TREE_CODE (init) == TREE_LIST)
4974 init = build_constructor_from_list (NULL_TREE, init);
4975 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
4976 DECL_INITIAL (decl) = init;
4977 DECL_INITIALIZED_P (decl) = 1;
4978 determine_visibility (decl);
4979 layout_var_decl (decl);
4980 maybe_commonize_var (decl);
4981 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
4984 /* Finish processing of a declaration;
4985 install its line number and initial value.
4986 If the length of an array type is not known before,
4987 it must be determined now, from the initial value, or it is an error.
4989 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
4990 true, then INIT is an integral constant expression.
4992 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
4993 if the (init) syntax was used. */
4995 void
4996 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
4997 tree asmspec_tree, int flags)
4999 tree type;
5000 tree cleanup;
5001 const char *asmspec = NULL;
5002 int was_readonly = 0;
5003 bool var_definition_p = false;
5004 int saved_processing_template_decl;
5006 if (decl == error_mark_node)
5007 return;
5008 else if (! decl)
5010 if (init)
5011 error ("assignment (not initialization) in declaration");
5012 return;
5015 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
5016 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
5017 gcc_assert (TREE_CODE (decl) != PARM_DECL);
5019 type = TREE_TYPE (decl);
5020 if (type == error_mark_node)
5021 return;
5023 /* Assume no cleanup is required. */
5024 cleanup = NULL_TREE;
5025 saved_processing_template_decl = processing_template_decl;
5027 /* If a name was specified, get the string. */
5028 if (global_scope_p (current_binding_level))
5029 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5030 if (asmspec_tree && asmspec_tree != error_mark_node)
5031 asmspec = TREE_STRING_POINTER (asmspec_tree);
5033 if (current_class_type
5034 && CP_DECL_CONTEXT (decl) == current_class_type
5035 && TYPE_BEING_DEFINED (current_class_type)
5036 && (DECL_INITIAL (decl) || init))
5037 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
5039 if (processing_template_decl)
5041 bool type_dependent_p;
5043 /* Add this declaration to the statement-tree. */
5044 if (at_function_scope_p ())
5045 add_decl_expr (decl);
5047 type_dependent_p = dependent_type_p (type);
5049 if (init && init_const_expr_p)
5051 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5052 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl))
5053 TREE_CONSTANT (decl) = 1;
5056 if (!init
5057 || !DECL_CLASS_SCOPE_P (decl)
5058 || !DECL_INTEGRAL_CONSTANT_VAR_P (decl)
5059 || type_dependent_p
5060 || value_dependent_expression_p (init))
5062 if (init)
5063 DECL_INITIAL (decl) = init;
5064 if (TREE_CODE (decl) == VAR_DECL
5065 && !DECL_PRETTY_FUNCTION_P (decl)
5066 && !type_dependent_p)
5067 maybe_deduce_size_from_array_init (decl, init);
5068 goto finish_end;
5071 init = fold_non_dependent_expr (init);
5072 processing_template_decl = 0;
5075 /* Take care of TYPE_DECLs up front. */
5076 if (TREE_CODE (decl) == TYPE_DECL)
5078 if (type != error_mark_node
5079 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
5081 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
5082 warning (0, "shadowing previous type declaration of %q#D", decl);
5083 set_identifier_type_value (DECL_NAME (decl), decl);
5086 /* If we have installed this as the canonical typedef for this
5087 type, and that type has not been defined yet, delay emitting
5088 the debug information for it, as we will emit it later. */
5089 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
5090 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
5091 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5093 rest_of_decl_compilation (decl, DECL_CONTEXT (decl) == NULL_TREE,
5094 at_eof);
5095 goto finish_end;
5098 /* A reference will be modified here, as it is initialized. */
5099 if (! DECL_EXTERNAL (decl)
5100 && TREE_READONLY (decl)
5101 && TREE_CODE (type) == REFERENCE_TYPE)
5103 was_readonly = 1;
5104 TREE_READONLY (decl) = 0;
5107 if (TREE_CODE (decl) == VAR_DECL)
5109 /* Only PODs can have thread-local storage. Other types may require
5110 various kinds of non-trivial initialization. */
5111 if (DECL_THREAD_LOCAL_P (decl) && !pod_type_p (TREE_TYPE (decl)))
5112 error ("%qD cannot be thread-local because it has non-POD type %qT",
5113 decl, TREE_TYPE (decl));
5114 /* If this is a local variable that will need a mangled name,
5115 register it now. We must do this before processing the
5116 initializer for the variable, since the initialization might
5117 require a guard variable, and since the mangled name of the
5118 guard variable will depend on the mangled name of this
5119 variable. */
5120 if (!processing_template_decl
5121 && DECL_FUNCTION_SCOPE_P (decl)
5122 && TREE_STATIC (decl)
5123 && !DECL_ARTIFICIAL (decl))
5124 push_local_name (decl);
5125 /* Convert the initializer to the type of DECL, if we have not
5126 already initialized DECL. */
5127 if (!DECL_INITIALIZED_P (decl)
5128 /* If !DECL_EXTERNAL then DECL is being defined. In the
5129 case of a static data member initialized inside the
5130 class-specifier, there can be an initializer even if DECL
5131 is *not* defined. */
5132 && (!DECL_EXTERNAL (decl) || init))
5134 if (init)
5136 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
5137 if (init_const_expr_p)
5139 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5140 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl))
5141 TREE_CONSTANT (decl) = 1;
5144 init = check_initializer (decl, init, flags, &cleanup);
5145 /* Thread-local storage cannot be dynamically initialized. */
5146 if (DECL_THREAD_LOCAL_P (decl) && init)
5148 error ("%qD is thread-local and so cannot be dynamically "
5149 "initialized", decl);
5150 init = NULL_TREE;
5153 /* Check that the initializer for a static data member was a
5154 constant. Although we check in the parser that the
5155 initializer is an integral constant expression, we do not
5156 simplify division-by-zero at the point at which it
5157 occurs. Therefore, in:
5159 struct S { static const int i = 7 / 0; };
5161 we issue an error at this point. It would
5162 probably be better to forbid division by zero in
5163 integral constant expressions. */
5164 if (DECL_EXTERNAL (decl) && init)
5166 error ("%qD cannot be initialized by a non-constant expression"
5167 " when being declared", decl);
5168 DECL_INITIALIZED_IN_CLASS_P (decl) = 0;
5169 init = NULL_TREE;
5172 /* Handle:
5174 [dcl.init]
5176 The memory occupied by any object of static storage
5177 duration is zero-initialized at program startup before
5178 any other initialization takes place.
5180 We cannot create an appropriate initializer until after
5181 the type of DECL is finalized. If DECL_INITIAL is set,
5182 then the DECL is statically initialized, and any
5183 necessary zero-initialization has already been performed. */
5184 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
5185 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
5186 /*nelts=*/NULL_TREE,
5187 /*static_storage_p=*/true);
5188 /* Remember that the initialization for this variable has
5189 taken place. */
5190 DECL_INITIALIZED_P (decl) = 1;
5191 /* This declaration is the definition of this variable,
5192 unless we are initializing a static data member within
5193 the class specifier. */
5194 if (!DECL_EXTERNAL (decl))
5195 var_definition_p = true;
5197 /* If the variable has an array type, lay out the type, even if
5198 there is no initializer. It is valid to index through the
5199 array, and we must get TYPE_ALIGN set correctly on the array
5200 type. */
5201 else if (TREE_CODE (type) == ARRAY_TYPE)
5202 layout_type (type);
5205 /* Add this declaration to the statement-tree. This needs to happen
5206 after the call to check_initializer so that the DECL_EXPR for a
5207 reference temp is added before the DECL_EXPR for the reference itself. */
5208 if (at_function_scope_p ())
5209 add_decl_expr (decl);
5211 /* Let the middle end know about variables and functions -- but not
5212 static data members in uninstantiated class templates. */
5213 if (!saved_processing_template_decl
5214 && (TREE_CODE (decl) == VAR_DECL
5215 || TREE_CODE (decl) == FUNCTION_DECL))
5217 if (TREE_CODE (decl) == VAR_DECL)
5219 layout_var_decl (decl);
5220 maybe_commonize_var (decl);
5221 if (DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl)
5222 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5224 /* This is a const variable with implicit 'static'. Set
5225 DECL_THIS_STATIC so we can tell it from variables that are
5226 !TREE_PUBLIC because of the anonymous namespace. */
5227 DECL_THIS_STATIC (decl) = 1;
5231 make_rtl_for_nonlocal_decl (decl, init, asmspec);
5233 /* Check for abstractness of the type. Notice that there is no
5234 need to strip array types here since the check for those types
5235 is already done within create_array_type_for_decl. */
5236 if (TREE_CODE (type) == FUNCTION_TYPE
5237 || TREE_CODE (type) == METHOD_TYPE)
5238 abstract_virtuals_error (decl, TREE_TYPE (type));
5239 else
5240 abstract_virtuals_error (decl, type);
5242 /* This needs to happen after the linkage is set. */
5243 determine_visibility (decl);
5245 if (TREE_CODE (decl) == FUNCTION_DECL
5246 || TREE_TYPE (decl) == error_mark_node)
5247 /* No initialization required. */
5249 else if (DECL_EXTERNAL (decl)
5250 && ! (DECL_LANG_SPECIFIC (decl)
5251 && DECL_NOT_REALLY_EXTERN (decl)))
5253 if (init)
5254 DECL_INITIAL (decl) = init;
5256 else
5258 /* A variable definition. */
5259 if (DECL_FUNCTION_SCOPE_P (decl))
5261 /* Initialize the local variable. */
5262 if (processing_template_decl)
5263 DECL_INITIAL (decl) = init;
5264 else if (!TREE_STATIC (decl))
5265 initialize_local_var (decl, init);
5268 /* If a variable is defined, and then a subsequent
5269 definition with external linkage is encountered, we will
5270 get here twice for the same variable. We want to avoid
5271 calling expand_static_init more than once. For variables
5272 that are not static data members, we can call
5273 expand_static_init only when we actually process the
5274 initializer. It is not legal to redeclare a static data
5275 member, so this issue does not arise in that case. */
5276 if (var_definition_p && TREE_STATIC (decl))
5277 expand_static_init (decl, init);
5281 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
5282 reference, insert it in the statement-tree now. */
5283 if (cleanup)
5284 push_cleanup (decl, cleanup, false);
5286 finish_end:
5287 processing_template_decl = saved_processing_template_decl;
5289 if (was_readonly)
5290 TREE_READONLY (decl) = 1;
5292 /* If this was marked 'used', be sure it will be output. */
5293 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
5294 mark_decl_referenced (decl);
5297 /* This is here for a midend callback from c-common.c. */
5299 void
5300 finish_decl (tree decl, tree init, tree asmspec_tree)
5302 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, asmspec_tree, 0);
5305 /* Returns a declaration for a VAR_DECL as if:
5307 extern "C" TYPE NAME;
5309 had been seen. Used to create compiler-generated global
5310 variables. */
5312 static tree
5313 declare_global_var (tree name, tree type)
5315 tree decl;
5317 push_to_top_level ();
5318 decl = build_decl (VAR_DECL, name, type);
5319 TREE_PUBLIC (decl) = 1;
5320 DECL_EXTERNAL (decl) = 1;
5321 DECL_ARTIFICIAL (decl) = 1;
5322 /* If the user has explicitly declared this variable (perhaps
5323 because the code we are compiling is part of a low-level runtime
5324 library), then it is possible that our declaration will be merged
5325 with theirs by pushdecl. */
5326 decl = pushdecl (decl);
5327 finish_decl (decl, NULL_TREE, NULL_TREE);
5328 pop_from_top_level ();
5330 return decl;
5333 /* Returns a pointer to the `atexit' function. Note that if
5334 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
5335 `__cxa_atexit' function specified in the IA64 C++ ABI. */
5337 static tree
5338 get_atexit_node (void)
5340 tree atexit_fndecl;
5341 tree arg_types;
5342 tree fn_type;
5343 tree fn_ptr_type;
5344 const char *name;
5345 bool use_aeabi_atexit;
5347 if (atexit_node)
5348 return atexit_node;
5350 if (flag_use_cxa_atexit)
5352 /* The declaration for `__cxa_atexit' is:
5354 int __cxa_atexit (void (*)(void *), void *, void *)
5356 We build up the argument types and then then function type
5357 itself. */
5359 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
5360 /* First, build the pointer-to-function type for the first
5361 argument. */
5362 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
5363 fn_type = build_function_type (void_type_node, arg_types);
5364 fn_ptr_type = build_pointer_type (fn_type);
5365 /* Then, build the rest of the argument types. */
5366 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
5367 if (use_aeabi_atexit)
5369 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
5370 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
5372 else
5374 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
5375 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
5377 /* And the final __cxa_atexit type. */
5378 fn_type = build_function_type (integer_type_node, arg_types);
5379 fn_ptr_type = build_pointer_type (fn_type);
5380 if (use_aeabi_atexit)
5381 name = "__aeabi_atexit";
5382 else
5383 name = "__cxa_atexit";
5385 else
5387 /* The declaration for `atexit' is:
5389 int atexit (void (*)());
5391 We build up the argument types and then then function type
5392 itself. */
5393 fn_type = build_function_type (void_type_node, void_list_node);
5394 fn_ptr_type = build_pointer_type (fn_type);
5395 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
5396 /* Build the final atexit type. */
5397 fn_type = build_function_type (integer_type_node, arg_types);
5398 name = "atexit";
5401 /* Now, build the function declaration. */
5402 push_lang_context (lang_name_c);
5403 atexit_fndecl = build_library_fn_ptr (name, fn_type);
5404 mark_used (atexit_fndecl);
5405 pop_lang_context ();
5406 atexit_node = decay_conversion (atexit_fndecl);
5408 return atexit_node;
5411 /* Returns the __dso_handle VAR_DECL. */
5413 static tree
5414 get_dso_handle_node (void)
5416 if (dso_handle_node)
5417 return dso_handle_node;
5419 /* Declare the variable. */
5420 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
5421 ptr_type_node);
5423 return dso_handle_node;
5426 /* Begin a new function with internal linkage whose job will be simply
5427 to destroy some particular variable. */
5429 static GTY(()) int start_cleanup_cnt;
5431 static tree
5432 start_cleanup_fn (void)
5434 char name[32];
5435 tree parmtypes;
5436 tree fntype;
5437 tree fndecl;
5439 push_to_top_level ();
5441 /* No need to mangle this. */
5442 push_lang_context (lang_name_c);
5444 /* Build the parameter-types. */
5445 parmtypes = void_list_node;
5446 /* Functions passed to __cxa_atexit take an additional parameter.
5447 We'll just ignore it. After we implement the new calling
5448 convention for destructors, we can eliminate the use of
5449 additional cleanup functions entirely in the -fnew-abi case. */
5450 if (flag_use_cxa_atexit)
5451 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
5452 /* Build the function type itself. */
5453 fntype = build_function_type (void_type_node, parmtypes);
5454 /* Build the name of the function. */
5455 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
5456 /* Build the function declaration. */
5457 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
5458 /* It's a function with internal linkage, generated by the
5459 compiler. */
5460 TREE_PUBLIC (fndecl) = 0;
5461 DECL_ARTIFICIAL (fndecl) = 1;
5462 /* Make the function `inline' so that it is only emitted if it is
5463 actually needed. It is unlikely that it will be inlined, since
5464 it is only called via a function pointer, but we avoid unnecessary
5465 emissions this way. */
5466 DECL_INLINE (fndecl) = 1;
5467 DECL_DECLARED_INLINE_P (fndecl) = 1;
5468 DECL_INTERFACE_KNOWN (fndecl) = 1;
5469 /* Build the parameter. */
5470 if (flag_use_cxa_atexit)
5472 tree parmdecl;
5474 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
5475 DECL_CONTEXT (parmdecl) = fndecl;
5476 TREE_USED (parmdecl) = 1;
5477 DECL_ARGUMENTS (fndecl) = parmdecl;
5480 pushdecl (fndecl);
5481 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
5483 pop_lang_context ();
5485 return current_function_decl;
5488 /* Finish the cleanup function begun by start_cleanup_fn. */
5490 static void
5491 end_cleanup_fn (void)
5493 expand_or_defer_fn (finish_function (0));
5495 pop_from_top_level ();
5498 /* Generate code to handle the destruction of DECL, an object with
5499 static storage duration. */
5501 tree
5502 register_dtor_fn (tree decl)
5504 tree cleanup;
5505 tree compound_stmt;
5506 tree args;
5507 tree fcall;
5509 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5510 return void_zero_node;
5512 /* Call build_cleanup before we enter the anonymous function so that
5513 any access checks will be done relative to the current scope,
5514 rather than the scope of the anonymous function. */
5515 build_cleanup (decl);
5517 /* Now start the function. */
5518 cleanup = start_cleanup_fn ();
5520 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
5521 to the original function, rather than the anonymous one. That
5522 will make the back-end think that nested functions are in use,
5523 which causes confusion. */
5525 push_deferring_access_checks (dk_no_check);
5526 fcall = build_cleanup (decl);
5527 pop_deferring_access_checks ();
5529 /* Create the body of the anonymous function. */
5530 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
5531 finish_expr_stmt (fcall);
5532 finish_compound_stmt (compound_stmt);
5533 end_cleanup_fn ();
5535 /* Call atexit with the cleanup function. */
5536 cxx_mark_addressable (cleanup);
5537 mark_used (cleanup);
5538 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
5539 if (flag_use_cxa_atexit)
5541 args = tree_cons (NULL_TREE,
5542 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
5543 NULL_TREE);
5544 if (targetm.cxx.use_aeabi_atexit ())
5546 args = tree_cons (NULL_TREE, cleanup, args);
5547 args = tree_cons (NULL_TREE, null_pointer_node, args);
5549 else
5551 args = tree_cons (NULL_TREE, null_pointer_node, args);
5552 args = tree_cons (NULL_TREE, cleanup, args);
5555 else
5556 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
5557 return build_function_call (get_atexit_node (), args);
5560 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
5561 is its initializer. Generate code to handle the construction
5562 and destruction of DECL. */
5564 static void
5565 expand_static_init (tree decl, tree init)
5567 gcc_assert (TREE_CODE (decl) == VAR_DECL);
5568 gcc_assert (TREE_STATIC (decl));
5570 /* Some variables require no initialization. */
5571 if (!init
5572 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
5573 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5574 return;
5576 if (DECL_FUNCTION_SCOPE_P (decl))
5578 /* Emit code to perform this initialization but once. */
5579 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
5580 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
5581 tree guard, guard_addr, guard_addr_list;
5582 tree acquire_fn, release_fn, abort_fn;
5583 tree flag, begin;
5585 /* Emit code to perform this initialization but once. This code
5586 looks like:
5588 static <type> guard;
5589 if (!guard.first_byte) {
5590 if (__cxa_guard_acquire (&guard)) {
5591 bool flag = false;
5592 try {
5593 // Do initialization.
5594 flag = true; __cxa_guard_release (&guard);
5595 // Register variable for destruction at end of program.
5596 } catch {
5597 if (!flag) __cxa_guard_abort (&guard);
5601 Note that the `flag' variable is only set to 1 *after* the
5602 initialization is complete. This ensures that an exception,
5603 thrown during the construction, will cause the variable to
5604 reinitialized when we pass through this code again, as per:
5606 [stmt.dcl]
5608 If the initialization exits by throwing an exception, the
5609 initialization is not complete, so it will be tried again
5610 the next time control enters the declaration.
5612 This process should be thread-safe, too; multiple threads
5613 should not be able to initialize the variable more than
5614 once. */
5616 /* Create the guard variable. */
5617 guard = get_guard (decl);
5619 /* This optimization isn't safe on targets with relaxed memory
5620 consistency. On such targets we force synchronization in
5621 __cxa_guard_acquire. */
5622 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
5624 /* Begin the conditional initialization. */
5625 if_stmt = begin_if_stmt ();
5626 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
5627 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
5630 if (flag_threadsafe_statics)
5632 guard_addr = build_address (guard);
5633 guard_addr_list = build_tree_list (NULL_TREE, guard_addr);
5635 acquire_fn = get_identifier ("__cxa_guard_acquire");
5636 release_fn = get_identifier ("__cxa_guard_release");
5637 abort_fn = get_identifier ("__cxa_guard_abort");
5638 if (!get_global_value_if_present (acquire_fn, &acquire_fn))
5640 tree argtypes = tree_cons (NULL_TREE, TREE_TYPE (guard_addr),
5641 void_list_node);
5642 tree vfntype = build_function_type (void_type_node, argtypes);
5643 acquire_fn = push_library_fn
5644 (acquire_fn, build_function_type (integer_type_node, argtypes));
5645 release_fn = push_library_fn (release_fn, vfntype);
5646 abort_fn = push_library_fn (abort_fn, vfntype);
5648 else
5650 release_fn = identifier_global_value (release_fn);
5651 abort_fn = identifier_global_value (abort_fn);
5654 inner_if_stmt = begin_if_stmt ();
5655 finish_if_stmt_cond (build_call (acquire_fn, guard_addr_list),
5656 inner_if_stmt);
5658 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
5659 begin = get_target_expr (boolean_false_node);
5660 flag = TARGET_EXPR_SLOT (begin);
5662 TARGET_EXPR_CLEANUP (begin)
5663 = build3 (COND_EXPR, void_type_node, flag,
5664 void_zero_node,
5665 build_call (abort_fn, guard_addr_list));
5666 CLEANUP_EH_ONLY (begin) = 1;
5668 /* Do the initialization itself. */
5669 init = add_stmt_to_compound (begin, init);
5670 init = add_stmt_to_compound
5671 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
5672 init = add_stmt_to_compound
5673 (init, build_call (release_fn, guard_addr_list));
5675 else
5676 init = add_stmt_to_compound (init, set_guard (guard));
5678 /* Use atexit to register a function for destroying this static
5679 variable. */
5680 init = add_stmt_to_compound (init, register_dtor_fn (decl));
5682 finish_expr_stmt (init);
5684 if (flag_threadsafe_statics)
5686 finish_compound_stmt (inner_then_clause);
5687 finish_then_clause (inner_if_stmt);
5688 finish_if_stmt (inner_if_stmt);
5691 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
5693 finish_compound_stmt (then_clause);
5694 finish_then_clause (if_stmt);
5695 finish_if_stmt (if_stmt);
5698 else
5699 static_aggregates = tree_cons (init, decl, static_aggregates);
5703 /* Make TYPE a complete type based on INITIAL_VALUE.
5704 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
5705 2 if there was no information (in which case assume 0 if DO_DEFAULT),
5706 3 if the initializer list is empty (in pedantic mode). */
5709 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
5711 int failure;
5712 tree type, elt_type;
5714 if (initial_value)
5716 /* An array of character type can be initialized from a
5717 brace-enclosed string constant.
5719 FIXME: this code is duplicated from reshape_init. Probably
5720 we should just call reshape_init here? */
5721 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
5722 && TREE_CODE (initial_value) == CONSTRUCTOR
5723 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (initial_value)))
5725 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
5726 tree value = VEC_index (constructor_elt, v, 0)->value;
5728 if (TREE_CODE (value) == STRING_CST
5729 && VEC_length (constructor_elt, v) == 1)
5730 initial_value = value;
5734 failure = complete_array_type (ptype, initial_value, do_default);
5736 /* We can create the array before the element type is complete, which
5737 means that we didn't have these two bits set in the original type
5738 either. In completing the type, we are expected to propagate these
5739 bits. See also complete_type which does the same thing for arrays
5740 of fixed size. */
5741 type = *ptype;
5742 if (TYPE_DOMAIN (type))
5744 elt_type = TREE_TYPE (type);
5745 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
5746 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5747 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
5750 return failure;
5753 /* Return zero if something is declared to be a member of type
5754 CTYPE when in the context of CUR_TYPE. STRING is the error
5755 message to print in that case. Otherwise, quietly return 1. */
5757 static int
5758 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
5760 if (ctype && ctype != cur_type)
5762 if (flags == DTOR_FLAG)
5763 error ("destructor for alien class %qT cannot be a member", ctype);
5764 else
5765 error ("constructor for alien class %qT cannot be a member", ctype);
5766 return 0;
5768 return 1;
5771 /* Subroutine of `grokdeclarator'. */
5773 /* Generate errors possibly applicable for a given set of specifiers.
5774 This is for ARM $7.1.2. */
5776 static void
5777 bad_specifiers (tree object,
5778 const char* type,
5779 int virtualp,
5780 int quals,
5781 int inlinep,
5782 int friendp,
5783 int raises)
5785 if (virtualp)
5786 error ("%qD declared as a %<virtual%> %s", object, type);
5787 if (inlinep)
5788 error ("%qD declared as an %<inline%> %s", object, type);
5789 if (quals)
5790 error ("%<const%> and %<volatile%> function specifiers on "
5791 "%qD invalid in %s declaration",
5792 object, type);
5793 if (friendp)
5794 error ("%q+D declared as a friend", object);
5795 if (raises
5796 && (TREE_CODE (object) == TYPE_DECL
5797 || (!TYPE_PTRFN_P (TREE_TYPE (object))
5798 && !TYPE_REFFN_P (TREE_TYPE (object))
5799 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
5800 error ("%q+D declared with an exception specification", object);
5803 /* DECL is a member function or static data member and is presently
5804 being defined. Check that the definition is taking place in a
5805 valid namespace. */
5807 static void
5808 check_class_member_definition_namespace (tree decl)
5810 /* These checks only apply to member functions and static data
5811 members. */
5812 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
5813 || TREE_CODE (decl) == VAR_DECL);
5814 /* We check for problems with specializations in pt.c in
5815 check_specialization_namespace, where we can issue better
5816 diagnostics. */
5817 if (processing_specialization)
5818 return;
5819 /* There are no restrictions on the placement of
5820 explicit instantiations. */
5821 if (processing_explicit_instantiation)
5822 return;
5823 /* [class.mfct]
5825 A member function definition that appears outside of the
5826 class definition shall appear in a namespace scope enclosing
5827 the class definition.
5829 [class.static.data]
5831 The definition for a static data member shall appear in a
5832 namespace scope enclosing the member's class definition. */
5833 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
5834 pedwarn ("definition of %qD is not in namespace enclosing %qT",
5835 decl, DECL_CONTEXT (decl));
5838 /* Build a PARM_DECL for the "this" parameter. TYPE is the
5839 METHOD_TYPE for a non-static member function; QUALS are the
5840 cv-qualifiers that apply to the function. */
5842 tree
5843 build_this_parm (tree type, cp_cv_quals quals)
5845 tree this_type;
5846 tree qual_type;
5847 tree parm;
5848 cp_cv_quals this_quals;
5850 this_type = TREE_VALUE (TYPE_ARG_TYPES (type));
5851 /* The `this' parameter is implicitly `const'; it cannot be
5852 assigned to. */
5853 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
5854 qual_type = cp_build_qualified_type (this_type, this_quals);
5855 parm = build_artificial_parm (this_identifier, qual_type);
5856 cp_apply_type_quals_to_decl (this_quals, parm);
5857 return parm;
5860 /* CTYPE is class type, or null if non-class.
5861 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
5862 or METHOD_TYPE.
5863 DECLARATOR is the function's name.
5864 PARMS is a chain of PARM_DECLs for the function.
5865 VIRTUALP is truthvalue of whether the function is virtual or not.
5866 FLAGS are to be passed through to `grokclassfn'.
5867 QUALS are qualifiers indicating whether the function is `const'
5868 or `volatile'.
5869 RAISES is a list of exceptions that this function can raise.
5870 CHECK is 1 if we must find this method in CTYPE, 0 if we should
5871 not look, and -1 if we should not call `grokclassfn' at all.
5873 SFK is the kind of special function (if any) for the new function.
5875 Returns `NULL_TREE' if something goes wrong, after issuing
5876 applicable error messages. */
5878 static tree
5879 grokfndecl (tree ctype,
5880 tree type,
5881 tree declarator,
5882 tree parms,
5883 tree orig_declarator,
5884 int virtualp,
5885 enum overload_flags flags,
5886 cp_cv_quals quals,
5887 tree raises,
5888 int check,
5889 int friendp,
5890 int publicp,
5891 int inlinep,
5892 special_function_kind sfk,
5893 bool funcdef_flag,
5894 int template_count,
5895 tree in_namespace,
5896 tree* attrlist)
5898 tree decl;
5899 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
5900 tree t;
5902 if (raises)
5903 type = build_exception_variant (type, raises);
5905 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
5906 if (TREE_CODE (type) == METHOD_TYPE)
5908 tree parm;
5909 parm = build_this_parm (type, quals);
5910 TREE_CHAIN (parm) = parms;
5911 parms = parm;
5913 DECL_ARGUMENTS (decl) = parms;
5914 /* Propagate volatile out from type to decl. */
5915 if (TYPE_VOLATILE (type))
5916 TREE_THIS_VOLATILE (decl) = 1;
5918 if (friendp
5919 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
5921 if (funcdef_flag)
5922 error
5923 ("defining explicit specialization %qD in friend declaration",
5924 orig_declarator);
5925 else
5927 tree fns = TREE_OPERAND (orig_declarator, 0);
5928 tree args = TREE_OPERAND (orig_declarator, 1);
5930 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
5932 /* Something like `template <class T> friend void f<T>()'. */
5933 error ("invalid use of template-id %qD in declaration "
5934 "of primary template",
5935 orig_declarator);
5936 return NULL_TREE;
5940 /* A friend declaration of the form friend void f<>(). Record
5941 the information in the TEMPLATE_ID_EXPR. */
5942 SET_DECL_IMPLICIT_INSTANTIATION (decl);
5944 if (TREE_CODE (fns) == COMPONENT_REF)
5946 /* Due to bison parser ickiness, we will have already looked
5947 up an operator_name or PFUNCNAME within the current class
5948 (see template_id in parse.y). If the current class contains
5949 such a name, we'll get a COMPONENT_REF here. Undo that. */
5951 gcc_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
5952 == current_class_type);
5953 fns = TREE_OPERAND (fns, 1);
5955 gcc_assert (TREE_CODE (fns) == IDENTIFIER_NODE
5956 || TREE_CODE (fns) == OVERLOAD);
5957 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
5959 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5960 if (TREE_PURPOSE (t)
5961 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
5963 error ("default arguments are not allowed in declaration "
5964 "of friend template specialization %qD",
5965 decl);
5966 return NULL_TREE;
5969 if (inlinep)
5971 error ("%<inline%> is not allowed in declaration of friend "
5972 "template specialization %qD",
5973 decl);
5974 return NULL_TREE;
5979 /* If this decl has namespace scope, set that up. */
5980 if (in_namespace)
5981 set_decl_namespace (decl, in_namespace, friendp);
5982 else if (!ctype)
5983 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5985 /* `main' and builtins have implicit 'C' linkage. */
5986 if ((MAIN_NAME_P (declarator)
5987 || (IDENTIFIER_LENGTH (declarator) > 10
5988 && IDENTIFIER_POINTER (declarator)[0] == '_'
5989 && IDENTIFIER_POINTER (declarator)[1] == '_'
5990 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
5991 && current_lang_name == lang_name_cplusplus
5992 && ctype == NULL_TREE
5993 /* NULL_TREE means global namespace. */
5994 && DECL_CONTEXT (decl) == NULL_TREE)
5995 SET_DECL_LANGUAGE (decl, lang_c);
5997 /* Should probably propagate const out from type to decl I bet (mrs). */
5998 if (staticp)
6000 DECL_STATIC_FUNCTION_P (decl) = 1;
6001 DECL_CONTEXT (decl) = ctype;
6004 if (ctype)
6006 DECL_CONTEXT (decl) = ctype;
6007 if (funcdef_flag)
6008 check_class_member_definition_namespace (decl);
6011 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
6013 if (processing_template_decl)
6014 error ("cannot declare %<::main%> to be a template");
6015 if (inlinep)
6016 error ("cannot declare %<::main%> to be inline");
6017 if (!publicp)
6018 error ("cannot declare %<::main%> to be static");
6019 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
6020 integer_type_node))
6022 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
6023 tree newtype;
6024 error ("%<::main%> must return %<int%>");
6025 newtype = build_function_type (integer_type_node,
6026 oldtypeargs);
6027 TREE_TYPE (decl) = newtype;
6029 inlinep = 0;
6030 publicp = 1;
6033 /* Members of anonymous types and local classes have no linkage; make
6034 them internal. If a typedef is made later, this will be changed. */
6035 if (ctype && (TYPE_ANONYMOUS_P (ctype)
6036 || decl_function_context (TYPE_MAIN_DECL (ctype))))
6037 publicp = 0;
6039 if (publicp)
6041 /* [basic.link]: A name with no linkage (notably, the name of a class
6042 or enumeration declared in a local scope) shall not be used to
6043 declare an entity with linkage.
6045 Only check this for public decls for now. See core 319, 389. */
6046 t = no_linkage_check (TREE_TYPE (decl),
6047 /*relaxed_p=*/false);
6048 if (t)
6050 if (TYPE_ANONYMOUS_P (t))
6052 if (DECL_EXTERN_C_P (decl))
6053 /* Allow this; it's pretty common in C. */;
6054 else
6056 pedwarn ("non-local function %q#D uses anonymous type",
6057 decl);
6058 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
6059 pedwarn ("%q+#D does not refer to the unqualified "
6060 "type, so it is not used for linkage",
6061 TYPE_NAME (t));
6064 else
6065 pedwarn ("non-local function %q#D uses local type %qT", decl, t);
6069 TREE_PUBLIC (decl) = publicp;
6070 if (! publicp)
6072 DECL_INTERFACE_KNOWN (decl) = 1;
6073 DECL_NOT_REALLY_EXTERN (decl) = 1;
6076 /* If the declaration was declared inline, mark it as such. */
6077 if (inlinep)
6078 DECL_DECLARED_INLINE_P (decl) = 1;
6079 /* We inline functions that are explicitly declared inline, or, when
6080 the user explicitly asks us to, all functions. */
6081 if (DECL_DECLARED_INLINE_P (decl)
6082 || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag))
6083 DECL_INLINE (decl) = 1;
6085 DECL_EXTERNAL (decl) = 1;
6086 if (quals && TREE_CODE (type) == FUNCTION_TYPE)
6088 error ("%smember function %qD cannot have cv-qualifier",
6089 (ctype ? "static " : "non-"), decl);
6090 quals = TYPE_UNQUALIFIED;
6093 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
6094 && !grok_op_properties (decl, /*complain=*/true))
6095 return NULL_TREE;
6097 if (ctype && decl_function_context (decl))
6098 DECL_NO_STATIC_CHAIN (decl) = 1;
6100 if (funcdef_flag)
6101 /* Make the init_value nonzero so pushdecl knows this is not
6102 tentative. error_mark_node is replaced later with the BLOCK. */
6103 DECL_INITIAL (decl) = error_mark_node;
6105 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
6106 TREE_NOTHROW (decl) = 1;
6108 /* Caller will do the rest of this. */
6109 if (check < 0)
6110 return decl;
6112 if (ctype != NULL_TREE)
6114 if (sfk == sfk_constructor)
6115 DECL_CONSTRUCTOR_P (decl) = 1;
6117 grokclassfn (ctype, decl, flags);
6120 decl = check_explicit_specialization (orig_declarator, decl,
6121 template_count,
6122 2 * funcdef_flag +
6123 4 * (friendp != 0));
6124 if (decl == error_mark_node)
6125 return NULL_TREE;
6127 if (attrlist)
6129 cplus_decl_attributes (&decl, *attrlist, 0);
6130 *attrlist = NULL_TREE;
6133 if (ctype != NULL_TREE
6134 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
6135 && check)
6137 tree old_decl;
6139 old_decl = check_classfn (ctype, decl,
6140 (processing_template_decl
6141 > template_class_depth (ctype))
6142 ? current_template_parms
6143 : NULL_TREE);
6144 if (old_decl)
6146 tree ok;
6147 tree pushed_scope;
6149 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
6150 /* Because grokfndecl is always supposed to return a
6151 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
6152 here. We depend on our callers to figure out that its
6153 really a template that's being returned. */
6154 old_decl = DECL_TEMPLATE_RESULT (old_decl);
6156 if (DECL_STATIC_FUNCTION_P (old_decl)
6157 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
6158 /* Remove the `this' parm added by grokclassfn.
6159 XXX Isn't this done in start_function, too? */
6160 revert_static_member_fn (decl);
6161 if (DECL_ARTIFICIAL (old_decl))
6162 error ("definition of implicitly-declared %qD", old_decl);
6164 /* Since we've smashed OLD_DECL to its
6165 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
6166 if (TREE_CODE (decl) == TEMPLATE_DECL)
6167 decl = DECL_TEMPLATE_RESULT (decl);
6169 /* Attempt to merge the declarations. This can fail, in
6170 the case of some invalid specialization declarations. */
6171 pushed_scope = push_scope (ctype);
6172 ok = duplicate_decls (decl, old_decl, friendp);
6173 if (pushed_scope)
6174 pop_scope (pushed_scope);
6175 if (!ok)
6177 error ("no %q#D member function declared in class %qT",
6178 decl, ctype);
6179 return NULL_TREE;
6181 return old_decl;
6185 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
6186 return NULL_TREE;
6188 if (ctype == NULL_TREE || check)
6189 return decl;
6191 if (virtualp)
6192 DECL_VIRTUAL_P (decl) = 1;
6194 return decl;
6197 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
6198 the linkage that DECL will receive in the object file. */
6200 static void
6201 set_linkage_for_static_data_member (tree decl)
6203 /* A static data member always has static storage duration and
6204 external linkage. Note that static data members are forbidden in
6205 local classes -- the only situation in which a class has
6206 non-external linkage. */
6207 TREE_PUBLIC (decl) = 1;
6208 TREE_STATIC (decl) = 1;
6209 /* For non-template classes, static data members are always put
6210 out in exactly those files where they are defined, just as
6211 with ordinary namespace-scope variables. */
6212 if (!processing_template_decl)
6213 DECL_INTERFACE_KNOWN (decl) = 1;
6216 /* Create a VAR_DECL named NAME with the indicated TYPE.
6218 If SCOPE is non-NULL, it is the class type or namespace containing
6219 the variable. If SCOPE is NULL, the variable should is created in
6220 the innermost enclosings scope. */
6222 static tree
6223 grokvardecl (tree type,
6224 tree name,
6225 const cp_decl_specifier_seq *declspecs,
6226 int initialized,
6227 int constp,
6228 tree scope)
6230 tree decl;
6231 tree explicit_scope;
6233 gcc_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE);
6235 /* Compute the scope in which to place the variable, but remember
6236 whether or not that scope was explicitly specified by the user. */
6237 explicit_scope = scope;
6238 if (!scope)
6240 /* An explicit "extern" specifier indicates a namespace-scope
6241 variable. */
6242 if (declspecs->storage_class == sc_extern)
6243 scope = current_namespace;
6244 else if (!at_function_scope_p ())
6245 scope = current_scope ();
6248 if (scope
6249 && (/* If the variable is a namespace-scope variable declared in a
6250 template, we need DECL_LANG_SPECIFIC. */
6251 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
6252 /* Similarly for namespace-scope variables with language linkage
6253 other than C++. */
6254 || (TREE_CODE (scope) == NAMESPACE_DECL
6255 && current_lang_name != lang_name_cplusplus)
6256 /* Similarly for static data members. */
6257 || TYPE_P (scope)))
6258 decl = build_lang_decl (VAR_DECL, name, type);
6259 else
6260 decl = build_decl (VAR_DECL, name, type);
6262 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
6263 set_decl_namespace (decl, explicit_scope, 0);
6264 else
6265 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6267 if (declspecs->storage_class == sc_extern)
6269 DECL_THIS_EXTERN (decl) = 1;
6270 DECL_EXTERNAL (decl) = !initialized;
6273 if (DECL_CLASS_SCOPE_P (decl))
6275 set_linkage_for_static_data_member (decl);
6276 /* This function is only called with out-of-class definitions. */
6277 DECL_EXTERNAL (decl) = 0;
6278 check_class_member_definition_namespace (decl);
6280 /* At top level, either `static' or no s.c. makes a definition
6281 (perhaps tentative), and absence of `static' makes it public. */
6282 else if (toplevel_bindings_p ())
6284 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
6285 && (DECL_THIS_EXTERN (decl) || ! constp));
6286 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
6288 /* Not at top level, only `static' makes a static definition. */
6289 else
6291 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
6292 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
6295 if (declspecs->specs[(int)ds_thread])
6297 if (targetm.have_tls)
6298 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
6299 else
6300 /* A mere warning is sure to result in improper semantics
6301 at runtime. Don't bother to allow this to compile. */
6302 error ("thread-local storage not supported for this target");
6305 if (TREE_PUBLIC (decl))
6307 /* [basic.link]: A name with no linkage (notably, the name of a class
6308 or enumeration declared in a local scope) shall not be used to
6309 declare an entity with linkage.
6311 Only check this for public decls for now. */
6312 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
6313 if (t)
6315 if (TYPE_ANONYMOUS_P (t))
6317 if (DECL_EXTERN_C_P (decl))
6318 /* Allow this; it's pretty common in C. */
6320 else
6322 /* DRs 132, 319 and 389 seem to indicate types with
6323 no linkage can only be used to declare extern "C"
6324 entities. Since it's not always an error in the
6325 ISO C++ 90 Standard, we only issue a warning. */
6326 warning (0, "non-local variable %q#D uses anonymous type",
6327 decl);
6328 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
6329 warning (0, "%q+#D does not refer to the unqualified "
6330 "type, so it is not used for linkage",
6331 TYPE_NAME (t));
6334 else
6335 warning (0, "non-local variable %q#D uses local type %qT", decl, t);
6338 else
6339 DECL_INTERFACE_KNOWN (decl) = 1;
6341 return decl;
6344 /* Create and return a canonical pointer to member function type, for
6345 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
6347 tree
6348 build_ptrmemfunc_type (tree type)
6350 tree field, fields;
6351 tree t;
6352 tree unqualified_variant = NULL_TREE;
6354 if (type == error_mark_node)
6355 return type;
6357 /* If a canonical type already exists for this type, use it. We use
6358 this method instead of type_hash_canon, because it only does a
6359 simple equality check on the list of field members. */
6361 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
6362 return t;
6364 /* Make sure that we always have the unqualified pointer-to-member
6365 type first. */
6366 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
6367 unqualified_variant
6368 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
6370 t = make_aggr_type (RECORD_TYPE);
6371 xref_basetypes (t, NULL_TREE);
6373 /* Let the front-end know this is a pointer to member function... */
6374 TYPE_PTRMEMFUNC_FLAG (t) = 1;
6375 /* ... and not really an aggregate. */
6376 SET_IS_AGGR_TYPE (t, 0);
6378 field = build_decl (FIELD_DECL, pfn_identifier, type);
6379 fields = field;
6381 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
6382 TREE_CHAIN (field) = fields;
6383 fields = field;
6385 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
6387 /* Zap out the name so that the back-end will give us the debugging
6388 information for this anonymous RECORD_TYPE. */
6389 TYPE_NAME (t) = NULL_TREE;
6391 /* If this is not the unqualified form of this pointer-to-member
6392 type, set the TYPE_MAIN_VARIANT for this type to be the
6393 unqualified type. Since they are actually RECORD_TYPEs that are
6394 not variants of each other, we must do this manually. */
6395 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
6397 t = build_qualified_type (t, cp_type_quals (type));
6398 TYPE_MAIN_VARIANT (t) = unqualified_variant;
6399 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
6400 TYPE_NEXT_VARIANT (unqualified_variant) = t;
6403 /* Cache this pointer-to-member type so that we can find it again
6404 later. */
6405 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
6407 return t;
6410 /* Create and return a pointer to data member type. */
6412 tree
6413 build_ptrmem_type (tree class_type, tree member_type)
6415 if (TREE_CODE (member_type) == METHOD_TYPE)
6417 tree arg_types;
6419 arg_types = TYPE_ARG_TYPES (member_type);
6420 class_type = (cp_build_qualified_type
6421 (class_type,
6422 cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types)))));
6423 member_type
6424 = build_method_type_directly (class_type,
6425 TREE_TYPE (member_type),
6426 TREE_CHAIN (arg_types));
6427 return build_ptrmemfunc_type (build_pointer_type (member_type));
6429 else
6431 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
6432 return build_offset_type (class_type, member_type);
6436 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
6437 Check to see that the definition is valid. Issue appropriate error
6438 messages. Return 1 if the definition is particularly bad, or 0
6439 otherwise. */
6442 check_static_variable_definition (tree decl, tree type)
6444 /* Motion 10 at San Diego: If a static const integral data member is
6445 initialized with an integral constant expression, the initializer
6446 may appear either in the declaration (within the class), or in
6447 the definition, but not both. If it appears in the class, the
6448 member is a member constant. The file-scope definition is always
6449 required. */
6450 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
6452 error ("invalid in-class initialization of static data member "
6453 "of non-integral type %qT",
6454 type);
6455 /* If we just return the declaration, crashes will sometimes
6456 occur. We therefore return void_type_node, as if this were a
6457 friend declaration, to cause callers to completely ignore
6458 this declaration. */
6459 return 1;
6461 else if (!CP_TYPE_CONST_P (type))
6462 error ("ISO C++ forbids in-class initialization of non-const "
6463 "static member %qD",
6464 decl);
6465 else if (pedantic && !INTEGRAL_TYPE_P (type))
6466 pedwarn ("ISO C++ forbids initialization of member constant "
6467 "%qD of non-integral type %qT", decl, type);
6469 return 0;
6472 /* Given the SIZE (i.e., number of elements) in an array, compute an
6473 appropriate index type for the array. If non-NULL, NAME is the
6474 name of the thing being declared. */
6476 tree
6477 compute_array_index_type (tree name, tree size)
6479 tree type;
6480 tree itype;
6482 if (error_operand_p (size))
6483 return error_mark_node;
6485 type = TREE_TYPE (size);
6486 /* The array bound must be an integer type. */
6487 if (!dependent_type_p (type) && !INTEGRAL_TYPE_P (type))
6489 if (name)
6490 error ("size of array %qD has non-integral type %qT", name, type);
6491 else
6492 error ("size of array has non-integral type %qT", type);
6493 size = integer_one_node;
6494 type = TREE_TYPE (size);
6497 if (abi_version_at_least (2)
6498 /* We should only handle value dependent expressions specially. */
6499 ? value_dependent_expression_p (size)
6500 /* But for abi-1, we handled all instances in templates. This
6501 effects the manglings produced. */
6502 : processing_template_decl)
6503 return build_index_type (build_min (MINUS_EXPR, sizetype,
6504 size, integer_one_node));
6506 /* The size might be the result of a cast. */
6507 STRIP_TYPE_NOPS (size);
6509 /* It might be a const variable or enumeration constant. */
6510 size = integral_constant_value (size);
6512 /* Normally, the array-bound will be a constant. */
6513 if (TREE_CODE (size) == INTEGER_CST)
6515 /* Check to see if the array bound overflowed. Make that an
6516 error, no matter how generous we're being. */
6517 int old_flag_pedantic_errors = flag_pedantic_errors;
6518 int old_pedantic = pedantic;
6519 pedantic = flag_pedantic_errors = 1;
6520 constant_expression_warning (size);
6521 pedantic = old_pedantic;
6522 flag_pedantic_errors = old_flag_pedantic_errors;
6524 /* An array must have a positive number of elements. */
6525 if (INT_CST_LT (size, integer_zero_node))
6527 if (name)
6528 error ("size of array %qD is negative", name);
6529 else
6530 error ("size of array is negative");
6531 size = integer_one_node;
6533 /* As an extension we allow zero-sized arrays. We always allow
6534 them in system headers because glibc uses them. */
6535 else if (integer_zerop (size) && pedantic && !in_system_header)
6537 if (name)
6538 pedwarn ("ISO C++ forbids zero-size array %qD", name);
6539 else
6540 pedwarn ("ISO C++ forbids zero-size array");
6543 else if (TREE_CONSTANT (size))
6545 /* `(int) &fn' is not a valid array bound. */
6546 if (name)
6547 error ("size of array %qD is not an integral constant-expression",
6548 name);
6549 else
6550 error ("size of array is not an integral constant-expression");
6551 size = integer_one_node;
6553 else if (pedantic)
6555 if (name)
6556 pedwarn ("ISO C++ forbids variable-size array %qD", name);
6557 else
6558 pedwarn ("ISO C++ forbids variable-size array");
6561 if (processing_template_decl && !TREE_CONSTANT (size))
6562 /* A variable sized array. */
6563 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
6564 else
6566 HOST_WIDE_INT saved_processing_template_decl;
6568 /* Compute the index of the largest element in the array. It is
6569 one less than the number of elements in the array. We save
6570 and restore PROCESSING_TEMPLATE_DECL so that computations in
6571 cp_build_binary_op will be appropriately folded. */
6572 saved_processing_template_decl = processing_template_decl;
6573 processing_template_decl = 0;
6574 itype = cp_build_binary_op (MINUS_EXPR,
6575 cp_convert (ssizetype, size),
6576 cp_convert (ssizetype, integer_one_node));
6577 itype = fold (itype);
6578 processing_template_decl = saved_processing_template_decl;
6580 if (!TREE_CONSTANT (itype))
6581 /* A variable sized array. */
6582 itype = variable_size (itype);
6583 /* Make sure that there was no overflow when creating to a signed
6584 index type. (For example, on a 32-bit machine, an array with
6585 size 2^32 - 1 is too big.) */
6586 else if (TREE_CODE (itype) == INTEGER_CST
6587 && TREE_OVERFLOW (itype))
6589 error ("overflow in array dimension");
6590 TREE_OVERFLOW (itype) = 0;
6594 /* Create and return the appropriate index type. */
6595 return build_index_type (itype);
6598 /* Returns the scope (if any) in which the entity declared by
6599 DECLARATOR will be located. If the entity was declared with an
6600 unqualified name, NULL_TREE is returned. */
6602 tree
6603 get_scope_of_declarator (const cp_declarator *declarator)
6605 while (declarator && declarator->kind != cdk_id)
6606 declarator = declarator->declarator;
6608 /* If the declarator-id is a SCOPE_REF, the scope in which the
6609 declaration occurs is the first operand. */
6610 if (declarator
6611 && declarator->u.id.qualifying_scope)
6612 return declarator->u.id.qualifying_scope;
6614 /* Otherwise, the declarator is not a qualified name; the entity will
6615 be declared in the current scope. */
6616 return NULL_TREE;
6619 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
6620 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
6621 with this type. */
6623 static tree
6624 create_array_type_for_decl (tree name, tree type, tree size)
6626 tree itype = NULL_TREE;
6627 const char* error_msg;
6629 /* If things have already gone awry, bail now. */
6630 if (type == error_mark_node || size == error_mark_node)
6631 return error_mark_node;
6633 /* Assume that everything will go OK. */
6634 error_msg = NULL;
6636 /* There are some types which cannot be array elements. */
6637 switch (TREE_CODE (type))
6639 case VOID_TYPE:
6640 error_msg = "array of void";
6641 break;
6643 case FUNCTION_TYPE:
6644 error_msg = "array of functions";
6645 break;
6647 case REFERENCE_TYPE:
6648 error_msg = "array of references";
6649 break;
6651 case METHOD_TYPE:
6652 error_msg = "array of function members";
6653 break;
6655 default:
6656 break;
6659 /* If something went wrong, issue an error-message and return. */
6660 if (error_msg)
6662 if (name)
6663 error ("declaration of %qD as %s", name, error_msg);
6664 else
6665 error ("creating %s", error_msg);
6667 return error_mark_node;
6670 /* [dcl.array]
6672 The constant expressions that specify the bounds of the arrays
6673 can be omitted only for the first member of the sequence. */
6674 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
6676 if (name)
6677 error ("declaration of %qD as multidimensional array must "
6678 "have bounds for all dimensions except the first",
6679 name);
6680 else
6681 error ("multidimensional array must have bounds for all "
6682 "dimensions except the first");
6684 return error_mark_node;
6687 /* Figure out the index type for the array. */
6688 if (size)
6689 itype = compute_array_index_type (name, size);
6691 /* [dcl.array]
6692 T is called the array element type; this type shall not be [...] an
6693 abstract class type. */
6694 abstract_virtuals_error (name, type);
6696 return build_cplus_array_type (type, itype);
6699 /* Check that it's OK to declare a function with the indicated TYPE.
6700 SFK indicates the kind of special function (if any) that this
6701 function is. OPTYPE is the type given in a conversion operator
6702 declaration, or the class type for a constructor/destructor.
6703 Returns the actual return type of the function; that
6704 may be different than TYPE if an error occurs, or for certain
6705 special functions. */
6707 static tree
6708 check_special_function_return_type (special_function_kind sfk,
6709 tree type,
6710 tree optype)
6712 switch (sfk)
6714 case sfk_constructor:
6715 if (type)
6716 error ("return type specification for constructor invalid");
6718 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
6719 type = build_pointer_type (optype);
6720 else
6721 type = void_type_node;
6722 break;
6724 case sfk_destructor:
6725 if (type)
6726 error ("return type specification for destructor invalid");
6727 /* We can't use the proper return type here because we run into
6728 problems with ambiguous bases and covariant returns.
6729 Java classes are left unchanged because (void *) isn't a valid
6730 Java type, and we don't want to change the Java ABI. */
6731 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
6732 type = build_pointer_type (void_type_node);
6733 else
6734 type = void_type_node;
6735 break;
6737 case sfk_conversion:
6738 if (type && !same_type_p (type, optype))
6739 error ("operator %qT declared to return %qT", optype, type);
6740 else if (type)
6741 pedwarn ("return type specified for %<operator %T%>", optype);
6742 type = optype;
6743 break;
6745 default:
6746 gcc_unreachable ();
6749 return type;
6752 /* A variable or data member (whose unqualified name is IDENTIFIER)
6753 has been declared with the indicated TYPE. If the TYPE is not
6754 acceptable, issue an error message and return a type to use for
6755 error-recovery purposes. */
6757 tree
6758 check_var_type (tree identifier, tree type)
6760 if (VOID_TYPE_P (type))
6762 if (!identifier)
6763 error ("unnamed variable or field declared void");
6764 else if (TREE_CODE (identifier) == IDENTIFIER_NODE)
6766 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
6767 error ("variable or field %qE declared void", identifier);
6769 else
6770 error ("variable or field declared void");
6771 type = error_mark_node;
6774 return type;
6777 /* Given declspecs and a declarator (abstract or otherwise), determine
6778 the name and type of the object declared and construct a DECL node
6779 for it.
6781 DECLSPECS is a chain of tree_list nodes whose value fields
6782 are the storage classes and type specifiers.
6784 DECL_CONTEXT says which syntactic context this declaration is in:
6785 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6786 FUNCDEF for a function definition. Like NORMAL but a few different
6787 error messages in each case. Return value may be zero meaning
6788 this definition is too screwy to try to parse.
6789 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
6790 handle member functions (which have FIELD context).
6791 Return value may be zero meaning this definition is too screwy to
6792 try to parse.
6793 PARM for a parameter declaration (either within a function prototype
6794 or before a function body). Make a PARM_DECL, or return void_type_node.
6795 CATCHPARM for a parameter declaration before a catch clause.
6796 TYPENAME if for a typename (in a cast or sizeof).
6797 Don't make a DECL node; just return the ..._TYPE node.
6798 FIELD for a struct or union field; make a FIELD_DECL.
6799 BITFIELD for a field with specified width.
6800 INITIALIZED is 1 if the decl has an initializer.
6802 ATTRLIST is a pointer to the list of attributes, which may be NULL
6803 if there are none; *ATTRLIST may be modified if attributes from inside
6804 the declarator should be applied to the declaration.
6806 When this function is called, scoping variables (such as
6807 CURRENT_CLASS_TYPE) should reflect the scope in which the
6808 declaration occurs, not the scope in which the new declaration will
6809 be placed. For example, on:
6811 void S::f() { ... }
6813 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
6814 should not be `S'.
6816 Returns a DECL (if a declarator is present), a TYPE (if there is no
6817 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
6818 error occurs. */
6820 tree
6821 grokdeclarator (const cp_declarator *declarator,
6822 const cp_decl_specifier_seq *declspecs,
6823 enum decl_context decl_context,
6824 int initialized,
6825 tree* attrlist)
6827 tree type = NULL_TREE;
6828 int longlong = 0;
6829 int virtualp, explicitp, friendp, inlinep, staticp;
6830 int explicit_int = 0;
6831 int explicit_char = 0;
6832 int defaulted_int = 0;
6833 tree dependent_name = NULL_TREE;
6835 tree typedef_decl = NULL_TREE;
6836 const char *name = NULL;
6837 tree typedef_type = NULL_TREE;
6838 /* True if this declarator is a function definition. */
6839 bool funcdef_flag = false;
6840 cp_declarator_kind innermost_code = cdk_error;
6841 int bitfield = 0;
6842 #if 0
6843 /* See the code below that used this. */
6844 tree decl_attr = NULL_TREE;
6845 #endif
6847 /* Keep track of what sort of function is being processed
6848 so that we can warn about default return values, or explicit
6849 return values which do not match prescribed defaults. */
6850 special_function_kind sfk = sfk_none;
6852 tree dname = NULL_TREE;
6853 tree ctor_return_type = NULL_TREE;
6854 enum overload_flags flags = NO_SPECIAL;
6855 /* cv-qualifiers that apply to the declarator, for a declaration of
6856 a member function. */
6857 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
6858 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
6859 int type_quals;
6860 tree raises = NULL_TREE;
6861 int template_count = 0;
6862 tree returned_attrs = NULL_TREE;
6863 tree parms = NULL_TREE;
6864 const cp_declarator *id_declarator;
6865 /* The unqualified name of the declarator; either an
6866 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
6867 tree unqualified_id;
6868 /* The class type, if any, in which this entity is located,
6869 or NULL_TREE if none. Note that this value may be different from
6870 the current class type; for example if an attempt is made to declare
6871 "A::f" inside "B", this value will be "A". */
6872 tree ctype = current_class_type;
6873 /* The NAMESPACE_DECL for the namespace in which this entity is
6874 located. If an unqualified name is used to declare the entity,
6875 this value will be NULL_TREE, even if the entity is located at
6876 namespace scope. */
6877 tree in_namespace = NULL_TREE;
6878 cp_storage_class storage_class;
6879 bool unsigned_p, signed_p, short_p, long_p, thread_p;
6880 bool type_was_error_mark_node = false;
6882 signed_p = declspecs->specs[(int)ds_signed];
6883 unsigned_p = declspecs->specs[(int)ds_unsigned];
6884 short_p = declspecs->specs[(int)ds_short];
6885 long_p = declspecs->specs[(int)ds_long];
6886 longlong = declspecs->specs[(int)ds_long] >= 2;
6887 thread_p = declspecs->specs[(int)ds_thread];
6889 if (decl_context == FUNCDEF)
6890 funcdef_flag = true, decl_context = NORMAL;
6891 else if (decl_context == MEMFUNCDEF)
6892 funcdef_flag = true, decl_context = FIELD;
6893 else if (decl_context == BITFIELD)
6894 bitfield = 1, decl_context = FIELD;
6896 /* Look inside a declarator for the name being declared
6897 and get it as a string, for an error message. */
6898 for (id_declarator = declarator;
6899 id_declarator;
6900 id_declarator = id_declarator->declarator)
6902 if (id_declarator->kind != cdk_id)
6903 innermost_code = id_declarator->kind;
6905 switch (id_declarator->kind)
6907 case cdk_function:
6908 if (id_declarator->declarator
6909 && id_declarator->declarator->kind == cdk_id)
6911 sfk = id_declarator->declarator->u.id.sfk;
6912 if (sfk == sfk_destructor)
6913 flags = DTOR_FLAG;
6915 break;
6917 case cdk_id:
6919 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
6920 tree decl = id_declarator->u.id.unqualified_name;
6921 if (!decl)
6922 break;
6923 if (qualifying_scope)
6925 if (at_function_scope_p ())
6927 /* [dcl.meaning]
6929 A declarator-id shall not be qualified except
6930 for ...
6932 None of the cases are permitted in block
6933 scope. */
6934 if (qualifying_scope == global_namespace)
6935 error ("invalid use of qualified-name %<::%D%>",
6936 decl);
6937 else if (TYPE_P (qualifying_scope))
6938 error ("invalid use of qualified-name %<%T::%D%>",
6939 qualifying_scope, decl);
6940 else
6941 error ("invalid use of qualified-name %<%D::%D%>",
6942 qualifying_scope, decl);
6943 return error_mark_node;
6945 else if (TYPE_P (qualifying_scope))
6947 ctype = qualifying_scope;
6948 if (innermost_code != cdk_function
6949 && current_class_type
6950 && !UNIQUELY_DERIVED_FROM_P (ctype,
6951 current_class_type))
6953 error ("type %qT is not derived from type %qT",
6954 ctype, current_class_type);
6955 return error_mark_node;
6958 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
6959 in_namespace = qualifying_scope;
6961 switch (TREE_CODE (decl))
6963 case BIT_NOT_EXPR:
6965 tree type;
6967 if (innermost_code != cdk_function)
6969 error ("declaration of %qD as non-function", decl);
6970 return error_mark_node;
6972 else if (!qualifying_scope
6973 && !(current_class_type && at_class_scope_p ()))
6975 error ("declaration of %qD as non-member", decl);
6976 return error_mark_node;
6979 type = TREE_OPERAND (decl, 0);
6980 name = IDENTIFIER_POINTER (constructor_name (type));
6981 dname = decl;
6983 break;
6985 case TEMPLATE_ID_EXPR:
6987 tree fns = TREE_OPERAND (decl, 0);
6989 dname = fns;
6990 if (TREE_CODE (dname) != IDENTIFIER_NODE)
6992 gcc_assert (is_overloaded_fn (dname));
6993 dname = DECL_NAME (get_first_fn (dname));
6996 /* Fall through. */
6998 case IDENTIFIER_NODE:
6999 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7000 dname = decl;
7002 if (C_IS_RESERVED_WORD (dname))
7004 error ("declarator-id missing; using reserved word %qD",
7005 dname);
7006 name = IDENTIFIER_POINTER (dname);
7008 else if (!IDENTIFIER_TYPENAME_P (dname))
7009 name = IDENTIFIER_POINTER (dname);
7010 else
7012 gcc_assert (flags == NO_SPECIAL);
7013 flags = TYPENAME_FLAG;
7014 ctor_return_type = TREE_TYPE (dname);
7015 sfk = sfk_conversion;
7016 if (is_typename_at_global_scope (dname))
7017 name = IDENTIFIER_POINTER (dname);
7018 else
7019 name = "<invalid operator>";
7021 break;
7023 default:
7024 gcc_unreachable ();
7026 break;
7028 case cdk_array:
7029 case cdk_pointer:
7030 case cdk_reference:
7031 case cdk_ptrmem:
7032 break;
7034 case cdk_error:
7035 return error_mark_node;
7037 default:
7038 gcc_unreachable ();
7041 if (id_declarator->kind == cdk_id)
7042 break;
7045 /* [dcl.fct.edf]
7047 The declarator in a function-definition shall have the form
7048 D1 ( parameter-declaration-clause) ... */
7049 if (funcdef_flag && innermost_code != cdk_function)
7051 error ("function definition does not declare parameters");
7052 return error_mark_node;
7055 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
7056 && innermost_code != cdk_function
7057 && ! (ctype && !declspecs->any_specifiers_p))
7059 error ("declaration of %qD as non-function", dname);
7060 return error_mark_node;
7063 /* Anything declared one level down from the top level
7064 must be one of the parameters of a function
7065 (because the body is at least two levels down). */
7067 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
7068 by not allowing C++ class definitions to specify their parameters
7069 with xdecls (must be spec.d in the parmlist).
7071 Since we now wait to push a class scope until we are sure that
7072 we are in a legitimate method context, we must set oldcname
7073 explicitly (since current_class_name is not yet alive).
7075 We also want to avoid calling this a PARM if it is in a namespace. */
7077 if (decl_context == NORMAL && !toplevel_bindings_p ())
7079 struct cp_binding_level *b = current_binding_level;
7080 current_binding_level = b->level_chain;
7081 if (current_binding_level != 0 && toplevel_bindings_p ())
7082 decl_context = PARM;
7083 current_binding_level = b;
7086 if (name == NULL)
7087 name = decl_context == PARM ? "parameter" : "type name";
7089 /* If there were multiple types specified in the decl-specifier-seq,
7090 issue an error message. */
7091 if (declspecs->multiple_types_p)
7093 error ("two or more data types in declaration of %qs", name);
7094 return error_mark_node;
7097 /* Extract the basic type from the decl-specifier-seq. */
7098 type = declspecs->type;
7099 if (type == error_mark_node)
7101 type = NULL_TREE;
7102 type_was_error_mark_node = true;
7104 /* If the entire declaration is itself tagged as deprecated then
7105 suppress reports of deprecated items. */
7106 if (type && TREE_DEPRECATED (type)
7107 && deprecated_state != DEPRECATED_SUPPRESS)
7108 warn_deprecated_use (type);
7109 if (type && TREE_CODE (type) == TYPE_DECL)
7111 typedef_decl = type;
7112 type = TREE_TYPE (typedef_decl);
7114 /* No type at all: default to `int', and set DEFAULTED_INT
7115 because it was not a user-defined typedef. */
7116 if (type == NULL_TREE && (signed_p || unsigned_p || long_p || short_p))
7118 /* These imply 'int'. */
7119 type = integer_type_node;
7120 defaulted_int = 1;
7122 /* Gather flags. */
7123 explicit_int = declspecs->explicit_int_p;
7124 explicit_char = declspecs->explicit_char_p;
7126 #if 0
7127 /* See the code below that used this. */
7128 if (typedef_decl)
7129 decl_attr = DECL_ATTRIBUTES (typedef_decl);
7130 #endif
7131 typedef_type = type;
7134 if (sfk != sfk_conversion)
7135 ctor_return_type = ctype;
7137 if (sfk != sfk_none)
7138 type = check_special_function_return_type (sfk, type,
7139 ctor_return_type);
7140 else if (type == NULL_TREE)
7142 int is_main;
7144 explicit_int = -1;
7146 /* We handle `main' specially here, because 'main () { }' is so
7147 common. With no options, it is allowed. With -Wreturn-type,
7148 it is a warning. It is only an error with -pedantic-errors. */
7149 is_main = (funcdef_flag
7150 && dname && MAIN_NAME_P (dname)
7151 && ctype == NULL_TREE
7152 && in_namespace == NULL_TREE
7153 && current_namespace == global_namespace);
7155 if (type_was_error_mark_node)
7156 /* We've already issued an error, don't complain more. */;
7157 else if (in_system_header || flag_ms_extensions)
7158 /* Allow it, sigh. */;
7159 else if (pedantic || ! is_main)
7160 pedwarn ("ISO C++ forbids declaration of %qs with no type", name);
7161 else if (warn_return_type)
7162 warning (0, "ISO C++ forbids declaration of %qs with no type", name);
7164 type = integer_type_node;
7167 ctype = NULL_TREE;
7169 /* Now process the modifiers that were specified
7170 and check for invalid combinations. */
7172 /* Long double is a special combination. */
7173 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
7175 long_p = false;
7176 type = build_qualified_type (long_double_type_node,
7177 cp_type_quals (type));
7180 /* Check all other uses of type modifiers. */
7182 if (unsigned_p || signed_p || long_p || short_p)
7184 int ok = 0;
7186 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
7187 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
7188 else if (signed_p && unsigned_p)
7189 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
7190 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
7191 error ("%<long long%> invalid for %qs", name);
7192 else if (long_p && TREE_CODE (type) == REAL_TYPE)
7193 error ("%<long%> invalid for %qs", name);
7194 else if (short_p && TREE_CODE (type) == REAL_TYPE)
7195 error ("%<short%> invalid for %qs", name);
7196 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
7197 error ("%<long%> or %<short%> invalid for %qs", name);
7198 else if ((long_p || short_p) && explicit_char)
7199 error ("%<long%> or %<short%> specified with char for %qs", name);
7200 else if (long_p && short_p)
7201 error ("%<long%> and %<short%> specified together for %qs", name);
7202 else
7204 ok = 1;
7205 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
7207 pedwarn ("long, short, signed or unsigned used invalidly for %qs",
7208 name);
7209 if (flag_pedantic_errors)
7210 ok = 0;
7214 /* Discard the type modifiers if they are invalid. */
7215 if (! ok)
7217 unsigned_p = false;
7218 signed_p = false;
7219 long_p = false;
7220 short_p = false;
7221 longlong = 0;
7225 /* Decide whether an integer type is signed or not.
7226 Optionally treat bitfields as signed by default. */
7227 if (unsigned_p
7228 /* [class.bit]
7230 It is implementation-defined whether a plain (neither
7231 explicitly signed or unsigned) char, short, int, or long
7232 bit-field is signed or unsigned.
7234 Naturally, we extend this to long long as well. Note that
7235 this does not include wchar_t. */
7236 || (bitfield && !flag_signed_bitfields
7237 && !signed_p
7238 /* A typedef for plain `int' without `signed' can be
7239 controlled just like plain `int', but a typedef for
7240 `signed int' cannot be so controlled. */
7241 && !(typedef_decl
7242 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
7243 && TREE_CODE (type) == INTEGER_TYPE
7244 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
7246 if (longlong)
7247 type = long_long_unsigned_type_node;
7248 else if (long_p)
7249 type = long_unsigned_type_node;
7250 else if (short_p)
7251 type = short_unsigned_type_node;
7252 else if (type == char_type_node)
7253 type = unsigned_char_type_node;
7254 else if (typedef_decl)
7255 type = c_common_unsigned_type (type);
7256 else
7257 type = unsigned_type_node;
7259 else if (signed_p && type == char_type_node)
7260 type = signed_char_type_node;
7261 else if (longlong)
7262 type = long_long_integer_type_node;
7263 else if (long_p)
7264 type = long_integer_type_node;
7265 else if (short_p)
7266 type = short_integer_type_node;
7268 if (declspecs->specs[(int)ds_complex])
7270 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
7271 error ("complex invalid for %qs", name);
7272 /* If we just have "complex", it is equivalent to
7273 "complex double", but if any modifiers at all are specified it is
7274 the complex form of TYPE. E.g, "complex short" is
7275 "complex short int". */
7277 else if (defaulted_int && ! longlong
7278 && ! (long_p || short_p || signed_p || unsigned_p))
7279 type = complex_double_type_node;
7280 else if (type == integer_type_node)
7281 type = complex_integer_type_node;
7282 else if (type == float_type_node)
7283 type = complex_float_type_node;
7284 else if (type == double_type_node)
7285 type = complex_double_type_node;
7286 else if (type == long_double_type_node)
7287 type = complex_long_double_type_node;
7288 else
7289 type = build_complex_type (type);
7292 type_quals = TYPE_UNQUALIFIED;
7293 if (declspecs->specs[(int)ds_const])
7294 type_quals |= TYPE_QUAL_CONST;
7295 if (declspecs->specs[(int)ds_volatile])
7296 type_quals |= TYPE_QUAL_VOLATILE;
7297 if (declspecs->specs[(int)ds_restrict])
7298 type_quals |= TYPE_QUAL_RESTRICT;
7299 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
7300 error ("qualifiers are not allowed on declaration of %<operator %T%>",
7301 ctor_return_type);
7303 if (TREE_CODE (type) == FUNCTION_TYPE
7304 && type_quals != TYPE_UNQUALIFIED)
7306 /* This was an error in C++98 (cv-qualifiers cannot be added to
7307 a function type), but DR 295 makes the code well-formed by
7308 dropping the extra qualifiers. */
7309 if (pedantic)
7311 tree bad_type = build_qualified_type (type, type_quals);
7312 pedwarn ("ignoring %qV qualifiers added to function type %qT",
7313 bad_type, type);
7315 type_quals = TYPE_UNQUALIFIED;
7317 type_quals |= cp_type_quals (type);
7318 type = cp_build_qualified_type_real
7319 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
7320 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
7321 /* We might have ignored or rejected some of the qualifiers. */
7322 type_quals = cp_type_quals (type);
7324 staticp = 0;
7325 inlinep = !! declspecs->specs[(int)ds_inline];
7326 virtualp = !! declspecs->specs[(int)ds_virtual];
7327 explicitp = !! declspecs->specs[(int)ds_explicit];
7329 storage_class = declspecs->storage_class;
7330 if (storage_class == sc_static)
7331 staticp = 1 + (decl_context == FIELD);
7333 if (virtualp && staticp == 2)
7335 error ("member %qD cannot be declared both virtual and static", dname);
7336 storage_class = sc_none;
7337 staticp = 0;
7339 friendp = !! declspecs->specs[(int)ds_friend];
7341 if (dependent_name && !friendp)
7343 error ("%<%T::%D%> is not a valid declarator", ctype, dependent_name);
7344 return error_mark_node;
7347 /* Issue errors about use of storage classes for parameters. */
7348 if (decl_context == PARM)
7350 if (declspecs->specs[(int)ds_typedef])
7352 error ("typedef declaration invalid in parameter declaration");
7353 return error_mark_node;
7355 else if (storage_class == sc_static
7356 || storage_class == sc_extern
7357 || thread_p)
7358 error ("storage class specifiers invalid in parameter declarations");
7361 /* Give error if `virtual' is used outside of class declaration. */
7362 if (virtualp
7363 && (current_class_name == NULL_TREE || decl_context != FIELD))
7365 error ("virtual outside class declaration");
7366 virtualp = 0;
7369 /* Static anonymous unions are dealt with here. */
7370 if (staticp && decl_context == TYPENAME
7371 && declspecs->type
7372 && ANON_AGGR_TYPE_P (declspecs->type))
7373 decl_context = FIELD;
7375 /* Warn about storage classes that are invalid for certain
7376 kinds of declarations (parameters, typenames, etc.). */
7377 if (declspecs->conflicting_specifiers_p)
7379 error ("conflicting specifiers in declaration of %qs", name);
7380 storage_class = sc_none;
7382 else if (thread_p
7383 && ((storage_class
7384 && storage_class != sc_extern
7385 && storage_class != sc_static)
7386 || declspecs->specs[(int)ds_typedef]))
7388 error ("multiple storage classes in declaration of %qs", name);
7389 thread_p = false;
7391 else if (decl_context != NORMAL
7392 && ((storage_class != sc_none
7393 && storage_class != sc_mutable)
7394 || thread_p))
7396 if ((decl_context == PARM || decl_context == CATCHPARM)
7397 && (storage_class == sc_register
7398 || storage_class == sc_auto))
7400 else if (declspecs->specs[(int)ds_typedef])
7402 else if (decl_context == FIELD
7403 /* C++ allows static class elements. */
7404 && storage_class == sc_static)
7405 /* C++ also allows inlines and signed and unsigned elements,
7406 but in those cases we don't come in here. */
7408 else
7410 if (decl_context == FIELD)
7411 error ("storage class specified for %qs", name);
7412 else
7414 if (decl_context == PARM || decl_context == CATCHPARM)
7415 error ("storage class specified for parameter %qs", name);
7416 else
7417 error ("storage class specified for typename");
7419 if (storage_class == sc_register
7420 || storage_class == sc_auto
7421 || storage_class == sc_extern
7422 || thread_p)
7423 storage_class = sc_none;
7426 else if (storage_class == sc_extern && initialized
7427 && !funcdef_flag)
7429 if (toplevel_bindings_p ())
7431 /* It's common practice (and completely valid) to have a const
7432 be initialized and declared extern. */
7433 if (!(type_quals & TYPE_QUAL_CONST))
7434 warning (0, "%qs initialized and declared %<extern%>", name);
7436 else
7437 error ("%qs has both %<extern%> and initializer", name);
7439 else if (storage_class == sc_extern && funcdef_flag
7440 && ! toplevel_bindings_p ())
7441 error ("nested function %qs declared %<extern%>", name);
7442 else if (toplevel_bindings_p ())
7444 if (storage_class == sc_auto)
7445 error ("top-level declaration of %qs specifies %<auto%>", name);
7447 else if (thread_p
7448 && storage_class != sc_extern
7449 && storage_class != sc_static)
7451 error ("function-scope %qs implicitly auto and declared %<__thread%>",
7452 name);
7453 thread_p = false;
7456 if (storage_class && friendp)
7457 error ("storage class specifiers invalid in friend function declarations");
7459 if (!id_declarator)
7460 unqualified_id = NULL_TREE;
7461 else
7463 unqualified_id = id_declarator->u.id.unqualified_name;
7464 switch (TREE_CODE (unqualified_id))
7466 case BIT_NOT_EXPR:
7467 unqualified_id
7468 = constructor_name (TREE_OPERAND (unqualified_id, 0));
7469 break;
7471 case IDENTIFIER_NODE:
7472 case TEMPLATE_ID_EXPR:
7473 break;
7475 default:
7476 gcc_unreachable ();
7480 /* Determine the type of the entity declared by recurring on the
7481 declarator. */
7482 for (; declarator; declarator = declarator->declarator)
7484 const cp_declarator *inner_declarator;
7485 tree attrs;
7487 if (type == error_mark_node)
7488 return error_mark_node;
7490 attrs = declarator->attributes;
7491 if (attrs)
7493 int attr_flags;
7495 attr_flags = 0;
7496 if (declarator == NULL || declarator->kind == cdk_id)
7497 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7498 if (declarator->kind == cdk_function)
7499 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7500 if (declarator->kind == cdk_array)
7501 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7502 returned_attrs = decl_attributes (&type,
7503 chainon (returned_attrs, attrs),
7504 attr_flags);
7507 if (declarator->kind == cdk_id)
7508 break;
7510 inner_declarator = declarator->declarator;
7512 switch (declarator->kind)
7514 case cdk_array:
7515 type = create_array_type_for_decl (dname, type,
7516 declarator->u.array.bounds);
7517 break;
7519 case cdk_function:
7521 tree arg_types;
7522 int funcdecl_p;
7524 /* Declaring a function type.
7525 Make sure we have a valid type for the function to return. */
7527 /* We now know that the TYPE_QUALS don't apply to the
7528 decl, but to its return type. */
7529 type_quals = TYPE_UNQUALIFIED;
7531 /* Warn about some types functions can't return. */
7533 if (TREE_CODE (type) == FUNCTION_TYPE)
7535 error ("%qs declared as function returning a function", name);
7536 type = integer_type_node;
7538 if (TREE_CODE (type) == ARRAY_TYPE)
7540 error ("%qs declared as function returning an array", name);
7541 type = integer_type_node;
7544 /* Pick up type qualifiers which should be applied to `this'. */
7545 memfn_quals = declarator->u.function.qualifiers;
7547 /* Pick up the exception specifications. */
7548 raises = declarator->u.function.exception_specification;
7550 /* Say it's a definition only for the CALL_EXPR
7551 closest to the identifier. */
7552 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
7554 if (ctype == NULL_TREE
7555 && decl_context == FIELD
7556 && funcdecl_p
7557 && (friendp == 0 || dname == current_class_name))
7558 ctype = current_class_type;
7560 if (ctype && (sfk == sfk_constructor
7561 || sfk == sfk_destructor))
7563 /* We are within a class's scope. If our declarator name
7564 is the same as the class name, and we are defining
7565 a function, then it is a constructor/destructor, and
7566 therefore returns a void type. */
7568 /* ISO C++ 12.4/2. A destructor may not be declared
7569 const or volatile. A destructor may not be
7570 static.
7572 ISO C++ 12.1. A constructor may not be declared
7573 const or volatile. A constructor may not be
7574 virtual. A constructor may not be static. */
7575 if (staticp == 2)
7576 error ((flags == DTOR_FLAG)
7577 ? "destructor cannot be static member function"
7578 : "constructor cannot be static member function");
7579 if (memfn_quals)
7581 error ((flags == DTOR_FLAG)
7582 ? "destructors may not be cv-qualified"
7583 : "constructors may not be cv-qualified");
7584 memfn_quals = TYPE_UNQUALIFIED;
7587 if (decl_context == FIELD
7588 && !member_function_or_else (ctype,
7589 current_class_type,
7590 flags))
7591 return error_mark_node;
7593 if (flags != DTOR_FLAG)
7595 /* It's a constructor. */
7596 if (explicitp == 1)
7597 explicitp = 2;
7598 if (virtualp)
7600 pedwarn ("constructors cannot be declared virtual");
7601 virtualp = 0;
7603 if (decl_context == FIELD
7604 && sfk != sfk_constructor)
7605 return error_mark_node;
7607 if (decl_context == FIELD)
7608 staticp = 0;
7610 else if (friendp)
7612 if (initialized)
7613 error ("can't initialize friend function %qs", name);
7614 if (virtualp)
7616 /* Cannot be both friend and virtual. */
7617 error ("virtual functions cannot be friends");
7618 friendp = 0;
7620 if (decl_context == NORMAL)
7621 error ("friend declaration not in class definition");
7622 if (current_function_decl && funcdef_flag)
7623 error ("can't define friend function %qs in a local "
7624 "class definition",
7625 name);
7628 arg_types = grokparms (declarator->u.function.parameters,
7629 &parms);
7631 if (inner_declarator
7632 && inner_declarator->kind == cdk_id
7633 && inner_declarator->u.id.sfk == sfk_destructor
7634 && arg_types != void_list_node)
7636 error ("destructors may not have parameters");
7637 arg_types = void_list_node;
7638 parms = NULL_TREE;
7641 type = build_function_type (type, arg_types);
7643 break;
7645 case cdk_pointer:
7646 case cdk_reference:
7647 case cdk_ptrmem:
7648 /* Filter out pointers-to-references and references-to-references.
7649 We can get these if a TYPE_DECL is used. */
7651 if (TREE_CODE (type) == REFERENCE_TYPE)
7653 error (declarator->kind == cdk_reference
7654 ? "cannot declare reference to %q#T"
7655 : "cannot declare pointer to %q#T", type);
7656 type = TREE_TYPE (type);
7658 else if (VOID_TYPE_P (type))
7660 if (declarator->kind == cdk_reference)
7661 error ("cannot declare reference to %q#T", type);
7662 else if (declarator->kind == cdk_ptrmem)
7663 error ("cannot declare pointer to %q#T member", type);
7666 /* We now know that the TYPE_QUALS don't apply to the decl,
7667 but to the target of the pointer. */
7668 type_quals = TYPE_UNQUALIFIED;
7670 if (declarator->kind == cdk_ptrmem
7671 && (TREE_CODE (type) == FUNCTION_TYPE || memfn_quals))
7673 memfn_quals |= cp_type_quals (type);
7674 type = build_memfn_type (type,
7675 declarator->u.pointer.class_type,
7676 memfn_quals);
7677 memfn_quals = TYPE_UNQUALIFIED;
7680 if (declarator->kind == cdk_reference)
7682 if (!VOID_TYPE_P (type))
7683 type = build_reference_type (type);
7685 else if (TREE_CODE (type) == METHOD_TYPE)
7686 type = build_ptrmemfunc_type (build_pointer_type (type));
7687 else if (declarator->kind == cdk_ptrmem)
7689 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
7690 != NAMESPACE_DECL);
7691 if (declarator->u.pointer.class_type == error_mark_node)
7692 /* We will already have complained. */
7693 type = error_mark_node;
7694 else
7695 type = build_ptrmem_type (declarator->u.pointer.class_type,
7696 type);
7698 else
7699 type = build_pointer_type (type);
7701 /* Process a list of type modifier keywords (such as
7702 const or volatile) that were given inside the `*' or `&'. */
7704 if (declarator->u.pointer.qualifiers)
7706 type
7707 = cp_build_qualified_type (type,
7708 declarator->u.pointer.qualifiers);
7709 type_quals = cp_type_quals (type);
7711 ctype = NULL_TREE;
7712 break;
7714 case cdk_error:
7715 break;
7717 default:
7718 gcc_unreachable ();
7722 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
7723 && TREE_CODE (type) != FUNCTION_TYPE
7724 && TREE_CODE (type) != METHOD_TYPE)
7726 error ("template-id %qD used as a declarator",
7727 unqualified_id);
7728 unqualified_id = dname;
7731 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
7732 qualified with a class-name, turn it into a METHOD_TYPE, unless
7733 we know that the function is static. We take advantage of this
7734 opportunity to do other processing that pertains to entities
7735 explicitly declared to be class members. Note that if DECLARATOR
7736 is non-NULL, we know it is a cdk_id declarator; otherwise, we
7737 would not have exited the loop above. */
7738 if (declarator
7739 && declarator->u.id.qualifying_scope
7740 && TYPE_P (declarator->u.id.qualifying_scope))
7742 tree t;
7744 ctype = declarator->u.id.qualifying_scope;
7745 ctype = TYPE_MAIN_VARIANT (ctype);
7746 t = ctype;
7747 while (t != NULL_TREE && CLASS_TYPE_P (t))
7749 /* You're supposed to have one `template <...>' for every
7750 template class, but you don't need one for a full
7751 specialization. For example:
7753 template <class T> struct S{};
7754 template <> struct S<int> { void f(); };
7755 void S<int>::f () {}
7757 is correct; there shouldn't be a `template <>' for the
7758 definition of `S<int>::f'. */
7759 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)
7760 && !any_dependent_template_arguments_p (CLASSTYPE_TI_ARGS (t)))
7761 /* T is an explicit (not partial) specialization. All
7762 containing classes must therefore also be explicitly
7763 specialized. */
7764 break;
7765 if ((CLASSTYPE_USE_TEMPLATE (t) || CLASSTYPE_IS_TEMPLATE (t))
7766 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
7767 template_count += 1;
7769 t = TYPE_MAIN_DECL (t);
7770 t = DECL_CONTEXT (t);
7773 if (ctype == current_class_type)
7775 if (friendp)
7776 pedwarn ("member functions are implicitly friends of their class");
7777 else
7778 pedwarn ("extra qualification %<%T::%> on member %qs",
7779 ctype, name);
7781 else if (/* If the qualifying type is already complete, then we
7782 can skip the following checks. */
7783 !COMPLETE_TYPE_P (ctype)
7784 && (/* If the function is being defined, then
7785 qualifying type must certainly be complete. */
7786 funcdef_flag
7787 /* A friend declaration of "T::f" is OK, even if
7788 "T" is a template parameter. But, if this
7789 function is not a friend, the qualifying type
7790 must be a class. */
7791 || (!friendp && !CLASS_TYPE_P (ctype))
7792 /* For a declaration, the type need not be
7793 complete, if either it is dependent (since there
7794 is no meaningful definition of complete in that
7795 case) or the qualifying class is currently being
7796 defined. */
7797 || !(dependent_type_p (ctype)
7798 || currently_open_class (ctype)))
7799 /* Check that the qualifying type is complete. */
7800 && !complete_type_or_else (ctype, NULL_TREE))
7801 return error_mark_node;
7802 else if (TREE_CODE (type) == FUNCTION_TYPE)
7804 tree sname = declarator->u.id.unqualified_name;
7806 if (current_class_type
7807 && (!friendp || funcdef_flag))
7809 error (funcdef_flag
7810 ? "cannot define member function %<%T::%s%> within %<%T%>"
7811 : "cannot declare member function %<%T::%s%> within %<%T%>",
7812 ctype, name, current_class_type);
7813 return error_mark_node;
7816 if (TREE_CODE (sname) == IDENTIFIER_NODE
7817 && NEW_DELETE_OPNAME_P (sname))
7818 /* Overloaded operator new and operator delete
7819 are always static functions. */
7821 else
7822 type = build_memfn_type (type, ctype, memfn_quals);
7824 else if (declspecs->specs[(int)ds_typedef]
7825 && current_class_type)
7827 error ("cannot declare member %<%T::%s%> within %qT",
7828 ctype, name, current_class_type);
7829 return error_mark_node;
7833 /* Now TYPE has the actual type. */
7835 if (returned_attrs)
7837 if (attrlist)
7838 *attrlist = chainon (returned_attrs, *attrlist);
7839 else
7840 attrlist = &returned_attrs;
7843 /* Did array size calculations overflow? */
7845 if (TREE_CODE (type) == ARRAY_TYPE
7846 && COMPLETE_TYPE_P (type)
7847 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7848 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
7850 error ("size of array %qs is too large", name);
7851 /* If we proceed with the array type as it is, we'll eventually
7852 crash in tree_low_cst(). */
7853 type = error_mark_node;
7856 if ((decl_context == FIELD || decl_context == PARM)
7857 && !processing_template_decl
7858 && variably_modified_type_p (type, NULL_TREE))
7860 if (decl_context == FIELD)
7861 error ("data member may not have variably modified type %qT", type);
7862 else
7863 error ("parameter may not have variably modified type %qT", type);
7864 type = error_mark_node;
7867 if (explicitp == 1 || (explicitp && friendp))
7869 /* [dcl.fct.spec] The explicit specifier shall only be used in
7870 declarations of constructors within a class definition. */
7871 error ("only declarations of constructors can be %<explicit%>");
7872 explicitp = 0;
7875 if (storage_class == sc_mutable)
7877 if (decl_context != FIELD || friendp)
7879 error ("non-member %qs cannot be declared %<mutable%>", name);
7880 storage_class = sc_none;
7882 else if (decl_context == TYPENAME || declspecs->specs[(int)ds_typedef])
7884 error ("non-object member %qs cannot be declared %<mutable%>", name);
7885 storage_class = sc_none;
7887 else if (TREE_CODE (type) == FUNCTION_TYPE
7888 || TREE_CODE (type) == METHOD_TYPE)
7890 error ("function %qs cannot be declared %<mutable%>", name);
7891 storage_class = sc_none;
7893 else if (staticp)
7895 error ("static %qs cannot be declared %<mutable%>", name);
7896 storage_class = sc_none;
7898 else if (type_quals & TYPE_QUAL_CONST)
7900 error ("const %qs cannot be declared %<mutable%>", name);
7901 storage_class = sc_none;
7905 /* If this is declaring a typedef name, return a TYPE_DECL. */
7906 if (declspecs->specs[(int)ds_typedef] && decl_context != TYPENAME)
7908 tree decl;
7910 /* Note that the grammar rejects storage classes
7911 in typenames, fields or parameters. */
7912 if (current_lang_name == lang_name_java)
7913 TYPE_FOR_JAVA (type) = 1;
7915 /* This declaration:
7917 typedef void f(int) const;
7919 declares a function type which is not a member of any
7920 particular class, but which is cv-qualified; for
7921 example "f S::*" declares a pointer to a const-qualified
7922 member function of S. We record the cv-qualification in the
7923 function type. */
7924 if (memfn_quals && TREE_CODE (type) == FUNCTION_TYPE)
7925 type = cp_build_qualified_type (type, memfn_quals);
7927 if (decl_context == FIELD)
7928 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
7929 else
7930 decl = build_decl (TYPE_DECL, unqualified_id, type);
7931 if (id_declarator && declarator->u.id.qualifying_scope)
7932 error ("%Jtypedef name may not be a nested-name-specifier", decl);
7934 if (decl_context != FIELD)
7936 if (!current_function_decl)
7937 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
7938 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
7939 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
7940 (current_function_decl)))
7941 /* The TYPE_DECL is "abstract" because there will be
7942 clones of this constructor/destructor, and there will
7943 be copies of this TYPE_DECL generated in those
7944 clones. */
7945 DECL_ABSTRACT (decl) = 1;
7947 else if (constructor_name_p (unqualified_id, current_class_type))
7948 pedwarn ("ISO C++ forbids nested type %qD with same name "
7949 "as enclosing class",
7950 unqualified_id);
7952 /* If the user declares "typedef struct {...} foo" then the
7953 struct will have an anonymous name. Fill that name in now.
7954 Nothing can refer to it, so nothing needs know about the name
7955 change. */
7956 if (type != error_mark_node
7957 && unqualified_id
7958 && TYPE_NAME (type)
7959 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7960 && TYPE_ANONYMOUS_P (type)
7961 /* Don't do this if there are attributes. */
7962 && (!attrlist || !*attrlist)
7963 && cp_type_quals (type) == TYPE_UNQUALIFIED)
7965 tree oldname = TYPE_NAME (type);
7966 tree t;
7968 /* Replace the anonymous name with the real name everywhere. */
7969 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7970 if (TYPE_NAME (t) == oldname)
7971 TYPE_NAME (t) = decl;
7973 if (TYPE_LANG_SPECIFIC (type))
7974 TYPE_WAS_ANONYMOUS (type) = 1;
7976 /* If this is a typedef within a template class, the nested
7977 type is a (non-primary) template. The name for the
7978 template needs updating as well. */
7979 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
7980 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
7981 = TYPE_IDENTIFIER (type);
7983 /* FIXME remangle member functions; member functions of a
7984 type with external linkage have external linkage. */
7987 /* Any qualifiers on a function type typedef have already been
7988 dealt with. */
7989 if (memfn_quals && !ctype && TREE_CODE (type) == FUNCTION_TYPE)
7990 memfn_quals = TYPE_UNQUALIFIED;
7992 if (signed_p
7993 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
7994 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7996 bad_specifiers (decl, "type", virtualp,
7997 memfn_quals != TYPE_UNQUALIFIED,
7998 inlinep, friendp, raises != NULL_TREE);
8000 return decl;
8003 /* Detect the case of an array type of unspecified size
8004 which came, as such, direct from a typedef name.
8005 We must copy the type, so that the array's domain can be
8006 individually set by the object's initializer. */
8008 if (type && typedef_type
8009 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
8010 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
8011 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
8013 /* Detect where we're using a typedef of function type to declare a
8014 function. PARMS will not be set, so we must create it now. */
8016 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
8018 tree decls = NULL_TREE;
8019 tree args;
8021 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
8023 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
8025 TREE_CHAIN (decl) = decls;
8026 decls = decl;
8029 parms = nreverse (decls);
8031 if (decl_context != TYPENAME)
8033 /* A cv-qualifier-seq shall only be part of the function type
8034 for a non-static member function. [8.3.5/4 dcl.fct] */
8035 if (cp_type_quals (type) != TYPE_UNQUALIFIED
8036 && (current_class_type == NULL_TREE || staticp) )
8038 error ("qualified function types cannot be used to declare %s functions",
8039 (staticp? "static member" : "free"));
8040 type = TYPE_MAIN_VARIANT (type);
8043 /* The qualifiers on the function type become the qualifiers on
8044 the non-static member function. */
8045 memfn_quals |= cp_type_quals (type);
8049 /* If this is a type name (such as, in a cast or sizeof),
8050 compute the type and return it now. */
8052 if (decl_context == TYPENAME)
8054 /* Note that the grammar rejects storage classes
8055 in typenames, fields or parameters. */
8056 if (type_quals != TYPE_UNQUALIFIED)
8057 type_quals = TYPE_UNQUALIFIED;
8059 /* Special case: "friend class foo" looks like a TYPENAME context. */
8060 if (friendp)
8062 if (type_quals != TYPE_UNQUALIFIED)
8064 error ("type qualifiers specified for friend class declaration");
8065 type_quals = TYPE_UNQUALIFIED;
8067 if (inlinep)
8069 error ("%<inline%> specified for friend class declaration");
8070 inlinep = 0;
8073 if (!current_aggr)
8075 /* Don't allow friend declaration without a class-key. */
8076 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
8077 pedwarn ("template parameters cannot be friends");
8078 else if (TREE_CODE (type) == TYPENAME_TYPE)
8079 pedwarn ("friend declaration requires class-key, "
8080 "i.e. %<friend class %T::%D%>",
8081 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
8082 else
8083 pedwarn ("friend declaration requires class-key, "
8084 "i.e. %<friend %#T%>",
8085 type);
8088 /* Only try to do this stuff if we didn't already give up. */
8089 if (type != integer_type_node)
8091 /* A friendly class? */
8092 if (current_class_type)
8093 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
8094 /*complain=*/true);
8095 else
8096 error ("trying to make class %qT a friend of global scope",
8097 type);
8099 type = void_type_node;
8102 else if (memfn_quals)
8104 if (ctype == NULL_TREE)
8106 if (TREE_CODE (type) != METHOD_TYPE)
8107 error ("invalid qualifiers on non-member function type");
8108 else
8109 ctype = TYPE_METHOD_BASETYPE (type);
8111 if (ctype)
8112 type = build_memfn_type (type, ctype, memfn_quals);
8115 return type;
8117 else if (unqualified_id == NULL_TREE && decl_context != PARM
8118 && decl_context != CATCHPARM
8119 && TREE_CODE (type) != UNION_TYPE
8120 && ! bitfield)
8122 error ("abstract declarator %qT used as declaration", type);
8123 return error_mark_node;
8126 /* Only functions may be declared using an operator-function-id. */
8127 if (unqualified_id
8128 && IDENTIFIER_OPNAME_P (unqualified_id)
8129 && TREE_CODE (type) != FUNCTION_TYPE
8130 && TREE_CODE (type) != METHOD_TYPE)
8132 error ("declaration of %qD as non-function", unqualified_id);
8133 return error_mark_node;
8136 /* We don't check parameter types here because we can emit a better
8137 error message later. */
8138 if (decl_context != PARM)
8140 type = check_var_type (unqualified_id, type);
8141 if (type == error_mark_node)
8142 return error_mark_node;
8145 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
8146 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
8148 if (decl_context == PARM || decl_context == CATCHPARM)
8150 if (ctype || in_namespace)
8151 error ("cannot use %<::%> in parameter declaration");
8153 /* A parameter declared as an array of T is really a pointer to T.
8154 One declared as a function is really a pointer to a function.
8155 One declared as a member is really a pointer to member. */
8157 if (TREE_CODE (type) == ARRAY_TYPE)
8159 /* Transfer const-ness of array into that of type pointed to. */
8160 type = build_pointer_type (TREE_TYPE (type));
8161 type_quals = TYPE_UNQUALIFIED;
8163 else if (TREE_CODE (type) == FUNCTION_TYPE)
8164 type = build_pointer_type (type);
8168 tree decl;
8170 if (decl_context == PARM)
8172 decl = cp_build_parm_decl (unqualified_id, type);
8174 bad_specifiers (decl, "parameter", virtualp,
8175 memfn_quals != TYPE_UNQUALIFIED,
8176 inlinep, friendp, raises != NULL_TREE);
8178 else if (decl_context == FIELD)
8180 /* The C99 flexible array extension. */
8181 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
8182 && TYPE_DOMAIN (type) == NULL_TREE)
8184 tree itype = compute_array_index_type (dname, integer_zero_node);
8185 type = build_cplus_array_type (TREE_TYPE (type), itype);
8188 if (type == error_mark_node)
8190 /* Happens when declaring arrays of sizes which
8191 are error_mark_node, for example. */
8192 decl = NULL_TREE;
8194 else if (in_namespace && !friendp)
8196 /* Something like struct S { int N::j; }; */
8197 error ("invalid use of %<::%>");
8198 return error_mark_node;
8200 else if (TREE_CODE (type) == FUNCTION_TYPE)
8202 int publicp = 0;
8203 tree function_context;
8205 if (friendp == 0)
8207 if (ctype == NULL_TREE)
8208 ctype = current_class_type;
8210 if (ctype == NULL_TREE)
8212 error ("can't make %qD into a method -- not in a class",
8213 unqualified_id);
8214 return error_mark_node;
8217 /* ``A union may [ ... ] not [ have ] virtual functions.''
8218 ARM 9.5 */
8219 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
8221 error ("function %qD declared virtual inside a union",
8222 unqualified_id);
8223 return error_mark_node;
8226 if (NEW_DELETE_OPNAME_P (unqualified_id))
8228 if (virtualp)
8230 error ("%qD cannot be declared virtual, since it "
8231 "is always static",
8232 unqualified_id);
8233 virtualp = 0;
8236 else if (staticp < 2)
8237 type = build_memfn_type (type, ctype, memfn_quals);
8240 /* Check that the name used for a destructor makes sense. */
8241 if (sfk == sfk_destructor)
8243 if (!ctype)
8245 gcc_assert (friendp);
8246 error ("expected qualified name in friend declaration "
8247 "for destructor %qD",
8248 id_declarator->u.id.unqualified_name);
8249 return error_mark_node;
8252 if (!same_type_p (TREE_OPERAND
8253 (id_declarator->u.id.unqualified_name, 0),
8254 ctype))
8256 error ("declaration of %qD as member of %qT",
8257 id_declarator->u.id.unqualified_name, ctype);
8258 return error_mark_node;
8262 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
8263 function_context = (ctype != NULL_TREE) ?
8264 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
8265 publicp = (! friendp || ! staticp)
8266 && function_context == NULL_TREE;
8267 decl = grokfndecl (ctype, type,
8268 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
8269 ? unqualified_id : dname,
8270 parms,
8271 unqualified_id,
8272 virtualp, flags, memfn_quals, raises,
8273 friendp ? -1 : 0, friendp, publicp, inlinep,
8274 sfk,
8275 funcdef_flag, template_count, in_namespace, attrlist);
8276 if (decl == NULL_TREE)
8277 return error_mark_node;
8278 #if 0
8279 /* This clobbers the attrs stored in `decl' from `attrlist'. */
8280 /* The decl and setting of decl_attr is also turned off. */
8281 decl = build_decl_attribute_variant (decl, decl_attr);
8282 #endif
8284 /* [class.conv.ctor]
8286 A constructor declared without the function-specifier
8287 explicit that can be called with a single parameter
8288 specifies a conversion from the type of its first
8289 parameter to the type of its class. Such a constructor
8290 is called a converting constructor. */
8291 if (explicitp == 2)
8292 DECL_NONCONVERTING_P (decl) = 1;
8293 else if (DECL_CONSTRUCTOR_P (decl))
8295 /* The constructor can be called with exactly one
8296 parameter if there is at least one parameter, and
8297 any subsequent parameters have default arguments.
8298 Ignore any compiler-added parms. */
8299 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
8301 if (arg_types == void_list_node
8302 || (arg_types
8303 && TREE_CHAIN (arg_types)
8304 && TREE_CHAIN (arg_types) != void_list_node
8305 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
8306 DECL_NONCONVERTING_P (decl) = 1;
8309 else if (TREE_CODE (type) == METHOD_TYPE)
8311 /* We only get here for friend declarations of
8312 members of other classes. */
8313 /* All method decls are public, so tell grokfndecl to set
8314 TREE_PUBLIC, also. */
8315 decl = grokfndecl (ctype, type,
8316 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
8317 ? unqualified_id : dname,
8318 parms,
8319 unqualified_id,
8320 virtualp, flags, memfn_quals, raises,
8321 friendp ? -1 : 0, friendp, 1, 0, sfk,
8322 funcdef_flag, template_count, in_namespace,
8323 attrlist);
8324 if (decl == NULL_TREE)
8325 return error_mark_node;
8327 else if (!staticp && !dependent_type_p (type)
8328 && !COMPLETE_TYPE_P (complete_type (type))
8329 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
8331 if (unqualified_id)
8332 error ("field %qD has incomplete type", unqualified_id);
8333 else
8334 error ("name %qT has incomplete type", type);
8336 /* If we're instantiating a template, tell them which
8337 instantiation made the field's type be incomplete. */
8338 if (current_class_type
8339 && TYPE_NAME (current_class_type)
8340 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8341 && declspecs->type
8342 && declspecs->type == type)
8343 error (" in instantiation of template %qT",
8344 current_class_type);
8346 return error_mark_node;
8348 else
8350 if (friendp)
8352 error ("%qE is neither function nor member function; "
8353 "cannot be declared friend", unqualified_id);
8354 friendp = 0;
8356 decl = NULL_TREE;
8359 if (friendp)
8361 /* Friends are treated specially. */
8362 if (ctype == current_class_type)
8363 ; /* We already issued a pedwarn. */
8364 else if (decl && DECL_NAME (decl))
8366 if (template_class_depth (current_class_type) == 0)
8368 decl = check_explicit_specialization
8369 (unqualified_id, decl, template_count,
8370 2 * funcdef_flag + 4);
8371 if (decl == error_mark_node)
8372 return error_mark_node;
8375 decl = do_friend (ctype, unqualified_id, decl,
8376 *attrlist, flags,
8377 funcdef_flag);
8378 return decl;
8380 else
8381 return error_mark_node;
8384 /* Structure field. It may not be a function, except for C++. */
8386 if (decl == NULL_TREE)
8388 if (initialized)
8390 if (!staticp)
8392 /* An attempt is being made to initialize a non-static
8393 member. But, from [class.mem]:
8395 4 A member-declarator can contain a
8396 constant-initializer only if it declares a static
8397 member (_class.static_) of integral or enumeration
8398 type, see _class.static.data_.
8400 This used to be relatively common practice, but
8401 the rest of the compiler does not correctly
8402 handle the initialization unless the member is
8403 static so we make it static below. */
8404 pedwarn ("ISO C++ forbids initialization of member %qD",
8405 unqualified_id);
8406 pedwarn ("making %qD static", unqualified_id);
8407 staticp = 1;
8410 if (uses_template_parms (type))
8411 /* We'll check at instantiation time. */
8413 else if (check_static_variable_definition (unqualified_id,
8414 type))
8415 /* If we just return the declaration, crashes
8416 will sometimes occur. We therefore return
8417 void_type_node, as if this was a friend
8418 declaration, to cause callers to completely
8419 ignore this declaration. */
8420 return error_mark_node;
8423 if (staticp)
8425 /* C++ allows static class members. All other work
8426 for this is done by grokfield. */
8427 decl = build_lang_decl (VAR_DECL, unqualified_id, type);
8428 set_linkage_for_static_data_member (decl);
8429 /* Even if there is an in-class initialization, DECL
8430 is considered undefined until an out-of-class
8431 definition is provided. */
8432 DECL_EXTERNAL (decl) = 1;
8434 if (thread_p)
8436 if (targetm.have_tls)
8437 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
8438 else
8439 /* A mere warning is sure to result in improper
8440 semantics at runtime. Don't bother to allow this to
8441 compile. */
8442 error ("thread-local storage not supported for this target");
8445 else
8447 decl = build_decl (FIELD_DECL, unqualified_id, type);
8448 DECL_NONADDRESSABLE_P (decl) = bitfield;
8449 if (storage_class == sc_mutable)
8451 DECL_MUTABLE_P (decl) = 1;
8452 storage_class = sc_none;
8456 bad_specifiers (decl, "field", virtualp,
8457 memfn_quals != TYPE_UNQUALIFIED,
8458 inlinep, friendp, raises != NULL_TREE);
8461 else if (TREE_CODE (type) == FUNCTION_TYPE
8462 || TREE_CODE (type) == METHOD_TYPE)
8464 tree original_name;
8465 int publicp = 0;
8467 if (!unqualified_id)
8468 return error_mark_node;
8470 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
8471 original_name = dname;
8472 else
8473 original_name = unqualified_id;
8475 if (storage_class == sc_auto)
8476 error ("storage class %<auto%> invalid for function %qs", name);
8477 else if (storage_class == sc_register)
8478 error ("storage class %<register%> invalid for function %qs", name);
8479 else if (thread_p)
8480 error ("storage class %<__thread%> invalid for function %qs", name);
8482 /* Function declaration not at top level.
8483 Storage classes other than `extern' are not allowed
8484 and `extern' makes no difference. */
8485 if (! toplevel_bindings_p ()
8486 && (storage_class == sc_static
8487 || declspecs->specs[(int)ds_inline])
8488 && pedantic)
8490 if (storage_class == sc_static)
8491 pedwarn ("%<static%> specified invalid for function %qs "
8492 "declared out of global scope", name);
8493 else
8494 pedwarn ("%<inline%> specifier invalid for function %qs "
8495 "declared out of global scope", name);
8498 if (ctype == NULL_TREE)
8500 if (virtualp)
8502 error ("virtual non-class function %qs", name);
8503 virtualp = 0;
8506 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
8507 && !NEW_DELETE_OPNAME_P (original_name))
8508 type = build_method_type_directly (ctype,
8509 TREE_TYPE (type),
8510 TYPE_ARG_TYPES (type));
8512 /* Record presence of `static'. */
8513 publicp = (ctype != NULL_TREE
8514 || storage_class == sc_extern
8515 || storage_class != sc_static);
8517 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
8518 virtualp, flags, memfn_quals, raises,
8519 1, friendp,
8520 publicp, inlinep, sfk, funcdef_flag,
8521 template_count, in_namespace, attrlist);
8522 if (decl == NULL_TREE)
8523 return error_mark_node;
8525 if (staticp == 1)
8527 int invalid_static = 0;
8529 /* Don't allow a static member function in a class, and forbid
8530 declaring main to be static. */
8531 if (TREE_CODE (type) == METHOD_TYPE)
8533 pedwarn ("cannot declare member function %qD to have "
8534 "static linkage", decl);
8535 invalid_static = 1;
8537 else if (current_function_decl)
8539 /* FIXME need arm citation */
8540 error ("cannot declare static function inside another function");
8541 invalid_static = 1;
8544 if (invalid_static)
8546 staticp = 0;
8547 storage_class = sc_none;
8551 else
8553 /* It's a variable. */
8555 /* An uninitialized decl with `extern' is a reference. */
8556 decl = grokvardecl (type, unqualified_id,
8557 declspecs,
8558 initialized,
8559 (type_quals & TYPE_QUAL_CONST) != 0,
8560 ctype ? ctype : in_namespace);
8561 bad_specifiers (decl, "variable", virtualp,
8562 memfn_quals != TYPE_UNQUALIFIED,
8563 inlinep, friendp, raises != NULL_TREE);
8565 if (ctype)
8567 DECL_CONTEXT (decl) = ctype;
8568 if (staticp == 1)
8570 pedwarn ("%<static%> may not be used when defining "
8571 "(as opposed to declaring) a static data member");
8572 staticp = 0;
8573 storage_class = sc_none;
8575 if (storage_class == sc_register && TREE_STATIC (decl))
8577 error ("static member %qD declared %<register%>", decl);
8578 storage_class = sc_none;
8580 if (storage_class == sc_extern && pedantic)
8582 pedwarn ("cannot explicitly declare member %q#D to have "
8583 "extern linkage",
8584 decl);
8585 storage_class = sc_none;
8590 /* Record `register' declaration for warnings on &
8591 and in case doing stupid register allocation. */
8593 if (storage_class == sc_register)
8594 DECL_REGISTER (decl) = 1;
8595 else if (storage_class == sc_extern)
8596 DECL_THIS_EXTERN (decl) = 1;
8597 else if (storage_class == sc_static)
8598 DECL_THIS_STATIC (decl) = 1;
8600 /* Record constancy and volatility. There's no need to do this
8601 when processing a template; we'll do this for the instantiated
8602 declaration based on the type of DECL. */
8603 if (!processing_template_decl)
8604 cp_apply_type_quals_to_decl (type_quals, decl);
8606 return decl;
8610 /* Subroutine of start_function. Ensure that each of the parameter
8611 types (as listed in PARMS) is complete, as is required for a
8612 function definition. */
8614 static void
8615 require_complete_types_for_parms (tree parms)
8617 for (; parms; parms = TREE_CHAIN (parms))
8619 if (dependent_type_p (TREE_TYPE (parms)))
8620 continue;
8621 if (!VOID_TYPE_P (TREE_TYPE (parms))
8622 && complete_type_or_else (TREE_TYPE (parms), parms))
8624 relayout_decl (parms);
8625 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
8627 else
8628 /* grokparms or complete_type_or_else will have already issued
8629 an error. */
8630 TREE_TYPE (parms) = error_mark_node;
8634 /* Returns nonzero if T is a local variable. */
8637 local_variable_p (tree t)
8639 if ((TREE_CODE (t) == VAR_DECL
8640 /* A VAR_DECL with a context that is a _TYPE is a static data
8641 member. */
8642 && !TYPE_P (CP_DECL_CONTEXT (t))
8643 /* Any other non-local variable must be at namespace scope. */
8644 && !DECL_NAMESPACE_SCOPE_P (t))
8645 || (TREE_CODE (t) == PARM_DECL))
8646 return 1;
8648 return 0;
8651 /* Returns nonzero if T is an automatic local variable or a label.
8652 (These are the declarations that need to be remapped when the code
8653 containing them is duplicated.) */
8656 nonstatic_local_decl_p (tree t)
8658 return ((local_variable_p (t) && !TREE_STATIC (t))
8659 || TREE_CODE (t) == LABEL_DECL
8660 || TREE_CODE (t) == RESULT_DECL);
8663 /* Like local_variable_p, but suitable for use as a tree-walking
8664 function. */
8666 static tree
8667 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
8668 void *data ATTRIBUTE_UNUSED)
8670 if (local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
8671 return *tp;
8672 else if (TYPE_P (*tp))
8673 *walk_subtrees = 0;
8675 return NULL_TREE;
8679 /* Check that ARG, which is a default-argument expression for a
8680 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
8681 something goes wrong. DECL may also be a _TYPE node, rather than a
8682 DECL, if there is no DECL available. */
8684 tree
8685 check_default_argument (tree decl, tree arg)
8687 tree var;
8688 tree decl_type;
8690 if (TREE_CODE (arg) == DEFAULT_ARG)
8691 /* We get a DEFAULT_ARG when looking at an in-class declaration
8692 with a default argument. Ignore the argument for now; we'll
8693 deal with it after the class is complete. */
8694 return arg;
8696 if (TYPE_P (decl))
8698 decl_type = decl;
8699 decl = NULL_TREE;
8701 else
8702 decl_type = TREE_TYPE (decl);
8704 if (arg == error_mark_node
8705 || decl == error_mark_node
8706 || TREE_TYPE (arg) == error_mark_node
8707 || decl_type == error_mark_node)
8708 /* Something already went wrong. There's no need to check
8709 further. */
8710 return error_mark_node;
8712 /* [dcl.fct.default]
8714 A default argument expression is implicitly converted to the
8715 parameter type. */
8716 if (!TREE_TYPE (arg)
8717 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg, LOOKUP_NORMAL))
8719 if (decl)
8720 error ("default argument for %q#D has type %qT",
8721 decl, TREE_TYPE (arg));
8722 else
8723 error ("default argument for parameter of type %qT has type %qT",
8724 decl_type, TREE_TYPE (arg));
8726 return error_mark_node;
8729 /* [dcl.fct.default]
8731 Local variables shall not be used in default argument
8732 expressions.
8734 The keyword `this' shall not be used in a default argument of a
8735 member function. */
8736 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
8737 NULL);
8738 if (var)
8740 error ("default argument %qE uses local variable %qD", arg, var);
8741 return error_mark_node;
8744 /* All is well. */
8745 return arg;
8748 /* Decode the list of parameter types for a function type.
8749 Given the list of things declared inside the parens,
8750 return a list of types.
8752 If this parameter does not end with an ellipsis, we append
8753 void_list_node.
8755 *PARMS is set to the chain of PARM_DECLs created. */
8757 static tree
8758 grokparms (cp_parameter_declarator *first_parm, tree *parms)
8760 tree result = NULL_TREE;
8761 tree decls = NULL_TREE;
8762 int ellipsis = !first_parm || first_parm->ellipsis_p;
8763 cp_parameter_declarator *parm;
8764 int any_error = 0;
8766 for (parm = first_parm; parm != NULL; parm = parm->next)
8768 tree type = NULL_TREE;
8769 tree init = parm->default_argument;
8770 tree attrs;
8771 tree decl;
8773 if (parm == no_parameters)
8774 break;
8776 attrs = parm->decl_specifiers.attributes;
8777 parm->decl_specifiers.attributes = NULL_TREE;
8778 decl = grokdeclarator (parm->declarator, &parm->decl_specifiers,
8779 PARM, init != NULL_TREE, &attrs);
8780 if (! decl || TREE_TYPE (decl) == error_mark_node)
8781 continue;
8783 if (attrs)
8784 cplus_decl_attributes (&decl, attrs, 0);
8786 type = TREE_TYPE (decl);
8787 if (VOID_TYPE_P (type))
8789 if (same_type_p (type, void_type_node)
8790 && DECL_SELF_REFERENCE_P (type)
8791 && !DECL_NAME (decl) && !result && !parm->next && !ellipsis)
8792 /* this is a parmlist of `(void)', which is ok. */
8793 break;
8794 cxx_incomplete_type_error (decl, type);
8795 /* It's not a good idea to actually create parameters of
8796 type `void'; other parts of the compiler assume that a
8797 void type terminates the parameter list. */
8798 type = error_mark_node;
8799 TREE_TYPE (decl) = error_mark_node;
8802 if (type != error_mark_node)
8804 /* Top-level qualifiers on the parameters are
8805 ignored for function types. */
8806 type = cp_build_qualified_type (type, 0);
8807 if (TREE_CODE (type) == METHOD_TYPE)
8809 error ("parameter %qD invalidly declared method type", decl);
8810 type = build_pointer_type (type);
8811 TREE_TYPE (decl) = type;
8813 else if (abstract_virtuals_error (decl, type))
8814 any_error = 1; /* Seems like a good idea. */
8815 else if (POINTER_TYPE_P (type))
8817 /* [dcl.fct]/6, parameter types cannot contain pointers
8818 (references) to arrays of unknown bound. */
8819 tree t = TREE_TYPE (type);
8820 int ptr = TYPE_PTR_P (type);
8822 while (1)
8824 if (TYPE_PTR_P (t))
8825 ptr = 1;
8826 else if (TREE_CODE (t) != ARRAY_TYPE)
8827 break;
8828 else if (!TYPE_DOMAIN (t))
8829 break;
8830 t = TREE_TYPE (t);
8832 if (TREE_CODE (t) == ARRAY_TYPE)
8833 error ("parameter %qD includes %s to array of unknown "
8834 "bound %qT",
8835 decl, ptr ? "pointer" : "reference", t);
8838 if (any_error)
8839 init = NULL_TREE;
8840 else if (init && !processing_template_decl)
8841 init = check_default_argument (decl, init);
8844 TREE_CHAIN (decl) = decls;
8845 decls = decl;
8846 result = tree_cons (init, type, result);
8848 decls = nreverse (decls);
8849 result = nreverse (result);
8850 if (!ellipsis)
8851 result = chainon (result, void_list_node);
8852 *parms = decls;
8854 return result;
8858 /* D is a constructor or overloaded `operator='.
8860 Let T be the class in which D is declared. Then, this function
8861 returns:
8863 -1 if D's is an ill-formed constructor or copy assignment operator
8864 whose first parameter is of type `T'.
8865 0 if D is not a copy constructor or copy assignment
8866 operator.
8867 1 if D is a copy constructor or copy assignment operator whose
8868 first parameter is a reference to const qualified T.
8869 2 if D is a copy constructor or copy assignment operator whose
8870 first parameter is a reference to non-const qualified T.
8872 This function can be used as a predicate. Positive values indicate
8873 a copy constructor and nonzero values indicate a copy assignment
8874 operator. */
8877 copy_fn_p (tree d)
8879 tree args;
8880 tree arg_type;
8881 int result = 1;
8883 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
8885 if (TREE_CODE (d) == TEMPLATE_DECL
8886 || (DECL_TEMPLATE_INFO (d)
8887 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
8888 /* Instantiations of template member functions are never copy
8889 functions. Note that member functions of templated classes are
8890 represented as template functions internally, and we must
8891 accept those as copy functions. */
8892 return 0;
8894 args = FUNCTION_FIRST_USER_PARMTYPE (d);
8895 if (!args)
8896 return 0;
8898 arg_type = TREE_VALUE (args);
8899 if (arg_type == error_mark_node)
8900 return 0;
8902 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
8904 /* Pass by value copy assignment operator. */
8905 result = -1;
8907 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
8908 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
8910 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
8911 result = 2;
8913 else
8914 return 0;
8916 args = TREE_CHAIN (args);
8918 if (args && args != void_list_node && !TREE_PURPOSE (args))
8919 /* There are more non-optional args. */
8920 return 0;
8922 return result;
8925 /* Remember any special properties of member function DECL. */
8927 void grok_special_member_properties (tree decl)
8929 tree class_type;
8931 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
8932 return;
8934 class_type = DECL_CONTEXT (decl);
8935 if (DECL_CONSTRUCTOR_P (decl))
8937 int ctor = copy_fn_p (decl);
8939 TYPE_HAS_CONSTRUCTOR (class_type) = 1;
8941 if (ctor > 0)
8943 /* [class.copy]
8945 A non-template constructor for class X is a copy
8946 constructor if its first parameter is of type X&, const
8947 X&, volatile X& or const volatile X&, and either there
8948 are no other parameters or else all other parameters have
8949 default arguments. */
8950 TYPE_HAS_INIT_REF (class_type) = 1;
8951 if (ctor > 1)
8952 TYPE_HAS_CONST_INIT_REF (class_type) = 1;
8954 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
8955 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
8957 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
8959 /* [class.copy]
8961 A non-template assignment operator for class X is a copy
8962 assignment operator if its parameter is of type X, X&, const
8963 X&, volatile X& or const volatile X&. */
8965 int assop = copy_fn_p (decl);
8967 if (assop)
8969 TYPE_HAS_ASSIGN_REF (class_type) = 1;
8970 if (assop != 1)
8971 TYPE_HAS_CONST_ASSIGN_REF (class_type) = 1;
8976 /* Check a constructor DECL has the correct form. Complains
8977 if the class has a constructor of the form X(X). */
8980 grok_ctor_properties (tree ctype, tree decl)
8982 int ctor_parm = copy_fn_p (decl);
8984 if (ctor_parm < 0)
8986 /* [class.copy]
8988 A declaration of a constructor for a class X is ill-formed if
8989 its first parameter is of type (optionally cv-qualified) X
8990 and either there are no other parameters or else all other
8991 parameters have default arguments.
8993 We *don't* complain about member template instantiations that
8994 have this form, though; they can occur as we try to decide
8995 what constructor to use during overload resolution. Since
8996 overload resolution will never prefer such a constructor to
8997 the non-template copy constructor (which is either explicitly
8998 or implicitly defined), there's no need to worry about their
8999 existence. Theoretically, they should never even be
9000 instantiated, but that's hard to forestall. */
9001 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
9002 ctype, ctype);
9003 return 0;
9006 return 1;
9009 /* An operator with this code is unary, but can also be binary. */
9011 static int
9012 ambi_op_p (enum tree_code code)
9014 return (code == INDIRECT_REF
9015 || code == ADDR_EXPR
9016 || code == UNARY_PLUS_EXPR
9017 || code == NEGATE_EXPR
9018 || code == PREINCREMENT_EXPR
9019 || code == PREDECREMENT_EXPR);
9022 /* An operator with this name can only be unary. */
9024 static int
9025 unary_op_p (enum tree_code code)
9027 return (code == TRUTH_NOT_EXPR
9028 || code == BIT_NOT_EXPR
9029 || code == COMPONENT_REF
9030 || code == TYPE_EXPR);
9033 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
9034 errors are issued for invalid declarations. */
9036 bool
9037 grok_op_properties (tree decl, bool complain)
9039 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9040 tree argtype;
9041 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
9042 tree name = DECL_NAME (decl);
9043 enum tree_code operator_code;
9044 int arity;
9045 bool ellipsis_p;
9046 tree class_type;
9048 /* Count the number of arguments and check for ellipsis. */
9049 for (argtype = argtypes, arity = 0;
9050 argtype && argtype != void_list_node;
9051 argtype = TREE_CHAIN (argtype))
9052 ++arity;
9053 ellipsis_p = !argtype;
9055 class_type = DECL_CONTEXT (decl);
9056 if (class_type && !CLASS_TYPE_P (class_type))
9057 class_type = NULL_TREE;
9059 if (DECL_CONV_FN_P (decl))
9060 operator_code = TYPE_EXPR;
9061 else
9064 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
9065 if (ansi_opname (CODE) == name) \
9067 operator_code = (CODE); \
9068 break; \
9070 else if (ansi_assopname (CODE) == name) \
9072 operator_code = (CODE); \
9073 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
9074 break; \
9077 #include "operators.def"
9078 #undef DEF_OPERATOR
9080 gcc_unreachable ();
9082 while (0);
9083 gcc_assert (operator_code != LAST_CPLUS_TREE_CODE);
9084 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
9086 if (class_type)
9087 switch (operator_code)
9089 case NEW_EXPR:
9090 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
9091 break;
9093 case DELETE_EXPR:
9094 TYPE_GETS_DELETE (class_type) |= 1;
9095 break;
9097 case VEC_NEW_EXPR:
9098 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
9099 break;
9101 case VEC_DELETE_EXPR:
9102 TYPE_GETS_DELETE (class_type) |= 2;
9103 break;
9105 default:
9106 break;
9109 /* [basic.std.dynamic.allocation]/1:
9111 A program is ill-formed if an allocation function is declared
9112 in a namespace scope other than global scope or declared static
9113 in global scope.
9115 The same also holds true for deallocation functions. */
9116 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
9117 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
9119 if (DECL_NAMESPACE_SCOPE_P (decl))
9121 if (CP_DECL_CONTEXT (decl) != global_namespace)
9123 error ("%qD may not be declared within a namespace", decl);
9124 return false;
9126 else if (!TREE_PUBLIC (decl))
9128 error ("%qD may not be declared as static", decl);
9129 return false;
9134 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
9135 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
9136 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
9137 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
9138 else
9140 /* An operator function must either be a non-static member function
9141 or have at least one parameter of a class, a reference to a class,
9142 an enumeration, or a reference to an enumeration. 13.4.0.6 */
9143 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
9145 if (operator_code == TYPE_EXPR
9146 || operator_code == CALL_EXPR
9147 || operator_code == COMPONENT_REF
9148 || operator_code == ARRAY_REF
9149 || operator_code == NOP_EXPR)
9151 error ("%qD must be a nonstatic member function", decl);
9152 return false;
9154 else
9156 tree p;
9158 if (DECL_STATIC_FUNCTION_P (decl))
9160 error ("%qD must be either a non-static member "
9161 "function or a non-member function", decl);
9162 return false;
9165 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
9167 tree arg = non_reference (TREE_VALUE (p));
9168 if (arg == error_mark_node)
9169 return false;
9171 /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used
9172 because these checks are performed even on
9173 template functions. */
9174 if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE)
9175 break;
9178 if (!p || p == void_list_node)
9180 if (complain)
9181 error ("%qD must have an argument of class or "
9182 "enumerated type", decl);
9183 return false;
9188 /* There are no restrictions on the arguments to an overloaded
9189 "operator ()". */
9190 if (operator_code == CALL_EXPR)
9191 return true;
9193 /* Warn about conversion operators that will never be used. */
9194 if (IDENTIFIER_TYPENAME_P (name)
9195 && ! DECL_TEMPLATE_INFO (decl)
9196 && warn_conversion
9197 /* Warn only declaring the function; there is no need to
9198 warn again about out-of-class definitions. */
9199 && class_type == current_class_type)
9201 tree t = TREE_TYPE (name);
9202 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
9203 const char *what = 0;
9205 if (ref)
9206 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
9208 if (TREE_CODE (t) == VOID_TYPE)
9209 what = "void";
9210 else if (class_type)
9212 if (t == class_type)
9213 what = "the same type";
9214 /* Don't force t to be complete here. */
9215 else if (IS_AGGR_TYPE (t)
9216 && COMPLETE_TYPE_P (t)
9217 && DERIVED_FROM_P (t, class_type))
9218 what = "a base class";
9221 if (what)
9222 warning (OPT_Wconversion, "conversion to %s%s will never use a type "
9223 "conversion operator",
9224 ref ? "a reference to " : "", what);
9227 if (operator_code == COND_EXPR)
9229 /* 13.4.0.3 */
9230 error ("ISO C++ prohibits overloading operator ?:");
9231 return false;
9233 else if (ellipsis_p)
9235 error ("%qD must not have variable number of arguments", decl);
9236 return false;
9238 else if (ambi_op_p (operator_code))
9240 if (arity == 1)
9241 /* We pick the one-argument operator codes by default, so
9242 we don't have to change anything. */
9244 else if (arity == 2)
9246 /* If we thought this was a unary operator, we now know
9247 it to be a binary operator. */
9248 switch (operator_code)
9250 case INDIRECT_REF:
9251 operator_code = MULT_EXPR;
9252 break;
9254 case ADDR_EXPR:
9255 operator_code = BIT_AND_EXPR;
9256 break;
9258 case UNARY_PLUS_EXPR:
9259 operator_code = PLUS_EXPR;
9260 break;
9262 case NEGATE_EXPR:
9263 operator_code = MINUS_EXPR;
9264 break;
9266 case PREINCREMENT_EXPR:
9267 operator_code = POSTINCREMENT_EXPR;
9268 break;
9270 case PREDECREMENT_EXPR:
9271 operator_code = POSTDECREMENT_EXPR;
9272 break;
9274 default:
9275 gcc_unreachable ();
9278 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
9280 if ((operator_code == POSTINCREMENT_EXPR
9281 || operator_code == POSTDECREMENT_EXPR)
9282 && ! processing_template_decl
9283 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
9285 if (methodp)
9286 error ("postfix %qD must take %<int%> as its argument",
9287 decl);
9288 else
9289 error ("postfix %qD must take %<int%> as its second "
9290 "argument", decl);
9291 return false;
9294 else
9296 if (methodp)
9297 error ("%qD must take either zero or one argument", decl);
9298 else
9299 error ("%qD must take either one or two arguments", decl);
9300 return false;
9303 /* More Effective C++ rule 6. */
9304 if (warn_ecpp
9305 && (operator_code == POSTINCREMENT_EXPR
9306 || operator_code == POSTDECREMENT_EXPR
9307 || operator_code == PREINCREMENT_EXPR
9308 || operator_code == PREDECREMENT_EXPR))
9310 tree arg = TREE_VALUE (argtypes);
9311 tree ret = TREE_TYPE (TREE_TYPE (decl));
9312 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
9313 arg = TREE_TYPE (arg);
9314 arg = TYPE_MAIN_VARIANT (arg);
9315 if (operator_code == PREINCREMENT_EXPR
9316 || operator_code == PREDECREMENT_EXPR)
9318 if (TREE_CODE (ret) != REFERENCE_TYPE
9319 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
9320 arg))
9321 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
9322 build_reference_type (arg));
9324 else
9326 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
9327 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
9331 else if (unary_op_p (operator_code))
9333 if (arity != 1)
9335 if (methodp)
9336 error ("%qD must take %<void%>", decl);
9337 else
9338 error ("%qD must take exactly one argument", decl);
9339 return false;
9342 else /* if (binary_op_p (operator_code)) */
9344 if (arity != 2)
9346 if (methodp)
9347 error ("%qD must take exactly one argument", decl);
9348 else
9349 error ("%qD must take exactly two arguments", decl);
9350 return false;
9353 /* More Effective C++ rule 7. */
9354 if (warn_ecpp
9355 && (operator_code == TRUTH_ANDIF_EXPR
9356 || operator_code == TRUTH_ORIF_EXPR
9357 || operator_code == COMPOUND_EXPR))
9358 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
9359 decl);
9362 /* Effective C++ rule 23. */
9363 if (warn_ecpp
9364 && arity == 2
9365 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
9366 && (operator_code == PLUS_EXPR
9367 || operator_code == MINUS_EXPR
9368 || operator_code == TRUNC_DIV_EXPR
9369 || operator_code == MULT_EXPR
9370 || operator_code == TRUNC_MOD_EXPR)
9371 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
9372 warning (OPT_Weffc__, "%qD should return by value", decl);
9374 /* [over.oper]/8 */
9375 for (; argtypes && argtypes != void_list_node;
9376 argtypes = TREE_CHAIN (argtypes))
9377 if (TREE_PURPOSE (argtypes))
9379 TREE_PURPOSE (argtypes) = NULL_TREE;
9380 if (operator_code == POSTINCREMENT_EXPR
9381 || operator_code == POSTDECREMENT_EXPR)
9383 if (pedantic)
9384 pedwarn ("%qD cannot have default arguments", decl);
9386 else
9388 error ("%qD cannot have default arguments", decl);
9389 return false;
9393 return true;
9396 /* Return a string giving the keyword associate with CODE. */
9398 static const char *
9399 tag_name (enum tag_types code)
9401 switch (code)
9403 case record_type:
9404 return "struct";
9405 case class_type:
9406 return "class";
9407 case union_type:
9408 return "union";
9409 case enum_type:
9410 return "enum";
9411 case typename_type:
9412 return "typename";
9413 default:
9414 gcc_unreachable ();
9418 /* Name lookup in an elaborated-type-specifier (after the keyword
9419 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
9420 elaborated-type-specifier is invalid, issue a diagnostic and return
9421 error_mark_node; otherwise, return the *_TYPE to which it referred.
9422 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
9424 tree
9425 check_elaborated_type_specifier (enum tag_types tag_code,
9426 tree decl,
9427 bool allow_template_p)
9429 tree type;
9431 /* In the case of:
9433 struct S { struct S *p; };
9435 name lookup will find the TYPE_DECL for the implicit "S::S"
9436 typedef. Adjust for that here. */
9437 if (DECL_SELF_REFERENCE_P (decl))
9438 decl = TYPE_NAME (TREE_TYPE (decl));
9440 type = TREE_TYPE (decl);
9442 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
9443 is false for this case as well. */
9444 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
9446 error ("using template type parameter %qT after %qs",
9447 type, tag_name (tag_code));
9448 return error_mark_node;
9450 /* [dcl.type.elab]
9452 If the identifier resolves to a typedef-name or a template
9453 type-parameter, the elaborated-type-specifier is ill-formed.
9455 In other words, the only legitimate declaration to use in the
9456 elaborated type specifier is the implicit typedef created when
9457 the type is declared. */
9458 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
9459 && tag_code != typename_type)
9461 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
9462 error ("%q+D has a previous declaration here", decl);
9463 return error_mark_node;
9465 else if (TREE_CODE (type) != RECORD_TYPE
9466 && TREE_CODE (type) != UNION_TYPE
9467 && tag_code != enum_type
9468 && tag_code != typename_type)
9470 error ("%qT referred to as %qs", type, tag_name (tag_code));
9471 error ("%q+T has a previous declaration here", type);
9472 return error_mark_node;
9474 else if (TREE_CODE (type) != ENUMERAL_TYPE
9475 && tag_code == enum_type)
9477 error ("%qT referred to as enum", type);
9478 error ("%q+T has a previous declaration here", type);
9479 return error_mark_node;
9481 else if (!allow_template_p
9482 && TREE_CODE (type) == RECORD_TYPE
9483 && CLASSTYPE_IS_TEMPLATE (type))
9485 /* If a class template appears as elaborated type specifier
9486 without a template header such as:
9488 template <class T> class C {};
9489 void f(class C); // No template header here
9491 then the required template argument is missing. */
9492 error ("template argument required for %<%s %T%>",
9493 tag_name (tag_code),
9494 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
9495 return error_mark_node;
9498 return type;
9501 /* Lookup NAME in elaborate type specifier in scope according to
9502 SCOPE and issue diagnostics if necessary.
9503 Return *_TYPE node upon success, NULL_TREE when the NAME is not
9504 found, and ERROR_MARK_NODE for type error. */
9506 static tree
9507 lookup_and_check_tag (enum tag_types tag_code, tree name,
9508 tag_scope scope, bool template_header_p)
9510 tree t;
9511 tree decl;
9512 if (scope == ts_global)
9514 /* First try ordinary name lookup, ignoring hidden class name
9515 injected via friend declaration. */
9516 decl = lookup_name_prefer_type (name, 2);
9517 /* If that fails, the name will be placed in the smallest
9518 non-class, non-function-prototype scope according to 3.3.1/5.
9519 We may already have a hidden name declared as friend in this
9520 scope. So lookup again but not ignoring hidden names.
9521 If we find one, that name will be made visible rather than
9522 creating a new tag. */
9523 if (!decl)
9524 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
9526 else
9527 decl = lookup_type_scope (name, scope);
9529 if (decl && DECL_CLASS_TEMPLATE_P (decl))
9530 decl = DECL_TEMPLATE_RESULT (decl);
9532 if (decl && TREE_CODE (decl) == TYPE_DECL)
9534 /* Look for invalid nested type:
9535 class C {
9536 class C {};
9537 }; */
9538 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
9540 error ("%qD has the same name as the class in which it is "
9541 "declared",
9542 decl);
9543 return error_mark_node;
9546 /* Two cases we need to consider when deciding if a class
9547 template is allowed as an elaborated type specifier:
9548 1. It is a self reference to its own class.
9549 2. It comes with a template header.
9551 For example:
9553 template <class T> class C {
9554 class C *c1; // DECL_SELF_REFERENCE_P is true
9555 class D;
9557 template <class U> class C; // template_header_p is true
9558 template <class T> class C<T>::D {
9559 class C *c2; // DECL_SELF_REFERENCE_P is true
9560 }; */
9562 t = check_elaborated_type_specifier (tag_code,
9563 decl,
9564 template_header_p
9565 | DECL_SELF_REFERENCE_P (decl));
9566 return t;
9568 else
9569 return NULL_TREE;
9572 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
9573 Define the tag as a forward-reference if it is not defined.
9575 If a declaration is given, process it here, and report an error if
9576 multiple declarations are not identical.
9578 SCOPE is TS_CURRENT when this is also a definition. Only look in
9579 the current frame for the name (since C++ allows new names in any
9580 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
9581 declaration. Only look beginning from the current scope outward up
9582 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
9584 TEMPLATE_HEADER_P is true when this declaration is preceded by
9585 a set of template parameters. */
9587 tree
9588 xref_tag (enum tag_types tag_code, tree name,
9589 tag_scope scope, bool template_header_p)
9591 enum tree_code code;
9592 tree t;
9593 tree context = NULL_TREE;
9595 timevar_push (TV_NAME_LOOKUP);
9597 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
9599 switch (tag_code)
9601 case record_type:
9602 case class_type:
9603 code = RECORD_TYPE;
9604 break;
9605 case union_type:
9606 code = UNION_TYPE;
9607 break;
9608 case enum_type:
9609 code = ENUMERAL_TYPE;
9610 break;
9611 default:
9612 gcc_unreachable ();
9615 /* In case of anonymous name, xref_tag is only called to
9616 make type node and push name. Name lookup is not required. */
9617 if (ANON_AGGRNAME_P (name))
9618 t = NULL_TREE;
9619 else
9620 t = lookup_and_check_tag (tag_code, name,
9621 scope, template_header_p);
9623 if (t == error_mark_node)
9624 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9626 if (scope != ts_current && t && current_class_type
9627 && template_class_depth (current_class_type)
9628 && template_header_p)
9630 /* Since SCOPE is not TS_CURRENT, we are not looking at a
9631 definition of this tag. Since, in addition, we are currently
9632 processing a (member) template declaration of a template
9633 class, we must be very careful; consider:
9635 template <class X>
9636 struct S1
9638 template <class U>
9639 struct S2
9640 { template <class V>
9641 friend struct S1; };
9643 Here, the S2::S1 declaration should not be confused with the
9644 outer declaration. In particular, the inner version should
9645 have a template parameter of level 2, not level 1. This
9646 would be particularly important if the member declaration
9647 were instead:
9649 template <class V = U> friend struct S1;
9651 say, when we should tsubst into `U' when instantiating
9652 S2. On the other hand, when presented with:
9654 template <class T>
9655 struct S1 {
9656 template <class U>
9657 struct S2 {};
9658 template <class U>
9659 friend struct S2;
9662 we must find the inner binding eventually. We
9663 accomplish this by making sure that the new type we
9664 create to represent this declaration has the right
9665 TYPE_CONTEXT. */
9666 context = TYPE_CONTEXT (t);
9667 t = NULL_TREE;
9670 if (! t)
9672 /* If no such tag is yet defined, create a forward-reference node
9673 and record it as the "definition".
9674 When a real declaration of this type is found,
9675 the forward-reference will be altered into a real type. */
9676 if (code == ENUMERAL_TYPE)
9678 error ("use of enum %q#D without previous declaration", name);
9679 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9681 else
9683 t = make_aggr_type (code);
9684 TYPE_CONTEXT (t) = context;
9685 t = pushtag (name, t, scope);
9688 else
9690 if (template_header_p && IS_AGGR_TYPE (t))
9692 if (!redeclare_class_template (t, current_template_parms))
9693 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9695 else if (!processing_template_decl
9696 && CLASS_TYPE_P (t)
9697 && CLASSTYPE_IS_TEMPLATE (t))
9699 error ("redeclaration of %qT as a non-template", t);
9700 error ("previous declaration %q+D", t);
9701 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9704 /* Make injected friend class visible. */
9705 if (scope != ts_within_enclosing_non_class
9706 && hidden_name_p (TYPE_NAME (t)))
9708 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
9709 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
9711 if (TYPE_TEMPLATE_INFO (t))
9713 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
9714 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
9719 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
9722 tree
9723 xref_tag_from_type (tree old, tree id, tag_scope scope)
9725 enum tag_types tag_kind;
9727 if (TREE_CODE (old) == RECORD_TYPE)
9728 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
9729 else
9730 tag_kind = union_type;
9732 if (id == NULL_TREE)
9733 id = TYPE_IDENTIFIER (old);
9735 return xref_tag (tag_kind, id, scope, false);
9738 /* Create the binfo hierarchy for REF with (possibly NULL) base list
9739 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
9740 access_* node, and the TREE_VALUE is the type of the base-class.
9741 Non-NULL TREE_TYPE indicates virtual inheritance.
9743 Returns true if the binfo heirarchy was successfully created,
9744 false if an error was detected. */
9746 bool
9747 xref_basetypes (tree ref, tree base_list)
9749 tree *basep;
9750 tree binfo, base_binfo;
9751 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
9752 unsigned max_bases = 0; /* Maximum direct bases. */
9753 int i;
9754 tree default_access;
9755 tree igo_prev; /* Track Inheritance Graph Order. */
9757 if (ref == error_mark_node)
9758 return false;
9760 /* The base of a derived class is private by default, all others are
9761 public. */
9762 default_access = (TREE_CODE (ref) == RECORD_TYPE
9763 && CLASSTYPE_DECLARED_CLASS (ref)
9764 ? access_private_node : access_public_node);
9766 /* First, make sure that any templates in base-classes are
9767 instantiated. This ensures that if we call ourselves recursively
9768 we do not get confused about which classes are marked and which
9769 are not. */
9770 basep = &base_list;
9771 while (*basep)
9773 tree basetype = TREE_VALUE (*basep);
9775 if (!(processing_template_decl && uses_template_parms (basetype))
9776 && !complete_type_or_else (basetype, NULL))
9777 /* An incomplete type. Remove it from the list. */
9778 *basep = TREE_CHAIN (*basep);
9779 else
9781 max_bases++;
9782 if (TREE_TYPE (*basep))
9783 max_vbases++;
9784 if (CLASS_TYPE_P (basetype))
9785 max_vbases += VEC_length (tree, CLASSTYPE_VBASECLASSES (basetype));
9786 basep = &TREE_CHAIN (*basep);
9790 TYPE_MARKED_P (ref) = 1;
9792 /* The binfo slot should be empty, unless this is an (ill-formed)
9793 redefinition. */
9794 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
9795 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
9797 binfo = make_tree_binfo (max_bases);
9799 TYPE_BINFO (ref) = binfo;
9800 BINFO_OFFSET (binfo) = size_zero_node;
9801 BINFO_TYPE (binfo) = ref;
9803 if (max_bases)
9805 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, max_bases);
9806 /* An aggregate cannot have baseclasses. */
9807 CLASSTYPE_NON_AGGREGATE (ref) = 1;
9809 if (TREE_CODE (ref) == UNION_TYPE)
9811 error ("derived union %qT invalid", ref);
9812 return false;
9816 if (max_bases > 1)
9818 if (TYPE_FOR_JAVA (ref))
9820 error ("Java class %qT cannot have multiple bases", ref);
9821 return false;
9825 if (max_vbases)
9827 CLASSTYPE_VBASECLASSES (ref) = VEC_alloc (tree, gc, max_vbases);
9829 if (TYPE_FOR_JAVA (ref))
9831 error ("Java class %qT cannot have virtual bases", ref);
9832 return false;
9836 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
9838 tree access = TREE_PURPOSE (base_list);
9839 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
9840 tree basetype = TREE_VALUE (base_list);
9842 if (access == access_default_node)
9843 access = default_access;
9845 if (TREE_CODE (basetype) == TYPE_DECL)
9846 basetype = TREE_TYPE (basetype);
9847 if (TREE_CODE (basetype) != RECORD_TYPE
9848 && TREE_CODE (basetype) != TYPENAME_TYPE
9849 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
9850 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM)
9852 error ("base type %qT fails to be a struct or class type",
9853 basetype);
9854 return false;
9857 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
9858 TYPE_FOR_JAVA (ref) = 1;
9860 base_binfo = NULL_TREE;
9861 if (CLASS_TYPE_P (basetype) && !dependent_type_p (basetype))
9863 base_binfo = TYPE_BINFO (basetype);
9864 /* The original basetype could have been a typedef'd type. */
9865 basetype = BINFO_TYPE (base_binfo);
9867 /* Inherit flags from the base. */
9868 TYPE_HAS_NEW_OPERATOR (ref)
9869 |= TYPE_HAS_NEW_OPERATOR (basetype);
9870 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
9871 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
9872 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
9873 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
9874 CLASSTYPE_DIAMOND_SHAPED_P (ref)
9875 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
9876 CLASSTYPE_REPEATED_BASE_P (ref)
9877 |= CLASSTYPE_REPEATED_BASE_P (basetype);
9880 /* We must do this test after we've seen through a typedef
9881 type. */
9882 if (TYPE_MARKED_P (basetype))
9884 if (basetype == ref)
9885 error ("recursive type %qT undefined", basetype);
9886 else
9887 error ("duplicate base type %qT invalid", basetype);
9888 return false;
9890 TYPE_MARKED_P (basetype) = 1;
9892 base_binfo = copy_binfo (base_binfo, basetype, ref,
9893 &igo_prev, via_virtual);
9894 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
9895 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
9897 BINFO_BASE_APPEND (binfo, base_binfo);
9898 BINFO_BASE_ACCESS_APPEND (binfo, access);
9901 if (VEC_space (tree, CLASSTYPE_VBASECLASSES (ref), 1))
9902 /* If we have space in the vbase vector, we must have shared at
9903 least one of them, and are therefore diamond shaped. */
9904 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
9906 /* Unmark all the types. */
9907 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9908 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
9909 TYPE_MARKED_P (ref) = 0;
9911 /* Now see if we have a repeated base type. */
9912 if (!CLASSTYPE_REPEATED_BASE_P (ref))
9914 for (base_binfo = binfo; base_binfo;
9915 base_binfo = TREE_CHAIN (base_binfo))
9917 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
9919 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
9920 break;
9922 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
9924 for (base_binfo = binfo; base_binfo;
9925 base_binfo = TREE_CHAIN (base_binfo))
9926 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
9927 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
9928 else
9929 break;
9932 return true;
9936 /* Begin compiling the definition of an enumeration type.
9937 NAME is its name.
9938 Returns the type object, as yet incomplete.
9939 Also records info about it so that build_enumerator
9940 may be used to declare the individual values as they are read. */
9942 tree
9943 start_enum (tree name)
9945 tree enumtype;
9947 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
9949 /* If this is the real definition for a previous forward reference,
9950 fill in the contents in the same object that used to be the
9951 forward reference. */
9953 enumtype = lookup_and_check_tag (enum_type, name,
9954 /*tag_scope=*/ts_current,
9955 /*template_header_p=*/false);
9957 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
9959 error ("multiple definition of %q#T", enumtype);
9960 error ("%Jprevious definition here", TYPE_MAIN_DECL (enumtype));
9961 /* Clear out TYPE_VALUES, and start again. */
9962 TYPE_VALUES (enumtype) = NULL_TREE;
9964 else
9966 /* In case of error, make a dummy enum to allow parsing to
9967 continue. */
9968 if (enumtype == error_mark_node)
9969 name = make_anon_name ();
9971 enumtype = make_node (ENUMERAL_TYPE);
9972 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
9975 return enumtype;
9978 /* After processing and defining all the values of an enumeration type,
9979 install their decls in the enumeration type and finish it off.
9980 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
9982 void
9983 finish_enum (tree enumtype)
9985 tree values;
9986 tree decl;
9987 tree value;
9988 tree minnode;
9989 tree maxnode;
9990 tree t;
9991 bool unsignedp;
9992 bool use_short_enum;
9993 int lowprec;
9994 int highprec;
9995 int precision;
9996 integer_type_kind itk;
9997 tree underlying_type = NULL_TREE;
9999 /* We built up the VALUES in reverse order. */
10000 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
10002 /* For an enum defined in a template, just set the type of the values;
10003 all further processing is postponed until the template is
10004 instantiated. We need to set the type so that tsubst of a CONST_DECL
10005 works. */
10006 if (processing_template_decl)
10008 for (values = TYPE_VALUES (enumtype);
10009 values;
10010 values = TREE_CHAIN (values))
10011 TREE_TYPE (TREE_VALUE (values)) = enumtype;
10012 if (at_function_scope_p ())
10013 add_stmt (build_min (TAG_DEFN, enumtype));
10014 return;
10017 /* Determine the minimum and maximum values of the enumerators. */
10018 if (TYPE_VALUES (enumtype))
10020 minnode = maxnode = NULL_TREE;
10022 for (values = TYPE_VALUES (enumtype);
10023 values;
10024 values = TREE_CHAIN (values))
10026 decl = TREE_VALUE (values);
10028 /* [dcl.enum]: Following the closing brace of an enum-specifier,
10029 each enumerator has the type of its enumeration. Prior to the
10030 closing brace, the type of each enumerator is the type of its
10031 initializing value. */
10032 TREE_TYPE (decl) = enumtype;
10034 /* Update the minimum and maximum values, if appropriate. */
10035 value = DECL_INITIAL (decl);
10036 if (value == error_mark_node)
10037 value = integer_zero_node;
10038 /* Figure out what the minimum and maximum values of the
10039 enumerators are. */
10040 if (!minnode)
10041 minnode = maxnode = value;
10042 else if (tree_int_cst_lt (maxnode, value))
10043 maxnode = value;
10044 else if (tree_int_cst_lt (value, minnode))
10045 minnode = value;
10048 else
10049 /* [dcl.enum]
10051 If the enumerator-list is empty, the underlying type is as if
10052 the enumeration had a single enumerator with value 0. */
10053 minnode = maxnode = integer_zero_node;
10055 /* Compute the number of bits require to represent all values of the
10056 enumeration. We must do this before the type of MINNODE and
10057 MAXNODE are transformed, since min_precision relies on the
10058 TREE_TYPE of the value it is passed. */
10059 unsignedp = tree_int_cst_sgn (minnode) >= 0;
10060 lowprec = min_precision (minnode, unsignedp);
10061 highprec = min_precision (maxnode, unsignedp);
10062 precision = MAX (lowprec, highprec);
10064 /* Determine the underlying type of the enumeration.
10066 [dcl.enum]
10068 The underlying type of an enumeration is an integral type that
10069 can represent all the enumerator values defined in the
10070 enumeration. It is implementation-defined which integral type is
10071 used as the underlying type for an enumeration except that the
10072 underlying type shall not be larger than int unless the value of
10073 an enumerator cannot fit in an int or unsigned int.
10075 We use "int" or an "unsigned int" as the underlying type, even if
10076 a smaller integral type would work, unless the user has
10077 explicitly requested that we use the smallest possible type. The
10078 user can request that for all enumerations with a command line
10079 flag, or for just one enumeration with an attribute. */
10081 use_short_enum = flag_short_enums
10082 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
10084 for (itk = (use_short_enum ? itk_char : itk_int);
10085 itk != itk_none;
10086 itk++)
10088 underlying_type = integer_types[itk];
10089 if (TYPE_PRECISION (underlying_type) >= precision
10090 && TYPE_UNSIGNED (underlying_type) == unsignedp)
10091 break;
10093 if (itk == itk_none)
10095 /* DR 377
10097 IF no integral type can represent all the enumerator values, the
10098 enumeration is ill-formed. */
10099 error ("no integral type can represent all of the enumerator values "
10100 "for %qT", enumtype);
10101 precision = TYPE_PRECISION (long_long_integer_type_node);
10102 underlying_type = integer_types[itk_unsigned_long_long];
10105 /* Compute the minium and maximum values for the type.
10107 [dcl.enum]
10109 For an enumeration where emin is the smallest enumerator and emax
10110 is the largest, the values of the enumeration are the values of the
10111 underlying type in the range bmin to bmax, where bmin and bmax are,
10112 respectively, the smallest and largest values of the smallest bit-
10113 field that can store emin and emax. */
10115 /* The middle-end currently assumes that types with TYPE_PRECISION
10116 narrower than their underlying type are suitably zero or sign
10117 extended to fill their mode. g++ doesn't make these guarantees.
10118 Until the middle-end can represent such paradoxical types, we
10119 set the TYPE_PRECISION to the width of the underlying type. */
10120 TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type);
10122 set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp);
10124 /* [dcl.enum]
10126 The value of sizeof() applied to an enumeration type, an object
10127 of an enumeration type, or an enumerator, is the value of sizeof()
10128 applied to the underlying type. */
10129 TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
10130 TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
10131 TYPE_MODE (enumtype) = TYPE_MODE (underlying_type);
10132 TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
10133 TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
10134 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (underlying_type);
10136 /* Convert each of the enumerators to the type of the underlying
10137 type of the enumeration. */
10138 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
10140 location_t saved_location;
10142 decl = TREE_VALUE (values);
10143 saved_location = input_location;
10144 input_location = DECL_SOURCE_LOCATION (decl);
10145 value = perform_implicit_conversion (underlying_type,
10146 DECL_INITIAL (decl));
10147 input_location = saved_location;
10149 /* Do not clobber shared ints. */
10150 value = copy_node (value);
10152 TREE_TYPE (value) = enumtype;
10153 DECL_INITIAL (decl) = value;
10154 TREE_VALUE (values) = value;
10157 /* Fix up all variant types of this enum type. */
10158 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
10160 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
10161 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
10162 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
10163 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
10164 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
10165 TYPE_MODE (t) = TYPE_MODE (enumtype);
10166 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
10167 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
10168 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
10169 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (enumtype);
10172 /* Finish debugging output for this type. */
10173 rest_of_type_compilation (enumtype, namespace_bindings_p ());
10176 /* Build and install a CONST_DECL for an enumeration constant of the
10177 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
10178 Assignment of sequential values by default is handled here. */
10180 void
10181 build_enumerator (tree name, tree value, tree enumtype)
10183 tree decl;
10184 tree context;
10185 tree type;
10187 /* If the VALUE was erroneous, pretend it wasn't there; that will
10188 result in the enum being assigned the next value in sequence. */
10189 if (value == error_mark_node)
10190 value = NULL_TREE;
10192 /* Remove no-op casts from the value. */
10193 if (value)
10194 STRIP_TYPE_NOPS (value);
10196 if (! processing_template_decl)
10198 /* Validate and default VALUE. */
10199 if (value != NULL_TREE)
10201 value = integral_constant_value (value);
10203 if (TREE_CODE (value) == INTEGER_CST)
10205 value = perform_integral_promotions (value);
10206 constant_expression_warning (value);
10208 else
10210 error ("enumerator value for %qD not integer constant", name);
10211 value = NULL_TREE;
10215 /* Default based on previous value. */
10216 if (value == NULL_TREE)
10218 if (TYPE_VALUES (enumtype))
10220 HOST_WIDE_INT hi;
10221 unsigned HOST_WIDE_INT lo;
10222 tree prev_value;
10223 bool overflowed;
10225 /* The next value is the previous value plus one. We can
10226 safely assume that the previous value is an INTEGER_CST.
10227 add_double doesn't know the type of the target expression,
10228 so we must check with int_fits_type_p as well. */
10229 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
10230 overflowed = add_double (TREE_INT_CST_LOW (prev_value),
10231 TREE_INT_CST_HIGH (prev_value),
10232 1, 0, &lo, &hi);
10233 value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi);
10234 overflowed |= !int_fits_type_p (value, TREE_TYPE (prev_value));
10236 if (overflowed)
10238 error ("overflow in enumeration values at %qD", name);
10239 value = error_mark_node;
10242 else
10243 value = integer_zero_node;
10246 /* Remove no-op casts from the value. */
10247 STRIP_TYPE_NOPS (value);
10250 /* C++ associates enums with global, function, or class declarations. */
10251 context = current_scope ();
10253 /* Build the actual enumeration constant. Note that the enumeration
10254 constants have the type of their initializers until the
10255 enumeration is complete:
10257 [ dcl.enum ]
10259 Following the closing brace of an enum-specifier, each enumer-
10260 ator has the type of its enumeration. Prior to the closing
10261 brace, the type of each enumerator is the type of its
10262 initializing value.
10264 In finish_enum we will reset the type. Of course, if we're
10265 processing a template, there may be no value. */
10266 type = value ? TREE_TYPE (value) : NULL_TREE;
10268 if (context && context == current_class_type)
10269 /* This enum declaration is local to the class. We need the full
10270 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
10271 decl = build_lang_decl (CONST_DECL, name, type);
10272 else
10273 /* It's a global enum, or it's local to a function. (Note local to
10274 a function could mean local to a class method. */
10275 decl = build_decl (CONST_DECL, name, type);
10277 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
10278 TREE_CONSTANT (decl) = 1;
10279 TREE_INVARIANT (decl) = 1;
10280 TREE_READONLY (decl) = 1;
10281 DECL_INITIAL (decl) = value;
10283 if (context && context == current_class_type)
10284 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
10285 on the TYPE_FIELDS list for `S'. (That's so that you can say
10286 things like `S::i' later.) */
10287 finish_member_declaration (decl);
10288 else
10289 pushdecl (decl);
10291 /* Add this enumeration constant to the list for this type. */
10292 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
10296 /* We're defining DECL. Make sure that it's type is OK. */
10298 static void
10299 check_function_type (tree decl, tree current_function_parms)
10301 tree fntype = TREE_TYPE (decl);
10302 tree return_type = complete_type (TREE_TYPE (fntype));
10304 /* In a function definition, arg types must be complete. */
10305 require_complete_types_for_parms (current_function_parms);
10307 if (dependent_type_p (return_type))
10308 return;
10309 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
10311 tree args = TYPE_ARG_TYPES (fntype);
10313 error ("return type %q#T is incomplete", return_type);
10315 /* Make it return void instead. */
10316 if (TREE_CODE (fntype) == METHOD_TYPE)
10317 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
10318 void_type_node,
10319 TREE_CHAIN (args));
10320 else
10321 fntype = build_function_type (void_type_node, args);
10322 TREE_TYPE (decl)
10323 = build_exception_variant (fntype,
10324 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
10326 else
10327 abstract_virtuals_error (decl, TREE_TYPE (fntype));
10330 /* Create the FUNCTION_DECL for a function definition.
10331 DECLSPECS and DECLARATOR are the parts of the declaration;
10332 they describe the function's name and the type it returns,
10333 but twisted together in a fashion that parallels the syntax of C.
10335 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
10336 DECLARATOR is really the DECL for the function we are about to
10337 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
10338 indicating that the function is an inline defined in-class.
10340 This function creates a binding context for the function body
10341 as well as setting up the FUNCTION_DECL in current_function_decl.
10343 For C++, we must first check whether that datum makes any sense.
10344 For example, "class A local_a(1,2);" means that variable local_a
10345 is an aggregate of type A, which should have a constructor
10346 applied to it with the argument list [1, 2]. */
10348 void
10349 start_preparsed_function (tree decl1, tree attrs, int flags)
10351 tree ctype = NULL_TREE;
10352 tree fntype;
10353 tree restype;
10354 int doing_friend = 0;
10355 struct cp_binding_level *bl;
10356 tree current_function_parms;
10357 struct c_fileinfo *finfo
10358 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
10359 bool honor_interface;
10361 /* Sanity check. */
10362 gcc_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE);
10363 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
10365 fntype = TREE_TYPE (decl1);
10366 if (TREE_CODE (fntype) == METHOD_TYPE)
10367 ctype = TYPE_METHOD_BASETYPE (fntype);
10369 /* ISO C++ 11.4/5. A friend function defined in a class is in
10370 the (lexical) scope of the class in which it is defined. */
10371 if (!ctype && DECL_FRIEND_P (decl1))
10373 ctype = DECL_FRIEND_CONTEXT (decl1);
10375 /* CTYPE could be null here if we're dealing with a template;
10376 for example, `inline friend float foo()' inside a template
10377 will have no CTYPE set. */
10378 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
10379 ctype = NULL_TREE;
10380 else
10381 doing_friend = 1;
10384 if (DECL_DECLARED_INLINE_P (decl1)
10385 && lookup_attribute ("noinline", attrs))
10386 warning (0, "inline function %q+D given attribute noinline", decl1);
10388 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
10389 /* This is a constructor, we must ensure that any default args
10390 introduced by this definition are propagated to the clones
10391 now. The clones are used directly in overload resolution. */
10392 adjust_clone_args (decl1);
10394 /* Sometimes we don't notice that a function is a static member, and
10395 build a METHOD_TYPE for it. Fix that up now. */
10396 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
10397 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
10399 revert_static_member_fn (decl1);
10400 ctype = NULL_TREE;
10403 /* Set up current_class_type, and enter the scope of the class, if
10404 appropriate. */
10405 if (ctype)
10406 push_nested_class (ctype);
10407 else if (DECL_STATIC_FUNCTION_P (decl1))
10408 push_nested_class (DECL_CONTEXT (decl1));
10410 /* Now that we have entered the scope of the class, we must restore
10411 the bindings for any template parameters surrounding DECL1, if it
10412 is an inline member template. (Order is important; consider the
10413 case where a template parameter has the same name as a field of
10414 the class.) It is not until after this point that
10415 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
10416 if (flags & SF_INCLASS_INLINE)
10417 maybe_begin_member_template_processing (decl1);
10419 /* Effective C++ rule 15. */
10420 if (warn_ecpp
10421 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
10422 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
10423 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
10425 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10426 error_mark_node is replaced below (in poplevel) with the BLOCK. */
10427 if (!DECL_INITIAL (decl1))
10428 DECL_INITIAL (decl1) = error_mark_node;
10430 /* This function exists in static storage.
10431 (This does not mean `static' in the C sense!) */
10432 TREE_STATIC (decl1) = 1;
10434 /* We must call push_template_decl after current_class_type is set
10435 up. (If we are processing inline definitions after exiting a
10436 class scope, current_class_type will be NULL_TREE until set above
10437 by push_nested_class.) */
10438 if (processing_template_decl)
10440 /* FIXME: Handle error_mark_node more gracefully. */
10441 tree newdecl1 = push_template_decl (decl1);
10442 if (newdecl1 != error_mark_node)
10443 decl1 = newdecl1;
10446 /* We are now in the scope of the function being defined. */
10447 current_function_decl = decl1;
10449 /* Save the parm names or decls from this function's declarator
10450 where store_parm_decls will find them. */
10451 current_function_parms = DECL_ARGUMENTS (decl1);
10453 /* Make sure the parameter and return types are reasonable. When
10454 you declare a function, these types can be incomplete, but they
10455 must be complete when you define the function. */
10456 check_function_type (decl1, current_function_parms);
10458 /* Build the return declaration for the function. */
10459 restype = TREE_TYPE (fntype);
10460 /* Promote the value to int before returning it. */
10461 if (c_promoting_integer_type_p (restype))
10462 restype = type_promotes_to (restype);
10463 if (DECL_RESULT (decl1) == NULL_TREE)
10465 tree resdecl;
10467 resdecl = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
10468 DECL_ARTIFICIAL (resdecl) = 1;
10469 DECL_IGNORED_P (resdecl) = 1;
10470 DECL_RESULT (decl1) = resdecl;
10472 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
10475 /* Initialize RTL machinery. We cannot do this until
10476 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
10477 even when processing a template; this is how we get
10478 CFUN set up, and our per-function variables initialized.
10479 FIXME factor out the non-RTL stuff. */
10480 bl = current_binding_level;
10481 allocate_struct_function (decl1);
10482 current_binding_level = bl;
10484 /* Even though we're inside a function body, we still don't want to
10485 call expand_expr to calculate the size of a variable-sized array.
10486 We haven't necessarily assigned RTL to all variables yet, so it's
10487 not safe to try to expand expressions involving them. */
10488 cfun->x_dont_save_pending_sizes_p = 1;
10490 /* Start the statement-tree, start the tree now. */
10491 DECL_SAVED_TREE (decl1) = push_stmt_list ();
10493 /* Let the user know we're compiling this function. */
10494 announce_function (decl1);
10496 /* Record the decl so that the function name is defined.
10497 If we already have a decl for this name, and it is a FUNCTION_DECL,
10498 use the old decl. */
10499 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
10501 /* A specialization is not used to guide overload resolution. */
10502 if (!DECL_FUNCTION_MEMBER_P (decl1)
10503 && !(DECL_USE_TEMPLATE (decl1) &&
10504 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
10506 tree olddecl = pushdecl (decl1);
10508 if (olddecl == error_mark_node)
10509 /* If something went wrong when registering the declaration,
10510 use DECL1; we have to have a FUNCTION_DECL to use when
10511 parsing the body of the function. */
10513 else
10514 /* Otherwise, OLDDECL is either a previous declaration of
10515 the same function or DECL1 itself. */
10516 decl1 = olddecl;
10518 else
10520 /* We need to set the DECL_CONTEXT. */
10521 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
10522 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
10524 fntype = TREE_TYPE (decl1);
10526 /* If #pragma weak applies, mark the decl appropriately now.
10527 The pragma only applies to global functions. Because
10528 determining whether or not the #pragma applies involves
10529 computing the mangled name for the declaration, we cannot
10530 apply the pragma until after we have merged this declaration
10531 with any previous declarations; if the original declaration
10532 has a linkage specification, that specification applies to
10533 the definition as well, and may affect the mangled name. */
10534 if (!DECL_CONTEXT (decl1))
10535 maybe_apply_pragma_weak (decl1);
10538 /* Reset these in case the call to pushdecl changed them. */
10539 current_function_decl = decl1;
10540 cfun->decl = decl1;
10542 /* If we are (erroneously) defining a function that we have already
10543 defined before, wipe out what we knew before. */
10544 if (!DECL_PENDING_INLINE_P (decl1))
10545 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
10547 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
10549 /* We know that this was set up by `grokclassfn'. We do not
10550 wait until `store_parm_decls', since evil parse errors may
10551 never get us to that point. Here we keep the consistency
10552 between `current_class_type' and `current_class_ptr'. */
10553 tree t = DECL_ARGUMENTS (decl1);
10555 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
10556 gcc_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE);
10558 cp_function_chain->x_current_class_ref
10559 = build_indirect_ref (t, NULL);
10560 cp_function_chain->x_current_class_ptr = t;
10562 /* Constructors and destructors need to know whether they're "in
10563 charge" of initializing virtual base classes. */
10564 t = TREE_CHAIN (t);
10565 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
10567 current_in_charge_parm = t;
10568 t = TREE_CHAIN (t);
10570 if (DECL_HAS_VTT_PARM_P (decl1))
10572 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
10573 current_vtt_parm = t;
10577 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
10578 /* Implicitly-defined methods (like the
10579 destructor for a class in which no destructor
10580 is explicitly declared) must not be defined
10581 until their definition is needed. So, we
10582 ignore interface specifications for
10583 compiler-generated functions. */
10584 && !DECL_ARTIFICIAL (decl1));
10586 if (DECL_INTERFACE_KNOWN (decl1))
10588 tree ctx = decl_function_context (decl1);
10590 if (DECL_NOT_REALLY_EXTERN (decl1))
10591 DECL_EXTERNAL (decl1) = 0;
10593 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
10594 && TREE_PUBLIC (ctx))
10595 /* This is a function in a local class in an extern inline
10596 function. */
10597 comdat_linkage (decl1);
10599 /* If this function belongs to an interface, it is public.
10600 If it belongs to someone else's interface, it is also external.
10601 This only affects inlines and template instantiations. */
10602 else if (!finfo->interface_unknown && honor_interface)
10604 if (DECL_DECLARED_INLINE_P (decl1)
10605 || DECL_TEMPLATE_INSTANTIATION (decl1)
10606 || processing_template_decl)
10608 DECL_EXTERNAL (decl1)
10609 = (finfo->interface_only
10610 || (DECL_DECLARED_INLINE_P (decl1)
10611 && ! flag_implement_inlines
10612 && !DECL_VINDEX (decl1)));
10614 /* For WIN32 we also want to put these in linkonce sections. */
10615 maybe_make_one_only (decl1);
10617 else
10618 DECL_EXTERNAL (decl1) = 0;
10619 DECL_INTERFACE_KNOWN (decl1) = 1;
10620 /* If this function is in an interface implemented in this file,
10621 make sure that the backend knows to emit this function
10622 here. */
10623 if (!DECL_EXTERNAL (decl1))
10624 mark_needed (decl1);
10626 else if (finfo->interface_unknown && finfo->interface_only
10627 && honor_interface)
10629 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
10630 interface, we will have both finfo->interface_unknown and
10631 finfo->interface_only set. In that case, we don't want to
10632 use the normal heuristics because someone will supply a
10633 #pragma implementation elsewhere, and deducing it here would
10634 produce a conflict. */
10635 comdat_linkage (decl1);
10636 DECL_EXTERNAL (decl1) = 0;
10637 DECL_INTERFACE_KNOWN (decl1) = 1;
10638 DECL_DEFER_OUTPUT (decl1) = 1;
10640 else
10642 /* This is a definition, not a reference.
10643 So clear DECL_EXTERNAL. */
10644 DECL_EXTERNAL (decl1) = 0;
10646 if ((DECL_DECLARED_INLINE_P (decl1)
10647 || DECL_TEMPLATE_INSTANTIATION (decl1))
10648 && ! DECL_INTERFACE_KNOWN (decl1)
10649 /* Don't try to defer nested functions for now. */
10650 && ! decl_function_context (decl1))
10651 DECL_DEFER_OUTPUT (decl1) = 1;
10652 else
10653 DECL_INTERFACE_KNOWN (decl1) = 1;
10656 /* Determine the ELF visibility attribute for the function. We must not
10657 do this before calling "pushdecl", as we must allow "duplicate_decls"
10658 to merge any attributes appropriately. We also need to wait until
10659 linkage is set. */
10660 if (!DECL_CLONED_FUNCTION_P (decl1))
10661 determine_visibility (decl1);
10663 begin_scope (sk_function_parms, decl1);
10665 ++function_depth;
10667 if (DECL_DESTRUCTOR_P (decl1)
10668 || (DECL_CONSTRUCTOR_P (decl1)
10669 && targetm.cxx.cdtor_returns_this ()))
10671 cdtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
10672 DECL_CONTEXT (cdtor_label) = current_function_decl;
10675 start_fname_decls ();
10677 store_parm_decls (current_function_parms);
10681 /* Like start_preparsed_function, except that instead of a
10682 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
10684 Returns 1 on success. If the DECLARATOR is not suitable for a function
10685 (it defines a datum instead), we return 0, which tells
10686 yyparse to report a parse error. */
10689 start_function (cp_decl_specifier_seq *declspecs,
10690 const cp_declarator *declarator,
10691 tree attrs)
10693 tree decl1;
10695 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
10696 /* If the declarator is not suitable for a function definition,
10697 cause a syntax error. */
10698 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
10699 return 0;
10701 if (DECL_MAIN_P (decl1))
10702 /* main must return int. grokfndecl should have corrected it
10703 (and issued a diagnostic) if the user got it wrong. */
10704 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
10705 integer_type_node));
10707 start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
10709 return 1;
10712 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
10713 FN. */
10715 static bool
10716 use_eh_spec_block (tree fn)
10718 return (flag_exceptions && flag_enforce_eh_specs
10719 && !processing_template_decl
10720 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
10721 /* We insert the EH_SPEC_BLOCK only in the original
10722 function; then, it is copied automatically to the
10723 clones. */
10724 && !DECL_CLONED_FUNCTION_P (fn)
10725 /* Implicitly-generated constructors and destructors have
10726 exception specifications. However, those specifications
10727 are the union of the possible exceptions specified by the
10728 constructors/destructors for bases and members, so no
10729 unallowed exception will ever reach this function. By
10730 not creating the EH_SPEC_BLOCK we save a little memory,
10731 and we avoid spurious warnings about unreachable
10732 code. */
10733 && !DECL_ARTIFICIAL (fn));
10736 /* Store the parameter declarations into the current function declaration.
10737 This is called after parsing the parameter declarations, before
10738 digesting the body of the function.
10740 Also install to binding contour return value identifier, if any. */
10742 static void
10743 store_parm_decls (tree current_function_parms)
10745 tree fndecl = current_function_decl;
10746 tree parm;
10748 /* This is a chain of any other decls that came in among the parm
10749 declarations. If a parm is declared with enum {foo, bar} x;
10750 then CONST_DECLs for foo and bar are put here. */
10751 tree nonparms = NULL_TREE;
10753 if (current_function_parms)
10755 /* This case is when the function was defined with an ANSI prototype.
10756 The parms already have decls, so we need not do anything here
10757 except record them as in effect
10758 and complain if any redundant old-style parm decls were written. */
10760 tree specparms = current_function_parms;
10761 tree next;
10763 /* Must clear this because it might contain TYPE_DECLs declared
10764 at class level. */
10765 current_binding_level->names = NULL;
10767 /* If we're doing semantic analysis, then we'll call pushdecl
10768 for each of these. We must do them in reverse order so that
10769 they end in the correct forward order. */
10770 specparms = nreverse (specparms);
10772 for (parm = specparms; parm; parm = next)
10774 next = TREE_CHAIN (parm);
10775 if (TREE_CODE (parm) == PARM_DECL)
10777 if (DECL_NAME (parm) == NULL_TREE
10778 || TREE_CODE (parm) != VOID_TYPE)
10779 pushdecl (parm);
10780 else
10781 error ("parameter %qD declared void", parm);
10783 else
10785 /* If we find an enum constant or a type tag,
10786 put it aside for the moment. */
10787 TREE_CHAIN (parm) = NULL_TREE;
10788 nonparms = chainon (nonparms, parm);
10792 /* Get the decls in their original chain order and record in the
10793 function. This is all and only the PARM_DECLs that were
10794 pushed into scope by the loop above. */
10795 DECL_ARGUMENTS (fndecl) = getdecls ();
10797 else
10798 DECL_ARGUMENTS (fndecl) = NULL_TREE;
10800 /* Now store the final chain of decls for the arguments
10801 as the decl-chain of the current lexical scope.
10802 Put the enumerators in as well, at the front so that
10803 DECL_ARGUMENTS is not modified. */
10804 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
10806 if (use_eh_spec_block (current_function_decl))
10807 current_eh_spec_block = begin_eh_spec_block ();
10811 /* We have finished doing semantic analysis on DECL, but have not yet
10812 generated RTL for its body. Save away our current state, so that
10813 when we want to generate RTL later we know what to do. */
10815 static void
10816 save_function_data (tree decl)
10818 struct language_function *f;
10820 /* Save the language-specific per-function data so that we can
10821 get it back when we really expand this function. */
10822 gcc_assert (!DECL_PENDING_INLINE_P (decl));
10824 /* Make a copy. */
10825 f = GGC_NEW (struct language_function);
10826 memcpy (f, cp_function_chain, sizeof (struct language_function));
10827 DECL_SAVED_FUNCTION_DATA (decl) = f;
10829 /* Clear out the bits we don't need. */
10830 f->base.x_stmt_tree.x_cur_stmt_list = NULL_TREE;
10831 f->bindings = NULL;
10832 f->x_local_names = NULL;
10836 /* Set the return value of the constructor (if present). */
10838 static void
10839 finish_constructor_body (void)
10841 tree val;
10842 tree exprstmt;
10844 if (targetm.cxx.cdtor_returns_this ())
10846 /* Any return from a constructor will end up here. */
10847 add_stmt (build_stmt (LABEL_EXPR, cdtor_label));
10849 val = DECL_ARGUMENTS (current_function_decl);
10850 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
10851 DECL_RESULT (current_function_decl), val);
10852 /* Return the address of the object. */
10853 exprstmt = build_stmt (RETURN_EXPR, val);
10854 add_stmt (exprstmt);
10858 /* Do all the processing for the beginning of a destructor; set up the
10859 vtable pointers and cleanups for bases and members. */
10861 static void
10862 begin_destructor_body (void)
10864 tree compound_stmt;
10866 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
10867 issued an error message. We still want to try to process the
10868 body of the function, but initialize_vtbl_ptrs will crash if
10869 TYPE_BINFO is NULL. */
10870 if (COMPLETE_TYPE_P (current_class_type))
10872 compound_stmt = begin_compound_stmt (0);
10873 /* Make all virtual function table pointers in non-virtual base
10874 classes point to CURRENT_CLASS_TYPE's virtual function
10875 tables. */
10876 initialize_vtbl_ptrs (current_class_ptr);
10877 finish_compound_stmt (compound_stmt);
10879 /* And insert cleanups for our bases and members so that they
10880 will be properly destroyed if we throw. */
10881 push_base_cleanups ();
10885 /* At the end of every destructor we generate code to delete the object if
10886 necessary. Do that now. */
10888 static void
10889 finish_destructor_body (void)
10891 tree exprstmt;
10893 /* Any return from a destructor will end up here; that way all base
10894 and member cleanups will be run when the function returns. */
10895 add_stmt (build_stmt (LABEL_EXPR, cdtor_label));
10897 /* In a virtual destructor, we must call delete. */
10898 if (DECL_VIRTUAL_P (current_function_decl))
10900 tree if_stmt;
10901 tree virtual_size = cxx_sizeof (current_class_type);
10903 /* [class.dtor]
10905 At the point of definition of a virtual destructor (including
10906 an implicit definition), non-placement operator delete shall
10907 be looked up in the scope of the destructor's class and if
10908 found shall be accessible and unambiguous. */
10909 exprstmt = build_op_delete_call(DELETE_EXPR, current_class_ptr,
10910 virtual_size,
10911 /*global_p=*/false,
10912 /*placement=*/NULL_TREE,
10913 /*alloc_fn=*/NULL_TREE);
10915 if_stmt = begin_if_stmt ();
10916 finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
10917 current_in_charge_parm,
10918 integer_one_node),
10919 if_stmt);
10920 finish_expr_stmt (exprstmt);
10921 finish_then_clause (if_stmt);
10922 finish_if_stmt (if_stmt);
10925 if (targetm.cxx.cdtor_returns_this ())
10927 tree val;
10929 val = DECL_ARGUMENTS (current_function_decl);
10930 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
10931 DECL_RESULT (current_function_decl), val);
10932 /* Return the address of the object. */
10933 exprstmt = build_stmt (RETURN_EXPR, val);
10934 add_stmt (exprstmt);
10938 /* Do the necessary processing for the beginning of a function body, which
10939 in this case includes member-initializers, but not the catch clauses of
10940 a function-try-block. Currently, this means opening a binding level
10941 for the member-initializers (in a ctor) and member cleanups (in a dtor). */
10943 tree
10944 begin_function_body (void)
10946 tree stmt;
10948 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
10949 return NULL_TREE;
10951 if (processing_template_decl)
10952 /* Do nothing now. */;
10953 else
10954 /* Always keep the BLOCK node associated with the outermost pair of
10955 curly braces of a function. These are needed for correct
10956 operation of dwarfout.c. */
10957 keep_next_level (true);
10959 stmt = begin_compound_stmt (BCS_FN_BODY);
10961 if (processing_template_decl)
10962 /* Do nothing now. */;
10963 else if (DECL_DESTRUCTOR_P (current_function_decl))
10964 begin_destructor_body ();
10966 return stmt;
10969 /* Do the processing for the end of a function body. Currently, this means
10970 closing out the cleanups for fully-constructed bases and members, and in
10971 the case of the destructor, deleting the object if desired. Again, this
10972 is only meaningful for [cd]tors, since they are the only functions where
10973 there is a significant distinction between the main body and any
10974 function catch clauses. Handling, say, main() return semantics here
10975 would be wrong, as flowing off the end of a function catch clause for
10976 main() would also need to return 0. */
10978 void
10979 finish_function_body (tree compstmt)
10981 if (compstmt == NULL_TREE)
10982 return;
10984 /* Close the block. */
10985 finish_compound_stmt (compstmt);
10987 if (processing_template_decl)
10988 /* Do nothing now. */;
10989 else if (DECL_CONSTRUCTOR_P (current_function_decl))
10990 finish_constructor_body ();
10991 else if (DECL_DESTRUCTOR_P (current_function_decl))
10992 finish_destructor_body ();
10995 /* Given a function, returns the BLOCK corresponding to the outermost level
10996 of curly braces, skipping the artificial block created for constructor
10997 initializers. */
10999 static tree
11000 outer_curly_brace_block (tree fndecl)
11002 tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl));
11003 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
11004 /* Skip the artificial function body block. */
11005 block = BLOCK_SUBBLOCKS (block);
11006 return block;
11009 /* Finish up a function declaration and compile that function
11010 all the way to assembler language output. The free the storage
11011 for the function definition.
11013 FLAGS is a bitwise or of the following values:
11014 2 - INCLASS_INLINE
11015 We just finished processing the body of an in-class inline
11016 function definition. (This processing will have taken place
11017 after the class definition is complete.) */
11019 tree
11020 finish_function (int flags)
11022 tree fndecl = current_function_decl;
11023 tree fntype, ctype = NULL_TREE;
11024 int inclass_inline = (flags & 2) != 0;
11025 int nested;
11027 /* When we get some parse errors, we can end up without a
11028 current_function_decl, so cope. */
11029 if (fndecl == NULL_TREE)
11030 return error_mark_node;
11032 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
11033 && DECL_VIRTUAL_P (fndecl)
11034 && !processing_template_decl)
11036 tree fnclass = DECL_CONTEXT (fndecl);
11037 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
11038 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
11041 nested = function_depth > 1;
11042 fntype = TREE_TYPE (fndecl);
11044 /* TREE_READONLY (fndecl) = 1;
11045 This caused &foo to be of type ptr-to-const-function
11046 which then got a warning when stored in a ptr-to-function variable. */
11048 gcc_assert (building_stmt_tree ());
11050 /* For a cloned function, we've already got all the code we need;
11051 there's no need to add any extra bits. */
11052 if (!DECL_CLONED_FUNCTION_P (fndecl))
11054 if (DECL_MAIN_P (current_function_decl))
11056 tree stmt;
11058 /* Make it so that `main' always returns 0 by default (or
11059 1 for VMS). */
11060 #if VMS_TARGET
11061 stmt = finish_return_stmt (integer_one_node);
11062 #else
11063 stmt = finish_return_stmt (integer_zero_node);
11064 #endif
11065 /* Hack. We don't want the middle-end to warn that this
11066 return is unreachable, so put the statement on the
11067 special line 0. */
11068 #ifdef USE_MAPPED_LOCATION
11069 SET_EXPR_LOCATION (stmt, UNKNOWN_LOCATION);
11070 #else
11071 annotate_with_file_line (stmt, input_filename, 0);
11072 #endif
11075 if (use_eh_spec_block (current_function_decl))
11076 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
11077 (TREE_TYPE (current_function_decl)),
11078 current_eh_spec_block);
11081 /* If we're saving up tree structure, tie off the function now. */
11082 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11084 finish_fname_decls ();
11086 /* If this function can't throw any exceptions, remember that. */
11087 if (!processing_template_decl
11088 && !cp_function_chain->can_throw
11089 && !flag_non_call_exceptions
11090 && targetm.binds_local_p (fndecl))
11091 TREE_NOTHROW (fndecl) = 1;
11093 /* This must come after expand_function_end because cleanups might
11094 have declarations (from inline functions) that need to go into
11095 this function's blocks. */
11097 /* If the current binding level isn't the outermost binding level
11098 for this function, either there is a bug, or we have experienced
11099 syntax errors and the statement tree is malformed. */
11100 if (current_binding_level->kind != sk_function_parms)
11102 /* Make sure we have already experienced errors. */
11103 gcc_assert (errorcount);
11105 /* Throw away the broken statement tree and extra binding
11106 levels. */
11107 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
11109 while (current_binding_level->kind != sk_function_parms)
11111 if (current_binding_level->kind == sk_class)
11112 pop_nested_class ();
11113 else
11114 poplevel (0, 0, 0);
11117 poplevel (1, 0, 1);
11119 /* Statements should always be full-expressions at the outermost set
11120 of curly braces for a function. */
11121 gcc_assert (stmts_are_full_exprs_p ());
11123 /* Set up the named return value optimization, if we can. Candidate
11124 variables are selected in check_return_value. */
11125 if (current_function_return_value)
11127 tree r = current_function_return_value;
11128 tree outer;
11130 if (r != error_mark_node
11131 /* This is only worth doing for fns that return in memory--and
11132 simpler, since we don't have to worry about promoted modes. */
11133 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
11134 /* Only allow this for variables declared in the outer scope of
11135 the function so we know that their lifetime always ends with a
11136 return; see g++.dg/opt/nrv6.C. We could be more flexible if
11137 we were to do this optimization in tree-ssa. */
11138 && (outer = outer_curly_brace_block (fndecl))
11139 && chain_member (r, BLOCK_VARS (outer)))
11140 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
11142 current_function_return_value = NULL_TREE;
11145 /* Remember that we were in class scope. */
11146 if (current_class_name)
11147 ctype = current_class_type;
11149 /* Must mark the RESULT_DECL as being in this function. */
11150 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11152 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
11153 to the FUNCTION_DECL node itself. */
11154 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11156 /* Save away current state, if appropriate. */
11157 if (!processing_template_decl)
11158 save_function_data (fndecl);
11160 /* Complain if there's just no return statement. */
11161 if (warn_return_type
11162 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
11163 && !dependent_type_p (TREE_TYPE (fntype))
11164 && !current_function_returns_value && !current_function_returns_null
11165 /* Don't complain if we abort or throw. */
11166 && !current_function_returns_abnormally
11167 && !DECL_NAME (DECL_RESULT (fndecl))
11168 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
11169 inline function, as we might never be compiled separately. */
11170 && (DECL_INLINE (fndecl) || processing_template_decl)
11171 /* Structor return values (if any) are set by the compiler. */
11172 && !DECL_CONSTRUCTOR_P (fndecl)
11173 && !DECL_DESTRUCTOR_P (fndecl))
11174 warning (OPT_Wreturn_type, "no return statement in function returning non-void");
11176 /* Store the end of the function, so that we get good line number
11177 info for the epilogue. */
11178 cfun->function_end_locus = input_location;
11180 /* Genericize before inlining. */
11181 if (!processing_template_decl)
11183 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
11184 cp_genericize (fndecl);
11185 /* Clear out the bits we don't need. */
11186 f->x_current_class_ptr = NULL;
11187 f->x_current_class_ref = NULL;
11188 f->x_eh_spec_block = NULL;
11189 f->x_in_charge_parm = NULL;
11190 f->x_vtt_parm = NULL;
11191 f->x_return_value = NULL;
11192 f->bindings = NULL;
11193 f->extern_decl_map = NULL;
11195 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
11196 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
11198 /* Clear out the bits we don't need. */
11199 local_names = NULL;
11201 /* We're leaving the context of this function, so zap cfun. It's still in
11202 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
11203 cfun = NULL;
11204 current_function_decl = NULL;
11206 /* If this is an in-class inline definition, we may have to pop the
11207 bindings for the template parameters that we added in
11208 maybe_begin_member_template_processing when start_function was
11209 called. */
11210 if (inclass_inline)
11211 maybe_end_member_template_processing ();
11213 /* Leave the scope of the class. */
11214 if (ctype)
11215 pop_nested_class ();
11217 --function_depth;
11219 /* Clean up. */
11220 if (! nested)
11221 /* Let the error reporting routines know that we're outside a
11222 function. For a nested function, this value is used in
11223 cxx_pop_function_context and then reset via pop_function_context. */
11224 current_function_decl = NULL_TREE;
11226 return fndecl;
11229 /* Create the FUNCTION_DECL for a function definition.
11230 DECLSPECS and DECLARATOR are the parts of the declaration;
11231 they describe the return type and the name of the function,
11232 but twisted together in a fashion that parallels the syntax of C.
11234 This function creates a binding context for the function body
11235 as well as setting up the FUNCTION_DECL in current_function_decl.
11237 Returns a FUNCTION_DECL on success.
11239 If the DECLARATOR is not suitable for a function (it defines a datum
11240 instead), we return 0, which tells yyparse to report a parse error.
11242 May return void_type_node indicating that this method is actually
11243 a friend. See grokfield for more details.
11245 Came here with a `.pushlevel' .
11247 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
11248 CHANGES TO CODE IN `grokfield'. */
11250 tree
11251 start_method (cp_decl_specifier_seq *declspecs,
11252 const cp_declarator *declarator, tree attrlist)
11254 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
11255 &attrlist);
11257 if (fndecl == error_mark_node)
11258 return error_mark_node;
11260 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
11262 error ("invalid member function declaration");
11263 return error_mark_node;
11266 if (attrlist)
11267 cplus_decl_attributes (&fndecl, attrlist, 0);
11269 /* Pass friends other than inline friend functions back. */
11270 if (fndecl == void_type_node)
11271 return fndecl;
11273 if (DECL_IN_AGGR_P (fndecl))
11275 if (DECL_CONTEXT (fndecl)
11276 && TREE_CODE (DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
11277 error ("%qD is already defined in class %qT", fndecl,
11278 DECL_CONTEXT (fndecl));
11279 return error_mark_node;
11282 check_template_shadow (fndecl);
11284 DECL_DECLARED_INLINE_P (fndecl) = 1;
11285 if (flag_default_inline)
11286 DECL_INLINE (fndecl) = 1;
11288 /* We process method specializations in finish_struct_1. */
11289 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
11291 fndecl = push_template_decl (fndecl);
11292 if (fndecl == error_mark_node)
11293 return fndecl;
11296 if (! DECL_FRIEND_P (fndecl))
11298 if (TREE_CHAIN (fndecl))
11300 fndecl = copy_node (fndecl);
11301 TREE_CHAIN (fndecl) = NULL_TREE;
11305 finish_decl (fndecl, NULL_TREE, NULL_TREE);
11307 /* Make a place for the parms. */
11308 begin_scope (sk_function_parms, fndecl);
11310 DECL_IN_AGGR_P (fndecl) = 1;
11311 return fndecl;
11314 /* Go through the motions of finishing a function definition.
11315 We don't compile this method until after the whole class has
11316 been processed.
11318 FINISH_METHOD must return something that looks as though it
11319 came from GROKFIELD (since we are defining a method, after all).
11321 This is called after parsing the body of the function definition.
11322 STMTS is the chain of statements that makes up the function body.
11324 DECL is the ..._DECL that `start_method' provided. */
11326 tree
11327 finish_method (tree decl)
11329 tree fndecl = decl;
11330 tree old_initial;
11332 tree link;
11334 if (decl == void_type_node)
11335 return decl;
11337 old_initial = DECL_INITIAL (fndecl);
11339 /* Undo the level for the parms (from start_method).
11340 This is like poplevel, but it causes nothing to be
11341 saved. Saving information here confuses symbol-table
11342 output routines. Besides, this information will
11343 be correctly output when this method is actually
11344 compiled. */
11346 /* Clear out the meanings of the local variables of this level;
11347 also record in each decl which block it belongs to. */
11349 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
11351 if (DECL_NAME (link) != NULL_TREE)
11352 pop_binding (DECL_NAME (link), link);
11353 gcc_assert (TREE_CODE (link) != FUNCTION_DECL);
11354 DECL_CONTEXT (link) = NULL_TREE;
11357 poplevel (0, 0, 0);
11359 DECL_INITIAL (fndecl) = old_initial;
11361 /* We used to check if the context of FNDECL was different from
11362 current_class_type as another way to get inside here. This didn't work
11363 for String.cc in libg++. */
11364 if (DECL_FRIEND_P (fndecl))
11366 VEC_safe_push (tree, gc, CLASSTYPE_INLINE_FRIENDS (current_class_type),
11367 fndecl);
11368 decl = void_type_node;
11371 return decl;
11375 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
11376 we can lay it out later, when and if its type becomes complete. */
11378 void
11379 maybe_register_incomplete_var (tree var)
11381 gcc_assert (TREE_CODE (var) == VAR_DECL);
11383 /* Keep track of variables with incomplete types. */
11384 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
11385 && DECL_EXTERNAL (var))
11387 tree inner_type = TREE_TYPE (var);
11389 while (TREE_CODE (inner_type) == ARRAY_TYPE)
11390 inner_type = TREE_TYPE (inner_type);
11391 inner_type = TYPE_MAIN_VARIANT (inner_type);
11393 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
11394 /* RTTI TD entries are created while defining the type_info. */
11395 || (TYPE_LANG_SPECIFIC (inner_type)
11396 && TYPE_BEING_DEFINED (inner_type)))
11397 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
11401 /* Called when a class type (given by TYPE) is defined. If there are
11402 any existing VAR_DECLs whose type hsa been completed by this
11403 declaration, update them now. */
11405 void
11406 complete_vars (tree type)
11408 tree *list = &incomplete_vars;
11410 gcc_assert (CLASS_TYPE_P (type));
11411 while (*list)
11413 if (same_type_p (type, TREE_PURPOSE (*list)))
11415 tree var = TREE_VALUE (*list);
11416 tree type = TREE_TYPE (var);
11417 /* Complete the type of the variable. The VAR_DECL itself
11418 will be laid out in expand_expr. */
11419 complete_type (type);
11420 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
11421 /* Remove this entry from the list. */
11422 *list = TREE_CHAIN (*list);
11424 else
11425 list = &TREE_CHAIN (*list);
11428 /* Check for pending declarations which may have abstract type. */
11429 complete_type_check_abstract (type);
11432 /* If DECL is of a type which needs a cleanup, build that cleanup
11433 here. */
11435 tree
11436 cxx_maybe_build_cleanup (tree decl)
11438 tree type = TREE_TYPE (decl);
11440 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
11442 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
11443 tree rval;
11444 bool has_vbases = (TREE_CODE (type) == RECORD_TYPE
11445 && CLASSTYPE_VBASECLASSES (type));
11447 if (TREE_CODE (type) == ARRAY_TYPE)
11448 rval = decl;
11449 else
11451 cxx_mark_addressable (decl);
11452 rval = build_unary_op (ADDR_EXPR, decl, 0);
11455 /* Optimize for space over speed here. */
11456 if (!has_vbases || flag_expensive_optimizations)
11457 flags |= LOOKUP_NONVIRTUAL;
11459 rval = build_delete (TREE_TYPE (rval), rval,
11460 sfk_complete_destructor, flags, 0);
11462 return rval;
11464 return NULL_TREE;
11467 /* When a stmt has been parsed, this function is called. */
11469 void
11470 finish_stmt (void)
11474 /* DECL was originally constructed as a non-static member function,
11475 but turned out to be static. Update it accordingly. */
11477 void
11478 revert_static_member_fn (tree decl)
11480 tree tmp;
11481 tree function = TREE_TYPE (decl);
11482 tree args = TYPE_ARG_TYPES (function);
11484 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
11485 != TYPE_UNQUALIFIED)
11486 error ("static member function %q#D declared with type qualifiers", decl);
11488 args = TREE_CHAIN (args);
11489 tmp = build_function_type (TREE_TYPE (function), args);
11490 tmp = build_qualified_type (tmp, cp_type_quals (function));
11491 tmp = build_exception_variant (tmp,
11492 TYPE_RAISES_EXCEPTIONS (function));
11493 TREE_TYPE (decl) = tmp;
11494 if (DECL_ARGUMENTS (decl))
11495 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
11496 DECL_STATIC_FUNCTION_P (decl) = 1;
11499 /* Initialize the variables used during compilation of a C++
11500 function. */
11502 void
11503 cxx_push_function_context (struct function * f)
11505 struct language_function *p = GGC_CNEW (struct language_function);
11506 f->language = p;
11508 /* Whenever we start a new function, we destroy temporaries in the
11509 usual way. */
11510 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
11512 if (f->decl)
11514 tree fn = f->decl;
11516 if (DECL_SAVED_FUNCTION_DATA (fn))
11518 /* If we already parsed this function, and we're just expanding it
11519 now, restore saved state. */
11520 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
11522 /* We don't need the saved data anymore. Unless this is an inline
11523 function; we need the named return value info for
11524 declare_return_variable. */
11525 if (! DECL_INLINE (fn))
11526 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
11531 /* Free the language-specific parts of F, now that we've finished
11532 compiling the function. */
11534 void
11535 cxx_pop_function_context (struct function * f)
11537 f->language = 0;
11540 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
11541 one of the language-independent trees. */
11543 enum cp_tree_node_structure_enum
11544 cp_tree_node_structure (union lang_tree_node * t)
11546 switch (TREE_CODE (&t->generic))
11548 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
11549 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
11550 case OVERLOAD: return TS_CP_OVERLOAD;
11551 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
11552 case TINST_LEVEL: return TS_CP_TINST_LEVEL;
11553 case PTRMEM_CST: return TS_CP_PTRMEM;
11554 case BASELINK: return TS_CP_BASELINK;
11555 default: return TS_CP_GENERIC;
11559 /* Build the void_list_node (void_type_node having been created). */
11560 tree
11561 build_void_list_node (void)
11563 tree t = build_tree_list (NULL_TREE, void_type_node);
11564 return t;
11567 bool
11568 cp_missing_noreturn_ok_p (tree decl)
11570 /* A missing noreturn is ok for the `main' function. */
11571 return DECL_MAIN_P (decl);
11574 /* Return the COMDAT group into which DECL should be placed. */
11576 const char *
11577 cxx_comdat_group (tree decl)
11579 tree name;
11581 /* Virtual tables, construction virtual tables, and virtual table
11582 tables all go in a single COMDAT group, named after the primary
11583 virtual table. */
11584 if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
11585 name = DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (DECL_CONTEXT (decl)));
11586 /* For all other DECLs, the COMDAT group is the mangled name of the
11587 declaration itself. */
11588 else
11590 while (DECL_THUNK_P (decl))
11592 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
11593 into the same section as the target function. In that case
11594 we must return target's name. */
11595 tree target = THUNK_TARGET (decl);
11596 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
11597 && DECL_SECTION_NAME (target) != NULL
11598 && DECL_ONE_ONLY (target))
11599 decl = target;
11600 else
11601 break;
11603 name = DECL_ASSEMBLER_NAME (decl);
11606 return IDENTIFIER_POINTER (name);
11609 #include "gt-cp-decl.h"