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)
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. */
33 #include "coretypes.h"
40 #include "tree-inline.h"
50 #include "diagnostic.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 *,
66 static void record_unknown_type (tree, const char *);
67 static tree builtin_function_1 (const char *, tree, tree,
68 enum built_in_function code,
69 enum built_in_class cl, const char *,
71 static tree build_library_fn_1 (tree, enum tree_code, tree);
72 static int member_function_or_else (tree, tree, enum overload_flags);
73 static void bad_specifiers (tree, const char *, int, int, int, int,
75 static void check_for_uninitialized_const_var (tree);
76 static hashval_t typename_hash (const void *);
77 static int typename_compare (const void *, const void *);
78 static tree local_variable_p_walkfn (tree *, int *, void *);
79 static tree record_builtin_java_type (const char *, int);
80 static const char *tag_name (enum tag_types);
81 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
82 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
83 static void maybe_deduce_size_from_array_init (tree, tree);
84 static void layout_var_decl (tree);
85 static void maybe_commonize_var (tree);
86 static tree check_initializer (tree, tree, int, tree *);
87 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
88 static void save_function_data (tree);
89 static void check_function_type (tree, tree);
90 static void finish_constructor_body (void);
91 static void begin_destructor_body (void);
92 static void finish_destructor_body (void);
93 static tree create_array_type_for_decl (tree, tree, tree);
94 static tree get_atexit_node (void);
95 static tree get_dso_handle_node (void);
96 static tree start_cleanup_fn (void);
97 static void end_cleanup_fn (void);
98 static tree cp_make_fname_decl (tree, int);
99 static void initialize_predefined_identifiers (void);
100 static tree check_special_function_return_type
101 (special_function_kind, tree, tree);
102 static tree push_cp_library_fn (enum tree_code, tree);
103 static tree build_cp_library_fn (tree, enum tree_code, tree);
104 static void store_parm_decls (tree);
105 static void initialize_local_var (tree, tree);
106 static void expand_static_init (tree, tree);
107 static tree next_initializable_field (tree);
109 /* The following symbols are subsumed in the cp_global_trees array, and
110 listed here individually for documentation purposes.
113 tree wchar_decl_node;
115 tree vtable_entry_type;
116 tree delta_type_node;
117 tree __t_desc_type_node;
119 tree class_type_node;
120 tree unknown_type_node;
122 Array type `vtable_entry_type[]'
125 tree vtbl_ptr_type_node;
132 A FUNCTION_DECL which can call `abort'. Not necessarily the
133 one that the user will declare, but sufficient to be called
134 by routines that want to abort the program.
138 The FUNCTION_DECL for the default `::operator delete'.
140 tree global_delete_fndecl;
143 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
144 tree tinfo_var_id; */
146 tree cp_global_trees[CPTI_MAX];
148 /* Indicates that there is a type value in some namespace, although
149 that is not necessarily in scope at the moment. */
151 tree global_type_node;
153 /* The node that holds the "name" of the global scope. */
154 tree global_scope_name;
156 #define local_names cp_function_chain->x_local_names
158 /* A list of objects which have constructors or destructors
159 which reside in the global scope. The decl is stored in
160 the TREE_VALUE slot and the initializer is stored
161 in the TREE_PURPOSE slot. */
162 tree static_aggregates;
166 /* A node for the integer constants 2, and 3. */
168 tree integer_two_node, integer_three_node;
170 /* Used only for jumps to as-yet undefined labels, since jumps to
171 defined labels can have their validity checked immediately. */
173 struct named_label_use_entry GTY(())
175 struct named_label_use_entry *next;
176 /* The binding level to which this entry is *currently* attached.
177 This is initially the binding level in which the goto appeared,
178 but is modified as scopes are closed. */
179 struct cp_binding_level *binding_level;
180 /* The head of the names list that was current when the goto appeared,
181 or the inner scope popped. These are the decls that will *not* be
182 skipped when jumping to the label. */
184 /* The location of the goto, for error reporting. */
185 location_t o_goto_locus;
186 /* True if an OpenMP structured block scope has been closed since
187 the goto appeared. This means that the branch from the label will
188 illegally exit an OpenMP scope. */
192 /* A list of all LABEL_DECLs in the function that have names. Here so
193 we can clear out their names' definitions at the end of the
194 function, and so we can check the validity of jumps to these labels. */
196 struct named_label_entry GTY(())
198 /* The decl itself. */
201 /* The binding level to which the label is *currently* attached.
202 This is initially set to the binding level in which the label
203 is defined, but is modified as scopes are closed. */
204 struct cp_binding_level *binding_level;
205 /* The head of the names list that was current when the label was
206 defined, or the inner scope popped. These are the decls that will
207 be skipped when jumping to the label. */
209 /* A tree list of all decls from all binding levels that would be
210 crossed by a backward branch to the label. */
213 /* A list of uses of the label, before the label is defined. */
214 struct named_label_use_entry *uses;
216 /* The following bits are set after the label is defined, and are
217 updated as scopes are popped. They indicate that a backward jump
218 to the label will illegally enter a scope of the given flavor. */
224 #define named_labels cp_function_chain->x_named_labels
226 /* The number of function bodies which we are currently processing.
227 (Zero if we are at namespace scope, one inside the body of a
228 function, two inside the body of a function in a local class, etc.) */
231 /* States indicating how grokdeclarator() should handle declspecs marked
232 with __attribute__((deprecated)). An object declared as
233 __attribute__((deprecated)) suppresses warnings of uses of other
235 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
236 /* An object declared as __attribute__((unavailable)) suppresses
237 any reports of being declared with unavailable or deprecated
239 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
241 enum deprecated_states {
244 /* APPLE LOCAL "unavailable" attribute (radar 2809697) */
245 , DEPRECATED_UNAVAILABLE_SUPPRESS
248 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
251 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
252 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
253 time the VAR_DECL was declared, the type was incomplete. */
255 static GTY(()) tree incomplete_vars;
257 /* Returns the kind of template specialization we are currently
258 processing, given that it's declaration contained N_CLASS_SCOPES
259 explicit scope qualifications. */
262 current_tmpl_spec_kind (int n_class_scopes)
264 int n_template_parm_scopes = 0;
265 int seen_specialization_p = 0;
266 int innermost_specialization_p = 0;
267 struct cp_binding_level *b;
269 /* Scan through the template parameter scopes. */
270 for (b = current_binding_level;
271 b->kind == sk_template_parms;
274 /* If we see a specialization scope inside a parameter scope,
275 then something is wrong. That corresponds to a declaration
278 template <class T> template <> ...
280 which is always invalid since [temp.expl.spec] forbids the
281 specialization of a class member template if the enclosing
282 class templates are not explicitly specialized as well. */
283 if (b->explicit_spec_p)
285 if (n_template_parm_scopes == 0)
286 innermost_specialization_p = 1;
288 seen_specialization_p = 1;
290 else if (seen_specialization_p == 1)
291 return tsk_invalid_member_spec;
293 ++n_template_parm_scopes;
296 /* Handle explicit instantiations. */
297 if (processing_explicit_instantiation)
299 if (n_template_parm_scopes != 0)
300 /* We've seen a template parameter list during an explicit
301 instantiation. For example:
303 template <class T> template void f(int);
305 This is erroneous. */
306 return tsk_invalid_expl_inst;
308 return tsk_expl_inst;
311 if (n_template_parm_scopes < n_class_scopes)
312 /* We've not seen enough template headers to match all the
313 specialized classes present. For example:
315 template <class T> void R<T>::S<T>::f(int);
317 This is invalid; there needs to be one set of template
318 parameters for each class. */
319 return tsk_insufficient_parms;
320 else if (n_template_parm_scopes == n_class_scopes)
321 /* We're processing a non-template declaration (even though it may
322 be a member of a template class.) For example:
324 template <class T> void S<T>::f(int);
326 The `class T' maches the `S<T>', leaving no template headers
327 corresponding to the `f'. */
329 else if (n_template_parm_scopes > n_class_scopes + 1)
330 /* We've got too many template headers. For example:
332 template <> template <class T> void f (T);
334 There need to be more enclosing classes. */
335 return tsk_excessive_parms;
337 /* This must be a template. It's of the form:
339 template <class T> template <class U> void S<T>::f(U);
341 This is a specialization if the innermost level was a
342 specialization; otherwise it's just a definition of the
344 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
347 /* Exit the current scope. */
355 /* When a label goes out of scope, check to see if that label was used
356 in a valid manner, and issue any appropriate warnings or errors. */
359 pop_label (tree label, tree old_value)
361 if (!processing_template_decl)
363 if (DECL_INITIAL (label) == NULL_TREE)
367 error ("label %q+D used but not defined", label);
368 #ifdef USE_MAPPED_LOCATION
369 location = input_location; /* FIXME want (input_filename, (line)0) */
371 location.file = input_filename;
374 /* Avoid crashing later. */
375 define_label (location, DECL_NAME (label));
377 else if (!TREE_USED (label))
378 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
381 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
384 /* At the end of a function, all labels declared within the function
385 go out of scope. BLOCK is the top-level block for the
389 pop_labels_1 (void **slot, void *data)
391 struct named_label_entry *ent = (struct named_label_entry *) *slot;
392 tree block = (tree) data;
394 pop_label (ent->label_decl, NULL_TREE);
396 /* Put the labels into the "variables" of the top-level block,
397 so debugger can see them. */
398 TREE_CHAIN (ent->label_decl) = BLOCK_VARS (block);
399 BLOCK_VARS (block) = ent->label_decl;
401 htab_clear_slot (named_labels, slot);
407 pop_labels (tree block)
411 htab_traverse (named_labels, pop_labels_1, block);
416 /* At the end of a block with local labels, restore the outer definition. */
419 pop_local_label (tree label, tree old_value)
421 struct named_label_entry dummy;
424 pop_label (label, old_value);
426 dummy.label_decl = label;
427 slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
428 htab_clear_slot (named_labels, slot);
431 /* The following two routines are used to interface to Objective-C++.
432 The binding level is purposely treated as an opaque type. */
435 objc_get_current_scope (void)
437 return current_binding_level;
440 /* The following routine is used by the NeXT-style SJLJ exceptions;
441 variables get marked 'volatile' so as to not be clobbered by
442 _setjmp()/_longjmp() calls. All variables in the current scope,
443 as well as parent scopes up to (but not including) ENCLOSING_BLK
444 shall be thusly marked. */
447 objc_mark_locals_volatile (void *enclosing_blk)
449 struct cp_binding_level *scope;
451 for (scope = current_binding_level;
452 scope && scope != enclosing_blk;
453 scope = scope->level_chain)
457 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
458 objc_volatilize_decl (decl);
460 /* Do not climb up past the current function. */
461 if (scope->kind == sk_function_parms)
466 /* Update data for defined and undefined labels when leaving a scope. */
469 poplevel_named_label_1 (void **slot, void *data)
471 struct named_label_entry *ent = (struct named_label_entry *) *slot;
472 struct cp_binding_level *bl = (struct cp_binding_level *) data;
473 struct cp_binding_level *obl = bl->level_chain;
475 if (ent->binding_level == bl)
479 for (decl = ent->names_in_scope; decl; decl = TREE_CHAIN (decl))
480 if (decl_jump_unsafe (decl))
481 ent->bad_decls = tree_cons (NULL, decl, ent->bad_decls);
483 ent->binding_level = obl;
484 ent->names_in_scope = obl->names;
488 ent->in_try_scope = true;
491 ent->in_catch_scope = true;
494 ent->in_omp_scope = true;
502 struct named_label_use_entry *use;
504 for (use = ent->uses; use ; use = use->next)
505 if (use->binding_level == bl)
507 use->binding_level = obl;
508 use->names_in_scope = obl->names;
509 if (bl->kind == sk_omp)
510 use->in_omp_scope = true;
517 /* Exit a binding level.
518 Pop the level off, and restore the state of the identifier-decl mappings
519 that were in effect when this level was entered.
521 If KEEP == 1, this level had explicit declarations, so
522 and create a "block" (a BLOCK node) for the level
523 to record its declarations and subblocks for symbol table output.
525 If FUNCTIONBODY is nonzero, this level is the body of a function,
526 so create a block as if KEEP were set and also clear out all
529 If REVERSE is nonzero, reverse the order of decls before putting
530 them into the BLOCK. */
533 poplevel (int keep, int reverse, int functionbody)
536 /* The chain of decls was accumulated in reverse order.
537 Put it into forward order, just for cleanliness. */
539 int tmp = functionbody;
540 int real_functionbody;
544 int leaving_for_scope;
547 timevar_push (TV_NAME_LOOKUP);
552 gcc_assert (current_binding_level->kind != sk_class);
554 real_functionbody = (current_binding_level->kind == sk_cleanup
555 ? ((functionbody = 0), tmp) : functionbody);
556 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
558 gcc_assert (!VEC_length(cp_class_binding,
559 current_binding_level->class_shadowed));
561 /* We used to use KEEP == 2 to indicate that the new block should go
562 at the beginning of the list of blocks at this binding level,
563 rather than the end. This hack is no longer used. */
564 gcc_assert (keep == 0 || keep == 1);
566 if (current_binding_level->keep)
569 /* Any uses of undefined labels, and any defined labels, now operate
570 under constraints of next binding contour. */
571 if (cfun && !functionbody && named_labels)
572 htab_traverse (named_labels, poplevel_named_label_1,
573 current_binding_level);
575 /* Get the decls in the order they were written.
576 Usually current_binding_level->names is in reverse order.
577 But parameter decls were previously put in forward order. */
580 current_binding_level->names
581 = decls = nreverse (current_binding_level->names);
583 decls = current_binding_level->names;
585 /* If there were any declarations or structure tags in that level,
586 or if this level is a function body,
587 create a BLOCK to record them for the life of this function. */
589 if (keep == 1 || functionbody)
590 block = make_node (BLOCK);
591 if (block != NULL_TREE)
593 BLOCK_VARS (block) = decls;
594 BLOCK_SUBBLOCKS (block) = subblocks;
597 /* In each subblock, record that this is its superior. */
599 for (link = subblocks; link; link = TREE_CHAIN (link))
600 BLOCK_SUPERCONTEXT (link) = block;
602 /* We still support the old for-scope rules, whereby the variables
603 in a for-init statement were in scope after the for-statement
604 ended. We only use the new rules if flag_new_for_scope is
607 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
609 /* Before we remove the declarations first check for unused variables. */
610 if (warn_unused_variable
611 && !processing_template_decl)
612 for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
613 if (TREE_CODE (decl) == VAR_DECL
614 && ! TREE_USED (decl)
615 && ! DECL_IN_SYSTEM_HEADER (decl)
616 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
617 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
619 /* Remove declarations for all the DECLs in this level. */
620 for (link = decls; link; link = TREE_CHAIN (link))
622 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
625 tree name = DECL_NAME (link);
629 ob = outer_binding (name,
630 IDENTIFIER_BINDING (name),
633 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
635 ns_binding = NULL_TREE;
637 if (ob && ob->scope == current_binding_level->level_chain)
638 /* We have something like:
643 and we are leaving the `for' scope. There's no reason to
644 keep the binding of the inner `i' in this case. */
645 pop_binding (name, link);
646 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
647 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
648 /* Here, we have something like:
656 We must pop the for-scope binding so we know what's a
657 type and what isn't. */
658 pop_binding (name, link);
661 /* Mark this VAR_DECL as dead so that we can tell we left it
662 there only for backward compatibility. */
663 DECL_DEAD_FOR_LOCAL (link) = 1;
665 /* Keep track of what should have happened when we
666 popped the binding. */
669 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
670 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
673 /* Add it to the list of dead variables in the next
674 outermost binding to that we can remove these when we
675 leave that binding. */
676 current_binding_level->level_chain->dead_vars_from_for
677 = tree_cons (NULL_TREE, link,
678 current_binding_level->level_chain->
681 /* Although we don't pop the cxx_binding, we do clear
682 its SCOPE since the scope is going away now. */
683 IDENTIFIER_BINDING (name)->scope
684 = current_binding_level->level_chain;
691 /* Remove the binding. */
694 if (TREE_CODE (decl) == TREE_LIST)
695 decl = TREE_VALUE (decl);
698 if (TREE_CODE (name) == OVERLOAD)
699 name = OVL_FUNCTION (name);
701 gcc_assert (DECL_P (name));
702 pop_binding (DECL_NAME (name), decl);
706 /* Remove declarations for any `for' variables from inner scopes
707 that we kept around. */
708 for (link = current_binding_level->dead_vars_from_for;
709 link; link = TREE_CHAIN (link))
710 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
712 /* Restore the IDENTIFIER_TYPE_VALUEs. */
713 for (link = current_binding_level->type_shadowed;
714 link; link = TREE_CHAIN (link))
715 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
717 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
718 for (link = current_binding_level->shadowed_labels;
720 link = TREE_CHAIN (link))
721 pop_local_label (TREE_VALUE (link), TREE_PURPOSE (link));
723 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
724 list if a `using' declaration put them there. The debugging
725 back-ends won't understand OVERLOAD, so we remove them here.
726 Because the BLOCK_VARS are (temporarily) shared with
727 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
728 popped all the bindings. */
733 for (d = &BLOCK_VARS (block); *d; )
735 if (TREE_CODE (*d) == TREE_LIST)
736 *d = TREE_CHAIN (*d);
738 d = &TREE_CHAIN (*d);
742 /* If the level being exited is the top level of a function,
743 check over all the labels. */
746 /* Since this is the top level block of a function, the vars are
747 the function's parameters. Don't leave them in the BLOCK
748 because they are found in the FUNCTION_DECL instead. */
749 BLOCK_VARS (block) = 0;
753 kind = current_binding_level->kind;
754 if (kind == sk_cleanup)
758 /* If this is a temporary binding created for a cleanup, then we'll
759 have pushed a statement list level. Pop that, create a new
760 BIND_EXPR for the block, and insert it into the stream. */
761 stmt = pop_stmt_list (current_binding_level->statement_list);
762 stmt = c_build_bind_expr (block, stmt);
769 /* The current function is being defined, so its DECL_INITIAL
770 should be error_mark_node. */
771 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
772 DECL_INITIAL (current_function_decl) = block;
775 current_binding_level->blocks
776 = chainon (current_binding_level->blocks, block);
778 /* If we did not make a block for the level just exited,
779 any blocks made for inner levels
780 (since they cannot be recorded as subblocks in that level)
781 must be carried forward so they will later become subblocks
782 of something else. */
784 current_binding_level->blocks
785 = chainon (current_binding_level->blocks, subblocks);
787 /* Each and every BLOCK node created here in `poplevel' is important
788 (e.g. for proper debugging information) so if we created one
789 earlier, mark it as "used". */
791 TREE_USED (block) = 1;
793 /* All temporary bindings created for cleanups are popped silently. */
794 if (kind == sk_cleanup)
797 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
800 /* Insert BLOCK at the end of the list of subblocks of the
801 current binding level. This is used when a BIND_EXPR is expanded,
802 to handle the BLOCK node inside the BIND_EXPR. */
805 insert_block (tree block)
807 TREE_USED (block) = 1;
808 current_binding_level->blocks
809 = chainon (current_binding_level->blocks, block);
812 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
813 itself, calling F for each. The DATA is passed to F as well. */
816 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
819 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
821 result |= (*f) (namespace, data);
823 for (; current; current = TREE_CHAIN (current))
824 result |= walk_namespaces_r (current, f, data);
829 /* Walk all the namespaces, calling F for each. The DATA is passed to
833 walk_namespaces (walk_namespaces_fn f, void* data)
835 return walk_namespaces_r (global_namespace, f, data);
838 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
839 DATA is non-NULL, this is the last time we will call
840 wrapup_global_declarations for this NAMESPACE. */
843 wrapup_globals_for_namespace (tree namespace, void* data)
845 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
846 VEC(tree,gc) *statics = level->static_decls;
847 tree *vec = VEC_address (tree, statics);
848 int len = VEC_length (tree, statics);
849 int last_time = (data != 0);
853 check_global_declarations (vec, len);
854 emit_debug_global_declarations (vec, len);
858 /* Write out any globals that need to be output. */
859 return wrapup_global_declarations (vec, len);
863 /* In C++, you don't have to write `struct S' to refer to `S'; you
864 can just use `S'. We accomplish this by creating a TYPE_DECL as
865 if the user had written `typedef struct S S'. Create and return
866 the TYPE_DECL for TYPE. */
869 create_implicit_typedef (tree name, tree type)
873 decl = build_decl (TYPE_DECL, name, type);
874 DECL_ARTIFICIAL (decl) = 1;
875 /* There are other implicit type declarations, like the one *within*
876 a class that allows you to write `S::S'. We must distinguish
878 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
879 TYPE_NAME (type) = decl;
884 /* Remember a local name for name-mangling purposes. */
887 push_local_name (tree decl)
892 timevar_push (TV_NAME_LOOKUP);
894 name = DECL_NAME (decl);
896 nelts = VEC_length (tree, local_names);
897 for (i = 0; i < nelts; i++)
899 t = VEC_index (tree, local_names, i);
900 if (DECL_NAME (t) == name)
902 if (!DECL_LANG_SPECIFIC (decl))
903 retrofit_lang_decl (decl);
904 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
905 if (DECL_LANG_SPECIFIC (t))
906 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
908 DECL_DISCRIMINATOR (decl) = 1;
910 VEC_replace (tree, local_names, i, decl);
911 timevar_pop (TV_NAME_LOOKUP);
916 VEC_safe_push (tree, gc, local_names, decl);
917 timevar_pop (TV_NAME_LOOKUP);
920 /* Subroutine of duplicate_decls: return truthvalue of whether
921 or not types of these decls match.
923 For C++, we must compare the parameter list so that `int' can match
924 `int&' in a parameter position, but `int&' is not confused with
928 decls_match (tree newdecl, tree olddecl)
932 if (newdecl == olddecl)
935 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
936 /* If the two DECLs are not even the same kind of thing, we're not
937 interested in their types. */
940 if (TREE_CODE (newdecl) == FUNCTION_DECL)
942 tree f1 = TREE_TYPE (newdecl);
943 tree f2 = TREE_TYPE (olddecl);
944 tree p1 = TYPE_ARG_TYPES (f1);
945 tree p2 = TYPE_ARG_TYPES (f2);
947 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
948 && ! (DECL_EXTERN_C_P (newdecl)
949 && DECL_EXTERN_C_P (olddecl)))
952 if (TREE_CODE (f1) != TREE_CODE (f2))
955 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
957 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
958 && (DECL_BUILT_IN (olddecl)
959 #ifndef NO_IMPLICIT_EXTERN_C
960 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
961 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
965 types_match = self_promoting_args_p (p1);
966 if (p1 == void_list_node)
967 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
969 #ifndef NO_IMPLICIT_EXTERN_C
970 else if (p1 == NULL_TREE
971 && (DECL_EXTERN_C_P (olddecl)
972 && DECL_IN_SYSTEM_HEADER (olddecl)
973 && !DECL_CLASS_SCOPE_P (olddecl))
974 && (DECL_EXTERN_C_P (newdecl)
975 && DECL_IN_SYSTEM_HEADER (newdecl)
976 && !DECL_CLASS_SCOPE_P (newdecl)))
978 types_match = self_promoting_args_p (p2);
979 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
983 types_match = compparms (p1, p2);
988 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
990 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
991 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
994 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
995 DECL_TEMPLATE_PARMS (olddecl)))
998 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
999 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1000 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
1002 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1003 DECL_TEMPLATE_RESULT (newdecl));
1007 /* Need to check scope for variable declaration (VAR_DECL).
1008 For typedef (TYPE_DECL), scope is ignored. */
1009 if (TREE_CODE (newdecl) == VAR_DECL
1010 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1012 Two declarations for an object with C language linkage
1013 with the same name (ignoring the namespace that qualify
1014 it) that appear in different namespace scopes refer to
1016 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1019 if (TREE_TYPE (newdecl) == error_mark_node)
1020 types_match = TREE_TYPE (olddecl) == error_mark_node;
1021 else if (TREE_TYPE (olddecl) == NULL_TREE)
1022 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1023 else if (TREE_TYPE (newdecl) == NULL_TREE)
1026 types_match = comptypes (TREE_TYPE (newdecl),
1027 TREE_TYPE (olddecl),
1028 COMPARE_REDECLARATION);
1034 /* If NEWDECL is `static' and an `extern' was seen previously,
1035 warn about it. OLDDECL is the previous declaration.
1037 Note that this does not apply to the C++ case of declaring
1038 a variable `extern const' and then later `const'.
1040 Don't complain about built-in functions, since they are beyond
1041 the user's control. */
1044 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1048 if (TREE_CODE (newdecl) == TYPE_DECL
1049 || TREE_CODE (newdecl) == TEMPLATE_DECL
1050 || TREE_CODE (newdecl) == CONST_DECL
1051 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1054 /* Don't get confused by static member functions; that's a different
1056 if (TREE_CODE (newdecl) == FUNCTION_DECL
1057 && DECL_STATIC_FUNCTION_P (newdecl))
1060 /* If the old declaration was `static', or the new one isn't, then
1061 then everything is OK. */
1062 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1065 /* It's OK to declare a builtin function as `static'. */
1066 if (TREE_CODE (olddecl) == FUNCTION_DECL
1067 && DECL_ARTIFICIAL (olddecl))
1070 name = DECL_ASSEMBLER_NAME (newdecl);
1071 pedwarn ("%qD was declared %<extern%> and later %<static%>", newdecl);
1072 pedwarn ("previous declaration of %q+D", olddecl);
1075 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1076 function templates. If their exception specifications do not
1077 match, issue an a diagnostic. */
1080 check_redeclaration_exception_specification (tree new_decl,
1085 tree new_exceptions;
1086 tree old_exceptions;
1088 new_type = TREE_TYPE (new_decl);
1089 new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1090 old_type = TREE_TYPE (old_decl);
1091 old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1095 If any declaration of a function has an exception-specification,
1096 all declarations, including the definition and an explicit
1097 specialization, of that function shall have an
1098 exception-specification with the same set of type-ids. */
1099 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1100 && ! DECL_IS_BUILTIN (old_decl)
1102 && !comp_except_specs (new_exceptions, old_exceptions,
1105 error ("declaration of %qF throws different exceptions", new_decl);
1106 error ("from previous declaration %q+F", old_decl);
1110 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1111 If the redeclaration is invalid, a diagnostic is issued, and the
1112 error_mark_node is returned. Otherwise, OLDDECL is returned.
1114 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1117 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1120 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1122 unsigned olddecl_uid = DECL_UID (olddecl);
1123 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1124 int new_defines_function = 0;
1127 if (newdecl == olddecl)
1130 types_match = decls_match (newdecl, olddecl);
1132 /* If either the type of the new decl or the type of the old decl is an
1133 error_mark_node, then that implies that we have already issued an
1134 error (earlier) for some bogus type specification, and in that case,
1135 it is rather pointless to harass the user with yet more error message
1136 about the same declaration, so just pretend the types match here. */
1137 if (TREE_TYPE (newdecl) == error_mark_node
1138 || TREE_TYPE (olddecl) == error_mark_node)
1139 return error_mark_node;
1141 if (DECL_P (olddecl)
1142 && TREE_CODE (newdecl) == FUNCTION_DECL
1143 && TREE_CODE (olddecl) == FUNCTION_DECL
1144 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1146 if (DECL_DECLARED_INLINE_P (newdecl)
1147 && DECL_UNINLINABLE (newdecl)
1148 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1149 /* Already warned elsewhere. */;
1150 else if (DECL_DECLARED_INLINE_P (olddecl)
1151 && DECL_UNINLINABLE (olddecl)
1152 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1153 /* Already warned. */;
1154 else if (DECL_DECLARED_INLINE_P (newdecl)
1155 && DECL_UNINLINABLE (olddecl)
1156 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1158 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1160 warning (OPT_Wattributes, "previous declaration of %q+D "
1161 "with attribute noinline", olddecl);
1163 else if (DECL_DECLARED_INLINE_P (olddecl)
1164 && DECL_UNINLINABLE (newdecl)
1165 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1167 warning (OPT_Wattributes, "function %q+D redeclared with "
1168 "attribute noinline", newdecl);
1169 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1174 /* Check for redeclaration and other discrepancies. */
1175 if (TREE_CODE (olddecl) == FUNCTION_DECL
1176 && DECL_ARTIFICIAL (olddecl))
1178 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1179 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1181 /* Avoid warnings redeclaring built-ins which have not been
1182 explicitly declared. */
1183 if (DECL_ANTICIPATED (olddecl))
1186 /* If you declare a built-in or predefined function name as static,
1187 the old definition is overridden, but optionally warn this was a
1188 bad choice of name. */
1189 if (! TREE_PUBLIC (newdecl))
1191 warning (OPT_Wshadow, "shadowing %s function %q#D",
1192 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1194 /* Discard the old built-in function. */
1197 /* If the built-in is not ansi, then programs can override
1198 it even globally without an error. */
1199 else if (! DECL_BUILT_IN (olddecl))
1200 warning (0, "library function %q#D redeclared as non-function %q#D",
1204 error ("declaration of %q#D", newdecl);
1205 error ("conflicts with built-in declaration %q#D",
1210 else if (!types_match)
1212 /* Avoid warnings redeclaring built-ins which have not been
1213 explicitly declared. */
1214 if (DECL_ANTICIPATED (olddecl))
1216 /* Deal with fileptr_type_node. FILE type is not known
1217 at the time we create the builtins. */
1220 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1221 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1223 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1226 else if (TREE_VALUE (t2) == fileptr_type_node)
1228 tree t = TREE_VALUE (t1);
1230 if (TREE_CODE (t) == POINTER_TYPE
1231 && TYPE_NAME (TREE_TYPE (t))
1232 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1233 == get_identifier ("FILE")
1234 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1236 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1238 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1239 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1240 types_match = decls_match (newdecl, olddecl);
1242 return duplicate_decls (newdecl, olddecl,
1244 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1247 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1250 else if ((DECL_EXTERN_C_P (newdecl)
1251 && DECL_EXTERN_C_P (olddecl))
1252 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1253 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1255 /* A near match; override the builtin. */
1257 if (TREE_PUBLIC (newdecl))
1259 warning (0, "new declaration %q#D", newdecl);
1260 warning (0, "ambiguates built-in declaration %q#D",
1264 warning (OPT_Wshadow, "shadowing %s function %q#D",
1265 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1269 /* Discard the old built-in function. */
1272 /* Replace the old RTL to avoid problems with inlining. */
1273 COPY_DECL_RTL (newdecl, olddecl);
1275 /* Even if the types match, prefer the new declarations type for
1276 built-ins which have not been explicitly declared, for
1277 exception lists, etc... */
1278 else if (DECL_ANTICIPATED (olddecl))
1280 tree type = TREE_TYPE (newdecl);
1281 tree attribs = (*targetm.merge_type_attributes)
1282 (TREE_TYPE (olddecl), type);
1284 type = cp_build_type_attribute_variant (type, attribs);
1285 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1288 /* Whether or not the builtin can throw exceptions has no
1289 bearing on this declarator. */
1290 TREE_NOTHROW (olddecl) = 0;
1292 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1294 /* If a builtin function is redeclared as `static', merge
1295 the declarations, but make the original one static. */
1296 DECL_THIS_STATIC (olddecl) = 1;
1297 TREE_PUBLIC (olddecl) = 0;
1299 /* Make the old declaration consistent with the new one so
1300 that all remnants of the builtin-ness of this function
1301 will be banished. */
1302 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1303 COPY_DECL_RTL (newdecl, olddecl);
1306 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1308 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1309 && TREE_CODE (newdecl) != TYPE_DECL
1310 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
1311 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
1312 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1313 && TREE_CODE (olddecl) != TYPE_DECL
1314 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
1315 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1318 /* We do nothing special here, because C++ does such nasty
1319 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1320 get shadowed, and know that if we need to find a TYPE_DECL
1321 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1322 slot of the identifier. */
1326 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1327 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1328 || (TREE_CODE (olddecl) == FUNCTION_DECL
1329 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1332 error ("%q#D redeclared as different kind of symbol", newdecl);
1333 if (TREE_CODE (olddecl) == TREE_LIST)
1334 olddecl = TREE_VALUE (olddecl);
1335 error ("previous declaration of %q+#D", olddecl);
1337 return error_mark_node;
1339 else if (!types_match)
1341 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1342 /* These are certainly not duplicate declarations; they're
1343 from different scopes. */
1346 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1348 /* The name of a class template may not be declared to refer to
1349 any other template, class, function, object, namespace, value,
1350 or type in the same scope. */
1351 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1352 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1354 error ("declaration of template %q#D", newdecl);
1355 error ("conflicts with previous declaration %q+#D", olddecl);
1357 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1358 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1359 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1360 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1361 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1362 DECL_TEMPLATE_PARMS (olddecl))
1363 /* Template functions can be disambiguated by
1365 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1366 TREE_TYPE (TREE_TYPE (olddecl))))
1368 error ("new declaration %q#D", newdecl);
1369 error ("ambiguates old declaration %q+#D", olddecl);
1373 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1375 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1377 error ("declaration of C function %q#D conflicts with",
1379 error ("previous declaration %q+#D here", olddecl);
1381 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1382 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1384 error ("new declaration %q#D", newdecl);
1385 error ("ambiguates old declaration %q+#D", olddecl);
1386 return error_mark_node;
1393 error ("conflicting declaration %q#D", newdecl);
1394 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
1395 return error_mark_node;
1398 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1399 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1400 && (!DECL_TEMPLATE_INFO (newdecl)
1401 || (DECL_TI_TEMPLATE (newdecl)
1402 != DECL_TI_TEMPLATE (olddecl))))
1403 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1404 && (!DECL_TEMPLATE_INFO (olddecl)
1405 || (DECL_TI_TEMPLATE (olddecl)
1406 != DECL_TI_TEMPLATE (newdecl))))))
1407 /* It's OK to have a template specialization and a non-template
1408 with the same type, or to have specializations of two
1409 different templates with the same type. Note that if one is a
1410 specialization, and the other is an instantiation of the same
1411 template, that we do not exit at this point. That situation
1412 can occur if we instantiate a template class, and then
1413 specialize one of its methods. This situation is valid, but
1414 the declarations must be merged in the usual way. */
1416 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1417 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1418 && !DECL_USE_TEMPLATE (newdecl))
1419 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1420 && !DECL_USE_TEMPLATE (olddecl))))
1421 /* One of the declarations is a template instantiation, and the
1422 other is not a template at all. That's OK. */
1424 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1426 /* In [namespace.alias] we have:
1428 In a declarative region, a namespace-alias-definition can be
1429 used to redefine a namespace-alias declared in that declarative
1430 region to refer only to the namespace to which it already
1433 Therefore, if we encounter a second alias directive for the same
1434 alias, we can just ignore the second directive. */
1435 if (DECL_NAMESPACE_ALIAS (newdecl)
1436 && (DECL_NAMESPACE_ALIAS (newdecl)
1437 == DECL_NAMESPACE_ALIAS (olddecl)))
1439 /* [namespace.alias]
1441 A namespace-name or namespace-alias shall not be declared as
1442 the name of any other entity in the same declarative region.
1443 A namespace-name defined at global scope shall not be
1444 declared as the name of any other entity in any global scope
1446 error ("declaration of namespace %qD conflicts with", newdecl);
1447 error ("previous declaration of namespace %q+D here", olddecl);
1448 return error_mark_node;
1452 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1455 error (errmsg, newdecl);
1456 if (DECL_NAME (olddecl) != NULL_TREE)
1457 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1458 ? "%q+#D previously defined here"
1459 : "%q+#D previously declared here", olddecl);
1460 return error_mark_node;
1462 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1463 && DECL_INITIAL (olddecl) != NULL_TREE
1464 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1465 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1467 /* Prototype decl follows defn w/o prototype. */
1468 warning (0, "prototype for %q+#D", newdecl);
1469 warning (0, "%Jfollows non-prototype definition here", olddecl);
1471 else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1472 || TREE_CODE (olddecl) == VAR_DECL)
1473 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1476 If two declarations of the same function or object
1477 specify different linkage-specifications ..., the program
1478 is ill-formed.... Except for functions with C++ linkage,
1479 a function declaration without a linkage specification
1480 shall not precede the first linkage specification for
1481 that function. A function can be declared without a
1482 linkage specification after an explicit linkage
1483 specification has been seen; the linkage explicitly
1484 specified in the earlier declaration is not affected by
1485 such a function declaration.
1487 DR 563 raises the question why the restrictions on
1488 functions should not also apply to objects. Older
1489 versions of G++ silently ignore the linkage-specification
1497 which is clearly wrong. Therefore, we now treat objects
1499 if (current_lang_depth () == 0)
1501 /* There is no explicit linkage-specification, so we use
1502 the linkage from the previous declaration. */
1503 if (!DECL_LANG_SPECIFIC (newdecl))
1504 retrofit_lang_decl (newdecl);
1505 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1509 error ("previous declaration of %q+#D with %qL linkage",
1510 olddecl, DECL_LANGUAGE (olddecl));
1511 error ("conflicts with new declaration with %qL linkage",
1512 DECL_LANGUAGE (newdecl));
1516 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1518 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1520 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1521 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1524 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1525 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1527 for (; t1 && t1 != void_list_node;
1528 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1529 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1531 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1534 pedwarn ("default argument given for parameter %d of %q#D",
1536 pedwarn ("after previous specification in %q+#D", olddecl);
1540 error ("default argument given for parameter %d of %q#D",
1542 error ("after previous specification in %q+#D",
1547 if (DECL_DECLARED_INLINE_P (newdecl)
1548 && ! DECL_DECLARED_INLINE_P (olddecl)
1549 && TREE_ADDRESSABLE (olddecl) && warn_inline)
1551 warning (0, "%q#D was used before it was declared inline", newdecl);
1552 warning (0, "%Jprevious non-inline declaration here", olddecl);
1557 /* Do not merge an implicit typedef with an explicit one. In:
1561 typedef class A A __attribute__ ((foo));
1563 the attribute should apply only to the typedef. */
1564 if (TREE_CODE (olddecl) == TYPE_DECL
1565 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1566 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1569 /* If new decl is `static' and an `extern' was seen previously,
1571 warn_extern_redeclared_static (newdecl, olddecl);
1573 /* We have committed to returning 1 at this point. */
1574 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1576 /* Now that functions must hold information normally held
1577 by field decls, there is extra work to do so that
1578 declaration information does not get destroyed during
1580 if (DECL_VINDEX (olddecl))
1581 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1582 if (DECL_CONTEXT (olddecl))
1583 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1584 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1585 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1586 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1587 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1588 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1589 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1590 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1591 SET_OVERLOADED_OPERATOR_CODE
1592 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1593 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1595 /* Optionally warn about more than one declaration for the same
1596 name, but don't warn about a function declaration followed by a
1598 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1599 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1600 /* Don't warn about extern decl followed by definition. */
1601 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1602 /* Don't warn about friends, let add_friend take care of it. */
1603 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)))
1605 warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1606 warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
1610 /* Deal with C++: must preserve virtual function table size. */
1611 if (TREE_CODE (olddecl) == TYPE_DECL)
1613 tree newtype = TREE_TYPE (newdecl);
1614 tree oldtype = TREE_TYPE (olddecl);
1616 if (newtype != error_mark_node && oldtype != error_mark_node
1617 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1618 CLASSTYPE_FRIEND_CLASSES (newtype)
1619 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1621 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1624 /* Copy all the DECL_... slots specified in the new decl
1625 except for any that we copy here from the old type. */
1626 DECL_ATTRIBUTES (newdecl)
1627 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1629 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1633 old_result = DECL_TEMPLATE_RESULT (olddecl);
1634 new_result = DECL_TEMPLATE_RESULT (newdecl);
1635 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1636 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1637 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1638 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1640 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1642 DECL_INLINE (old_result)
1643 |= DECL_INLINE (new_result);
1644 DECL_DECLARED_INLINE_P (old_result)
1645 |= DECL_DECLARED_INLINE_P (new_result);
1646 check_redeclaration_exception_specification (newdecl, olddecl);
1649 /* If the new declaration is a definition, update the file and
1650 line information on the declaration, and also make
1651 the old declaration the same definition. */
1652 if (DECL_INITIAL (old_result) == NULL_TREE
1653 && DECL_INITIAL (new_result) != NULL_TREE)
1655 DECL_SOURCE_LOCATION (olddecl)
1656 = DECL_SOURCE_LOCATION (old_result)
1657 = DECL_SOURCE_LOCATION (newdecl);
1658 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
1659 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1660 DECL_ARGUMENTS (old_result)
1661 = DECL_ARGUMENTS (new_result);
1669 /* Automatically handles default parameters. */
1670 tree oldtype = TREE_TYPE (olddecl);
1673 /* Merge the data types specified in the two decls. */
1674 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1676 /* If merge_types produces a non-typedef type, just use the old type. */
1677 if (TREE_CODE (newdecl) == TYPE_DECL
1678 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1681 if (TREE_CODE (newdecl) == VAR_DECL)
1683 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1684 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1685 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1686 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1687 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1688 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1690 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1691 if (DECL_LANG_SPECIFIC (olddecl)
1692 && CP_DECL_THREADPRIVATE_P (olddecl))
1694 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1695 if (!DECL_LANG_SPECIFIC (newdecl))
1696 retrofit_lang_decl (newdecl);
1698 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1699 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1703 /* Do this after calling `merge_types' so that default
1704 parameters don't confuse us. */
1705 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1706 check_redeclaration_exception_specification (newdecl, olddecl);
1707 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1709 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1710 check_default_args (newdecl);
1712 /* Lay the type out, unless already done. */
1713 if (! same_type_p (newtype, oldtype)
1714 && TREE_TYPE (newdecl) != error_mark_node
1715 && !(processing_template_decl && uses_template_parms (newdecl)))
1716 layout_type (TREE_TYPE (newdecl));
1718 if ((TREE_CODE (newdecl) == VAR_DECL
1719 || TREE_CODE (newdecl) == PARM_DECL
1720 || TREE_CODE (newdecl) == RESULT_DECL
1721 || TREE_CODE (newdecl) == FIELD_DECL
1722 || TREE_CODE (newdecl) == TYPE_DECL)
1723 && !(processing_template_decl && uses_template_parms (newdecl)))
1724 layout_decl (newdecl, 0);
1726 /* Merge the type qualifiers. */
1727 if (TREE_READONLY (newdecl))
1728 TREE_READONLY (olddecl) = 1;
1729 if (TREE_THIS_VOLATILE (newdecl))
1730 TREE_THIS_VOLATILE (olddecl) = 1;
1731 if (TREE_NOTHROW (newdecl))
1732 TREE_NOTHROW (olddecl) = 1;
1734 /* Merge deprecatedness. */
1735 if (TREE_DEPRECATED (newdecl))
1736 TREE_DEPRECATED (olddecl) = 1;
1738 /* Merge the initialization information. */
1739 if (DECL_INITIAL (newdecl) == NULL_TREE
1740 && DECL_INITIAL (olddecl) != NULL_TREE)
1742 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1743 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1744 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
1745 && DECL_LANG_SPECIFIC (newdecl)
1746 && DECL_LANG_SPECIFIC (olddecl))
1748 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1749 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1753 /* Merge the section attribute.
1754 We want to issue an error if the sections conflict but that must be
1755 done later in decl_attributes since we are called before attributes
1757 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1758 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1760 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1762 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1763 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1764 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1765 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1766 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1767 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1768 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1769 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1770 /* Keep the old RTL. */
1771 COPY_DECL_RTL (olddecl, newdecl);
1773 else if (TREE_CODE (newdecl) == VAR_DECL
1774 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1776 /* Keep the old RTL. We cannot keep the old RTL if the old
1777 declaration was for an incomplete object and the new
1778 declaration is not since many attributes of the RTL will
1780 COPY_DECL_RTL (olddecl, newdecl);
1783 /* If cannot merge, then use the new type and qualifiers,
1784 and don't preserve the old rtl. */
1787 /* Clean out any memory we had of the old declaration. */
1788 tree oldstatic = value_member (olddecl, static_aggregates);
1790 TREE_VALUE (oldstatic) = error_mark_node;
1792 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1793 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1794 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1795 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1798 /* Merge the storage class information. */
1799 merge_weak (newdecl, olddecl);
1801 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
1802 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1803 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1804 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1805 if (! DECL_EXTERNAL (olddecl))
1806 DECL_EXTERNAL (newdecl) = 0;
1808 new_template = NULL_TREE;
1809 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1811 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1812 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1813 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1814 DECL_TEMPLATE_INSTANTIATED (newdecl)
1815 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1817 /* If the OLDDECL is an instantiation and/or specialization,
1818 then the NEWDECL must be too. But, it may not yet be marked
1819 as such if the caller has created NEWDECL, but has not yet
1820 figured out that it is a redeclaration. */
1821 if (!DECL_USE_TEMPLATE (newdecl))
1822 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
1824 /* Don't really know how much of the language-specific
1825 values we should copy from old to new. */
1826 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
1827 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
1828 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
1829 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
1830 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
1831 if (DECL_TEMPLATE_INFO (newdecl))
1832 new_template = DECL_TI_TEMPLATE (newdecl);
1833 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
1834 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1835 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
1836 olddecl_friend = DECL_FRIEND_P (olddecl);
1837 hidden_friend = (DECL_ANTICIPATED (olddecl)
1838 && DECL_HIDDEN_FRIEND_P (olddecl)
1839 && newdecl_is_friend);
1841 /* Only functions have DECL_BEFRIENDING_CLASSES. */
1842 if (TREE_CODE (newdecl) == FUNCTION_DECL
1843 || DECL_FUNCTION_TEMPLATE_P (newdecl))
1845 DECL_BEFRIENDING_CLASSES (newdecl)
1846 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1847 DECL_BEFRIENDING_CLASSES (olddecl));
1848 /* DECL_THUNKS is only valid for virtual functions,
1849 otherwise it is a DECL_FRIEND_CONTEXT. */
1850 if (DECL_VIRTUAL_P (newdecl))
1851 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
1855 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1857 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1858 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
1860 /* If newdecl is not a specialization, then it is not a
1861 template-related function at all. And that means that we
1862 should have exited above, returning 0. */
1863 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
1865 if (TREE_USED (olddecl))
1866 /* From [temp.expl.spec]:
1868 If a template, a member template or the member of a class
1869 template is explicitly specialized then that
1870 specialization shall be declared before the first use of
1871 that specialization that would cause an implicit
1872 instantiation to take place, in every translation unit in
1873 which such a use occurs. */
1874 error ("explicit specialization of %qD after first use",
1877 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
1879 /* Don't propagate visibility from the template to the
1880 specialization here. We'll do that in determine_visibility if
1882 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
1884 /* [temp.expl.spec/14] We don't inline explicit specialization
1885 just because the primary template says so. */
1889 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
1890 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
1892 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
1894 /* If either decl says `inline', this fn is inline, unless
1895 its definition was passed already. */
1896 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
1897 DECL_INLINE (olddecl) = 1;
1898 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
1900 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1901 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1904 /* Preserve abstractness on cloned [cd]tors. */
1905 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
1909 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1910 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
1911 COPY_DECL_RTL (newdecl, olddecl);
1913 if (! types_match || new_defines_function)
1915 /* These need to be copied so that the names are available.
1916 Note that if the types do match, we'll preserve inline
1917 info and other bits, but if not, we won't. */
1918 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
1919 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
1921 if (new_defines_function)
1922 /* If defining a function declared with other language
1923 linkage, use the previously declared language linkage. */
1924 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1925 else if (types_match)
1927 /* If redeclaring a builtin function, and not a definition,
1928 it stays built in. */
1929 if (DECL_BUILT_IN (olddecl))
1931 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1932 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1933 /* If we're keeping the built-in definition, keep the rtl,
1934 regardless of declaration matches. */
1935 COPY_DECL_RTL (olddecl, newdecl);
1938 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1939 /* Don't clear out the arguments if we're redefining a function. */
1940 if (DECL_ARGUMENTS (olddecl))
1941 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1944 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1945 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
1947 /* Now preserve various other info from the definition. */
1948 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
1949 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
1950 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1951 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1953 /* Warn about conflicting visibility specifications. */
1954 if (DECL_VISIBILITY_SPECIFIED (olddecl)
1955 && DECL_VISIBILITY_SPECIFIED (newdecl)
1956 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1958 warning (OPT_Wattributes, "%q+D: visibility attribute ignored "
1959 "because it", newdecl);
1960 warning (OPT_Wattributes, "%Jconflicts with previous "
1961 "declaration here", olddecl);
1963 /* Choose the declaration which specified visibility. */
1964 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1966 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1967 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1969 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
1970 so keep this behavior. */
1971 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
1973 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
1974 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
1977 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
1978 with that from NEWDECL below. */
1979 if (DECL_LANG_SPECIFIC (olddecl))
1981 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
1982 != DECL_LANG_SPECIFIC (newdecl));
1983 ggc_free (DECL_LANG_SPECIFIC (olddecl));
1986 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1990 function_size = sizeof (struct tree_decl_common);
1992 memcpy ((char *) olddecl + sizeof (struct tree_common),
1993 (char *) newdecl + sizeof (struct tree_common),
1994 function_size - sizeof (struct tree_common));
1996 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1997 (char *) newdecl + sizeof (struct tree_decl_common),
1998 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2000 /* If newdecl is a template instantiation, it is possible that
2001 the following sequence of events has occurred:
2003 o A friend function was declared in a class template. The
2004 class template was instantiated.
2006 o The instantiation of the friend declaration was
2007 recorded on the instantiation list, and is newdecl.
2009 o Later, however, instantiate_class_template called pushdecl
2010 on the newdecl to perform name injection. But, pushdecl in
2011 turn called duplicate_decls when it discovered that another
2012 declaration of a global function with the same name already
2015 o Here, in duplicate_decls, we decided to clobber newdecl.
2017 If we're going to do that, we'd better make sure that
2018 olddecl, and not newdecl, is on the list of
2019 instantiations so that if we try to do the instantiation
2020 again we won't get the clobbered declaration. */
2021 reregister_specialization (newdecl,
2027 size_t size = tree_code_size (TREE_CODE (olddecl));
2028 memcpy ((char *) olddecl + sizeof (struct tree_common),
2029 (char *) newdecl + sizeof (struct tree_common),
2030 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2031 switch (TREE_CODE (olddecl))
2041 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2042 (char *) newdecl + sizeof (struct tree_decl_common),
2043 size - sizeof (struct tree_decl_common)
2044 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2048 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2049 (char *) newdecl + sizeof (struct tree_decl_common),
2050 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2051 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2055 DECL_UID (olddecl) = olddecl_uid;
2057 DECL_FRIEND_P (olddecl) = 1;
2060 DECL_ANTICIPATED (olddecl) = 1;
2061 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2064 /* NEWDECL contains the merged attribute lists.
2065 Update OLDDECL to be the same. */
2066 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2068 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2069 so that encode_section_info has a chance to look at the new decl
2070 flags and attributes. */
2071 if (DECL_RTL_SET_P (olddecl)
2072 && (TREE_CODE (olddecl) == FUNCTION_DECL
2073 || (TREE_CODE (olddecl) == VAR_DECL
2074 && TREE_STATIC (olddecl))))
2075 make_decl_rtl (olddecl);
2077 /* The NEWDECL will no longer be needed. Because every out-of-class
2078 declaration of a member results in a call to duplicate_decls,
2079 freeing these nodes represents in a significant savings. */
2085 /* Return zero if the declaration NEWDECL is valid
2086 when the declaration OLDDECL (assumed to be for the same name)
2087 has already been seen.
2088 Otherwise return an error message format string with a %s
2089 where the identifier should go. */
2092 redeclaration_error_message (tree newdecl, tree olddecl)
2094 if (TREE_CODE (newdecl) == TYPE_DECL)
2096 /* Because C++ can put things into name space for free,
2097 constructs like "typedef struct foo { ... } foo"
2098 would look like an erroneous redeclaration. */
2099 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2102 return "redefinition of %q#D";
2104 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2106 /* If this is a pure function, its olddecl will actually be
2107 the original initialization to `0' (which we force to call
2108 abort()). Don't complain about redefinition in this case. */
2109 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2110 && DECL_INITIAL (olddecl) == NULL_TREE)
2113 /* If both functions come from different namespaces, this is not
2114 a redeclaration - this is a conflict with a used function. */
2115 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2116 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2117 && ! decls_match (olddecl, newdecl))
2118 return "%qD conflicts with used function";
2120 /* We'll complain about linkage mismatches in
2121 warn_extern_redeclared_static. */
2123 /* Defining the same name twice is no good. */
2124 if (DECL_INITIAL (olddecl) != NULL_TREE
2125 && DECL_INITIAL (newdecl) != NULL_TREE)
2127 if (DECL_NAME (olddecl) == NULL_TREE)
2128 return "%q#D not declared in class";
2130 return "redefinition of %q#D";
2134 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2138 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2140 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2141 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2142 return "redefinition of %q#D";
2146 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2147 || (DECL_TEMPLATE_RESULT (newdecl)
2148 == DECL_TEMPLATE_RESULT (olddecl)))
2151 nt = DECL_TEMPLATE_RESULT (newdecl);
2152 if (DECL_TEMPLATE_INFO (nt))
2153 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2154 ot = DECL_TEMPLATE_RESULT (olddecl);
2155 if (DECL_TEMPLATE_INFO (ot))
2156 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2157 if (DECL_INITIAL (nt) && DECL_INITIAL (ot))
2158 return "redefinition of %q#D";
2162 else if (TREE_CODE (newdecl) == VAR_DECL
2163 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2164 && (! DECL_LANG_SPECIFIC (olddecl)
2165 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2166 || DECL_THREAD_LOCAL_P (newdecl)))
2168 /* Only variables can be thread-local, and all declarations must
2169 agree on this property. */
2170 if (DECL_THREAD_LOCAL_P (newdecl))
2171 return "thread-local declaration of %q#D follows "
2172 "non-thread-local declaration";
2174 return "non-thread-local declaration of %q#D follows "
2175 "thread-local declaration";
2177 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2179 /* The objects have been declared at namespace scope. If either
2180 is a member of an anonymous union, then this is an invalid
2181 redeclaration. For example:
2187 if (DECL_ANON_UNION_VAR_P (newdecl)
2188 || DECL_ANON_UNION_VAR_P (olddecl))
2189 return "redeclaration of %q#D";
2190 /* If at least one declaration is a reference, there is no
2191 conflict. For example:
2197 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2199 /* Reject two definitions. */
2200 return "redefinition of %q#D";
2204 /* Objects declared with block scope: */
2205 /* Reject two definitions, and reject a definition
2206 together with an external reference. */
2207 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2208 return "redeclaration of %q#D";
2213 /* Hash and equality functions for the named_label table. */
2216 named_label_entry_hash (const void *data)
2218 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2219 return DECL_UID (ent->label_decl);
2223 named_label_entry_eq (const void *a, const void *b)
2225 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2226 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2227 return ent_a->label_decl == ent_b->label_decl;
2230 /* Create a new label, named ID. */
2233 make_label_decl (tree id, int local_p)
2235 struct named_label_entry *ent;
2239 decl = build_decl (LABEL_DECL, id, void_type_node);
2241 DECL_CONTEXT (decl) = current_function_decl;
2242 DECL_MODE (decl) = VOIDmode;
2243 C_DECLARED_LABEL_FLAG (decl) = local_p;
2245 /* Say where one reference is to the label, for the sake of the
2246 error if it is not defined. */
2247 DECL_SOURCE_LOCATION (decl) = input_location;
2249 /* Record the fact that this identifier is bound to this label. */
2250 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2252 /* Create the label htab for the function on demand. */
2254 named_labels = htab_create_ggc (13, named_label_entry_hash,
2255 named_label_entry_eq, NULL);
2257 /* Record this label on the list of labels used in this function.
2258 We do this before calling make_label_decl so that we get the
2259 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2260 ent = GGC_CNEW (struct named_label_entry);
2261 ent->label_decl = decl;
2263 slot = htab_find_slot (named_labels, ent, INSERT);
2264 gcc_assert (*slot == NULL);
2270 /* Look for a label named ID in the current function. If one cannot
2271 be found, create one. (We keep track of used, but undefined,
2272 labels, and complain about them at the end of a function.) */
2275 lookup_label (tree id)
2279 timevar_push (TV_NAME_LOOKUP);
2280 /* You can't use labels at global scope. */
2281 if (current_function_decl == NULL_TREE)
2283 error ("label %qE referenced outside of any function", id);
2284 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2287 /* See if we've already got this label. */
2288 decl = IDENTIFIER_LABEL_VALUE (id);
2289 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2290 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2292 decl = make_label_decl (id, /*local_p=*/0);
2293 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2296 /* Declare a local label named ID. */
2299 declare_local_label (tree id)
2303 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2304 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2305 shadow = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2306 current_binding_level->shadowed_labels);
2307 current_binding_level->shadowed_labels = shadow;
2309 decl = make_label_decl (id, /*local_p=*/1);
2310 TREE_VALUE (shadow) = decl;
2315 /* Returns nonzero if it is ill-formed to jump past the declaration of
2316 DECL. Returns 2 if it's also a real problem. */
2319 decl_jump_unsafe (tree decl)
2321 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2322 || TREE_TYPE (decl) == error_mark_node)
2325 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
2326 || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2329 if (pod_type_p (TREE_TYPE (decl)))
2332 /* The POD stuff is just pedantry; why should it matter if the class
2333 contains a field of pointer to member type? */
2337 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2340 identify_goto (tree decl, const location_t *locus)
2343 pedwarn ("jump to label %qD", decl);
2345 pedwarn ("jump to case label");
2347 pedwarn ("%H from here", locus);
2350 /* Check that a single previously seen jump to a newly defined label
2351 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2352 the jump context; NAMES are the names in scope in LEVEL at the jump
2353 context; LOCUS is the source position of the jump or 0. Returns
2354 true if all is well. */
2357 check_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names,
2358 bool exited_omp, const location_t *locus)
2360 struct cp_binding_level *b;
2361 bool identified = false, saw_eh = false, saw_omp = false;
2365 identify_goto (decl, locus);
2366 error (" exits OpenMP structured block");
2367 identified = saw_omp = true;
2370 for (b = current_binding_level; b ; b = b->level_chain)
2372 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2374 for (new_decls = b->names; new_decls != old_decls;
2375 new_decls = TREE_CHAIN (new_decls))
2377 int problem = decl_jump_unsafe (new_decls);
2383 identify_goto (decl, locus);
2387 error (" crosses initialization of %q+#D", new_decls);
2389 pedwarn (" enters scope of non-POD %q+#D", new_decls);
2394 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2398 identify_goto (decl, locus);
2401 if (b->kind == sk_try)
2402 error (" enters try block");
2404 error (" enters catch block");
2407 if (b->kind == sk_omp && !saw_omp)
2411 identify_goto (decl, locus);
2414 error (" enters OpenMP structured block");
2423 check_previous_goto (tree decl, struct named_label_use_entry *use)
2425 check_previous_goto_1 (decl, use->binding_level,
2426 use->names_in_scope, use->in_omp_scope,
2427 &use->o_goto_locus);
2431 check_switch_goto (struct cp_binding_level* level)
2433 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2436 /* Check that a new jump to a label DECL is OK. Called by
2437 finish_goto_stmt. */
2440 check_goto (tree decl)
2442 struct named_label_entry *ent, dummy;
2443 bool saw_catch = false, identified = false;
2446 /* We can't know where a computed goto is jumping.
2447 So we assume that it's OK. */
2448 if (TREE_CODE (decl) != LABEL_DECL)
2451 /* We didn't record any information about this label when we created it,
2452 and there's not much point since it's trivial to analyze as a return. */
2453 if (decl == cdtor_label)
2456 dummy.label_decl = decl;
2457 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2458 gcc_assert (ent != NULL);
2460 /* If the label hasn't been defined yet, defer checking. */
2461 if (! DECL_INITIAL (decl))
2463 struct named_label_use_entry *new_use;
2465 /* Don't bother creating another use if the last goto had the
2466 same data, and will therefore create the same set of errors. */
2468 && ent->uses->names_in_scope == current_binding_level->names)
2471 new_use = GGC_NEW (struct named_label_use_entry);
2472 new_use->binding_level = current_binding_level;
2473 new_use->names_in_scope = current_binding_level->names;
2474 new_use->o_goto_locus = input_location;
2475 new_use->in_omp_scope = false;
2477 new_use->next = ent->uses;
2478 ent->uses = new_use;
2482 if (ent->in_try_scope || ent->in_catch_scope
2483 || ent->in_omp_scope || ent->bad_decls)
2485 pedwarn ("jump to label %q+D", decl);
2486 pedwarn (" from here");
2490 for (bad = ent->bad_decls; bad; bad = TREE_CHAIN (bad))
2492 tree b = TREE_VALUE (bad);
2493 int u = decl_jump_unsafe (b);
2495 if (u > 1 && DECL_ARTIFICIAL (b))
2497 /* Can't skip init of __exception_info. */
2498 error ("%J enters catch block", b);
2502 error (" skips initialization of %q+#D", b);
2504 pedwarn (" enters scope of non-POD %q+#D", b);
2507 if (ent->in_try_scope)
2508 error (" enters try block");
2509 else if (ent->in_catch_scope && !saw_catch)
2510 error (" enters catch block");
2512 if (ent->in_omp_scope)
2513 error (" enters OpenMP structured block");
2514 else if (flag_openmp)
2516 struct cp_binding_level *b;
2517 for (b = current_binding_level; b ; b = b->level_chain)
2519 if (b == ent->binding_level)
2521 if (b->kind == sk_omp)
2525 pedwarn ("jump to label %q+D", decl);
2526 pedwarn (" from here");
2529 error (" exits OpenMP structured block");
2536 /* Check that a return is ok wrt OpenMP structured blocks.
2537 Called by finish_return_stmt. Returns true if all is well. */
2540 check_omp_return (void)
2542 struct cp_binding_level *b;
2543 for (b = current_binding_level; b ; b = b->level_chain)
2544 if (b->kind == sk_omp)
2546 error ("invalid exit from OpenMP structured block");
2552 /* Define a label, specifying the location in the source file.
2553 Return the LABEL_DECL node for the label. */
2556 define_label (location_t location, tree name)
2558 struct named_label_entry *ent, dummy;
2559 struct cp_binding_level *p;
2562 timevar_push (TV_NAME_LOOKUP);
2564 decl = lookup_label (name);
2566 dummy.label_decl = decl;
2567 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2568 gcc_assert (ent != NULL);
2570 /* After labels, make any new cleanups in the function go into their
2571 own new (temporary) binding contour. */
2572 for (p = current_binding_level;
2573 p->kind != sk_function_parms;
2575 p->more_cleanups_ok = 0;
2577 if (name == get_identifier ("wchar_t"))
2578 pedwarn ("label named wchar_t");
2580 if (DECL_INITIAL (decl) != NULL_TREE)
2582 error ("duplicate label %qD", decl);
2583 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2587 struct named_label_use_entry *use;
2589 /* Mark label as having been defined. */
2590 DECL_INITIAL (decl) = error_mark_node;
2591 /* Say where in the source. */
2592 DECL_SOURCE_LOCATION (decl) = location;
2594 ent->binding_level = current_binding_level;
2595 ent->names_in_scope = current_binding_level->names;
2597 for (use = ent->uses; use ; use = use->next)
2598 check_previous_goto (decl, use);
2602 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2607 struct cp_binding_level *level;
2608 struct cp_switch *next;
2609 /* The SWITCH_STMT being built. */
2611 /* A splay-tree mapping the low element of a case range to the high
2612 element, or NULL_TREE if there is no high element. Used to
2613 determine whether or not a new case label duplicates an old case
2614 label. We need a tree, rather than simply a hash table, because
2615 of the GNU case range extension. */
2619 /* A stack of the currently active switch statements. The innermost
2620 switch statement is on the top of the stack. There is no need to
2621 mark the stack for garbage collection because it is only active
2622 during the processing of the body of a function, and we never
2623 collect at that point. */
2625 static struct cp_switch *switch_stack;
2627 /* Called right after a switch-statement condition is parsed.
2628 SWITCH_STMT is the switch statement being parsed. */
2631 push_switch (tree switch_stmt)
2633 struct cp_switch *p = XNEW (struct cp_switch);
2634 p->level = current_binding_level;
2635 p->next = switch_stack;
2636 p->switch_stmt = switch_stmt;
2637 p->cases = splay_tree_new (case_compare, NULL, NULL);
2644 struct cp_switch *cs = switch_stack;
2645 location_t switch_location;
2647 /* Emit warnings as needed. */
2648 if (EXPR_HAS_LOCATION (cs->switch_stmt))
2649 switch_location = EXPR_LOCATION (cs->switch_stmt);
2651 switch_location = input_location;
2652 if (!processing_template_decl)
2653 c_do_switch_warnings (cs->cases, switch_location,
2654 SWITCH_STMT_TYPE (cs->switch_stmt),
2655 SWITCH_STMT_COND (cs->switch_stmt));
2657 splay_tree_delete (cs->cases);
2658 switch_stack = switch_stack->next;
2662 /* Note that we've seen a definition of a case label, and complain if this
2663 is a bad place for one. */
2666 finish_case_label (tree low_value, tree high_value)
2669 struct cp_binding_level *p;
2671 if (processing_template_decl)
2675 /* For templates, just add the case label; we'll do semantic
2676 analysis at instantiation-time. */
2677 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2678 return add_stmt (build_case_label (low_value, high_value, label));
2681 /* Find the condition on which this switch statement depends. */
2682 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
2683 if (cond && TREE_CODE (cond) == TREE_LIST)
2684 cond = TREE_VALUE (cond);
2686 if (!check_switch_goto (switch_stack->level))
2687 return error_mark_node;
2689 r = c_add_case_label (switch_stack->cases, cond, TREE_TYPE (cond),
2690 low_value, high_value);
2692 /* After labels, make any new cleanups in the function go into their
2693 own new (temporary) binding contour. */
2694 for (p = current_binding_level;
2695 p->kind != sk_function_parms;
2697 p->more_cleanups_ok = 0;
2702 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2705 typename_hash (const void* k)
2710 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2711 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
2716 typedef struct typename_info {
2724 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
2727 typename_compare (const void * k1, const void * k2)
2730 const typename_info *t2;
2733 t2 = (const typename_info *) k2;
2735 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2736 && TYPE_CONTEXT (t1) == t2->scope
2737 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
2738 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
2739 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
2742 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
2743 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
2745 Returns the new TYPENAME_TYPE. */
2747 static GTY ((param_is (union tree_node))) htab_t typename_htab;
2750 build_typename_type (tree context, tree name, tree fullname,
2751 enum tag_types tag_type)
2759 if (typename_htab == NULL)
2760 typename_htab = htab_create_ggc (61, &typename_hash,
2761 &typename_compare, NULL);
2763 ti.scope = FROB_CONTEXT (context);
2765 ti.template_id = fullname;
2766 ti.enum_p = tag_type == enum_type;
2767 ti.class_p = (tag_type == class_type
2768 || tag_type == record_type
2769 || tag_type == union_type);
2770 hash = (htab_hash_pointer (ti.scope)
2771 ^ htab_hash_pointer (ti.name));
2773 /* See if we already have this type. */
2774 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
2779 /* Build the TYPENAME_TYPE. */
2780 t = make_aggr_type (TYPENAME_TYPE);
2781 TYPE_CONTEXT (t) = ti.scope;
2782 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
2783 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
2784 TYPENAME_IS_CLASS_P (t) = ti.class_p;
2786 /* Build the corresponding TYPE_DECL. */
2787 d = build_decl (TYPE_DECL, name, t);
2788 TYPE_NAME (TREE_TYPE (d)) = d;
2789 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2790 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2791 DECL_ARTIFICIAL (d) = 1;
2793 /* Store it in the hash table. */
2800 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
2801 provided to name the type. Returns an appropriate type, unless an
2802 error occurs, in which case error_mark_node is returned. If we
2803 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
2804 return that, rather than the _TYPE it corresponds to, in other
2805 cases we look through the type decl. If TF_ERROR is set, complain
2806 about errors, otherwise be quiet. */
2809 make_typename_type (tree context, tree name, enum tag_types tag_type,
2810 tsubst_flags_t complain)
2816 if (name == error_mark_node
2817 || context == NULL_TREE
2818 || context == error_mark_node)
2819 return error_mark_node;
2823 if (!(TYPE_LANG_SPECIFIC (name)
2824 && (CLASSTYPE_IS_TEMPLATE (name)
2825 || CLASSTYPE_USE_TEMPLATE (name))))
2826 name = TYPE_IDENTIFIER (name);
2828 /* Create a TEMPLATE_ID_EXPR for the type. */
2829 name = build_nt (TEMPLATE_ID_EXPR,
2830 CLASSTYPE_TI_TEMPLATE (name),
2831 CLASSTYPE_TI_ARGS (name));
2833 else if (TREE_CODE (name) == TYPE_DECL)
2834 name = DECL_NAME (name);
2838 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2840 name = TREE_OPERAND (name, 0);
2841 if (TREE_CODE (name) == TEMPLATE_DECL)
2842 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
2843 else if (TREE_CODE (name) == OVERLOAD)
2845 error ("%qD is not a type", name);
2846 return error_mark_node;
2849 if (TREE_CODE (name) == TEMPLATE_DECL)
2851 error ("%qD used without template parameters", name);
2852 return error_mark_node;
2854 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2855 gcc_assert (TYPE_P (context));
2857 /* When the CONTEXT is a dependent type, NAME could refer to a
2858 dependent base class of CONTEXT. So we cannot peek inside it,
2859 even if CONTEXT is a currently open scope. */
2860 if (dependent_type_p (context))
2861 return build_typename_type (context, name, fullname, tag_type);
2863 if (!IS_AGGR_TYPE (context))
2865 if (complain & tf_error)
2866 error ("%q#T is not a class", context);
2867 return error_mark_node;
2870 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
2872 /* We should only set WANT_TYPE when we're a nested typename type.
2873 Then we can give better diagnostics if we find a non-type. */
2874 t = lookup_field (context, name, 0, /*want_type=*/true);
2877 if (complain & tf_error)
2878 error (want_template ? "no class template named %q#T in %q#T"
2879 : "no type named %q#T in %q#T", name, context);
2880 return error_mark_node;
2883 if (want_template && !DECL_CLASS_TEMPLATE_P (t))
2885 if (complain & tf_error)
2886 error ("%<typename %T::%D%> names %q#T, which is not a class template",
2888 return error_mark_node;
2890 if (!want_template && TREE_CODE (t) != TYPE_DECL)
2892 if (complain & tf_error)
2893 error ("%<typename %T::%D%> names %q#T, which is not a type",
2895 return error_mark_node;
2898 if (complain & tf_error)
2899 perform_or_defer_access_check (TYPE_BINFO (context), t, t);
2902 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
2904 /*entering_scope=*/0,
2905 tf_warning_or_error | tf_user);
2907 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
2913 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
2914 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
2915 in which case error_mark_node is returned.
2917 If PARM_LIST is non-NULL, also make sure that the template parameter
2918 list of TEMPLATE_DECL matches.
2920 If COMPLAIN zero, don't complain about any errors that occur. */
2923 make_unbound_class_template (tree context, tree name, tree parm_list,
2924 tsubst_flags_t complain)
2930 name = TYPE_IDENTIFIER (name);
2931 else if (DECL_P (name))
2932 name = DECL_NAME (name);
2933 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2935 if (!dependent_type_p (context)
2936 || currently_open_class (context))
2938 tree tmpl = NULL_TREE;
2940 if (IS_AGGR_TYPE (context))
2941 tmpl = lookup_field (context, name, 0, false);
2943 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
2945 if (complain & tf_error)
2946 error ("no class template named %q#T in %q#T", name, context);
2947 return error_mark_node;
2951 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
2953 if (complain & tf_error)
2955 error ("template parameters do not match template");
2956 error ("%q+D declared here", tmpl);
2958 return error_mark_node;
2961 if (complain & tf_error)
2962 perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl);
2967 /* Build the UNBOUND_CLASS_TEMPLATE. */
2968 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
2969 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
2970 TREE_TYPE (t) = NULL_TREE;
2972 /* Build the corresponding TEMPLATE_DECL. */
2973 d = build_decl (TEMPLATE_DECL, name, t);
2974 TYPE_NAME (TREE_TYPE (d)) = d;
2975 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2976 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2977 DECL_ARTIFICIAL (d) = 1;
2978 DECL_TEMPLATE_PARMS (d) = parm_list;
2985 /* Push the declarations of builtin types into the namespace.
2986 RID_INDEX is the index of the builtin type in the array
2987 RID_POINTERS. NAME is the name used when looking up the builtin
2988 type. TYPE is the _TYPE node for the builtin type. */
2991 record_builtin_type (enum rid rid_index,
2995 tree rname = NULL_TREE, tname = NULL_TREE;
2996 tree tdecl = NULL_TREE;
2998 if ((int) rid_index < (int) RID_MAX)
2999 rname = ridpointers[(int) rid_index];
3001 tname = get_identifier (name);
3003 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3004 eliminated. Built-in types should not be looked up name; their
3005 names are keywords that the parser can recognize. However, there
3006 is code in c-common.c that uses identifier_global_value to look
3007 up built-in types by name. */
3010 tdecl = build_decl (TYPE_DECL, tname, type);
3011 DECL_ARTIFICIAL (tdecl) = 1;
3012 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3018 tdecl = build_decl (TYPE_DECL, rname, type);
3019 DECL_ARTIFICIAL (tdecl) = 1;
3021 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3024 if (!TYPE_NAME (type))
3025 TYPE_NAME (type) = tdecl;
3028 debug_hooks->type_decl (tdecl, 0);
3031 /* Record one of the standard Java types.
3032 * Declare it as having the given NAME.
3033 * If SIZE > 0, it is the size of one of the integral types;
3034 * otherwise it is the negative of the size of one of the other types. */
3037 record_builtin_java_type (const char* name, int size)
3041 type = make_signed_type (size);
3042 else if (size > -32)
3043 { /* "__java_char" or ""__java_boolean". */
3044 type = make_unsigned_type (-size);
3045 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3048 { /* "__java_float" or ""__java_double". */
3049 type = make_node (REAL_TYPE);
3050 TYPE_PRECISION (type) = - size;
3053 record_builtin_type (RID_MAX, name, type);
3054 decl = TYPE_NAME (type);
3056 /* Suppress generate debug symbol entries for these types,
3057 since for normal C++ they are just clutter.
3058 However, push_lang_context undoes this if extern "Java" is seen. */
3059 DECL_IGNORED_P (decl) = 1;
3061 TYPE_FOR_JAVA (type) = 1;
3065 /* Push a type into the namespace so that the back-ends ignore it. */
3068 record_unknown_type (tree type, const char* name)
3070 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
3071 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3072 DECL_IGNORED_P (decl) = 1;
3073 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3074 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3075 TYPE_ALIGN (type) = 1;
3076 TYPE_USER_ALIGN (type) = 0;
3077 TYPE_MODE (type) = TYPE_MODE (void_type_node);
3080 /* A string for which we should create an IDENTIFIER_NODE at
3083 typedef struct predefined_identifier
3085 /* The name of the identifier. */
3086 const char *const name;
3087 /* The place where the IDENTIFIER_NODE should be stored. */
3089 /* Nonzero if this is the name of a constructor or destructor. */
3090 const int ctor_or_dtor_p;
3091 } predefined_identifier;
3093 /* Create all the predefined identifiers. */
3096 initialize_predefined_identifiers (void)
3098 const predefined_identifier *pid;
3100 /* A table of identifiers to create at startup. */
3101 static const predefined_identifier predefined_identifiers[] = {
3102 { "C++", &lang_name_cplusplus, 0 },
3103 { "C", &lang_name_c, 0 },
3104 { "Java", &lang_name_java, 0 },
3105 /* Some of these names have a trailing space so that it is
3106 impossible for them to conflict with names written by users. */
3107 { "__ct ", &ctor_identifier, 1 },
3108 { "__base_ctor ", &base_ctor_identifier, 1 },
3109 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3110 { "__dt ", &dtor_identifier, 1 },
3111 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3112 { "__base_dtor ", &base_dtor_identifier, 1 },
3113 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3114 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3115 { "nelts", &nelts_identifier, 0 },
3116 { THIS_NAME, &this_identifier, 0 },
3117 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3118 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3119 { "_vptr", &vptr_identifier, 0 },
3120 { "__vtt_parm", &vtt_parm_identifier, 0 },
3121 { "::", &global_scope_name, 0 },
3122 { "std", &std_identifier, 0 },
3126 for (pid = predefined_identifiers; pid->name; ++pid)
3128 *pid->node = get_identifier (pid->name);
3129 if (pid->ctor_or_dtor_p)
3130 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3134 /* Create the predefined scalar types of C,
3135 and some nodes representing standard constants (0, 1, (void *)0).
3136 Initialize the global binding level.
3137 Make definitions for built-in primitive functions. */
3140 cxx_init_decl_processing (void)
3143 tree void_ftype_ptr;
3145 build_common_tree_nodes (flag_signed_char, false);
3147 /* Create all the identifiers we need. */
3148 initialize_predefined_identifiers ();
3150 /* Create the global variables. */
3151 push_to_top_level ();
3153 current_function_decl = NULL_TREE;
3154 current_binding_level = NULL;
3155 /* Enter the global namespace. */
3156 gcc_assert (global_namespace == NULL_TREE);
3157 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3159 TREE_PUBLIC (global_namespace) = 1;
3160 begin_scope (sk_namespace, global_namespace);
3162 current_lang_name = NULL_TREE;
3164 /* Adjust various flags based on command-line settings. */
3165 if (!flag_permissive)
3166 flag_pedantic_errors = 1;
3167 if (!flag_no_inline)
3169 flag_inline_trees = 1;
3172 if (flag_inline_functions)
3173 flag_inline_trees = 2;
3175 if (flag_visibility_ms_compat)
3176 default_visibility = VISIBILITY_HIDDEN;
3179 current_lang_name = lang_name_c;
3181 /* Create the `std' namespace. */
3182 push_namespace (std_identifier);
3183 std_node = current_namespace;
3186 c_common_nodes_and_builtins ();
3188 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3189 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3190 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3191 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3192 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3193 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3194 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3195 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3197 integer_two_node = build_int_cst (NULL_TREE, 2);
3198 integer_three_node = build_int_cst (NULL_TREE, 3);
3200 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3201 truthvalue_type_node = boolean_type_node;
3202 truthvalue_false_node = boolean_false_node;
3203 truthvalue_true_node = boolean_true_node;
3205 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3208 record_builtin_type (RID_MAX, NULL, string_type_node);
3211 delta_type_node = ptrdiff_type_node;
3212 vtable_index_type = ptrdiff_type_node;
3214 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3215 void_ftype = build_function_type (void_type_node, void_list_node);
3216 void_ftype_ptr = build_function_type (void_type_node,
3217 tree_cons (NULL_TREE,
3221 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3223 /* C++ extensions */
3225 unknown_type_node = make_node (UNKNOWN_TYPE);
3226 record_unknown_type (unknown_type_node, "unknown type");
3228 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3229 TREE_TYPE (unknown_type_node) = unknown_type_node;
3231 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3233 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3234 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3237 /* Make sure we get a unique function type, so we can give
3238 its pointer type a name. (This wins for gdb.) */
3239 tree vfunc_type = make_node (FUNCTION_TYPE);
3240 TREE_TYPE (vfunc_type) = integer_type_node;
3241 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3242 layout_type (vfunc_type);
3244 vtable_entry_type = build_pointer_type (vfunc_type);
3246 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3249 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3250 layout_type (vtbl_type_node);
3251 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3252 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3253 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3254 layout_type (vtbl_ptr_type_node);
3255 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3257 push_namespace (get_identifier ("__cxxabiv1"));
3258 abi_node = current_namespace;
3261 global_type_node = make_node (LANG_TYPE);
3262 record_unknown_type (global_type_node, "global type");
3265 current_lang_name = lang_name_cplusplus;
3269 tree bad_alloc_type_node;
3270 tree bad_alloc_decl;
3271 tree newtype, deltype;
3272 tree ptr_ftype_sizetype;
3274 push_namespace (std_identifier);
3275 bad_alloc_id = get_identifier ("bad_alloc");
3276 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
3277 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3279 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3280 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3281 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
3285 = build_function_type (ptr_type_node,
3286 tree_cons (NULL_TREE,
3289 newtype = build_exception_variant
3290 (ptr_ftype_sizetype, add_exception_specifier
3291 (NULL_TREE, bad_alloc_type_node, -1));
3292 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3293 push_cp_library_fn (NEW_EXPR, newtype);
3294 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3295 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3296 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3300 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3302 /* Perform other language dependent initializations. */
3303 init_class_processing ();
3304 init_rtti_processing ();
3306 if (flag_exceptions)
3307 init_exception_processing ();
3309 if (! supports_one_only ())
3312 make_fname_decl = cp_make_fname_decl;
3313 start_fname_decls ();
3315 /* Show we use EH for cleanups. */
3316 if (flag_exceptions)
3317 using_eh_for_cleanups ();
3320 /* Generate an initializer for a function naming variable from
3321 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3322 filled in with the type of the init. */
3325 cp_fname_init (const char* name, tree *type_p)
3327 tree domain = NULL_TREE;
3329 tree init = NULL_TREE;
3334 length = strlen (name);
3335 domain = build_index_type (size_int (length));
3336 init = build_string (length + 1, name);
3339 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3340 type = build_cplus_array_type (type, domain);
3345 TREE_TYPE (init) = type;
3347 init = error_mark_node;
3352 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
3353 decl, NAME is the initialization string and TYPE_DEP indicates whether
3354 NAME depended on the type of the function. We make use of that to detect
3355 __PRETTY_FUNCTION__ inside a template fn. This is being done
3356 lazily at the point of first use, so we mustn't push the decl now. */
3359 cp_make_fname_decl (tree id, int type_dep)
3361 const char *const name = (type_dep && processing_template_decl
3362 ? NULL : fname_as_string (type_dep));
3364 tree init = cp_fname_init (name, &type);
3365 tree decl = build_decl (VAR_DECL, id, type);
3368 free ((char *) name);
3370 /* As we're using pushdecl_with_scope, we must set the context. */
3371 DECL_CONTEXT (decl) = current_function_decl;
3372 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3374 TREE_STATIC (decl) = 1;
3375 TREE_READONLY (decl) = 1;
3376 DECL_ARTIFICIAL (decl) = 1;
3378 TREE_USED (decl) = 1;
3380 if (current_function_decl)
3382 struct cp_binding_level *b = current_binding_level;
3383 while (b->level_chain->kind != sk_function_parms)
3385 pushdecl_with_scope (decl, b, /*is_friend=*/false);
3386 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
3387 LOOKUP_ONLYCONVERTING);
3390 pushdecl_top_level_and_finish (decl, init);
3395 /* Make a definition for a builtin function named NAME in the current
3396 namespace, whose data type is TYPE and whose context is CONTEXT.
3397 TYPE should be a function type with argument types.
3399 CLASS and CODE tell later passes how to compile calls to this function.
3400 See tree.h for possible values.
3402 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3403 the name to be called if we can't opencode the function.
3404 If ATTRS is nonzero, use that for the function's attribute
3408 builtin_function_1 (const char* name,
3411 enum built_in_function code,
3412 enum built_in_class class,
3413 const char* libname,
3416 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
3417 DECL_BUILT_IN_CLASS (decl) = class;
3418 DECL_FUNCTION_CODE (decl) = code;
3419 DECL_CONTEXT (decl) = context;
3423 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
3424 we cannot change DECL_ASSEMBLER_NAME until we have installed this
3425 function in the namespace. */
3427 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
3429 /* A function in the user's namespace should have an explicit
3430 declaration before it is used. Mark the built-in function as
3431 anticipated but not actually declared. */
3432 if (name[0] != '_' || name[1] != '_')
3433 DECL_ANTICIPATED (decl) = 1;
3435 /* Possibly apply some default attributes to this built-in function. */
3437 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
3439 decl_attributes (&decl, NULL_TREE, 0);
3444 /* Entry point for the benefit of c_common_nodes_and_builtins.
3446 Make a definition for a builtin function named NAME and whose data type
3447 is TYPE. TYPE should be a function type with argument types. This
3448 function places the anticipated declaration in the global namespace
3449 and additionally in the std namespace if appropriate.
3451 CLASS and CODE tell later passes how to compile calls to this function.
3452 See tree.h for possible values.
3454 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3455 the name to be called if we can't opencode the function.
3457 If ATTRS is nonzero, use that for the function's attribute
3461 builtin_function (const char* name,
3464 enum built_in_class cl,
3465 const char* libname,
3468 /* All builtins that don't begin with an '_' should additionally
3469 go in the 'std' namespace. */
3472 push_namespace (std_identifier);
3473 builtin_function_1 (name, type, std_node, code, cl, libname, attrs);
3477 return builtin_function_1 (name, type, NULL_TREE, code,
3478 cl, libname, attrs);
3481 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
3482 function. Not called directly. */
3485 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3487 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3488 DECL_EXTERNAL (fn) = 1;
3489 TREE_PUBLIC (fn) = 1;
3490 DECL_ARTIFICIAL (fn) = 1;
3491 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
3492 SET_DECL_LANGUAGE (fn, lang_c);
3493 /* Runtime library routines are, by definition, available in an
3494 external shared object. */
3495 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3496 DECL_VISIBILITY_SPECIFIED (fn) = 1;
3500 /* Returns the _DECL for a library function with C linkage.
3501 We assume that such functions never throw; if this is incorrect,
3502 callers should unset TREE_NOTHROW. */
3505 build_library_fn (tree name, tree type)
3507 tree fn = build_library_fn_1 (name, ERROR_MARK, type);
3508 TREE_NOTHROW (fn) = 1;
3512 /* Returns the _DECL for a library function with C++ linkage. */
3515 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
3517 tree fn = build_library_fn_1 (name, operator_code, type);
3518 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
3519 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
3520 SET_DECL_LANGUAGE (fn, lang_cplusplus);
3524 /* Like build_library_fn, but takes a C string instead of an
3528 build_library_fn_ptr (const char* name, tree type)
3530 return build_library_fn (get_identifier (name), type);
3533 /* Like build_cp_library_fn, but takes a C string instead of an
3537 build_cp_library_fn_ptr (const char* name, tree type)
3539 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
3542 /* Like build_library_fn, but also pushes the function so that we will
3543 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
3546 push_library_fn (tree name, tree type)
3548 tree fn = build_library_fn (name, type);
3549 pushdecl_top_level (fn);
3553 /* Like build_cp_library_fn, but also pushes the function so that it
3554 will be found by normal lookup. */
3557 push_cp_library_fn (enum tree_code operator_code, tree type)
3559 tree fn = build_cp_library_fn (ansi_opname (operator_code),
3566 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3570 push_void_library_fn (tree name, tree parmtypes)
3572 tree type = build_function_type (void_type_node, parmtypes);
3573 return push_library_fn (name, type);
3576 /* Like push_library_fn, but also note that this function throws
3577 and does not return. Used for __throw_foo and the like. */
3580 push_throw_library_fn (tree name, tree type)
3582 tree fn = push_library_fn (name, type);
3583 TREE_THIS_VOLATILE (fn) = 1;
3584 TREE_NOTHROW (fn) = 0;
3588 /* When we call finish_struct for an anonymous union, we create
3589 default copy constructors and such. But, an anonymous union
3590 shouldn't have such things; this function undoes the damage to the
3591 anonymous union type T.
3593 (The reason that we create the synthesized methods is that we don't
3594 distinguish `union { int i; }' from `typedef union { int i; } U'.
3595 The first is an anonymous union; the second is just an ordinary
3599 fixup_anonymous_aggr (tree t)
3603 /* Wipe out memory of synthesized methods. */
3604 TYPE_HAS_CONSTRUCTOR (t) = 0;
3605 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3606 TYPE_HAS_INIT_REF (t) = 0;
3607 TYPE_HAS_CONST_INIT_REF (t) = 0;
3608 TYPE_HAS_ASSIGN_REF (t) = 0;
3609 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
3611 /* Splice the implicitly generated functions out of the TYPE_METHODS
3613 q = &TYPE_METHODS (t);
3616 if (DECL_ARTIFICIAL (*q))
3617 *q = TREE_CHAIN (*q);
3619 q = &TREE_CHAIN (*q);
3622 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
3623 if (TYPE_METHODS (t))
3624 error ("%Jan anonymous union cannot have function members",
3625 TYPE_MAIN_DECL (t));
3627 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3628 assignment operators (because they cannot have these methods themselves).
3629 For anonymous unions this is already checked because they are not allowed
3630 in any union, otherwise we have to check it. */
3631 if (TREE_CODE (t) != UNION_TYPE)
3635 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3636 if (TREE_CODE (field) == FIELD_DECL)
3638 type = TREE_TYPE (field);
3639 if (CLASS_TYPE_P (type))
3641 if (TYPE_NEEDS_CONSTRUCTING (type))
3642 error ("member %q+#D with constructor not allowed "
3643 "in anonymous aggregate", field);
3644 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3645 error ("member %q+#D with destructor not allowed "
3646 "in anonymous aggregate", field);
3647 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3648 error ("member %q+#D with copy assignment operator "
3649 "not allowed in anonymous aggregate", field);
3655 /* Make sure that a declaration with no declarator is well-formed, i.e.
3656 just declares a tagged type or anonymous union.
3658 Returns the type declared; or NULL_TREE if none. */
3661 check_tag_decl (cp_decl_specifier_seq *declspecs)
3663 int saw_friend = declspecs->specs[(int)ds_friend] != 0;
3664 int saw_typedef = declspecs->specs[(int)ds_typedef] != 0;
3665 /* If a class, struct, or enum type is declared by the DECLSPECS
3666 (i.e, if a class-specifier, enum-specifier, or non-typename
3667 elaborated-type-specifier appears in the DECLSPECS),
3668 DECLARED_TYPE is set to the corresponding type. */
3669 tree declared_type = NULL_TREE;
3670 bool error_p = false;
3672 if (declspecs->multiple_types_p)
3673 error ("multiple types in one declaration");
3674 else if (declspecs->redefined_builtin_type)
3676 if (!in_system_header)
3677 pedwarn ("redeclaration of C++ built-in type %qT",
3678 declspecs->redefined_builtin_type);
3683 && TYPE_P (declspecs->type)
3684 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
3685 && IS_AGGR_TYPE (declspecs->type))
3686 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
3687 declared_type = declspecs->type;
3688 else if (declspecs->type == error_mark_node)
3690 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
3691 pedwarn ("declaration does not declare anything");
3692 /* Check for an anonymous union. */
3693 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
3694 && TYPE_ANONYMOUS_P (declared_type))
3696 /* 7/3 In a simple-declaration, the optional init-declarator-list
3697 can be omitted only when declaring a class (clause 9) or
3698 enumeration (7.2), that is, when the decl-specifier-seq contains
3699 either a class-specifier, an elaborated-type-specifier with
3700 a class-key (9.1), or an enum-specifier. In these cases and
3701 whenever a class-specifier or enum-specifier is present in the
3702 decl-specifier-seq, the identifiers in these specifiers are among
3703 the names being declared by the declaration (as class-name,
3704 enum-names, or enumerators, depending on the syntax). In such
3705 cases, and except for the declaration of an unnamed bit-field (9.6),
3706 the decl-specifier-seq shall introduce one or more names into the
3707 program, or shall redeclare a name introduced by a previous
3708 declaration. [Example:
3709 enum { }; // ill-formed
3710 typedef class { }; // ill-formed
3714 error ("missing type-name in typedef-declaration");
3717 /* Anonymous unions are objects, so they can have specifiers. */;
3718 SET_ANON_AGGR_TYPE_P (declared_type);
3720 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
3721 && !in_system_header)
3722 pedwarn ("ISO C++ prohibits anonymous structs");
3727 if (declspecs->specs[(int)ds_inline]
3728 || declspecs->specs[(int)ds_virtual])
3729 error ("%qs can only be specified for functions",
3730 declspecs->specs[(int)ds_inline]
3731 ? "inline" : "virtual");
3733 && (!current_class_type
3734 || current_scope () != current_class_type))
3735 error ("%<friend%> can only be specified inside a class");
3736 else if (declspecs->specs[(int)ds_explicit])
3737 error ("%<explicit%> can only be specified for constructors");
3738 else if (declspecs->storage_class)
3739 error ("a storage class can only be specified for objects "
3741 else if (declspecs->specs[(int)ds_const]
3742 || declspecs->specs[(int)ds_volatile]
3743 || declspecs->specs[(int)ds_restrict]
3744 || declspecs->specs[(int)ds_thread])
3745 error ("qualifiers can only be specified for objects "
3749 return declared_type;
3752 /* Called when a declaration is seen that contains no names to declare.
3753 If its type is a reference to a structure, union or enum inherited
3754 from a containing scope, shadow that tag name for the current scope
3755 with a forward reference.
3756 If its type defines a new named structure or union
3757 or defines an enum, it is valid but we need not do anything here.
3758 Otherwise, it is an error.
3760 C++: may have to grok the declspecs to learn about static,
3761 complain for anonymous unions.
3763 Returns the TYPE declared -- or NULL_TREE if none. */
3766 shadow_tag (cp_decl_specifier_seq *declspecs)
3768 tree t = check_tag_decl (declspecs);
3773 if (declspecs->attributes)
3775 warning (0, "attribute ignored in declaration of %q+#T", t);
3776 warning (0, "attribute for %q+#T must follow the %qs keyword",
3777 t, class_key_or_enum_as_string (t));
3781 if (maybe_process_partial_specialization (t) == error_mark_node)
3784 /* This is where the variables in an anonymous union are
3785 declared. An anonymous union declaration looks like:
3787 because there is no declarator after the union, the parser
3788 sends that declaration here. */
3789 if (ANON_AGGR_TYPE_P (t))
3791 fixup_anonymous_aggr (t);
3793 if (TYPE_FIELDS (t))
3795 tree decl = grokdeclarator (/*declarator=*/NULL,
3796 declspecs, NORMAL, 0, NULL);
3797 finish_anon_union (decl);
3804 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3807 groktypename (cp_decl_specifier_seq *type_specifiers,
3808 const cp_declarator *declarator)
3812 attrs = type_specifiers->attributes;
3813 type_specifiers->attributes = NULL_TREE;
3814 type = grokdeclarator (declarator, type_specifiers, TYPENAME, 0, &attrs);
3816 cplus_decl_attributes (&type, attrs, 0);
3820 /* Decode a declarator in an ordinary declaration or data definition.
3821 This is called as soon as the type information and variable name
3822 have been parsed, before parsing the initializer if any.
3823 Here we create the ..._DECL node, fill in its type,
3824 and put it on the list of decls for the current context.
3825 The ..._DECL node is returned as the value.
3827 Exception: for arrays where the length is not specified,
3828 the type is left null, to be filled in by `cp_finish_decl'.
3830 Function definitions do not come here; they go to start_function
3831 instead. However, external and forward declarations of functions
3832 do go through here. Structure field declarations are done by
3833 grokfield and not through here. */
3836 start_decl (const cp_declarator *declarator,
3837 cp_decl_specifier_seq *declspecs,
3840 tree prefix_attributes,
3841 tree *pushed_scope_p)
3846 /* APPLE LOCAL "unavailable" attribute (radar 2809697) */
3850 *pushed_scope_p = NULL_TREE;
3852 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
3853 /* An object declared as __attribute__((unavailable)) suppresses
3854 any reports of being declared with unavailable or deprecated
3855 items. An object declared as __attribute__((deprecated))
3856 suppresses warnings of uses of other deprecated items. */
3857 #ifdef A_LESS_INEFFICENT_WAY /* which I really don't want to do! */
3858 if (lookup_attribute ("deprecated", attributes))
3859 deprecated_state = DEPRECATED_SUPPRESS;
3860 else if (lookup_attribute ("unavailable", attributes))
3861 deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS;
3862 #else /* a more efficient way doing what lookup_attribute would do */
3863 for (a = attributes; a; a = TREE_CHAIN (a))
3865 tree name = TREE_PURPOSE (a);
3866 if (TREE_CODE (name) == IDENTIFIER_NODE)
3867 if (is_attribute_p ("deprecated", name))
3869 deprecated_state = DEPRECATED_SUPPRESS;
3872 if (is_attribute_p ("unavailable", name))
3874 deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS;
3879 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
3881 attributes = chainon (attributes, prefix_attributes);
3883 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
3886 deprecated_state = DEPRECATED_NORMAL;
3888 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE
3889 || decl == error_mark_node)
3890 return error_mark_node;
3892 type = TREE_TYPE (decl);
3894 context = DECL_CONTEXT (decl);
3898 *pushed_scope_p = push_scope (context);
3900 /* We are only interested in class contexts, later. */
3901 if (TREE_CODE (context) == NAMESPACE_DECL)
3902 context = NULL_TREE;
3906 /* Is it valid for this decl to have an initializer at all?
3907 If not, set INITIALIZED to zero, which will indirectly
3908 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
3909 switch (TREE_CODE (decl))
3912 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3913 return error_mark_node;
3916 error ("function %q#D is initialized like a variable", decl);
3917 return error_mark_node;
3925 if (! toplevel_bindings_p ()
3926 && DECL_EXTERNAL (decl))
3927 warning (0, "declaration of %q#D has %<extern%> and is initialized",
3929 DECL_EXTERNAL (decl) = 0;
3930 if (toplevel_bindings_p ())
3931 TREE_STATIC (decl) = 1;
3934 /* Set attributes here so if duplicate decl, will have proper attributes. */
3935 cplus_decl_attributes (&decl, attributes, 0);
3937 /* Dllimported symbols cannot be defined. Static data members (which
3938 can be initialized in-class and dllimported) go through grokfield,
3939 not here, so we don't need to exclude those decls when checking for
3941 if (initialized && DECL_DLLIMPORT_P (decl))
3943 error ("definition of %q#D is marked %<dllimport%>", decl);
3944 DECL_DLLIMPORT_P (decl) = 0;
3947 /* If #pragma weak was used, mark the decl weak now. */
3948 maybe_apply_pragma_weak (decl);
3950 if (TREE_CODE (decl) == FUNCTION_DECL
3951 && DECL_DECLARED_INLINE_P (decl)
3952 && DECL_UNINLINABLE (decl)
3953 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3954 warning (0, "inline function %q+D given attribute noinline", decl);
3956 if (context && COMPLETE_TYPE_P (complete_type (context)))
3958 if (TREE_CODE (decl) == VAR_DECL)
3960 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
3961 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
3962 error ("%q#D is not a static member of %q#T", decl, context);
3965 if (DECL_CONTEXT (field) != context)
3967 if (!same_type_p (DECL_CONTEXT (field), context))
3968 pedwarn ("ISO C++ does not permit %<%T::%D%> "
3969 "to be defined as %<%T::%D%>",
3970 DECL_CONTEXT (field), DECL_NAME (decl),
3971 context, DECL_NAME (decl));
3972 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
3974 if (processing_specialization
3975 && template_class_depth (context) == 0
3976 && CLASSTYPE_TEMPLATE_SPECIALIZATION (context))
3977 error ("template header not allowed in member definition "
3978 "of explicitly specialized class");
3979 /* Static data member are tricky; an in-class initialization
3980 still doesn't provide a definition, so the in-class
3981 declaration will have DECL_EXTERNAL set, but will have an
3982 initialization. Thus, duplicate_decls won't warn
3983 about this situation, and so we check here. */
3984 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
3985 error ("duplicate initialization of %qD", decl);
3986 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
3992 tree field = check_classfn (context, decl,
3993 (processing_template_decl
3994 > template_class_depth (context))
3995 ? current_template_parms
3997 if (field && duplicate_decls (decl, field,
3998 /*newdecl_is_friend=*/false))
4002 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
4003 DECL_IN_AGGR_P (decl) = 0;
4004 /* Do not mark DECL as an explicit specialization if it was not
4005 already marked as an instantiation; a declaration should
4006 never be marked as a specialization unless we know what
4007 template is being specialized. */
4008 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4010 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
4012 /* [temp.expl.spec] An explicit specialization of a static data
4013 member of a template is a definition if the declaration
4014 includes an initializer; otherwise, it is a declaration.
4016 We check for processing_specialization so this only applies
4017 to the new specialization syntax. */
4018 if (!initialized && processing_specialization)
4019 DECL_EXTERNAL (decl) = 1;
4022 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
4023 pedwarn ("declaration of %q#D outside of class is not definition",
4027 was_public = TREE_PUBLIC (decl);
4029 /* Enter this declaration into the symbol table. */
4030 tem = maybe_push_decl (decl);
4032 if (processing_template_decl)
4033 tem = push_template_decl (tem);
4034 if (tem == error_mark_node)
4035 return error_mark_node;
4037 /* Tell the back-end to use or not use .common as appropriate. If we say
4038 -fconserve-space, we want this to save .data space, at the expense of
4039 wrong semantics. If we say -fno-conserve-space, we want this to
4040 produce errors about redefs; to do this we force variables into the
4042 if (flag_conserve_space
4043 && TREE_CODE (tem) == VAR_DECL
4044 && TREE_PUBLIC (tem)
4045 && !DECL_THREAD_LOCAL_P (tem)
4046 && !have_global_bss_p ())
4047 DECL_COMMON (tem) = 1;
4049 if (TREE_CODE (tem) == VAR_DECL
4050 && DECL_NAMESPACE_SCOPE_P (tem) && !TREE_PUBLIC (tem) && !was_public
4051 && !DECL_THIS_STATIC (tem) && !DECL_ARTIFICIAL (tem))
4053 /* This is a const variable with implicit 'static'. Set
4054 DECL_THIS_STATIC so we can tell it from variables that are
4055 !TREE_PUBLIC because of the anonymous namespace. */
4056 gcc_assert (cp_type_readonly (TREE_TYPE (tem)));
4057 DECL_THIS_STATIC (tem) = 1;
4060 if (!processing_template_decl && TREE_CODE (tem) == VAR_DECL)
4061 start_decl_1 (tem, initialized);
4067 start_decl_1 (tree decl, bool initialized)
4071 gcc_assert (!processing_template_decl);
4073 if (error_operand_p (decl))
4076 gcc_assert (TREE_CODE (decl) == VAR_DECL);
4077 type = TREE_TYPE (decl);
4080 /* Is it valid for this decl to have an initializer at all?
4081 If not, set INITIALIZED to zero, which will indirectly
4082 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4084 /* Don't allow initializations for incomplete types except for
4085 arrays which might be completed by the initialization. */
4086 if (COMPLETE_TYPE_P (complete_type (type)))
4087 ; /* A complete type is ok. */
4088 else if (TREE_CODE (type) != ARRAY_TYPE)
4090 error ("variable %q#D has initializer but incomplete type", decl);
4092 type = TREE_TYPE (decl) = error_mark_node;
4094 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
4096 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
4097 error ("elements of array %q#D have incomplete type", decl);
4098 /* else we already gave an error in start_decl. */
4102 else if (IS_AGGR_TYPE (type)
4103 && ! DECL_EXTERNAL (decl))
4105 if (!COMPLETE_TYPE_P (complete_type (type)))
4107 error ("aggregate %q#D has incomplete type and cannot be defined",
4109 /* Change the type so that assemble_variable will give
4110 DECL an rtl we can live with: (mem (const_int 0)). */
4111 type = TREE_TYPE (decl) = error_mark_node;
4115 /* If any base type in the hierarchy of TYPE needs a constructor,
4116 then we set initialized to 1. This way any nodes which are
4117 created for the purposes of initializing this aggregate
4118 will live as long as it does. This is necessary for global
4119 aggregates which do not have their initializers processed until
4120 the end of the file. */
4121 initialized = TYPE_NEEDS_CONSTRUCTING (type);
4125 /* Create a new scope to hold this declaration if necessary.
4126 Whether or not a new scope is necessary cannot be determined
4127 until after the type has been completed; if the type is a
4128 specialization of a class template it is not until after
4129 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4130 will be set correctly. */
4131 maybe_push_cleanup_level (type);
4134 /* Handle initialization of references. DECL, TYPE, and INIT have the
4135 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
4136 but will be set to a new CLEANUP_STMT if a temporary is created
4137 that must be destroyed subsequently.
4139 Returns an initializer expression to use to initialize DECL, or
4140 NULL if the initialization can be performed statically.
4142 Quotes on semantics can be found in ARM 8.4.3. */
4145 grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
4149 if (init == NULL_TREE)
4151 if ((DECL_LANG_SPECIFIC (decl) == 0
4152 || DECL_IN_AGGR_P (decl) == 0)
4153 && ! DECL_THIS_EXTERN (decl))
4154 error ("%qD declared as reference but not initialized", decl);
4158 if (TREE_CODE (init) == CONSTRUCTOR)
4160 error ("ISO C++ forbids use of initializer list to "
4161 "initialize reference %qD", decl);
4165 if (TREE_CODE (init) == TREE_LIST)
4166 init = build_x_compound_expr_from_list (init, "initializer");
4168 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
4169 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
4170 /* Note: default conversion is only called in very special cases. */
4171 init = decay_conversion (init);
4173 /* Convert INIT to the reference type TYPE. This may involve the
4174 creation of a temporary, whose lifetime must be the same as that
4175 of the reference. If so, a DECL_EXPR for the temporary will be
4176 added just after the DECL_EXPR for DECL. That's why we don't set
4177 DECL_INITIAL for local references (instead assigning to them
4178 explicitly); we need to allow the temporary to be initialized
4180 tmp = initialize_reference (type, init, decl, cleanup);
4182 if (tmp == error_mark_node)
4184 else if (tmp == NULL_TREE)
4186 error ("cannot initialize %qT from %qT", type, TREE_TYPE (init));
4190 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
4193 DECL_INITIAL (decl) = tmp;
4198 /* Designated initializers in arrays are not supported in GNU C++.
4199 The parser cannot detect this error since it does not know whether
4200 a given brace-enclosed initializer is for a class type or for an
4201 array. This function checks that CE does not use a designated
4202 initializer. If it does, an error is issued. Returns true if CE
4203 is valid, i.e., does not have a designated initializer. */
4206 check_array_designated_initializer (const constructor_elt *ce)
4208 /* Designated initializers for array elements arenot supported. */
4211 /* The parser only allows identifiers as designated
4213 gcc_assert (TREE_CODE (ce->index) == IDENTIFIER_NODE);
4214 error ("name %qD used in a GNU-style designated "
4215 "initializer for an array", ce->index);
4222 /* When parsing `int a[] = {1, 2};' we don't know the size of the
4223 array until we finish parsing the initializer. If that's the
4224 situation we're in, update DECL accordingly. */
4227 maybe_deduce_size_from_array_init (tree decl, tree init)
4229 tree type = TREE_TYPE (decl);
4231 if (TREE_CODE (type) == ARRAY_TYPE
4232 && TYPE_DOMAIN (type) == NULL_TREE
4233 && TREE_CODE (decl) != TYPE_DECL)
4235 /* do_default is really a C-ism to deal with tentative definitions.
4236 But let's leave it here to ease the eventual merge. */
4237 int do_default = !DECL_EXTERNAL (decl);
4238 tree initializer = init ? init : DECL_INITIAL (decl);
4241 /* Check that there are no designated initializers in INIT, as
4242 those are not supported in GNU C++, and as the middle-end
4243 will crash if presented with a non-numeric designated
4245 if (initializer && TREE_CODE (initializer) == CONSTRUCTOR)
4247 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initializer);
4248 constructor_elt *ce;
4251 VEC_iterate (constructor_elt, v, i, ce);
4253 if (!check_array_designated_initializer (ce))
4259 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
4263 error ("initializer fails to determine size of %qD", decl);
4264 TREE_TYPE (decl) = error_mark_node;
4266 else if (failure == 2)
4270 error ("array size missing in %qD", decl);
4271 TREE_TYPE (decl) = error_mark_node;
4273 /* If a `static' var's size isn't known, make it extern as
4274 well as static, so it does not get allocated. If it's not
4275 `static', then don't mark it extern; finish_incomplete_decl
4276 will give it a default size and it will get allocated. */
4277 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4278 DECL_EXTERNAL (decl) = 1;
4280 else if (failure == 3)
4282 error ("zero-size array %qD", decl);
4283 TREE_TYPE (decl) = error_mark_node;
4287 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
4289 layout_decl (decl, 0);
4293 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
4294 any appropriate error messages regarding the layout. */
4297 layout_var_decl (tree decl)
4301 type = TREE_TYPE (decl);
4302 if (type == error_mark_node)
4305 /* If we haven't already layed out this declaration, do so now.
4306 Note that we must not call complete type for an external object
4307 because it's type might involve templates that we are not
4308 supposed to instantiate yet. (And it's perfectly valid to say
4309 `extern X x' for some incomplete type `X'.) */
4310 if (!DECL_EXTERNAL (decl))
4311 complete_type (type);
4312 if (!DECL_SIZE (decl)
4313 && TREE_TYPE (decl) != error_mark_node
4314 && (COMPLETE_TYPE_P (type)
4315 || (TREE_CODE (type) == ARRAY_TYPE
4316 && !TYPE_DOMAIN (type)
4317 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
4318 layout_decl (decl, 0);
4320 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
4322 /* An automatic variable with an incomplete type: that is an error.
4323 Don't talk about array types here, since we took care of that
4324 message in grokdeclarator. */
4325 error ("storage size of %qD isn't known", decl);
4326 TREE_TYPE (decl) = error_mark_node;
4329 /* Keep this code around in case we later want to control debug info
4330 based on whether a type is "used". (jason 1999-11-11) */
4332 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
4333 /* Let debugger know it should output info for this type. */
4334 note_debug_info_needed (ttype);
4336 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
4337 note_debug_info_needed (DECL_CONTEXT (decl));
4340 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4341 && DECL_SIZE (decl) != NULL_TREE
4342 && ! TREE_CONSTANT (DECL_SIZE (decl)))
4344 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4345 constant_expression_warning (DECL_SIZE (decl));
4347 error ("storage size of %qD isn't constant", decl);
4351 /* If a local static variable is declared in an inline function, or if
4352 we have a weak definition, we must endeavor to create only one
4353 instance of the variable at link-time. */
4356 maybe_commonize_var (tree decl)
4358 /* Static data in a function with comdat linkage also has comdat
4360 if (TREE_STATIC (decl)
4361 /* Don't mess with __FUNCTION__. */
4362 && ! DECL_ARTIFICIAL (decl)
4363 && DECL_FUNCTION_SCOPE_P (decl)
4364 /* Unfortunately, import_export_decl has not always been called
4365 before the function is processed, so we cannot simply check
4367 && (DECL_COMDAT (DECL_CONTEXT (decl))
4368 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
4369 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
4370 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
4374 /* With weak symbols, we simply make the variable COMDAT;
4375 that will cause copies in multiple translations units to
4377 comdat_linkage (decl);
4381 if (DECL_INITIAL (decl) == NULL_TREE
4382 || DECL_INITIAL (decl) == error_mark_node)
4384 /* Without weak symbols, we can use COMMON to merge
4385 uninitialized variables. */
4386 TREE_PUBLIC (decl) = 1;
4387 DECL_COMMON (decl) = 1;
4391 /* While for initialized variables, we must use internal
4392 linkage -- which means that multiple copies will not
4394 TREE_PUBLIC (decl) = 0;
4395 DECL_COMMON (decl) = 0;
4396 warning (0, "sorry: semantics of inline function static "
4397 "data %q+#D are wrong (you'll wind up "
4398 "with multiple copies)", decl);
4399 warning (0, "%J you can work around this by removing "
4405 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
4406 /* Set it up again; we might have set DECL_INITIAL since the last
4408 comdat_linkage (decl);
4411 /* Issue an error message if DECL is an uninitialized const variable. */
4414 check_for_uninitialized_const_var (tree decl)
4416 tree type = TREE_TYPE (decl);
4418 /* ``Unless explicitly declared extern, a const object does not have
4419 external linkage and must be initialized. ($8.4; $12.1)'' ARM
4421 if (TREE_CODE (decl) == VAR_DECL
4422 && TREE_CODE (type) != REFERENCE_TYPE
4423 && CP_TYPE_CONST_P (type)
4424 && !TYPE_NEEDS_CONSTRUCTING (type)
4425 && !DECL_INITIAL (decl))
4426 error ("uninitialized const %qD", decl);
4430 /* Structure holding the current initializer being processed by reshape_init.
4431 CUR is a pointer to the current element being processed, END is a pointer
4432 after the last element present in the initializer. */
4433 typedef struct reshape_iterator_t
4435 constructor_elt *cur;
4436 constructor_elt *end;
4439 static tree reshape_init_r (tree, reshape_iter *, bool);
4441 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
4442 returned is the next FIELD_DECL (possibly FIELD itself) that can be
4443 initialized. If there are no more such fields, the return value
4447 next_initializable_field (tree field)
4450 && (TREE_CODE (field) != FIELD_DECL
4451 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
4452 || DECL_ARTIFICIAL (field)))
4453 field = TREE_CHAIN (field);
4458 /* Subroutine of reshape_init_array and reshape_init_vector, which does
4459 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
4460 INTEGER_CST representing the size of the array minus one (the maximum index),
4461 or NULL_TREE if the array was declared without specifying the size. D is
4462 the iterator within the constructor. */
4465 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d)
4468 bool sized_array_p = (max_index != NULL_TREE);
4469 unsigned HOST_WIDE_INT max_index_cst = 0;
4470 unsigned HOST_WIDE_INT index;
4472 /* The initializer for an array is always a CONSTRUCTOR. */
4473 new_init = build_constructor (NULL_TREE, NULL);
4477 /* Minus 1 is used for zero sized arrays. */
4478 if (integer_all_onesp (max_index))
4481 if (host_integerp (max_index, 1))
4482 max_index_cst = tree_low_cst (max_index, 1);
4483 /* sizetype is sign extended, not zero extended. */
4485 max_index_cst = tree_low_cst (fold_convert (size_type_node, max_index),
4489 /* Loop until there are no more initializers. */
4491 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
4496 check_array_designated_initializer (d->cur);
4497 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false);
4498 if (elt_init == error_mark_node)
4499 return error_mark_node;
4500 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), NULL_TREE, elt_init);
4506 /* Subroutine of reshape_init_r, processes the initializers for arrays.
4507 Parameters are the same of reshape_init_r. */
4510 reshape_init_array (tree type, reshape_iter *d)
4512 tree max_index = NULL_TREE;
4514 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
4516 if (TYPE_DOMAIN (type))
4517 max_index = array_type_nelts (type);
4519 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4522 /* Subroutine of reshape_init_r, processes the initializers for vectors.
4523 Parameters are the same of reshape_init_r. */
4526 reshape_init_vector (tree type, reshape_iter *d)
4528 tree max_index = NULL_TREE;
4531 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
4533 if (COMPOUND_LITERAL_P (d->cur->value))
4535 tree value = d->cur->value;
4536 if (!same_type_p (TREE_TYPE (value), type))
4538 error ("invalid type %qT as initializer for a vector of type %qT",
4539 TREE_TYPE (d->cur->value), type);
4540 value = error_mark_node;
4546 /* For a vector, the representation type is a struct
4547 containing a single member which is an array of the
4548 appropriate size. */
4549 rtype = TYPE_DEBUG_REPRESENTATION_TYPE (type);
4550 if (rtype && TYPE_DOMAIN (TREE_TYPE (TYPE_FIELDS (rtype))))
4551 max_index = array_type_nelts (TREE_TYPE (TYPE_FIELDS (rtype)));
4553 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4556 /* Subroutine of reshape_init_r, processes the initializers for classes
4557 or union. Parameters are the same of reshape_init_r. */
4560 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p)
4565 gcc_assert (CLASS_TYPE_P (type));
4567 /* The initializer for a class is always a CONSTRUCTOR. */
4568 new_init = build_constructor (NULL_TREE, NULL);
4569 field = next_initializable_field (TYPE_FIELDS (type));
4575 An initializer for an aggregate member that is an
4576 empty class shall have the form of an empty
4577 initializer-list {}. */
4578 if (!first_initializer_p)
4580 error ("initializer for %qT must be brace-enclosed", type);
4581 return error_mark_node;
4586 /* Loop through the initializable fields, gathering initializers. */
4587 while (d->cur != d->end)
4591 /* Handle designated initializers, as an extension. */
4594 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
4596 if (!field || TREE_CODE (field) != FIELD_DECL)
4598 error ("%qT has no non-static data member named %qD", type,
4600 return error_mark_node;
4604 /* If we processed all the member of the class, we are done. */
4608 field_init = reshape_init_r (TREE_TYPE (field), d,
4609 /*first_initializer_p=*/false);
4610 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
4614 When a union is initialized with a brace-enclosed
4615 initializer, the braces shall only contain an
4616 initializer for the first member of the union. */
4617 if (TREE_CODE (type) == UNION_TYPE)
4620 field = next_initializable_field (TREE_CHAIN (field));
4626 /* Subroutine of reshape_init, which processes a single initializer (part of
4627 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
4628 iterator within the CONSTRUCTOR which points to the initializer to process.
4629 FIRST_INITIALIZER_P is true if this is the first initializer of the
4630 CONSTRUCTOR node. */
4633 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p)
4635 tree init = d->cur->value;
4637 /* A non-aggregate type is always initialized with a single
4639 if (!CP_AGGREGATE_TYPE_P (type))
4641 /* It is invalid to initialize a non-aggregate type with a
4642 brace-enclosed initializer.
4643 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
4644 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
4645 a CONSTRUCTOR (with a record type). */
4646 if (TREE_CODE (init) == CONSTRUCTOR
4647 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
4649 error ("braces around scalar initializer for type %qT", type);
4650 init = error_mark_node;
4659 All implicit type conversions (clause _conv_) are considered when
4660 initializing the aggregate member with an initializer from an
4661 initializer-list. If the initializer can initialize a member,
4662 the member is initialized. Otherwise, if the member is itself a
4663 non-empty subaggregate, brace elision is assumed and the
4664 initializer is considered for the initialization of the first
4665 member of the subaggregate. */
4666 if (TREE_CODE (init) != CONSTRUCTOR
4667 && can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL))
4673 /* [dcl.init.string]
4675 A char array (whether plain char, signed char, or unsigned char)
4676 can be initialized by a string-literal (optionally enclosed in
4677 braces); a wchar_t array can be initialized by a wide
4678 string-literal (optionally enclosed in braces). */
4679 if (TREE_CODE (type) == ARRAY_TYPE
4680 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
4682 tree str_init = init;
4684 /* Strip one level of braces if and only if they enclose a single
4685 element (as allowed by [dcl.init.string]). */
4686 if (!first_initializer_p
4687 && TREE_CODE (str_init) == CONSTRUCTOR
4688 && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (str_init)) == 1)
4690 str_init = VEC_index (constructor_elt,
4691 CONSTRUCTOR_ELTS (str_init), 0)->value;
4694 /* If it's a string literal, then it's the initializer for the array
4695 as a whole. Otherwise, continue with normal initialization for
4696 array types (one value per array element). */
4697 if (TREE_CODE (str_init) == STRING_CST)
4704 /* The following cases are about aggregates. If we are not within a full
4705 initializer already, and there is not a CONSTRUCTOR, it means that there
4706 is a missing set of braces (that is, we are processing the case for
4707 which reshape_init exists). */
4708 if (!first_initializer_p)
4710 if (TREE_CODE (init) == CONSTRUCTOR)
4712 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
4713 /* There is no need to reshape pointer-to-member function
4714 initializers, as they are always constructed correctly
4715 by the front end. */
4717 else if (COMPOUND_LITERAL_P (init))
4718 /* For a nested compound literal, there is no need to reshape since
4719 brace elision is not allowed. Even if we decided to allow it,
4720 we should add a call to reshape_init in finish_compound_literal,
4721 before calling digest_init, so changing this code would still
4722 not be necessary. */
4723 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
4727 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
4728 return reshape_init (type, init);
4732 warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
4736 /* Dispatch to specialized routines. */
4737 if (CLASS_TYPE_P (type))
4738 return reshape_init_class (type, d, first_initializer_p);
4739 else if (TREE_CODE (type) == ARRAY_TYPE)
4740 return reshape_init_array (type, d);
4741 else if (TREE_CODE (type) == VECTOR_TYPE)
4742 return reshape_init_vector (type, d);
4747 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
4748 brace-enclosed aggregate initializer.
4750 INIT is the CONSTRUCTOR containing the list of initializers describing
4751 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
4752 It may not presently match the shape of the TYPE; for example:
4754 struct S { int a; int b; };
4755 struct S a[] = { 1, 2, 3, 4 };
4757 Here INIT will hold a VEC of four elements, rather than a
4758 VEC of two elements, each itself a VEC of two elements. This
4759 routine transforms INIT from the former form into the latter. The
4760 revised CONSTRUCTOR node is returned. */
4763 reshape_init (tree type, tree init)
4765 VEC(constructor_elt, gc) *v;
4769 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
4771 v = CONSTRUCTOR_ELTS (init);
4773 /* An empty constructor does not need reshaping, and it is always a valid
4775 if (VEC_empty (constructor_elt, v))
4778 /* Recurse on this CONSTRUCTOR. */
4779 d.cur = VEC_index (constructor_elt, v, 0);
4780 d.end = d.cur + VEC_length (constructor_elt, v);
4782 new_init = reshape_init_r (type, &d, true);
4783 if (new_init == error_mark_node)
4784 return error_mark_node;
4786 /* Make sure all the element of the constructor were used. Otherwise,
4787 issue an error about exceeding initializers. */
4789 error ("too many initializers for %qT", type);
4794 /* Verify INIT (the initializer for DECL), and record the
4795 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
4796 grok_reference_init.
4798 If the return value is non-NULL, it is an expression that must be
4799 evaluated dynamically to initialize DECL. */
4802 check_initializer (tree decl, tree init, int flags, tree *cleanup)
4804 tree type = TREE_TYPE (decl);
4805 tree init_code = NULL;
4807 /* Things that are going to be initialized need to have complete
4809 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
4811 if (type == error_mark_node)
4812 /* We will have already complained. */
4815 if (TREE_CODE (type) == ARRAY_TYPE)
4817 tree element_type = TREE_TYPE (type);
4819 /* The array type itself need not be complete, because the
4820 initializer may tell us how many elements are in the array.
4821 But, the elements of the array must be complete. */
4822 if (!COMPLETE_TYPE_P (complete_type (element_type)))
4824 error ("elements of array %q#D have incomplete type", decl);
4827 /* It is not valid to initialize an a VLA. */
4829 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
4830 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
4832 error ("variable-sized object %qD may not be initialized", decl);
4836 else if (!COMPLETE_TYPE_P (type))
4838 error ("%qD has incomplete type", decl);
4839 TREE_TYPE (decl) = error_mark_node;
4843 /* There is no way to make a variable-sized class type in GNU C++. */
4844 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
4846 if (!CP_AGGREGATE_TYPE_P (type)
4847 && init && BRACE_ENCLOSED_INITIALIZER_P (init)
4848 && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)) != 1)
4850 error ("scalar object %qD requires one element in initializer", decl);
4851 TREE_TYPE (decl) = error_mark_node;
4855 if (TREE_CODE (decl) == CONST_DECL)
4857 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
4859 DECL_INITIAL (decl) = init;
4861 gcc_assert (init != NULL_TREE);
4864 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
4865 init = grok_reference_init (decl, type, init, cleanup);
4868 /* Do not reshape constructors of vectors (they don't need to be
4870 if (TREE_CODE (init) == CONSTRUCTOR
4871 && !COMPOUND_LITERAL_P (init)
4872 && !TREE_TYPE (init)) /* ptrmemfunc */
4874 init = reshape_init (type, init);
4876 if ((*targetm.vector_opaque_p) (type))
4878 error ("opaque vector types cannot be initialized");
4879 init = error_mark_node;
4883 /* If DECL has an array type without a specific bound, deduce the
4884 array size from the initializer. */
4885 maybe_deduce_size_from_array_init (decl, init);
4886 type = TREE_TYPE (decl);
4887 if (type == error_mark_node)
4890 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
4892 if (TREE_CODE (type) == ARRAY_TYPE)
4893 goto initialize_aggr;
4894 else if (TREE_CODE (init) == CONSTRUCTOR)
4896 if (TYPE_NON_AGGREGATE_CLASS (type))
4898 error ("%qD must be initialized by constructor, "
4901 init = error_mark_node;
4904 goto dont_use_constructor;
4908 int saved_stmts_are_full_exprs_p;
4911 saved_stmts_are_full_exprs_p = 0;
4912 if (building_stmt_tree ())
4914 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
4915 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4917 init = build_aggr_init (decl, init, flags);
4918 if (building_stmt_tree ())
4919 current_stmt_tree ()->stmts_are_full_exprs_p =
4920 saved_stmts_are_full_exprs_p;
4926 dont_use_constructor:
4927 if (TREE_CODE (init) != TREE_VEC)
4929 init_code = store_init_value (decl, init);
4930 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
4931 && DECL_INITIAL (decl)
4932 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
4933 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
4934 warning (0, "array %qD initialized by parenthesized string literal %qE",
4935 decl, DECL_INITIAL (decl));
4940 else if (DECL_EXTERNAL (decl))
4942 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
4943 goto initialize_aggr;
4944 else if (IS_AGGR_TYPE (type))
4946 tree core_type = strip_array_types (type);
4948 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
4949 error ("structure %qD with uninitialized const members", decl);
4950 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
4951 error ("structure %qD with uninitialized reference members", decl);
4953 check_for_uninitialized_const_var (decl);
4956 check_for_uninitialized_const_var (decl);
4958 if (init && init != error_mark_node)
4959 init_code = build2 (INIT_EXPR, type, decl, init);
4964 /* If DECL is not a local variable, give it RTL. */
4967 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
4969 int toplev = toplevel_bindings_p ();
4971 const char *filename;
4973 /* Set the DECL_ASSEMBLER_NAME for the object. */
4976 /* The `register' keyword, when used together with an
4977 asm-specification, indicates that the variable should be
4978 placed in a particular register. */
4979 if (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
4981 set_user_assembler_name (decl, asmspec);
4982 DECL_HARD_REGISTER (decl) = 1;
4986 if (TREE_CODE (decl) == FUNCTION_DECL
4987 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4988 set_builtin_user_assembler_name (decl, asmspec);
4989 set_user_assembler_name (decl, asmspec);
4993 /* Handle non-variables up front. */
4994 if (TREE_CODE (decl) != VAR_DECL)
4996 rest_of_decl_compilation (decl, toplev, at_eof);
5000 /* If we see a class member here, it should be a static data
5002 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
5004 gcc_assert (TREE_STATIC (decl));
5005 /* An in-class declaration of a static data member should be
5006 external; it is only a declaration, and not a definition. */
5007 if (init == NULL_TREE)
5008 gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
5011 /* We don't create any RTL for local variables. */
5012 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
5015 /* We defer emission of local statics until the corresponding
5016 DECL_EXPR is expanded. */
5017 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
5019 /* We try to defer namespace-scope static constants so that they are
5020 not emitted into the object file unnecessarily. */
5021 filename = input_filename;
5022 if (!DECL_VIRTUAL_P (decl)
5023 && TREE_READONLY (decl)
5024 && DECL_INITIAL (decl) != NULL_TREE
5025 && DECL_INITIAL (decl) != error_mark_node
5027 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
5029 && !TREE_PUBLIC (decl))
5031 /* Fool with the linkage of static consts according to #pragma
5033 struct c_fileinfo *finfo = get_fileinfo (filename);
5034 if (!finfo->interface_unknown && !TREE_PUBLIC (decl))
5036 TREE_PUBLIC (decl) = 1;
5037 DECL_EXTERNAL (decl) = finfo->interface_only;
5042 /* Likewise for template instantiations. */
5043 else if (DECL_LANG_SPECIFIC (decl)
5044 && DECL_IMPLICIT_INSTANTIATION (decl))
5047 /* If we're not deferring, go ahead and assemble the variable. */
5049 rest_of_decl_compilation (decl, toplev, at_eof);
5052 /* Generate code to initialize DECL (a local variable). */
5055 initialize_local_var (tree decl, tree init)
5057 tree type = TREE_TYPE (decl);
5060 gcc_assert (TREE_CODE (decl) == VAR_DECL
5061 || TREE_CODE (decl) == RESULT_DECL);
5062 gcc_assert (!TREE_STATIC (decl));
5064 if (DECL_SIZE (decl) == NULL_TREE)
5066 /* If we used it already as memory, it must stay in memory. */
5067 DECL_INITIAL (decl) = NULL_TREE;
5068 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5071 if (DECL_SIZE (decl) && type != error_mark_node)
5075 /* Compute and store the initial value. */
5076 already_used = TREE_USED (decl) || TREE_USED (type);
5078 /* Perform the initialization. */
5081 int saved_stmts_are_full_exprs_p;
5083 gcc_assert (building_stmt_tree ());
5084 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
5085 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
5086 finish_expr_stmt (init);
5087 current_stmt_tree ()->stmts_are_full_exprs_p =
5088 saved_stmts_are_full_exprs_p;
5091 /* Set this to 0 so we can tell whether an aggregate which was
5092 initialized was ever used. Don't do this if it has a
5093 destructor, so we don't complain about the 'resource
5094 allocation is initialization' idiom. Now set
5095 attribute((unused)) on types so decls of that type will be
5096 marked used. (see TREE_USED, above.) */
5097 if (TYPE_NEEDS_CONSTRUCTING (type)
5099 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
5100 && DECL_NAME (decl))
5101 TREE_USED (decl) = 0;
5102 else if (already_used)
5103 TREE_USED (decl) = 1;
5106 /* Generate a cleanup, if necessary. */
5107 cleanup = cxx_maybe_build_cleanup (decl);
5108 if (DECL_SIZE (decl) && cleanup)
5109 finish_decl_cleanup (decl, cleanup);
5112 /* DECL is a VAR_DECL for a compiler-generated variable with static
5113 storage duration (like a virtual table) whose initializer is a
5114 compile-time constant. INIT must be either a TREE_LIST of values,
5115 or a CONSTRUCTOR. Initialize the variable and provide it to the
5119 initialize_artificial_var (tree decl, tree init)
5121 gcc_assert (DECL_ARTIFICIAL (decl));
5122 if (TREE_CODE (init) == TREE_LIST)
5123 init = build_constructor_from_list (NULL_TREE, init);
5124 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
5125 DECL_INITIAL (decl) = init;
5126 DECL_INITIALIZED_P (decl) = 1;
5127 determine_visibility (decl);
5128 layout_var_decl (decl);
5129 maybe_commonize_var (decl);
5130 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
5133 /* INIT is the initializer for a variable, as represented by the
5134 parser. Returns true iff INIT is value-dependent. */
5137 value_dependent_init_p (tree init)
5139 if (TREE_CODE (init) == TREE_LIST)
5140 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
5141 return any_value_dependent_elements_p (init);
5142 else if (TREE_CODE (init) == CONSTRUCTOR)
5143 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
5145 VEC(constructor_elt, gc) *elts;
5149 elts = CONSTRUCTOR_ELTS (init);
5150 nelts = VEC_length (constructor_elt, elts);
5151 for (i = 0; i < nelts; ++i)
5152 if (value_dependent_init_p (VEC_index (constructor_elt,
5157 /* It must be a simple expression, e.g., int i = 3; */
5158 return value_dependent_expression_p (init);
5163 /* Finish processing of a declaration;
5164 install its line number and initial value.
5165 If the length of an array type is not known before,
5166 it must be determined now, from the initial value, or it is an error.
5168 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
5169 true, then INIT is an integral constant expression.
5171 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
5172 if the (init) syntax was used. */
5175 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
5176 tree asmspec_tree, int flags)
5180 const char *asmspec = NULL;
5181 int was_readonly = 0;
5182 bool var_definition_p = false;
5183 int saved_processing_template_decl;
5185 if (decl == error_mark_node)
5190 error ("assignment (not initialization) in declaration");
5194 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
5195 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
5196 gcc_assert (TREE_CODE (decl) != PARM_DECL);
5198 type = TREE_TYPE (decl);
5199 if (type == error_mark_node)
5202 /* Assume no cleanup is required. */
5203 cleanup = NULL_TREE;
5204 saved_processing_template_decl = processing_template_decl;
5206 /* If a name was specified, get the string. */
5207 if (global_scope_p (current_binding_level))
5208 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5209 if (asmspec_tree && asmspec_tree != error_mark_node)
5210 asmspec = TREE_STRING_POINTER (asmspec_tree);
5212 if (current_class_type
5213 && CP_DECL_CONTEXT (decl) == current_class_type
5214 && TYPE_BEING_DEFINED (current_class_type)
5215 && (DECL_INITIAL (decl) || init))
5216 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
5218 if (processing_template_decl)
5220 bool type_dependent_p;
5222 /* Add this declaration to the statement-tree. */
5223 if (at_function_scope_p ())
5224 add_decl_expr (decl);
5226 type_dependent_p = dependent_type_p (type);
5228 if (init && init_const_expr_p)
5230 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5231 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl))
5232 TREE_CONSTANT (decl) = 1;
5235 /* Generally, initializers in templates are expanded when the
5236 template is instantiated. But, if DECL is an integral
5237 constant static data member, then it can be used in future
5238 integral constant expressions, and its value must be
5241 && DECL_CLASS_SCOPE_P (decl)
5242 && DECL_INTEGRAL_CONSTANT_VAR_P (decl)
5243 && !type_dependent_p
5244 && !value_dependent_init_p (init)))
5247 DECL_INITIAL (decl) = init;
5248 if (TREE_CODE (decl) == VAR_DECL
5249 && !DECL_PRETTY_FUNCTION_P (decl)
5250 && !type_dependent_p)
5251 maybe_deduce_size_from_array_init (decl, init);
5255 init = fold_non_dependent_expr (init);
5256 processing_template_decl = 0;
5259 /* Take care of TYPE_DECLs up front. */
5260 if (TREE_CODE (decl) == TYPE_DECL)
5262 if (type != error_mark_node
5263 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
5265 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
5266 warning (0, "shadowing previous type declaration of %q#D", decl);
5267 set_identifier_type_value (DECL_NAME (decl), decl);
5270 /* If we have installed this as the canonical typedef for this
5271 type, and that type has not been defined yet, delay emitting
5272 the debug information for it, as we will emit it later. */
5273 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
5274 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
5275 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5277 rest_of_decl_compilation (decl, DECL_CONTEXT (decl) == NULL_TREE,
5282 /* A reference will be modified here, as it is initialized. */
5283 if (! DECL_EXTERNAL (decl)
5284 && TREE_READONLY (decl)
5285 && TREE_CODE (type) == REFERENCE_TYPE)
5288 TREE_READONLY (decl) = 0;
5291 if (TREE_CODE (decl) == VAR_DECL)
5293 /* Only PODs can have thread-local storage. Other types may require
5294 various kinds of non-trivial initialization. */
5295 if (DECL_THREAD_LOCAL_P (decl) && !pod_type_p (TREE_TYPE (decl)))
5296 error ("%qD cannot be thread-local because it has non-POD type %qT",
5297 decl, TREE_TYPE (decl));
5298 /* If this is a local variable that will need a mangled name,
5299 register it now. We must do this before processing the
5300 initializer for the variable, since the initialization might
5301 require a guard variable, and since the mangled name of the
5302 guard variable will depend on the mangled name of this
5304 if (!processing_template_decl
5305 && DECL_FUNCTION_SCOPE_P (decl)
5306 && TREE_STATIC (decl)
5307 && !DECL_ARTIFICIAL (decl))
5308 push_local_name (decl);
5309 /* Convert the initializer to the type of DECL, if we have not
5310 already initialized DECL. */
5311 if (!DECL_INITIALIZED_P (decl)
5312 /* If !DECL_EXTERNAL then DECL is being defined. In the
5313 case of a static data member initialized inside the
5314 class-specifier, there can be an initializer even if DECL
5315 is *not* defined. */
5316 && (!DECL_EXTERNAL (decl) || init))
5320 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
5321 if (init_const_expr_p)
5323 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5324 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl))
5325 TREE_CONSTANT (decl) = 1;
5328 init = check_initializer (decl, init, flags, &cleanup);
5329 /* Thread-local storage cannot be dynamically initialized. */
5330 if (DECL_THREAD_LOCAL_P (decl) && init)
5332 error ("%qD is thread-local and so cannot be dynamically "
5333 "initialized", decl);
5337 /* Check that the initializer for a static data member was a
5338 constant. Although we check in the parser that the
5339 initializer is an integral constant expression, we do not
5340 simplify division-by-zero at the point at which it
5341 occurs. Therefore, in:
5343 struct S { static const int i = 7 / 0; };
5345 we issue an error at this point. It would
5346 probably be better to forbid division by zero in
5347 integral constant expressions. */
5348 if (DECL_EXTERNAL (decl) && init)
5350 error ("%qD cannot be initialized by a non-constant expression"
5351 " when being declared", decl);
5352 DECL_INITIALIZED_IN_CLASS_P (decl) = 0;
5360 The memory occupied by any object of static storage
5361 duration is zero-initialized at program startup before
5362 any other initialization takes place.
5364 We cannot create an appropriate initializer until after
5365 the type of DECL is finalized. If DECL_INITIAL is set,
5366 then the DECL is statically initialized, and any
5367 necessary zero-initialization has already been performed. */
5368 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
5369 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
5370 /*nelts=*/NULL_TREE,
5371 /*static_storage_p=*/true);
5372 /* Remember that the initialization for this variable has
5374 DECL_INITIALIZED_P (decl) = 1;
5375 /* This declaration is the definition of this variable,
5376 unless we are initializing a static data member within
5377 the class specifier. */
5378 if (!DECL_EXTERNAL (decl))
5379 var_definition_p = true;
5381 /* If the variable has an array type, lay out the type, even if
5382 there is no initializer. It is valid to index through the
5383 array, and we must get TYPE_ALIGN set correctly on the array
5385 else if (TREE_CODE (type) == ARRAY_TYPE)
5389 /* Add this declaration to the statement-tree. This needs to happen
5390 after the call to check_initializer so that the DECL_EXPR for a
5391 reference temp is added before the DECL_EXPR for the reference itself. */
5392 if (at_function_scope_p ())
5393 add_decl_expr (decl);
5395 /* Let the middle end know about variables and functions -- but not
5396 static data members in uninstantiated class templates. */
5397 if (!saved_processing_template_decl
5398 && (TREE_CODE (decl) == VAR_DECL
5399 || TREE_CODE (decl) == FUNCTION_DECL))
5401 if (TREE_CODE (decl) == VAR_DECL)
5403 layout_var_decl (decl);
5404 maybe_commonize_var (decl);
5407 make_rtl_for_nonlocal_decl (decl, init, asmspec);
5409 /* Check for abstractness of the type. Notice that there is no
5410 need to strip array types here since the check for those types
5411 is already done within create_array_type_for_decl. */
5412 if (TREE_CODE (type) == FUNCTION_TYPE
5413 || TREE_CODE (type) == METHOD_TYPE)
5414 abstract_virtuals_error (decl, TREE_TYPE (type));
5416 abstract_virtuals_error (decl, type);
5418 /* This needs to happen after the linkage is set. */
5419 determine_visibility (decl);
5421 if (TREE_CODE (decl) == FUNCTION_DECL
5422 || TREE_TYPE (decl) == error_mark_node)
5423 /* No initialization required. */
5425 else if (DECL_EXTERNAL (decl)
5426 && ! (DECL_LANG_SPECIFIC (decl)
5427 && DECL_NOT_REALLY_EXTERN (decl)))
5430 DECL_INITIAL (decl) = init;
5434 /* A variable definition. */
5435 if (DECL_FUNCTION_SCOPE_P (decl))
5437 /* Initialize the local variable. */
5438 if (processing_template_decl)
5439 DECL_INITIAL (decl) = init;
5440 else if (!TREE_STATIC (decl))
5441 initialize_local_var (decl, init);
5444 /* If a variable is defined, and then a subsequent
5445 definition with external linkage is encountered, we will
5446 get here twice for the same variable. We want to avoid
5447 calling expand_static_init more than once. For variables
5448 that are not static data members, we can call
5449 expand_static_init only when we actually process the
5450 initializer. It is not legal to redeclare a static data
5451 member, so this issue does not arise in that case. */
5452 if (var_definition_p && TREE_STATIC (decl))
5454 /* If a TREE_READONLY variable needs initialization
5455 at runtime, it is no longer readonly and we need to
5456 avoid MEM_READONLY_P being set on RTL created for it. */
5459 if (TREE_READONLY (decl))
5460 TREE_READONLY (decl) = 0;
5463 expand_static_init (decl, init);
5468 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
5469 reference, insert it in the statement-tree now. */
5471 push_cleanup (decl, cleanup, false);
5474 processing_template_decl = saved_processing_template_decl;
5477 TREE_READONLY (decl) = 1;
5479 /* If this was marked 'used', be sure it will be output. */
5480 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
5481 mark_decl_referenced (decl);
5484 /* This is here for a midend callback from c-common.c. */
5487 finish_decl (tree decl, tree init, tree asmspec_tree)
5489 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, asmspec_tree, 0);
5492 /* Returns a declaration for a VAR_DECL as if:
5494 extern "C" TYPE NAME;
5496 had been seen. Used to create compiler-generated global
5500 declare_global_var (tree name, tree type)
5504 push_to_top_level ();
5505 decl = build_decl (VAR_DECL, name, type);
5506 TREE_PUBLIC (decl) = 1;
5507 DECL_EXTERNAL (decl) = 1;
5508 DECL_ARTIFICIAL (decl) = 1;
5509 /* If the user has explicitly declared this variable (perhaps
5510 because the code we are compiling is part of a low-level runtime
5511 library), then it is possible that our declaration will be merged
5512 with theirs by pushdecl. */
5513 decl = pushdecl (decl);
5514 finish_decl (decl, NULL_TREE, NULL_TREE);
5515 pop_from_top_level ();
5520 /* Returns a pointer to the `atexit' function. Note that if
5521 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
5522 `__cxa_atexit' function specified in the IA64 C++ ABI. */
5525 get_atexit_node (void)
5532 bool use_aeabi_atexit;
5537 if (flag_use_cxa_atexit)
5539 /* The declaration for `__cxa_atexit' is:
5541 int __cxa_atexit (void (*)(void *), void *, void *)
5543 We build up the argument types and then then function type
5546 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
5547 /* First, build the pointer-to-function type for the first
5549 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
5550 fn_type = build_function_type (void_type_node, arg_types);
5551 fn_ptr_type = build_pointer_type (fn_type);
5552 /* Then, build the rest of the argument types. */
5553 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
5554 if (use_aeabi_atexit)
5556 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
5557 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
5561 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
5562 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
5564 /* And the final __cxa_atexit type. */
5565 fn_type = build_function_type (integer_type_node, arg_types);
5566 fn_ptr_type = build_pointer_type (fn_type);
5567 if (use_aeabi_atexit)
5568 name = "__aeabi_atexit";
5570 name = "__cxa_atexit";
5574 /* The declaration for `atexit' is:
5576 int atexit (void (*)());
5578 We build up the argument types and then then function type
5580 fn_type = build_function_type (void_type_node, void_list_node);
5581 fn_ptr_type = build_pointer_type (fn_type);
5582 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
5583 /* Build the final atexit type. */
5584 fn_type = build_function_type (integer_type_node, arg_types);
5588 /* Now, build the function declaration. */
5589 push_lang_context (lang_name_c);
5590 atexit_fndecl = build_library_fn_ptr (name, fn_type);
5591 mark_used (atexit_fndecl);
5592 pop_lang_context ();
5593 atexit_node = decay_conversion (atexit_fndecl);
5598 /* Returns the __dso_handle VAR_DECL. */
5601 get_dso_handle_node (void)
5603 if (dso_handle_node)
5604 return dso_handle_node;
5606 /* Declare the variable. */
5607 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
5610 return dso_handle_node;
5613 /* Begin a new function with internal linkage whose job will be simply
5614 to destroy some particular variable. */
5616 static GTY(()) int start_cleanup_cnt;
5619 start_cleanup_fn (void)
5626 push_to_top_level ();
5628 /* No need to mangle this. */
5629 push_lang_context (lang_name_c);
5631 /* Build the parameter-types. */
5632 parmtypes = void_list_node;
5633 /* Functions passed to __cxa_atexit take an additional parameter.
5634 We'll just ignore it. After we implement the new calling
5635 convention for destructors, we can eliminate the use of
5636 additional cleanup functions entirely in the -fnew-abi case. */
5637 if (flag_use_cxa_atexit)
5638 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
5639 /* Build the function type itself. */
5640 fntype = build_function_type (void_type_node, parmtypes);
5641 /* Build the name of the function. */
5642 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
5643 /* Build the function declaration. */
5644 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
5645 /* It's a function with internal linkage, generated by the
5647 TREE_PUBLIC (fndecl) = 0;
5648 DECL_ARTIFICIAL (fndecl) = 1;
5649 /* Make the function `inline' so that it is only emitted if it is
5650 actually needed. It is unlikely that it will be inlined, since
5651 it is only called via a function pointer, but we avoid unnecessary
5652 emissions this way. */
5653 DECL_INLINE (fndecl) = 1;
5654 DECL_DECLARED_INLINE_P (fndecl) = 1;
5655 DECL_INTERFACE_KNOWN (fndecl) = 1;
5656 /* Build the parameter. */
5657 if (flag_use_cxa_atexit)
5661 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
5662 DECL_CONTEXT (parmdecl) = fndecl;
5663 TREE_USED (parmdecl) = 1;
5664 DECL_ARGUMENTS (fndecl) = parmdecl;
5668 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
5670 pop_lang_context ();
5672 return current_function_decl;
5675 /* Finish the cleanup function begun by start_cleanup_fn. */
5678 end_cleanup_fn (void)
5680 expand_or_defer_fn (finish_function (0));
5682 pop_from_top_level ();
5685 /* Generate code to handle the destruction of DECL, an object with
5686 static storage duration. */
5689 register_dtor_fn (tree decl)
5696 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5697 return void_zero_node;
5699 /* Call build_cleanup before we enter the anonymous function so that
5700 any access checks will be done relative to the current scope,
5701 rather than the scope of the anonymous function. */
5702 build_cleanup (decl);
5704 /* Now start the function. */
5705 cleanup = start_cleanup_fn ();
5707 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
5708 to the original function, rather than the anonymous one. That
5709 will make the back-end think that nested functions are in use,
5710 which causes confusion. */
5712 push_deferring_access_checks (dk_no_check);
5713 fcall = build_cleanup (decl);
5714 pop_deferring_access_checks ();
5716 /* Create the body of the anonymous function. */
5717 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
5718 finish_expr_stmt (fcall);
5719 finish_compound_stmt (compound_stmt);
5722 /* Call atexit with the cleanup function. */
5723 cxx_mark_addressable (cleanup);
5724 mark_used (cleanup);
5725 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
5726 if (flag_use_cxa_atexit)
5728 args = tree_cons (NULL_TREE,
5729 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
5731 if (targetm.cxx.use_aeabi_atexit ())
5733 args = tree_cons (NULL_TREE, cleanup, args);
5734 args = tree_cons (NULL_TREE, null_pointer_node, args);
5738 args = tree_cons (NULL_TREE, null_pointer_node, args);
5739 args = tree_cons (NULL_TREE, cleanup, args);
5743 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
5744 return build_function_call (get_atexit_node (), args);
5747 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
5748 is its initializer. Generate code to handle the construction
5749 and destruction of DECL. */
5752 expand_static_init (tree decl, tree init)
5754 gcc_assert (TREE_CODE (decl) == VAR_DECL);
5755 gcc_assert (TREE_STATIC (decl));
5757 /* Some variables require no initialization. */
5759 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
5760 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5763 if (DECL_FUNCTION_SCOPE_P (decl))
5765 /* Emit code to perform this initialization but once. */
5766 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
5767 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
5768 tree guard, guard_addr, guard_addr_list;
5769 tree acquire_fn, release_fn, abort_fn;
5772 /* Emit code to perform this initialization but once. This code
5775 static <type> guard;
5776 if (!guard.first_byte) {
5777 if (__cxa_guard_acquire (&guard)) {
5780 // Do initialization.
5781 flag = true; __cxa_guard_release (&guard);
5782 // Register variable for destruction at end of program.
5784 if (!flag) __cxa_guard_abort (&guard);
5788 Note that the `flag' variable is only set to 1 *after* the
5789 initialization is complete. This ensures that an exception,
5790 thrown during the construction, will cause the variable to
5791 reinitialized when we pass through this code again, as per:
5795 If the initialization exits by throwing an exception, the
5796 initialization is not complete, so it will be tried again
5797 the next time control enters the declaration.
5799 This process should be thread-safe, too; multiple threads
5800 should not be able to initialize the variable more than
5803 /* Create the guard variable. */
5804 guard = get_guard (decl);
5806 /* This optimization isn't safe on targets with relaxed memory
5807 consistency. On such targets we force synchronization in
5808 __cxa_guard_acquire. */
5809 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
5811 /* Begin the conditional initialization. */
5812 if_stmt = begin_if_stmt ();
5813 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
5814 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
5817 if (flag_threadsafe_statics)
5819 guard_addr = build_address (guard);
5820 guard_addr_list = build_tree_list (NULL_TREE, guard_addr);
5822 acquire_fn = get_identifier ("__cxa_guard_acquire");
5823 release_fn = get_identifier ("__cxa_guard_release");
5824 abort_fn = get_identifier ("__cxa_guard_abort");
5825 if (!get_global_value_if_present (acquire_fn, &acquire_fn))
5827 tree argtypes = tree_cons (NULL_TREE, TREE_TYPE (guard_addr),
5829 tree vfntype = build_function_type (void_type_node, argtypes);
5830 acquire_fn = push_library_fn
5831 (acquire_fn, build_function_type (integer_type_node, argtypes));
5832 release_fn = push_library_fn (release_fn, vfntype);
5833 abort_fn = push_library_fn (abort_fn, vfntype);
5837 release_fn = identifier_global_value (release_fn);
5838 abort_fn = identifier_global_value (abort_fn);
5841 inner_if_stmt = begin_if_stmt ();
5842 finish_if_stmt_cond (build_call (acquire_fn, guard_addr_list),
5845 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
5846 begin = get_target_expr (boolean_false_node);
5847 flag = TARGET_EXPR_SLOT (begin);
5849 TARGET_EXPR_CLEANUP (begin)
5850 = build3 (COND_EXPR, void_type_node, flag,
5852 build_call (abort_fn, guard_addr_list));
5853 CLEANUP_EH_ONLY (begin) = 1;
5855 /* Do the initialization itself. */
5856 init = add_stmt_to_compound (begin, init);
5857 init = add_stmt_to_compound
5858 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
5859 init = add_stmt_to_compound
5860 (init, build_call (release_fn, guard_addr_list));
5863 init = add_stmt_to_compound (init, set_guard (guard));
5865 /* Use atexit to register a function for destroying this static
5867 init = add_stmt_to_compound (init, register_dtor_fn (decl));
5869 finish_expr_stmt (init);
5871 if (flag_threadsafe_statics)
5873 finish_compound_stmt (inner_then_clause);
5874 finish_then_clause (inner_if_stmt);
5875 finish_if_stmt (inner_if_stmt);
5878 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
5880 finish_compound_stmt (then_clause);
5881 finish_then_clause (if_stmt);
5882 finish_if_stmt (if_stmt);
5886 static_aggregates = tree_cons (init, decl, static_aggregates);
5890 /* Make TYPE a complete type based on INITIAL_VALUE.
5891 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
5892 2 if there was no information (in which case assume 0 if DO_DEFAULT),
5893 3 if the initializer list is empty (in pedantic mode). */
5896 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
5899 tree type, elt_type;
5903 /* An array of character type can be initialized from a
5904 brace-enclosed string constant.
5906 FIXME: this code is duplicated from reshape_init. Probably
5907 we should just call reshape_init here? */
5908 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
5909 && TREE_CODE (initial_value) == CONSTRUCTOR
5910 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (initial_value)))
5912 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
5913 tree value = VEC_index (constructor_elt, v, 0)->value;
5915 if (TREE_CODE (value) == STRING_CST
5916 && VEC_length (constructor_elt, v) == 1)
5917 initial_value = value;
5921 failure = complete_array_type (ptype, initial_value, do_default);
5923 /* We can create the array before the element type is complete, which
5924 means that we didn't have these two bits set in the original type
5925 either. In completing the type, we are expected to propagate these
5926 bits. See also complete_type which does the same thing for arrays
5929 if (TYPE_DOMAIN (type))
5931 elt_type = TREE_TYPE (type);
5932 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
5933 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5934 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
5940 /* Return zero if something is declared to be a member of type
5941 CTYPE when in the context of CUR_TYPE. STRING is the error
5942 message to print in that case. Otherwise, quietly return 1. */
5945 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
5947 if (ctype && ctype != cur_type)
5949 if (flags == DTOR_FLAG)
5950 error ("destructor for alien class %qT cannot be a member", ctype);
5952 error ("constructor for alien class %qT cannot be a member", ctype);
5958 /* Subroutine of `grokdeclarator'. */
5960 /* Generate errors possibly applicable for a given set of specifiers.
5961 This is for ARM $7.1.2. */
5964 bad_specifiers (tree object,
5973 error ("%qD declared as a %<virtual%> %s", object, type);
5975 error ("%qD declared as an %<inline%> %s", object, type);
5977 error ("%<const%> and %<volatile%> function specifiers on "
5978 "%qD invalid in %s declaration",
5981 error ("%q+D declared as a friend", object);
5983 && (TREE_CODE (object) == TYPE_DECL
5984 || (!TYPE_PTRFN_P (TREE_TYPE (object))
5985 && !TYPE_REFFN_P (TREE_TYPE (object))
5986 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
5987 error ("%q+D declared with an exception specification", object);
5990 /* DECL is a member function or static data member and is presently
5991 being defined. Check that the definition is taking place in a
5995 check_class_member_definition_namespace (tree decl)
5997 /* These checks only apply to member functions and static data
5999 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6000 || TREE_CODE (decl) == VAR_DECL);
6001 /* We check for problems with specializations in pt.c in
6002 check_specialization_namespace, where we can issue better
6004 if (processing_specialization)
6006 /* There are no restrictions on the placement of
6007 explicit instantiations. */
6008 if (processing_explicit_instantiation)
6012 A member function definition that appears outside of the
6013 class definition shall appear in a namespace scope enclosing
6014 the class definition.
6018 The definition for a static data member shall appear in a
6019 namespace scope enclosing the member's class definition. */
6020 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
6021 pedwarn ("definition of %qD is not in namespace enclosing %qT",
6022 decl, DECL_CONTEXT (decl));
6025 /* Build a PARM_DECL for the "this" parameter. TYPE is the
6026 METHOD_TYPE for a non-static member function; QUALS are the
6027 cv-qualifiers that apply to the function. */
6030 build_this_parm (tree type, cp_cv_quals quals)
6035 cp_cv_quals this_quals;
6037 this_type = TREE_VALUE (TYPE_ARG_TYPES (type));
6038 /* The `this' parameter is implicitly `const'; it cannot be
6040 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
6041 qual_type = cp_build_qualified_type (this_type, this_quals);
6042 parm = build_artificial_parm (this_identifier, qual_type);
6043 cp_apply_type_quals_to_decl (this_quals, parm);
6047 /* CTYPE is class type, or null if non-class.
6048 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
6050 DECLARATOR is the function's name.
6051 PARMS is a chain of PARM_DECLs for the function.
6052 VIRTUALP is truthvalue of whether the function is virtual or not.
6053 FLAGS are to be passed through to `grokclassfn'.
6054 QUALS are qualifiers indicating whether the function is `const'
6056 RAISES is a list of exceptions that this function can raise.
6057 CHECK is 1 if we must find this method in CTYPE, 0 if we should
6058 not look, and -1 if we should not call `grokclassfn' at all.
6060 SFK is the kind of special function (if any) for the new function.
6062 Returns `NULL_TREE' if something goes wrong, after issuing
6063 applicable error messages. */
6066 grokfndecl (tree ctype,
6070 tree orig_declarator,
6072 enum overload_flags flags,
6079 special_function_kind sfk,
6086 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
6090 type = build_exception_variant (type, raises);
6092 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
6093 if (TREE_CODE (type) == METHOD_TYPE)
6096 parm = build_this_parm (type, quals);
6097 TREE_CHAIN (parm) = parms;
6100 DECL_ARGUMENTS (decl) = parms;
6101 /* Propagate volatile out from type to decl. */
6102 if (TYPE_VOLATILE (type))
6103 TREE_THIS_VOLATILE (decl) = 1;
6105 /* If pointers to member functions use the least significant bit to
6106 indicate whether a function is virtual, ensure a pointer
6107 to this function will have that bit clear. */
6108 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6109 && TREE_CODE (type) == METHOD_TYPE
6110 && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT)
6111 DECL_ALIGN (decl) = 2 * BITS_PER_UNIT;
6114 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
6118 ("defining explicit specialization %qD in friend declaration",
6122 tree fns = TREE_OPERAND (orig_declarator, 0);
6123 tree args = TREE_OPERAND (orig_declarator, 1);
6125 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
6127 /* Something like `template <class T> friend void f<T>()'. */
6128 error ("invalid use of template-id %qD in declaration "
6129 "of primary template",
6135 /* A friend declaration of the form friend void f<>(). Record
6136 the information in the TEMPLATE_ID_EXPR. */
6137 SET_DECL_IMPLICIT_INSTANTIATION (decl);
6139 if (TREE_CODE (fns) == COMPONENT_REF)
6141 /* Due to bison parser ickiness, we will have already looked
6142 up an operator_name or PFUNCNAME within the current class
6143 (see template_id in parse.y). If the current class contains
6144 such a name, we'll get a COMPONENT_REF here. Undo that. */
6146 gcc_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
6147 == current_class_type);
6148 fns = TREE_OPERAND (fns, 1);
6150 gcc_assert (TREE_CODE (fns) == IDENTIFIER_NODE
6151 || TREE_CODE (fns) == OVERLOAD);
6152 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
6154 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
6155 if (TREE_PURPOSE (t)
6156 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
6158 error ("default arguments are not allowed in declaration "
6159 "of friend template specialization %qD",
6166 error ("%<inline%> is not allowed in declaration of friend "
6167 "template specialization %qD",
6174 /* If this decl has namespace scope, set that up. */
6176 set_decl_namespace (decl, in_namespace, friendp);
6178 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
6180 /* `main' and builtins have implicit 'C' linkage. */
6181 if ((MAIN_NAME_P (declarator)
6182 || (IDENTIFIER_LENGTH (declarator) > 10
6183 && IDENTIFIER_POINTER (declarator)[0] == '_'
6184 && IDENTIFIER_POINTER (declarator)[1] == '_'
6185 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
6186 && current_lang_name == lang_name_cplusplus
6187 && ctype == NULL_TREE
6188 /* NULL_TREE means global namespace. */
6189 && DECL_CONTEXT (decl) == NULL_TREE)
6190 SET_DECL_LANGUAGE (decl, lang_c);
6192 /* Should probably propagate const out from type to decl I bet (mrs). */
6195 DECL_STATIC_FUNCTION_P (decl) = 1;
6196 DECL_CONTEXT (decl) = ctype;
6201 DECL_CONTEXT (decl) = ctype;
6203 check_class_member_definition_namespace (decl);
6206 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
6208 if (processing_template_decl)
6209 error ("cannot declare %<::main%> to be a template");
6211 error ("cannot declare %<::main%> to be inline");
6213 error ("cannot declare %<::main%> to be static");
6218 /* Members of anonymous types and local classes have no linkage; make
6219 them internal. If a typedef is made later, this will be changed. */
6220 if (ctype && (TYPE_ANONYMOUS_P (ctype)
6221 || decl_function_context (TYPE_MAIN_DECL (ctype))))
6226 /* [basic.link]: A name with no linkage (notably, the name of a class
6227 or enumeration declared in a local scope) shall not be used to
6228 declare an entity with linkage.
6230 Only check this for public decls for now. See core 319, 389. */
6231 t = no_linkage_check (TREE_TYPE (decl),
6232 /*relaxed_p=*/false);
6235 if (TYPE_ANONYMOUS_P (t))
6237 if (DECL_EXTERN_C_P (decl))
6238 /* Allow this; it's pretty common in C. */;
6241 pedwarn ("non-local function %q#D uses anonymous type",
6243 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
6244 pedwarn ("%q+#D does not refer to the unqualified "
6245 "type, so it is not used for linkage",
6250 pedwarn ("non-local function %q#D uses local type %qT", decl, t);
6254 TREE_PUBLIC (decl) = publicp;
6257 DECL_INTERFACE_KNOWN (decl) = 1;
6258 DECL_NOT_REALLY_EXTERN (decl) = 1;
6261 /* If the declaration was declared inline, mark it as such. */
6263 DECL_DECLARED_INLINE_P (decl) = 1;
6264 /* We inline functions that are explicitly declared inline, or, when
6265 the user explicitly asks us to, all functions. */
6266 if (DECL_DECLARED_INLINE_P (decl)
6267 || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag))
6268 DECL_INLINE (decl) = 1;
6270 DECL_EXTERNAL (decl) = 1;
6271 if (quals && TREE_CODE (type) == FUNCTION_TYPE)
6273 error ("%smember function %qD cannot have cv-qualifier",
6274 (ctype ? "static " : "non-"), decl);
6275 quals = TYPE_UNQUALIFIED;
6278 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
6279 && !grok_op_properties (decl, /*complain=*/true))
6282 if (ctype && decl_function_context (decl))
6283 DECL_NO_STATIC_CHAIN (decl) = 1;
6286 /* Make the init_value nonzero so pushdecl knows this is not
6287 tentative. error_mark_node is replaced later with the BLOCK. */
6288 DECL_INITIAL (decl) = error_mark_node;
6290 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
6291 TREE_NOTHROW (decl) = 1;
6293 /* Caller will do the rest of this. */
6297 if (ctype != NULL_TREE)
6299 if (sfk == sfk_constructor)
6300 DECL_CONSTRUCTOR_P (decl) = 1;
6302 grokclassfn (ctype, decl, flags);
6305 decl = check_explicit_specialization (orig_declarator, decl,
6308 4 * (friendp != 0));
6309 if (decl == error_mark_node)
6314 cplus_decl_attributes (&decl, *attrlist, 0);
6315 *attrlist = NULL_TREE;
6318 /* Check main's type after attributes have been applied. */
6319 if (ctype == NULL_TREE && DECL_MAIN_P (decl)
6320 && !same_type_p (TREE_TYPE (TREE_TYPE (decl)),
6323 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
6325 error ("%<::main%> must return %<int%>");
6326 newtype = build_function_type (integer_type_node, oldtypeargs);
6327 TREE_TYPE (decl) = newtype;
6330 if (ctype != NULL_TREE
6331 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
6336 old_decl = check_classfn (ctype, decl,
6337 (processing_template_decl
6338 > template_class_depth (ctype))
6339 ? current_template_parms
6346 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
6347 /* Because grokfndecl is always supposed to return a
6348 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
6349 here. We depend on our callers to figure out that its
6350 really a template that's being returned. */
6351 old_decl = DECL_TEMPLATE_RESULT (old_decl);
6353 if (DECL_STATIC_FUNCTION_P (old_decl)
6354 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
6355 /* Remove the `this' parm added by grokclassfn.
6356 XXX Isn't this done in start_function, too? */
6357 revert_static_member_fn (decl);
6358 if (DECL_ARTIFICIAL (old_decl))
6359 error ("definition of implicitly-declared %qD", old_decl);
6361 /* Since we've smashed OLD_DECL to its
6362 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
6363 if (TREE_CODE (decl) == TEMPLATE_DECL)
6364 decl = DECL_TEMPLATE_RESULT (decl);
6366 /* Attempt to merge the declarations. This can fail, in
6367 the case of some invalid specialization declarations. */
6368 pushed_scope = push_scope (ctype);
6369 ok = duplicate_decls (decl, old_decl, friendp);
6371 pop_scope (pushed_scope);
6374 error ("no %q#D member function declared in class %qT",
6382 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
6385 if (ctype == NULL_TREE || check)
6389 DECL_VIRTUAL_P (decl) = 1;
6394 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
6395 the linkage that DECL will receive in the object file. */
6398 set_linkage_for_static_data_member (tree decl)
6400 /* A static data member always has static storage duration and
6401 external linkage. Note that static data members are forbidden in
6402 local classes -- the only situation in which a class has
6403 non-external linkage. */
6404 TREE_PUBLIC (decl) = 1;
6405 TREE_STATIC (decl) = 1;
6406 /* For non-template classes, static data members are always put
6407 out in exactly those files where they are defined, just as
6408 with ordinary namespace-scope variables. */
6409 if (!processing_template_decl)
6410 DECL_INTERFACE_KNOWN (decl) = 1;
6413 /* Create a VAR_DECL named NAME with the indicated TYPE.
6415 If SCOPE is non-NULL, it is the class type or namespace containing
6416 the variable. If SCOPE is NULL, the variable should is created in
6417 the innermost enclosings scope. */
6420 grokvardecl (tree type,
6422 const cp_decl_specifier_seq *declspecs,
6428 tree explicit_scope;
6430 gcc_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE);
6432 /* Compute the scope in which to place the variable, but remember
6433 whether or not that scope was explicitly specified by the user. */
6434 explicit_scope = scope;
6437 /* An explicit "extern" specifier indicates a namespace-scope
6439 if (declspecs->storage_class == sc_extern)
6440 scope = current_namespace;
6441 else if (!at_function_scope_p ())
6442 scope = current_scope ();
6446 && (/* If the variable is a namespace-scope variable declared in a
6447 template, we need DECL_LANG_SPECIFIC. */
6448 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
6449 /* Similarly for namespace-scope variables with language linkage
6451 || (TREE_CODE (scope) == NAMESPACE_DECL
6452 && current_lang_name != lang_name_cplusplus)
6453 /* Similarly for static data members. */
6455 decl = build_lang_decl (VAR_DECL, name, type);
6457 decl = build_decl (VAR_DECL, name, type);
6459 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
6460 set_decl_namespace (decl, explicit_scope, 0);
6462 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6464 if (declspecs->storage_class == sc_extern)
6466 DECL_THIS_EXTERN (decl) = 1;
6467 DECL_EXTERNAL (decl) = !initialized;
6470 if (DECL_CLASS_SCOPE_P (decl))
6472 set_linkage_for_static_data_member (decl);
6473 /* This function is only called with out-of-class definitions. */
6474 DECL_EXTERNAL (decl) = 0;
6475 check_class_member_definition_namespace (decl);
6477 /* At top level, either `static' or no s.c. makes a definition
6478 (perhaps tentative), and absence of `static' makes it public. */
6479 else if (toplevel_bindings_p ())
6481 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
6482 && (DECL_THIS_EXTERN (decl) || ! constp));
6483 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
6485 /* Not at top level, only `static' makes a static definition. */
6488 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
6489 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
6492 if (declspecs->specs[(int)ds_thread])
6494 if (targetm.have_tls)
6495 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
6497 /* A mere warning is sure to result in improper semantics
6498 at runtime. Don't bother to allow this to compile. */
6499 error ("thread-local storage not supported for this target");
6502 if (TREE_PUBLIC (decl))
6504 /* [basic.link]: A name with no linkage (notably, the name of a class
6505 or enumeration declared in a local scope) shall not be used to
6506 declare an entity with linkage.
6508 Only check this for public decls for now. */
6509 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
6512 if (TYPE_ANONYMOUS_P (t))
6514 if (DECL_EXTERN_C_P (decl))
6515 /* Allow this; it's pretty common in C. */
6519 /* DRs 132, 319 and 389 seem to indicate types with
6520 no linkage can only be used to declare extern "C"
6521 entities. Since it's not always an error in the
6522 ISO C++ 90 Standard, we only issue a warning. */
6523 warning (0, "non-local variable %q#D uses anonymous type",
6525 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
6526 warning (0, "%q+#D does not refer to the unqualified "
6527 "type, so it is not used for linkage",
6532 warning (0, "non-local variable %q#D uses local type %qT", decl, t);
6536 DECL_INTERFACE_KNOWN (decl) = 1;
6541 /* Create and return a canonical pointer to member function type, for
6542 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
6545 build_ptrmemfunc_type (tree type)
6549 tree unqualified_variant = NULL_TREE;
6551 if (type == error_mark_node)
6554 /* If a canonical type already exists for this type, use it. We use
6555 this method instead of type_hash_canon, because it only does a
6556 simple equality check on the list of field members. */
6558 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
6561 /* Make sure that we always have the unqualified pointer-to-member
6563 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
6565 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
6567 t = make_aggr_type (RECORD_TYPE);
6568 xref_basetypes (t, NULL_TREE);
6570 /* Let the front-end know this is a pointer to member function... */
6571 TYPE_PTRMEMFUNC_FLAG (t) = 1;
6572 /* ... and not really an aggregate. */
6573 SET_IS_AGGR_TYPE (t, 0);
6575 field = build_decl (FIELD_DECL, pfn_identifier, type);
6578 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
6579 TREE_CHAIN (field) = fields;
6582 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
6584 /* Zap out the name so that the back-end will give us the debugging
6585 information for this anonymous RECORD_TYPE. */
6586 TYPE_NAME (t) = NULL_TREE;
6588 /* If this is not the unqualified form of this pointer-to-member
6589 type, set the TYPE_MAIN_VARIANT for this type to be the
6590 unqualified type. Since they are actually RECORD_TYPEs that are
6591 not variants of each other, we must do this manually. */
6592 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
6594 t = build_qualified_type (t, cp_type_quals (type));
6595 TYPE_MAIN_VARIANT (t) = unqualified_variant;
6596 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
6597 TYPE_NEXT_VARIANT (unqualified_variant) = t;
6600 /* Cache this pointer-to-member type so that we can find it again
6602 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
6607 /* Create and return a pointer to data member type. */
6610 build_ptrmem_type (tree class_type, tree member_type)
6612 if (TREE_CODE (member_type) == METHOD_TYPE)
6616 arg_types = TYPE_ARG_TYPES (member_type);
6617 class_type = (cp_build_qualified_type
6619 cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types)))));
6621 = build_method_type_directly (class_type,
6622 TREE_TYPE (member_type),
6623 TREE_CHAIN (arg_types));
6624 return build_ptrmemfunc_type (build_pointer_type (member_type));
6628 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
6629 return build_offset_type (class_type, member_type);
6633 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
6634 Check to see that the definition is valid. Issue appropriate error
6635 messages. Return 1 if the definition is particularly bad, or 0
6639 check_static_variable_definition (tree decl, tree type)
6641 /* Motion 10 at San Diego: If a static const integral data member is
6642 initialized with an integral constant expression, the initializer
6643 may appear either in the declaration (within the class), or in
6644 the definition, but not both. If it appears in the class, the
6645 member is a member constant. The file-scope definition is always
6647 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
6649 error ("invalid in-class initialization of static data member "
6650 "of non-integral type %qT",
6652 /* If we just return the declaration, crashes will sometimes
6653 occur. We therefore return void_type_node, as if this were a
6654 friend declaration, to cause callers to completely ignore
6655 this declaration. */
6658 else if (!CP_TYPE_CONST_P (type))
6659 error ("ISO C++ forbids in-class initialization of non-const "
6660 "static member %qD",
6662 else if (pedantic && !INTEGRAL_TYPE_P (type))
6663 pedwarn ("ISO C++ forbids initialization of member constant "
6664 "%qD of non-integral type %qT", decl, type);
6669 /* Given the SIZE (i.e., number of elements) in an array, compute an
6670 appropriate index type for the array. If non-NULL, NAME is the
6671 name of the thing being declared. */
6674 compute_array_index_type (tree name, tree size)
6679 if (error_operand_p (size))
6680 return error_mark_node;
6682 type = TREE_TYPE (size);
6683 /* The array bound must be an integer type. */
6684 if (!dependent_type_p (type) && !INTEGRAL_TYPE_P (type))
6687 error ("size of array %qD has non-integral type %qT", name, type);
6689 error ("size of array has non-integral type %qT", type);
6690 size = integer_one_node;
6691 type = TREE_TYPE (size);
6694 if (abi_version_at_least (2)
6695 /* We should only handle value dependent expressions specially. */
6696 ? value_dependent_expression_p (size)
6697 /* But for abi-1, we handled all instances in templates. This
6698 effects the manglings produced. */
6699 : processing_template_decl)
6700 return build_index_type (build_min (MINUS_EXPR, sizetype,
6701 size, integer_one_node));
6703 /* The size might be the result of a cast. */
6704 STRIP_TYPE_NOPS (size);
6706 /* It might be a const variable or enumeration constant. */
6707 size = integral_constant_value (size);
6709 /* Normally, the array-bound will be a constant. */
6710 if (TREE_CODE (size) == INTEGER_CST)
6712 /* Check to see if the array bound overflowed. Make that an
6713 error, no matter how generous we're being. */
6714 int old_flag_pedantic_errors = flag_pedantic_errors;
6715 int old_pedantic = pedantic;
6716 pedantic = flag_pedantic_errors = 1;
6717 constant_expression_warning (size);
6718 pedantic = old_pedantic;
6719 flag_pedantic_errors = old_flag_pedantic_errors;
6721 /* An array must have a positive number of elements. */
6722 if (INT_CST_LT (size, integer_zero_node))
6725 error ("size of array %qD is negative", name);
6727 error ("size of array is negative");
6728 size = integer_one_node;
6730 /* As an extension we allow zero-sized arrays. We always allow
6731 them in system headers because glibc uses them. */
6732 else if (integer_zerop (size) && pedantic && !in_system_header)
6735 pedwarn ("ISO C++ forbids zero-size array %qD", name);
6737 pedwarn ("ISO C++ forbids zero-size array");
6740 else if (TREE_CONSTANT (size))
6742 /* `(int) &fn' is not a valid array bound. */
6744 error ("size of array %qD is not an integral constant-expression",
6747 error ("size of array is not an integral constant-expression");
6748 size = integer_one_node;
6750 else if (pedantic && warn_vla != 0)
6753 pedwarn ("ISO C++ forbids variable length array %qD", name);
6755 pedwarn ("ISO C++ forbids variable length array");
6757 else if (warn_vla > 0)
6761 "variable length array %qD is used", name);
6764 "variable length array is used");
6767 if (processing_template_decl && !TREE_CONSTANT (size))
6768 /* A variable sized array. */
6769 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
6772 HOST_WIDE_INT saved_processing_template_decl;
6774 /* Compute the index of the largest element in the array. It is
6775 one less than the number of elements in the array. We save
6776 and restore PROCESSING_TEMPLATE_DECL so that computations in
6777 cp_build_binary_op will be appropriately folded. */
6778 saved_processing_template_decl = processing_template_decl;
6779 processing_template_decl = 0;
6780 itype = cp_build_binary_op (MINUS_EXPR,
6781 cp_convert (ssizetype, size),
6782 cp_convert (ssizetype, integer_one_node));
6783 itype = fold (itype);
6784 processing_template_decl = saved_processing_template_decl;
6786 if (!TREE_CONSTANT (itype))
6787 /* A variable sized array. */
6788 itype = variable_size (itype);
6789 /* Make sure that there was no overflow when creating to a signed
6790 index type. (For example, on a 32-bit machine, an array with
6791 size 2^32 - 1 is too big.) */
6792 else if (TREE_CODE (itype) == INTEGER_CST
6793 && TREE_OVERFLOW (itype))
6795 error ("overflow in array dimension");
6796 TREE_OVERFLOW (itype) = 0;
6800 /* Create and return the appropriate index type. */
6801 return build_index_type (itype);
6804 /* Returns the scope (if any) in which the entity declared by
6805 DECLARATOR will be located. If the entity was declared with an
6806 unqualified name, NULL_TREE is returned. */
6809 get_scope_of_declarator (const cp_declarator *declarator)
6811 while (declarator && declarator->kind != cdk_id)
6812 declarator = declarator->declarator;
6814 /* If the declarator-id is a SCOPE_REF, the scope in which the
6815 declaration occurs is the first operand. */
6817 && declarator->u.id.qualifying_scope)
6818 return declarator->u.id.qualifying_scope;
6820 /* Otherwise, the declarator is not a qualified name; the entity will
6821 be declared in the current scope. */
6825 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
6826 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
6830 create_array_type_for_decl (tree name, tree type, tree size)
6832 tree itype = NULL_TREE;
6833 const char* error_msg;
6835 /* If things have already gone awry, bail now. */
6836 if (type == error_mark_node || size == error_mark_node)
6837 return error_mark_node;
6839 /* Assume that everything will go OK. */
6842 /* There are some types which cannot be array elements. */
6843 switch (TREE_CODE (type))
6846 error_msg = "array of void";
6850 error_msg = "array of functions";
6853 case REFERENCE_TYPE:
6854 error_msg = "array of references";
6858 error_msg = "array of function members";
6865 /* If something went wrong, issue an error-message and return. */
6869 error ("declaration of %qD as %s", name, error_msg);
6871 error ("creating %s", error_msg);
6873 return error_mark_node;
6878 The constant expressions that specify the bounds of the arrays
6879 can be omitted only for the first member of the sequence. */
6880 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
6883 error ("declaration of %qD as multidimensional array must "
6884 "have bounds for all dimensions except the first",
6887 error ("multidimensional array must have bounds for all "
6888 "dimensions except the first");
6890 return error_mark_node;
6893 /* Figure out the index type for the array. */
6895 itype = compute_array_index_type (name, size);
6898 T is called the array element type; this type shall not be [...] an
6899 abstract class type. */
6900 abstract_virtuals_error (name, type);
6902 return build_cplus_array_type (type, itype);
6905 /* Check that it's OK to declare a function with the indicated TYPE.
6906 SFK indicates the kind of special function (if any) that this
6907 function is. OPTYPE is the type given in a conversion operator
6908 declaration, or the class type for a constructor/destructor.
6909 Returns the actual return type of the function; that
6910 may be different than TYPE if an error occurs, or for certain
6911 special functions. */
6914 check_special_function_return_type (special_function_kind sfk,
6920 case sfk_constructor:
6922 error ("return type specification for constructor invalid");
6924 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
6925 type = build_pointer_type (optype);
6927 type = void_type_node;
6930 case sfk_destructor:
6932 error ("return type specification for destructor invalid");
6933 /* We can't use the proper return type here because we run into
6934 problems with ambiguous bases and covariant returns.
6935 Java classes are left unchanged because (void *) isn't a valid
6936 Java type, and we don't want to change the Java ABI. */
6937 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
6938 type = build_pointer_type (void_type_node);
6940 type = void_type_node;
6943 case sfk_conversion:
6944 if (type && !same_type_p (type, optype))
6945 error ("operator %qT declared to return %qT", optype, type);
6947 pedwarn ("return type specified for %<operator %T%>", optype);
6958 /* A variable or data member (whose unqualified name is IDENTIFIER)
6959 has been declared with the indicated TYPE. If the TYPE is not
6960 acceptable, issue an error message and return a type to use for
6961 error-recovery purposes. */
6964 check_var_type (tree identifier, tree type)
6966 if (VOID_TYPE_P (type))
6969 error ("unnamed variable or field declared void");
6970 else if (TREE_CODE (identifier) == IDENTIFIER_NODE)
6972 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
6973 error ("variable or field %qE declared void", identifier);
6976 error ("variable or field declared void");
6977 type = error_mark_node;
6983 /* Given declspecs and a declarator (abstract or otherwise), determine
6984 the name and type of the object declared and construct a DECL node
6987 DECLSPECS is a chain of tree_list nodes whose value fields
6988 are the storage classes and type specifiers.
6990 DECL_CONTEXT says which syntactic context this declaration is in:
6991 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6992 FUNCDEF for a function definition. Like NORMAL but a few different
6993 error messages in each case. Return value may be zero meaning
6994 this definition is too screwy to try to parse.
6995 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
6996 handle member functions (which have FIELD context).
6997 Return value may be zero meaning this definition is too screwy to
6999 PARM for a parameter declaration (either within a function prototype
7000 or before a function body). Make a PARM_DECL, or return void_type_node.
7001 CATCHPARM for a parameter declaration before a catch clause.
7002 TYPENAME if for a typename (in a cast or sizeof).
7003 Don't make a DECL node; just return the ..._TYPE node.
7004 FIELD for a struct or union field; make a FIELD_DECL.
7005 BITFIELD for a field with specified width.
7006 INITIALIZED is 1 if the decl has an initializer.
7008 ATTRLIST is a pointer to the list of attributes, which may be NULL
7009 if there are none; *ATTRLIST may be modified if attributes from inside
7010 the declarator should be applied to the declaration.
7012 When this function is called, scoping variables (such as
7013 CURRENT_CLASS_TYPE) should reflect the scope in which the
7014 declaration occurs, not the scope in which the new declaration will
7015 be placed. For example, on:
7019 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
7022 Returns a DECL (if a declarator is present), a TYPE (if there is no
7023 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
7027 grokdeclarator (const cp_declarator *declarator,
7028 const cp_decl_specifier_seq *declspecs,
7029 enum decl_context decl_context,
7033 tree type = NULL_TREE;
7035 int virtualp, explicitp, friendp, inlinep, staticp;
7036 int explicit_int = 0;
7037 int explicit_char = 0;
7038 int defaulted_int = 0;
7039 tree dependent_name = NULL_TREE;
7041 tree typedef_decl = NULL_TREE;
7042 const char *name = NULL;
7043 tree typedef_type = NULL_TREE;
7044 /* True if this declarator is a function definition. */
7045 bool funcdef_flag = false;
7046 cp_declarator_kind innermost_code = cdk_error;
7049 /* See the code below that used this. */
7050 tree decl_attr = NULL_TREE;
7053 /* Keep track of what sort of function is being processed
7054 so that we can warn about default return values, or explicit
7055 return values which do not match prescribed defaults. */
7056 special_function_kind sfk = sfk_none;
7058 tree dname = NULL_TREE;
7059 tree ctor_return_type = NULL_TREE;
7060 enum overload_flags flags = NO_SPECIAL;
7061 /* cv-qualifiers that apply to the declarator, for a declaration of
7062 a member function. */
7063 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
7064 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
7066 tree raises = NULL_TREE;
7067 int template_count = 0;
7068 tree returned_attrs = NULL_TREE;
7069 tree parms = NULL_TREE;
7070 const cp_declarator *id_declarator;
7071 /* The unqualified name of the declarator; either an
7072 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
7073 tree unqualified_id;
7074 /* The class type, if any, in which this entity is located,
7075 or NULL_TREE if none. Note that this value may be different from
7076 the current class type; for example if an attempt is made to declare
7077 "A::f" inside "B", this value will be "A". */
7078 tree ctype = current_class_type;
7079 /* The NAMESPACE_DECL for the namespace in which this entity is
7080 located. If an unqualified name is used to declare the entity,
7081 this value will be NULL_TREE, even if the entity is located at
7083 tree in_namespace = NULL_TREE;
7084 cp_storage_class storage_class;
7085 bool unsigned_p, signed_p, short_p, long_p, thread_p;
7086 bool type_was_error_mark_node = false;
7088 signed_p = declspecs->specs[(int)ds_signed];
7089 unsigned_p = declspecs->specs[(int)ds_unsigned];
7090 short_p = declspecs->specs[(int)ds_short];
7091 long_p = declspecs->specs[(int)ds_long];
7092 longlong = declspecs->specs[(int)ds_long] >= 2;
7093 thread_p = declspecs->specs[(int)ds_thread];
7095 if (decl_context == FUNCDEF)
7096 funcdef_flag = true, decl_context = NORMAL;
7097 else if (decl_context == MEMFUNCDEF)
7098 funcdef_flag = true, decl_context = FIELD;
7099 else if (decl_context == BITFIELD)
7100 bitfield = 1, decl_context = FIELD;
7102 /* Look inside a declarator for the name being declared
7103 and get it as a string, for an error message. */
7104 for (id_declarator = declarator;
7106 id_declarator = id_declarator->declarator)
7108 if (id_declarator->kind != cdk_id)
7109 innermost_code = id_declarator->kind;
7111 switch (id_declarator->kind)
7114 if (id_declarator->declarator
7115 && id_declarator->declarator->kind == cdk_id)
7117 sfk = id_declarator->declarator->u.id.sfk;
7118 if (sfk == sfk_destructor)
7125 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
7126 tree decl = id_declarator->u.id.unqualified_name;
7129 if (qualifying_scope)
7131 if (at_function_scope_p ())
7135 A declarator-id shall not be qualified except
7138 None of the cases are permitted in block
7140 if (qualifying_scope == global_namespace)
7141 error ("invalid use of qualified-name %<::%D%>",
7143 else if (TYPE_P (qualifying_scope))
7144 error ("invalid use of qualified-name %<%T::%D%>",
7145 qualifying_scope, decl);
7147 error ("invalid use of qualified-name %<%D::%D%>",
7148 qualifying_scope, decl);
7149 return error_mark_node;
7151 else if (TYPE_P (qualifying_scope))
7153 ctype = qualifying_scope;
7154 if (innermost_code != cdk_function
7155 && current_class_type
7156 && !UNIQUELY_DERIVED_FROM_P (ctype,
7157 current_class_type))
7159 error ("type %qT is not derived from type %qT",
7160 ctype, current_class_type);
7161 return error_mark_node;
7164 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
7165 in_namespace = qualifying_scope;
7167 switch (TREE_CODE (decl))
7173 if (innermost_code != cdk_function)
7175 error ("declaration of %qD as non-function", decl);
7176 return error_mark_node;
7178 else if (!qualifying_scope
7179 && !(current_class_type && at_class_scope_p ()))
7181 error ("declaration of %qD as non-member", decl);
7182 return error_mark_node;
7185 type = TREE_OPERAND (decl, 0);
7186 name = IDENTIFIER_POINTER (constructor_name (type));
7191 case TEMPLATE_ID_EXPR:
7193 tree fns = TREE_OPERAND (decl, 0);
7196 if (TREE_CODE (dname) != IDENTIFIER_NODE)
7198 gcc_assert (is_overloaded_fn (dname));
7199 dname = DECL_NAME (get_first_fn (dname));
7204 case IDENTIFIER_NODE:
7205 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7208 if (C_IS_RESERVED_WORD (dname))
7210 error ("declarator-id missing; using reserved word %qD",
7212 name = IDENTIFIER_POINTER (dname);
7214 else if (!IDENTIFIER_TYPENAME_P (dname))
7215 name = IDENTIFIER_POINTER (dname);
7218 gcc_assert (flags == NO_SPECIAL);
7219 flags = TYPENAME_FLAG;
7220 ctor_return_type = TREE_TYPE (dname);
7221 sfk = sfk_conversion;
7222 if (is_typename_at_global_scope (dname))
7223 name = IDENTIFIER_POINTER (dname);
7225 name = "<invalid operator>";
7241 return error_mark_node;
7247 if (id_declarator->kind == cdk_id)
7253 The declarator in a function-definition shall have the form
7254 D1 ( parameter-declaration-clause) ... */
7255 if (funcdef_flag && innermost_code != cdk_function)
7257 error ("function definition does not declare parameters");
7258 return error_mark_node;
7261 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
7262 && innermost_code != cdk_function
7263 && ! (ctype && !declspecs->any_specifiers_p))
7265 error ("declaration of %qD as non-function", dname);
7266 return error_mark_node;
7269 /* Anything declared one level down from the top level
7270 must be one of the parameters of a function
7271 (because the body is at least two levels down). */
7273 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
7274 by not allowing C++ class definitions to specify their parameters
7275 with xdecls (must be spec.d in the parmlist).
7277 Since we now wait to push a class scope until we are sure that
7278 we are in a legitimate method context, we must set oldcname
7279 explicitly (since current_class_name is not yet alive).
7281 We also want to avoid calling this a PARM if it is in a namespace. */
7283 if (decl_context == NORMAL && !toplevel_bindings_p ())
7285 struct cp_binding_level *b = current_binding_level;
7286 current_binding_level = b->level_chain;
7287 if (current_binding_level != 0 && toplevel_bindings_p ())
7288 decl_context = PARM;
7289 current_binding_level = b;
7293 name = decl_context == PARM ? "parameter" : "type name";
7295 /* If there were multiple types specified in the decl-specifier-seq,
7296 issue an error message. */
7297 if (declspecs->multiple_types_p)
7299 error ("two or more data types in declaration of %qs", name);
7300 return error_mark_node;
7303 /* Extract the basic type from the decl-specifier-seq. */
7304 type = declspecs->type;
7305 if (type == error_mark_node)
7308 type_was_error_mark_node = true;
7311 /* APPLE LOCAL begin unavailable attribute (radar 2809697) --bowdidge */
7312 /* If the entire declaration is itself tagged as unavailable then
7313 suppress reports of unavailable/deprecated items. If the
7314 entire declaration is tagged as only deprecated we still
7315 report unavailable uses. */
7316 if (type && TREE_DEPRECATED (type) && TREE_UNAVAILABLE (type))
7318 if (deprecated_state != DEPRECATED_UNAVAILABLE_SUPPRESS)
7319 warn_deprecated_use (type);
7322 /* APPLE LOCAL end unavailable attribute (radar 2809697) --bowdidge */
7323 /* If the entire declaration is itself tagged as deprecated then
7324 suppress reports of deprecated items. */
7325 if (type && TREE_DEPRECATED (type)
7326 && deprecated_state != DEPRECATED_SUPPRESS)
7327 warn_deprecated_use (type);
7328 if (type && TREE_CODE (type) == TYPE_DECL)
7330 typedef_decl = type;
7331 type = TREE_TYPE (typedef_decl);
7333 /* No type at all: default to `int', and set DEFAULTED_INT
7334 because it was not a user-defined typedef. */
7335 if (type == NULL_TREE && (signed_p || unsigned_p || long_p || short_p))
7337 /* These imply 'int'. */
7338 type = integer_type_node;
7342 explicit_int = declspecs->explicit_int_p;
7343 explicit_char = declspecs->explicit_char_p;
7346 /* See the code below that used this. */
7348 decl_attr = DECL_ATTRIBUTES (typedef_decl);
7350 typedef_type = type;
7353 if (sfk != sfk_conversion)
7354 ctor_return_type = ctype;
7356 if (sfk != sfk_none)
7357 type = check_special_function_return_type (sfk, type,
7359 else if (type == NULL_TREE)
7365 /* We handle `main' specially here, because 'main () { }' is so
7366 common. With no options, it is allowed. With -Wreturn-type,
7367 it is a warning. It is only an error with -pedantic-errors. */
7368 is_main = (funcdef_flag
7369 && dname && MAIN_NAME_P (dname)
7370 && ctype == NULL_TREE
7371 && in_namespace == NULL_TREE
7372 && current_namespace == global_namespace);
7374 if (type_was_error_mark_node)
7375 /* We've already issued an error, don't complain more. */;
7376 else if (in_system_header || flag_ms_extensions)
7377 /* Allow it, sigh. */;
7378 else if (pedantic || ! is_main)
7379 pedwarn ("ISO C++ forbids declaration of %qs with no type", name);
7380 else if (warn_return_type)
7381 warning (0, "ISO C++ forbids declaration of %qs with no type", name);
7383 type = integer_type_node;
7388 /* Now process the modifiers that were specified
7389 and check for invalid combinations. */
7391 /* Long double is a special combination. */
7392 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
7395 type = build_qualified_type (long_double_type_node,
7396 cp_type_quals (type));
7399 /* Check all other uses of type modifiers. */
7401 if (unsigned_p || signed_p || long_p || short_p)
7405 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
7406 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
7407 else if (signed_p && unsigned_p)
7408 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
7409 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
7410 error ("%<long long%> invalid for %qs", name);
7411 else if (long_p && TREE_CODE (type) == REAL_TYPE)
7412 error ("%<long%> invalid for %qs", name);
7413 else if (short_p && TREE_CODE (type) == REAL_TYPE)
7414 error ("%<short%> invalid for %qs", name);
7415 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
7416 error ("%<long%> or %<short%> invalid for %qs", name);
7417 else if ((long_p || short_p) && explicit_char)
7418 error ("%<long%> or %<short%> specified with char for %qs", name);
7419 else if (long_p && short_p)
7420 error ("%<long%> and %<short%> specified together for %qs", name);
7424 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
7426 pedwarn ("long, short, signed or unsigned used invalidly for %qs",
7428 if (flag_pedantic_errors)
7433 /* Discard the type modifiers if they are invalid. */
7444 /* Decide whether an integer type is signed or not.
7445 Optionally treat bitfields as signed by default. */
7449 It is implementation-defined whether a plain (neither
7450 explicitly signed or unsigned) char, short, int, or long
7451 bit-field is signed or unsigned.
7453 Naturally, we extend this to long long as well. Note that
7454 this does not include wchar_t. */
7455 || (bitfield && !flag_signed_bitfields
7457 /* A typedef for plain `int' without `signed' can be
7458 controlled just like plain `int', but a typedef for
7459 `signed int' cannot be so controlled. */
7461 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
7462 && TREE_CODE (type) == INTEGER_TYPE
7463 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
7466 type = long_long_unsigned_type_node;
7468 type = long_unsigned_type_node;
7470 type = short_unsigned_type_node;
7471 else if (type == char_type_node)
7472 type = unsigned_char_type_node;
7473 else if (typedef_decl)
7474 type = c_common_unsigned_type (type);
7476 type = unsigned_type_node;
7478 else if (signed_p && type == char_type_node)
7479 type = signed_char_type_node;
7481 type = long_long_integer_type_node;
7483 type = long_integer_type_node;
7485 type = short_integer_type_node;
7487 if (declspecs->specs[(int)ds_complex])
7489 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
7490 error ("complex invalid for %qs", name);
7491 /* If we just have "complex", it is equivalent to
7492 "complex double", but if any modifiers at all are specified it is
7493 the complex form of TYPE. E.g, "complex short" is
7494 "complex short int". */
7496 else if (defaulted_int && ! longlong
7497 && ! (long_p || short_p || signed_p || unsigned_p))
7498 type = complex_double_type_node;
7499 else if (type == integer_type_node)
7500 type = complex_integer_type_node;
7501 else if (type == float_type_node)
7502 type = complex_float_type_node;
7503 else if (type == double_type_node)
7504 type = complex_double_type_node;
7505 else if (type == long_double_type_node)
7506 type = complex_long_double_type_node;
7508 type = build_complex_type (type);
7511 type_quals = TYPE_UNQUALIFIED;
7512 if (declspecs->specs[(int)ds_const])
7513 type_quals |= TYPE_QUAL_CONST;
7514 if (declspecs->specs[(int)ds_volatile])
7515 type_quals |= TYPE_QUAL_VOLATILE;
7516 if (declspecs->specs[(int)ds_restrict])
7517 type_quals |= TYPE_QUAL_RESTRICT;
7518 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
7519 error ("qualifiers are not allowed on declaration of %<operator %T%>",
7522 if (TREE_CODE (type) == FUNCTION_TYPE
7523 && type_quals != TYPE_UNQUALIFIED)
7525 /* This was an error in C++98 (cv-qualifiers cannot be added to
7526 a function type), but DR 295 makes the code well-formed by
7527 dropping the extra qualifiers. */
7530 tree bad_type = build_qualified_type (type, type_quals);
7531 pedwarn ("ignoring %qV qualifiers added to function type %qT",
7534 type_quals = TYPE_UNQUALIFIED;
7536 type_quals |= cp_type_quals (type);
7537 type = cp_build_qualified_type_real
7538 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
7539 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
7540 /* We might have ignored or rejected some of the qualifiers. */
7541 type_quals = cp_type_quals (type);
7544 inlinep = !! declspecs->specs[(int)ds_inline];
7545 virtualp = !! declspecs->specs[(int)ds_virtual];
7546 explicitp = !! declspecs->specs[(int)ds_explicit];
7548 storage_class = declspecs->storage_class;
7549 if (storage_class == sc_static)
7550 staticp = 1 + (decl_context == FIELD);
7552 if (virtualp && staticp == 2)
7554 error ("member %qD cannot be declared both virtual and static", dname);
7555 storage_class = sc_none;
7558 friendp = !! declspecs->specs[(int)ds_friend];
7560 if (dependent_name && !friendp)
7562 error ("%<%T::%D%> is not a valid declarator", ctype, dependent_name);
7563 return error_mark_node;
7566 /* Issue errors about use of storage classes for parameters. */
7567 if (decl_context == PARM)
7569 if (declspecs->specs[(int)ds_typedef])
7571 error ("typedef declaration invalid in parameter declaration");
7572 return error_mark_node;
7574 else if (storage_class == sc_static
7575 || storage_class == sc_extern
7577 error ("storage class specifiers invalid in parameter declarations");
7580 /* Give error if `virtual' is used outside of class declaration. */
7582 && (current_class_name == NULL_TREE || decl_context != FIELD))
7584 error ("virtual outside class declaration");
7588 /* Static anonymous unions are dealt with here. */
7589 if (staticp && decl_context == TYPENAME
7591 && ANON_AGGR_TYPE_P (declspecs->type))
7592 decl_context = FIELD;
7594 /* Warn about storage classes that are invalid for certain
7595 kinds of declarations (parameters, typenames, etc.). */
7598 && storage_class != sc_extern
7599 && storage_class != sc_static)
7600 || declspecs->specs[(int)ds_typedef]))
7602 error ("multiple storage classes in declaration of %qs", name);
7605 if (declspecs->conflicting_specifiers_p)
7607 error ("conflicting specifiers in declaration of %qs", name);
7608 storage_class = sc_none;
7610 else if (decl_context != NORMAL
7611 && ((storage_class != sc_none
7612 && storage_class != sc_mutable)
7615 if ((decl_context == PARM || decl_context == CATCHPARM)
7616 && (storage_class == sc_register
7617 || storage_class == sc_auto))
7619 else if (declspecs->specs[(int)ds_typedef])
7621 else if (decl_context == FIELD
7622 /* C++ allows static class elements. */
7623 && storage_class == sc_static)
7624 /* C++ also allows inlines and signed and unsigned elements,
7625 but in those cases we don't come in here. */
7629 if (decl_context == FIELD)
7630 error ("storage class specified for %qs", name);
7633 if (decl_context == PARM || decl_context == CATCHPARM)
7634 error ("storage class specified for parameter %qs", name);
7636 error ("storage class specified for typename");
7638 if (storage_class == sc_register
7639 || storage_class == sc_auto
7640 || storage_class == sc_extern
7642 storage_class = sc_none;
7645 else if (storage_class == sc_extern && initialized
7648 if (toplevel_bindings_p ())
7650 /* It's common practice (and completely valid) to have a const
7651 be initialized and declared extern. */
7652 if (!(type_quals & TYPE_QUAL_CONST))
7653 warning (0, "%qs initialized and declared %<extern%>", name);
7656 error ("%qs has both %<extern%> and initializer", name);
7658 else if (storage_class == sc_extern && funcdef_flag
7659 && ! toplevel_bindings_p ())
7660 error ("nested function %qs declared %<extern%>", name);
7661 else if (toplevel_bindings_p ())
7663 if (storage_class == sc_auto)
7664 error ("top-level declaration of %qs specifies %<auto%>", name);
7667 && storage_class != sc_extern
7668 && storage_class != sc_static)
7670 error ("function-scope %qs implicitly auto and declared %<__thread%>",
7675 if (storage_class && friendp)
7676 error ("storage class specifiers invalid in friend function declarations");
7679 unqualified_id = NULL_TREE;
7682 unqualified_id = id_declarator->u.id.unqualified_name;
7683 switch (TREE_CODE (unqualified_id))
7687 = constructor_name (TREE_OPERAND (unqualified_id, 0));
7690 case IDENTIFIER_NODE:
7691 case TEMPLATE_ID_EXPR:
7699 /* Determine the type of the entity declared by recurring on the
7701 for (; declarator; declarator = declarator->declarator)
7703 const cp_declarator *inner_declarator;
7706 if (type == error_mark_node)
7707 return error_mark_node;
7709 attrs = declarator->attributes;
7715 if (declarator == NULL || declarator->kind == cdk_id)
7716 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7717 if (declarator->kind == cdk_function)
7718 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7719 if (declarator->kind == cdk_array)
7720 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7721 returned_attrs = decl_attributes (&type,
7722 chainon (returned_attrs, attrs),
7726 if (declarator->kind == cdk_id)
7729 inner_declarator = declarator->declarator;
7731 switch (declarator->kind)
7734 type = create_array_type_for_decl (dname, type,
7735 declarator->u.array.bounds);
7743 /* Declaring a function type.
7744 Make sure we have a valid type for the function to return. */
7746 /* We now know that the TYPE_QUALS don't apply to the
7747 decl, but to its return type. */
7748 type_quals = TYPE_UNQUALIFIED;
7750 /* Warn about some types functions can't return. */
7752 if (TREE_CODE (type) == FUNCTION_TYPE)
7754 error ("%qs declared as function returning a function", name);
7755 type = integer_type_node;
7757 if (TREE_CODE (type) == ARRAY_TYPE)
7759 error ("%qs declared as function returning an array", name);
7760 type = integer_type_node;
7763 /* Pick up type qualifiers which should be applied to `this'. */
7764 memfn_quals = declarator->u.function.qualifiers;
7766 /* Pick up the exception specifications. */
7767 raises = declarator->u.function.exception_specification;
7769 /* Say it's a definition only for the CALL_EXPR
7770 closest to the identifier. */
7771 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
7773 if (ctype == NULL_TREE
7774 && decl_context == FIELD
7776 && (friendp == 0 || dname == current_class_name))
7777 ctype = current_class_type;
7779 if (ctype && (sfk == sfk_constructor
7780 || sfk == sfk_destructor))
7782 /* We are within a class's scope. If our declarator name
7783 is the same as the class name, and we are defining
7784 a function, then it is a constructor/destructor, and
7785 therefore returns a void type. */
7787 /* ISO C++ 12.4/2. A destructor may not be declared
7788 const or volatile. A destructor may not be
7791 ISO C++ 12.1. A constructor may not be declared
7792 const or volatile. A constructor may not be
7793 virtual. A constructor may not be static. */
7795 error ((flags == DTOR_FLAG)
7796 ? "destructor cannot be static member function"
7797 : "constructor cannot be static member function");
7800 error ((flags == DTOR_FLAG)
7801 ? "destructors may not be cv-qualified"
7802 : "constructors may not be cv-qualified");
7803 memfn_quals = TYPE_UNQUALIFIED;
7806 if (decl_context == FIELD
7807 && !member_function_or_else (ctype,
7810 return error_mark_node;
7812 if (flags != DTOR_FLAG)
7814 /* It's a constructor. */
7819 pedwarn ("constructors cannot be declared virtual");
7822 if (decl_context == FIELD
7823 && sfk != sfk_constructor)
7824 return error_mark_node;
7826 if (decl_context == FIELD)
7832 error ("can't initialize friend function %qs", name);
7835 /* Cannot be both friend and virtual. */
7836 error ("virtual functions cannot be friends");
7839 if (decl_context == NORMAL)
7840 error ("friend declaration not in class definition");
7841 if (current_function_decl && funcdef_flag)
7842 error ("can't define friend function %qs in a local "
7847 arg_types = grokparms (declarator->u.function.parameters,
7850 if (inner_declarator
7851 && inner_declarator->kind == cdk_id
7852 && inner_declarator->u.id.sfk == sfk_destructor
7853 && arg_types != void_list_node)
7855 error ("destructors may not have parameters");
7856 arg_types = void_list_node;
7860 type = build_function_type (type, arg_types);
7867 /* Filter out pointers-to-references and references-to-references.
7868 We can get these if a TYPE_DECL is used. */
7870 if (TREE_CODE (type) == REFERENCE_TYPE)
7872 error (declarator->kind == cdk_reference
7873 ? "cannot declare reference to %q#T"
7874 : "cannot declare pointer to %q#T", type);
7875 type = TREE_TYPE (type);
7877 else if (VOID_TYPE_P (type))
7879 if (declarator->kind == cdk_reference)
7880 error ("cannot declare reference to %q#T", type);
7881 else if (declarator->kind == cdk_ptrmem)
7882 error ("cannot declare pointer to %q#T member", type);
7885 /* We now know that the TYPE_QUALS don't apply to the decl,
7886 but to the target of the pointer. */
7887 type_quals = TYPE_UNQUALIFIED;
7889 if (declarator->kind == cdk_ptrmem
7890 && (TREE_CODE (type) == FUNCTION_TYPE || memfn_quals))
7892 memfn_quals |= cp_type_quals (type);
7893 type = build_memfn_type (type,
7894 declarator->u.pointer.class_type,
7896 memfn_quals = TYPE_UNQUALIFIED;
7899 if (declarator->kind == cdk_reference)
7901 if (!VOID_TYPE_P (type))
7902 type = build_reference_type (type);
7904 else if (TREE_CODE (type) == METHOD_TYPE)
7905 type = build_ptrmemfunc_type (build_pointer_type (type));
7906 else if (declarator->kind == cdk_ptrmem)
7908 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
7910 if (declarator->u.pointer.class_type == error_mark_node)
7911 /* We will already have complained. */
7912 type = error_mark_node;
7914 type = build_ptrmem_type (declarator->u.pointer.class_type,
7918 type = build_pointer_type (type);
7920 /* Process a list of type modifier keywords (such as
7921 const or volatile) that were given inside the `*' or `&'. */
7923 if (declarator->u.pointer.qualifiers)
7926 = cp_build_qualified_type (type,
7927 declarator->u.pointer.qualifiers);
7928 type_quals = cp_type_quals (type);
7941 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
7942 && TREE_CODE (type) != FUNCTION_TYPE
7943 && TREE_CODE (type) != METHOD_TYPE)
7945 error ("template-id %qD used as a declarator",
7947 unqualified_id = dname;
7950 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
7951 qualified with a class-name, turn it into a METHOD_TYPE, unless
7952 we know that the function is static. We take advantage of this
7953 opportunity to do other processing that pertains to entities
7954 explicitly declared to be class members. Note that if DECLARATOR
7955 is non-NULL, we know it is a cdk_id declarator; otherwise, we
7956 would not have exited the loop above. */
7958 && declarator->u.id.qualifying_scope
7959 && TYPE_P (declarator->u.id.qualifying_scope))
7963 ctype = declarator->u.id.qualifying_scope;
7964 ctype = TYPE_MAIN_VARIANT (ctype);
7966 while (t != NULL_TREE && CLASS_TYPE_P (t))
7968 /* You're supposed to have one `template <...>' for every
7969 template class, but you don't need one for a full
7970 specialization. For example:
7972 template <class T> struct S{};
7973 template <> struct S<int> { void f(); };
7974 void S<int>::f () {}
7976 is correct; there shouldn't be a `template <>' for the
7977 definition of `S<int>::f'. */
7978 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)
7979 && !any_dependent_template_arguments_p (CLASSTYPE_TI_ARGS (t)))
7980 /* T is an explicit (not partial) specialization. All
7981 containing classes must therefore also be explicitly
7984 if ((CLASSTYPE_USE_TEMPLATE (t) || CLASSTYPE_IS_TEMPLATE (t))
7985 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
7986 template_count += 1;
7988 t = TYPE_MAIN_DECL (t);
7989 t = DECL_CONTEXT (t);
7992 if (ctype == current_class_type)
7995 pedwarn ("member functions are implicitly friends of their class");
7997 pedwarn ("extra qualification %<%T::%> on member %qs",
8000 else if (/* If the qualifying type is already complete, then we
8001 can skip the following checks. */
8002 !COMPLETE_TYPE_P (ctype)
8003 && (/* If the function is being defined, then
8004 qualifying type must certainly be complete. */
8006 /* A friend declaration of "T::f" is OK, even if
8007 "T" is a template parameter. But, if this
8008 function is not a friend, the qualifying type
8010 || (!friendp && !CLASS_TYPE_P (ctype))
8011 /* For a declaration, the type need not be
8012 complete, if either it is dependent (since there
8013 is no meaningful definition of complete in that
8014 case) or the qualifying class is currently being
8016 || !(dependent_type_p (ctype)
8017 || currently_open_class (ctype)))
8018 /* Check that the qualifying type is complete. */
8019 && !complete_type_or_else (ctype, NULL_TREE))
8020 return error_mark_node;
8021 else if (TREE_CODE (type) == FUNCTION_TYPE)
8023 tree sname = declarator->u.id.unqualified_name;
8025 if (current_class_type
8026 && (!friendp || funcdef_flag))
8029 ? "cannot define member function %<%T::%s%> within %<%T%>"
8030 : "cannot declare member function %<%T::%s%> within %<%T%>",
8031 ctype, name, current_class_type);
8032 return error_mark_node;
8035 if (TREE_CODE (sname) == IDENTIFIER_NODE
8036 && NEW_DELETE_OPNAME_P (sname))
8037 /* Overloaded operator new and operator delete
8038 are always static functions. */
8041 type = build_memfn_type (type, ctype, memfn_quals);
8043 else if (declspecs->specs[(int)ds_typedef]
8044 && current_class_type)
8046 error ("cannot declare member %<%T::%s%> within %qT",
8047 ctype, name, current_class_type);
8048 return error_mark_node;
8052 /* Now TYPE has the actual type. */
8057 *attrlist = chainon (returned_attrs, *attrlist);
8059 attrlist = &returned_attrs;
8062 /* Did array size calculations overflow? */
8064 if (TREE_CODE (type) == ARRAY_TYPE
8065 && COMPLETE_TYPE_P (type)
8066 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
8067 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
8069 error ("size of array %qs is too large", name);
8070 /* If we proceed with the array type as it is, we'll eventually
8071 crash in tree_low_cst(). */
8072 type = error_mark_node;
8075 if ((decl_context == FIELD || decl_context == PARM)
8076 && !processing_template_decl
8077 && variably_modified_type_p (type, NULL_TREE))
8079 if (decl_context == FIELD)
8080 error ("data member may not have variably modified type %qT", type);
8082 error ("parameter may not have variably modified type %qT", type);
8083 type = error_mark_node;
8086 if (explicitp == 1 || (explicitp && friendp))
8088 /* [dcl.fct.spec] The explicit specifier shall only be used in
8089 declarations of constructors within a class definition. */
8090 error ("only declarations of constructors can be %<explicit%>");
8094 if (storage_class == sc_mutable)
8096 if (decl_context != FIELD || friendp)
8098 error ("non-member %qs cannot be declared %<mutable%>", name);
8099 storage_class = sc_none;
8101 else if (decl_context == TYPENAME || declspecs->specs[(int)ds_typedef])
8103 error ("non-object member %qs cannot be declared %<mutable%>", name);
8104 storage_class = sc_none;
8106 else if (TREE_CODE (type) == FUNCTION_TYPE
8107 || TREE_CODE (type) == METHOD_TYPE)
8109 error ("function %qs cannot be declared %<mutable%>", name);
8110 storage_class = sc_none;
8114 error ("static %qs cannot be declared %<mutable%>", name);
8115 storage_class = sc_none;
8117 else if (type_quals & TYPE_QUAL_CONST)
8119 error ("const %qs cannot be declared %<mutable%>", name);
8120 storage_class = sc_none;
8124 /* If this is declaring a typedef name, return a TYPE_DECL. */
8125 if (declspecs->specs[(int)ds_typedef] && decl_context != TYPENAME)
8129 /* Note that the grammar rejects storage classes
8130 in typenames, fields or parameters. */
8131 if (current_lang_name == lang_name_java)
8132 TYPE_FOR_JAVA (type) = 1;
8134 /* This declaration:
8136 typedef void f(int) const;
8138 declares a function type which is not a member of any
8139 particular class, but which is cv-qualified; for
8140 example "f S::*" declares a pointer to a const-qualified
8141 member function of S. We record the cv-qualification in the
8143 if (memfn_quals && TREE_CODE (type) == FUNCTION_TYPE)
8144 type = cp_build_qualified_type (type, memfn_quals);
8146 if (decl_context == FIELD)
8147 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
8149 decl = build_decl (TYPE_DECL, unqualified_id, type);
8150 if (id_declarator && declarator->u.id.qualifying_scope)
8151 error ("%Jtypedef name may not be a nested-name-specifier", decl);
8153 if (decl_context != FIELD)
8155 if (!current_function_decl)
8156 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8157 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
8158 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
8159 (current_function_decl)))
8160 /* The TYPE_DECL is "abstract" because there will be
8161 clones of this constructor/destructor, and there will
8162 be copies of this TYPE_DECL generated in those
8164 DECL_ABSTRACT (decl) = 1;
8166 else if (constructor_name_p (unqualified_id, current_class_type))
8167 pedwarn ("ISO C++ forbids nested type %qD with same name "
8168 "as enclosing class",
8171 /* If the user declares "typedef struct {...} foo" then the
8172 struct will have an anonymous name. Fill that name in now.
8173 Nothing can refer to it, so nothing needs know about the name
8175 if (type != error_mark_node
8178 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8179 && TYPE_ANONYMOUS_P (type)
8180 /* Don't do this if there are attributes. */
8181 && (!attrlist || !*attrlist)
8182 && cp_type_quals (type) == TYPE_UNQUALIFIED)
8184 tree oldname = TYPE_NAME (type);
8187 /* Replace the anonymous name with the real name everywhere. */
8188 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8189 if (TYPE_NAME (t) == oldname)
8190 TYPE_NAME (t) = decl;
8192 if (TYPE_LANG_SPECIFIC (type))
8193 TYPE_WAS_ANONYMOUS (type) = 1;
8195 /* If this is a typedef within a template class, the nested
8196 type is a (non-primary) template. The name for the
8197 template needs updating as well. */
8198 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
8199 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
8200 = TYPE_IDENTIFIER (type);
8202 /* FIXME remangle member functions; member functions of a
8203 type with external linkage have external linkage. */
8206 /* Any qualifiers on a function type typedef have already been
8208 if (memfn_quals && !ctype && TREE_CODE (type) == FUNCTION_TYPE)
8209 memfn_quals = TYPE_UNQUALIFIED;
8212 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
8213 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
8215 bad_specifiers (decl, "type", virtualp,
8216 memfn_quals != TYPE_UNQUALIFIED,
8217 inlinep, friendp, raises != NULL_TREE);
8222 /* Detect the case of an array type of unspecified size
8223 which came, as such, direct from a typedef name.
8224 We must copy the type, so that the array's domain can be
8225 individually set by the object's initializer. */
8227 if (type && typedef_type
8228 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
8229 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
8230 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
8232 /* Detect where we're using a typedef of function type to declare a
8233 function. PARMS will not be set, so we must create it now. */
8235 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
8237 tree decls = NULL_TREE;
8240 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
8242 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
8244 TREE_CHAIN (decl) = decls;
8248 parms = nreverse (decls);
8250 if (decl_context != TYPENAME)
8252 /* A cv-qualifier-seq shall only be part of the function type
8253 for a non-static member function. [8.3.5/4 dcl.fct] */
8254 if (cp_type_quals (type) != TYPE_UNQUALIFIED
8255 && (current_class_type == NULL_TREE || staticp) )
8257 error ("qualified function types cannot be used to declare %s functions",
8258 (staticp? "static member" : "free"));
8259 type = TYPE_MAIN_VARIANT (type);
8262 /* The qualifiers on the function type become the qualifiers on
8263 the non-static member function. */
8264 memfn_quals |= cp_type_quals (type);
8268 /* If this is a type name (such as, in a cast or sizeof),
8269 compute the type and return it now. */
8271 if (decl_context == TYPENAME)
8273 /* Note that the grammar rejects storage classes
8274 in typenames, fields or parameters. */
8275 if (type_quals != TYPE_UNQUALIFIED)
8276 type_quals = TYPE_UNQUALIFIED;
8278 /* Special case: "friend class foo" looks like a TYPENAME context. */
8281 if (type_quals != TYPE_UNQUALIFIED)
8283 error ("type qualifiers specified for friend class declaration");
8284 type_quals = TYPE_UNQUALIFIED;
8288 error ("%<inline%> specified for friend class declaration");
8294 /* Don't allow friend declaration without a class-key. */
8295 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
8296 pedwarn ("template parameters cannot be friends");
8297 else if (TREE_CODE (type) == TYPENAME_TYPE)
8298 pedwarn ("friend declaration requires class-key, "
8299 "i.e. %<friend class %T::%D%>",
8300 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
8302 pedwarn ("friend declaration requires class-key, "
8303 "i.e. %<friend %#T%>",
8307 /* Only try to do this stuff if we didn't already give up. */
8308 if (type != integer_type_node)
8310 /* A friendly class? */
8311 if (current_class_type)
8312 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
8315 error ("trying to make class %qT a friend of global scope",
8318 type = void_type_node;
8321 else if (memfn_quals)
8323 if (ctype == NULL_TREE)
8325 if (TREE_CODE (type) != METHOD_TYPE)
8326 error ("invalid qualifiers on non-member function type");
8328 ctype = TYPE_METHOD_BASETYPE (type);
8331 type = build_memfn_type (type, ctype, memfn_quals);
8336 else if (unqualified_id == NULL_TREE && decl_context != PARM
8337 && decl_context != CATCHPARM
8338 && TREE_CODE (type) != UNION_TYPE
8341 error ("abstract declarator %qT used as declaration", type);
8342 return error_mark_node;
8345 /* Only functions may be declared using an operator-function-id. */
8347 && IDENTIFIER_OPNAME_P (unqualified_id)
8348 && TREE_CODE (type) != FUNCTION_TYPE
8349 && TREE_CODE (type) != METHOD_TYPE)
8351 error ("declaration of %qD as non-function", unqualified_id);
8352 return error_mark_node;
8355 /* We don't check parameter types here because we can emit a better
8356 error message later. */
8357 if (decl_context != PARM)
8359 type = check_var_type (unqualified_id, type);
8360 if (type == error_mark_node)
8361 return error_mark_node;
8364 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
8365 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
8367 if (decl_context == PARM || decl_context == CATCHPARM)
8369 if (ctype || in_namespace)
8370 error ("cannot use %<::%> in parameter declaration");
8372 /* A parameter declared as an array of T is really a pointer to T.
8373 One declared as a function is really a pointer to a function.
8374 One declared as a member is really a pointer to member. */
8376 if (TREE_CODE (type) == ARRAY_TYPE)
8378 /* Transfer const-ness of array into that of type pointed to. */
8379 type = build_pointer_type (TREE_TYPE (type));
8380 type_quals = TYPE_UNQUALIFIED;
8382 else if (TREE_CODE (type) == FUNCTION_TYPE)
8383 type = build_pointer_type (type);
8389 if (decl_context == PARM)
8391 decl = cp_build_parm_decl (unqualified_id, type);
8393 bad_specifiers (decl, "parameter", virtualp,
8394 memfn_quals != TYPE_UNQUALIFIED,
8395 inlinep, friendp, raises != NULL_TREE);
8397 else if (decl_context == FIELD)
8399 /* The C99 flexible array extension. */
8400 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
8401 && TYPE_DOMAIN (type) == NULL_TREE)
8403 tree itype = compute_array_index_type (dname, integer_zero_node);
8404 type = build_cplus_array_type (TREE_TYPE (type), itype);
8407 if (type == error_mark_node)
8409 /* Happens when declaring arrays of sizes which
8410 are error_mark_node, for example. */
8413 else if (in_namespace && !friendp)
8415 /* Something like struct S { int N::j; }; */
8416 error ("invalid use of %<::%>");
8417 return error_mark_node;
8419 else if (TREE_CODE (type) == FUNCTION_TYPE)
8422 tree function_context;
8426 if (ctype == NULL_TREE)
8427 ctype = current_class_type;
8429 if (ctype == NULL_TREE)
8431 error ("can't make %qD into a method -- not in a class",
8433 return error_mark_node;
8436 /* ``A union may [ ... ] not [ have ] virtual functions.''
8438 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
8440 error ("function %qD declared virtual inside a union",
8442 return error_mark_node;
8445 if (NEW_DELETE_OPNAME_P (unqualified_id))
8449 error ("%qD cannot be declared virtual, since it "
8455 else if (staticp < 2)
8456 type = build_memfn_type (type, ctype, memfn_quals);
8459 /* Check that the name used for a destructor makes sense. */
8460 if (sfk == sfk_destructor)
8464 gcc_assert (friendp);
8465 error ("expected qualified name in friend declaration "
8466 "for destructor %qD",
8467 id_declarator->u.id.unqualified_name);
8468 return error_mark_node;
8471 if (!same_type_p (TREE_OPERAND
8472 (id_declarator->u.id.unqualified_name, 0),
8475 error ("declaration of %qD as member of %qT",
8476 id_declarator->u.id.unqualified_name, ctype);
8477 return error_mark_node;
8481 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
8482 function_context = (ctype != NULL_TREE) ?
8483 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
8484 publicp = (! friendp || ! staticp)
8485 && function_context == NULL_TREE;
8486 decl = grokfndecl (ctype, type,
8487 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
8488 ? unqualified_id : dname,
8491 virtualp, flags, memfn_quals, raises,
8492 friendp ? -1 : 0, friendp, publicp, inlinep,
8494 funcdef_flag, template_count, in_namespace, attrlist);
8495 if (decl == NULL_TREE)
8496 return error_mark_node;
8498 /* This clobbers the attrs stored in `decl' from `attrlist'. */
8499 /* The decl and setting of decl_attr is also turned off. */
8500 decl = build_decl_attribute_variant (decl, decl_attr);
8503 /* [class.conv.ctor]
8505 A constructor declared without the function-specifier
8506 explicit that can be called with a single parameter
8507 specifies a conversion from the type of its first
8508 parameter to the type of its class. Such a constructor
8509 is called a converting constructor. */
8511 DECL_NONCONVERTING_P (decl) = 1;
8512 else if (DECL_CONSTRUCTOR_P (decl))
8514 /* The constructor can be called with exactly one
8515 parameter if there is at least one parameter, and
8516 any subsequent parameters have default arguments.
8517 Ignore any compiler-added parms. */
8518 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
8520 if (arg_types == void_list_node
8522 && TREE_CHAIN (arg_types)
8523 && TREE_CHAIN (arg_types) != void_list_node
8524 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
8525 DECL_NONCONVERTING_P (decl) = 1;
8528 else if (TREE_CODE (type) == METHOD_TYPE)
8530 /* We only get here for friend declarations of
8531 members of other classes. */
8532 /* All method decls are public, so tell grokfndecl to set
8533 TREE_PUBLIC, also. */
8534 decl = grokfndecl (ctype, type,
8535 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
8536 ? unqualified_id : dname,
8539 virtualp, flags, memfn_quals, raises,
8540 friendp ? -1 : 0, friendp, 1, 0, sfk,
8541 funcdef_flag, template_count, in_namespace,
8543 if (decl == NULL_TREE)
8544 return error_mark_node;
8546 else if (!staticp && !dependent_type_p (type)
8547 && !COMPLETE_TYPE_P (complete_type (type))
8548 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
8551 error ("field %qD has incomplete type", unqualified_id);
8553 error ("name %qT has incomplete type", type);
8555 /* If we're instantiating a template, tell them which
8556 instantiation made the field's type be incomplete. */
8557 if (current_class_type
8558 && TYPE_NAME (current_class_type)
8559 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8561 && declspecs->type == type)
8562 error (" in instantiation of template %qT",
8563 current_class_type);
8565 return error_mark_node;
8571 error ("%qE is neither function nor member function; "
8572 "cannot be declared friend", unqualified_id);
8580 /* Friends are treated specially. */
8581 if (ctype == current_class_type)
8582 ; /* We already issued a pedwarn. */
8583 else if (decl && DECL_NAME (decl))
8585 if (template_class_depth (current_class_type) == 0)
8587 decl = check_explicit_specialization
8588 (unqualified_id, decl, template_count,
8589 2 * funcdef_flag + 4);
8590 if (decl == error_mark_node)
8591 return error_mark_node;
8594 decl = do_friend (ctype, unqualified_id, decl,
8600 return error_mark_node;
8603 /* Structure field. It may not be a function, except for C++. */
8605 if (decl == NULL_TREE)
8611 /* An attempt is being made to initialize a non-static
8612 member. But, from [class.mem]:
8614 4 A member-declarator can contain a
8615 constant-initializer only if it declares a static
8616 member (_class.static_) of integral or enumeration
8617 type, see _class.static.data_.
8619 This used to be relatively common practice, but
8620 the rest of the compiler does not correctly
8621 handle the initialization unless the member is
8622 static so we make it static below. */
8623 pedwarn ("ISO C++ forbids initialization of member %qD",
8625 pedwarn ("making %qD static", unqualified_id);
8629 if (uses_template_parms (type))
8630 /* We'll check at instantiation time. */
8632 else if (check_static_variable_definition (unqualified_id,
8634 /* If we just return the declaration, crashes
8635 will sometimes occur. We therefore return
8636 void_type_node, as if this was a friend
8637 declaration, to cause callers to completely
8638 ignore this declaration. */
8639 return error_mark_node;
8644 /* C++ allows static class members. All other work
8645 for this is done by grokfield. */
8646 decl = build_lang_decl (VAR_DECL, unqualified_id, type);
8647 set_linkage_for_static_data_member (decl);
8648 /* Even if there is an in-class initialization, DECL
8649 is considered undefined until an out-of-class
8650 definition is provided. */
8651 DECL_EXTERNAL (decl) = 1;
8655 if (targetm.have_tls)
8656 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
8658 /* A mere warning is sure to result in improper
8659 semantics at runtime. Don't bother to allow this to
8661 error ("thread-local storage not supported for this target");
8666 decl = build_decl (FIELD_DECL, unqualified_id, type);
8667 DECL_NONADDRESSABLE_P (decl) = bitfield;
8668 if (storage_class == sc_mutable)
8670 DECL_MUTABLE_P (decl) = 1;
8671 storage_class = sc_none;
8675 bad_specifiers (decl, "field", virtualp,
8676 memfn_quals != TYPE_UNQUALIFIED,
8677 inlinep, friendp, raises != NULL_TREE);
8680 else if (TREE_CODE (type) == FUNCTION_TYPE
8681 || TREE_CODE (type) == METHOD_TYPE)
8686 if (!unqualified_id)
8687 return error_mark_node;
8689 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
8690 original_name = dname;
8692 original_name = unqualified_id;
8694 if (storage_class == sc_auto)
8695 error ("storage class %<auto%> invalid for function %qs", name);
8696 else if (storage_class == sc_register)
8697 error ("storage class %<register%> invalid for function %qs", name);
8699 error ("storage class %<__thread%> invalid for function %qs", name);
8701 /* Function declaration not at top level.
8702 Storage classes other than `extern' are not allowed
8703 and `extern' makes no difference. */
8704 if (! toplevel_bindings_p ()
8705 && (storage_class == sc_static
8706 || declspecs->specs[(int)ds_inline])
8709 if (storage_class == sc_static)
8710 pedwarn ("%<static%> specified invalid for function %qs "
8711 "declared out of global scope", name);
8713 pedwarn ("%<inline%> specifier invalid for function %qs "
8714 "declared out of global scope", name);
8717 if (ctype == NULL_TREE)
8721 error ("virtual non-class function %qs", name);
8725 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
8726 && !NEW_DELETE_OPNAME_P (original_name))
8727 type = build_method_type_directly (ctype,
8729 TYPE_ARG_TYPES (type));
8731 /* Record presence of `static'. */
8732 publicp = (ctype != NULL_TREE
8733 || storage_class == sc_extern
8734 || storage_class != sc_static);
8736 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
8737 virtualp, flags, memfn_quals, raises,
8739 publicp, inlinep, sfk, funcdef_flag,
8740 template_count, in_namespace, attrlist);
8741 if (decl == NULL_TREE)
8742 return error_mark_node;
8746 int invalid_static = 0;
8748 /* Don't allow a static member function in a class, and forbid
8749 declaring main to be static. */
8750 if (TREE_CODE (type) == METHOD_TYPE)
8752 pedwarn ("cannot declare member function %qD to have "
8753 "static linkage", decl);
8756 else if (current_function_decl)
8758 /* FIXME need arm citation */
8759 error ("cannot declare static function inside another function");
8766 storage_class = sc_none;
8772 /* It's a variable. */
8774 /* An uninitialized decl with `extern' is a reference. */
8775 decl = grokvardecl (type, unqualified_id,
8778 (type_quals & TYPE_QUAL_CONST) != 0,
8779 ctype ? ctype : in_namespace);
8780 bad_specifiers (decl, "variable", virtualp,
8781 memfn_quals != TYPE_UNQUALIFIED,
8782 inlinep, friendp, raises != NULL_TREE);
8786 DECL_CONTEXT (decl) = ctype;
8789 pedwarn ("%<static%> may not be used when defining "
8790 "(as opposed to declaring) a static data member");
8792 storage_class = sc_none;
8794 if (storage_class == sc_register && TREE_STATIC (decl))
8796 error ("static member %qD declared %<register%>", decl);
8797 storage_class = sc_none;
8799 if (storage_class == sc_extern && pedantic)
8801 pedwarn ("cannot explicitly declare member %q#D to have "
8804 storage_class = sc_none;
8809 /* Record `register' declaration for warnings on &
8810 and in case doing stupid register allocation. */
8812 if (storage_class == sc_register)
8813 DECL_REGISTER (decl) = 1;
8814 else if (storage_class == sc_extern)
8815 DECL_THIS_EXTERN (decl) = 1;
8816 else if (storage_class == sc_static)
8817 DECL_THIS_STATIC (decl) = 1;
8819 /* Record constancy and volatility. There's no need to do this
8820 when processing a template; we'll do this for the instantiated
8821 declaration based on the type of DECL. */
8822 if (!processing_template_decl)
8823 cp_apply_type_quals_to_decl (type_quals, decl);
8829 /* Subroutine of start_function. Ensure that each of the parameter
8830 types (as listed in PARMS) is complete, as is required for a
8831 function definition. */
8834 require_complete_types_for_parms (tree parms)
8836 for (; parms; parms = TREE_CHAIN (parms))
8838 if (dependent_type_p (TREE_TYPE (parms)))
8840 if (!VOID_TYPE_P (TREE_TYPE (parms))
8841 && complete_type_or_else (TREE_TYPE (parms), parms))
8843 relayout_decl (parms);
8844 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
8847 /* grokparms or complete_type_or_else will have already issued
8849 TREE_TYPE (parms) = error_mark_node;
8853 /* Returns nonzero if T is a local variable. */
8856 local_variable_p (tree t)
8858 if ((TREE_CODE (t) == VAR_DECL
8859 /* A VAR_DECL with a context that is a _TYPE is a static data
8861 && !TYPE_P (CP_DECL_CONTEXT (t))
8862 /* Any other non-local variable must be at namespace scope. */
8863 && !DECL_NAMESPACE_SCOPE_P (t))
8864 || (TREE_CODE (t) == PARM_DECL))
8870 /* Returns nonzero if T is an automatic local variable or a label.
8871 (These are the declarations that need to be remapped when the code
8872 containing them is duplicated.) */
8875 nonstatic_local_decl_p (tree t)
8877 return ((local_variable_p (t) && !TREE_STATIC (t))
8878 || TREE_CODE (t) == LABEL_DECL
8879 || TREE_CODE (t) == RESULT_DECL);
8882 /* Like local_variable_p, but suitable for use as a tree-walking
8886 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
8887 void *data ATTRIBUTE_UNUSED)
8889 if (local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
8891 else if (TYPE_P (*tp))
8898 /* Check that ARG, which is a default-argument expression for a
8899 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
8900 something goes wrong. DECL may also be a _TYPE node, rather than a
8901 DECL, if there is no DECL available. */
8904 check_default_argument (tree decl, tree arg)
8909 if (TREE_CODE (arg) == DEFAULT_ARG)
8910 /* We get a DEFAULT_ARG when looking at an in-class declaration
8911 with a default argument. Ignore the argument for now; we'll
8912 deal with it after the class is complete. */
8921 decl_type = TREE_TYPE (decl);
8923 if (arg == error_mark_node
8924 || decl == error_mark_node
8925 || TREE_TYPE (arg) == error_mark_node
8926 || decl_type == error_mark_node)
8927 /* Something already went wrong. There's no need to check
8929 return error_mark_node;
8931 /* [dcl.fct.default]
8933 A default argument expression is implicitly converted to the
8935 if (!TREE_TYPE (arg)
8936 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg, LOOKUP_NORMAL))
8939 error ("default argument for %q#D has type %qT",
8940 decl, TREE_TYPE (arg));
8942 error ("default argument for parameter of type %qT has type %qT",
8943 decl_type, TREE_TYPE (arg));
8945 return error_mark_node;
8948 /* [dcl.fct.default]
8950 Local variables shall not be used in default argument
8953 The keyword `this' shall not be used in a default argument of a
8955 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
8959 error ("default argument %qE uses local variable %qD", arg, var);
8960 return error_mark_node;
8967 /* Decode the list of parameter types for a function type.
8968 Given the list of things declared inside the parens,
8969 return a list of types.
8971 If this parameter does not end with an ellipsis, we append
8974 *PARMS is set to the chain of PARM_DECLs created. */
8977 grokparms (cp_parameter_declarator *first_parm, tree *parms)
8979 tree result = NULL_TREE;
8980 tree decls = NULL_TREE;
8981 int ellipsis = !first_parm || first_parm->ellipsis_p;
8982 cp_parameter_declarator *parm;
8985 for (parm = first_parm; parm != NULL; parm = parm->next)
8987 tree type = NULL_TREE;
8988 tree init = parm->default_argument;
8992 if (parm == no_parameters)
8995 attrs = parm->decl_specifiers.attributes;
8996 parm->decl_specifiers.attributes = NULL_TREE;
8997 decl = grokdeclarator (parm->declarator, &parm->decl_specifiers,
8998 PARM, init != NULL_TREE, &attrs);
8999 if (! decl || TREE_TYPE (decl) == error_mark_node)
9003 cplus_decl_attributes (&decl, attrs, 0);
9005 type = TREE_TYPE (decl);
9006 if (VOID_TYPE_P (type))
9008 if (same_type_p (type, void_type_node)
9009 && DECL_SELF_REFERENCE_P (type)
9010 && !DECL_NAME (decl) && !result && !parm->next && !ellipsis)
9011 /* this is a parmlist of `(void)', which is ok. */
9013 cxx_incomplete_type_error (decl, type);
9014 /* It's not a good idea to actually create parameters of
9015 type `void'; other parts of the compiler assume that a
9016 void type terminates the parameter list. */
9017 type = error_mark_node;
9018 TREE_TYPE (decl) = error_mark_node;
9021 if (type != error_mark_node)
9023 /* Top-level qualifiers on the parameters are
9024 ignored for function types. */
9025 type = cp_build_qualified_type (type, 0);
9026 if (TREE_CODE (type) == METHOD_TYPE)
9028 error ("parameter %qD invalidly declared method type", decl);
9029 type = build_pointer_type (type);
9030 TREE_TYPE (decl) = type;
9032 else if (abstract_virtuals_error (decl, type))
9033 any_error = 1; /* Seems like a good idea. */
9034 else if (POINTER_TYPE_P (type))
9036 /* [dcl.fct]/6, parameter types cannot contain pointers
9037 (references) to arrays of unknown bound. */
9038 tree t = TREE_TYPE (type);
9039 int ptr = TYPE_PTR_P (type);
9045 else if (TREE_CODE (t) != ARRAY_TYPE)
9047 else if (!TYPE_DOMAIN (t))
9051 if (TREE_CODE (t) == ARRAY_TYPE)
9052 error ("parameter %qD includes %s to array of unknown "
9054 decl, ptr ? "pointer" : "reference", t);
9059 else if (init && !processing_template_decl)
9060 init = check_default_argument (decl, init);
9063 TREE_CHAIN (decl) = decls;
9065 result = tree_cons (init, type, result);
9067 decls = nreverse (decls);
9068 result = nreverse (result);
9070 result = chainon (result, void_list_node);
9077 /* D is a constructor or overloaded `operator='.
9079 Let T be the class in which D is declared. Then, this function
9082 -1 if D's is an ill-formed constructor or copy assignment operator
9083 whose first parameter is of type `T'.
9084 0 if D is not a copy constructor or copy assignment
9086 1 if D is a copy constructor or copy assignment operator whose
9087 first parameter is a reference to const qualified T.
9088 2 if D is a copy constructor or copy assignment operator whose
9089 first parameter is a reference to non-const qualified T.
9091 This function can be used as a predicate. Positive values indicate
9092 a copy constructor and nonzero values indicate a copy assignment
9102 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
9104 if (TREE_CODE (d) == TEMPLATE_DECL
9105 || (DECL_TEMPLATE_INFO (d)
9106 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
9107 /* Instantiations of template member functions are never copy
9108 functions. Note that member functions of templated classes are
9109 represented as template functions internally, and we must
9110 accept those as copy functions. */
9113 args = FUNCTION_FIRST_USER_PARMTYPE (d);
9117 arg_type = TREE_VALUE (args);
9118 if (arg_type == error_mark_node)
9121 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
9123 /* Pass by value copy assignment operator. */
9126 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
9127 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
9129 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
9135 args = TREE_CHAIN (args);
9137 if (args && args != void_list_node && !TREE_PURPOSE (args))
9138 /* There are more non-optional args. */
9144 /* Remember any special properties of member function DECL. */
9146 void grok_special_member_properties (tree decl)
9150 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
9153 class_type = DECL_CONTEXT (decl);
9154 if (DECL_CONSTRUCTOR_P (decl))
9156 int ctor = copy_fn_p (decl);
9158 TYPE_HAS_CONSTRUCTOR (class_type) = 1;
9164 A non-template constructor for class X is a copy
9165 constructor if its first parameter is of type X&, const
9166 X&, volatile X& or const volatile X&, and either there
9167 are no other parameters or else all other parameters have
9168 default arguments. */
9169 TYPE_HAS_INIT_REF (class_type) = 1;
9171 TYPE_HAS_CONST_INIT_REF (class_type) = 1;
9173 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
9174 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
9176 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
9180 A non-template assignment operator for class X is a copy
9181 assignment operator if its parameter is of type X, X&, const
9182 X&, volatile X& or const volatile X&. */
9184 int assop = copy_fn_p (decl);
9188 TYPE_HAS_ASSIGN_REF (class_type) = 1;
9190 TYPE_HAS_CONST_ASSIGN_REF (class_type) = 1;
9195 /* Check a constructor DECL has the correct form. Complains
9196 if the class has a constructor of the form X(X). */
9199 grok_ctor_properties (tree ctype, tree decl)
9201 int ctor_parm = copy_fn_p (decl);
9207 A declaration of a constructor for a class X is ill-formed if
9208 its first parameter is of type (optionally cv-qualified) X
9209 and either there are no other parameters or else all other
9210 parameters have default arguments.
9212 We *don't* complain about member template instantiations that
9213 have this form, though; they can occur as we try to decide
9214 what constructor to use during overload resolution. Since
9215 overload resolution will never prefer such a constructor to
9216 the non-template copy constructor (which is either explicitly
9217 or implicitly defined), there's no need to worry about their
9218 existence. Theoretically, they should never even be
9219 instantiated, but that's hard to forestall. */
9220 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
9228 /* An operator with this code is unary, but can also be binary. */
9231 ambi_op_p (enum tree_code code)
9233 return (code == INDIRECT_REF
9234 || code == ADDR_EXPR
9235 || code == UNARY_PLUS_EXPR
9236 || code == NEGATE_EXPR
9237 || code == PREINCREMENT_EXPR
9238 || code == PREDECREMENT_EXPR);
9241 /* An operator with this name can only be unary. */
9244 unary_op_p (enum tree_code code)
9246 return (code == TRUTH_NOT_EXPR
9247 || code == BIT_NOT_EXPR
9248 || code == COMPONENT_REF
9249 || code == TYPE_EXPR);
9252 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
9253 errors are issued for invalid declarations. */
9256 grok_op_properties (tree decl, bool complain)
9258 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9260 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
9261 tree name = DECL_NAME (decl);
9262 enum tree_code operator_code;
9267 /* Count the number of arguments and check for ellipsis. */
9268 for (argtype = argtypes, arity = 0;
9269 argtype && argtype != void_list_node;
9270 argtype = TREE_CHAIN (argtype))
9272 ellipsis_p = !argtype;
9274 class_type = DECL_CONTEXT (decl);
9275 if (class_type && !CLASS_TYPE_P (class_type))
9276 class_type = NULL_TREE;
9278 if (DECL_CONV_FN_P (decl))
9279 operator_code = TYPE_EXPR;
9283 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
9284 if (ansi_opname (CODE) == name) \
9286 operator_code = (CODE); \
9289 else if (ansi_assopname (CODE) == name) \
9291 operator_code = (CODE); \
9292 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
9296 #include "operators.def"
9302 gcc_assert (operator_code != LAST_CPLUS_TREE_CODE);
9303 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
9306 switch (operator_code)
9309 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
9313 TYPE_GETS_DELETE (class_type) |= 1;
9317 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
9320 case VEC_DELETE_EXPR:
9321 TYPE_GETS_DELETE (class_type) |= 2;
9328 /* [basic.std.dynamic.allocation]/1:
9330 A program is ill-formed if an allocation function is declared
9331 in a namespace scope other than global scope or declared static
9334 The same also holds true for deallocation functions. */
9335 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
9336 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
9338 if (DECL_NAMESPACE_SCOPE_P (decl))
9340 if (CP_DECL_CONTEXT (decl) != global_namespace)
9342 error ("%qD may not be declared within a namespace", decl);
9345 else if (!TREE_PUBLIC (decl))
9347 error ("%qD may not be declared as static", decl);
9353 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
9354 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
9355 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
9356 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
9359 /* An operator function must either be a non-static member function
9360 or have at least one parameter of a class, a reference to a class,
9361 an enumeration, or a reference to an enumeration. 13.4.0.6 */
9362 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
9364 if (operator_code == TYPE_EXPR
9365 || operator_code == CALL_EXPR
9366 || operator_code == COMPONENT_REF
9367 || operator_code == ARRAY_REF
9368 || operator_code == NOP_EXPR)
9370 error ("%qD must be a nonstatic member function", decl);
9377 if (DECL_STATIC_FUNCTION_P (decl))
9379 error ("%qD must be either a non-static member "
9380 "function or a non-member function", decl);
9384 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
9386 tree arg = non_reference (TREE_VALUE (p));
9387 if (arg == error_mark_node)
9390 /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used
9391 because these checks are performed even on
9392 template functions. */
9393 if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE)
9397 if (!p || p == void_list_node)
9400 error ("%qD must have an argument of class or "
9401 "enumerated type", decl);
9407 /* There are no restrictions on the arguments to an overloaded
9409 if (operator_code == CALL_EXPR)
9412 /* Warn about conversion operators that will never be used. */
9413 if (IDENTIFIER_TYPENAME_P (name)
9414 && ! DECL_TEMPLATE_INFO (decl)
9416 /* Warn only declaring the function; there is no need to
9417 warn again about out-of-class definitions. */
9418 && class_type == current_class_type)
9420 tree t = TREE_TYPE (name);
9421 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
9422 const char *what = 0;
9425 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
9427 if (TREE_CODE (t) == VOID_TYPE)
9429 else if (class_type)
9431 if (t == class_type)
9432 what = "the same type";
9433 /* Don't force t to be complete here. */
9434 else if (IS_AGGR_TYPE (t)
9435 && COMPLETE_TYPE_P (t)
9436 && DERIVED_FROM_P (t, class_type))
9437 what = "a base class";
9441 warning (OPT_Wconversion, "conversion to %s%s will never use a type "
9442 "conversion operator",
9443 ref ? "a reference to " : "", what);
9446 if (operator_code == COND_EXPR)
9449 error ("ISO C++ prohibits overloading operator ?:");
9452 else if (ellipsis_p)
9454 error ("%qD must not have variable number of arguments", decl);
9457 else if (ambi_op_p (operator_code))
9460 /* We pick the one-argument operator codes by default, so
9461 we don't have to change anything. */
9463 else if (arity == 2)
9465 /* If we thought this was a unary operator, we now know
9466 it to be a binary operator. */
9467 switch (operator_code)
9470 operator_code = MULT_EXPR;
9474 operator_code = BIT_AND_EXPR;
9477 case UNARY_PLUS_EXPR:
9478 operator_code = PLUS_EXPR;
9482 operator_code = MINUS_EXPR;
9485 case PREINCREMENT_EXPR:
9486 operator_code = POSTINCREMENT_EXPR;
9489 case PREDECREMENT_EXPR:
9490 operator_code = POSTDECREMENT_EXPR;
9497 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
9499 if ((operator_code == POSTINCREMENT_EXPR
9500 || operator_code == POSTDECREMENT_EXPR)
9501 && ! processing_template_decl
9502 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
9505 error ("postfix %qD must take %<int%> as its argument",
9508 error ("postfix %qD must take %<int%> as its second "
9516 error ("%qD must take either zero or one argument", decl);
9518 error ("%qD must take either one or two arguments", decl);
9522 /* More Effective C++ rule 6. */
9524 && (operator_code == POSTINCREMENT_EXPR
9525 || operator_code == POSTDECREMENT_EXPR
9526 || operator_code == PREINCREMENT_EXPR
9527 || operator_code == PREDECREMENT_EXPR))
9529 tree arg = TREE_VALUE (argtypes);
9530 tree ret = TREE_TYPE (TREE_TYPE (decl));
9531 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
9532 arg = TREE_TYPE (arg);
9533 arg = TYPE_MAIN_VARIANT (arg);
9534 if (operator_code == PREINCREMENT_EXPR
9535 || operator_code == PREDECREMENT_EXPR)
9537 if (TREE_CODE (ret) != REFERENCE_TYPE
9538 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
9540 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
9541 build_reference_type (arg));
9545 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
9546 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
9550 else if (unary_op_p (operator_code))
9555 error ("%qD must take %<void%>", decl);
9557 error ("%qD must take exactly one argument", decl);
9561 else /* if (binary_op_p (operator_code)) */
9566 error ("%qD must take exactly one argument", decl);
9568 error ("%qD must take exactly two arguments", decl);
9572 /* More Effective C++ rule 7. */
9574 && (operator_code == TRUTH_ANDIF_EXPR
9575 || operator_code == TRUTH_ORIF_EXPR
9576 || operator_code == COMPOUND_EXPR))
9577 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
9581 /* Effective C++ rule 23. */
9584 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
9585 && (operator_code == PLUS_EXPR
9586 || operator_code == MINUS_EXPR
9587 || operator_code == TRUNC_DIV_EXPR
9588 || operator_code == MULT_EXPR
9589 || operator_code == TRUNC_MOD_EXPR)
9590 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
9591 warning (OPT_Weffc__, "%qD should return by value", decl);
9594 for (; argtypes && argtypes != void_list_node;
9595 argtypes = TREE_CHAIN (argtypes))
9596 if (TREE_PURPOSE (argtypes))
9598 TREE_PURPOSE (argtypes) = NULL_TREE;
9599 if (operator_code == POSTINCREMENT_EXPR
9600 || operator_code == POSTDECREMENT_EXPR)
9603 pedwarn ("%qD cannot have default arguments", decl);
9607 error ("%qD cannot have default arguments", decl);
9615 /* Return a string giving the keyword associate with CODE. */
9618 tag_name (enum tag_types code)
9637 /* Name lookup in an elaborated-type-specifier (after the keyword
9638 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
9639 elaborated-type-specifier is invalid, issue a diagnostic and return
9640 error_mark_node; otherwise, return the *_TYPE to which it referred.
9641 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
9644 check_elaborated_type_specifier (enum tag_types tag_code,
9646 bool allow_template_p)
9652 struct S { struct S *p; };
9654 name lookup will find the TYPE_DECL for the implicit "S::S"
9655 typedef. Adjust for that here. */
9656 if (DECL_SELF_REFERENCE_P (decl))
9657 decl = TYPE_NAME (TREE_TYPE (decl));
9659 type = TREE_TYPE (decl);
9661 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
9662 is false for this case as well. */
9663 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
9665 error ("using template type parameter %qT after %qs",
9666 type, tag_name (tag_code));
9667 return error_mark_node;
9671 If the identifier resolves to a typedef-name or a template
9672 type-parameter, the elaborated-type-specifier is ill-formed.
9674 In other words, the only legitimate declaration to use in the
9675 elaborated type specifier is the implicit typedef created when
9676 the type is declared. */
9677 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
9678 && tag_code != typename_type)
9680 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
9681 error ("%q+D has a previous declaration here", decl);
9682 return error_mark_node;
9684 else if (TREE_CODE (type) != RECORD_TYPE
9685 && TREE_CODE (type) != UNION_TYPE
9686 && tag_code != enum_type
9687 && tag_code != typename_type)
9689 error ("%qT referred to as %qs", type, tag_name (tag_code));
9690 error ("%q+T has a previous declaration here", type);
9691 return error_mark_node;
9693 else if (TREE_CODE (type) != ENUMERAL_TYPE
9694 && tag_code == enum_type)
9696 error ("%qT referred to as enum", type);
9697 error ("%q+T has a previous declaration here", type);
9698 return error_mark_node;
9700 else if (!allow_template_p
9701 && TREE_CODE (type) == RECORD_TYPE
9702 && CLASSTYPE_IS_TEMPLATE (type))
9704 /* If a class template appears as elaborated type specifier
9705 without a template header such as:
9707 template <class T> class C {};
9708 void f(class C); // No template header here
9710 then the required template argument is missing. */
9711 error ("template argument required for %<%s %T%>",
9712 tag_name (tag_code),
9713 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
9714 return error_mark_node;
9720 /* Lookup NAME in elaborate type specifier in scope according to
9721 SCOPE and issue diagnostics if necessary.
9722 Return *_TYPE node upon success, NULL_TREE when the NAME is not
9723 found, and ERROR_MARK_NODE for type error. */
9726 lookup_and_check_tag (enum tag_types tag_code, tree name,
9727 tag_scope scope, bool template_header_p)
9731 if (scope == ts_global)
9733 /* First try ordinary name lookup, ignoring hidden class name
9734 injected via friend declaration. */
9735 decl = lookup_name_prefer_type (name, 2);
9736 /* If that fails, the name will be placed in the smallest
9737 non-class, non-function-prototype scope according to 3.3.1/5.
9738 We may already have a hidden name declared as friend in this
9739 scope. So lookup again but not ignoring hidden names.
9740 If we find one, that name will be made visible rather than
9741 creating a new tag. */
9743 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
9746 decl = lookup_type_scope (name, scope);
9748 if (decl && DECL_CLASS_TEMPLATE_P (decl))
9749 decl = DECL_TEMPLATE_RESULT (decl);
9751 if (decl && TREE_CODE (decl) == TYPE_DECL)
9753 /* Look for invalid nested type:
9757 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
9759 error ("%qD has the same name as the class in which it is "
9762 return error_mark_node;
9765 /* Two cases we need to consider when deciding if a class
9766 template is allowed as an elaborated type specifier:
9767 1. It is a self reference to its own class.
9768 2. It comes with a template header.
9772 template <class T> class C {
9773 class C *c1; // DECL_SELF_REFERENCE_P is true
9776 template <class U> class C; // template_header_p is true
9777 template <class T> class C<T>::D {
9778 class C *c2; // DECL_SELF_REFERENCE_P is true
9781 t = check_elaborated_type_specifier (tag_code,
9784 | DECL_SELF_REFERENCE_P (decl));
9787 else if (decl && TREE_CODE (decl) == TREE_LIST)
9789 error ("reference to %qD is ambiguous", name);
9790 print_candidates (decl);
9791 return error_mark_node;
9797 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
9798 Define the tag as a forward-reference if it is not defined.
9800 If a declaration is given, process it here, and report an error if
9801 multiple declarations are not identical.
9803 SCOPE is TS_CURRENT when this is also a definition. Only look in
9804 the current frame for the name (since C++ allows new names in any
9805 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
9806 declaration. Only look beginning from the current scope outward up
9807 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
9809 TEMPLATE_HEADER_P is true when this declaration is preceded by
9810 a set of template parameters. */
9813 xref_tag (enum tag_types tag_code, tree name,
9814 tag_scope scope, bool template_header_p)
9816 enum tree_code code;
9818 tree context = NULL_TREE;
9820 timevar_push (TV_NAME_LOOKUP);
9822 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
9834 code = ENUMERAL_TYPE;
9840 /* In case of anonymous name, xref_tag is only called to
9841 make type node and push name. Name lookup is not required. */
9842 if (ANON_AGGRNAME_P (name))
9845 t = lookup_and_check_tag (tag_code, name,
9846 scope, template_header_p);
9848 if (t == error_mark_node)
9849 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9851 if (scope != ts_current && t && current_class_type
9852 && template_class_depth (current_class_type)
9853 && template_header_p)
9855 /* Since SCOPE is not TS_CURRENT, we are not looking at a
9856 definition of this tag. Since, in addition, we are currently
9857 processing a (member) template declaration of a template
9858 class, we must be very careful; consider:
9865 { template <class V>
9866 friend struct S1; };
9868 Here, the S2::S1 declaration should not be confused with the
9869 outer declaration. In particular, the inner version should
9870 have a template parameter of level 2, not level 1. This
9871 would be particularly important if the member declaration
9874 template <class V = U> friend struct S1;
9876 say, when we should tsubst into `U' when instantiating
9877 S2. On the other hand, when presented with:
9887 we must find the inner binding eventually. We
9888 accomplish this by making sure that the new type we
9889 create to represent this declaration has the right
9891 context = TYPE_CONTEXT (t);
9897 /* If no such tag is yet defined, create a forward-reference node
9898 and record it as the "definition".
9899 When a real declaration of this type is found,
9900 the forward-reference will be altered into a real type. */
9901 if (code == ENUMERAL_TYPE)
9903 error ("use of enum %q#D without previous declaration", name);
9904 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9908 t = make_aggr_type (code);
9909 TYPE_CONTEXT (t) = context;
9910 t = pushtag (name, t, scope);
9915 if (template_header_p && IS_AGGR_TYPE (t))
9917 if (!redeclare_class_template (t, current_template_parms))
9918 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9920 else if (!processing_template_decl
9922 && CLASSTYPE_IS_TEMPLATE (t))
9924 error ("redeclaration of %qT as a non-template", t);
9925 error ("previous declaration %q+D", t);
9926 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9929 /* Make injected friend class visible. */
9930 if (scope != ts_within_enclosing_non_class
9931 && hidden_name_p (TYPE_NAME (t)))
9933 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
9934 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
9936 if (TYPE_TEMPLATE_INFO (t))
9938 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
9939 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
9944 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
9948 xref_tag_from_type (tree old, tree id, tag_scope scope)
9950 enum tag_types tag_kind;
9952 if (TREE_CODE (old) == RECORD_TYPE)
9953 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
9955 tag_kind = union_type;
9957 if (id == NULL_TREE)
9958 id = TYPE_IDENTIFIER (old);
9960 return xref_tag (tag_kind, id, scope, false);
9963 /* Create the binfo hierarchy for REF with (possibly NULL) base list
9964 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
9965 access_* node, and the TREE_VALUE is the type of the base-class.
9966 Non-NULL TREE_TYPE indicates virtual inheritance.
9968 Returns true if the binfo heirarchy was successfully created,
9969 false if an error was detected. */
9972 xref_basetypes (tree ref, tree base_list)
9975 tree binfo, base_binfo;
9976 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
9977 unsigned max_bases = 0; /* Maximum direct bases. */
9979 tree default_access;
9980 tree igo_prev; /* Track Inheritance Graph Order. */
9982 if (ref == error_mark_node)
9985 /* The base of a derived class is private by default, all others are
9987 default_access = (TREE_CODE (ref) == RECORD_TYPE
9988 && CLASSTYPE_DECLARED_CLASS (ref)
9989 ? access_private_node : access_public_node);
9991 /* First, make sure that any templates in base-classes are
9992 instantiated. This ensures that if we call ourselves recursively
9993 we do not get confused about which classes are marked and which
9998 tree basetype = TREE_VALUE (*basep);
10000 if (!(processing_template_decl && uses_template_parms (basetype))
10001 && !complete_type_or_else (basetype, NULL))
10002 /* An incomplete type. Remove it from the list. */
10003 *basep = TREE_CHAIN (*basep);
10007 if (TREE_TYPE (*basep))
10009 if (CLASS_TYPE_P (basetype))
10010 max_vbases += VEC_length (tree, CLASSTYPE_VBASECLASSES (basetype));
10011 basep = &TREE_CHAIN (*basep);
10015 TYPE_MARKED_P (ref) = 1;
10017 /* The binfo slot should be empty, unless this is an (ill-formed)
10019 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
10020 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
10022 binfo = make_tree_binfo (max_bases);
10024 TYPE_BINFO (ref) = binfo;
10025 BINFO_OFFSET (binfo) = size_zero_node;
10026 BINFO_TYPE (binfo) = ref;
10030 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, max_bases);
10031 /* An aggregate cannot have baseclasses. */
10032 CLASSTYPE_NON_AGGREGATE (ref) = 1;
10034 if (TREE_CODE (ref) == UNION_TYPE)
10036 error ("derived union %qT invalid", ref);
10043 if (TYPE_FOR_JAVA (ref))
10045 error ("Java class %qT cannot have multiple bases", ref);
10052 CLASSTYPE_VBASECLASSES (ref) = VEC_alloc (tree, gc, max_vbases);
10054 if (TYPE_FOR_JAVA (ref))
10056 error ("Java class %qT cannot have virtual bases", ref);
10061 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
10063 tree access = TREE_PURPOSE (base_list);
10064 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
10065 tree basetype = TREE_VALUE (base_list);
10067 if (access == access_default_node)
10068 access = default_access;
10070 if (TREE_CODE (basetype) == TYPE_DECL)
10071 basetype = TREE_TYPE (basetype);
10072 if (TREE_CODE (basetype) != RECORD_TYPE
10073 && TREE_CODE (basetype) != TYPENAME_TYPE
10074 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
10075 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM)
10077 error ("base type %qT fails to be a struct or class type",
10082 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
10083 TYPE_FOR_JAVA (ref) = 1;
10085 base_binfo = NULL_TREE;
10086 if (CLASS_TYPE_P (basetype) && !dependent_type_p (basetype))
10088 base_binfo = TYPE_BINFO (basetype);
10089 /* The original basetype could have been a typedef'd type. */
10090 basetype = BINFO_TYPE (base_binfo);
10092 /* Inherit flags from the base. */
10093 TYPE_HAS_NEW_OPERATOR (ref)
10094 |= TYPE_HAS_NEW_OPERATOR (basetype);
10095 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
10096 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
10097 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
10098 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
10099 CLASSTYPE_DIAMOND_SHAPED_P (ref)
10100 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
10101 CLASSTYPE_REPEATED_BASE_P (ref)
10102 |= CLASSTYPE_REPEATED_BASE_P (basetype);
10105 /* We must do this test after we've seen through a typedef
10107 if (TYPE_MARKED_P (basetype))
10109 if (basetype == ref)
10110 error ("recursive type %qT undefined", basetype);
10112 error ("duplicate base type %qT invalid", basetype);
10115 TYPE_MARKED_P (basetype) = 1;
10117 base_binfo = copy_binfo (base_binfo, basetype, ref,
10118 &igo_prev, via_virtual);
10119 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
10120 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
10122 BINFO_BASE_APPEND (binfo, base_binfo);
10123 BINFO_BASE_ACCESS_APPEND (binfo, access);
10126 if (VEC_space (tree, CLASSTYPE_VBASECLASSES (ref), 1))
10127 /* If we have space in the vbase vector, we must have shared at
10128 least one of them, and are therefore diamond shaped. */
10129 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
10131 /* Unmark all the types. */
10132 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
10133 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
10134 TYPE_MARKED_P (ref) = 0;
10136 /* Now see if we have a repeated base type. */
10137 if (!CLASSTYPE_REPEATED_BASE_P (ref))
10139 for (base_binfo = binfo; base_binfo;
10140 base_binfo = TREE_CHAIN (base_binfo))
10142 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
10144 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
10147 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
10149 for (base_binfo = binfo; base_binfo;
10150 base_binfo = TREE_CHAIN (base_binfo))
10151 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
10152 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
10161 /* Begin compiling the definition of an enumeration type.
10163 Returns the type object, as yet incomplete.
10164 Also records info about it so that build_enumerator
10165 may be used to declare the individual values as they are read. */
10168 start_enum (tree name)
10172 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
10174 /* If this is the real definition for a previous forward reference,
10175 fill in the contents in the same object that used to be the
10176 forward reference. */
10178 enumtype = lookup_and_check_tag (enum_type, name,
10179 /*tag_scope=*/ts_current,
10180 /*template_header_p=*/false);
10182 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
10184 error ("multiple definition of %q#T", enumtype);
10185 error ("%Jprevious definition here", TYPE_MAIN_DECL (enumtype));
10186 /* Clear out TYPE_VALUES, and start again. */
10187 TYPE_VALUES (enumtype) = NULL_TREE;
10191 /* In case of error, make a dummy enum to allow parsing to
10193 if (enumtype == error_mark_node)
10194 name = make_anon_name ();
10196 enumtype = make_node (ENUMERAL_TYPE);
10197 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
10203 /* After processing and defining all the values of an enumeration type,
10204 install their decls in the enumeration type and finish it off.
10205 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
10208 finish_enum (tree enumtype)
10217 bool use_short_enum;
10221 integer_type_kind itk;
10222 tree underlying_type = NULL_TREE;
10224 /* We built up the VALUES in reverse order. */
10225 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
10227 /* For an enum defined in a template, just set the type of the values;
10228 all further processing is postponed until the template is
10229 instantiated. We need to set the type so that tsubst of a CONST_DECL
10231 if (processing_template_decl)
10233 for (values = TYPE_VALUES (enumtype);
10235 values = TREE_CHAIN (values))
10236 TREE_TYPE (TREE_VALUE (values)) = enumtype;
10237 if (at_function_scope_p ())
10238 add_stmt (build_min (TAG_DEFN, enumtype));
10242 /* Determine the minimum and maximum values of the enumerators. */
10243 if (TYPE_VALUES (enumtype))
10245 minnode = maxnode = NULL_TREE;
10247 for (values = TYPE_VALUES (enumtype);
10249 values = TREE_CHAIN (values))
10251 decl = TREE_VALUE (values);
10253 /* [dcl.enum]: Following the closing brace of an enum-specifier,
10254 each enumerator has the type of its enumeration. Prior to the
10255 closing brace, the type of each enumerator is the type of its
10256 initializing value. */
10257 TREE_TYPE (decl) = enumtype;
10259 /* Update the minimum and maximum values, if appropriate. */
10260 value = DECL_INITIAL (decl);
10261 if (value == error_mark_node)
10262 value = integer_zero_node;
10263 /* Figure out what the minimum and maximum values of the
10264 enumerators are. */
10266 minnode = maxnode = value;
10267 else if (tree_int_cst_lt (maxnode, value))
10269 else if (tree_int_cst_lt (value, minnode))
10276 If the enumerator-list is empty, the underlying type is as if
10277 the enumeration had a single enumerator with value 0. */
10278 minnode = maxnode = integer_zero_node;
10280 /* Compute the number of bits require to represent all values of the
10281 enumeration. We must do this before the type of MINNODE and
10282 MAXNODE are transformed, since min_precision relies on the
10283 TREE_TYPE of the value it is passed. */
10284 unsignedp = tree_int_cst_sgn (minnode) >= 0;
10285 lowprec = min_precision (minnode, unsignedp);
10286 highprec = min_precision (maxnode, unsignedp);
10287 precision = MAX (lowprec, highprec);
10289 /* Determine the underlying type of the enumeration.
10293 The underlying type of an enumeration is an integral type that
10294 can represent all the enumerator values defined in the
10295 enumeration. It is implementation-defined which integral type is
10296 used as the underlying type for an enumeration except that the
10297 underlying type shall not be larger than int unless the value of
10298 an enumerator cannot fit in an int or unsigned int.
10300 We use "int" or an "unsigned int" as the underlying type, even if
10301 a smaller integral type would work, unless the user has
10302 explicitly requested that we use the smallest possible type. The
10303 user can request that for all enumerations with a command line
10304 flag, or for just one enumeration with an attribute. */
10306 use_short_enum = flag_short_enums
10307 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
10309 for (itk = (use_short_enum ? itk_char : itk_int);
10313 underlying_type = integer_types[itk];
10314 if (TYPE_PRECISION (underlying_type) >= precision
10315 && TYPE_UNSIGNED (underlying_type) == unsignedp)
10318 if (itk == itk_none)
10322 IF no integral type can represent all the enumerator values, the
10323 enumeration is ill-formed. */
10324 error ("no integral type can represent all of the enumerator values "
10325 "for %qT", enumtype);
10326 precision = TYPE_PRECISION (long_long_integer_type_node);
10327 underlying_type = integer_types[itk_unsigned_long_long];
10330 /* Compute the minium and maximum values for the type.
10334 For an enumeration where emin is the smallest enumerator and emax
10335 is the largest, the values of the enumeration are the values of the
10336 underlying type in the range bmin to bmax, where bmin and bmax are,
10337 respectively, the smallest and largest values of the smallest bit-
10338 field that can store emin and emax. */
10340 /* The middle-end currently assumes that types with TYPE_PRECISION
10341 narrower than their underlying type are suitably zero or sign
10342 extended to fill their mode. g++ doesn't make these guarantees.
10343 Until the middle-end can represent such paradoxical types, we
10344 set the TYPE_PRECISION to the width of the underlying type. */
10345 TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type);
10347 set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp);
10351 The value of sizeof() applied to an enumeration type, an object
10352 of an enumeration type, or an enumerator, is the value of sizeof()
10353 applied to the underlying type. */
10354 TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
10355 TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
10356 TYPE_MODE (enumtype) = TYPE_MODE (underlying_type);
10357 TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
10358 TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
10359 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (underlying_type);
10361 /* Convert each of the enumerators to the type of the underlying
10362 type of the enumeration. */
10363 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
10365 location_t saved_location;
10367 decl = TREE_VALUE (values);
10368 saved_location = input_location;
10369 input_location = DECL_SOURCE_LOCATION (decl);
10370 value = perform_implicit_conversion (underlying_type,
10371 DECL_INITIAL (decl));
10372 input_location = saved_location;
10374 /* Do not clobber shared ints. */
10375 value = copy_node (value);
10377 TREE_TYPE (value) = enumtype;
10378 DECL_INITIAL (decl) = value;
10379 TREE_VALUE (values) = value;
10382 /* Fix up all variant types of this enum type. */
10383 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
10385 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
10386 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
10387 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
10388 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
10389 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
10390 TYPE_MODE (t) = TYPE_MODE (enumtype);
10391 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
10392 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
10393 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
10394 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (enumtype);
10397 /* Finish debugging output for this type. */
10398 rest_of_type_compilation (enumtype, namespace_bindings_p ());
10401 /* Build and install a CONST_DECL for an enumeration constant of the
10402 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
10403 Assignment of sequential values by default is handled here. */
10406 build_enumerator (tree name, tree value, tree enumtype)
10412 /* If the VALUE was erroneous, pretend it wasn't there; that will
10413 result in the enum being assigned the next value in sequence. */
10414 if (value == error_mark_node)
10417 /* Remove no-op casts from the value. */
10419 STRIP_TYPE_NOPS (value);
10421 if (! processing_template_decl)
10423 /* Validate and default VALUE. */
10424 if (value != NULL_TREE)
10426 value = integral_constant_value (value);
10428 if (TREE_CODE (value) == INTEGER_CST)
10430 value = perform_integral_promotions (value);
10431 constant_expression_warning (value);
10435 error ("enumerator value for %qD not integer constant", name);
10440 /* Default based on previous value. */
10441 if (value == NULL_TREE)
10443 if (TYPE_VALUES (enumtype))
10446 unsigned HOST_WIDE_INT lo;
10450 /* The next value is the previous value plus one. We can
10451 safely assume that the previous value is an INTEGER_CST.
10452 add_double doesn't know the type of the target expression,
10453 so we must check with int_fits_type_p as well. */
10454 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
10455 overflowed = add_double (TREE_INT_CST_LOW (prev_value),
10456 TREE_INT_CST_HIGH (prev_value),
10458 value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi);
10459 overflowed |= !int_fits_type_p (value, TREE_TYPE (prev_value));
10463 error ("overflow in enumeration values at %qD", name);
10464 value = error_mark_node;
10468 value = integer_zero_node;
10471 /* Remove no-op casts from the value. */
10472 STRIP_TYPE_NOPS (value);
10475 /* C++ associates enums with global, function, or class declarations. */
10476 context = current_scope ();
10478 /* Build the actual enumeration constant. Note that the enumeration
10479 constants have the type of their initializers until the
10480 enumeration is complete:
10484 Following the closing brace of an enum-specifier, each enumer-
10485 ator has the type of its enumeration. Prior to the closing
10486 brace, the type of each enumerator is the type of its
10487 initializing value.
10489 In finish_enum we will reset the type. Of course, if we're
10490 processing a template, there may be no value. */
10491 type = value ? TREE_TYPE (value) : NULL_TREE;
10493 if (context && context == current_class_type)
10494 /* This enum declaration is local to the class. We need the full
10495 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
10496 decl = build_lang_decl (CONST_DECL, name, type);
10498 /* It's a global enum, or it's local to a function. (Note local to
10499 a function could mean local to a class method. */
10500 decl = build_decl (CONST_DECL, name, type);
10502 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
10503 TREE_CONSTANT (decl) = 1;
10504 TREE_INVARIANT (decl) = 1;
10505 TREE_READONLY (decl) = 1;
10506 DECL_INITIAL (decl) = value;
10508 if (context && context == current_class_type)
10509 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
10510 on the TYPE_FIELDS list for `S'. (That's so that you can say
10511 things like `S::i' later.) */
10512 finish_member_declaration (decl);
10516 /* Add this enumeration constant to the list for this type. */
10517 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
10521 /* We're defining DECL. Make sure that it's type is OK. */
10524 check_function_type (tree decl, tree current_function_parms)
10526 tree fntype = TREE_TYPE (decl);
10527 tree return_type = complete_type (TREE_TYPE (fntype));
10529 /* In a function definition, arg types must be complete. */
10530 require_complete_types_for_parms (current_function_parms);
10532 if (dependent_type_p (return_type))
10534 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
10536 tree args = TYPE_ARG_TYPES (fntype);
10538 error ("return type %q#T is incomplete", return_type);
10540 /* Make it return void instead. */
10541 if (TREE_CODE (fntype) == METHOD_TYPE)
10542 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
10544 TREE_CHAIN (args));
10546 fntype = build_function_type (void_type_node, args);
10548 = build_exception_variant (fntype,
10549 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
10552 abstract_virtuals_error (decl, TREE_TYPE (fntype));
10555 /* Create the FUNCTION_DECL for a function definition.
10556 DECLSPECS and DECLARATOR are the parts of the declaration;
10557 they describe the function's name and the type it returns,
10558 but twisted together in a fashion that parallels the syntax of C.
10560 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
10561 DECLARATOR is really the DECL for the function we are about to
10562 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
10563 indicating that the function is an inline defined in-class.
10565 This function creates a binding context for the function body
10566 as well as setting up the FUNCTION_DECL in current_function_decl.
10568 For C++, we must first check whether that datum makes any sense.
10569 For example, "class A local_a(1,2);" means that variable local_a
10570 is an aggregate of type A, which should have a constructor
10571 applied to it with the argument list [1, 2].
10573 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
10574 or may be a BLOCK if the function has been defined previously
10575 in this translation unit. On exit, DECL_INITIAL (decl1) will be
10576 error_mark_node if the function has never been defined, or
10577 a BLOCK if the function has been defined somewhere. */
10580 start_preparsed_function (tree decl1, tree attrs, int flags)
10582 tree ctype = NULL_TREE;
10585 int doing_friend = 0;
10586 struct cp_binding_level *bl;
10587 tree current_function_parms;
10588 struct c_fileinfo *finfo
10589 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
10590 bool honor_interface;
10592 /* Sanity check. */
10593 gcc_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE);
10594 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
10596 fntype = TREE_TYPE (decl1);
10597 if (TREE_CODE (fntype) == METHOD_TYPE)
10598 ctype = TYPE_METHOD_BASETYPE (fntype);
10600 /* ISO C++ 11.4/5. A friend function defined in a class is in
10601 the (lexical) scope of the class in which it is defined. */
10602 if (!ctype && DECL_FRIEND_P (decl1))
10604 ctype = DECL_FRIEND_CONTEXT (decl1);
10606 /* CTYPE could be null here if we're dealing with a template;
10607 for example, `inline friend float foo()' inside a template
10608 will have no CTYPE set. */
10609 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
10615 if (DECL_DECLARED_INLINE_P (decl1)
10616 && lookup_attribute ("noinline", attrs))
10617 warning (0, "inline function %q+D given attribute noinline", decl1);
10619 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
10620 /* This is a constructor, we must ensure that any default args
10621 introduced by this definition are propagated to the clones
10622 now. The clones are used directly in overload resolution. */
10623 adjust_clone_args (decl1);
10625 /* Sometimes we don't notice that a function is a static member, and
10626 build a METHOD_TYPE for it. Fix that up now. */
10627 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
10628 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
10630 revert_static_member_fn (decl1);
10634 /* Set up current_class_type, and enter the scope of the class, if
10637 push_nested_class (ctype);
10638 else if (DECL_STATIC_FUNCTION_P (decl1))
10639 push_nested_class (DECL_CONTEXT (decl1));
10641 /* Now that we have entered the scope of the class, we must restore
10642 the bindings for any template parameters surrounding DECL1, if it
10643 is an inline member template. (Order is important; consider the
10644 case where a template parameter has the same name as a field of
10645 the class.) It is not until after this point that
10646 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
10647 if (flags & SF_INCLASS_INLINE)
10648 maybe_begin_member_template_processing (decl1);
10650 /* Effective C++ rule 15. */
10652 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
10653 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
10654 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
10656 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10657 error_mark_node is replaced below (in poplevel) with the BLOCK. */
10658 if (!DECL_INITIAL (decl1))
10659 DECL_INITIAL (decl1) = error_mark_node;
10661 /* This function exists in static storage.
10662 (This does not mean `static' in the C sense!) */
10663 TREE_STATIC (decl1) = 1;
10665 /* We must call push_template_decl after current_class_type is set
10666 up. (If we are processing inline definitions after exiting a
10667 class scope, current_class_type will be NULL_TREE until set above
10668 by push_nested_class.) */
10669 if (processing_template_decl)
10671 /* FIXME: Handle error_mark_node more gracefully. */
10672 tree newdecl1 = push_template_decl (decl1);
10673 if (newdecl1 != error_mark_node)
10677 /* We are now in the scope of the function being defined. */
10678 current_function_decl = decl1;
10680 /* Save the parm names or decls from this function's declarator
10681 where store_parm_decls will find them. */
10682 current_function_parms = DECL_ARGUMENTS (decl1);
10684 /* Make sure the parameter and return types are reasonable. When
10685 you declare a function, these types can be incomplete, but they
10686 must be complete when you define the function. */
10687 check_function_type (decl1, current_function_parms);
10689 /* Build the return declaration for the function. */
10690 restype = TREE_TYPE (fntype);
10691 /* Promote the value to int before returning it. */
10692 if (c_promoting_integer_type_p (restype))
10693 restype = type_promotes_to (restype);
10694 if (DECL_RESULT (decl1) == NULL_TREE)
10698 resdecl = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
10699 DECL_ARTIFICIAL (resdecl) = 1;
10700 DECL_IGNORED_P (resdecl) = 1;
10701 DECL_RESULT (decl1) = resdecl;
10703 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
10706 /* Let the user know we're compiling this function. */
10707 announce_function (decl1);
10709 /* Record the decl so that the function name is defined.
10710 If we already have a decl for this name, and it is a FUNCTION_DECL,
10711 use the old decl. */
10712 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
10714 /* A specialization is not used to guide overload resolution. */
10715 if (!DECL_FUNCTION_MEMBER_P (decl1)
10716 && !(DECL_USE_TEMPLATE (decl1) &&
10717 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
10719 tree olddecl = pushdecl (decl1);
10721 if (olddecl == error_mark_node)
10722 /* If something went wrong when registering the declaration,
10723 use DECL1; we have to have a FUNCTION_DECL to use when
10724 parsing the body of the function. */
10727 /* Otherwise, OLDDECL is either a previous declaration of
10728 the same function or DECL1 itself. */
10733 /* We need to set the DECL_CONTEXT. */
10734 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
10735 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
10737 fntype = TREE_TYPE (decl1);
10739 /* If #pragma weak applies, mark the decl appropriately now.
10740 The pragma only applies to global functions. Because
10741 determining whether or not the #pragma applies involves
10742 computing the mangled name for the declaration, we cannot
10743 apply the pragma until after we have merged this declaration
10744 with any previous declarations; if the original declaration
10745 has a linkage specification, that specification applies to
10746 the definition as well, and may affect the mangled name. */
10747 if (!DECL_CONTEXT (decl1))
10748 maybe_apply_pragma_weak (decl1);
10751 /* Reset this in case the call to pushdecl changed it. */
10752 current_function_decl = decl1;
10754 gcc_assert (DECL_INITIAL (decl1));
10756 /* This function may already have been parsed, in which case just
10757 return; our caller will skip over the body without parsing. */
10758 if (DECL_INITIAL (decl1) != error_mark_node)
10761 /* Initialize RTL machinery. We cannot do this until
10762 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
10763 even when processing a template; this is how we get
10764 CFUN set up, and our per-function variables initialized.
10765 FIXME factor out the non-RTL stuff. */
10766 bl = current_binding_level;
10767 allocate_struct_function (decl1);
10768 current_binding_level = bl;
10770 /* Even though we're inside a function body, we still don't want to
10771 call expand_expr to calculate the size of a variable-sized array.
10772 We haven't necessarily assigned RTL to all variables yet, so it's
10773 not safe to try to expand expressions involving them. */
10774 cfun->x_dont_save_pending_sizes_p = 1;
10776 /* Start the statement-tree, start the tree now. */
10777 DECL_SAVED_TREE (decl1) = push_stmt_list ();
10779 /* If we are (erroneously) defining a function that we have already
10780 defined before, wipe out what we knew before. */
10781 if (!DECL_PENDING_INLINE_P (decl1))
10782 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
10784 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
10786 /* We know that this was set up by `grokclassfn'. We do not
10787 wait until `store_parm_decls', since evil parse errors may
10788 never get us to that point. Here we keep the consistency
10789 between `current_class_type' and `current_class_ptr'. */
10790 tree t = DECL_ARGUMENTS (decl1);
10792 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
10793 gcc_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE);
10795 cp_function_chain->x_current_class_ref
10796 = build_indirect_ref (t, NULL);
10797 cp_function_chain->x_current_class_ptr = t;
10799 /* Constructors and destructors need to know whether they're "in
10800 charge" of initializing virtual base classes. */
10801 t = TREE_CHAIN (t);
10802 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
10804 current_in_charge_parm = t;
10805 t = TREE_CHAIN (t);
10807 if (DECL_HAS_VTT_PARM_P (decl1))
10809 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
10810 current_vtt_parm = t;
10814 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
10815 /* Implicitly-defined methods (like the
10816 destructor for a class in which no destructor
10817 is explicitly declared) must not be defined
10818 until their definition is needed. So, we
10819 ignore interface specifications for
10820 compiler-generated functions. */
10821 && !DECL_ARTIFICIAL (decl1));
10823 if (DECL_INTERFACE_KNOWN (decl1))
10825 tree ctx = decl_function_context (decl1);
10827 if (DECL_NOT_REALLY_EXTERN (decl1))
10828 DECL_EXTERNAL (decl1) = 0;
10830 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
10831 && TREE_PUBLIC (ctx))
10832 /* This is a function in a local class in an extern inline
10834 comdat_linkage (decl1);
10836 /* If this function belongs to an interface, it is public.
10837 If it belongs to someone else's interface, it is also external.
10838 This only affects inlines and template instantiations. */
10839 else if (!finfo->interface_unknown && honor_interface)
10841 if (DECL_DECLARED_INLINE_P (decl1)
10842 || DECL_TEMPLATE_INSTANTIATION (decl1)
10843 || processing_template_decl)
10845 DECL_EXTERNAL (decl1)
10846 = (finfo->interface_only
10847 || (DECL_DECLARED_INLINE_P (decl1)
10848 && ! flag_implement_inlines
10849 && !DECL_VINDEX (decl1)));
10851 /* For WIN32 we also want to put these in linkonce sections. */
10852 maybe_make_one_only (decl1);
10855 DECL_EXTERNAL (decl1) = 0;
10856 DECL_INTERFACE_KNOWN (decl1) = 1;
10857 /* If this function is in an interface implemented in this file,
10858 make sure that the backend knows to emit this function
10860 if (!DECL_EXTERNAL (decl1))
10861 mark_needed (decl1);
10863 else if (finfo->interface_unknown && finfo->interface_only
10864 && honor_interface)
10866 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
10867 interface, we will have both finfo->interface_unknown and
10868 finfo->interface_only set. In that case, we don't want to
10869 use the normal heuristics because someone will supply a
10870 #pragma implementation elsewhere, and deducing it here would
10871 produce a conflict. */
10872 comdat_linkage (decl1);
10873 DECL_EXTERNAL (decl1) = 0;
10874 DECL_INTERFACE_KNOWN (decl1) = 1;
10875 DECL_DEFER_OUTPUT (decl1) = 1;
10879 /* This is a definition, not a reference.
10880 So clear DECL_EXTERNAL. */
10881 DECL_EXTERNAL (decl1) = 0;
10883 if ((DECL_DECLARED_INLINE_P (decl1)
10884 || DECL_TEMPLATE_INSTANTIATION (decl1))
10885 && ! DECL_INTERFACE_KNOWN (decl1)
10886 /* Don't try to defer nested functions for now. */
10887 && ! decl_function_context (decl1))
10888 DECL_DEFER_OUTPUT (decl1) = 1;
10890 DECL_INTERFACE_KNOWN (decl1) = 1;
10893 /* Determine the ELF visibility attribute for the function. We must not
10894 do this before calling "pushdecl", as we must allow "duplicate_decls"
10895 to merge any attributes appropriately. We also need to wait until
10897 if (!DECL_CLONED_FUNCTION_P (decl1))
10898 determine_visibility (decl1);
10900 begin_scope (sk_function_parms, decl1);
10904 if (DECL_DESTRUCTOR_P (decl1)
10905 || (DECL_CONSTRUCTOR_P (decl1)
10906 && targetm.cxx.cdtor_returns_this ()))
10908 cdtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
10909 DECL_CONTEXT (cdtor_label) = current_function_decl;
10912 start_fname_decls ();
10914 store_parm_decls (current_function_parms);
10918 /* Like start_preparsed_function, except that instead of a
10919 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
10921 Returns 1 on success. If the DECLARATOR is not suitable for a function
10922 (it defines a datum instead), we return 0, which tells
10923 yyparse to report a parse error. */
10926 start_function (cp_decl_specifier_seq *declspecs,
10927 const cp_declarator *declarator,
10932 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
10933 /* If the declarator is not suitable for a function definition,
10934 cause a syntax error. */
10935 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
10938 if (DECL_MAIN_P (decl1))
10939 /* main must return int. grokfndecl should have corrected it
10940 (and issued a diagnostic) if the user got it wrong. */
10941 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
10942 integer_type_node));
10944 start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
10949 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
10953 use_eh_spec_block (tree fn)
10955 return (flag_exceptions && flag_enforce_eh_specs
10956 && !processing_template_decl
10957 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
10958 /* We insert the EH_SPEC_BLOCK only in the original
10959 function; then, it is copied automatically to the
10961 && !DECL_CLONED_FUNCTION_P (fn)
10962 /* Implicitly-generated constructors and destructors have
10963 exception specifications. However, those specifications
10964 are the union of the possible exceptions specified by the
10965 constructors/destructors for bases and members, so no
10966 unallowed exception will ever reach this function. By
10967 not creating the EH_SPEC_BLOCK we save a little memory,
10968 and we avoid spurious warnings about unreachable
10970 && !DECL_ARTIFICIAL (fn));
10973 /* Store the parameter declarations into the current function declaration.
10974 This is called after parsing the parameter declarations, before
10975 digesting the body of the function.
10977 Also install to binding contour return value identifier, if any. */
10980 store_parm_decls (tree current_function_parms)
10982 tree fndecl = current_function_decl;
10985 /* This is a chain of any other decls that came in among the parm
10986 declarations. If a parm is declared with enum {foo, bar} x;
10987 then CONST_DECLs for foo and bar are put here. */
10988 tree nonparms = NULL_TREE;
10990 if (current_function_parms)
10992 /* This case is when the function was defined with an ANSI prototype.
10993 The parms already have decls, so we need not do anything here
10994 except record them as in effect
10995 and complain if any redundant old-style parm decls were written. */
10997 tree specparms = current_function_parms;
11000 /* Must clear this because it might contain TYPE_DECLs declared
11002 current_binding_level->names = NULL;
11004 /* If we're doing semantic analysis, then we'll call pushdecl
11005 for each of these. We must do them in reverse order so that
11006 they end in the correct forward order. */
11007 specparms = nreverse (specparms);
11009 for (parm = specparms; parm; parm = next)
11011 next = TREE_CHAIN (parm);
11012 if (TREE_CODE (parm) == PARM_DECL)
11014 if (DECL_NAME (parm) == NULL_TREE
11015 || TREE_CODE (parm) != VOID_TYPE)
11018 error ("parameter %qD declared void", parm);
11022 /* If we find an enum constant or a type tag,
11023 put it aside for the moment. */
11024 TREE_CHAIN (parm) = NULL_TREE;
11025 nonparms = chainon (nonparms, parm);
11029 /* Get the decls in their original chain order and record in the
11030 function. This is all and only the PARM_DECLs that were
11031 pushed into scope by the loop above. */
11032 DECL_ARGUMENTS (fndecl) = getdecls ();
11035 DECL_ARGUMENTS (fndecl) = NULL_TREE;
11037 /* Now store the final chain of decls for the arguments
11038 as the decl-chain of the current lexical scope.
11039 Put the enumerators in as well, at the front so that
11040 DECL_ARGUMENTS is not modified. */
11041 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
11043 if (use_eh_spec_block (current_function_decl))
11044 current_eh_spec_block = begin_eh_spec_block ();
11048 /* We have finished doing semantic analysis on DECL, but have not yet
11049 generated RTL for its body. Save away our current state, so that
11050 when we want to generate RTL later we know what to do. */
11053 save_function_data (tree decl)
11055 struct language_function *f;
11057 /* Save the language-specific per-function data so that we can
11058 get it back when we really expand this function. */
11059 gcc_assert (!DECL_PENDING_INLINE_P (decl));
11062 f = GGC_NEW (struct language_function);
11063 memcpy (f, cp_function_chain, sizeof (struct language_function));
11064 DECL_SAVED_FUNCTION_DATA (decl) = f;
11066 /* Clear out the bits we don't need. */
11067 f->base.x_stmt_tree.x_cur_stmt_list = NULL_TREE;
11068 f->bindings = NULL;
11069 f->x_local_names = NULL;
11073 /* Set the return value of the constructor (if present). */
11076 finish_constructor_body (void)
11081 if (targetm.cxx.cdtor_returns_this ())
11083 /* Any return from a constructor will end up here. */
11084 add_stmt (build_stmt (LABEL_EXPR, cdtor_label));
11086 val = DECL_ARGUMENTS (current_function_decl);
11087 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
11088 DECL_RESULT (current_function_decl), val);
11089 /* Return the address of the object. */
11090 exprstmt = build_stmt (RETURN_EXPR, val);
11091 add_stmt (exprstmt);
11095 /* Do all the processing for the beginning of a destructor; set up the
11096 vtable pointers and cleanups for bases and members. */
11099 begin_destructor_body (void)
11101 tree compound_stmt;
11103 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
11104 issued an error message. We still want to try to process the
11105 body of the function, but initialize_vtbl_ptrs will crash if
11106 TYPE_BINFO is NULL. */
11107 if (COMPLETE_TYPE_P (current_class_type))
11109 compound_stmt = begin_compound_stmt (0);
11110 /* Make all virtual function table pointers in non-virtual base
11111 classes point to CURRENT_CLASS_TYPE's virtual function
11113 initialize_vtbl_ptrs (current_class_ptr);
11114 finish_compound_stmt (compound_stmt);
11116 /* And insert cleanups for our bases and members so that they
11117 will be properly destroyed if we throw. */
11118 push_base_cleanups ();
11122 /* At the end of every destructor we generate code to delete the object if
11123 necessary. Do that now. */
11126 finish_destructor_body (void)
11130 /* Any return from a destructor will end up here; that way all base
11131 and member cleanups will be run when the function returns. */
11132 add_stmt (build_stmt (LABEL_EXPR, cdtor_label));
11134 /* In a virtual destructor, we must call delete. */
11135 if (DECL_VIRTUAL_P (current_function_decl))
11138 tree virtual_size = cxx_sizeof (current_class_type);
11142 At the point of definition of a virtual destructor (including
11143 an implicit definition), non-placement operator delete shall
11144 be looked up in the scope of the destructor's class and if
11145 found shall be accessible and unambiguous. */
11146 exprstmt = build_op_delete_call(DELETE_EXPR, current_class_ptr,
11148 /*global_p=*/false,
11149 /*placement=*/NULL_TREE,
11150 /*alloc_fn=*/NULL_TREE);
11152 if_stmt = begin_if_stmt ();
11153 finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
11154 current_in_charge_parm,
11157 finish_expr_stmt (exprstmt);
11158 finish_then_clause (if_stmt);
11159 finish_if_stmt (if_stmt);
11162 if (targetm.cxx.cdtor_returns_this ())
11166 val = DECL_ARGUMENTS (current_function_decl);
11167 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
11168 DECL_RESULT (current_function_decl), val);
11169 /* Return the address of the object. */
11170 exprstmt = build_stmt (RETURN_EXPR, val);
11171 add_stmt (exprstmt);
11175 /* Do the necessary processing for the beginning of a function body, which
11176 in this case includes member-initializers, but not the catch clauses of
11177 a function-try-block. Currently, this means opening a binding level
11178 for the member-initializers (in a ctor) and member cleanups (in a dtor). */
11181 begin_function_body (void)
11185 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
11188 if (processing_template_decl)
11189 /* Do nothing now. */;
11191 /* Always keep the BLOCK node associated with the outermost pair of
11192 curly braces of a function. These are needed for correct
11193 operation of dwarfout.c. */
11194 keep_next_level (true);
11196 stmt = begin_compound_stmt (BCS_FN_BODY);
11198 if (processing_template_decl)
11199 /* Do nothing now. */;
11200 else if (DECL_DESTRUCTOR_P (current_function_decl))
11201 begin_destructor_body ();
11206 /* Do the processing for the end of a function body. Currently, this means
11207 closing out the cleanups for fully-constructed bases and members, and in
11208 the case of the destructor, deleting the object if desired. Again, this
11209 is only meaningful for [cd]tors, since they are the only functions where
11210 there is a significant distinction between the main body and any
11211 function catch clauses. Handling, say, main() return semantics here
11212 would be wrong, as flowing off the end of a function catch clause for
11213 main() would also need to return 0. */
11216 finish_function_body (tree compstmt)
11218 if (compstmt == NULL_TREE)
11221 /* Close the block. */
11222 finish_compound_stmt (compstmt);
11224 if (processing_template_decl)
11225 /* Do nothing now. */;
11226 else if (DECL_CONSTRUCTOR_P (current_function_decl))
11227 finish_constructor_body ();
11228 else if (DECL_DESTRUCTOR_P (current_function_decl))
11229 finish_destructor_body ();
11232 /* Given a function, returns the BLOCK corresponding to the outermost level
11233 of curly braces, skipping the artificial block created for constructor
11237 outer_curly_brace_block (tree fndecl)
11239 tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl));
11240 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
11241 /* Skip the artificial function body block. */
11242 block = BLOCK_SUBBLOCKS (block);
11246 /* Finish up a function declaration and compile that function
11247 all the way to assembler language output. The free the storage
11248 for the function definition.
11250 FLAGS is a bitwise or of the following values:
11252 We just finished processing the body of an in-class inline
11253 function definition. (This processing will have taken place
11254 after the class definition is complete.) */
11257 finish_function (int flags)
11259 tree fndecl = current_function_decl;
11260 tree fntype, ctype = NULL_TREE;
11261 int inclass_inline = (flags & 2) != 0;
11264 /* When we get some parse errors, we can end up without a
11265 current_function_decl, so cope. */
11266 if (fndecl == NULL_TREE)
11267 return error_mark_node;
11269 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
11270 && DECL_VIRTUAL_P (fndecl)
11271 && !processing_template_decl)
11273 tree fnclass = DECL_CONTEXT (fndecl);
11274 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
11275 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
11278 nested = function_depth > 1;
11279 fntype = TREE_TYPE (fndecl);
11281 /* TREE_READONLY (fndecl) = 1;
11282 This caused &foo to be of type ptr-to-const-function
11283 which then got a warning when stored in a ptr-to-function variable. */
11285 gcc_assert (building_stmt_tree ());
11286 /* The current function is being defined, so its DECL_INITIAL should
11287 be set, and unless there's a multiple definition, it should be
11288 error_mark_node. */
11289 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
11291 /* For a cloned function, we've already got all the code we need;
11292 there's no need to add any extra bits. */
11293 if (!DECL_CLONED_FUNCTION_P (fndecl))
11295 if (DECL_MAIN_P (current_function_decl))
11299 /* Make it so that `main' always returns 0 by default (or
11302 stmt = finish_return_stmt (integer_one_node);
11304 stmt = finish_return_stmt (integer_zero_node);
11306 /* Hack. We don't want the middle-end to warn that this
11307 return is unreachable, so put the statement on the
11309 #ifdef USE_MAPPED_LOCATION
11310 SET_EXPR_LOCATION (stmt, UNKNOWN_LOCATION);
11312 annotate_with_file_line (stmt, input_filename, 0);
11316 if (use_eh_spec_block (current_function_decl))
11317 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
11318 (TREE_TYPE (current_function_decl)),
11319 current_eh_spec_block);
11322 /* If we're saving up tree structure, tie off the function now. */
11323 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11325 finish_fname_decls ();
11327 /* If this function can't throw any exceptions, remember that. */
11328 if (!processing_template_decl
11329 && !cp_function_chain->can_throw
11330 && !flag_non_call_exceptions
11331 && !DECL_REPLACEABLE_P (fndecl))
11332 TREE_NOTHROW (fndecl) = 1;
11334 /* This must come after expand_function_end because cleanups might
11335 have declarations (from inline functions) that need to go into
11336 this function's blocks. */
11338 /* If the current binding level isn't the outermost binding level
11339 for this function, either there is a bug, or we have experienced
11340 syntax errors and the statement tree is malformed. */
11341 if (current_binding_level->kind != sk_function_parms)
11343 /* Make sure we have already experienced errors. */
11344 gcc_assert (errorcount);
11346 /* Throw away the broken statement tree and extra binding
11348 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
11350 while (current_binding_level->kind != sk_function_parms)
11352 if (current_binding_level->kind == sk_class)
11353 pop_nested_class ();
11355 poplevel (0, 0, 0);
11358 poplevel (1, 0, 1);
11360 /* Statements should always be full-expressions at the outermost set
11361 of curly braces for a function. */
11362 gcc_assert (stmts_are_full_exprs_p ());
11364 /* Set up the named return value optimization, if we can. Candidate
11365 variables are selected in check_return_expr. */
11366 if (current_function_return_value)
11368 tree r = current_function_return_value;
11371 if (r != error_mark_node
11372 /* This is only worth doing for fns that return in memory--and
11373 simpler, since we don't have to worry about promoted modes. */
11374 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
11375 /* Only allow this for variables declared in the outer scope of
11376 the function so we know that their lifetime always ends with a
11377 return; see g++.dg/opt/nrv6.C. We could be more flexible if
11378 we were to do this optimization in tree-ssa. */
11379 && (outer = outer_curly_brace_block (fndecl))
11380 && chain_member (r, BLOCK_VARS (outer)))
11381 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
11383 current_function_return_value = NULL_TREE;
11386 /* Remember that we were in class scope. */
11387 if (current_class_name)
11388 ctype = current_class_type;
11390 /* Must mark the RESULT_DECL as being in this function. */
11391 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11393 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
11394 to the FUNCTION_DECL node itself. */
11395 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11397 /* Save away current state, if appropriate. */
11398 if (!processing_template_decl)
11399 save_function_data (fndecl);
11401 /* Complain if there's just no return statement. */
11402 if (warn_return_type
11403 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
11404 && !dependent_type_p (TREE_TYPE (fntype))
11405 && !current_function_returns_value && !current_function_returns_null
11406 /* Don't complain if we abort or throw. */
11407 && !current_function_returns_abnormally
11408 && !DECL_NAME (DECL_RESULT (fndecl))
11409 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
11410 inline function, as we might never be compiled separately. */
11411 && (DECL_INLINE (fndecl) || processing_template_decl)
11412 /* Structor return values (if any) are set by the compiler. */
11413 && !DECL_CONSTRUCTOR_P (fndecl)
11414 && !DECL_DESTRUCTOR_P (fndecl))
11415 warning (OPT_Wreturn_type, "no return statement in function returning non-void");
11417 /* Store the end of the function, so that we get good line number
11418 info for the epilogue. */
11419 cfun->function_end_locus = input_location;
11421 /* Genericize before inlining. */
11422 if (!processing_template_decl)
11424 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
11425 cp_genericize (fndecl);
11426 /* Clear out the bits we don't need. */
11427 f->x_current_class_ptr = NULL;
11428 f->x_current_class_ref = NULL;
11429 f->x_eh_spec_block = NULL;
11430 f->x_in_charge_parm = NULL;
11431 f->x_vtt_parm = NULL;
11432 f->x_return_value = NULL;
11433 f->bindings = NULL;
11434 f->extern_decl_map = NULL;
11436 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
11437 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
11439 /* Clear out the bits we don't need. */
11440 local_names = NULL;
11442 /* We're leaving the context of this function, so zap cfun. It's still in
11443 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
11445 current_function_decl = NULL;
11447 /* If this is an in-class inline definition, we may have to pop the
11448 bindings for the template parameters that we added in
11449 maybe_begin_member_template_processing when start_function was
11451 if (inclass_inline)
11452 maybe_end_member_template_processing ();
11454 /* Leave the scope of the class. */
11456 pop_nested_class ();
11462 /* Let the error reporting routines know that we're outside a
11463 function. For a nested function, this value is used in
11464 cxx_pop_function_context and then reset via pop_function_context. */
11465 current_function_decl = NULL_TREE;
11470 /* Create the FUNCTION_DECL for a function definition.
11471 DECLSPECS and DECLARATOR are the parts of the declaration;
11472 they describe the return type and the name of the function,
11473 but twisted together in a fashion that parallels the syntax of C.
11475 This function creates a binding context for the function body
11476 as well as setting up the FUNCTION_DECL in current_function_decl.
11478 Returns a FUNCTION_DECL on success.
11480 If the DECLARATOR is not suitable for a function (it defines a datum
11481 instead), we return 0, which tells yyparse to report a parse error.
11483 May return void_type_node indicating that this method is actually
11484 a friend. See grokfield for more details.
11486 Came here with a `.pushlevel' .
11488 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
11489 CHANGES TO CODE IN `grokfield'. */
11492 start_method (cp_decl_specifier_seq *declspecs,
11493 const cp_declarator *declarator, tree attrlist)
11495 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
11498 if (fndecl == error_mark_node)
11499 return error_mark_node;
11501 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
11503 error ("invalid member function declaration");
11504 return error_mark_node;
11508 cplus_decl_attributes (&fndecl, attrlist, 0);
11510 /* Pass friends other than inline friend functions back. */
11511 if (fndecl == void_type_node)
11514 if (DECL_IN_AGGR_P (fndecl))
11516 if (DECL_CONTEXT (fndecl)
11517 && TREE_CODE (DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
11518 error ("%qD is already defined in class %qT", fndecl,
11519 DECL_CONTEXT (fndecl));
11520 return error_mark_node;
11523 check_template_shadow (fndecl);
11525 DECL_DECLARED_INLINE_P (fndecl) = 1;
11526 if (flag_default_inline)
11527 DECL_INLINE (fndecl) = 1;
11529 /* We process method specializations in finish_struct_1. */
11530 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
11532 fndecl = push_template_decl (fndecl);
11533 if (fndecl == error_mark_node)
11537 if (! DECL_FRIEND_P (fndecl))
11539 if (TREE_CHAIN (fndecl))
11541 fndecl = copy_node (fndecl);
11542 TREE_CHAIN (fndecl) = NULL_TREE;
11546 finish_decl (fndecl, NULL_TREE, NULL_TREE);
11548 /* Make a place for the parms. */
11549 begin_scope (sk_function_parms, fndecl);
11551 DECL_IN_AGGR_P (fndecl) = 1;
11555 /* Go through the motions of finishing a function definition.
11556 We don't compile this method until after the whole class has
11559 FINISH_METHOD must return something that looks as though it
11560 came from GROKFIELD (since we are defining a method, after all).
11562 This is called after parsing the body of the function definition.
11563 STMTS is the chain of statements that makes up the function body.
11565 DECL is the ..._DECL that `start_method' provided. */
11568 finish_method (tree decl)
11570 tree fndecl = decl;
11575 if (decl == void_type_node)
11578 old_initial = DECL_INITIAL (fndecl);
11580 /* Undo the level for the parms (from start_method).
11581 This is like poplevel, but it causes nothing to be
11582 saved. Saving information here confuses symbol-table
11583 output routines. Besides, this information will
11584 be correctly output when this method is actually
11587 /* Clear out the meanings of the local variables of this level;
11588 also record in each decl which block it belongs to. */
11590 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
11592 if (DECL_NAME (link) != NULL_TREE)
11593 pop_binding (DECL_NAME (link), link);
11594 gcc_assert (TREE_CODE (link) != FUNCTION_DECL);
11595 DECL_CONTEXT (link) = NULL_TREE;
11598 poplevel (0, 0, 0);
11600 DECL_INITIAL (fndecl) = old_initial;
11602 /* We used to check if the context of FNDECL was different from
11603 current_class_type as another way to get inside here. This didn't work
11604 for String.cc in libg++. */
11605 if (DECL_FRIEND_P (fndecl))
11607 VEC_safe_push (tree, gc, CLASSTYPE_INLINE_FRIENDS (current_class_type),
11609 decl = void_type_node;
11616 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
11617 we can lay it out later, when and if its type becomes complete. */
11620 maybe_register_incomplete_var (tree var)
11622 gcc_assert (TREE_CODE (var) == VAR_DECL);
11624 /* Keep track of variables with incomplete types. */
11625 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
11626 && DECL_EXTERNAL (var))
11628 tree inner_type = TREE_TYPE (var);
11630 while (TREE_CODE (inner_type) == ARRAY_TYPE)
11631 inner_type = TREE_TYPE (inner_type);
11632 inner_type = TYPE_MAIN_VARIANT (inner_type);
11634 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
11635 /* RTTI TD entries are created while defining the type_info. */
11636 || (TYPE_LANG_SPECIFIC (inner_type)
11637 && TYPE_BEING_DEFINED (inner_type)))
11638 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
11642 /* Called when a class type (given by TYPE) is defined. If there are
11643 any existing VAR_DECLs whose type hsa been completed by this
11644 declaration, update them now. */
11647 complete_vars (tree type)
11649 tree *list = &incomplete_vars;
11651 gcc_assert (CLASS_TYPE_P (type));
11654 if (same_type_p (type, TREE_PURPOSE (*list)))
11656 tree var = TREE_VALUE (*list);
11657 tree type = TREE_TYPE (var);
11658 /* Complete the type of the variable. The VAR_DECL itself
11659 will be laid out in expand_expr. */
11660 complete_type (type);
11661 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
11662 /* Remove this entry from the list. */
11663 *list = TREE_CHAIN (*list);
11666 list = &TREE_CHAIN (*list);
11669 /* Check for pending declarations which may have abstract type. */
11670 complete_type_check_abstract (type);
11673 /* If DECL is of a type which needs a cleanup, build that cleanup
11677 cxx_maybe_build_cleanup (tree decl)
11679 tree type = TREE_TYPE (decl);
11681 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
11683 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
11685 bool has_vbases = (TREE_CODE (type) == RECORD_TYPE
11686 && CLASSTYPE_VBASECLASSES (type));
11688 if (TREE_CODE (type) == ARRAY_TYPE)
11692 cxx_mark_addressable (decl);
11693 rval = build_unary_op (ADDR_EXPR, decl, 0);
11696 /* Optimize for space over speed here. */
11697 if (!has_vbases || flag_expensive_optimizations)
11698 flags |= LOOKUP_NONVIRTUAL;
11700 rval = build_delete (TREE_TYPE (rval), rval,
11701 sfk_complete_destructor, flags, 0);
11708 /* When a stmt has been parsed, this function is called. */
11715 /* DECL was originally constructed as a non-static member function,
11716 but turned out to be static. Update it accordingly. */
11719 revert_static_member_fn (tree decl)
11722 tree function = TREE_TYPE (decl);
11723 tree args = TYPE_ARG_TYPES (function);
11725 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
11726 != TYPE_UNQUALIFIED)
11727 error ("static member function %q#D declared with type qualifiers", decl);
11729 args = TREE_CHAIN (args);
11730 tmp = build_function_type (TREE_TYPE (function), args);
11731 tmp = build_qualified_type (tmp, cp_type_quals (function));
11732 tmp = build_exception_variant (tmp,
11733 TYPE_RAISES_EXCEPTIONS (function));
11734 TREE_TYPE (decl) = tmp;
11735 if (DECL_ARGUMENTS (decl))
11736 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
11737 DECL_STATIC_FUNCTION_P (decl) = 1;
11740 /* Initialize the variables used during compilation of a C++
11744 cxx_push_function_context (struct function * f)
11746 struct language_function *p = GGC_CNEW (struct language_function);
11749 /* Whenever we start a new function, we destroy temporaries in the
11751 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
11757 if (DECL_SAVED_FUNCTION_DATA (fn))
11759 /* If we already parsed this function, and we're just expanding it
11760 now, restore saved state. */
11761 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
11763 /* We don't need the saved data anymore. Unless this is an inline
11764 function; we need the named return value info for
11765 declare_return_variable. */
11766 if (! DECL_INLINE (fn))
11767 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
11772 /* Free the language-specific parts of F, now that we've finished
11773 compiling the function. */
11776 cxx_pop_function_context (struct function * f)
11781 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
11782 one of the language-independent trees. */
11784 enum cp_tree_node_structure_enum
11785 cp_tree_node_structure (union lang_tree_node * t)
11787 switch (TREE_CODE (&t->generic))
11789 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
11790 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
11791 case OVERLOAD: return TS_CP_OVERLOAD;
11792 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
11793 case TINST_LEVEL: return TS_CP_TINST_LEVEL;
11794 case PTRMEM_CST: return TS_CP_PTRMEM;
11795 case BASELINK: return TS_CP_BASELINK;
11796 default: return TS_CP_GENERIC;
11800 /* Build the void_list_node (void_type_node having been created). */
11802 build_void_list_node (void)
11804 tree t = build_tree_list (NULL_TREE, void_type_node);
11809 cp_missing_noreturn_ok_p (tree decl)
11811 /* A missing noreturn is ok for the `main' function. */
11812 return DECL_MAIN_P (decl);
11815 /* Return the COMDAT group into which DECL should be placed. */
11818 cxx_comdat_group (tree decl)
11822 /* Virtual tables, construction virtual tables, and virtual table
11823 tables all go in a single COMDAT group, named after the primary
11825 if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
11826 name = DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (DECL_CONTEXT (decl)));
11827 /* For all other DECLs, the COMDAT group is the mangled name of the
11828 declaration itself. */
11831 while (DECL_THUNK_P (decl))
11833 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
11834 into the same section as the target function. In that case
11835 we must return target's name. */
11836 tree target = THUNK_TARGET (decl);
11837 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
11838 && DECL_SECTION_NAME (target) != NULL
11839 && DECL_ONE_ONLY (target))
11844 name = DECL_ASSEMBLER_NAME (decl);
11847 return IDENTIFIER_POINTER (name);
11850 #include "gt-cp-decl.h"