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 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, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, 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"
51 #include "diagnostic.h"
54 static tree grokparms (tree);
55 static const char *redeclaration_error_message (tree, tree);
57 static void push_binding_level (struct cp_binding_level *, int,
59 static void pop_binding_level (void);
60 static void suspend_binding_level (void);
61 static void resume_binding_level (struct cp_binding_level *);
62 static struct cp_binding_level *make_binding_level (void);
63 static void declare_namespace_level (void);
64 static int decl_jump_unsafe (tree);
65 static void storedecls (tree);
66 static void require_complete_types_for_parms (tree);
67 static int ambi_op_p (enum tree_code);
68 static int unary_op_p (enum tree_code);
69 static tree store_bindings (tree, tree);
70 static tree lookup_tag_reverse (tree, tree);
71 static void push_local_name (tree);
72 static void warn_extern_redeclared_static (tree, tree);
73 static tree grok_reference_init (tree, tree, tree);
74 static tree grokfndecl (tree, tree, tree, tree, int,
75 enum overload_flags, tree,
76 tree, int, int, int, int, int, int, tree);
77 static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
78 static tree follow_tag_typedef (tree);
79 static tree lookup_tag (enum tree_code, tree,
80 struct cp_binding_level *, int);
81 static void set_identifier_type_value_with_scope
82 (tree, tree, struct cp_binding_level *);
83 static void record_unknown_type (tree, const char *);
84 static tree builtin_function_1 (const char *, tree, tree, int,
85 enum built_in_class, const char *,
87 static tree build_library_fn_1 (tree, enum tree_code, tree);
88 static int member_function_or_else (tree, tree, enum overload_flags);
89 static void bad_specifiers (tree, const char *, int, int, int, int,
91 static tree maybe_process_template_type_declaration
92 (tree, int, struct cp_binding_level*);
93 static void check_for_uninitialized_const_var (tree);
94 static hashval_t typename_hash (const void *);
95 static int typename_compare (const void *, const void *);
96 static void push_binding (tree, tree, struct cp_binding_level*);
97 static int add_binding (tree, tree);
98 static void pop_binding (tree, tree);
99 static tree local_variable_p_walkfn (tree *, int *, void *);
100 static tree find_binding (tree, tree);
101 static tree select_decl (tree, int);
102 static int lookup_flags (int, int);
103 static tree qualify_lookup (tree, int);
104 static tree record_builtin_java_type (const char *, int);
105 static const char *tag_name (enum tag_types code);
106 static void find_class_binding_level (void);
107 static struct cp_binding_level *innermost_nonclass_level (void);
108 static void warn_about_implicit_typename_lookup (tree, tree);
109 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
110 static int walk_globals_r (tree, void*);
111 static int walk_vtables_r (tree, void*);
112 static void add_decl_to_level (tree, struct cp_binding_level *);
113 static tree make_label_decl (tree, int);
114 static void use_label (tree);
115 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
117 static void check_previous_goto (struct named_label_use_list *);
118 static void check_switch_goto (struct cp_binding_level *);
119 static void check_previous_gotos (tree);
120 static void pop_label (tree, tree);
121 static void pop_labels (tree);
122 static void maybe_deduce_size_from_array_init (tree, tree);
123 static void layout_var_decl (tree);
124 static void maybe_commonize_var (tree);
125 static tree check_initializer (tree, tree, int);
126 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
127 static void save_function_data (tree);
128 static void check_function_type (tree, tree);
129 static void begin_constructor_body (void);
130 static void finish_constructor_body (void);
131 static void begin_destructor_body (void);
132 static void finish_destructor_body (void);
133 static tree create_array_type_for_decl (tree, tree, tree);
134 static tree get_atexit_node (void);
135 static tree get_dso_handle_node (void);
136 static tree start_cleanup_fn (void);
137 static void end_cleanup_fn (void);
138 static tree cp_make_fname_decl (tree, int);
139 static void initialize_predefined_identifiers (void);
140 static tree check_special_function_return_type
141 (special_function_kind, tree, tree);
142 static tree push_cp_library_fn (enum tree_code, tree);
143 static tree build_cp_library_fn (tree, enum tree_code, tree);
144 static void store_parm_decls (tree);
145 static int cp_missing_noreturn_ok_p (tree);
146 static void initialize_local_var (tree, tree);
147 static void expand_static_init (tree, tree);
148 static tree next_initializable_field (tree);
149 static tree reshape_init (tree, tree *);
151 #if defined (DEBUG_BINDING_LEVELS)
152 static void indent (void);
155 /* Erroneous argument lists can use this *IFF* they do not modify it. */
156 tree error_mark_list;
158 /* The following symbols are subsumed in the cp_global_trees array, and
159 listed here individually for documentation purposes.
162 tree wchar_decl_node;
164 tree vtable_entry_type;
165 tree delta_type_node;
166 tree __t_desc_type_node;
167 tree ti_desc_type_node;
168 tree bltn_desc_type_node, ptr_desc_type_node;
169 tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
170 tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
171 tree ptm_desc_type_node;
172 tree base_desc_type_node;
174 tree class_type_node, record_type_node, union_type_node, enum_type_node;
175 tree unknown_type_node;
177 Array type `vtable_entry_type[]'
180 tree vtbl_ptr_type_node;
187 A FUNCTION_DECL which can call `abort'. Not necessarily the
188 one that the user will declare, but sufficient to be called
189 by routines that want to abort the program.
193 The FUNCTION_DECL for the default `::operator delete'.
195 tree global_delete_fndecl;
198 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
203 tree cp_global_trees[CPTI_MAX];
205 /* Indicates that there is a type value in some namespace, although
206 that is not necessarily in scope at the moment. */
208 static GTY(()) tree global_type_node;
210 /* Used only for jumps to as-yet undefined labels, since jumps to
211 defined labels can have their validity checked immediately. */
213 struct named_label_use_list GTY(())
215 struct cp_binding_level *binding_level;
218 const char *filename_o_goto;
220 struct named_label_use_list *next;
223 #define named_label_uses cp_function_chain->x_named_label_uses
225 #define local_names cp_function_chain->x_local_names
227 /* A list of objects which have constructors or destructors
228 which reside in the global scope. The decl is stored in
229 the TREE_VALUE slot and the initializer is stored
230 in the TREE_PURPOSE slot. */
231 tree static_aggregates;
235 /* A node for the integer constants 2, and 3. */
237 tree integer_two_node, integer_three_node;
239 /* Similar, for last_function_parm_tags. */
240 tree last_function_parms;
242 /* A list of all LABEL_DECLs in the function that have names. Here so
243 we can clear out their names' definitions at the end of the
244 function, and so we can check the validity of jumps to these labels. */
246 struct named_label_list GTY(())
248 struct cp_binding_level *binding_level;
253 struct named_label_list *next;
254 unsigned int in_try_scope : 1;
255 unsigned int in_catch_scope : 1;
258 #define named_labels cp_function_chain->x_named_labels
260 /* The name of the anonymous namespace, throughout this translation
262 tree anonymous_namespace_name;
264 /* The number of function bodies which we are currently processing.
265 (Zero if we are at namespace scope, one inside the body of a
266 function, two inside the body of a function in a local class, etc.) */
269 /* States indicating how grokdeclarator() should handle declspecs marked
270 with __attribute__((deprecated)). An object declared as
271 __attribute__((deprecated)) suppresses warnings of uses of other
274 enum deprecated_states {
279 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
281 /* Set by add_implicitly_declared_members() to keep those members from
282 being flagged as deprecated or reported as using deprecated
284 int adding_implicit_members = 0;
286 /* True if a declaration with an `extern' linkage specifier is being
288 bool have_extern_spec;
291 /* For each binding contour we allocate a binding_level structure
292 which records the names defined in that contour.
295 1) one for each function definition,
296 where internal declarations of the parameters appear.
297 2) one for each compound statement,
298 to record its declarations.
300 The current meaning of a name can be found by searching the levels
301 from the current one out to the global one.
303 Off to the side, may be the class_binding_level. This exists only
304 to catch class-local declarations. It is otherwise nonexistent.
306 Also there may be binding levels that catch cleanups that must be
307 run when exceptions occur. Thus, to see whether a name is bound in
308 the current scope, it is not enough to look in the
309 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
312 /* Note that the information in the `names' component of the global contour
313 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
315 struct cp_binding_level GTY(())
317 /* A chain of _DECL nodes for all variables, constants, functions,
318 and typedef types. These are in the reverse of the order
319 supplied. There may be OVERLOADs on this list, too, but they
320 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
323 /* Count of elements in names chain. */
326 /* A chain of NAMESPACE_DECL nodes. */
329 /* A chain of VTABLE_DECL nodes. */
332 /* A list of structure, union and enum definitions, for looking up
334 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
335 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
336 or ENUMERAL_TYPE node.
338 C++: the TREE_VALUE nodes can be simple types for
339 component_bindings. */
342 /* A list of USING_DECL nodes. */
345 /* A list of used namespaces. PURPOSE is the namespace,
346 VALUE the common ancestor with this binding_level's namespace. */
347 tree using_directives;
349 /* If this binding level is the binding level for a class, then
350 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
351 is the name of an entity bound in the class. The TREE_TYPE is
352 the DECL bound by this name in the class. */
355 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
356 is used for all binding levels. In addition the TREE_VALUE is the
357 IDENTIFIER_TYPE_VALUE before we entered the class. */
360 /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local
361 label in this scope. The TREE_PURPOSE is the previous value of
362 the IDENTIFIER_LABEL VALUE. */
363 tree shadowed_labels;
365 /* For each level (except not the global one),
366 a chain of BLOCK nodes for all the levels
367 that were entered and exited one level down. */
370 /* The _TYPE node for this level, if parm_flag == 2. */
373 /* The binding level which this one is contained in (inherits from). */
374 struct cp_binding_level *level_chain;
376 /* List of VAR_DECLS saved from a previous for statement.
377 These would be dead in ISO-conforming code, but might
378 be referenced in ARM-era code. These are stored in a
379 TREE_LIST; the TREE_VALUE is the actual declaration. */
380 tree dead_vars_from_for;
382 /* 1 for the level that holds the parameters of a function.
383 2 for the level that holds a class declaration. */
384 unsigned parm_flag : 2;
386 /* 1 means make a BLOCK for this level regardless of all else.
387 2 for temporary binding contours created by the compiler. */
390 /* Nonzero if this level "doesn't exist" for tags. */
391 unsigned tag_transparent : 1;
393 /* Nonzero if this level can safely have additional
394 cleanup-needing variables added to it. */
395 unsigned more_cleanups_ok : 1;
396 unsigned have_cleanups : 1;
398 /* Nonzero if this scope is for storing the decls for template
399 parameters and generic decls; these decls will be discarded and
400 replaced with a TEMPLATE_DECL. */
401 unsigned template_parms_p : 1;
403 /* Nonzero if this scope corresponds to the `<>' in a
404 `template <>' clause. Whenever this flag is set,
405 TEMPLATE_PARMS_P will be set as well. */
406 unsigned template_spec_p : 1;
408 /* This is set for a namespace binding level. */
409 unsigned namespace_p : 1;
411 /* True if this level is that of a for-statement where we need to
412 worry about ambiguous (ARM or ISO) scope rules. */
413 unsigned is_for_scope : 1;
415 /* True if this level corresponds to a TRY block. Currently this
416 information is only available while building the tree structure. */
417 unsigned is_try_scope : 1;
419 /* True if this level corresponds to a CATCH block. Currently this
420 information is only available while building the tree structure. */
421 unsigned is_catch_scope : 1;
423 /* Three bits left for this word. */
425 /* Binding depth at which this level began. */
426 unsigned binding_depth;
429 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
431 /* The binding level currently in effect. */
433 #define current_binding_level \
434 (cfun && cp_function_chain->bindings \
435 ? cp_function_chain->bindings \
436 : scope_chain->bindings)
438 /* The binding level of the current class, if any. */
440 #define class_binding_level scope_chain->class_bindings
442 /* A chain of binding_level structures awaiting reuse. */
444 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
446 /* The outermost binding level, for names of file scope.
447 This is created when the compiler is started and exists
448 through the entire run. */
450 static GTY(()) struct cp_binding_level *global_binding_level;
452 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
454 static int keep_next_level_flag;
456 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
457 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
458 time the VAR_DECL was declared, the type was incomplete. */
460 static GTY(()) tree incomplete_vars;
462 #if defined(DEBUG_BINDING_LEVELS)
463 static int binding_depth = 0;
464 static int is_class_level = 0;
471 for (i = 0; i < binding_depth*2; i++)
474 #endif /* defined(DEBUG_BINDING_LEVELS) */
476 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
479 push_binding_level (struct cp_binding_level *newlevel,
483 /* Add this level to the front of the chain (stack) of levels that
485 memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
486 newlevel->level_chain = current_binding_level;
487 current_binding_level = newlevel;
488 newlevel->tag_transparent = tag_transparent;
489 newlevel->more_cleanups_ok = 1;
491 newlevel->keep = keep;
492 #if defined(DEBUG_BINDING_LEVELS)
493 newlevel->binding_depth = binding_depth;
495 fprintf (stderr, "push %s level 0x%08x line %d\n",
496 (is_class_level) ? "class" : "block", newlevel, lineno);
499 #endif /* defined(DEBUG_BINDING_LEVELS) */
502 /* Find the innermost enclosing class scope, and reset
503 CLASS_BINDING_LEVEL appropriately. */
506 find_class_binding_level (void)
508 struct cp_binding_level *level = current_binding_level;
510 while (level && level->parm_flag != 2)
511 level = level->level_chain;
512 if (level && level->parm_flag == 2)
513 class_binding_level = level;
515 class_binding_level = 0;
519 pop_binding_level (void)
521 if (global_binding_level)
523 /* Cannot pop a level, if there are none left to pop. */
524 if (current_binding_level == global_binding_level)
527 /* Pop the current level, and free the structure for reuse. */
528 #if defined(DEBUG_BINDING_LEVELS)
531 fprintf (stderr, "pop %s level 0x%08x line %d\n",
532 (is_class_level) ? "class" : "block",
533 current_binding_level, lineno);
534 if (is_class_level != (current_binding_level == class_binding_level))
537 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
540 #endif /* defined(DEBUG_BINDING_LEVELS) */
542 register struct cp_binding_level *level = current_binding_level;
543 current_binding_level = current_binding_level->level_chain;
544 level->level_chain = free_binding_level;
545 #if 0 /* defined(DEBUG_BINDING_LEVELS) */
546 if (level->binding_depth != binding_depth)
548 #endif /* defined(DEBUG_BINDING_LEVELS) */
549 free_binding_level = level;
550 find_class_binding_level ();
555 suspend_binding_level (void)
557 if (class_binding_level)
558 current_binding_level = class_binding_level;
560 if (global_binding_level)
562 /* Cannot suspend a level, if there are none left to suspend. */
563 if (current_binding_level == global_binding_level)
566 /* Suspend the current level. */
567 #if defined(DEBUG_BINDING_LEVELS)
570 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
571 (is_class_level) ? "class" : "block",
572 current_binding_level, lineno);
573 if (is_class_level != (current_binding_level == class_binding_level))
576 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
579 #endif /* defined(DEBUG_BINDING_LEVELS) */
580 current_binding_level = current_binding_level->level_chain;
581 find_class_binding_level ();
585 resume_binding_level (struct cp_binding_level* b)
587 /* Resuming binding levels is meant only for namespaces,
588 and those cannot nest into classes. */
589 my_friendly_assert(!class_binding_level, 386);
590 /* Also, resuming a non-directly nested namespace is a no-no. */
591 my_friendly_assert(b->level_chain == current_binding_level, 386);
592 current_binding_level = b;
593 #if defined(DEBUG_BINDING_LEVELS)
594 b->binding_depth = binding_depth;
596 fprintf (stderr, "resume %s level 0x%08x line %d\n",
597 (is_class_level) ? "class" : "block", b, lineno);
600 #endif /* defined(DEBUG_BINDING_LEVELS) */
603 /* Create a new `struct cp_binding_level'. */
606 struct cp_binding_level *
607 make_binding_level (void)
610 return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
613 /* Nonzero if we are currently in the global binding level. */
616 global_bindings_p (void)
618 return current_binding_level == global_binding_level;
621 /* Return the innermost binding level that is not for a class scope. */
623 static struct cp_binding_level *
624 innermost_nonclass_level (void)
626 struct cp_binding_level *b;
628 b = current_binding_level;
629 while (b->parm_flag == 2)
635 /* Nonzero if we are currently in a toplevel binding level. This
636 means either the global binding level or a namespace in a toplevel
637 binding level. Since there are no non-toplevel namespace levels,
638 this really means any namespace or template parameter level. We
639 also include a class whose context is toplevel. */
642 toplevel_bindings_p (void)
644 struct cp_binding_level *b = innermost_nonclass_level ();
646 return b->namespace_p || b->template_parms_p;
649 /* Nonzero if this is a namespace scope, or if we are defining a class
650 which is itself at namespace scope, or whose enclosing class is
651 such a class, etc. */
654 namespace_bindings_p (void)
656 struct cp_binding_level *b = innermost_nonclass_level ();
658 return b->namespace_p;
661 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
662 unconditionally. Otherwise, use the normal logic to decide whether
663 or not to create a BLOCK. */
666 keep_next_level (int keep)
668 keep_next_level_flag = keep;
671 /* Nonzero if the current level needs to have a BLOCK made. */
676 return (current_binding_level->blocks != NULL_TREE
677 || current_binding_level->keep
678 || current_binding_level->names != NULL_TREE
679 || (current_binding_level->tags != NULL_TREE
680 && !current_binding_level->tag_transparent));
684 declare_namespace_level (void)
686 current_binding_level->namespace_p = 1;
689 /* Returns nonzero if this scope was created to store template
693 template_parm_scope_p (void)
695 return current_binding_level->template_parms_p;
698 /* Returns the kind of template specialization we are currently
699 processing, given that it's declaration contained N_CLASS_SCOPES
700 explicit scope qualifications. */
703 current_tmpl_spec_kind (int n_class_scopes)
705 int n_template_parm_scopes = 0;
706 int seen_specialization_p = 0;
707 int innermost_specialization_p = 0;
708 struct cp_binding_level *b;
710 /* Scan through the template parameter scopes. */
711 for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
713 /* If we see a specialization scope inside a parameter scope,
714 then something is wrong. That corresponds to a declaration
717 template <class T> template <> ...
719 which is always invalid since [temp.expl.spec] forbids the
720 specialization of a class member template if the enclosing
721 class templates are not explicitly specialized as well. */
722 if (b->template_spec_p)
724 if (n_template_parm_scopes == 0)
725 innermost_specialization_p = 1;
727 seen_specialization_p = 1;
729 else if (seen_specialization_p == 1)
730 return tsk_invalid_member_spec;
732 ++n_template_parm_scopes;
735 /* Handle explicit instantiations. */
736 if (processing_explicit_instantiation)
738 if (n_template_parm_scopes != 0)
739 /* We've seen a template parameter list during an explicit
740 instantiation. For example:
742 template <class T> template void f(int);
744 This is erroneous. */
745 return tsk_invalid_expl_inst;
747 return tsk_expl_inst;
750 if (n_template_parm_scopes < n_class_scopes)
751 /* We've not seen enough template headers to match all the
752 specialized classes present. For example:
754 template <class T> void R<T>::S<T>::f(int);
756 This is invalid; there needs to be one set of template
757 parameters for each class. */
758 return tsk_insufficient_parms;
759 else if (n_template_parm_scopes == n_class_scopes)
760 /* We're processing a non-template declaration (even though it may
761 be a member of a template class.) For example:
763 template <class T> void S<T>::f(int);
765 The `class T' maches the `S<T>', leaving no template headers
766 corresponding to the `f'. */
768 else if (n_template_parm_scopes > n_class_scopes + 1)
769 /* We've got too many template headers. For example:
771 template <> template <class T> void f (T);
773 There need to be more enclosing classes. */
774 return tsk_excessive_parms;
776 /* This must be a template. It's of the form:
778 template <class T> template <class U> void S<T>::f(U);
780 This is a specialization if the innermost level was a
781 specialization; otherwise it's just a definition of the
783 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
787 set_class_shadows (tree shadows)
789 class_binding_level->class_shadowed = shadows;
792 /* Enter a new binding level.
793 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
794 not for that of tags. */
797 pushlevel (int tag_transparent)
799 struct cp_binding_level *newlevel;
801 if (cfun && !doing_semantic_analysis_p ())
804 /* Reuse or create a struct for this binding level. */
805 #if defined(DEBUG_BINDING_LEVELS)
807 #else /* !defined(DEBUG_BINDING_LEVELS) */
808 if (free_binding_level)
809 #endif /* !defined(DEBUG_BINDING_LEVELS) */
811 newlevel = free_binding_level;
812 free_binding_level = free_binding_level->level_chain;
815 newlevel = make_binding_level ();
817 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
818 keep_next_level_flag = 0;
821 /* We're defining an object of type TYPE. If it needs a cleanup, but
822 we're not allowed to add any more objects with cleanups to the current
823 scope, create a new binding level. */
826 maybe_push_cleanup_level (tree type)
828 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
829 && current_binding_level->more_cleanups_ok == 0)
834 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
838 /* Enter a new scope. The KIND indicates what kind of scope is being
842 begin_scope (scope_kind sk)
852 current_binding_level->is_try_scope = 1;
856 current_binding_level->is_catch_scope = 1;
860 current_binding_level->is_for_scope = 1;
863 case sk_template_spec:
864 current_binding_level->template_spec_p = 1;
867 case sk_template_parms:
868 current_binding_level->template_parms_p = 1;
876 /* Exit the current scope. */
884 /* For a binding between a name and an entity at a block scope,
885 this is the `struct cp_binding_level' for the block. */
886 #define BINDING_LEVEL(NODE) \
887 (((struct tree_binding*)(NODE))->scope.level)
889 /* A free list of CPLUS_BINDING nodes, connected by their
892 static GTY((deletable (""))) tree free_bindings;
894 /* Make DECL the innermost binding for ID. The LEVEL is the binding
895 level at which this declaration is being bound. */
898 push_binding (tree id,
900 struct cp_binding_level* level)
906 binding = free_bindings;
907 free_bindings = TREE_CHAIN (binding);
910 binding = make_node (CPLUS_BINDING);
912 /* Now, fill in the binding information. */
913 BINDING_VALUE (binding) = decl;
914 BINDING_TYPE (binding) = NULL_TREE;
915 BINDING_LEVEL (binding) = level;
916 INHERITED_VALUE_BINDING_P (binding) = 0;
917 LOCAL_BINDING_P (binding) = (level != class_binding_level);
918 BINDING_HAS_LEVEL_P (binding) = 1;
920 /* And put it on the front of the list of bindings for ID. */
921 TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
922 IDENTIFIER_BINDING (id) = binding;
925 /* ID is already bound in the current scope. But, DECL is an
926 additional binding for ID in the same scope. This is the `struct
927 stat' hack whereby a non-typedef class-name or enum-name can be
928 bound at the same level as some other kind of entity. It's the
929 responsibility of the caller to check that inserting this name is
930 valid here. Returns nonzero if the new binding was successful. */
932 add_binding (tree id, tree decl)
934 tree binding = IDENTIFIER_BINDING (id);
937 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
938 /* The new name is the type name. */
939 BINDING_TYPE (binding) = decl;
940 else if (!BINDING_VALUE (binding))
941 /* This situation arises when push_class_level_binding moves an
942 inherited type-binding out of the way to make room for a new
944 BINDING_VALUE (binding) = decl;
945 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
946 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
948 /* The old binding was a type name. It was placed in
949 BINDING_VALUE because it was thought, at the point it was
950 declared, to be the only entity with such a name. Move the
951 type name into the type slot; it is now hidden by the new
953 BINDING_TYPE (binding) = BINDING_VALUE (binding);
954 BINDING_VALUE (binding) = decl;
955 INHERITED_VALUE_BINDING_P (binding) = 0;
957 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
958 && TREE_CODE (decl) == TYPE_DECL
959 && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
960 && (same_type_p (TREE_TYPE (decl),
961 TREE_TYPE (BINDING_VALUE (binding)))
962 /* If either type involves template parameters, we must
963 wait until instantiation. */
964 || uses_template_parms (TREE_TYPE (decl))
965 || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
966 /* We have two typedef-names, both naming the same type to have
967 the same name. This is OK because of:
971 In a given scope, a typedef specifier can be used to redefine
972 the name of any type declared in that scope to refer to the
973 type to which it already refers. */
975 /* There can be two block-scope declarations of the same variable,
976 so long as they are `extern' declarations. */
977 else if (TREE_CODE (decl) == VAR_DECL
978 && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
979 && DECL_EXTERNAL (decl)
980 && DECL_EXTERNAL (BINDING_VALUE (binding)))
982 duplicate_decls (decl, BINDING_VALUE (binding));
987 error ("declaration of `%#D'", decl);
988 cp_error_at ("conflicts with previous declaration `%#D'",
989 BINDING_VALUE (binding));
996 /* Add DECL to the list of things declared in B. */
999 add_decl_to_level (tree decl,
1000 struct cp_binding_level* b)
1002 if (TREE_CODE (decl) == NAMESPACE_DECL
1003 && !DECL_NAMESPACE_ALIAS (decl))
1005 TREE_CHAIN (decl) = b->namespaces;
1006 b->namespaces = decl;
1008 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
1010 TREE_CHAIN (decl) = b->vtables;
1015 /* We build up the list in reverse order, and reverse it later if
1017 TREE_CHAIN (decl) = b->names;
1023 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1024 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1025 doesn't really belong to this binding level, that it got here
1026 through a using-declaration. */
1029 push_local_binding (tree id, tree decl, int flags)
1031 struct cp_binding_level *b;
1033 /* Skip over any local classes. This makes sense if we call
1034 push_local_binding with a friend decl of a local class. */
1035 b = current_binding_level;
1036 while (b->parm_flag == 2)
1039 if (lookup_name_current_level (id))
1041 /* Supplement the existing binding. */
1042 if (!add_binding (id, decl))
1043 /* It didn't work. Something else must be bound at this
1044 level. Do not add DECL to the list of things to pop
1049 /* Create a new binding. */
1050 push_binding (id, decl, b);
1052 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1053 /* We must put the OVERLOAD into a TREE_LIST since the
1054 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1055 decls that got here through a using-declaration. */
1056 decl = build_tree_list (NULL_TREE, decl);
1058 /* And put DECL on the list of things declared by the current
1060 add_decl_to_level (decl, b);
1063 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1064 binding was successful. */
1067 push_class_binding (tree id, tree decl)
1070 tree binding = IDENTIFIER_BINDING (id);
1073 /* Note that we declared this value so that we can issue an error if
1074 this is an invalid redeclaration of a name already used for some
1076 note_name_declared_in_class (id, decl);
1078 if (binding && BINDING_LEVEL (binding) == class_binding_level)
1079 /* Supplement the existing binding. */
1080 result = add_binding (id, decl);
1082 /* Create a new binding. */
1083 push_binding (id, decl, class_binding_level);
1085 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1086 class-level declaration. Note that we do not use DECL here
1087 because of the possibility of the `struct stat' hack; if DECL is
1088 a class-name or enum-name we might prefer a field-name, or some
1090 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1092 /* If this is a binding from a base class, mark it as such. */
1093 binding = IDENTIFIER_BINDING (id);
1094 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1096 /* Any implicit typename must be from a base-class. The
1097 context for an implicit typename declaration is always
1098 the derived class in which the lookup was done, so the checks
1099 based on the context of DECL below will not trigger. */
1100 if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl))
1101 INHERITED_VALUE_BINDING_P (binding) = 1;
1104 if (TREE_CODE (decl) == OVERLOAD)
1105 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1108 my_friendly_assert (DECL_P (decl), 0);
1109 context = context_for_name_lookup (decl);
1112 if (is_properly_derived_from (current_class_type, context))
1113 INHERITED_VALUE_BINDING_P (binding) = 1;
1115 INHERITED_VALUE_BINDING_P (binding) = 0;
1118 else if (BINDING_VALUE (binding) == decl)
1119 /* We only encounter a TREE_LIST when push_class_decls detects an
1120 ambiguity. Such an ambiguity can be overridden by a definition
1122 INHERITED_VALUE_BINDING_P (binding) = 1;
1127 /* Remove the binding for DECL which should be the innermost binding
1131 pop_binding (tree id, tree decl)
1135 if (id == NULL_TREE)
1136 /* It's easiest to write the loops that call this function without
1137 checking whether or not the entities involved have names. We
1138 get here for such an entity. */
1141 /* Get the innermost binding for ID. */
1142 binding = IDENTIFIER_BINDING (id);
1144 /* The name should be bound. */
1145 my_friendly_assert (binding != NULL_TREE, 0);
1147 /* The DECL will be either the ordinary binding or the type
1148 binding for this identifier. Remove that binding. */
1149 if (BINDING_VALUE (binding) == decl)
1150 BINDING_VALUE (binding) = NULL_TREE;
1151 else if (BINDING_TYPE (binding) == decl)
1152 BINDING_TYPE (binding) = NULL_TREE;
1156 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1158 /* We're completely done with the innermost binding for this
1159 identifier. Unhook it from the list of bindings. */
1160 IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1162 /* Add it to the free list. */
1163 TREE_CHAIN (binding) = free_bindings;
1164 free_bindings = binding;
1166 /* Clear the BINDING_LEVEL so the garbage collector doesn't walk
1168 BINDING_LEVEL (binding) = NULL;
1172 /* When a label goes out of scope, check to see if that label was used
1173 in a valid manner, and issue any appropriate warnings or errors. */
1176 pop_label (tree label, tree old_value)
1178 if (!processing_template_decl && doing_semantic_analysis_p ())
1180 if (DECL_INITIAL (label) == NULL_TREE)
1182 cp_error_at ("label `%D' used but not defined", label);
1183 /* Avoid crashing later. */
1184 define_label (input_filename, 1, DECL_NAME (label));
1186 else if (warn_unused_label && !TREE_USED (label))
1187 cp_warning_at ("label `%D' defined but not used", label);
1190 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1193 /* At the end of a function, all labels declared within the function
1194 go out of scope. BLOCK is the top-level block for the
1198 pop_labels (tree block)
1200 struct named_label_list *link;
1202 /* Clear out the definitions of all label names, since their scopes
1204 for (link = named_labels; link; link = link->next)
1206 pop_label (link->label_decl, link->old_value);
1207 /* Put the labels into the "variables" of the top-level block,
1208 so debugger can see them. */
1209 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1210 BLOCK_VARS (block) = link->label_decl;
1213 named_labels = NULL;
1216 /* Exit a binding level.
1217 Pop the level off, and restore the state of the identifier-decl mappings
1218 that were in effect when this level was entered.
1220 If KEEP == 1, this level had explicit declarations, so
1221 and create a "block" (a BLOCK node) for the level
1222 to record its declarations and subblocks for symbol table output.
1224 If FUNCTIONBODY is nonzero, this level is the body of a function,
1225 so create a block as if KEEP were set and also clear out all
1228 If REVERSE is nonzero, reverse the order of decls before putting
1229 them into the BLOCK. */
1232 poplevel (int keep, int reverse, int functionbody)
1235 /* The chain of decls was accumulated in reverse order.
1236 Put it into forward order, just for cleanliness. */
1238 int tmp = functionbody;
1239 int real_functionbody;
1242 tree block = NULL_TREE;
1244 int leaving_for_scope;
1246 if (cfun && !doing_semantic_analysis_p ())
1249 my_friendly_assert (current_binding_level->parm_flag != 2,
1252 real_functionbody = (current_binding_level->keep == 2
1253 ? ((functionbody = 0), tmp) : functionbody);
1254 tags = functionbody >= 0 ? current_binding_level->tags : 0;
1255 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1257 my_friendly_assert (!current_binding_level->class_shadowed,
1260 /* We used to use KEEP == 2 to indicate that the new block should go
1261 at the beginning of the list of blocks at this binding level,
1262 rather than the end. This hack is no longer used. */
1263 my_friendly_assert (keep == 0 || keep == 1, 0);
1265 if (current_binding_level->keep == 1)
1268 /* Any uses of undefined labels, and any defined labels, now operate
1269 under constraints of next binding contour. */
1270 if (cfun && !functionbody)
1272 struct cp_binding_level *level_chain;
1273 level_chain = current_binding_level->level_chain;
1276 struct named_label_use_list *uses;
1277 struct named_label_list *labels;
1278 for (labels = named_labels; labels; labels = labels->next)
1279 if (labels->binding_level == current_binding_level)
1282 if (current_binding_level->is_try_scope)
1283 labels->in_try_scope = 1;
1284 if (current_binding_level->is_catch_scope)
1285 labels->in_catch_scope = 1;
1286 for (decl = labels->names_in_scope; decl;
1287 decl = TREE_CHAIN (decl))
1288 if (decl_jump_unsafe (decl))
1289 labels->bad_decls = tree_cons (NULL_TREE, decl,
1291 labels->binding_level = level_chain;
1292 labels->names_in_scope = level_chain->names;
1295 for (uses = named_label_uses; uses; uses = uses->next)
1296 if (uses->binding_level == current_binding_level)
1298 uses->binding_level = level_chain;
1299 uses->names_in_scope = level_chain->names;
1304 /* Get the decls in the order they were written.
1305 Usually current_binding_level->names is in reverse order.
1306 But parameter decls were previously put in forward order. */
1309 current_binding_level->names
1310 = decls = nreverse (current_binding_level->names);
1312 decls = current_binding_level->names;
1314 /* Output any nested inline functions within this block
1315 if they weren't already output. */
1316 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1317 if (TREE_CODE (decl) == FUNCTION_DECL
1318 && ! TREE_ASM_WRITTEN (decl)
1319 && DECL_INITIAL (decl) != NULL_TREE
1320 && TREE_ADDRESSABLE (decl)
1321 && decl_function_context (decl) == current_function_decl)
1323 /* If this decl was copied from a file-scope decl
1324 on account of a block-scope extern decl,
1325 propagate TREE_ADDRESSABLE to the file-scope decl. */
1326 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1327 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1330 push_function_context ();
1331 output_inline_function (decl);
1332 pop_function_context ();
1336 /* When not in function-at-a-time mode, expand_end_bindings will
1337 warn about unused variables. But, in function-at-a-time mode
1338 expand_end_bindings is not passed the list of variables in the
1339 current scope, and therefore no warning is emitted. So, we
1340 explicitly warn here. */
1341 if (!processing_template_decl)
1342 warn_about_unused_variables (getdecls ());
1344 /* If there were any declarations or structure tags in that level,
1345 or if this level is a function body,
1346 create a BLOCK to record them for the life of this function. */
1348 if (keep == 1 || functionbody)
1349 block = make_node (BLOCK);
1350 if (block != NULL_TREE)
1352 BLOCK_VARS (block) = decls;
1353 BLOCK_SUBBLOCKS (block) = subblocks;
1356 /* In each subblock, record that this is its superior. */
1358 for (link = subblocks; link; link = TREE_CHAIN (link))
1359 BLOCK_SUPERCONTEXT (link) = block;
1361 /* We still support the old for-scope rules, whereby the variables
1362 in a for-init statement were in scope after the for-statement
1363 ended. We only use the new rules if flag_new_for_scope is
1366 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1368 /* Remove declarations for all the DECLs in this level. */
1369 for (link = decls; link; link = TREE_CHAIN (link))
1371 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1372 && DECL_NAME (link))
1375 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1379 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1381 ns_binding = NULL_TREE;
1384 && (BINDING_LEVEL (outer_binding)
1385 == current_binding_level->level_chain))
1386 /* We have something like:
1391 and we are leaving the `for' scope. There's no reason to
1392 keep the binding of the inner `i' in this case. */
1393 pop_binding (DECL_NAME (link), link);
1394 else if ((outer_binding
1395 && (TREE_CODE (BINDING_VALUE (outer_binding))
1398 && TREE_CODE (ns_binding) == TYPE_DECL))
1399 /* Here, we have something like:
1407 We must pop the for-scope binding so we know what's a
1408 type and what isn't. */
1409 pop_binding (DECL_NAME (link), link);
1412 /* Mark this VAR_DECL as dead so that we can tell we left it
1413 there only for backward compatibility. */
1414 DECL_DEAD_FOR_LOCAL (link) = 1;
1416 /* Keep track of what should of have happenned when we
1417 popped the binding. */
1418 if (outer_binding && BINDING_VALUE (outer_binding))
1419 DECL_SHADOWED_FOR_VAR (link)
1420 = BINDING_VALUE (outer_binding);
1422 /* Add it to the list of dead variables in the next
1423 outermost binding to that we can remove these when we
1424 leave that binding. */
1425 current_binding_level->level_chain->dead_vars_from_for
1426 = tree_cons (NULL_TREE, link,
1427 current_binding_level->level_chain->
1428 dead_vars_from_for);
1430 /* Although we don't pop the CPLUS_BINDING, we do clear
1431 its BINDING_LEVEL since the level is going away now. */
1432 BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1438 /* Remove the binding. */
1440 if (TREE_CODE (decl) == TREE_LIST)
1441 decl = TREE_VALUE (decl);
1443 pop_binding (DECL_NAME (decl), decl);
1444 else if (TREE_CODE (decl) == OVERLOAD)
1445 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1451 /* Remove declarations for any `for' variables from inner scopes
1452 that we kept around. */
1453 for (link = current_binding_level->dead_vars_from_for;
1454 link; link = TREE_CHAIN (link))
1455 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1457 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1458 for (link = current_binding_level->type_shadowed;
1459 link; link = TREE_CHAIN (link))
1460 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1462 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1463 for (link = current_binding_level->shadowed_labels;
1465 link = TREE_CHAIN (link))
1466 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1468 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1469 list if a `using' declaration put them there. The debugging
1470 back-ends won't understand OVERLOAD, so we remove them here.
1471 Because the BLOCK_VARS are (temporarily) shared with
1472 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1473 popped all the bindings. */
1478 for (d = &BLOCK_VARS (block); *d; )
1480 if (TREE_CODE (*d) == TREE_LIST)
1481 *d = TREE_CHAIN (*d);
1483 d = &TREE_CHAIN (*d);
1487 /* If the level being exited is the top level of a function,
1488 check over all the labels. */
1491 /* Since this is the top level block of a function, the vars are
1492 the function's parameters. Don't leave them in the BLOCK
1493 because they are found in the FUNCTION_DECL instead. */
1494 BLOCK_VARS (block) = 0;
1498 tmp = current_binding_level->keep;
1500 pop_binding_level ();
1502 DECL_INITIAL (current_function_decl) = block;
1504 current_binding_level->blocks
1505 = chainon (current_binding_level->blocks, block);
1507 /* If we did not make a block for the level just exited,
1508 any blocks made for inner levels
1509 (since they cannot be recorded as subblocks in that level)
1510 must be carried forward so they will later become subblocks
1511 of something else. */
1513 current_binding_level->blocks
1514 = chainon (current_binding_level->blocks, subblocks);
1516 /* Each and every BLOCK node created here in `poplevel' is important
1517 (e.g. for proper debugging information) so if we created one
1518 earlier, mark it as "used". */
1520 TREE_USED (block) = 1;
1522 /* Take care of compiler's internal binding structures. */
1528 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1531 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1532 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1535 block = poplevel (keep, reverse, functionbody);
1541 /* Delete the node BLOCK from the current binding level.
1542 This is used for the block inside a stmt expr ({...})
1543 so that the block can be reinserted where appropriate. */
1546 delete_block (tree block)
1549 if (current_binding_level->blocks == block)
1550 current_binding_level->blocks = TREE_CHAIN (block);
1551 for (t = current_binding_level->blocks; t;)
1553 if (TREE_CHAIN (t) == block)
1554 TREE_CHAIN (t) = TREE_CHAIN (block);
1558 TREE_CHAIN (block) = NULL_TREE;
1559 /* Clear TREE_USED which is always set by poplevel.
1560 The flag is set again if insert_block is called. */
1561 TREE_USED (block) = 0;
1564 /* Insert BLOCK at the end of the list of subblocks of the
1565 current binding level. This is used when a BIND_EXPR is expanded,
1566 to handle the BLOCK node inside the BIND_EXPR. */
1569 insert_block (tree block)
1571 TREE_USED (block) = 1;
1572 current_binding_level->blocks
1573 = chainon (current_binding_level->blocks, block);
1576 /* Set the BLOCK node for the innermost scope
1577 (the one we are currently in). */
1580 set_block (tree block ATTRIBUTE_UNUSED )
1582 /* The RTL expansion machinery requires us to provide this callback,
1583 but it is not applicable in function-at-a-time mode. */
1584 my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1587 /* Do a pushlevel for class declarations. */
1590 pushlevel_class (void)
1592 register struct cp_binding_level *newlevel;
1594 /* Reuse or create a struct for this binding level. */
1595 #if defined(DEBUG_BINDING_LEVELS)
1597 #else /* !defined(DEBUG_BINDING_LEVELS) */
1598 if (free_binding_level)
1599 #endif /* !defined(DEBUG_BINDING_LEVELS) */
1601 newlevel = free_binding_level;
1602 free_binding_level = free_binding_level->level_chain;
1605 newlevel = make_binding_level ();
1607 #if defined(DEBUG_BINDING_LEVELS)
1609 #endif /* defined(DEBUG_BINDING_LEVELS) */
1611 push_binding_level (newlevel, 0, 0);
1613 class_binding_level = current_binding_level;
1614 class_binding_level->parm_flag = 2;
1615 class_binding_level->this_class = current_class_type;
1618 /* ...and a poplevel for class declarations. */
1621 poplevel_class (void)
1623 register struct cp_binding_level *level = class_binding_level;
1626 my_friendly_assert (level != 0, 354);
1628 /* If we're leaving a toplevel class, don't bother to do the setting
1629 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1630 shouldn't even be used when current_class_type isn't set, and second,
1631 if we don't touch it here, we're able to use the cache effect if the
1632 next time we're entering a class scope, it is the same class. */
1633 if (current_class_depth != 1)
1635 struct cp_binding_level* b;
1637 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1638 for (shadowed = level->class_shadowed;
1640 shadowed = TREE_CHAIN (shadowed))
1641 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1643 /* Find the next enclosing class, and recreate
1644 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1645 b = level->level_chain;
1646 while (b && b->parm_flag != 2)
1650 for (shadowed = b->class_shadowed;
1652 shadowed = TREE_CHAIN (shadowed))
1656 t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1657 while (t && BINDING_LEVEL (t) != b)
1661 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1662 = BINDING_VALUE (t);
1666 /* Remember to save what IDENTIFIER's were bound in this scope so we
1667 can recover from cache misses. */
1669 previous_class_type = current_class_type;
1670 previous_class_values = class_binding_level->class_shadowed;
1672 for (shadowed = level->type_shadowed;
1674 shadowed = TREE_CHAIN (shadowed))
1675 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1677 /* Remove the bindings for all of the class-level declarations. */
1678 for (shadowed = level->class_shadowed;
1680 shadowed = TREE_CHAIN (shadowed))
1681 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1683 /* Now, pop out of the binding level which we created up in the
1684 `pushlevel_class' routine. */
1685 #if defined(DEBUG_BINDING_LEVELS)
1687 #endif /* defined(DEBUG_BINDING_LEVELS) */
1689 pop_binding_level ();
1692 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1693 for any names in enclosing classes. */
1696 clear_identifier_class_values (void)
1700 if (!class_binding_level)
1703 for (t = class_binding_level->class_shadowed;
1706 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1709 /* Returns nonzero if T is a virtual function table. */
1712 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1714 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1717 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1721 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1723 return (TREE_CODE (t) == TYPE_DECL
1724 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1725 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1728 /* Return the declarations that are members of the namespace NS. */
1731 cp_namespace_decls (tree ns)
1733 return NAMESPACE_LEVEL (ns)->names;
1736 struct walk_globals_data {
1737 walk_globals_pred p;
1742 /* Walk the vtable declarations in NAMESPACE. Whenever one is found
1743 for which P returns nonzero, call F with its address. If any call
1744 to F returns a nonzero value, return a nonzero value. */
1747 walk_vtables_r (tree namespace, void* data)
1749 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1750 walk_globals_fn f = wgd->f;
1751 void *d = wgd->data;
1752 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1755 for (; decl ; decl = TREE_CHAIN (decl))
1756 result |= (*f) (&decl, d);
1761 /* Walk the vtable declarations. Whenever one is found for which P
1762 returns nonzero, call F with its address. If any call to F
1763 returns a nonzero value, return a nonzero value. */
1765 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1767 struct walk_globals_data wgd;
1772 return walk_namespaces (walk_vtables_r, &wgd);
1775 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1776 itself, calling F for each. The DATA is passed to F as well. */
1779 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1782 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
1784 result |= (*f) (namespace, data);
1786 for (; current; current = TREE_CHAIN (current))
1787 result |= walk_namespaces_r (current, f, data);
1792 /* Walk all the namespaces, calling F for each. The DATA is passed to
1796 walk_namespaces (walk_namespaces_fn f, void* data)
1798 return walk_namespaces_r (global_namespace, f, data);
1801 /* Walk the global declarations in NAMESPACE. Whenever one is found
1802 for which P returns nonzero, call F with its address. If any call
1803 to F returns a nonzero value, return a nonzero value. */
1806 walk_globals_r (tree namespace, void* data)
1808 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1809 walk_globals_pred p = wgd->p;
1810 walk_globals_fn f = wgd->f;
1811 void *d = wgd->data;
1815 t = &NAMESPACE_LEVEL (namespace)->names;
1822 result |= (*f) (t, d);
1824 /* If F changed *T, then *T still points at the next item to
1827 t = &TREE_CHAIN (*t);
1833 /* Walk the global declarations. Whenever one is found for which P
1834 returns true, call F with its address. If any call to F
1835 returns true, return true. */
1838 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1840 struct walk_globals_data wgd;
1845 return walk_namespaces (walk_globals_r, &wgd);
1848 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1849 DATA is non-NULL, this is the last time we will call
1850 wrapup_global_declarations for this NAMESPACE. */
1853 wrapup_globals_for_namespace (tree namespace, void* data)
1855 tree globals = cp_namespace_decls (namespace);
1856 int len = NAMESPACE_LEVEL (namespace)->names_size;
1857 tree *vec = (tree *) alloca (sizeof (tree) * len);
1861 int last_time = (data != 0);
1863 if (last_time && namespace == global_namespace)
1864 /* Let compile_file handle the global namespace. */
1867 /* Process the decls in reverse order--earliest first.
1868 Put them into VEC from back to front, then take out from front. */
1869 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
1870 vec[len - i - 1] = decl;
1874 check_global_declarations (vec, len);
1878 /* Write out any globals that need to be output. */
1879 result = wrapup_global_declarations (vec, len);
1885 /* For debugging. */
1886 static int no_print_functions = 0;
1887 static int no_print_builtins = 0;
1890 print_binding_level (struct cp_binding_level* lvl)
1894 fprintf (stderr, " blocks=");
1895 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1896 if (lvl->tag_transparent)
1897 fprintf (stderr, " tag-transparent");
1898 if (lvl->more_cleanups_ok)
1899 fprintf (stderr, " more-cleanups-ok");
1900 if (lvl->have_cleanups)
1901 fprintf (stderr, " have-cleanups");
1902 fprintf (stderr, "\n");
1905 fprintf (stderr, " names:\t");
1906 /* We can probably fit 3 names to a line? */
1907 for (t = lvl->names; t; t = TREE_CHAIN (t))
1909 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1911 if (no_print_builtins
1912 && (TREE_CODE (t) == TYPE_DECL)
1913 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1916 /* Function decls tend to have longer names. */
1917 if (TREE_CODE (t) == FUNCTION_DECL)
1924 fprintf (stderr, "\n\t");
1927 print_node_brief (stderr, "", t, 0);
1928 if (t == error_mark_node)
1932 fprintf (stderr, "\n");
1936 fprintf (stderr, " tags:\t");
1938 for (t = lvl->tags; t; t = TREE_CHAIN (t))
1940 if (TREE_PURPOSE (t) == NULL_TREE)
1942 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1949 fprintf (stderr, "\n\t");
1952 if (TREE_PURPOSE (t) == NULL_TREE)
1954 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1955 fprintf (stderr, ">");
1957 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1958 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1961 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1962 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1963 fprintf (stderr, ">");
1967 fprintf (stderr, "\n");
1969 if (lvl->class_shadowed)
1971 fprintf (stderr, " class-shadowed:");
1972 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1974 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1976 fprintf (stderr, "\n");
1978 if (lvl->type_shadowed)
1980 fprintf (stderr, " type-shadowed:");
1981 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1983 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1985 fprintf (stderr, "\n");
1990 print_other_binding_stack (struct cp_binding_level *stack)
1992 struct cp_binding_level *level;
1993 for (level = stack; level != global_binding_level; level = level->level_chain)
1995 fprintf (stderr, "binding level ");
1996 fprintf (stderr, HOST_PTR_PRINTF, level);
1997 fprintf (stderr, "\n");
1998 print_binding_level (level);
2003 print_binding_stack (void)
2005 struct cp_binding_level *b;
2006 fprintf (stderr, "current_binding_level=");
2007 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
2008 fprintf (stderr, "\nclass_binding_level=");
2009 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2010 fprintf (stderr, "\nglobal_binding_level=");
2011 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
2012 fprintf (stderr, "\n");
2013 if (class_binding_level)
2015 for (b = class_binding_level; b; b = b->level_chain)
2016 if (b == current_binding_level)
2019 b = class_binding_level;
2021 b = current_binding_level;
2024 b = current_binding_level;
2025 print_other_binding_stack (b);
2026 fprintf (stderr, "global:\n");
2027 print_binding_level (global_binding_level);
2030 /* Namespace binding access routines: The namespace_bindings field of
2031 the identifier is polymorphic, with three possible values:
2032 NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
2033 indicating the BINDING_VALUE of global_namespace. */
2035 /* Check whether the a binding for the name to scope is known.
2036 Assumes that the bindings of the name are already a list
2037 of bindings. Returns the binding found, or NULL_TREE. */
2040 find_binding (tree name, tree scope)
2042 tree iter, prev = NULL_TREE;
2044 scope = ORIGINAL_NAMESPACE (scope);
2046 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2047 iter = TREE_CHAIN (iter))
2049 my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2050 if (BINDING_SCOPE (iter) == scope)
2052 /* Move binding found to the front of the list, so
2053 subsequent lookups will find it faster. */
2056 TREE_CHAIN (prev) = TREE_CHAIN (iter);
2057 TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2058 IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2067 /* Always returns a binding for name in scope. If the
2068 namespace_bindings is not a list, convert it to one first.
2069 If no binding is found, make a new one. */
2072 binding_for_name (tree name, tree scope)
2074 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2077 scope = ORIGINAL_NAMESPACE (scope);
2079 if (b && TREE_CODE (b) != CPLUS_BINDING)
2081 /* Get rid of optimization for global scope. */
2082 IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2083 BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2084 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2086 if (b && (result = find_binding (name, scope)))
2088 /* Not found, make a new one. */
2089 result = make_node (CPLUS_BINDING);
2090 TREE_CHAIN (result) = b;
2091 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2092 BINDING_SCOPE (result) = scope;
2093 BINDING_TYPE (result) = NULL_TREE;
2094 BINDING_VALUE (result) = NULL_TREE;
2098 /* Return the binding value for name in scope, considering that
2099 namespace_binding may or may not be a list of CPLUS_BINDINGS. */
2102 namespace_binding (tree name, tree scope)
2104 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2107 if (scope == NULL_TREE)
2108 scope = global_namespace;
2109 if (TREE_CODE (b) != CPLUS_BINDING)
2110 return (scope == global_namespace) ? b : NULL_TREE;
2111 name = find_binding (name,scope);
2112 if (name == NULL_TREE)
2114 return BINDING_VALUE (name);
2117 /* Set the binding value for name in scope. If modifying the binding
2118 of global_namespace is attempted, try to optimize it. */
2121 set_namespace_binding (tree name, tree scope, tree val)
2125 if (scope == NULL_TREE)
2126 scope = global_namespace;
2128 if (scope == global_namespace)
2130 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2131 if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2133 IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2137 b = binding_for_name (name, scope);
2138 BINDING_VALUE (b) = val;
2141 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2142 select a name that is unique to this compilation unit. */
2145 push_namespace (tree name)
2149 int implicit_use = 0;
2151 if (!global_namespace)
2153 /* This must be ::. */
2154 my_friendly_assert (name == get_identifier ("::"), 377);
2159 /* The name of anonymous namespace is unique for the translation
2161 if (!anonymous_namespace_name)
2162 anonymous_namespace_name = get_file_function_name ('N');
2163 name = anonymous_namespace_name;
2164 d = IDENTIFIER_NAMESPACE_VALUE (name);
2166 /* Reopening anonymous namespace. */
2172 /* Check whether this is an extended namespace definition. */
2173 d = IDENTIFIER_NAMESPACE_VALUE (name);
2174 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2177 if (DECL_NAMESPACE_ALIAS (d))
2179 error ("namespace alias `%D' not allowed here, assuming `%D'",
2180 d, DECL_NAMESPACE_ALIAS (d));
2181 d = DECL_NAMESPACE_ALIAS (d);
2188 /* Make a new namespace, binding the name to it. */
2189 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2190 /* The global namespace is not pushed, and the global binding
2191 level is set elsewhere. */
2194 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2197 declare_namespace_level ();
2198 NAMESPACE_LEVEL (d) = current_binding_level;
2202 resume_binding_level (NAMESPACE_LEVEL (d));
2205 do_using_directive (d);
2206 /* Enter the name space. */
2207 current_namespace = d;
2210 /* Pop from the scope of the current namespace. */
2213 pop_namespace (void)
2215 my_friendly_assert (current_namespace != global_namespace, 20010801);
2216 current_namespace = CP_DECL_CONTEXT (current_namespace);
2217 /* The binding level is not popped, as it might be re-opened later. */
2218 suspend_binding_level ();
2221 /* Push into the scope of the namespace NS, even if it is deeply
2222 nested within another namespace. */
2225 push_nested_namespace (tree ns)
2227 if (ns == global_namespace)
2228 push_to_top_level ();
2231 push_nested_namespace (CP_DECL_CONTEXT (ns));
2232 push_namespace (DECL_NAME (ns));
2236 /* Pop back from the scope of the namespace NS, which was previously
2237 entered with push_nested_namespace. */
2240 pop_nested_namespace (tree ns)
2242 while (ns != global_namespace)
2245 ns = CP_DECL_CONTEXT (ns);
2248 pop_from_top_level ();
2252 /* Subroutines for reverting temporarily to top-level for instantiation
2253 of templates and such. We actually need to clear out the class- and
2254 local-value slots of all identifiers, so that only the global values
2255 are at all visible. Simply setting current_binding_level to the global
2256 scope isn't enough, because more binding levels may be pushed. */
2257 struct saved_scope *scope_chain;
2260 store_bindings (tree names, tree old_bindings)
2263 tree search_bindings = old_bindings;
2265 for (t = names; t; t = TREE_CHAIN (t))
2267 tree binding, t1, id;
2269 if (TREE_CODE (t) == TREE_LIST)
2270 id = TREE_PURPOSE (t);
2275 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2276 we have no IDENTIFIER_BINDING if we have left the class
2277 scope, but cached the class-level declarations. */
2278 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2281 for (t1 = search_bindings; t1; t1 = TREE_CHAIN (t1))
2282 if (TREE_VEC_ELT (t1, 0) == id)
2285 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2286 binding = make_tree_vec (4);
2287 TREE_VEC_ELT (binding, 0) = id;
2288 TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
2289 TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
2290 TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
2291 IDENTIFIER_BINDING (id) = NULL_TREE;
2292 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2293 TREE_CHAIN (binding) = old_bindings;
2294 old_bindings = binding;
2298 return old_bindings;
2302 maybe_push_to_top_level (int pseudo)
2304 struct saved_scope *s;
2305 struct cp_binding_level *b;
2309 s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2311 b = scope_chain ? current_binding_level : 0;
2313 /* If we're in the middle of some function, save our state. */
2317 push_function_context_to (NULL_TREE);
2322 old_bindings = NULL_TREE;
2323 if (scope_chain && previous_class_type)
2324 old_bindings = store_bindings (previous_class_values, old_bindings);
2326 /* Have to include global_binding_level, because class-level decls
2327 aren't listed anywhere useful. */
2328 for (; b; b = b->level_chain)
2332 /* Template IDs are inserted into the global level. If they were
2333 inserted into namespace level, finish_file wouldn't find them
2334 when doing pending instantiations. Therefore, don't stop at
2335 namespace level, but continue until :: . */
2336 if (b == global_binding_level || (pseudo && b->template_parms_p))
2339 old_bindings = store_bindings (b->names, old_bindings);
2340 /* We also need to check class_shadowed to save class-level type
2341 bindings, since pushclass doesn't fill in b->names. */
2342 if (b->parm_flag == 2)
2343 old_bindings = store_bindings (b->class_shadowed, old_bindings);
2345 /* Unwind type-value slots back to top level. */
2346 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2347 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2349 s->prev = scope_chain;
2350 s->old_bindings = old_bindings;
2352 s->need_pop_function_context = need_pop;
2353 s->function_decl = current_function_decl;
2354 s->last_parms = last_function_parms;
2355 s->check_access = flag_access_control;
2358 current_function_decl = NULL_TREE;
2359 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2360 current_lang_name = lang_name_cplusplus;
2361 current_namespace = global_namespace;
2365 push_to_top_level (void)
2367 maybe_push_to_top_level (0);
2371 pop_from_top_level (void)
2373 struct saved_scope *s = scope_chain;
2376 /* Clear out class-level bindings cache. */
2377 if (previous_class_type)
2378 invalidate_class_lookup_cache ();
2380 current_lang_base = 0;
2382 scope_chain = s->prev;
2383 for (t = s->old_bindings; t; t = TREE_CHAIN (t))
2385 tree id = TREE_VEC_ELT (t, 0);
2387 SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
2388 IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
2389 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2392 /* If we were in the middle of compiling a function, restore our
2394 if (s->need_pop_function_context)
2395 pop_function_context_from (NULL_TREE);
2396 current_function_decl = s->function_decl;
2397 last_function_parms = s->last_parms;
2400 /* Push a definition of struct, union or enum tag "name".
2401 into binding_level "b". "type" should be the type node,
2402 We assume that the tag "name" is not already defined.
2404 Note that the definition may really be just a forward reference.
2405 In that case, the TYPE_SIZE will be a NULL_TREE.
2407 C++ gratuitously puts all these tags in the name space. */
2409 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2410 record the shadowed value for this binding contour. TYPE is
2411 the type that ID maps to. */
2414 set_identifier_type_value_with_scope (tree id,
2416 struct cp_binding_level* b)
2418 if (!b->namespace_p)
2420 /* Shadow the marker, not the real thing, so that the marker
2421 gets restored later. */
2422 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2424 = tree_cons (id, old_type_value, b->type_shadowed);
2428 tree binding = binding_for_name (id, current_namespace);
2429 BINDING_TYPE (binding) = type;
2430 /* Store marker instead of real type. */
2431 type = global_type_node;
2433 SET_IDENTIFIER_TYPE_VALUE (id, type);
2436 /* As set_identifier_type_value_with_scope, but using current_binding_level. */
2439 set_identifier_type_value (tree id, tree type)
2441 set_identifier_type_value_with_scope (id, type, current_binding_level);
2444 /* Return the type associated with id. */
2447 identifier_type_value (tree id)
2449 /* There is no type with that name, anywhere. */
2450 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2452 /* This is not the type marker, but the real thing. */
2453 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2454 return REAL_IDENTIFIER_TYPE_VALUE (id);
2455 /* Have to search for it. It must be on the global level, now.
2456 Ask lookup_name not to return non-types. */
2457 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2459 return TREE_TYPE (id);
2463 /* Pop off extraneous binding levels left over due to syntax errors.
2465 We don't pop past namespaces, as they might be valid. */
2468 pop_everything (void)
2470 #ifdef DEBUG_BINDING_LEVELS
2471 fprintf (stderr, "XXX entering pop_everything ()\n");
2473 while (!toplevel_bindings_p ())
2475 if (current_binding_level->parm_flag == 2)
2476 pop_nested_class ();
2480 #ifdef DEBUG_BINDING_LEVELS
2481 fprintf (stderr, "XXX leaving pop_everything ()\n");
2485 /* The type TYPE is being declared. If it is a class template, or a
2486 specialization of a class template, do any processing required and
2487 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
2488 being declared a friend. B is the binding level at which this TYPE
2491 Returns the TYPE_DECL for TYPE, which may have been altered by this
2495 maybe_process_template_type_declaration (tree type,
2497 struct cp_binding_level* b)
2499 tree decl = TYPE_NAME (type);
2501 if (processing_template_parmlist)
2502 /* You can't declare a new template type in a template parameter
2503 list. But, you can declare a non-template type:
2505 template <class A*> struct S;
2507 is a forward-declaration of `A'. */
2511 maybe_check_template_type (type);
2513 my_friendly_assert (IS_AGGR_TYPE (type)
2514 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2517 if (processing_template_decl)
2519 /* This may change after the call to
2520 push_template_decl_real, but we want the original value. */
2521 tree name = DECL_NAME (decl);
2523 decl = push_template_decl_real (decl, globalize);
2524 /* If the current binding level is the binding level for the
2525 template parameters (see the comment in
2526 begin_template_parm_list) and the enclosing level is a class
2527 scope, and we're not looking at a friend, push the
2528 declaration of the member class into the class scope. In the
2529 friend case, push_template_decl will already have put the
2530 friend into global scope, if appropriate. */
2531 if (TREE_CODE (type) != ENUMERAL_TYPE
2532 && !globalize && b->template_parms_p
2533 && b->level_chain->parm_flag == 2)
2535 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2536 /* Put this tag on the list of tags for the class, since
2537 that won't happen below because B is not the class
2538 binding level, but is instead the pseudo-global level. */
2539 b->level_chain->tags =
2540 tree_cons (name, type, b->level_chain->tags);
2541 if (!COMPLETE_TYPE_P (current_class_type))
2543 maybe_add_class_template_decl_list (current_class_type,
2544 type, /*friend_p=*/0);
2545 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2554 /* In C++, you don't have to write `struct S' to refer to `S'; you
2555 can just use `S'. We accomplish this by creating a TYPE_DECL as
2556 if the user had written `typedef struct S S'. Create and return
2557 the TYPE_DECL for TYPE. */
2560 create_implicit_typedef (tree name, tree type)
2564 decl = build_decl (TYPE_DECL, name, type);
2565 DECL_ARTIFICIAL (decl) = 1;
2566 /* There are other implicit type declarations, like the one *within*
2567 a class that allows you to write `S::S'. We must distinguish
2569 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2570 TYPE_NAME (type) = decl;
2575 /* Remember a local name for name-mangling purposes. */
2578 push_local_name (tree decl)
2584 VARRAY_TREE_INIT (local_names, 8, "local_names");
2586 name = DECL_NAME (decl);
2588 nelts = VARRAY_ACTIVE_SIZE (local_names);
2589 for (i = 0; i < nelts; i++)
2591 t = VARRAY_TREE (local_names, i);
2592 if (DECL_NAME (t) == name)
2594 if (!DECL_LANG_SPECIFIC (decl))
2595 retrofit_lang_decl (decl);
2596 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2597 if (DECL_LANG_SPECIFIC (t))
2598 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2600 DECL_DISCRIMINATOR (decl) = 1;
2602 VARRAY_TREE (local_names, i) = decl;
2607 VARRAY_PUSH_TREE (local_names, decl);
2610 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2611 Normally put it into the inner-most non-tag-transparent scope,
2612 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2613 The latter is needed for implicit declarations. */
2616 pushtag (tree name, tree type, int globalize)
2618 register struct cp_binding_level *b;
2620 b = current_binding_level;
2621 while (b->tag_transparent
2622 || (b->parm_flag == 2
2624 /* We may be defining a new type in the initializer
2625 of a static member variable. We allow this when
2626 not pedantic, and it is particularly useful for
2627 type punning via an anonymous union. */
2628 || COMPLETE_TYPE_P (b->this_class))))
2631 b->tags = tree_cons (name, type, b->tags);
2635 /* Do C++ gratuitous typedefing. */
2636 if (IDENTIFIER_TYPE_VALUE (name) != type)
2638 register tree d = NULL_TREE;
2640 tree context = TYPE_CONTEXT (type);
2644 tree cs = current_scope ();
2648 else if (cs != NULL_TREE && TYPE_P (cs))
2649 /* When declaring a friend class of a local class, we want
2650 to inject the newly named class into the scope
2651 containing the local class, not the namespace scope. */
2652 context = decl_function_context (get_type_decl (cs));
2655 context = current_namespace;
2657 if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2658 || b->parm_flag == 2)
2661 if (current_lang_name == lang_name_java)
2662 TYPE_FOR_JAVA (type) = 1;
2664 d = create_implicit_typedef (name, type);
2665 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2667 set_identifier_type_value_with_scope (name, type, b);
2669 d = maybe_process_template_type_declaration (type,
2672 if (b->parm_flag == 2)
2674 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2675 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2676 class. But if it's a member template class, we
2677 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2679 finish_member_declaration (d);
2681 pushdecl_class_level (d);
2684 d = pushdecl_with_scope (d, b);
2686 /* FIXME what if it gets a name from typedef? */
2687 if (ANON_AGGRNAME_P (name))
2688 DECL_IGNORED_P (d) = 1;
2690 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2692 /* If this is a local class, keep track of it. We need this
2693 information for name-mangling, and so that it is possible to find
2694 all function definitions in a translation unit in a convenient
2695 way. (It's otherwise tricky to find a member function definition
2696 it's only pointed to from within a local class.) */
2697 if (TYPE_CONTEXT (type)
2698 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2699 && !processing_template_decl)
2700 VARRAY_PUSH_TREE (local_classes, type);
2702 if (b->parm_flag == 2)
2704 if (!COMPLETE_TYPE_P (current_class_type))
2706 maybe_add_class_template_decl_list (current_class_type,
2707 type, /*friend_p=*/0);
2708 CLASSTYPE_TAGS (current_class_type) = b->tags;
2713 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2714 /* Use the canonical TYPE_DECL for this node. */
2715 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2718 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2719 will be the tagged type we just added to the current
2720 binding level. This fake NULL-named TYPE_DECL node helps
2721 dwarfout.c to know when it needs to output a
2722 representation of a tagged type, and it also gives us a
2723 convenient place to record the "scope start" address for
2726 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2727 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2731 /* Counter used to create anonymous type names. */
2733 static GTY(()) int anon_cnt;
2735 /* Return an IDENTIFIER which can be used as a name for
2736 anonymous structs and unions. */
2739 make_anon_name (void)
2743 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2744 return get_identifier (buf);
2747 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2748 This keeps dbxout from getting confused. */
2751 clear_anon_tags (void)
2753 register struct cp_binding_level *b;
2755 static int last_cnt = 0;
2757 /* Fast out if no new anon names were declared. */
2758 if (last_cnt == anon_cnt)
2761 b = current_binding_level;
2762 while (b->tag_transparent)
2767 /* A NULL purpose means we have already processed all tags
2768 from here to the end of the list. */
2769 if (TREE_PURPOSE (tags) == NULL_TREE)
2771 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2772 TREE_PURPOSE (tags) = NULL_TREE;
2773 tags = TREE_CHAIN (tags);
2775 last_cnt = anon_cnt;
2778 /* Subroutine of duplicate_decls: return truthvalue of whether
2779 or not types of these decls match.
2781 For C++, we must compare the parameter list so that `int' can match
2782 `int&' in a parameter position, but `int&' is not confused with
2786 decls_match (tree newdecl, tree olddecl)
2790 if (newdecl == olddecl)
2793 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2794 /* If the two DECLs are not even the same kind of thing, we're not
2795 interested in their types. */
2798 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2800 tree f1 = TREE_TYPE (newdecl);
2801 tree f2 = TREE_TYPE (olddecl);
2802 tree p1 = TYPE_ARG_TYPES (f1);
2803 tree p2 = TYPE_ARG_TYPES (f2);
2805 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2806 && ! (DECL_EXTERN_C_P (newdecl)
2807 && DECL_EXTERN_C_P (olddecl)))
2810 if (TREE_CODE (f1) != TREE_CODE (f2))
2813 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2815 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2816 && (DECL_BUILT_IN (olddecl)
2817 #ifndef NO_IMPLICIT_EXTERN_C
2818 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2819 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2823 types_match = self_promoting_args_p (p1);
2824 if (p1 == void_list_node)
2825 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2827 #ifndef NO_IMPLICIT_EXTERN_C
2828 else if (p1 == NULL_TREE
2829 && (DECL_EXTERN_C_P (olddecl)
2830 && DECL_IN_SYSTEM_HEADER (olddecl)
2831 && !DECL_CLASS_SCOPE_P (olddecl))
2832 && (DECL_EXTERN_C_P (newdecl)
2833 && DECL_IN_SYSTEM_HEADER (newdecl)
2834 && !DECL_CLASS_SCOPE_P (newdecl)))
2836 types_match = self_promoting_args_p (p2);
2837 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2841 types_match = compparms (p1, p2);
2846 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2848 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2849 DECL_TEMPLATE_PARMS (olddecl)))
2852 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2853 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2856 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2859 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2860 DECL_TEMPLATE_RESULT (newdecl));
2864 if (TREE_TYPE (newdecl) == error_mark_node)
2865 types_match = TREE_TYPE (olddecl) == error_mark_node;
2866 else if (TREE_TYPE (olddecl) == NULL_TREE)
2867 types_match = TREE_TYPE (newdecl) == NULL_TREE;
2868 else if (TREE_TYPE (newdecl) == NULL_TREE)
2871 types_match = comptypes (TREE_TYPE (newdecl),
2872 TREE_TYPE (olddecl),
2873 COMPARE_REDECLARATION);
2879 /* If NEWDECL is `static' and an `extern' was seen previously,
2880 warn about it. OLDDECL is the previous declaration.
2882 Note that this does not apply to the C++ case of declaring
2883 a variable `extern const' and then later `const'.
2885 Don't complain about built-in functions, since they are beyond
2886 the user's control. */
2889 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2891 static const char *const explicit_extern_static_warning
2892 = "`%D' was declared `extern' and later `static'";
2893 static const char *const implicit_extern_static_warning
2894 = "`%D' was declared implicitly `extern' and later `static'";
2898 if (TREE_CODE (newdecl) == TYPE_DECL
2899 || TREE_CODE (newdecl) == TEMPLATE_DECL
2900 || TREE_CODE (newdecl) == CONST_DECL)
2903 /* Don't get confused by static member functions; that's a different
2905 if (TREE_CODE (newdecl) == FUNCTION_DECL
2906 && DECL_STATIC_FUNCTION_P (newdecl))
2909 /* If the old declaration was `static', or the new one isn't, then
2910 then everything is OK. */
2911 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2914 /* It's OK to declare a builtin function as `static'. */
2915 if (TREE_CODE (olddecl) == FUNCTION_DECL
2916 && DECL_ARTIFICIAL (olddecl))
2919 name = DECL_ASSEMBLER_NAME (newdecl);
2920 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2921 ? implicit_extern_static_warning
2922 : explicit_extern_static_warning, newdecl);
2923 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2926 /* Handle when a new declaration NEWDECL has the same name as an old
2927 one OLDDECL in the same binding contour. Prints an error message
2930 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2931 Otherwise, return 0. */
2934 duplicate_decls (tree newdecl, tree olddecl)
2936 unsigned olddecl_uid = DECL_UID (olddecl);
2937 int olddecl_friend = 0, types_match = 0;
2938 int new_defines_function = 0;
2940 if (newdecl == olddecl)
2943 types_match = decls_match (newdecl, olddecl);
2945 /* If either the type of the new decl or the type of the old decl is an
2946 error_mark_node, then that implies that we have already issued an
2947 error (earlier) for some bogus type specification, and in that case,
2948 it is rather pointless to harass the user with yet more error message
2949 about the same declaration, so just pretend the types match here. */
2950 if (TREE_TYPE (newdecl) == error_mark_node
2951 || TREE_TYPE (olddecl) == error_mark_node)
2954 if (DECL_P (olddecl)
2955 && TREE_CODE (newdecl) == FUNCTION_DECL
2956 && TREE_CODE (olddecl) == FUNCTION_DECL
2957 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2959 if (DECL_DECLARED_INLINE_P (newdecl)
2960 && DECL_UNINLINABLE (newdecl)
2961 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2962 /* Already warned elsewhere. */;
2963 else if (DECL_DECLARED_INLINE_P (olddecl)
2964 && DECL_UNINLINABLE (olddecl)
2965 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2966 /* Already warned. */;
2967 else if (DECL_DECLARED_INLINE_P (newdecl)
2968 && DECL_UNINLINABLE (olddecl)
2969 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2971 warning_with_decl (newdecl,
2972 "function `%s' redeclared as inline");
2973 warning_with_decl (olddecl,
2974 "previous declaration of function `%s' with attribute noinline");
2976 else if (DECL_DECLARED_INLINE_P (olddecl)
2977 && DECL_UNINLINABLE (newdecl)
2978 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2980 warning_with_decl (newdecl,
2981 "function `%s' redeclared with attribute noinline");
2982 warning_with_decl (olddecl,
2983 "previous declaration of function `%s' was inline");
2987 /* Check for redeclaration and other discrepancies. */
2988 if (TREE_CODE (olddecl) == FUNCTION_DECL
2989 && DECL_ARTIFICIAL (olddecl))
2991 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2993 /* Avoid warnings redeclaring anticipated built-ins. */
2994 if (DECL_ANTICIPATED (olddecl))
2997 /* If you declare a built-in or predefined function name as static,
2998 the old definition is overridden, but optionally warn this was a
2999 bad choice of name. */
3000 if (! TREE_PUBLIC (newdecl))
3003 warning ("shadowing %s function `%#D'",
3004 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3006 /* Discard the old built-in function. */
3009 /* If the built-in is not ansi, then programs can override
3010 it even globally without an error. */
3011 else if (! DECL_BUILT_IN (olddecl))
3012 warning ("library function `%#D' redeclared as non-function `%#D'",
3016 error ("declaration of `%#D'", newdecl);
3017 error ("conflicts with built-in declaration `%#D'",
3022 else if (!types_match)
3024 /* Avoid warnings redeclaring anticipated built-ins. */
3025 if (DECL_ANTICIPATED (olddecl))
3026 ; /* Do nothing yet. */
3027 else if ((DECL_EXTERN_C_P (newdecl)
3028 && DECL_EXTERN_C_P (olddecl))
3029 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3030 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3032 /* A near match; override the builtin. */
3034 if (TREE_PUBLIC (newdecl))
3036 warning ("new declaration `%#D'", newdecl);
3037 warning ("ambiguates built-in declaration `%#D'",
3040 else if (warn_shadow)
3041 warning ("shadowing %s function `%#D'",
3042 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3046 /* Discard the old built-in function. */
3049 /* Replace the old RTL to avoid problems with inlining. */
3050 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3053 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
3055 /* If a builtin function is redeclared as `static', merge
3056 the declarations, but make the original one static. */
3057 DECL_THIS_STATIC (olddecl) = 1;
3058 TREE_PUBLIC (olddecl) = 0;
3060 /* Make the old declaration consistent with the new one so
3061 that all remnants of the builtin-ness of this function
3062 will be banished. */
3063 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3064 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3067 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3069 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3070 && TREE_CODE (newdecl) != TYPE_DECL
3071 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3072 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3073 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3074 && TREE_CODE (olddecl) != TYPE_DECL
3075 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3076 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3079 /* We do nothing special here, because C++ does such nasty
3080 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3081 get shadowed, and know that if we need to find a TYPE_DECL
3082 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3083 slot of the identifier. */
3087 if ((TREE_CODE (newdecl) == FUNCTION_DECL
3088 && DECL_FUNCTION_TEMPLATE_P (olddecl))
3089 || (TREE_CODE (olddecl) == FUNCTION_DECL
3090 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3093 error ("`%#D' redeclared as different kind of symbol", newdecl);
3094 if (TREE_CODE (olddecl) == TREE_LIST)
3095 olddecl = TREE_VALUE (olddecl);
3096 cp_error_at ("previous declaration of `%#D'", olddecl);
3098 /* New decl is completely inconsistent with the old one =>
3099 tell caller to replace the old one. */
3103 else if (!types_match)
3105 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3106 /* These are certainly not duplicate declarations; they're
3107 from different scopes. */
3110 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3112 /* The name of a class template may not be declared to refer to
3113 any other template, class, function, object, namespace, value,
3114 or type in the same scope. */
3115 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3116 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3118 error ("declaration of template `%#D'", newdecl);
3119 cp_error_at ("conflicts with previous declaration `%#D'",
3122 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3123 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3124 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3125 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3126 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3127 DECL_TEMPLATE_PARMS (olddecl))
3128 /* Template functions can be disambiguated by
3130 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3131 TREE_TYPE (TREE_TYPE (olddecl))))
3133 error ("new declaration `%#D'", newdecl);
3134 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3138 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3140 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3142 error ("declaration of C function `%#D' conflicts with",
3144 cp_error_at ("previous declaration `%#D' here", olddecl);
3146 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3147 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3149 error ("new declaration `%#D'", newdecl);
3150 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3156 /* Already complained about this, so don't do so again. */
3157 else if (current_class_type == NULL_TREE
3158 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3160 error ("conflicting types for `%#D'", newdecl);
3161 cp_error_at ("previous declaration as `%#D'", olddecl);
3164 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3165 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3166 && (!DECL_TEMPLATE_INFO (newdecl)
3167 || (DECL_TI_TEMPLATE (newdecl)
3168 != DECL_TI_TEMPLATE (olddecl))))
3169 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3170 && (!DECL_TEMPLATE_INFO (olddecl)
3171 || (DECL_TI_TEMPLATE (olddecl)
3172 != DECL_TI_TEMPLATE (newdecl))))))
3173 /* It's OK to have a template specialization and a non-template
3174 with the same type, or to have specializations of two
3175 different templates with the same type. Note that if one is a
3176 specialization, and the other is an instantiation of the same
3177 template, that we do not exit at this point. That situation
3178 can occur if we instantiate a template class, and then
3179 specialize one of its methods. This situation is valid, but
3180 the declarations must be merged in the usual way. */
3182 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3183 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3184 && !DECL_USE_TEMPLATE (newdecl))
3185 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3186 && !DECL_USE_TEMPLATE (olddecl))))
3187 /* One of the declarations is a template instantiation, and the
3188 other is not a template at all. That's OK. */
3190 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3191 && DECL_NAMESPACE_ALIAS (newdecl)
3192 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3193 /* Redeclaration of namespace alias, ignore it. */
3197 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3200 error (errmsg, newdecl);
3201 if (DECL_NAME (olddecl) != NULL_TREE)
3202 cp_error_at ((DECL_INITIAL (olddecl)
3203 && namespace_bindings_p ())
3204 ? "`%#D' previously defined here"
3205 : "`%#D' previously declared here", olddecl);
3208 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3209 && DECL_INITIAL (olddecl) != NULL_TREE
3210 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3211 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3213 /* Prototype decl follows defn w/o prototype. */
3214 cp_warning_at ("prototype for `%#D'", newdecl);
3215 cp_warning_at ("follows non-prototype definition here", olddecl);
3217 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3218 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3220 /* extern "C" int foo ();
3221 int foo () { bar (); }
3223 if (current_lang_depth () == 0)
3224 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3227 cp_error_at ("previous declaration of `%#D' with %L linkage",
3228 olddecl, DECL_LANGUAGE (olddecl));
3229 error ("conflicts with new declaration with %L linkage",
3230 DECL_LANGUAGE (newdecl));
3234 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3236 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3238 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3239 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3242 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3243 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3245 for (; t1 && t1 != void_list_node;
3246 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3247 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3249 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3252 pedwarn ("default argument given for parameter %d of `%#D'",
3254 cp_pedwarn_at ("after previous specification in `%#D'",
3259 error ("default argument given for parameter %d of `%#D'",
3261 cp_error_at ("after previous specification in `%#D'",
3266 if (DECL_DECLARED_INLINE_P (newdecl)
3267 && ! DECL_DECLARED_INLINE_P (olddecl)
3268 && TREE_ADDRESSABLE (olddecl) && warn_inline)
3270 warning ("`%#D' was used before it was declared inline",
3272 cp_warning_at ("previous non-inline declaration here",
3278 /* Do not merge an implicit typedef with an explicit one. In:
3282 typedef class A A __attribute__ ((foo));
3284 the attribute should apply only to the typedef. */
3285 if (TREE_CODE (olddecl) == TYPE_DECL
3286 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3287 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3290 /* If new decl is `static' and an `extern' was seen previously,
3292 warn_extern_redeclared_static (newdecl, olddecl);
3294 /* We have committed to returning 1 at this point. */
3295 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3297 /* Now that functions must hold information normally held
3298 by field decls, there is extra work to do so that
3299 declaration information does not get destroyed during
3301 if (DECL_VINDEX (olddecl))
3302 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3303 if (DECL_CONTEXT (olddecl))
3304 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3305 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3306 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3307 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3308 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3309 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3310 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3311 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3312 SET_OVERLOADED_OPERATOR_CODE
3313 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3314 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3316 /* Optionally warn about more than one declaration for the same
3317 name, but don't warn about a function declaration followed by a
3319 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3320 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3321 /* Don't warn about extern decl followed by definition. */
3322 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3323 /* Don't warn about friends, let add_friend take care of it. */
3324 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3326 warning ("redundant redeclaration of `%D' in same scope", newdecl);
3327 cp_warning_at ("previous declaration of `%D'", olddecl);
3331 /* Deal with C++: must preserve virtual function table size. */
3332 if (TREE_CODE (olddecl) == TYPE_DECL)
3334 register tree newtype = TREE_TYPE (newdecl);
3335 register tree oldtype = TREE_TYPE (olddecl);
3337 if (newtype != error_mark_node && oldtype != error_mark_node
3338 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3339 CLASSTYPE_FRIEND_CLASSES (newtype)
3340 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3342 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3345 /* Copy all the DECL_... slots specified in the new decl
3346 except for any that we copy here from the old type. */
3347 DECL_ATTRIBUTES (newdecl)
3348 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3350 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3352 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3353 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3354 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3355 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3357 /* If the new declaration is a definition, update the file and
3358 line information on the declaration. */
3359 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3360 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3362 DECL_SOURCE_LOCATION (olddecl)
3363 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3364 = DECL_SOURCE_LOCATION (newdecl);
3372 /* Automatically handles default parameters. */
3373 tree oldtype = TREE_TYPE (olddecl);
3376 /* Merge the data types specified in the two decls. */
3377 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3379 /* If merge_types produces a non-typedef type, just use the old type. */
3380 if (TREE_CODE (newdecl) == TYPE_DECL
3381 && newtype == DECL_ORIGINAL_TYPE (newdecl))
3384 if (TREE_CODE (newdecl) == VAR_DECL)
3386 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3387 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3390 /* Do this after calling `merge_types' so that default
3391 parameters don't confuse us. */
3392 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3393 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3394 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3396 TREE_TYPE (newdecl) = build_exception_variant (newtype,
3397 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3398 TREE_TYPE (olddecl) = build_exception_variant (newtype,
3399 TYPE_RAISES_EXCEPTIONS (oldtype));
3401 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3402 && DECL_SOURCE_LINE (olddecl) != 0
3404 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3405 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3407 error ("declaration of `%F' throws different exceptions",
3409 cp_error_at ("than previous declaration `%F'", olddecl);
3412 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3414 /* Lay the type out, unless already done. */
3415 if (! same_type_p (newtype, oldtype)
3416 && TREE_TYPE (newdecl) != error_mark_node
3417 && !(processing_template_decl && uses_template_parms (newdecl)))
3418 layout_type (TREE_TYPE (newdecl));
3420 if ((TREE_CODE (newdecl) == VAR_DECL
3421 || TREE_CODE (newdecl) == PARM_DECL
3422 || TREE_CODE (newdecl) == RESULT_DECL
3423 || TREE_CODE (newdecl) == FIELD_DECL
3424 || TREE_CODE (newdecl) == TYPE_DECL)
3425 && !(processing_template_decl && uses_template_parms (newdecl)))
3426 layout_decl (newdecl, 0);
3428 /* Merge the type qualifiers. */
3429 if (TREE_READONLY (newdecl))
3430 TREE_READONLY (olddecl) = 1;
3431 if (TREE_THIS_VOLATILE (newdecl))
3432 TREE_THIS_VOLATILE (olddecl) = 1;
3434 /* Merge the initialization information. */
3435 if (DECL_INITIAL (newdecl) == NULL_TREE
3436 && DECL_INITIAL (olddecl) != NULL_TREE)
3438 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3439 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3440 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3441 && DECL_LANG_SPECIFIC (newdecl)
3442 && DECL_LANG_SPECIFIC (olddecl))
3443 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3446 /* Merge the section attribute.
3447 We want to issue an error if the sections conflict but that must be
3448 done later in decl_attributes since we are called before attributes
3450 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3451 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3453 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3455 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3456 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3457 DECL_NO_LIMIT_STACK (newdecl)
3458 |= DECL_NO_LIMIT_STACK (olddecl);
3459 /* Keep the old RTL. */
3460 COPY_DECL_RTL (olddecl, newdecl);
3462 else if (TREE_CODE (newdecl) == VAR_DECL
3463 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3465 /* Keep the old RTL. We cannot keep the old RTL if the old
3466 declaration was for an incomplete object and the new
3467 declaration is not since many attributes of the RTL will
3469 COPY_DECL_RTL (olddecl, newdecl);
3472 /* If cannot merge, then use the new type and qualifiers,
3473 and don't preserve the old rtl. */
3476 /* Clean out any memory we had of the old declaration. */
3477 tree oldstatic = value_member (olddecl, static_aggregates);
3479 TREE_VALUE (oldstatic) = error_mark_node;
3481 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3482 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3483 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3484 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3487 /* Merge the storage class information. */
3488 merge_weak (newdecl, olddecl);
3490 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3491 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3492 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3493 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3494 if (! DECL_EXTERNAL (olddecl))
3495 DECL_EXTERNAL (newdecl) = 0;
3497 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3499 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3500 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3501 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3502 DECL_TEMPLATE_INSTANTIATED (newdecl)
3503 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3504 /* Don't really know how much of the language-specific
3505 values we should copy from old to new. */
3506 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3507 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
3508 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3509 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3510 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3511 DECL_INITIALIZED_IN_CLASS_P (newdecl)
3512 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3513 olddecl_friend = DECL_FRIEND_P (olddecl);
3515 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3516 if (TREE_CODE (newdecl) == FUNCTION_DECL
3517 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3519 DECL_BEFRIENDING_CLASSES (newdecl)
3520 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3521 DECL_BEFRIENDING_CLASSES (olddecl));
3522 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3526 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3528 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3529 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3531 /* If newdecl is not a specialization, then it is not a
3532 template-related function at all. And that means that we
3533 shoud have exited above, returning 0. */
3534 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3537 if (TREE_USED (olddecl))
3538 /* From [temp.expl.spec]:
3540 If a template, a member template or the member of a class
3541 template is explicitly specialized then that
3542 specialization shall be declared before the first use of
3543 that specialization that would cause an implicit
3544 instantiation to take place, in every translation unit in
3545 which such a use occurs. */
3546 error ("explicit specialization of %D after first use",
3549 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3551 /* [temp.expl.spec/14] We don't inline explicit specialization
3552 just because the primary template says so. */
3556 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3557 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3559 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3561 /* If either decl says `inline', this fn is inline, unless
3562 its definition was passed already. */
3563 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3564 DECL_INLINE (olddecl) = 1;
3565 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3567 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3568 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3571 /* Preserve abstractness on cloned [cd]tors. */
3572 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3576 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3577 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3578 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3580 if (! types_match || new_defines_function)
3582 /* These need to be copied so that the names are available.
3583 Note that if the types do match, we'll preserve inline
3584 info and other bits, but if not, we won't. */
3585 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3586 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3588 if (new_defines_function)
3589 /* If defining a function declared with other language
3590 linkage, use the previously declared language linkage. */
3591 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3592 else if (types_match)
3594 /* If redeclaring a builtin function, and not a definition,
3595 it stays built in. */
3596 if (DECL_BUILT_IN (olddecl))
3598 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3599 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3600 /* If we're keeping the built-in definition, keep the rtl,
3601 regardless of declaration matches. */
3602 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3605 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3607 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3608 /* Don't clear out the arguments if we're redefining a function. */
3609 if (DECL_ARGUMENTS (olddecl))
3610 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3613 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3614 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3616 /* Now preserve various other info from the definition. */
3617 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3618 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3619 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3620 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3622 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3626 function_size = sizeof (struct tree_decl);
3628 memcpy ((char *) olddecl + sizeof (struct tree_common),
3629 (char *) newdecl + sizeof (struct tree_common),
3630 function_size - sizeof (struct tree_common));
3632 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3634 /* If newdecl is a template instantiation, it is possible that
3635 the following sequence of events has occurred:
3637 o A friend function was declared in a class template. The
3638 class template was instantiated.
3640 o The instantiation of the friend declaration was
3641 recorded on the instantiation list, and is newdecl.
3643 o Later, however, instantiate_class_template called pushdecl
3644 on the newdecl to perform name injection. But, pushdecl in
3645 turn called duplicate_decls when it discovered that another
3646 declaration of a global function with the same name already
3649 o Here, in duplicate_decls, we decided to clobber newdecl.
3651 If we're going to do that, we'd better make sure that
3652 olddecl, and not newdecl, is on the list of
3653 instantiations so that if we try to do the instantiation
3654 again we won't get the clobbered declaration. */
3656 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3657 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3659 for (; decls; decls = TREE_CHAIN (decls))
3660 if (TREE_VALUE (decls) == newdecl)
3661 TREE_VALUE (decls) = olddecl;
3666 memcpy ((char *) olddecl + sizeof (struct tree_common),
3667 (char *) newdecl + sizeof (struct tree_common),
3668 sizeof (struct tree_decl) - sizeof (struct tree_common)
3669 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3672 DECL_UID (olddecl) = olddecl_uid;
3674 DECL_FRIEND_P (olddecl) = 1;
3676 /* NEWDECL contains the merged attribute lists.
3677 Update OLDDECL to be the same. */
3678 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3683 /* Record a decl-node X as belonging to the current lexical scope.
3684 Check for errors (such as an incompatible declaration for the same
3685 name already seen in the same scope).
3687 Returns either X or an old decl for the same name.
3688 If an old decl is returned, it may have been smashed
3689 to agree with what X says. */
3696 int need_new_binding;
3698 /* We shouldn't be calling pushdecl when we're generating RTL for a
3699 function that we already did semantic analysis on previously. */
3700 my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3703 need_new_binding = 1;
3705 if (DECL_TEMPLATE_PARM_P (x))
3706 /* Template parameters have no context; they are not X::T even
3707 when declared within a class or namespace. */
3711 if (current_function_decl && x != current_function_decl
3712 /* A local declaration for a function doesn't constitute
3714 && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3715 /* A local declaration for an `extern' variable is in the
3716 scope of the current namespace, not the current
3718 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3719 && !DECL_CONTEXT (x))
3720 DECL_CONTEXT (x) = current_function_decl;
3722 /* If this is the declaration for a namespace-scope function,
3723 but the declaration itself is in a local scope, mark the
3725 if (TREE_CODE (x) == FUNCTION_DECL
3726 && DECL_NAMESPACE_SCOPE_P (x)
3727 && current_function_decl
3728 && x != current_function_decl)
3729 DECL_LOCAL_FUNCTION_P (x) = 1;
3732 name = DECL_NAME (x);
3735 int different_binding_level = 0;
3737 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3738 name = TREE_OPERAND (name, 0);
3740 /* In case this decl was explicitly namespace-qualified, look it
3741 up in its namespace context. */
3742 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3743 && namespace_bindings_p ())
3744 t = namespace_binding (name, DECL_CONTEXT (x));
3746 t = lookup_name_current_level (name);
3748 /* [basic.link] If there is a visible declaration of an entity
3749 with linkage having the same name and type, ignoring entities
3750 declared outside the innermost enclosing namespace scope, the
3751 block scope declaration declares that same entity and
3752 receives the linkage of the previous declaration. */
3753 if (! t && current_function_decl && x != current_function_decl
3754 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3755 && DECL_EXTERNAL (x))
3757 /* Look in block scope. */
3758 t = IDENTIFIER_VALUE (name);
3759 /* Or in the innermost namespace. */
3761 t = namespace_binding (name, DECL_CONTEXT (x));
3762 /* Does it have linkage? Note that if this isn't a DECL, it's an
3763 OVERLOAD, which is OK. */
3764 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3767 different_binding_level = 1;
3770 /* If we are declaring a function, and the result of name-lookup
3771 was an OVERLOAD, look for an overloaded instance that is
3772 actually the same as the function we are declaring. (If
3773 there is one, we have to merge our declaration with the
3774 previous declaration.) */
3775 if (t && TREE_CODE (t) == OVERLOAD)
3779 if (TREE_CODE (x) == FUNCTION_DECL)
3780 for (match = t; match; match = OVL_NEXT (match))
3782 if (decls_match (OVL_CURRENT (match), x))
3786 /* Just choose one. */
3790 t = OVL_CURRENT (match);
3795 if (t == error_mark_node)
3797 /* error_mark_node is 0 for a while during initialization! */
3799 cp_error_at ("`%#D' used prior to declaration", x);
3801 else if (t != NULL_TREE)
3803 if (different_binding_level)
3805 if (decls_match (x, t))
3806 /* The standard only says that the local extern
3807 inherits linkage from the previous decl; in
3808 particular, default args are not shared. It would
3809 be nice to propagate inlining info, though. FIXME. */
3810 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3812 else if (TREE_CODE (t) == PARM_DECL)
3814 if (DECL_CONTEXT (t) == NULL_TREE)
3815 /* This is probaby caused by too many errors, but calling
3816 abort will say that if errors have occurred. */
3819 /* Check for duplicate params. */
3820 if (duplicate_decls (x, t))
3823 else if ((DECL_EXTERN_C_FUNCTION_P (x)
3824 || DECL_FUNCTION_TEMPLATE_P (x))
3825 && is_overloaded_fn (t))
3826 /* Don't do anything just yet. */;
3827 else if (t == wchar_decl_node)
3829 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3830 pedwarn ("redeclaration of `wchar_t' as `%T'",
3833 /* Throw away the redeclaration. */
3836 else if (TREE_CODE (t) != TREE_CODE (x))
3838 if (duplicate_decls (x, t))
3841 else if (duplicate_decls (x, t))
3843 if (TREE_CODE (t) == TYPE_DECL)
3844 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3845 else if (TREE_CODE (t) == FUNCTION_DECL)
3846 check_default_args (t);
3850 else if (DECL_MAIN_P (x))
3852 /* A redeclaration of main, but not a duplicate of the
3857 This function shall not be overloaded. */
3858 cp_error_at ("invalid redeclaration of `%D'", t);
3859 error ("as `%D'", x);
3860 /* We don't try to push this declaration since that
3866 check_template_shadow (x);
3868 /* If this is a function conjured up by the backend, massage it
3869 so it looks friendly. */
3870 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3872 retrofit_lang_decl (x);
3873 SET_DECL_LANGUAGE (x, lang_c);
3876 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3878 t = push_overloaded_decl (x, PUSH_LOCAL);
3881 if (!namespace_bindings_p ())
3882 /* We do not need to create a binding for this name;
3883 push_overloaded_decl will have already done so if
3885 need_new_binding = 0;
3887 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3889 t = push_overloaded_decl (x, PUSH_GLOBAL);
3891 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3895 /* If declaring a type as a typedef, copy the type (unless we're
3896 at line 0), and install this TYPE_DECL as the new type's typedef
3897 name. See the extensive comment in ../c-decl.c (pushdecl). */
3898 if (TREE_CODE (x) == TYPE_DECL)
3900 tree type = TREE_TYPE (x);
3901 if (DECL_SOURCE_LINE (x) == 0)
3903 if (TYPE_NAME (type) == 0)
3904 TYPE_NAME (type) = x;
3906 else if (type != error_mark_node && TYPE_NAME (type) != x
3907 /* We don't want to copy the type when all we're
3908 doing is making a TYPE_DECL for the purposes of
3910 && (!TYPE_NAME (type)
3911 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3913 DECL_ORIGINAL_TYPE (x) = type;
3914 type = build_type_copy (type);
3915 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3916 TYPE_NAME (type) = x;
3917 TREE_TYPE (x) = type;
3920 if (type != error_mark_node
3922 && TYPE_IDENTIFIER (type))
3923 set_identifier_type_value_with_scope (DECL_NAME (x), type,
3924 current_binding_level);
3928 /* Multiple external decls of the same identifier ought to match.
3930 We get warnings about inline functions where they are defined.
3931 We get warnings about other functions from push_overloaded_decl.
3933 Avoid duplicate warnings where they are used. */
3934 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3938 decl = IDENTIFIER_NAMESPACE_VALUE (name);
3939 if (decl && TREE_CODE (decl) == OVERLOAD)
3940 decl = OVL_FUNCTION (decl);
3942 if (decl && decl != error_mark_node
3943 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3944 /* If different sort of thing, we already gave an error. */
3945 && TREE_CODE (decl) == TREE_CODE (x)
3946 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3948 pedwarn ("type mismatch with previous external decl", x);
3949 cp_pedwarn_at ("previous external decl of `%#D'", decl);
3953 /* This name is new in its binding level.
3954 Install the new declaration and return it. */
3955 if (namespace_bindings_p ())
3957 /* Install a global value. */
3959 /* If the first global decl has external linkage,
3960 warn if we later see static one. */
3961 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3962 TREE_PUBLIC (name) = 1;
3964 /* Bind the name for the entity. */
3965 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3967 && (TREE_CODE (x) == TYPE_DECL
3968 || TREE_CODE (x) == VAR_DECL
3969 || TREE_CODE (x) == ALIAS_DECL
3970 || TREE_CODE (x) == NAMESPACE_DECL
3971 || TREE_CODE (x) == CONST_DECL
3972 || TREE_CODE (x) == TEMPLATE_DECL))
3973 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3975 /* Don't forget if the function was used via an implicit decl. */
3976 if (IDENTIFIER_IMPLICIT_DECL (name)
3977 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3980 /* Don't forget if its address was taken in that way. */
3981 if (IDENTIFIER_IMPLICIT_DECL (name)
3982 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3983 TREE_ADDRESSABLE (x) = 1;
3985 /* Warn about mismatches against previous implicit decl. */
3986 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3987 /* If this real decl matches the implicit, don't complain. */
3988 && ! (TREE_CODE (x) == FUNCTION_DECL
3989 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3991 ("`%D' was previously implicitly declared to return `int'", x);
3993 /* If new decl is `static' and an `extern' was seen previously,
3995 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3996 warn_extern_redeclared_static (x, t);
4000 /* Here to install a non-global value. */
4001 tree oldlocal = IDENTIFIER_VALUE (name);
4002 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
4004 if (need_new_binding)
4006 push_local_binding (name, x, 0);
4007 /* Because push_local_binding will hook X on to the
4008 current_binding_level's name list, we don't want to
4009 do that again below. */
4010 need_new_binding = 0;
4013 /* If this is a TYPE_DECL, push it into the type value slot. */
4014 if (TREE_CODE (x) == TYPE_DECL)
4015 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
4016 current_binding_level);
4018 /* Clear out any TYPE_DECL shadowed by a namespace so that
4019 we won't think this is a type. The C struct hack doesn't
4020 go through namespaces. */
4021 if (TREE_CODE (x) == NAMESPACE_DECL)
4022 set_identifier_type_value_with_scope (name, NULL_TREE,
4023 current_binding_level);
4030 && TREE_CODE (oldlocal) == VAR_DECL
4031 && DECL_DEAD_FOR_LOCAL (oldlocal))
4032 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
4034 if (oldlocal == NULL_TREE)
4035 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
4038 /* If this is an extern function declaration, see if we
4039 have a global definition or declaration for the function. */
4040 if (oldlocal == NULL_TREE
4041 && DECL_EXTERNAL (x)
4042 && oldglobal != NULL_TREE
4043 && TREE_CODE (x) == FUNCTION_DECL
4044 && TREE_CODE (oldglobal) == FUNCTION_DECL)
4046 /* We have one. Their types must agree. */
4047 if (decls_match (x, oldglobal))
4051 warning ("extern declaration of `%#D' doesn't match", x);
4052 cp_warning_at ("global declaration `%#D'", oldglobal);
4055 /* If we have a local external declaration,
4056 and no file-scope declaration has yet been seen,
4057 then if we later have a file-scope decl it must not be static. */
4058 if (oldlocal == NULL_TREE
4059 && oldglobal == NULL_TREE
4060 && DECL_EXTERNAL (x)
4062 TREE_PUBLIC (name) = 1;
4064 /* Warn if shadowing an argument at the top level of the body. */
4065 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
4066 /* Inline decls shadow nothing. */
4067 && !DECL_FROM_INLINE (x)
4068 && TREE_CODE (oldlocal) == PARM_DECL)
4072 /* Don't complain if it's from an enclosing function. */
4073 if (DECL_CONTEXT (oldlocal) == current_function_decl
4074 && TREE_CODE (x) != PARM_DECL)
4076 /* Go to where the parms should be and see if we find
4078 struct cp_binding_level *b = current_binding_level->level_chain;
4080 /* Skip the ctor/dtor cleanup level. */
4084 if (b->parm_flag == 1)
4086 error ("declaration of `%#D' shadows a parameter",
4092 if (warn_shadow && !err)
4093 shadow_warning ("a parameter", name, oldlocal);
4096 /* Maybe warn if shadowing something else. */
4097 else if (warn_shadow && !DECL_EXTERNAL (x)
4098 /* No shadow warnings for internally generated vars. */
4099 && ! DECL_ARTIFICIAL (x)
4100 /* No shadow warnings for vars made for inlining. */
4101 && ! DECL_FROM_INLINE (x))
4103 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4104 && current_class_ptr
4105 && !TREE_STATIC (name))
4106 warning ("declaration of `%s' shadows a member of `this'",
4107 IDENTIFIER_POINTER (name));
4108 else if (oldlocal != NULL_TREE
4109 && TREE_CODE (oldlocal) == VAR_DECL)
4110 shadow_warning ("a previous local", name, oldlocal);
4111 else if (oldglobal != NULL_TREE
4112 && TREE_CODE (oldglobal) == VAR_DECL)
4113 /* XXX shadow warnings in outer-more namespaces */
4114 shadow_warning ("a global declaration", name, oldglobal);
4118 if (TREE_CODE (x) == FUNCTION_DECL)
4119 check_default_args (x);
4121 if (TREE_CODE (x) == VAR_DECL)
4122 maybe_register_incomplete_var (x);
4125 if (need_new_binding)
4126 add_decl_to_level (x,
4127 DECL_NAMESPACE_SCOPE_P (x)
4128 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4129 : current_binding_level);
4134 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4135 caller to set DECL_CONTEXT properly. */
4138 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4140 register struct cp_binding_level *b;
4141 tree function_decl = current_function_decl;
4143 current_function_decl = NULL_TREE;
4144 if (level->parm_flag == 2)
4146 b = class_binding_level;
4147 class_binding_level = level;
4148 pushdecl_class_level (x);
4149 class_binding_level = b;
4153 b = current_binding_level;
4154 current_binding_level = level;
4156 current_binding_level = b;
4158 current_function_decl = function_decl;
4162 /* Like pushdecl, only it places X in the current namespace,
4166 pushdecl_namespace_level (tree x)
4168 register struct cp_binding_level *b = current_binding_level;
4171 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4173 /* Now, the type_shadowed stack may screw us. Munge it so it does
4175 if (TREE_CODE (x) == TYPE_DECL)
4177 tree name = DECL_NAME (x);
4179 tree *ptr = (tree *)0;
4180 for (; b != global_binding_level; b = b->level_chain)
4182 tree shadowed = b->type_shadowed;
4183 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4184 if (TREE_PURPOSE (shadowed) == name)
4186 ptr = &TREE_VALUE (shadowed);
4187 /* Can't break out of the loop here because sometimes
4188 a binding level will have duplicate bindings for
4189 PT names. It's gross, but I haven't time to fix it. */
4192 newval = TREE_TYPE (x);
4193 if (ptr == (tree *)0)
4195 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4196 up here if this is changed to an assertion. --KR */
4197 SET_IDENTIFIER_TYPE_VALUE (name, newval);
4207 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4211 pushdecl_top_level (tree x)
4213 push_to_top_level ();
4214 x = pushdecl_namespace_level (x);
4215 pop_from_top_level ();
4219 /* Make the declaration of X appear in CLASS scope. */
4222 pushdecl_class_level (tree x)
4226 /* Get the name of X. */
4227 if (TREE_CODE (x) == OVERLOAD)
4228 name = DECL_NAME (get_first_fn (x));
4230 name = DECL_NAME (x);
4234 push_class_level_binding (name, x);
4235 if (TREE_CODE (x) == TYPE_DECL)
4236 set_identifier_type_value (name, TREE_TYPE (x));
4238 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4240 /* If X is an anonymous aggregate, all of its members are
4241 treated as if they were members of the class containing the
4242 aggregate, for naming purposes. */
4245 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4246 pushdecl_class_level (f);
4250 /* Enter DECL into the symbol table, if that's appropriate. Returns
4251 DECL, or a modified version thereof. */
4254 maybe_push_decl (tree decl)
4256 tree type = TREE_TYPE (decl);
4258 /* Add this decl to the current binding level, but not if it comes
4259 from another scope, e.g. a static member variable. TEM may equal
4260 DECL or it may be a previous decl of the same name. */
4261 if (decl == error_mark_node
4262 || (TREE_CODE (decl) != PARM_DECL
4263 && DECL_CONTEXT (decl) != NULL_TREE
4264 /* Definitions of namespace members outside their namespace are
4266 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4267 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4268 || TREE_CODE (type) == UNKNOWN_TYPE
4269 /* The declaration of a template specialization does not affect
4270 the functions available for overload resolution, so we do not
4272 || (TREE_CODE (decl) == FUNCTION_DECL
4273 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4276 return pushdecl (decl);
4279 /* Make the declaration(s) of X appear in CLASS scope
4280 under the name NAME. */
4283 push_class_level_binding (tree name, tree x)
4286 /* The class_binding_level will be NULL if x is a template
4287 parameter name in a member template. */
4288 if (!class_binding_level)
4291 /* Make sure that this new member does not have the same name
4292 as a template parameter. */
4293 if (TYPE_BEING_DEFINED (current_class_type))
4294 check_template_shadow (x);
4296 /* If this declaration shadows a declaration from an enclosing
4297 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4298 we leave this class. Record the shadowed declaration here. */
4299 binding = IDENTIFIER_BINDING (name);
4301 && ((TREE_CODE (x) == OVERLOAD
4302 && BINDING_VALUE (binding)
4303 && is_overloaded_fn (BINDING_VALUE (binding)))
4304 || INHERITED_VALUE_BINDING_P (binding)))
4309 /* If the old binding was from a base class, and was for a tag
4310 name, slide it over to make room for the new binding. The
4311 old binding is still visible if explicitly qualified with a
4313 if (INHERITED_VALUE_BINDING_P (binding)
4314 && BINDING_VALUE (binding)
4315 && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4316 && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4317 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4319 old_decl = BINDING_TYPE (binding);
4320 BINDING_TYPE (binding) = BINDING_VALUE (binding);
4321 BINDING_VALUE (binding) = NULL_TREE;
4322 INHERITED_VALUE_BINDING_P (binding) = 0;
4325 old_decl = BINDING_VALUE (binding);
4327 /* Find the previous binding of name on the class-shadowed
4328 list, and update it. */
4329 for (shadow = class_binding_level->class_shadowed;
4331 shadow = TREE_CHAIN (shadow))
4332 if (TREE_PURPOSE (shadow) == name
4333 && TREE_TYPE (shadow) == old_decl)
4335 BINDING_VALUE (binding) = x;
4336 INHERITED_VALUE_BINDING_P (binding) = 0;
4337 TREE_TYPE (shadow) = x;
4338 IDENTIFIER_CLASS_VALUE (name) = x;
4343 /* If we didn't replace an existing binding, put the binding on the
4344 stack of bindings for the identifier, and update the shadowed list. */
4345 if (push_class_binding (name, x))
4347 class_binding_level->class_shadowed
4348 = tree_cons (name, NULL,
4349 class_binding_level->class_shadowed);
4350 /* Record the value we are binding NAME to so that we can know
4351 what to pop later. */
4352 TREE_TYPE (class_binding_level->class_shadowed) = x;
4356 /* Insert another USING_DECL into the current binding level, returning
4357 this declaration. If this is a redeclaration, do nothing, and
4358 return NULL_TREE if this not in namespace scope (in namespace
4359 scope, a using decl might extend any previous bindings). */
4362 push_using_decl (tree scope, tree name)
4366 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4367 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4368 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4369 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4372 return namespace_bindings_p () ? decl : NULL_TREE;
4373 decl = build_lang_decl (USING_DECL, name, void_type_node);
4374 DECL_INITIAL (decl) = scope;
4375 TREE_CHAIN (decl) = current_binding_level->usings;
4376 current_binding_level->usings = decl;
4380 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4381 changed (i.e. there was already a directive), or the fresh
4382 TREE_LIST otherwise. */
4385 push_using_directive (tree used)
4387 tree ud = current_binding_level->using_directives;
4388 tree iter, ancestor;
4390 /* Check if we already have this. */
4391 if (purpose_member (used, ud) != NULL_TREE)
4394 /* Recursively add all namespaces used. */
4395 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4396 push_using_directive (TREE_PURPOSE (iter));
4398 ancestor = namespace_ancestor (current_decl_namespace (), used);
4399 ud = current_binding_level->using_directives;
4400 ud = tree_cons (used, ancestor, ud);
4401 current_binding_level->using_directives = ud;
4405 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4406 other definitions already in place. We get around this by making
4407 the value of the identifier point to a list of all the things that
4408 want to be referenced by that name. It is then up to the users of
4409 that name to decide what to do with that list.
4411 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4412 DECL_TEMPLATE_RESULT. It is dealt with the same way.
4414 FLAGS is a bitwise-or of the following values:
4415 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4417 PUSH_USING: DECL is being pushed as the result of a using
4420 The value returned may be a previous declaration if we guessed wrong
4421 about what language DECL should belong to (C or C++). Otherwise,
4422 it's always DECL (and never something that's not a _DECL). */
4425 push_overloaded_decl (tree decl, int flags)
4427 tree name = DECL_NAME (decl);
4430 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4433 old = namespace_binding (name, DECL_CONTEXT (decl));
4435 old = lookup_name_current_level (name);
4439 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4441 tree t = TREE_TYPE (old);
4442 if (IS_AGGR_TYPE (t) && warn_shadow
4443 && (! DECL_IN_SYSTEM_HEADER (decl)
4444 || ! DECL_IN_SYSTEM_HEADER (old)))
4445 warning ("`%#D' hides constructor for `%#T'", decl, t);
4448 else if (is_overloaded_fn (old))
4452 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4454 tree fn = OVL_CURRENT (tmp);
4456 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4457 && !(flags & PUSH_USING)
4458 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4459 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4460 error ("`%#D' conflicts with previous using declaration `%#D'",
4463 if (duplicate_decls (decl, fn))
4467 else if (old == error_mark_node)
4468 /* Ignore the undefined symbol marker. */
4472 cp_error_at ("previous non-function declaration `%#D'", old);
4473 error ("conflicts with function declaration `%#D'", decl);
4478 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4480 if (old && TREE_CODE (old) != OVERLOAD)
4481 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4483 new_binding = ovl_cons (decl, old);
4484 if (flags & PUSH_USING)
4485 OVL_USED (new_binding) = 1;
4488 /* NAME is not ambiguous. */
4492 set_namespace_binding (name, current_namespace, new_binding);
4495 /* We only create an OVERLOAD if there was a previous binding at
4496 this level, or if decl is a template. In the former case, we
4497 need to remove the old binding and replace it with the new
4498 binding. We must also run through the NAMES on the binding
4499 level where the name was bound to update the chain. */
4501 if (TREE_CODE (new_binding) == OVERLOAD && old)
4505 for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4507 d = &TREE_CHAIN (*d))
4509 || (TREE_CODE (*d) == TREE_LIST
4510 && TREE_VALUE (*d) == old))
4512 if (TREE_CODE (*d) == TREE_LIST)
4513 /* Just replace the old binding with the new. */
4514 TREE_VALUE (*d) = new_binding;
4516 /* Build a TREE_LIST to wrap the OVERLOAD. */
4517 *d = tree_cons (NULL_TREE, new_binding,
4520 /* And update the CPLUS_BINDING node. */
4521 BINDING_VALUE (IDENTIFIER_BINDING (name))
4526 /* We should always find a previous binding in this case. */
4530 /* Install the new binding. */
4531 push_local_binding (name, new_binding, flags);
4537 /* Generate an implicit declaration for identifier FUNCTIONID
4538 as a function of type int (). Print a warning if appropriate. */
4541 implicitly_declare (tree functionid)
4545 /* We used to reuse an old implicit decl here,
4546 but this loses with inline functions because it can clobber
4547 the saved decl chains. */
4548 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4550 DECL_EXTERNAL (decl) = 1;
4551 TREE_PUBLIC (decl) = 1;
4553 /* ISO standard says implicit declarations are in the innermost block.
4554 So we record the decl in the standard fashion. */
4556 rest_of_decl_compilation (decl, NULL, 0, 0);
4559 /* Only one warning per identifier. */
4560 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4562 pedwarn ("implicit declaration of function `%#D'", decl);
4565 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4570 /* Return zero if the declaration NEWDECL is valid
4571 when the declaration OLDDECL (assumed to be for the same name)
4572 has already been seen.
4573 Otherwise return an error message format string with a %s
4574 where the identifier should go. */
4577 redeclaration_error_message (tree newdecl, tree olddecl)
4579 if (TREE_CODE (newdecl) == TYPE_DECL)
4581 /* Because C++ can put things into name space for free,
4582 constructs like "typedef struct foo { ... } foo"
4583 would look like an erroneous redeclaration. */
4584 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4587 return "redefinition of `%#D'";
4589 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4591 /* If this is a pure function, its olddecl will actually be
4592 the original initialization to `0' (which we force to call
4593 abort()). Don't complain about redefinition in this case. */
4594 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4597 /* If both functions come from different namespaces, this is not
4598 a redeclaration - this is a conflict with a used function. */
4599 if (DECL_NAMESPACE_SCOPE_P (olddecl)
4600 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4601 return "`%D' conflicts with used function";
4603 /* We'll complain about linkage mismatches in
4604 warn_extern_redeclared_static. */
4606 /* Defining the same name twice is no good. */
4607 if (DECL_INITIAL (olddecl) != NULL_TREE
4608 && DECL_INITIAL (newdecl) != NULL_TREE)
4610 if (DECL_NAME (olddecl) == NULL_TREE)
4611 return "`%#D' not declared in class";
4613 return "redefinition of `%#D'";
4617 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4619 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4620 && (DECL_TEMPLATE_RESULT (newdecl)
4621 != DECL_TEMPLATE_RESULT (olddecl))
4622 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4623 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4624 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4625 && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4626 && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4627 return "redefinition of `%#D'";
4630 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4632 /* Objects declared at top level: */
4633 /* If at least one is a reference, it's ok. */
4634 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4636 /* Reject two definitions. */
4637 return "redefinition of `%#D'";
4641 /* Objects declared with block scope: */
4642 /* Reject two definitions, and reject a definition
4643 together with an external reference. */
4644 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4645 return "redeclaration of `%#D'";
4650 /* Create a new label, named ID. */
4653 make_label_decl (tree id, int local_p)
4657 decl = build_decl (LABEL_DECL, id, void_type_node);
4659 /* Make sure every label has an rtx. */
4662 DECL_CONTEXT (decl) = current_function_decl;
4663 DECL_MODE (decl) = VOIDmode;
4664 C_DECLARED_LABEL_FLAG (decl) = local_p;
4666 /* Say where one reference is to the label, for the sake of the
4667 error if it is not defined. */
4668 DECL_SOURCE_LINE (decl) = lineno;
4669 DECL_SOURCE_FILE (decl) = input_filename;
4671 /* Record the fact that this identifier is bound to this label. */
4672 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4677 /* Record this label on the list of used labels so that we can check
4678 at the end of the function to see whether or not the label was
4679 actually defined, and so we can check when the label is defined whether
4680 this use is valid. */
4683 use_label (tree decl)
4685 if (named_label_uses == NULL
4686 || named_label_uses->names_in_scope != current_binding_level->names
4687 || named_label_uses->label_decl != decl)
4689 struct named_label_use_list *new_ent;
4690 new_ent = ((struct named_label_use_list *)
4691 ggc_alloc (sizeof (struct named_label_use_list)));
4692 new_ent->label_decl = decl;
4693 new_ent->names_in_scope = current_binding_level->names;
4694 new_ent->binding_level = current_binding_level;
4695 new_ent->lineno_o_goto = lineno;
4696 new_ent->filename_o_goto = input_filename;
4697 new_ent->next = named_label_uses;
4698 named_label_uses = new_ent;
4702 /* Look for a label named ID in the current function. If one cannot
4703 be found, create one. (We keep track of used, but undefined,
4704 labels, and complain about them at the end of a function.) */
4707 lookup_label (tree id)
4710 struct named_label_list *ent;
4712 /* You can't use labels at global scope. */
4713 if (current_function_decl == NULL_TREE)
4715 error ("label `%s' referenced outside of any function",
4716 IDENTIFIER_POINTER (id));
4720 /* See if we've already got this label. */
4721 decl = IDENTIFIER_LABEL_VALUE (id);
4722 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4725 /* Record this label on the list of labels used in this function.
4726 We do this before calling make_label_decl so that we get the
4727 IDENTIFIER_LABEL_VALUE before the new label is declared. */
4728 ent = ((struct named_label_list *)
4729 ggc_alloc_cleared (sizeof (struct named_label_list)));
4730 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4731 ent->next = named_labels;
4734 /* We need a new label. */
4735 decl = make_label_decl (id, /*local_p=*/0);
4737 /* Now fill in the information we didn't have before. */
4738 ent->label_decl = decl;
4743 /* Declare a local label named ID. */
4746 declare_local_label (tree id)
4750 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4751 this scope we can restore the old value of
4752 IDENTIFIER_TYPE_VALUE. */
4753 current_binding_level->shadowed_labels
4754 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4755 current_binding_level->shadowed_labels);
4756 /* Look for the label. */
4757 decl = make_label_decl (id, /*local_p=*/1);
4758 /* Now fill in the information we didn't have before. */
4759 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4764 /* Returns nonzero if it is ill-formed to jump past the declaration of
4765 DECL. Returns 2 if it's also a real problem. */
4768 decl_jump_unsafe (tree decl)
4770 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4773 if (DECL_INITIAL (decl) == NULL_TREE
4774 && pod_type_p (TREE_TYPE (decl)))
4777 /* This is really only important if we're crossing an initialization.
4778 The POD stuff is just pedantry; why should it matter if the class
4779 contains a field of pointer to member type? */
4780 if (DECL_INITIAL (decl)
4781 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4786 /* Check that a single previously seen jump to a newly defined label
4787 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4788 the jump context; NAMES are the names in scope in LEVEL at the jump
4789 context; FILE and LINE are the source position of the jump or 0. */
4792 check_previous_goto_1 (tree decl,
4793 struct cp_binding_level* level,
4800 struct cp_binding_level *b = current_binding_level;
4801 for (; b; b = b->level_chain)
4803 tree new_decls = b->names;
4804 tree old_decls = (b == level ? names : NULL_TREE);
4805 for (; new_decls != old_decls;
4806 new_decls = TREE_CHAIN (new_decls))
4808 int problem = decl_jump_unsafe (new_decls);
4815 pedwarn ("jump to label `%D'", decl);
4817 pedwarn ("jump to case label");
4820 pedwarn_with_file_and_line (file, line, " from here");
4825 cp_error_at (" crosses initialization of `%#D'",
4828 cp_pedwarn_at (" enters scope of non-POD `%#D'",
4834 if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
4839 pedwarn ("jump to label `%D'", decl);
4841 pedwarn ("jump to case label");
4844 pedwarn_with_file_and_line (file, line, " from here");
4847 if (b->is_try_scope)
4848 error (" enters try block");
4850 error (" enters catch block");
4857 check_previous_goto (struct named_label_use_list* use)
4859 check_previous_goto_1 (use->label_decl, use->binding_level,
4860 use->names_in_scope, use->filename_o_goto,
4861 use->lineno_o_goto);
4865 check_switch_goto (struct cp_binding_level* level)
4867 check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0);
4870 /* Check that any previously seen jumps to a newly defined label DECL
4871 are OK. Called by define_label. */
4874 check_previous_gotos (tree decl)
4876 struct named_label_use_list **usep;
4878 if (! TREE_USED (decl))
4881 for (usep = &named_label_uses; *usep; )
4883 struct named_label_use_list *use = *usep;
4884 if (use->label_decl == decl)
4886 check_previous_goto (use);
4890 usep = &(use->next);
4894 /* Check that a new jump to a label DECL is OK. Called by
4895 finish_goto_stmt. */
4898 check_goto (tree decl)
4902 struct named_label_list *lab;
4904 /* We can't know where a computed goto is jumping. So we assume
4906 if (! DECL_P (decl))
4909 /* If the label hasn't been defined yet, defer checking. */
4910 if (! DECL_INITIAL (decl))
4916 for (lab = named_labels; lab; lab = lab->next)
4917 if (decl == lab->label_decl)
4920 /* If the label is not on named_labels it's a gcc local label, so
4921 it must be in an outer scope, so jumping to it is always OK. */
4925 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4928 cp_pedwarn_at ("jump to label `%D'", decl);
4929 pedwarn (" from here");
4933 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4935 tree b = TREE_VALUE (bad);
4936 int u = decl_jump_unsafe (b);
4938 if (u > 1 && DECL_ARTIFICIAL (b))
4939 /* Can't skip init of __exception_info. */
4940 cp_error_at (" enters catch block", b);
4942 cp_error_at (" skips initialization of `%#D'", b);
4944 cp_pedwarn_at (" enters scope of non-POD `%#D'", b);
4947 if (lab->in_try_scope)
4948 error (" enters try block");
4949 else if (lab->in_catch_scope)
4950 error (" enters catch block");
4953 /* Define a label, specifying the location in the source file.
4954 Return the LABEL_DECL node for the label, if the definition is valid.
4955 Otherwise return 0. */
4958 define_label (const char* filename, int line, tree name)
4960 tree decl = lookup_label (name);
4961 struct named_label_list *ent;
4962 register struct cp_binding_level *p;
4964 for (ent = named_labels; ent; ent = ent->next)
4965 if (ent->label_decl == decl)
4968 /* After labels, make any new cleanups in the function go into their
4969 own new (temporary) binding contour. */
4970 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
4971 p->more_cleanups_ok = 0;
4973 if (name == get_identifier ("wchar_t"))
4974 pedwarn ("label named wchar_t");
4976 if (DECL_INITIAL (decl) != NULL_TREE)
4978 error ("duplicate label `%D'", decl);
4983 /* Mark label as having been defined. */
4984 DECL_INITIAL (decl) = error_mark_node;
4985 /* Say where in the source. */
4986 DECL_SOURCE_FILE (decl) = filename;
4987 DECL_SOURCE_LINE (decl) = line;
4990 ent->names_in_scope = current_binding_level->names;
4991 ent->binding_level = current_binding_level;
4993 check_previous_gotos (decl);
5000 struct cp_binding_level *level;
5001 struct cp_switch *next;
5002 /* The SWITCH_STMT being built. */
5004 /* A splay-tree mapping the low element of a case range to the high
5005 element, or NULL_TREE if there is no high element. Used to
5006 determine whether or not a new case label duplicates an old case
5007 label. We need a tree, rather than simply a hash table, because
5008 of the GNU case range extension. */
5012 /* A stack of the currently active switch statements. The innermost
5013 switch statement is on the top of the stack. There is no need to
5014 mark the stack for garbage collection because it is only active
5015 during the processing of the body of a function, and we never
5016 collect at that point. */
5018 static struct cp_switch *switch_stack;
5020 /* Called right after a switch-statement condition is parsed.
5021 SWITCH_STMT is the switch statement being parsed. */
5024 push_switch (tree switch_stmt)
5027 = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
5028 p->level = current_binding_level;
5029 p->next = switch_stack;
5030 p->switch_stmt = switch_stmt;
5031 p->cases = splay_tree_new (case_compare, NULL, NULL);
5038 struct cp_switch *cs;
5041 splay_tree_delete (cs->cases);
5042 switch_stack = switch_stack->next;
5046 /* Note that we've seen a definition of a case label, and complain if this
5047 is a bad place for one. */
5050 finish_case_label (tree low_value, tree high_value)
5053 register struct cp_binding_level *p;
5058 error ("case label not within a switch statement");
5060 error ("case label `%E' not within a switch statement",
5063 error ("`default' label not within a switch statement");
5067 if (processing_template_decl)
5071 /* For templates, just add the case label; we'll do semantic
5072 analysis at instantiation-time. */
5073 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5074 return add_stmt (build_case_label (low_value, high_value, label));
5077 /* Find the condition on which this switch statement depends. */
5078 cond = SWITCH_COND (switch_stack->switch_stmt);
5079 if (cond && TREE_CODE (cond) == TREE_LIST)
5080 cond = TREE_VALUE (cond);
5082 r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5083 if (r == error_mark_node)
5086 check_switch_goto (switch_stack->level);
5088 /* After labels, make any new cleanups in the function go into their
5089 own new (temporary) binding contour. */
5090 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5091 p->more_cleanups_ok = 0;
5096 /* Return the list of declarations of the current level.
5097 Note that this list is in reverse order unless/until
5098 you nreverse it; and when you do nreverse it, you must
5099 store the result back using `storedecls' or you will lose. */
5104 return current_binding_level->names;
5107 /* Return the list of type-tags (for structs, etc) of the current level. */
5112 return current_binding_level->tags;
5115 /* Store the list of declarations of the current level.
5116 This is done for the parameter declarations of a function being defined,
5117 after they are modified in the light of any missing parameters. */
5120 storedecls (tree decls)
5122 current_binding_level->names = decls;
5125 /* Similarly, store the list of tags of the current level. */
5128 storetags (tree tags)
5130 current_binding_level->tags = tags;
5133 /* Return the type that should be used when TYPE's name is preceded
5134 by a tag such as 'struct' or 'union', or null if the name cannot
5135 be used in this way.
5137 For example, when processing the third line of:
5143 lookup of A will find the typedef. Given A's typedef, this function
5144 will return the type associated with "struct A". For the tag to be
5145 anything other than TYPE, TYPE must be a typedef whose original type
5146 has the same name and context as TYPE itself.
5148 It is not valid for a typedef of an anonymous type to be used with
5151 typedef struct { ... } B;
5154 Return null for this case. */
5157 follow_tag_typedef (tree type)
5161 original = original_type (type);
5162 if (! TYPE_NAME (original))
5164 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5165 && (CP_DECL_CONTEXT (TYPE_NAME (original))
5166 == CP_DECL_CONTEXT (TYPE_NAME (type)))
5167 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5173 /* Given NAME, an IDENTIFIER_NODE,
5174 return the structure (or union or enum) definition for that name.
5175 Searches binding levels from BINDING_LEVEL up to the global level.
5176 If THISLEVEL_ONLY is nonzero, searches only the specified context
5177 (but skips any tag-transparent contexts to find one that is
5178 meaningful for tags).
5179 FORM says which kind of type the caller wants;
5180 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5181 If the wrong kind of type is found, and it's not a template, an error is
5185 lookup_tag (enum tree_code form,
5187 struct cp_binding_level* binding_level,
5190 register struct cp_binding_level *level;
5191 /* Nonzero if, we should look past a template parameter level, even
5192 if THISLEVEL_ONLY. */
5193 int allow_template_parms_p = 1;
5195 for (level = binding_level; level; level = level->level_chain)
5198 if (ANON_AGGRNAME_P (name))
5199 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5201 /* There's no need for error checking here, because
5202 anon names are unique throughout the compilation. */
5203 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5204 return TREE_VALUE (tail);
5206 else if (level->namespace_p)
5207 /* Do namespace lookup. */
5208 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5210 tree old = binding_for_name (name, tail);
5212 /* If we just skipped past a template parameter level,
5213 even though THISLEVEL_ONLY, and we find a template
5214 class declaration, then we use the _TYPE node for the
5215 template. See the example below. */
5216 if (thislevel_only && !allow_template_parms_p
5217 && old && BINDING_VALUE (old)
5218 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
5219 old = TREE_TYPE (BINDING_VALUE (old));
5221 old = BINDING_TYPE (old);
5225 /* We've found something at this binding level. If it is
5226 a typedef, extract the tag it refers to. Lookup fails
5227 if the typedef doesn't refer to a taggable type. */
5228 old = follow_tag_typedef (old);
5231 if (TREE_CODE (old) != form
5232 && (form == ENUMERAL_TYPE
5233 || TREE_CODE (old) == ENUMERAL_TYPE))
5235 error ("`%#D' redeclared as %C", old, form);
5240 if (thislevel_only || tail == global_namespace)
5244 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5246 if (TREE_PURPOSE (tail) == name)
5248 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5251 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5253 /* Definition isn't the kind we were looking for. */
5254 error ("`%#D' redeclared as %C", TREE_VALUE (tail), form);
5257 return TREE_VALUE (tail);
5260 if (thislevel_only && ! level->tag_transparent)
5262 if (level->template_parms_p && allow_template_parms_p)
5264 /* We must deal with cases like this:
5266 template <class T> struct S;
5267 template <class T> struct S {};
5269 When looking up `S', for the second declaration, we
5270 would like to find the first declaration. But, we
5271 are in the pseudo-global level created for the
5272 template parameters, rather than the (surrounding)
5273 namespace level. Thus, we keep going one more level,
5274 even though THISLEVEL_ONLY is nonzero. */
5275 allow_template_parms_p = 0;
5285 /* Given a type, find the tag that was defined for it and return the tag name.
5286 Otherwise return 0. However, the value can never be 0
5287 in the cases in which this is used.
5289 C++: If NAME is nonzero, this is the new name to install. This is
5290 done when replacing anonymous tags with real tag names. */
5293 lookup_tag_reverse (tree type, tree name)
5295 register struct cp_binding_level *level;
5297 for (level = current_binding_level; level; level = level->level_chain)
5300 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5302 if (TREE_VALUE (tail) == type)
5305 TREE_PURPOSE (tail) = name;
5306 return TREE_PURPOSE (tail);
5313 /* Look up NAME in the NAMESPACE. */
5316 lookup_namespace_name (tree namespace, tree name)
5319 tree template_id = NULL_TREE;
5321 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5323 if (TREE_CODE (name) == NAMESPACE_DECL)
5324 /* This happens for A::B<int> when B is a namespace. */
5326 else if (TREE_CODE (name) == TEMPLATE_DECL)
5328 /* This happens for A::B where B is a template, and there are no
5329 template arguments. */
5330 error ("invalid use of `%D'", name);
5331 return error_mark_node;
5334 namespace = ORIGINAL_NAMESPACE (namespace);
5336 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5339 name = TREE_OPERAND (name, 0);
5340 if (TREE_CODE (name) == OVERLOAD)
5341 name = DECL_NAME (OVL_CURRENT (name));
5342 else if (DECL_P (name))
5343 name = DECL_NAME (name);
5346 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5348 val = make_node (CPLUS_BINDING);
5349 if (!qualified_lookup_using_namespace (name, namespace, val, 0))
5350 return error_mark_node;
5352 if (BINDING_VALUE (val))
5354 val = BINDING_VALUE (val);
5358 if (DECL_CLASS_TEMPLATE_P (val))
5359 val = lookup_template_class (val,
5360 TREE_OPERAND (template_id, 1),
5361 /*in_decl=*/NULL_TREE,
5362 /*context=*/NULL_TREE,
5363 /*entering_scope=*/0,
5364 tf_error | tf_warning);
5365 else if (DECL_FUNCTION_TEMPLATE_P (val)
5366 || TREE_CODE (val) == OVERLOAD)
5367 val = lookup_template_function (val,
5368 TREE_OPERAND (template_id, 1));
5371 error ("`%D::%D' is not a template",
5373 return error_mark_node;
5377 /* If we have a single function from a using decl, pull it out. */
5378 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5379 val = OVL_FUNCTION (val);
5381 /* Ignore built-in functions that haven't been prototyped yet. */
5382 if (!val || !DECL_P(val)
5383 || !DECL_LANG_SPECIFIC(val)
5384 || !DECL_ANTICIPATED (val))
5388 error ("`%D' undeclared in namespace `%D'", name, namespace);
5389 return error_mark_node;
5392 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5395 typename_hash (const void* k)
5400 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5401 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5406 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5409 typename_compare (const void * k1, const void * k2)
5418 d1 = TYPE_NAME (t1);
5419 d2 = TYPE_NAME (t2);
5421 return (DECL_NAME (d1) == DECL_NAME (d2)
5422 && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5423 && ((TREE_TYPE (t1) != NULL_TREE)
5424 == (TREE_TYPE (t2) != NULL_TREE))
5425 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5426 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5429 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5430 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5431 is non-NULL, this type is being created by the implicit typename
5432 extension, and BASE_TYPE is a type named `t' in some base class of
5433 `T' which depends on template parameters.
5435 Returns the new TYPENAME_TYPE. */
5437 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5440 build_typename_type (tree context, tree name, tree fullname, tree base_type)
5446 if (typename_htab == NULL)
5448 typename_htab = htab_create_ggc (61, &typename_hash,
5449 &typename_compare, NULL);
5452 /* Build the TYPENAME_TYPE. */
5453 t = make_aggr_type (TYPENAME_TYPE);
5454 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5455 TYPENAME_TYPE_FULLNAME (t) = fullname;
5456 TREE_TYPE (t) = base_type;
5458 /* Build the corresponding TYPE_DECL. */
5459 d = build_decl (TYPE_DECL, name, t);
5460 TYPE_NAME (TREE_TYPE (d)) = d;
5461 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5462 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5463 DECL_ARTIFICIAL (d) = 1;
5465 /* See if we already have this type. */
5466 e = htab_find_slot (typename_htab, t, INSERT);
5475 /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type,
5476 unless an error occurs, in which case error_mark_node is returned.
5477 If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5478 set, we return that, rather than the _TYPE it corresponds to, in
5479 other cases we look through the type decl. If TF_ERROR is set,
5480 complain about errors, otherwise be quiet. */
5483 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5489 if (!(TYPE_LANG_SPECIFIC (name)
5490 && (CLASSTYPE_IS_TEMPLATE (name)
5491 || CLASSTYPE_USE_TEMPLATE (name))))
5492 name = TYPE_IDENTIFIER (name);
5494 /* Create a TEMPLATE_ID_EXPR for the type. */
5495 name = build_nt (TEMPLATE_ID_EXPR,
5496 CLASSTYPE_TI_TEMPLATE (name),
5497 CLASSTYPE_TI_ARGS (name));
5499 else if (TREE_CODE (name) == TYPE_DECL)
5500 name = DECL_NAME (name);
5504 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5506 name = TREE_OPERAND (name, 0);
5507 if (TREE_CODE (name) == TEMPLATE_DECL)
5508 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5510 if (TREE_CODE (name) == TEMPLATE_DECL)
5512 error ("`%D' used without template parameters", name);
5513 return error_mark_node;
5515 if (TREE_CODE (name) != IDENTIFIER_NODE)
5518 if (TREE_CODE (context) == NAMESPACE_DECL)
5520 /* We can get here from typename_sub0 in the explicit_template_type
5521 expansion. Just fail. */
5522 if (complain & tf_error)
5523 error ("no class template named `%#T' in `%#T'",
5525 return error_mark_node;
5528 if (! uses_template_parms (context)
5529 || currently_open_class (context))
5531 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5533 tree tmpl = NULL_TREE;
5534 if (IS_AGGR_TYPE (context))
5535 tmpl = lookup_field (context, name, 0, 0);
5536 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5538 if (complain & tf_error)
5539 error ("no class template named `%#T' in `%#T'",
5541 return error_mark_node;
5544 if (complain & tf_error)
5546 if (complain & tf_parsing)
5547 type_access_control (context, tmpl);
5549 enforce_access (context, tmpl);
5552 return lookup_template_class (tmpl,
5553 TREE_OPERAND (fullname, 1),
5555 /*entering_scope=*/0,
5556 tf_error | tf_warning);
5562 if (!IS_AGGR_TYPE (context))
5564 if (complain & tf_error)
5565 error ("no type named `%#T' in `%#T'", name, context);
5566 return error_mark_node;
5569 t = lookup_field (context, name, 0, 1);
5572 if (TREE_CODE (t) != TYPE_DECL)
5574 if (complain & tf_error)
5575 error ("no type named `%#T' in `%#T'", name, context);
5576 return error_mark_node;
5579 if (complain & tf_error)
5581 if (complain & tf_parsing)
5582 type_access_control (context, t);
5584 enforce_access (context, t);
5587 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5589 if (IMPLICIT_TYPENAME_P (t))
5591 /* Lookup found an implicit typename that we had
5592 injected into the current scope. Doing things
5593 properly would have located the exact same type,
5594 so there is no error here. We must remove the
5595 implicitness so that we do not warn about it. */
5597 TREE_TYPE (t) = NULL_TREE;
5605 /* If the CONTEXT is not a template type, then either the field is
5606 there now or its never going to be. */
5607 if (!uses_template_parms (context))
5609 if (complain & tf_error)
5610 error ("no type named `%#T' in `%#T'", name, context);
5611 return error_mark_node;
5614 return build_typename_type (context, name, fullname, NULL_TREE);
5617 /* Resolve `CONTEXT::template NAME'. Returns an appropriate type,
5618 unless an error occurs, in which case error_mark_node is returned.
5619 If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5620 corresponds to. If COMPLAIN zero, don't complain about any errors
5624 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5630 name = TYPE_IDENTIFIER (name);
5631 else if (DECL_P (name))
5632 name = DECL_NAME (name);
5633 if (TREE_CODE (name) != IDENTIFIER_NODE)
5636 if (!uses_template_parms (context)
5637 || currently_open_class (context))
5639 tree tmpl = NULL_TREE;
5641 if (IS_AGGR_TYPE (context))
5642 tmpl = lookup_field (context, name, 0, 0);
5644 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5646 if (complain & tf_error)
5647 error ("no class template named `%#T' in `%#T'", name, context);
5648 return error_mark_node;
5651 if (complain & tf_error)
5653 if (complain & tf_parsing)
5654 type_access_control (context, tmpl);
5656 enforce_access (context, tmpl);
5662 /* Build the UNBOUND_CLASS_TEMPLATE. */
5663 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5664 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5665 TREE_TYPE (t) = NULL_TREE;
5667 /* Build the corresponding TEMPLATE_DECL. */
5668 d = build_decl (TEMPLATE_DECL, name, t);
5669 TYPE_NAME (TREE_TYPE (d)) = d;
5670 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5671 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5672 DECL_ARTIFICIAL (d) = 1;
5677 /* Select the right _DECL from multiple choices. */
5680 select_decl (tree binding, int flags)
5683 val = BINDING_VALUE (binding);
5685 if (LOOKUP_NAMESPACES_ONLY (flags))
5687 /* We are not interested in types. */
5688 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5693 /* If we could have a type and
5694 we have nothing or we need a type and have none. */
5695 if (BINDING_TYPE (binding)
5696 && (!val || ((flags & LOOKUP_PREFER_TYPES)
5697 && TREE_CODE (val) != TYPE_DECL)))
5698 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5699 /* Don't return non-types if we really prefer types. */
5700 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
5701 && (TREE_CODE (val) != TEMPLATE_DECL
5702 || !DECL_CLASS_TEMPLATE_P (val)))
5708 /* Unscoped lookup of a global: iterate over current namespaces,
5709 considering using-directives. If SPACESP is non-NULL, store a list
5710 of the namespaces we've considered in it. */
5713 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5715 tree b = make_node (CPLUS_BINDING);
5716 tree initial = current_decl_namespace ();
5717 tree scope = initial;
5719 struct cp_binding_level *level;
5720 tree val = NULL_TREE;
5723 *spacesp = NULL_TREE;
5725 for (; !val; scope = CP_DECL_CONTEXT (scope))
5728 *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5729 val = binding_for_name (name, scope);
5731 /* Ignore anticipated built-in functions. */
5732 if (val && BINDING_VALUE (val)
5733 && DECL_P (BINDING_VALUE (val))
5734 && DECL_LANG_SPECIFIC (BINDING_VALUE (val))
5735 && DECL_ANTICIPATED (BINDING_VALUE (val)))
5737 BINDING_VALUE (b) = NULL_TREE;
5738 BINDING_TYPE (b) = NULL_TREE;
5742 /* Initialize binding for this context. */
5743 BINDING_VALUE (b) = BINDING_VALUE (val);
5744 BINDING_TYPE (b) = BINDING_TYPE (val);
5747 /* Add all _DECLs seen through local using-directives. */
5748 for (level = current_binding_level;
5749 !level->namespace_p;
5750 level = level->level_chain)
5751 if (!lookup_using_namespace (name, b, level->using_directives,
5752 scope, flags, spacesp))
5753 /* Give up because of error. */
5754 return error_mark_node;
5756 /* Add all _DECLs seen through global using-directives. */
5757 /* XXX local and global using lists should work equally. */
5761 if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter),
5762 scope, flags, spacesp))
5763 /* Give up because of error. */
5764 return error_mark_node;
5765 if (siter == scope) break;
5766 siter = CP_DECL_CONTEXT (siter);
5769 val = select_decl (b, flags);
5770 if (scope == global_namespace)
5776 /* Combine prefer_type and namespaces_only into flags. */
5779 lookup_flags (int prefer_type, int namespaces_only)
5781 if (namespaces_only)
5782 return LOOKUP_PREFER_NAMESPACES;
5783 if (prefer_type > 1)
5784 return LOOKUP_PREFER_TYPES;
5785 if (prefer_type > 0)
5786 return LOOKUP_PREFER_BOTH;
5790 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5791 ignore it or not. Subroutine of lookup_name_real. */
5794 qualify_lookup (tree val, int flags)
5796 if (val == NULL_TREE)
5798 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5800 if ((flags & LOOKUP_PREFER_TYPES) && TREE_CODE (val) == TYPE_DECL)
5802 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5807 /* Any other BINDING overrides an implicit TYPENAME. Warn about
5811 warn_about_implicit_typename_lookup (tree typename, tree binding)
5813 tree subtype = TREE_TYPE (TREE_TYPE (typename));
5814 tree name = DECL_NAME (typename);
5816 if (! (TREE_CODE (binding) == TEMPLATE_DECL
5817 && CLASSTYPE_TEMPLATE_INFO (subtype)
5818 && CLASSTYPE_TI_TEMPLATE (subtype) == binding)
5819 && ! (TREE_CODE (binding) == TYPE_DECL
5820 && same_type_p (TREE_TYPE (binding), subtype)))
5822 warning ("lookup of `%D' finds `%#D'",
5824 warning (" instead of `%D' from dependent base class",
5826 warning (" (use `typename %T::%D' if that's what you meant)",
5827 constructor_name (current_class_type), name);
5831 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5832 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
5835 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5836 declaration found. */
5839 lookup_qualified_name (tree scope, tree name, bool is_type_p, int flags)
5841 if (TREE_CODE (scope) == NAMESPACE_DECL)
5845 val = make_node (CPLUS_BINDING);
5846 flags |= LOOKUP_COMPLAIN;
5848 flags |= LOOKUP_PREFER_TYPES;
5849 if (!qualified_lookup_using_namespace (name, scope, val, flags))
5851 return select_decl (val, flags);
5854 return lookup_member (scope, name, 0, is_type_p);
5857 /* Check to see whether or not DECL is a variable that would have been
5858 in scope under the ARM, but is not in scope under the ANSI/ISO
5859 standard. If so, issue an error message. If name lookup would
5860 work in both cases, but return a different result, this function
5861 returns the result of ANSI/ISO lookup. Otherwise, it returns
5865 check_for_out_of_scope_variable (tree decl)
5869 /* We only care about out of scope variables. */
5870 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5873 shadowed = DECL_SHADOWED_FOR_VAR (decl);
5874 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5875 && DECL_DEAD_FOR_LOCAL (shadowed))
5876 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5878 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5881 if (!DECL_ERROR_REPORTED (decl))
5883 warning ("name lookup of `%D' changed",
5885 cp_warning_at (" matches this `%D' under ISO standard rules",
5887 cp_warning_at (" matches this `%D' under old rules", decl);
5888 DECL_ERROR_REPORTED (decl) = 1;
5893 /* If we have already complained about this declaration, there's no
5894 need to do it again. */
5895 if (DECL_ERROR_REPORTED (decl))
5898 DECL_ERROR_REPORTED (decl) = 1;
5899 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5901 error ("name lookup of `%D' changed for new ISO `for' scoping",
5903 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl);
5904 return error_mark_node;
5908 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5910 cp_pedwarn_at (" using obsolete binding at `%D'", decl);
5916 /* Look up NAME in the current binding level and its superiors in the
5917 namespace of variables, functions and typedefs. Return a ..._DECL
5918 node of some kind representing its definition if there is only one
5919 such declaration, or return a TREE_LIST with all the overloaded
5920 definitions if there are many, or return 0 if it is undefined.
5922 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5923 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5924 Otherwise we prefer non-TYPE_DECLs.
5926 If NONCLASS is nonzero, we don't look for the NAME in class scope,
5927 using IDENTIFIER_CLASS_VALUE. */
5930 lookup_name_real (tree name,
5933 int namespaces_only,
5937 tree val = NULL_TREE;
5938 int val_is_implicit_typename = 0;
5940 /* Conversion operators are handled specially because ordinary
5941 unqualified name lookup will not find template conversion
5943 if (IDENTIFIER_TYPENAME_P (name))
5945 struct cp_binding_level *level;
5947 for (level = current_binding_level;
5948 level && !level->namespace_p;
5949 level = level->level_chain)
5954 /* A conversion operator can only be declared in a class
5956 if (level->parm_flag != 2)
5959 /* Lookup the conversion operator in the class. */
5960 class_type = level->this_class;
5961 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5969 flags |= lookup_flags (prefer_type, namespaces_only);
5971 /* First, look in non-namespace scopes. */
5973 if (current_class_type == NULL_TREE)
5976 for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t))
5980 if (!LOCAL_BINDING_P (t) && nonclass)
5981 /* We're not looking for class-scoped bindings, so keep going. */
5984 /* If this is the kind of thing we're looking for, we're done. */
5985 if (qualify_lookup (BINDING_VALUE (t), flags))
5986 binding = BINDING_VALUE (t);
5987 else if ((flags & LOOKUP_PREFER_TYPES)
5988 && qualify_lookup (BINDING_TYPE (t), flags))
5989 binding = BINDING_TYPE (t);
5991 binding = NULL_TREE;
5994 && (!val || !IMPLICIT_TYPENAME_TYPE_DECL_P (binding)))
5996 if (val_is_implicit_typename)
5997 warn_about_implicit_typename_lookup (val, binding);
5999 val_is_implicit_typename
6000 = IMPLICIT_TYPENAME_TYPE_DECL_P (val);
6001 if (!val_is_implicit_typename)
6006 /* Now lookup in namespace scopes. */
6007 if (!val || val_is_implicit_typename)
6009 t = unqualified_namespace_lookup (name, flags, 0);
6012 if (val_is_implicit_typename)
6013 warn_about_implicit_typename_lookup (val, t);
6020 /* If we have a single function from a using decl, pull it out. */
6021 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
6022 val = OVL_FUNCTION (val);
6029 lookup_name_nonclass (tree name)
6031 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
6035 lookup_function_nonclass (tree name, tree args)
6037 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
6041 lookup_name (tree name, int prefer_type)
6043 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
6046 /* Similar to `lookup_name' but look only in the innermost non-class
6050 lookup_name_current_level (tree name)
6052 struct cp_binding_level *b;
6055 b = current_binding_level;
6056 while (b->parm_flag == 2)
6061 t = IDENTIFIER_NAMESPACE_VALUE (name);
6063 /* extern "C" function() */
6064 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
6067 else if (IDENTIFIER_BINDING (name)
6068 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
6072 if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
6073 return IDENTIFIER_VALUE (name);
6085 /* Like lookup_name_current_level, but for types. */
6088 lookup_type_current_level (tree name)
6090 register tree t = NULL_TREE;
6092 my_friendly_assert (! current_binding_level->namespace_p, 980716);
6094 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
6095 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
6097 struct cp_binding_level *b = current_binding_level;
6100 if (purpose_member (name, b->type_shadowed))
6101 return REAL_IDENTIFIER_TYPE_VALUE (name);
6113 /* Push the declarations of builtin types into the namespace.
6114 RID_INDEX is the index of the builtin type
6115 in the array RID_POINTERS. NAME is the name used when looking
6116 up the builtin type. TYPE is the _TYPE node for the builtin type. */
6119 record_builtin_type (enum rid rid_index,
6123 tree rname = NULL_TREE, tname = NULL_TREE;
6124 tree tdecl = NULL_TREE;
6126 if ((int) rid_index < (int) RID_MAX)
6127 rname = ridpointers[(int) rid_index];
6129 tname = get_identifier (name);
6131 TYPE_BUILT_IN (type) = 1;
6135 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6136 set_identifier_type_value (tname, NULL_TREE);
6137 if ((int) rid_index < (int) RID_MAX)
6138 /* Built-in types live in the global namespace. */
6139 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6141 if (rname != NULL_TREE)
6143 if (tname != NULL_TREE)
6145 set_identifier_type_value (rname, NULL_TREE);
6146 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6150 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6151 set_identifier_type_value (rname, NULL_TREE);
6156 /* Record one of the standard Java types.
6157 * Declare it as having the given NAME.
6158 * If SIZE > 0, it is the size of one of the integral types;
6159 * otherwise it is the negative of the size of one of the other types. */
6162 record_builtin_java_type (const char* name, int size)
6166 type = make_signed_type (size);
6167 else if (size > -32)
6168 { /* "__java_char" or ""__java_boolean". */
6169 type = make_unsigned_type (-size);
6170 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6173 { /* "__java_float" or ""__java_double". */
6174 type = make_node (REAL_TYPE);
6175 TYPE_PRECISION (type) = - size;
6178 record_builtin_type (RID_MAX, name, type);
6179 decl = TYPE_NAME (type);
6181 /* Suppress generate debug symbol entries for these types,
6182 since for normal C++ they are just clutter.
6183 However, push_lang_context undoes this if extern "Java" is seen. */
6184 DECL_IGNORED_P (decl) = 1;
6186 TYPE_FOR_JAVA (type) = 1;
6190 /* Push a type into the namespace so that the back-ends ignore it. */
6193 record_unknown_type (tree type, const char* name)
6195 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6196 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
6197 DECL_IGNORED_P (decl) = 1;
6198 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6199 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6200 TYPE_ALIGN (type) = 1;
6201 TYPE_USER_ALIGN (type) = 0;
6202 TYPE_MODE (type) = TYPE_MODE (void_type_node);
6205 /* An string for which we should create an IDENTIFIER_NODE at
6208 typedef struct predefined_identifier
6210 /* The name of the identifier. */
6211 const char *const name;
6212 /* The place where the IDENTIFIER_NODE should be stored. */
6214 /* Nonzero if this is the name of a constructor or destructor. */
6215 const int ctor_or_dtor_p;
6216 } predefined_identifier;
6218 /* Create all the predefined identifiers. */
6221 initialize_predefined_identifiers (void)
6223 const predefined_identifier *pid;
6225 /* A table of identifiers to create at startup. */
6226 static const predefined_identifier predefined_identifiers[] = {
6227 { "C++", &lang_name_cplusplus, 0 },
6228 { "C", &lang_name_c, 0 },
6229 { "Java", &lang_name_java, 0 },
6230 { CTOR_NAME, &ctor_identifier, 1 },
6231 { "__base_ctor", &base_ctor_identifier, 1 },
6232 { "__comp_ctor", &complete_ctor_identifier, 1 },
6233 { DTOR_NAME, &dtor_identifier, 1 },
6234 { "__comp_dtor", &complete_dtor_identifier, 1 },
6235 { "__base_dtor", &base_dtor_identifier, 1 },
6236 { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6237 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6238 { "nelts", &nelts_identifier, 0 },
6239 { THIS_NAME, &this_identifier, 0 },
6240 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6241 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6242 { "_vptr", &vptr_identifier, 0 },
6243 { "__vtt_parm", &vtt_parm_identifier, 0 },
6244 { "std", &std_identifier, 0 },
6248 for (pid = predefined_identifiers; pid->name; ++pid)
6250 *pid->node = get_identifier (pid->name);
6251 if (pid->ctor_or_dtor_p)
6252 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6256 /* Create the predefined scalar types of C,
6257 and some nodes representing standard constants (0, 1, (void *)0).
6258 Initialize the global binding level.
6259 Make definitions for built-in primitive functions. */
6262 cxx_init_decl_processing (void)
6265 tree void_ftype_ptr;
6267 /* Create all the identifiers we need. */
6268 initialize_predefined_identifiers ();
6270 /* Fill in back-end hooks. */
6271 lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6273 /* Create the global variables. */
6274 push_to_top_level ();
6276 /* Enter the global namespace. */
6277 my_friendly_assert (global_namespace == NULL_TREE, 375);
6278 push_namespace (get_identifier ("::"));
6279 global_namespace = current_namespace;
6280 current_lang_name = NULL_TREE;
6282 /* Adjust various flags based on command-line settings. */
6283 if (! flag_permissive && ! pedantic)
6284 flag_pedantic_errors = 1;
6285 if (!flag_no_inline)
6287 flag_inline_trees = 1;
6290 if (flag_inline_functions)
6292 flag_inline_trees = 2;
6293 flag_inline_functions = 0;
6296 /* Force minimum function alignment if using the least significant
6297 bit of function pointers to store the virtual bit. */
6298 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6299 && force_align_functions_log < 1)
6300 force_align_functions_log = 1;
6303 current_lang_name = lang_name_c;
6305 current_function_decl = NULL_TREE;
6306 current_binding_level = NULL_BINDING_LEVEL;
6307 free_binding_level = NULL_BINDING_LEVEL;
6309 build_common_tree_nodes (flag_signed_char);
6311 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6312 TREE_TYPE (error_mark_list) = error_mark_node;
6314 /* Make the binding_level structure for global names. */
6316 global_binding_level = current_binding_level;
6317 /* The global level is the namespace level of ::. */
6318 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6319 declare_namespace_level ();
6321 /* Create the `std' namespace. */
6322 push_namespace (std_identifier);
6323 std_node = current_namespace;
6326 c_common_nodes_and_builtins ();
6328 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6329 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6330 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6331 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6332 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6333 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6334 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6335 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6337 integer_two_node = build_int_2 (2, 0);
6338 TREE_TYPE (integer_two_node) = integer_type_node;
6339 integer_three_node = build_int_2 (3, 0);
6340 TREE_TYPE (integer_three_node) = integer_type_node;
6342 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6343 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6344 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6345 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6346 TYPE_PRECISION (boolean_type_node) = 1;
6347 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6348 boolean_false_node = build_int_2 (0, 0);
6349 TREE_TYPE (boolean_false_node) = boolean_type_node;
6350 boolean_true_node = build_int_2 (1, 0);
6351 TREE_TYPE (boolean_true_node) = boolean_type_node;
6353 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6356 record_builtin_type (RID_MAX, NULL, string_type_node);
6359 delta_type_node = ptrdiff_type_node;
6360 vtable_index_type = ptrdiff_type_node;
6362 vtt_parm_type = build_pointer_type (const_ptr_type_node);
6363 void_ftype = build_function_type (void_type_node, void_list_node);
6364 void_ftype_ptr = build_function_type (void_type_node,
6365 tree_cons (NULL_TREE,
6369 = build_exception_variant (void_ftype_ptr, empty_except_spec);
6371 /* C++ extensions */
6373 unknown_type_node = make_node (UNKNOWN_TYPE);
6374 record_unknown_type (unknown_type_node, "unknown type");
6376 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6377 TREE_TYPE (unknown_type_node) = unknown_type_node;
6379 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6381 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6382 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6385 /* Make sure we get a unique function type, so we can give
6386 its pointer type a name. (This wins for gdb.) */
6387 tree vfunc_type = make_node (FUNCTION_TYPE);
6388 TREE_TYPE (vfunc_type) = integer_type_node;
6389 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6390 layout_type (vfunc_type);
6392 vtable_entry_type = build_pointer_type (vfunc_type);
6394 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6397 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6398 layout_type (vtbl_type_node);
6399 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6400 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6401 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6402 layout_type (vtbl_ptr_type_node);
6403 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6405 push_namespace (get_identifier ("__cxxabiv1"));
6406 abi_node = current_namespace;
6409 global_type_node = make_node (LANG_TYPE);
6410 record_unknown_type (global_type_node, "global type");
6413 current_lang_name = lang_name_cplusplus;
6416 tree bad_alloc_type_node, newtype, deltype;
6417 tree ptr_ftype_sizetype;
6419 push_namespace (std_identifier);
6421 = xref_tag (class_type, get_identifier ("bad_alloc"),
6422 /*attributes=*/NULL_TREE, 1);
6425 = build_function_type (ptr_type_node,
6426 tree_cons (NULL_TREE,
6429 newtype = build_exception_variant
6430 (ptr_ftype_sizetype, add_exception_specifier
6431 (NULL_TREE, bad_alloc_type_node, -1));
6432 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6433 push_cp_library_fn (NEW_EXPR, newtype);
6434 push_cp_library_fn (VEC_NEW_EXPR, newtype);
6435 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6436 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6440 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6442 /* Perform other language dependent initializations. */
6443 init_class_processing ();
6444 init_search_processing ();
6445 init_rtti_processing ();
6447 if (flag_exceptions)
6448 init_exception_processing ();
6450 if (! supports_one_only ())
6453 make_fname_decl = cp_make_fname_decl;
6454 start_fname_decls ();
6456 /* Show we use EH for cleanups. */
6457 using_eh_for_cleanups ();
6459 /* Maintain consistency. Perhaps we should just complain if they
6460 say -fwritable-strings? */
6461 if (flag_writable_strings)
6462 flag_const_strings = 0;
6465 /* Generate an initializer for a function naming variable from
6466 NAME. NAME may be NULL, in which case we generate a special
6467 ERROR_MARK node which should be replaced later. */
6470 cp_fname_init (const char* name)
6472 tree domain = NULL_TREE;
6474 tree init = NULL_TREE;
6479 length = strlen (name);
6480 domain = build_index_type (size_int (length));
6481 init = build_string (length + 1, name);
6484 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6485 type = build_cplus_array_type (type, domain);
6488 TREE_TYPE (init) = type;
6490 /* We don't know the value until instantiation time. Make
6491 something which will be digested now, but replaced later. */
6492 init = build (ERROR_MARK, type);
6497 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6498 decl, NAME is the initialization string and TYPE_DEP indicates whether
6499 NAME depended on the type of the function. We make use of that to detect
6500 __PRETTY_FUNCTION__ inside a template fn. This is being done
6501 lazily at the point of first use, so we musn't push the decl now. */
6504 cp_make_fname_decl (tree id, int type_dep)
6506 const char *const name = (type_dep && processing_template_decl
6507 ? NULL : fname_as_string (type_dep));
6508 tree init = cp_fname_init (name);
6509 tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6511 /* As we're using pushdecl_with_scope, we must set the context. */
6512 DECL_CONTEXT (decl) = current_function_decl;
6513 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6515 TREE_STATIC (decl) = 1;
6516 TREE_READONLY (decl) = 1;
6517 DECL_ARTIFICIAL (decl) = 1;
6518 DECL_INITIAL (decl) = init;
6520 TREE_USED (decl) = 1;
6522 if (current_function_decl)
6524 struct cp_binding_level *b = current_binding_level;
6525 while (b->level_chain->parm_flag == 0)
6527 pushdecl_with_scope (decl, b);
6530 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6535 /* Make a definition for a builtin function named NAME in the current
6536 namespace, whose data type is TYPE and whose context is CONTEXT.
6537 TYPE should be a function type with argument types.
6539 CLASS and CODE tell later passes how to compile calls to this function.
6540 See tree.h for possible values.
6542 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6543 the name to be called if we can't opencode the function.
6544 If ATTRS is nonzero, use that for the function's attribute
6548 builtin_function_1 (const char* name,
6552 enum built_in_class class,
6553 const char* libname,
6556 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6557 DECL_BUILT_IN_CLASS (decl) = class;
6558 DECL_FUNCTION_CODE (decl) = code;
6559 DECL_CONTEXT (decl) = context;
6563 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6564 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6565 function in the namespace. */
6567 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6568 make_decl_rtl (decl, NULL);
6570 /* Warn if a function in the namespace for users
6571 is used without an occasion to consider it declared. */
6572 if (name[0] != '_' || name[1] != '_')
6573 DECL_ANTICIPATED (decl) = 1;
6575 /* Possibly apply some default attributes to this built-in function. */
6577 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6579 decl_attributes (&decl, NULL_TREE, 0);
6584 /* Entry point for the benefit of c_common_nodes_and_builtins.
6586 Make a defintion for a builtin function named NAME and whose data type
6587 is TYPE. TYPE should be a function type with argument types. This
6588 function places the anticipated declaration in the global namespace
6589 and additionally in the std namespace if appropriate.
6591 CLASS and CODE tell later passes how to compile calls to this function.
6592 See tree.h for possible values.
6594 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6595 the name to be called if we can't opencode the function.
6597 If ATTRS is nonzero, use that for the function's attribute
6601 builtin_function (const char* name,
6604 enum built_in_class class,
6605 const char* libname,
6608 /* All builtins that don't begin with an '_' should additionally
6609 go in the 'std' namespace. */
6612 push_namespace (std_identifier);
6613 builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6617 return builtin_function_1 (name, type, NULL_TREE, code,
6618 class, libname, attrs);
6621 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6622 function. Not called directly. */
6625 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6627 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6628 DECL_EXTERNAL (fn) = 1;
6629 TREE_PUBLIC (fn) = 1;
6630 DECL_ARTIFICIAL (fn) = 1;
6631 TREE_NOTHROW (fn) = 1;
6632 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6633 SET_DECL_LANGUAGE (fn, lang_c);
6637 /* Returns the _DECL for a library function with C linkage.
6638 We assume that such functions never throw; if this is incorrect,
6639 callers should unset TREE_NOTHROW. */
6642 build_library_fn (tree name, tree type)
6644 return build_library_fn_1 (name, ERROR_MARK, type);
6647 /* Returns the _DECL for a library function with C++ linkage. */
6650 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6652 tree fn = build_library_fn_1 (name, operator_code, type);
6653 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6654 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6655 SET_DECL_LANGUAGE (fn, lang_cplusplus);
6656 set_mangled_name_for_decl (fn);
6660 /* Like build_library_fn, but takes a C string instead of an
6664 build_library_fn_ptr (const char* name, tree type)
6666 return build_library_fn (get_identifier (name), type);
6669 /* Like build_cp_library_fn, but takes a C string instead of an
6673 build_cp_library_fn_ptr (const char* name, tree type)
6675 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6678 /* Like build_library_fn, but also pushes the function so that we will
6679 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
6682 push_library_fn (tree name, tree type)
6684 tree fn = build_library_fn (name, type);
6685 pushdecl_top_level (fn);
6689 /* Like build_cp_library_fn, but also pushes the function so that it
6690 will be found by normal lookup. */
6693 push_cp_library_fn (enum tree_code operator_code, tree type)
6695 tree fn = build_cp_library_fn (ansi_opname (operator_code),
6702 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6706 push_void_library_fn (tree name, tree parmtypes)
6708 tree type = build_function_type (void_type_node, parmtypes);
6709 return push_library_fn (name, type);
6712 /* Like push_library_fn, but also note that this function throws
6713 and does not return. Used for __throw_foo and the like. */
6716 push_throw_library_fn (tree name, tree type)
6718 tree fn = push_library_fn (name, type);
6719 TREE_THIS_VOLATILE (fn) = 1;
6720 TREE_NOTHROW (fn) = 0;
6724 /* Apply default attributes to a function, if a system function with default
6728 cxx_insert_default_attributes (tree decl)
6730 if (!DECL_EXTERN_C_FUNCTION_P (decl))
6732 if (!TREE_PUBLIC (decl))
6734 c_common_insert_default_attributes (decl);
6737 /* When we call finish_struct for an anonymous union, we create
6738 default copy constructors and such. But, an anonymous union
6739 shouldn't have such things; this function undoes the damage to the
6740 anonymous union type T.
6742 (The reason that we create the synthesized methods is that we don't
6743 distinguish `union { int i; }' from `typedef union { int i; } U'.
6744 The first is an anonymous union; the second is just an ordinary
6748 fixup_anonymous_aggr (tree t)
6752 /* Wipe out memory of synthesized methods */
6753 TYPE_HAS_CONSTRUCTOR (t) = 0;
6754 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6755 TYPE_HAS_INIT_REF (t) = 0;
6756 TYPE_HAS_CONST_INIT_REF (t) = 0;
6757 TYPE_HAS_ASSIGN_REF (t) = 0;
6758 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6760 /* Splice the implicitly generated functions out of the TYPE_METHODS
6762 q = &TYPE_METHODS (t);
6765 if (DECL_ARTIFICIAL (*q))
6766 *q = TREE_CHAIN (*q);
6768 q = &TREE_CHAIN (*q);
6771 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
6772 if (TYPE_METHODS (t))
6773 cp_error_at ("an anonymous union cannot have function members", t);
6775 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6776 assignment operators (because they cannot have these methods themselves).
6777 For anonymous unions this is already checked because they are not allowed
6778 in any union, otherwise we have to check it. */
6779 if (TREE_CODE (t) != UNION_TYPE)
6783 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6784 if (TREE_CODE (field) == FIELD_DECL)
6786 type = TREE_TYPE (field);
6787 if (CLASS_TYPE_P (type))
6789 if (TYPE_NEEDS_CONSTRUCTING (type))
6790 cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6792 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6793 cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6795 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6796 cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6803 /* Make sure that a declaration with no declarator is well-formed, i.e.
6804 just declares a tagged type or anonymous union.
6806 Returns the type declared; or NULL_TREE if none. */
6809 check_tag_decl (tree declspecs)
6813 int saw_typedef = 0;
6814 tree ob_modifier = NULL_TREE;
6816 /* If a class, struct, or enum type is declared by the DECLSPECS
6817 (i.e, if a class-specifier, enum-specifier, or non-typename
6818 elaborated-type-specifier appears in the DECLSPECS),
6819 DECLARED_TYPE is set to the corresponding type. */
6820 tree declared_type = NULL_TREE;
6821 bool error_p = false;
6823 for (link = declspecs; link; link = TREE_CHAIN (link))
6825 tree value = TREE_VALUE (link);
6828 || TREE_CODE (value) == TYPE_DECL
6829 || (TREE_CODE (value) == IDENTIFIER_NODE
6830 && IDENTIFIER_GLOBAL_VALUE (value)
6831 && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6835 if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6837 if (! in_system_header)
6838 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6843 && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6844 || TREE_CODE (value) == ENUMERAL_TYPE))
6846 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6847 declared_type = value;
6850 else if (value == ridpointers[(int) RID_TYPEDEF])
6852 else if (value == ridpointers[(int) RID_FRIEND])
6854 if (current_class_type == NULL_TREE
6855 || current_scope () != current_class_type)
6856 ob_modifier = value;
6860 else if (value == ridpointers[(int) RID_STATIC]
6861 || value == ridpointers[(int) RID_EXTERN]
6862 || value == ridpointers[(int) RID_AUTO]
6863 || value == ridpointers[(int) RID_REGISTER]
6864 || value == ridpointers[(int) RID_INLINE]
6865 || value == ridpointers[(int) RID_VIRTUAL]
6866 || value == ridpointers[(int) RID_CONST]
6867 || value == ridpointers[(int) RID_VOLATILE]
6868 || value == ridpointers[(int) RID_EXPLICIT]
6869 || value == ridpointers[(int) RID_THREAD])
6870 ob_modifier = value;
6871 else if (value == error_mark_node)
6876 error ("multiple types in one declaration");
6878 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6879 pedwarn ("declaration does not declare anything");
6880 /* Check for an anonymous union. */
6881 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6882 && TYPE_ANONYMOUS_P (declared_type))
6884 /* 7/3 In a simple-declaration, the optional init-declarator-list
6885 can be omitted only when declaring a class (clause 9) or
6886 enumeration (7.2), that is, when the decl-specifier-seq contains
6887 either a class-specifier, an elaborated-type-specifier with
6888 a class-key (9.1), or an enum-specifier. In these cases and
6889 whenever a class-specifier or enum-specifier is present in the
6890 decl-specifier-seq, the identifiers in these specifiers are among
6891 the names being declared by the declaration (as class-name,
6892 enum-names, or enumerators, depending on the syntax). In such
6893 cases, and except for the declaration of an unnamed bit-field (9.6),
6894 the decl-specifier-seq shall introduce one or more names into the
6895 program, or shall redeclare a name introduced by a previous
6896 declaration. [Example:
6897 enum { }; // ill-formed
6898 typedef class { }; // ill-formed
6902 error ("missing type-name in typedef-declaration");
6905 /* Anonymous unions are objects, so they can have specifiers. */;
6906 SET_ANON_AGGR_TYPE_P (declared_type);
6908 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
6909 && !in_system_header)
6910 pedwarn ("ISO C++ prohibits anonymous structs");
6913 else if (ob_modifier)
6915 if (ob_modifier == ridpointers[(int) RID_INLINE]
6916 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6917 error ("`%D' can only be specified for functions", ob_modifier);
6918 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6919 error ("`%D' can only be specified inside a class", ob_modifier);
6920 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6921 error ("`%D' can only be specified for constructors",
6924 error ("`%D' can only be specified for objects and functions",
6928 return declared_type;
6931 /* Called when a declaration is seen that contains no names to declare.
6932 If its type is a reference to a structure, union or enum inherited
6933 from a containing scope, shadow that tag name for the current scope
6934 with a forward reference.
6935 If its type defines a new named structure or union
6936 or defines an enum, it is valid but we need not do anything here.
6937 Otherwise, it is an error.
6939 C++: may have to grok the declspecs to learn about static,
6940 complain for anonymous unions.
6942 Returns the TYPE declared -- or NULL_TREE if none. */
6945 shadow_tag (tree declspecs)
6947 tree t = check_tag_decl (declspecs);
6952 maybe_process_partial_specialization (t);
6954 /* This is where the variables in an anonymous union are
6955 declared. An anonymous union declaration looks like:
6957 because there is no declarator after the union, the parser
6958 sends that declaration here. */
6959 if (ANON_AGGR_TYPE_P (t))
6961 fixup_anonymous_aggr (t);
6963 if (TYPE_FIELDS (t))
6965 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6967 finish_anon_union (decl);
6974 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6977 groktypename (tree typename)
6981 if (TREE_CODE (typename) != TREE_LIST)
6983 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6984 type = grokdeclarator (TREE_VALUE (typename), specs,
6985 TYPENAME, 0, &attrs);
6987 cplus_decl_attributes (&type, attrs, 0);
6991 /* Decode a declarator in an ordinary declaration or data definition.
6992 This is called as soon as the type information and variable name
6993 have been parsed, before parsing the initializer if any.
6994 Here we create the ..._DECL node, fill in its type,
6995 and put it on the list of decls for the current context.
6996 The ..._DECL node is returned as the value.
6998 Exception: for arrays where the length is not specified,
6999 the type is left null, to be filled in by `cp_finish_decl'.
7001 Function definitions do not come here; they go to start_function
7002 instead. However, external and forward declarations of functions
7003 do go through here. Structure field declarations are done by
7004 grokfield and not through here. */
7007 start_decl (tree declarator,
7011 tree prefix_attributes)
7014 register tree type, tem;
7017 /* This should only be done once on the top most decl. */
7018 if (have_extern_spec)
7020 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
7022 have_extern_spec = false;
7025 /* An object declared as __attribute__((deprecated)) suppresses
7026 warnings of uses of other deprecated items. */
7027 if (lookup_attribute ("deprecated", attributes))
7028 deprecated_state = DEPRECATED_SUPPRESS;
7030 attributes = chainon (attributes, prefix_attributes);
7032 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
7035 deprecated_state = DEPRECATED_NORMAL;
7037 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
7040 type = TREE_TYPE (decl);
7042 if (type == error_mark_node)
7045 context = DECL_CONTEXT (decl);
7047 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
7048 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
7050 /* When parsing the initializer, lookup should use the object's
7052 push_decl_namespace (context);
7055 /* We are only interested in class contexts, later. */
7056 if (context && TREE_CODE (context) == NAMESPACE_DECL)
7057 context = NULL_TREE;
7060 /* Is it valid for this decl to have an initializer at all?
7061 If not, set INITIALIZED to zero, which will indirectly
7062 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
7063 switch (TREE_CODE (decl))
7066 error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
7071 error ("function `%#D' is initialized like a variable", decl);
7081 if (! toplevel_bindings_p ()
7082 && DECL_EXTERNAL (decl))
7083 warning ("declaration of `%#D' has `extern' and is initialized",
7085 DECL_EXTERNAL (decl) = 0;
7086 if (toplevel_bindings_p ())
7087 TREE_STATIC (decl) = 1;
7089 /* Tell `pushdecl' this is an initialized decl
7090 even though we don't yet have the initializer expression.
7091 Also tell `cp_finish_decl' it may store the real initializer. */
7092 DECL_INITIAL (decl) = error_mark_node;
7095 /* Set attributes here so if duplicate decl, will have proper attributes. */
7096 cplus_decl_attributes (&decl, attributes, 0);
7098 /* If #pragma weak was used, mark the decl weak now. */
7099 if (current_binding_level == global_binding_level)
7100 maybe_apply_pragma_weak (decl);
7102 if (TREE_CODE (decl) == FUNCTION_DECL
7103 && DECL_DECLARED_INLINE_P (decl)
7104 && DECL_UNINLINABLE (decl)
7105 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
7106 warning_with_decl (decl,
7107 "inline function `%s' given attribute noinline");
7109 if (context && COMPLETE_TYPE_P (complete_type (context)))
7111 push_nested_class (context, 2);
7113 if (TREE_CODE (decl) == VAR_DECL)
7115 tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
7116 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7117 error ("`%#D' is not a static member of `%#T'", decl, context);
7120 if (DECL_CONTEXT (field) != context)
7122 if (!same_type_p (DECL_CONTEXT (field), context))
7123 pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7124 DECL_CONTEXT (field), DECL_NAME (decl),
7125 context, DECL_NAME (decl));
7126 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7128 /* Static data member are tricky; an in-class initialization
7129 still doesn't provide a definition, so the in-class
7130 declaration will have DECL_EXTERNAL set, but will have an
7131 initialization. Thus, duplicate_decls won't warn
7132 about this situation, and so we check here. */
7133 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7134 error ("duplicate initialization of %D", decl);
7135 if (duplicate_decls (decl, field))
7141 tree field = check_classfn (context, decl);
7142 if (field && duplicate_decls (decl, field))
7146 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
7147 DECL_IN_AGGR_P (decl) = 0;
7148 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7149 || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7151 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7152 /* [temp.expl.spec] An explicit specialization of a static data
7153 member of a template is a definition if the declaration
7154 includes an initializer; otherwise, it is a declaration.
7156 We check for processing_specialization so this only applies
7157 to the new specialization syntax. */
7158 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7159 DECL_EXTERNAL (decl) = 1;
7162 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7163 pedwarn ("declaration of `%#D' outside of class is not definition",
7167 /* Enter this declaration into the symbol table. */
7168 tem = maybe_push_decl (decl);
7170 if (processing_template_decl)
7171 tem = push_template_decl (tem);
7173 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7174 /* Tell the back-end to use or not use .common as appropriate. If we say
7175 -fconserve-space, we want this to save .data space, at the expense of
7176 wrong semantics. If we say -fno-conserve-space, we want this to
7177 produce errors about redefs; to do this we force variables into the
7179 DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7180 || !DECL_THREAD_LOCAL (tem))
7181 && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7184 if (! processing_template_decl)
7191 start_decl_1 (tree decl)
7193 tree type = TREE_TYPE (decl);
7194 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7196 if (type == error_mark_node)
7199 maybe_push_cleanup_level (type);
7202 /* Is it valid for this decl to have an initializer at all?
7203 If not, set INITIALIZED to zero, which will indirectly
7204 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
7206 /* Don't allow initializations for incomplete types except for
7207 arrays which might be completed by the initialization. */
7208 if (COMPLETE_TYPE_P (complete_type (type)))
7209 ; /* A complete type is ok. */
7210 else if (TREE_CODE (type) != ARRAY_TYPE)
7212 error ("variable `%#D' has initializer but incomplete type",
7215 type = TREE_TYPE (decl) = error_mark_node;
7217 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7219 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7220 error ("elements of array `%#D' have incomplete type", decl);
7221 /* else we already gave an error in start_decl. */
7227 && TREE_CODE (decl) != TYPE_DECL
7228 && TREE_CODE (decl) != TEMPLATE_DECL
7229 && type != error_mark_node
7230 && IS_AGGR_TYPE (type)
7231 && ! DECL_EXTERNAL (decl))
7233 if ((! processing_template_decl || ! uses_template_parms (type))
7234 && !COMPLETE_TYPE_P (complete_type (type)))
7236 error ("aggregate `%#D' has incomplete type and cannot be defined",
7238 /* Change the type so that assemble_variable will give
7239 DECL an rtl we can live with: (mem (const_int 0)). */
7240 type = TREE_TYPE (decl) = error_mark_node;
7244 /* If any base type in the hierarchy of TYPE needs a constructor,
7245 then we set initialized to 1. This way any nodes which are
7246 created for the purposes of initializing this aggregate
7247 will live as long as it does. This is necessary for global
7248 aggregates which do not have their initializers processed until
7249 the end of the file. */
7250 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7255 DECL_INITIAL (decl) = NULL_TREE;
7258 /* Handle initialization of references.
7259 These three arguments are from `cp_finish_decl', and have the
7260 same meaning here that they do there.
7262 Quotes on semantics can be found in ARM 8.4.3. */
7265 grok_reference_init (tree decl, tree type, tree init)
7269 if (init == NULL_TREE)
7271 if ((DECL_LANG_SPECIFIC (decl) == 0
7272 || DECL_IN_AGGR_P (decl) == 0)
7273 && ! DECL_THIS_EXTERN (decl))
7274 error ("`%D' declared as reference but not initialized", decl);
7278 if (TREE_CODE (init) == CONSTRUCTOR)
7280 error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7284 if (TREE_CODE (init) == TREE_LIST)
7285 init = build_compound_expr (init);
7287 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7288 init = convert_from_reference (init);
7290 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7291 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7293 /* Note: default conversion is only called in very special cases. */
7294 init = default_conversion (init);
7297 /* Convert INIT to the reference type TYPE. This may involve the
7298 creation of a temporary, whose lifetime must be the same as that
7299 of the reference. If so, a DECL_STMT for the temporary will be
7300 added just after the DECL_STMT for DECL. That's why we don't set
7301 DECL_INITIAL for local references (instead assigning to them
7302 explicitly); we need to allow the temporary to be initialized
7304 tmp = convert_to_reference
7305 (type, init, CONV_IMPLICIT,
7306 LOOKUP_ONLYCONVERTING|LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND,
7309 if (tmp == error_mark_node)
7311 else if (tmp == NULL_TREE)
7313 error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7317 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7320 DECL_INITIAL (decl) = tmp;
7325 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7326 array until we finish parsing the initializer. If that's the
7327 situation we're in, update DECL accordingly. */
7330 maybe_deduce_size_from_array_init (tree decl, tree init)
7332 tree type = TREE_TYPE (decl);
7334 if (TREE_CODE (type) == ARRAY_TYPE
7335 && TYPE_DOMAIN (type) == NULL_TREE
7336 && TREE_CODE (decl) != TYPE_DECL)
7338 /* do_default is really a C-ism to deal with tentative definitions.
7339 But let's leave it here to ease the eventual merge. */
7340 int do_default = !DECL_EXTERNAL (decl);
7341 tree initializer = init ? init : DECL_INITIAL (decl);
7342 int failure = complete_array_type (type, initializer, do_default);
7345 error ("initializer fails to determine size of `%D'", decl);
7350 error ("array size missing in `%D'", decl);
7351 /* If a `static' var's size isn't known, make it extern as
7352 well as static, so it does not get allocated. If it's not
7353 `static', then don't mark it extern; finish_incomplete_decl
7354 will give it a default size and it will get allocated. */
7355 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7356 DECL_EXTERNAL (decl) = 1;
7359 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7360 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7362 error ("zero-size array `%D'", decl);
7364 layout_decl (decl, 0);
7368 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7369 any appropriate error messages regarding the layout. */
7372 layout_var_decl (tree decl)
7374 tree type = TREE_TYPE (decl);
7376 tree ttype = target_type (type);
7379 /* If we haven't already layed out this declaration, do so now.
7380 Note that we must not call complete type for an external object
7381 because it's type might involve templates that we are not
7382 supposed to isntantiate yet. (And it's perfectly valid to say
7383 `extern X x' for some incomplete type `X'.) */
7384 if (!DECL_EXTERNAL (decl))
7385 complete_type (type);
7386 if (!DECL_SIZE (decl)
7387 && TREE_TYPE (decl) != error_mark_node
7388 && (COMPLETE_TYPE_P (type)
7389 || (TREE_CODE (type) == ARRAY_TYPE
7390 && !TYPE_DOMAIN (type)
7391 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7392 layout_decl (decl, 0);
7394 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7396 /* An automatic variable with an incomplete type: that is an error.
7397 Don't talk about array types here, since we took care of that
7398 message in grokdeclarator. */
7399 error ("storage size of `%D' isn't known", decl);
7400 TREE_TYPE (decl) = error_mark_node;
7403 /* Keep this code around in case we later want to control debug info
7404 based on whether a type is "used". (jason 1999-11-11) */
7406 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7407 /* Let debugger know it should output info for this type. */
7408 note_debug_info_needed (ttype);
7410 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7411 note_debug_info_needed (DECL_CONTEXT (decl));
7414 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7415 && DECL_SIZE (decl) != NULL_TREE
7416 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7418 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7419 constant_expression_warning (DECL_SIZE (decl));
7421 error ("storage size of `%D' isn't constant", decl);
7424 if (TREE_STATIC (decl)
7425 && !DECL_ARTIFICIAL (decl)
7426 && current_function_decl
7427 && DECL_CONTEXT (decl) == current_function_decl)
7428 push_local_name (decl);
7431 /* If a local static variable is declared in an inline function, or if
7432 we have a weak definition, we must endeavor to create only one
7433 instance of the variable at link-time. */
7436 maybe_commonize_var (tree decl)
7438 /* Static data in a function with comdat linkage also has comdat
7440 if (TREE_STATIC (decl)
7441 /* Don't mess with __FUNCTION__. */
7442 && ! DECL_ARTIFICIAL (decl)
7443 && current_function_decl
7444 && DECL_CONTEXT (decl) == current_function_decl
7445 && (DECL_DECLARED_INLINE_P (current_function_decl)
7446 || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
7447 && TREE_PUBLIC (current_function_decl))
7449 /* If flag_weak, we don't need to mess with this, as we can just
7450 make the function weak, and let it refer to its unique local
7451 copy. This works because we don't allow the function to be
7455 if (DECL_INTERFACE_KNOWN (current_function_decl))
7457 TREE_PUBLIC (decl) = 1;
7458 DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7460 else if (DECL_INITIAL (decl) == NULL_TREE
7461 || DECL_INITIAL (decl) == error_mark_node)
7463 TREE_PUBLIC (decl) = 1;
7464 DECL_COMMON (decl) = 1;
7466 /* else we lose. We can only do this if we can use common,
7467 which we can't if it has been initialized. */
7469 if (!TREE_PUBLIC (decl))
7471 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7472 cp_warning_at (" you can work around this by removing the initializer", decl);
7476 comdat_linkage (decl);
7478 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7479 /* Set it up again; we might have set DECL_INITIAL since the last
7481 comdat_linkage (decl);
7484 /* Issue an error message if DECL is an uninitialized const variable. */
7487 check_for_uninitialized_const_var (tree decl)
7489 tree type = TREE_TYPE (decl);
7491 /* ``Unless explicitly declared extern, a const object does not have
7492 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7494 if (TREE_CODE (decl) == VAR_DECL
7495 && TREE_CODE (type) != REFERENCE_TYPE
7496 && CP_TYPE_CONST_P (type)
7497 && !TYPE_NEEDS_CONSTRUCTING (type)
7498 && !DECL_INITIAL (decl))
7499 error ("uninitialized const `%D'", decl);
7502 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
7503 returned is the next FIELD_DECL (possibly FIELD itself) that can be
7504 initialized. If there are no more such fields, the return value
7508 next_initializable_field (tree field)
7511 && (TREE_CODE (field) != FIELD_DECL
7512 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7513 || DECL_ARTIFICIAL (field)))
7514 field = TREE_CHAIN (field);
7519 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7520 brace-enclosed aggregate initializer.
7522 *INITP is one of a list of initializers describing a brace-enclosed
7523 initializer for an entity of the indicated aggregate TYPE. It may
7524 not presently match the shape of the TYPE; for example:
7526 struct S { int a; int b; };
7527 struct S a[] = { 1, 2, 3, 4 };
7529 Here *INITP will point to TREE_LIST of four elements, rather than a
7530 list of two elements, each itself a list of two elements. This
7531 routine transforms INIT from the former form into the latter. The
7532 revised initializer is returned. */
7535 reshape_init (tree type, tree *initp)
7539 tree old_init_value;
7541 bool brace_enclosed_p;
7544 old_init_value = (TREE_CODE (*initp) == TREE_LIST
7545 ? TREE_VALUE (*initp) : old_init);
7547 /* For some parse errors, OLD_INIT_VALUE may be NULL. */
7548 if (!old_init_value)
7550 my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
7551 TREE_VALUE (old_init) = error_mark_node;
7555 /* If the initializer is brace-enclosed, pull initializers from the
7556 enclosed elements. Advance past the brace-enclosed initializer
7558 if (TREE_CODE (old_init_value) == CONSTRUCTOR
7559 && TREE_HAS_CONSTRUCTOR (old_init_value))
7561 *initp = TREE_CHAIN (old_init);
7562 TREE_CHAIN (old_init) = NULL_TREE;
7563 inits = CONSTRUCTOR_ELTS (old_init_value);
7565 brace_enclosed_p = true;
7570 brace_enclosed_p = false;
7573 /* A non-aggregate type is always initialized with a single
7575 if (!CP_AGGREGATE_TYPE_P (type))
7577 *initp = TREE_CHAIN (old_init);
7578 TREE_CHAIN (old_init) = NULL_TREE;
7579 /* It is invalid to initialize a non-aggregate type with a
7580 brace-enclosed initializer. */
7581 if (brace_enclosed_p)
7583 error ("brace-enclosed initializer used to initialize `%T'",
7585 if (TREE_CODE (old_init) == TREE_LIST)
7586 TREE_VALUE (old_init) = error_mark_node;
7588 old_init = error_mark_node;
7596 All implicit type conversions (clause _conv_) are considered when
7597 initializing the aggregate member with an initializer from an
7598 initializer-list. If the initializer can initialize a member,
7599 the member is initialized. Otherwise, if the member is itself a
7600 non-empty subaggregate, brace elision is assumed and the
7601 initializer is considered for the initialization of the first
7602 member of the subaggregate. */
7603 if (CLASS_TYPE_P (type)
7604 && !brace_enclosed_p
7605 && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7607 *initp = TREE_CHAIN (old_init);
7608 TREE_CHAIN (old_init) = NULL_TREE;
7612 if (TREE_CODE (old_init_value) == STRING_CST
7613 && TREE_CODE (type) == ARRAY_TYPE
7614 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7616 /* [dcl.init.string]
7618 A char array (whether plain char, signed char, or unsigned char)
7619 can be initialized by a string-literal (optionally enclosed in
7620 braces); a wchar_t array can be initialized by a wide
7621 string-literal (optionally enclosed in braces). */
7622 new_init = old_init;
7623 /* Move past the initializer. */
7624 *initp = TREE_CHAIN (old_init);
7625 TREE_CHAIN (old_init) = NULL_TREE;
7629 /* Build a CONSTRUCTOR to hold the contents of the aggregate. */
7630 new_init = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
7631 TREE_HAS_CONSTRUCTOR (new_init) = 1;
7633 if (CLASS_TYPE_P (type))
7637 field = next_initializable_field (TYPE_FIELDS (type));
7643 An initializer for an aggregate member that is an
7644 empty class shall have the form of an empty
7645 initializer-list {}. */
7646 if (!brace_enclosed_p)
7647 error ("initializer for `%T' must be brace-enclosed",
7652 /* Loop through the initializable fields, gathering
7654 while (*initp && field)
7658 field_init = reshape_init (TREE_TYPE (field), initp);
7659 TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7660 CONSTRUCTOR_ELTS (new_init) = field_init;
7663 When a union is initialized with a brace-enclosed
7664 initializer, the braces shall only contain an
7665 initializer for the first member of the union. */
7666 if (TREE_CODE (type) == UNION_TYPE)
7668 if (TREE_PURPOSE (field_init))
7669 field = TREE_PURPOSE (field_init);
7670 field = next_initializable_field (TREE_CHAIN (field));
7674 else if (TREE_CODE (type) == ARRAY_TYPE)
7679 /* If the bound of the array is known, take no more initializers
7680 than are allowed. */
7681 max_index = (TYPE_DOMAIN (type)
7682 ? array_type_nelts (type) : NULL_TREE);
7683 /* Loop through the array elements, gathering initializers. */
7684 for (index = size_zero_node;
7685 *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7686 index = size_binop (PLUS_EXPR, index, size_one_node))
7690 element_init = reshape_init (TREE_TYPE (type), initp);
7691 TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7692 CONSTRUCTOR_ELTS (new_init) = element_init;
7693 if (TREE_PURPOSE (element_init))
7694 index = TREE_PURPOSE (element_init);
7700 /* The initializers were placed in reverse order in the
7702 CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7704 if (TREE_CODE (old_init) == TREE_LIST)
7705 new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7708 /* If this was a brace-enclosed initializer and all of the
7709 initializers were not used up, there is a problem. */
7710 if (brace_enclosed_p && *initp)
7711 error ("too many initializers for `%T'", type);
7716 /* Verify INIT (the initializer for DECL), and record the
7717 initialization in DECL_INITIAL, if appropriate.
7719 If the return value is non-NULL, it is an expression that must be
7720 evaluated dynamically to initialize DECL. */
7723 check_initializer (tree decl, tree init, int flags)
7725 tree type = TREE_TYPE (decl);
7727 /* If `start_decl' didn't like having an initialization, ignore it now. */
7728 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7731 /* If an initializer is present, DECL_INITIAL has been
7732 error_mark_node, to indicate that an as-of-yet unevaluated
7733 initialization will occur. From now on, DECL_INITIAL reflects
7734 the static initialization -- if any -- of DECL. */
7735 DECL_INITIAL (decl) = NULL_TREE;
7737 /* Things that are going to be initialized need to have complete
7739 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7741 if (type == error_mark_node)
7742 /* We will have already complained. */
7744 else if (init && COMPLETE_TYPE_P (type)
7745 && !TREE_CONSTANT (TYPE_SIZE (type)))
7747 error ("variable-sized object `%D' may not be initialized", decl);
7750 else if (TREE_CODE (type) == ARRAY_TYPE
7751 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7753 error ("elements of array `%#D' have incomplete type", decl);
7756 else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7758 error ("`%D' has incomplete type", decl);
7759 TREE_TYPE (decl) = error_mark_node;
7763 if (TREE_CODE (decl) == CONST_DECL)
7765 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7767 DECL_INITIAL (decl) = init;
7769 my_friendly_assert (init != NULL_TREE, 149);
7772 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7773 init = grok_reference_init (decl, type, init);
7776 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7778 /* [dcl.init] paragraph 13,
7779 If T is a scalar type, then a declaration of the form
7784 reshape_init will complain about the extra braces,
7785 and doesn't do anything useful in the case where TYPE is
7786 scalar, so just don't call it. */
7787 if (CP_AGGREGATE_TYPE_P (type))
7788 init = reshape_init (type, &init);
7791 /* If DECL has an array type without a specific bound, deduce the
7792 array size from the initializer. */
7793 maybe_deduce_size_from_array_init (decl, init);
7794 type = TREE_TYPE (decl);
7795 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7796 TREE_TYPE (init) = type;
7798 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7800 if (TREE_CODE (type) == ARRAY_TYPE)
7801 goto initialize_aggr;
7802 else if (TREE_CODE (init) == CONSTRUCTOR
7803 && TREE_HAS_CONSTRUCTOR (init))
7805 if (TYPE_NON_AGGREGATE_CLASS (type))
7807 error ("`%D' must be initialized by constructor, not by `{...}'",
7809 init = error_mark_node;
7812 goto dont_use_constructor;
7816 int saved_stmts_are_full_exprs_p;
7819 saved_stmts_are_full_exprs_p = 0;
7820 if (building_stmt_tree ())
7822 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7823 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7825 init = build_aggr_init (decl, init, flags);
7826 if (building_stmt_tree ())
7827 current_stmt_tree ()->stmts_are_full_exprs_p =
7828 saved_stmts_are_full_exprs_p;
7834 dont_use_constructor:
7835 if (TREE_CODE (init) != TREE_VEC)
7836 init = store_init_value (decl, init);
7839 else if (DECL_EXTERNAL (decl))
7841 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7842 goto initialize_aggr;
7843 else if (IS_AGGR_TYPE (type))
7845 tree core_type = strip_array_types (type);
7847 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7848 error ("structure `%D' with uninitialized const members", decl);
7849 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7850 error ("structure `%D' with uninitialized reference members",
7853 check_for_uninitialized_const_var (decl);
7856 check_for_uninitialized_const_var (decl);
7858 if (init && init != error_mark_node)
7859 init = build (INIT_EXPR, type, decl, init);
7864 /* If DECL is not a local variable, give it RTL. */
7867 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7869 int toplev = toplevel_bindings_p ();
7872 /* Handle non-variables up front. */
7873 if (TREE_CODE (decl) != VAR_DECL)
7875 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7879 /* If we see a class member here, it should be a static data
7881 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7883 my_friendly_assert (TREE_STATIC (decl), 19990828);
7884 /* An in-class declaration of a static data member should be
7885 external; it is only a declaration, and not a definition. */
7886 if (init == NULL_TREE)
7887 my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7890 /* Set the DECL_ASSEMBLER_NAME for the variable. */
7893 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7894 /* The `register' keyword, when used together with an
7895 asm-specification, indicates that the variable should be
7896 placed in a particular register. */
7897 if (DECL_REGISTER (decl))
7898 DECL_C_HARD_REGISTER (decl) = 1;
7901 /* We don't create any RTL for local variables. */
7902 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7905 /* We defer emission of local statics until the corresponding
7906 DECL_STMT is expanded. */
7907 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7909 /* We try to defer namespace-scope static constants so that they are
7910 not emitted into the object file unnecessarily. */
7911 if (!DECL_VIRTUAL_P (decl)
7912 && TREE_READONLY (decl)
7913 && DECL_INITIAL (decl) != NULL_TREE
7914 && DECL_INITIAL (decl) != error_mark_node
7915 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7917 && !TREE_PUBLIC (decl))
7919 /* Fool with the linkage of static consts according to #pragma
7921 if (!interface_unknown && !TREE_PUBLIC (decl))
7923 TREE_PUBLIC (decl) = 1;
7924 DECL_EXTERNAL (decl) = interface_only;
7929 /* Likewise for template instantiations. */
7930 else if (DECL_COMDAT (decl))
7933 /* If we're deferring the variable, we only need to make RTL if
7934 there's an ASMSPEC. Otherwise, we'll lazily create it later when
7935 we need it. (There's no way to lazily create RTL for things that
7936 have assembly specs because the information about the specifier
7937 isn't stored in the tree, yet) */
7938 if (defer_p && asmspec)
7939 make_decl_rtl (decl, asmspec);
7940 /* If we're not deferring, go ahead and assemble the variable. */
7942 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7945 /* The old ARM scoping rules injected variables declared in the
7946 initialization statement of a for-statement into the surrounding
7947 scope. We support this usage, in order to be backward-compatible.
7948 DECL is a just-declared VAR_DECL; if necessary inject its
7949 declaration into the surrounding scope. */
7952 maybe_inject_for_scope_var (tree decl)
7954 if (!DECL_NAME (decl))
7957 /* Declarations of __FUNCTION__ and its ilk appear magically when
7958 the variable is first used. If that happens to be inside a
7959 for-loop, we don't want to do anything special. */
7960 if (DECL_PRETTY_FUNCTION_P (decl))
7963 if (current_binding_level->is_for_scope)
7965 struct cp_binding_level *outer
7966 = current_binding_level->level_chain;
7968 /* Check to see if the same name is already bound at the outer
7969 level, either because it was directly declared, or because a
7970 dead for-decl got preserved. In either case, the code would
7971 not have been valid under the ARM scope rules, so clear
7972 is_for_scope for the current_binding_level.
7974 Otherwise, we need to preserve the temp slot for decl to last
7975 into the outer binding level. */
7978 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
7980 if (outer_binding && BINDING_LEVEL (outer_binding) == outer
7981 && (TREE_CODE (BINDING_VALUE (outer_binding))
7983 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7985 BINDING_VALUE (outer_binding)
7986 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7987 current_binding_level->is_for_scope = 0;
7992 /* Generate code to initialize DECL (a local variable). */
7995 initialize_local_var (tree decl, tree init)
7997 tree type = TREE_TYPE (decl);
7999 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
8000 || TREE_CODE (decl) == RESULT_DECL,
8002 my_friendly_assert (!TREE_STATIC (decl), 20021010);
8004 if (DECL_SIZE (decl) == NULL_TREE)
8006 /* If we used it already as memory, it must stay in memory. */
8007 DECL_INITIAL (decl) = NULL_TREE;
8008 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8011 if (DECL_SIZE (decl) && type != error_mark_node)
8015 /* Compute and store the initial value. */
8016 already_used = TREE_USED (decl) || TREE_USED (type);
8018 /* Perform the initialization. */
8021 int saved_stmts_are_full_exprs_p;
8023 my_friendly_assert (building_stmt_tree (), 20000906);
8024 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8025 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8026 finish_expr_stmt (init);
8027 current_stmt_tree ()->stmts_are_full_exprs_p =
8028 saved_stmts_are_full_exprs_p;
8031 /* Set this to 0 so we can tell whether an aggregate which was
8032 initialized was ever used. Don't do this if it has a
8033 destructor, so we don't complain about the 'resource
8034 allocation is initialization' idiom. Now set
8035 attribute((unused)) on types so decls of that type will be
8036 marked used. (see TREE_USED, above.) */
8037 if (TYPE_NEEDS_CONSTRUCTING (type)
8039 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8040 && DECL_NAME (decl))
8041 TREE_USED (decl) = 0;
8042 else if (already_used)
8043 TREE_USED (decl) = 1;
8046 /* Generate a cleanup, if necessary. */
8047 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8051 /* Compute the cleanup. */
8052 cleanup = cxx_maybe_build_cleanup (decl);
8054 /* Record the cleanup required for this declaration. */
8055 if (DECL_SIZE (decl) && cleanup)
8056 finish_decl_cleanup (decl, cleanup);
8060 /* Finish processing of a declaration;
8061 install its line number and initial value.
8062 If the length of an array type is not known before,
8063 it must be determined now, from the initial value, or it is an error.
8065 INIT holds the value of an initializer that should be allowed to escape
8068 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8069 if the (init) syntax was used. */
8072 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
8075 tree ttype = NULL_TREE;
8076 const char *asmspec = NULL;
8077 int was_readonly = 0;
8082 error ("assignment (not initialization) in declaration");
8086 /* If a name was specified, get the string. */
8087 if (current_binding_level == global_binding_level)
8088 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8090 asmspec = TREE_STRING_POINTER (asmspec_tree);
8092 if (init && TREE_CODE (init) == NAMESPACE_DECL)
8094 error ("cannot initialize `%D' to namespace `%D'",
8099 if (current_class_type
8100 && CP_DECL_CONTEXT (decl) == current_class_type
8101 && TYPE_BEING_DEFINED (current_class_type)
8102 && (DECL_INITIAL (decl) || init))
8103 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8105 if (TREE_CODE (decl) == VAR_DECL
8106 && DECL_CONTEXT (decl)
8107 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8108 && DECL_CONTEXT (decl) != current_namespace
8111 /* Leave the namespace of the object. */
8112 pop_decl_namespace ();
8115 type = TREE_TYPE (decl);
8117 if (type == error_mark_node)
8120 if (TYPE_HAS_MUTABLE_P (type))
8121 TREE_READONLY (decl) = 0;
8123 if (processing_template_decl)
8125 /* Add this declaration to the statement-tree. */
8126 if (at_function_scope_p ()
8127 && TREE_CODE (decl) != RESULT_DECL)
8128 add_decl_stmt (decl);
8130 if (init && DECL_INITIAL (decl))
8131 DECL_INITIAL (decl) = init;
8135 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8136 my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8138 /* Take care of TYPE_DECLs up front. */
8139 if (TREE_CODE (decl) == TYPE_DECL)
8141 if (type != error_mark_node
8142 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8144 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8145 warning ("shadowing previous type declaration of `%#D'", decl);
8146 set_identifier_type_value (DECL_NAME (decl), type);
8147 CLASSTYPE_GOT_SEMICOLON (type) = 1;
8150 /* If we have installed this as the canonical typedef for this
8151 type, and that type has not been defined yet, delay emitting
8152 the debug information for it, as we will emit it later. */
8153 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8154 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8155 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8157 rest_of_decl_compilation (decl, NULL,
8158 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8162 if (TREE_CODE (decl) != FUNCTION_DECL)
8163 ttype = target_type (type);
8165 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8166 && TYPE_NEEDS_CONSTRUCTING (type))
8168 /* Currently, GNU C++ puts constants in text space, making them
8169 impossible to initialize. In the future, one would hope for
8170 an operating system which understood the difference between
8171 initialization and the running of a program. */
8173 TREE_READONLY (decl) = 0;
8176 if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8178 /* This must override the asm specifier which was placed by
8179 grokclassfn. Lay this out fresh. */
8180 SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8181 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8182 make_decl_rtl (decl, asmspec);
8184 else if (TREE_CODE (decl) == RESULT_DECL)
8185 init = check_initializer (decl, init, flags);
8186 else if (TREE_CODE (decl) == VAR_DECL)
8188 /* Only PODs can have thread-local storage. Other types may require
8189 various kinds of non-trivial initialization. */
8190 if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8191 error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8192 decl, TREE_TYPE (decl));
8193 /* Convert the initializer to the type of DECL, if we have not
8194 already initialized DECL. */
8195 if (!DECL_INITIALIZED_P (decl)
8196 /* If !DECL_EXTERNAL then DECL is being defined. In the
8197 case of a static data member initialized inside the
8198 class-specifier, there can be an initializer even if DECL
8199 is *not* defined. */
8200 && (!DECL_EXTERNAL (decl) || init))
8202 init = check_initializer (decl, init, flags);
8203 /* Thread-local storage cannot be dynamically initialized. */
8204 if (DECL_THREAD_LOCAL (decl) && init)
8206 error ("`%D' is thread-local and so cannot be dynamically "
8207 "initialized", decl);
8214 The memory occupied by any object of static storage
8215 duration is zero-initialized at program startup before
8216 any other initialization takes place.
8218 We cannot create an appropriate initializer until after
8219 the type of DECL is finalized. If DECL_INITIAL is set,
8220 then the DECL is statically initialized, and any
8221 necessary zero-initialization has already been performed. */
8222 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8223 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8224 /*static_storage_p=*/true);
8225 /* Remember that the initialization for this variable has
8227 DECL_INITIALIZED_P (decl) = 1;
8229 /* If the variable has an array type, lay out the type, even if
8230 there is no initializer. It is valid to index through the
8231 array, and we must get TYPE_ALIGN set correctly on the array
8233 else if (TREE_CODE (type) == ARRAY_TYPE)
8237 /* Add this declaration to the statement-tree. This needs to happen
8238 after the call to check_initializer so that the DECL_STMT for a
8239 reference temp is added before the DECL_STMT for the reference itself. */
8240 if (building_stmt_tree ()
8241 && at_function_scope_p ()
8242 && TREE_CODE (decl) != RESULT_DECL)
8243 add_decl_stmt (decl);
8245 if (TREE_CODE (decl) == VAR_DECL)
8246 layout_var_decl (decl);
8248 /* Output the assembler code and/or RTL code for variables and functions,
8249 unless the type is an undefined structure or union.
8250 If not, it will get done when the type is completed. */
8251 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8252 || TREE_CODE (decl) == RESULT_DECL)
8254 if (TREE_CODE (decl) == VAR_DECL)
8255 maybe_commonize_var (decl);
8257 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8259 if (TREE_CODE (type) == FUNCTION_TYPE
8260 || TREE_CODE (type) == METHOD_TYPE)
8261 abstract_virtuals_error (decl,
8262 strip_array_types (TREE_TYPE (type)));
8264 abstract_virtuals_error (decl, strip_array_types (type));
8266 if (TREE_CODE (decl) == FUNCTION_DECL
8267 || TREE_TYPE (decl) == error_mark_node)
8268 /* No initialization required. */
8270 else if (DECL_EXTERNAL (decl)
8271 && ! (DECL_LANG_SPECIFIC (decl)
8272 && DECL_NOT_REALLY_EXTERN (decl)))
8275 DECL_INITIAL (decl) = init;
8279 /* A variable definition. */
8280 if (DECL_FUNCTION_SCOPE_P (decl))
8282 /* This is a local declaration. */
8283 if (doing_semantic_analysis_p ())
8284 maybe_inject_for_scope_var (decl);
8285 /* Initialize the local variable. */
8286 if (processing_template_decl)
8288 if (init || DECL_INITIAL (decl) == error_mark_node)
8289 DECL_INITIAL (decl) = init;
8291 else if (!TREE_STATIC (decl))
8292 initialize_local_var (decl, init);
8295 if (TREE_STATIC (decl))
8296 expand_static_init (decl, init);
8300 /* Undo call to `pushclass' that was done in `start_decl'
8301 due to initialization of qualified member variable.
8302 I.e., Foo::x = 10; */
8304 tree context = CP_DECL_CONTEXT (decl);
8307 && (TREE_CODE (decl) == VAR_DECL
8308 /* We also have a pushclass done that we need to undo here
8309 if we're at top level and declare a method. */
8310 || TREE_CODE (decl) == FUNCTION_DECL)
8311 /* If size hasn't been set, we're still defining it,
8312 and therefore inside the class body; don't pop
8313 the binding level.. */
8314 && COMPLETE_TYPE_P (context)
8315 && context == current_class_type)
8316 pop_nested_class ();
8323 TREE_READONLY (decl) = 1;
8326 /* This is here for a midend callback from c-common.c */
8329 finish_decl (tree decl, tree init, tree asmspec_tree)
8331 cp_finish_decl (decl, init, asmspec_tree, 0);
8334 /* Returns a declaration for a VAR_DECL as if:
8336 extern "C" TYPE NAME;
8338 had been seen. Used to create compiler-generated global
8342 declare_global_var (tree name, tree type)
8346 push_to_top_level ();
8347 decl = build_decl (VAR_DECL, name, type);
8348 TREE_PUBLIC (decl) = 1;
8349 DECL_EXTERNAL (decl) = 1;
8350 DECL_ARTIFICIAL (decl) = 1;
8352 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8353 pop_from_top_level ();
8358 /* Returns a pointer to the `atexit' function. Note that if
8359 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8360 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8363 get_atexit_node (void)
8374 if (flag_use_cxa_atexit)
8376 /* The declaration for `__cxa_atexit' is:
8378 int __cxa_atexit (void (*)(void *), void *, void *)
8380 We build up the argument types and then then function type
8383 /* First, build the pointer-to-function type for the first
8385 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8386 fn_type = build_function_type (void_type_node, arg_types);
8387 fn_ptr_type = build_pointer_type (fn_type);
8388 /* Then, build the rest of the argument types. */
8389 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8390 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8391 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8392 /* And the final __cxa_atexit type. */
8393 fn_type = build_function_type (integer_type_node, arg_types);
8394 fn_ptr_type = build_pointer_type (fn_type);
8395 name = "__cxa_atexit";
8399 /* The declaration for `atexit' is:
8401 int atexit (void (*)());
8403 We build up the argument types and then then function type
8405 fn_type = build_function_type (void_type_node, void_list_node);
8406 fn_ptr_type = build_pointer_type (fn_type);
8407 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8408 /* Build the final atexit type. */
8409 fn_type = build_function_type (integer_type_node, arg_types);
8413 /* Now, build the function declaration. */
8414 push_lang_context (lang_name_c);
8415 atexit_fndecl = build_library_fn_ptr (name, fn_type);
8416 mark_used (atexit_fndecl);
8417 pop_lang_context ();
8418 atexit_node = default_conversion (atexit_fndecl);
8423 /* Returns the __dso_handle VAR_DECL. */
8426 get_dso_handle_node (void)
8428 if (dso_handle_node)
8429 return dso_handle_node;
8431 /* Declare the variable. */
8432 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8435 return dso_handle_node;
8438 /* Begin a new function with internal linkage whose job will be simply
8439 to destroy some particular variable. */
8442 start_cleanup_fn (void)
8444 static int counter = 0;
8445 int old_interface_only = interface_only;
8446 int old_interface_unknown = interface_unknown;
8452 push_to_top_level ();
8454 /* No need to mangle this. */
8455 push_lang_context (lang_name_c);
8458 interface_unknown = 1;
8460 /* Build the parameter-types. */
8461 parmtypes = void_list_node;
8462 /* Functions passed to __cxa_atexit take an additional parameter.
8463 We'll just ignore it. After we implement the new calling
8464 convention for destructors, we can eliminate the use of
8465 additional cleanup functions entirely in the -fnew-abi case. */
8466 if (flag_use_cxa_atexit)
8467 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8468 /* Build the function type itself. */
8469 fntype = build_function_type (void_type_node, parmtypes);
8470 /* Build the name of the function. */
8471 sprintf (name, "__tcf_%d", counter++);
8472 /* Build the function declaration. */
8473 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8474 /* It's a function with internal linkage, generated by the
8476 TREE_PUBLIC (fndecl) = 0;
8477 DECL_ARTIFICIAL (fndecl) = 1;
8478 /* Make the function `inline' so that it is only emitted if it is
8479 actually needed. It is unlikely that it will be inlined, since
8480 it is only called via a function pointer, but we avoid unnecessary
8481 emissions this way. */
8482 DECL_INLINE (fndecl) = 1;
8483 /* Build the parameter. */
8484 if (flag_use_cxa_atexit)
8488 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8489 DECL_CONTEXT (parmdecl) = fndecl;
8490 TREE_USED (parmdecl) = 1;
8491 DECL_ARGUMENTS (fndecl) = parmdecl;
8495 start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8497 interface_unknown = old_interface_unknown;
8498 interface_only = old_interface_only;
8500 pop_lang_context ();
8502 return current_function_decl;
8505 /* Finish the cleanup function begun by start_cleanup_fn. */
8508 end_cleanup_fn (void)
8510 expand_body (finish_function (0));
8512 pop_from_top_level ();
8515 /* Generate code to handle the destruction of DECL, an object with
8516 static storage duration. */
8519 register_dtor_fn (tree decl)
8525 int saved_flag_access_control;
8527 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8530 /* Call build_cleanup before we enter the anonymous function so that
8531 any access checks will be done relative to the current scope,
8532 rather than the scope of the anonymous function. */
8533 build_cleanup (decl);
8535 /* Now start the function. */
8536 cleanup = start_cleanup_fn ();
8538 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8539 to the original function, rather than the anonymous one. That
8540 will make the back-end think that nested functions are in use,
8541 which causes confusion. */
8542 saved_flag_access_control = flag_access_control;
8543 scope_chain->check_access = flag_access_control = 0;
8544 fcall = build_cleanup (decl);
8545 scope_chain->check_access = flag_access_control = saved_flag_access_control;
8547 /* Create the body of the anonymous function. */
8548 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
8549 finish_expr_stmt (fcall);
8550 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
8553 /* Call atexit with the cleanup function. */
8554 cxx_mark_addressable (cleanup);
8555 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8556 if (flag_use_cxa_atexit)
8558 args = tree_cons (NULL_TREE,
8559 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8561 args = tree_cons (NULL_TREE, null_pointer_node, args);
8562 args = tree_cons (NULL_TREE, cleanup, args);
8565 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8566 finish_expr_stmt (build_function_call (get_atexit_node (), args));
8569 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8570 is its initializer. Generate code to handle the construction
8571 and destruction of DECL. */
8574 expand_static_init (tree decl, tree init)
8578 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8579 my_friendly_assert (TREE_STATIC (decl), 20021010);
8581 /* Some variables require no initialization. */
8583 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8584 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8587 oldstatic = value_member (decl, static_aggregates);
8591 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8592 error ("multiple initializations given for `%D'", decl);
8594 else if (! toplevel_bindings_p ())
8596 /* Emit code to perform this initialization but once. */
8603 /* Emit code to perform this initialization but once. This code
8606 static int guard = 0;
8608 // Do initialization.
8610 // Register variable for destruction at end of program.
8613 Note that the `temp' variable is only set to 1 *after* the
8614 initialization is complete. This ensures that an exception,
8615 thrown during the construction, will cause the variable to
8616 reinitialized when we pass through this code again, as per:
8620 If the initialization exits by throwing an exception, the
8621 initialization is not complete, so it will be tried again
8622 the next time control enters the declaration.
8624 In theory, this process should be thread-safe, too; multiple
8625 threads should not be able to initialize the variable more
8626 than once. We don't yet attempt to ensure thread-safety. */
8628 /* Create the guard variable. */
8629 guard = get_guard (decl);
8631 /* Begin the conditional initialization. */
8632 if_stmt = begin_if_stmt ();
8633 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8634 then_clause = begin_compound_stmt (/*has_no_scope=*/0);
8636 /* Do the initialization itself. */
8637 assignment = init ? init : NULL_TREE;
8639 /* Once the assignment is complete, set TEMP to 1. Since the
8640 construction of the static object is complete at this point,
8641 we want to make sure TEMP is set to 1 even if a temporary
8642 constructed during the initialization throws an exception
8643 when it is destroyed. So, we combine the initialization and
8644 the assignment to TEMP into a single expression, ensuring
8645 that when we call finish_expr_stmt the cleanups will not be
8646 run until after TEMP is set to 1. */
8647 guard_init = set_guard (guard);
8650 assignment = tree_cons (NULL_TREE, assignment,
8651 build_tree_list (NULL_TREE,
8653 assignment = build_compound_expr (assignment);
8656 assignment = guard_init;
8657 finish_expr_stmt (assignment);
8659 /* Use atexit to register a function for destroying this static
8661 register_dtor_fn (decl);
8663 finish_compound_stmt (/*has_no_scope=*/0, then_clause);
8664 finish_then_clause (if_stmt);
8668 static_aggregates = tree_cons (init, decl, static_aggregates);
8671 /* Finish the declaration of a catch-parameter. */
8674 start_handler_parms (tree declspecs, tree declarator)
8679 decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8681 if (decl == NULL_TREE)
8682 error ("invalid catch parameter");
8691 /* Make TYPE a complete type based on INITIAL_VALUE.
8692 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8693 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8696 complete_array_type (tree type, tree initial_value, int do_default)
8698 register tree maxindex = NULL_TREE;
8703 /* An array of character type can be initialized from a
8704 brace-enclosed string constant. */
8705 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8706 && TREE_CODE (initial_value) == CONSTRUCTOR
8707 && CONSTRUCTOR_ELTS (initial_value)
8708 && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8710 && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8711 initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8713 /* Note MAXINDEX is really the maximum index, one less than the
8715 if (TREE_CODE (initial_value) == STRING_CST)
8718 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8719 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8722 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8724 tree elts = CONSTRUCTOR_ELTS (initial_value);
8726 maxindex = ssize_int (-1);
8727 for (; elts; elts = TREE_CHAIN (elts))
8729 if (TREE_PURPOSE (elts))
8730 maxindex = TREE_PURPOSE (elts);
8732 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8734 maxindex = copy_node (maxindex);
8738 /* Make an error message unless that happened already. */
8739 if (initial_value != error_mark_node)
8742 initial_value = NULL_TREE;
8744 /* Prevent further error messages. */
8745 maxindex = build_int_2 (0, 0);
8752 maxindex = build_int_2 (0, 0);
8761 domain = build_index_type (maxindex);
8762 TYPE_DOMAIN (type) = domain;
8764 if (! TREE_TYPE (maxindex))
8765 TREE_TYPE (maxindex) = domain;
8767 itype = TREE_TYPE (initial_value);
8770 if (itype && !TYPE_DOMAIN (itype))
8771 TYPE_DOMAIN (itype) = domain;
8772 /* The type of the main variant should never be used for arrays
8773 of different sizes. It should only ever be completed with the
8774 size of the array. */
8775 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8776 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8779 /* Lay out the type now that we can get the real answer. */
8786 /* Return zero if something is declared to be a member of type
8787 CTYPE when in the context of CUR_TYPE. STRING is the error
8788 message to print in that case. Otherwise, quietly return 1. */
8791 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8793 if (ctype && ctype != cur_type)
8795 if (flags == DTOR_FLAG)
8796 error ("destructor for alien class `%T' cannot be a member",
8799 error ("constructor for alien class `%T' cannot be a member",
8806 /* Subroutine of `grokdeclarator'. */
8808 /* Generate errors possibly applicable for a given set of specifiers.
8809 This is for ARM $7.1.2. */
8812 bad_specifiers (tree object,
8821 error ("`%D' declared as a `virtual' %s", object, type);
8823 error ("`%D' declared as an `inline' %s", object, type);
8825 error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8828 cp_error_at ("`%D' declared as a friend", object);
8830 && (TREE_CODE (object) == TYPE_DECL
8831 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8832 && !TYPE_REFFN_P (TREE_TYPE (object))
8833 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8834 cp_error_at ("`%D' declared with an exception specification", object);
8837 /* CTYPE is class type, or null if non-class.
8838 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8840 DECLARATOR is the function's name.
8841 VIRTUALP is truthvalue of whether the function is virtual or not.
8842 FLAGS are to be passed through to `grokclassfn'.
8843 QUALS are qualifiers indicating whether the function is `const'
8845 RAISES is a list of exceptions that this function can raise.
8846 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8847 not look, and -1 if we should not call `grokclassfn' at all.
8849 Returns `NULL_TREE' if something goes wrong, after issuing
8850 applicable error messages. */
8853 grokfndecl (tree ctype,
8856 tree orig_declarator,
8858 enum overload_flags flags,
8870 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8871 int has_default_arg = 0;
8875 type = build_exception_variant (type, raises);
8877 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8878 /* Propagate volatile out from type to decl. */
8879 if (TYPE_VOLATILE (type))
8880 TREE_THIS_VOLATILE (decl) = 1;
8882 /* If this decl has namespace scope, set that up. */
8884 set_decl_namespace (decl, in_namespace, friendp);
8886 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8888 /* `main' and builtins have implicit 'C' linkage. */
8889 if ((MAIN_NAME_P (declarator)
8890 || (IDENTIFIER_LENGTH (declarator) > 10
8891 && IDENTIFIER_POINTER (declarator)[0] == '_'
8892 && IDENTIFIER_POINTER (declarator)[1] == '_'
8893 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8894 && current_lang_name == lang_name_cplusplus
8895 && ctype == NULL_TREE
8896 /* NULL_TREE means global namespace. */
8897 && DECL_CONTEXT (decl) == NULL_TREE)
8898 SET_DECL_LANGUAGE (decl, lang_c);
8900 /* Should probably propagate const out from type to decl I bet (mrs). */
8903 DECL_STATIC_FUNCTION_P (decl) = 1;
8904 DECL_CONTEXT (decl) = ctype;
8908 DECL_CONTEXT (decl) = ctype;
8910 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8912 if (processing_template_decl)
8913 error ("cannot declare `::main' to be a template");
8915 error ("cannot declare `::main' to be inline");
8917 error ("cannot declare `::main' to be static");
8918 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8920 error ("`main' must return `int'");
8925 /* Members of anonymous types and local classes have no linkage; make
8927 /* FIXME what if it gets a name from typedef? */
8928 if (ctype && (TYPE_ANONYMOUS_P (ctype)
8929 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8934 /* [basic.link]: A name with no linkage (notably, the name of a class
8935 or enumeration declared in a local scope) shall not be used to
8936 declare an entity with linkage.
8938 Only check this for public decls for now. */
8939 t = no_linkage_check (TREE_TYPE (decl));
8942 if (TYPE_ANONYMOUS_P (t))
8944 if (DECL_EXTERN_C_P (decl))
8945 /* Allow this; it's pretty common in C. */;
8948 pedwarn ("non-local function `%#D' uses anonymous type",
8950 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8952 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8957 pedwarn ("non-local function `%#D' uses local type `%T'",
8962 TREE_PUBLIC (decl) = publicp;
8965 DECL_INTERFACE_KNOWN (decl) = 1;
8966 DECL_NOT_REALLY_EXTERN (decl) = 1;
8969 /* If the declaration was declared inline, mark it as such. */
8971 DECL_DECLARED_INLINE_P (decl) = 1;
8972 /* We inline functions that are explicitly declared inline, or, when
8973 the user explicitly asks us to, all functions. */
8974 if (DECL_DECLARED_INLINE_P (decl) || flag_inline_trees == 2)
8975 DECL_INLINE (decl) = 1;
8977 DECL_EXTERNAL (decl) = 1;
8978 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8980 error ("%smember function `%D' cannot have `%T' method qualifier",
8981 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8985 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8986 grok_op_properties (decl, friendp);
8988 if (ctype && decl_function_context (decl))
8989 DECL_NO_STATIC_CHAIN (decl) = 1;
8991 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8992 if (TREE_PURPOSE (t)
8993 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8995 has_default_arg = 1;
9000 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9004 ("defining explicit specialization `%D' in friend declaration",
9008 tree fns = TREE_OPERAND (orig_declarator, 0);
9009 tree args = TREE_OPERAND (orig_declarator, 1);
9011 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9013 /* Something like `template <class T> friend void f<T>()'. */
9014 error ("invalid use of template-id `%D' in declaration of primary template",
9020 /* A friend declaration of the form friend void f<>(). Record
9021 the information in the TEMPLATE_ID_EXPR. */
9022 SET_DECL_IMPLICIT_INSTANTIATION (decl);
9024 if (TREE_CODE (fns) == COMPONENT_REF)
9026 /* Due to bison parser ickiness, we will have already looked
9027 up an operator_name or PFUNCNAME within the current class
9028 (see template_id in parse.y). If the current class contains
9029 such a name, we'll get a COMPONENT_REF here. Undo that. */
9031 my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
9032 == current_class_type, 20001120);
9033 fns = TREE_OPERAND (fns, 1);
9035 my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
9036 || TREE_CODE (fns) == LOOKUP_EXPR
9037 || TREE_CODE (fns) == OVERLOAD, 20001120);
9038 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
9040 if (has_default_arg)
9042 error ("default arguments are not allowed in declaration of friend template specialization `%D'",
9049 error ("`inline' is not allowed in declaration of friend template specialization `%D'",
9057 /* Make the init_value nonzero so pushdecl knows this is not
9058 tentative. error_mark_node is replaced later with the BLOCK. */
9059 DECL_INITIAL (decl) = error_mark_node;
9061 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9062 TREE_NOTHROW (decl) = 1;
9064 /* Caller will do the rest of this. */
9068 if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
9069 DECL_CONSTRUCTOR_P (decl) = 1;
9071 /* Function gets the ugly name, field gets the nice one. This call
9072 may change the type of the function (because of default
9074 if (ctype != NULL_TREE)
9075 grokclassfn (ctype, decl, flags, quals);
9077 decl = check_explicit_specialization (orig_declarator, decl,
9079 2 * (funcdef_flag != 0) +
9080 4 * (friendp != 0));
9081 if (decl == error_mark_node)
9084 if (ctype != NULL_TREE
9085 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9090 old_decl = check_classfn (ctype, decl);
9092 if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9093 /* Because grokfndecl is always supposed to return a
9094 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9095 here. We depend on our callers to figure out that its
9096 really a template that's being returned. */
9097 old_decl = DECL_TEMPLATE_RESULT (old_decl);
9099 if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9100 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9102 /* Remove the `this' parm added by grokclassfn.
9103 XXX Isn't this done in start_function, too? */
9104 revert_static_member_fn (decl);
9105 last_function_parms = TREE_CHAIN (last_function_parms);
9107 if (old_decl && DECL_ARTIFICIAL (old_decl))
9108 error ("definition of implicitly-declared `%D'", old_decl);
9112 /* Since we've smashed OLD_DECL to its
9113 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9114 if (TREE_CODE (decl) == TEMPLATE_DECL)
9115 decl = DECL_TEMPLATE_RESULT (decl);
9117 /* Attempt to merge the declarations. This can fail, in
9118 the case of some invalid specialization declarations. */
9119 if (!duplicate_decls (decl, old_decl))
9120 error ("no `%#D' member function declared in class `%T'",
9126 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9129 if (ctype == NULL_TREE || check)
9133 DECL_VIRTUAL_P (decl) = 1;
9138 /* Create a VAR_DECL named NAME with the indicated TYPE.
9140 If SCOPE is non-NULL, it is the class type or namespace containing
9141 the variable. If SCOPE is NULL, the variable should is created in
9142 the innermost enclosings scope. */
9145 grokvardecl (tree type,
9147 RID_BIT_TYPE * specbits_in,
9153 RID_BIT_TYPE specbits;
9155 my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE,
9158 specbits = *specbits_in;
9160 /* Compute the scope in which to place the variable. */
9163 /* An explicit "extern" specifier indicates a namespace-scope
9165 if (RIDBIT_SETP (RID_EXTERN, specbits))
9166 scope = current_namespace;
9167 else if (!at_function_scope_p ())
9169 scope = current_scope ();
9171 scope = current_namespace;
9176 && (/* If the variable is a namespace-scope variable declared in a
9177 template, we need DECL_LANG_SPECIFIC. */
9178 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9179 /* Similarly for namespace-scope variables with language linkage
9181 || (TREE_CODE (scope) == NAMESPACE_DECL
9182 && current_lang_name != lang_name_cplusplus)
9183 /* Similarly for static data members. */
9185 decl = build_lang_decl (VAR_DECL, name, type);
9187 decl = build_decl (VAR_DECL, name, type);
9189 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9190 set_decl_namespace (decl, scope, 0);
9192 DECL_CONTEXT (decl) = scope;
9194 if (name && scope && current_lang_name != lang_name_c)
9195 /* We can't mangle lazily here because we don't have any
9196 way to recover whether or not a variable was `extern
9200 if (RIDBIT_SETP (RID_EXTERN, specbits))
9202 DECL_THIS_EXTERN (decl) = 1;
9203 DECL_EXTERNAL (decl) = !initialized;
9206 /* In class context, static means one per class,
9207 public access, and static storage. */
9208 if (DECL_CLASS_SCOPE_P (decl))
9210 TREE_PUBLIC (decl) = 1;
9211 TREE_STATIC (decl) = 1;
9212 DECL_EXTERNAL (decl) = 0;
9214 /* At top level, either `static' or no s.c. makes a definition
9215 (perhaps tentative), and absence of `static' makes it public. */
9216 else if (toplevel_bindings_p ())
9218 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9219 && (DECL_THIS_EXTERN (decl) || ! constp));
9220 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9222 /* Not at top level, only `static' makes a static definition. */
9225 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9226 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9229 if (RIDBIT_SETP (RID_THREAD, specbits))
9231 if (targetm.have_tls)
9232 DECL_THREAD_LOCAL (decl) = 1;
9234 /* A mere warning is sure to result in improper semantics
9235 at runtime. Don't bother to allow this to compile. */
9236 error ("thread-local storage not supported for this target");
9239 if (TREE_PUBLIC (decl))
9241 /* [basic.link]: A name with no linkage (notably, the name of a class
9242 or enumeration declared in a local scope) shall not be used to
9243 declare an entity with linkage.
9245 Only check this for public decls for now. */
9246 tree t = no_linkage_check (TREE_TYPE (decl));
9249 if (TYPE_ANONYMOUS_P (t))
9250 /* Ignore for now; `enum { foo } e' is pretty common. */;
9252 pedwarn ("non-local variable `%#D' uses local type `%T'",
9260 /* Create and return a canonical pointer to member function type, for
9261 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9264 build_ptrmemfunc_type (tree type)
9268 tree unqualified_variant = NULL_TREE;
9270 if (type == error_mark_node)
9273 /* If a canonical type already exists for this type, use it. We use
9274 this method instead of type_hash_canon, because it only does a
9275 simple equality check on the list of field members. */
9277 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9280 /* Make sure that we always have the unqualified pointer-to-member
9282 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9284 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9286 t = make_aggr_type (RECORD_TYPE);
9287 /* Let the front-end know this is a pointer to member function... */
9288 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9289 /* ... and not really an aggregate. */
9290 SET_IS_AGGR_TYPE (t, 0);
9292 field = build_decl (FIELD_DECL, pfn_identifier, type);
9295 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9296 TREE_CHAIN (field) = fields;
9299 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9301 /* Zap out the name so that the back-end will give us the debugging
9302 information for this anonymous RECORD_TYPE. */
9303 TYPE_NAME (t) = NULL_TREE;
9305 /* If this is not the unqualified form of this pointer-to-member
9306 type, set the TYPE_MAIN_VARIANT for this type to be the
9307 unqualified type. Since they are actually RECORD_TYPEs that are
9308 not variants of each other, we must do this manually. */
9309 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9311 t = build_qualified_type (t, cp_type_quals (type));
9312 TYPE_MAIN_VARIANT (t) = unqualified_variant;
9313 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9314 TYPE_NEXT_VARIANT (unqualified_variant) = t;
9317 /* Cache this pointer-to-member type so that we can find it again
9319 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9321 /* Seems to be wanted. */
9322 CLASSTYPE_GOT_SEMICOLON (t) = 1;
9327 /* Create and return a pointer to data member type. */
9330 build_ptrmem_type (tree class_type, tree member_type)
9332 return build_pointer_type (build_offset_type (class_type, member_type));
9335 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9336 Check to see that the definition is valid. Issue appropriate error
9337 messages. Return 1 if the definition is particularly bad, or 0
9341 check_static_variable_definition (tree decl, tree type)
9343 /* Motion 10 at San Diego: If a static const integral data member is
9344 initialized with an integral constant expression, the initializer
9345 may appear either in the declaration (within the class), or in
9346 the definition, but not both. If it appears in the class, the
9347 member is a member constant. The file-scope definition is always
9349 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9351 error ("invalid in-class initialization of static data member of non-integral type `%T'",
9353 /* If we just return the declaration, crashes will sometimes
9354 occur. We therefore return void_type_node, as if this was a
9355 friend declaration, to cause callers to completely ignore
9356 this declaration. */
9359 else if (!CP_TYPE_CONST_P (type))
9360 error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9362 else if (pedantic && !INTEGRAL_TYPE_P (type))
9363 pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9368 /* Given the SIZE (i.e., number of elements) in an array, compute an
9369 appropriate index type for the array. If non-NULL, NAME is the
9370 name of the thing being declared. */
9373 compute_array_index_type (tree name, tree size)
9377 /* If this involves a template parameter, it will be a constant at
9378 instantiation time, but we don't know what the value is yet.
9379 Even if no template parameters are involved, we may an expression
9380 that is not a constant; we don't even simplify `1 + 2' when
9381 processing a template. */
9382 if (processing_template_decl)
9384 /* Resolve a qualified reference to an enumerator or static
9385 const data member of ours. */
9386 if (TREE_CODE (size) == SCOPE_REF
9387 && TREE_OPERAND (size, 0) == current_class_type)
9389 tree t = lookup_field (current_class_type,
9390 TREE_OPERAND (size, 1), 0, 0);
9395 return build_index_type (build_min (MINUS_EXPR, sizetype,
9396 size, integer_one_node));
9399 /* The size might be the result of a cast. */
9400 STRIP_TYPE_NOPS (size);
9402 /* It might be a const variable or enumeration constant. */
9403 size = decl_constant_value (size);
9405 /* The array bound must be an integer type. */
9406 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9407 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9408 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9411 error ("size of array `%D' has non-integer type", name);
9413 error ("size of array has non-integer type");
9414 size = integer_one_node;
9417 /* Normally, the array-bound will be a constant. */
9418 if (TREE_CODE (size) == INTEGER_CST)
9420 /* Check to see if the array bound overflowed. Make that an
9421 error, no matter how generous we're being. */
9422 int old_flag_pedantic_errors = flag_pedantic_errors;
9423 int old_pedantic = pedantic;
9424 pedantic = flag_pedantic_errors = 1;
9425 constant_expression_warning (size);
9426 pedantic = old_pedantic;
9427 flag_pedantic_errors = old_flag_pedantic_errors;
9429 /* An array must have a positive number of elements. */
9430 if (INT_CST_LT (size, integer_zero_node))
9433 error ("size of array `%D' is negative", name);
9435 error ("size of array is negative");
9436 size = integer_one_node;
9438 /* Except that an extension we allow zero-sized arrays. We
9439 always allow them in system headers because glibc uses
9441 else if (integer_zerop (size) && pedantic && !in_system_header)
9444 pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9446 pedwarn ("ISO C++ forbids zero-size array");
9449 else if (TREE_CONSTANT (size))
9451 /* `(int) &fn' is not a valid array bound. */
9453 error ("size of array `%D' is not an integral constant-expression",
9456 error ("size of array is not an integral constant-expression");
9459 /* Compute the index of the largest element in the array. It is
9460 one less than the number of elements in the array. */
9462 = fold (cp_build_binary_op (MINUS_EXPR,
9463 cp_convert (ssizetype, size),
9464 cp_convert (ssizetype,
9465 integer_one_node)));
9467 /* Check for variable-sized arrays. We allow such things as an
9468 extension, even though they are not allowed in ANSI/ISO C++. */
9469 if (!TREE_CONSTANT (itype))
9474 pedwarn ("ISO C++ forbids variable-size array `%D'",
9477 pedwarn ("ISO C++ forbids variable-size array");
9480 /* Create a variable-sized array index type. */
9481 itype = variable_size (itype);
9483 /* Make sure that there was no overflow when creating to a signed
9484 index type. (For example, on a 32-bit machine, an array with
9485 size 2^32 - 1 is too big.) */
9486 else if (TREE_OVERFLOW (itype))
9488 error ("overflow in array dimension");
9489 TREE_OVERFLOW (itype) = 0;
9492 /* Create and return the appropriate index type. */
9493 return build_index_type (itype);
9496 /* Returns the scope (if any) in which the entity declared by
9497 DECLARATOR will be located. If the entity was declared with an
9498 unqualified name, NULL_TREE is returned. */
9501 get_scope_of_declarator (tree declarator)
9506 switch (TREE_CODE (declarator))
9512 /* For any of these, the main declarator is the first operand. */
9513 return get_scope_of_declarator (TREE_OPERAND
9517 /* For a pointer-to-member, continue descending. */
9518 if (TREE_CODE (TREE_OPERAND (declarator, 1))
9520 return get_scope_of_declarator (TREE_OPERAND
9522 /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9523 which the declaration occurs is the first operand. */
9524 return TREE_OPERAND (declarator, 0);
9527 /* Attributes to be applied. The declarator is TREE_VALUE. */
9528 return get_scope_of_declarator (TREE_VALUE (declarator));
9531 /* Otherwise, we have a declarator-id which is not a qualified
9532 name; the entity will be declared in the current scope. */
9537 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9538 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9542 create_array_type_for_decl (tree name, tree type, tree size)
9544 tree itype = NULL_TREE;
9545 const char* error_msg;
9547 /* If things have already gone awry, bail now. */
9548 if (type == error_mark_node || size == error_mark_node)
9549 return error_mark_node;
9551 /* Assume that everything will go OK. */
9554 /* There are some types which cannot be array elements. */
9555 switch (TREE_CODE (type))
9558 error_msg = "array of void";
9562 error_msg = "array of functions";
9565 case REFERENCE_TYPE:
9566 error_msg = "array of references";
9570 error_msg = "array of data members";
9574 error_msg = "array of function members";
9581 /* If something went wrong, issue an error-message and return. */
9585 error ("declaration of `%D' as %s", name, error_msg);
9587 error ("creating %s", error_msg);
9589 return error_mark_node;
9594 The constant expressions that specify the bounds of the arrays
9595 can be omitted only for the first member of the sequence. */
9596 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9599 error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9602 error ("multidimensional array must have bounds for all dimensions except the first");
9604 return error_mark_node;
9607 /* Figure out the index type for the array. */
9609 itype = compute_array_index_type (name, size);
9611 return build_cplus_array_type (type, itype);
9614 /* Check that it's OK to declare a function with the indicated TYPE.
9615 SFK indicates the kind of special function (if any) that this
9616 function is. OPTYPE is the type given in a conversion operator
9617 declaration. Returns the actual return type of the function; that
9618 may be different than TYPE if an error occurs, or for certain
9619 special functions. */
9622 check_special_function_return_type (special_function_kind sfk,
9628 case sfk_constructor:
9630 error ("return type specification for constructor invalid");
9632 type = void_type_node;
9635 case sfk_destructor:
9637 error ("return type specification for destructor invalid");
9638 type = void_type_node;
9641 case sfk_conversion:
9642 if (type && !same_type_p (type, optype))
9643 error ("operator `%T' declared to return `%T'", optype, type);
9645 pedwarn ("return type specified for `operator %T'", optype);
9657 /* Given declspecs and a declarator (abstract or otherwise), determine
9658 the name and type of the object declared and construct a DECL node
9661 DECLSPECS is a chain of tree_list nodes whose value fields
9662 are the storage classes and type specifiers.
9664 DECL_CONTEXT says which syntactic context this declaration is in:
9665 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9666 FUNCDEF for a function definition. Like NORMAL but a few different
9667 error messages in each case. Return value may be zero meaning
9668 this definition is too screwy to try to parse.
9669 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9670 handle member functions (which have FIELD context).
9671 Return value may be zero meaning this definition is too screwy to
9673 PARM for a parameter declaration (either within a function prototype
9674 or before a function body). Make a PARM_DECL, or return void_type_node.
9675 CATCHPARM for a parameter declaration before a catch clause.
9676 TYPENAME if for a typename (in a cast or sizeof).
9677 Don't make a DECL node; just return the ..._TYPE node.
9678 FIELD for a struct or union field; make a FIELD_DECL.
9679 BITFIELD for a field with specified width.
9680 INITIALIZED is 1 if the decl has an initializer.
9682 ATTRLIST is a pointer to the list of attributes, which may be NULL
9683 if there are none; *ATTRLIST may be modified if attributes from inside
9684 the declarator should be applied to the declaration.
9686 When this function is called, scoping variables (such as
9687 CURRENT_CLASS_TYPE) should reflect the scope in which the
9688 declaration occurs, not the scope in which the new declaration will
9689 be placed. For example, on:
9693 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9694 should not be `S'. */
9697 grokdeclarator (tree declarator,
9699 enum decl_context decl_context,
9703 RID_BIT_TYPE specbits;
9706 tree type = NULL_TREE;
9709 int virtualp, explicitp, friendp, inlinep, staticp;
9710 int explicit_int = 0;
9711 int explicit_char = 0;
9712 int defaulted_int = 0;
9713 int extern_langp = 0;
9714 tree dependant_name = NULL_TREE;
9716 tree typedef_decl = NULL_TREE;
9718 tree typedef_type = NULL_TREE;
9719 int funcdef_flag = 0;
9720 enum tree_code innermost_code = ERROR_MARK;
9723 /* See the code below that used this. */
9724 tree decl_attr = NULL_TREE;
9726 /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
9727 All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */
9728 tree init = NULL_TREE;
9730 /* Keep track of what sort of function is being processed
9731 so that we can warn about default return values, or explicit
9732 return values which do not match prescribed defaults. */
9733 special_function_kind sfk = sfk_none;
9735 tree dname = NULL_TREE;
9736 tree ctype = current_class_type;
9737 tree ctor_return_type = NULL_TREE;
9738 enum overload_flags flags = NO_SPECIAL;
9739 tree quals = NULL_TREE;
9740 tree raises = NULL_TREE;
9741 int template_count = 0;
9742 tree in_namespace = NULL_TREE;
9743 tree returned_attrs = NULL_TREE;
9744 tree scope = NULL_TREE;
9746 RIDBIT_RESET_ALL (specbits);
9747 if (decl_context == FUNCDEF)
9748 funcdef_flag = 1, decl_context = NORMAL;
9749 else if (decl_context == MEMFUNCDEF)
9750 funcdef_flag = -1, decl_context = FIELD;
9751 else if (decl_context == BITFIELD)
9752 bitfield = 1, decl_context = FIELD;
9754 /* Look inside a declarator for the name being declared
9755 and get it as a string, for an error message. */
9757 tree *next = &declarator;
9761 while (next && *next)
9764 switch (TREE_CODE (decl))
9767 /* For attributes. */
9768 next = &TREE_VALUE (decl);
9773 next = &TREE_OPERAND (decl, 0);
9776 case BIT_NOT_EXPR: /* For C++ destructors! */
9778 tree name = TREE_OPERAND (decl, 0);
9779 tree rename = NULL_TREE;
9781 my_friendly_assert (flags == NO_SPECIAL, 152);
9783 sfk = sfk_destructor;
9785 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9786 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9787 if (ctype == NULL_TREE)
9789 if (current_class_type == NULL_TREE)
9791 error ("destructors must be member functions");
9796 tree t = constructor_name (current_class_type);
9803 tree t = constructor_name (ctype);
9810 error ("destructor `%T' must match class name `%T'",
9812 TREE_OPERAND (decl, 0) = rename;
9818 case ADDR_EXPR: /* C++ reference declaration */
9823 innermost_code = TREE_CODE (decl);
9824 next = &TREE_OPERAND (decl, 0);
9828 if (parmlist_is_exprlist (CALL_DECLARATOR_PARMS (decl)))
9830 /* This is actually a variable declaration using
9831 constructor syntax. We need to call start_decl and
9832 cp_finish_decl so we can get the variable
9837 if (decl_context != NORMAL)
9839 error ("variable declaration is not allowed here");
9840 return error_mark_node;
9843 *next = TREE_OPERAND (decl, 0);
9844 init = CALL_DECLARATOR_PARMS (decl);
9848 attributes = *attrlist;
9852 attributes = NULL_TREE;
9855 decl = start_decl (declarator, declspecs, 1,
9856 attributes, NULL_TREE);
9857 decl_type_access_control (decl);
9860 /* Look for __unused__ attribute */
9861 if (TREE_USED (TREE_TYPE (decl)))
9862 TREE_USED (decl) = 1;
9863 finish_decl (decl, init, NULL_TREE);
9866 error ("invalid declarator");
9869 innermost_code = TREE_CODE (decl);
9870 if (decl_context == FIELD && ctype == NULL_TREE)
9871 ctype = current_class_type;
9873 && TREE_OPERAND (decl, 0)
9874 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9875 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9877 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9878 next = &TREE_OPERAND (decl, 0);
9880 if (ctype != NULL_TREE
9881 && decl != NULL_TREE && flags != DTOR_FLAG
9882 && constructor_name_p (decl, ctype))
9884 sfk = sfk_constructor;
9885 ctor_return_type = ctype;
9890 case TEMPLATE_ID_EXPR:
9892 tree fns = TREE_OPERAND (decl, 0);
9894 if (TREE_CODE (fns) == LOOKUP_EXPR)
9895 fns = TREE_OPERAND (fns, 0);
9898 if (TREE_CODE (dname) == COMPONENT_REF)
9899 dname = TREE_OPERAND (dname, 1);
9900 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9902 my_friendly_assert (is_overloaded_fn (dname),
9904 dname = DECL_NAME (get_first_fn (dname));
9909 case IDENTIFIER_NODE:
9910 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9915 if (C_IS_RESERVED_WORD (dname))
9917 error ("declarator-id missing; using reserved word `%D'",
9919 name = IDENTIFIER_POINTER (dname);
9921 else if (!IDENTIFIER_TYPENAME_P (dname))
9922 name = IDENTIFIER_POINTER (dname);
9925 my_friendly_assert (flags == NO_SPECIAL, 154);
9926 flags = TYPENAME_FLAG;
9927 ctor_return_type = TREE_TYPE (dname);
9928 sfk = sfk_conversion;
9929 if (IDENTIFIER_GLOBAL_VALUE (dname)
9930 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9932 name = IDENTIFIER_POINTER (dname);
9934 name = "<invalid operator>";
9941 /* Perform error checking, and decide on a ctype. */
9942 tree cname = TREE_OPERAND (decl, 0);
9943 if (cname == NULL_TREE)
9945 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9948 in_namespace = TREE_OPERAND (decl, 0);
9950 else if (! is_aggr_type (cname, 1))
9952 /* Must test TREE_OPERAND (decl, 1), in case user gives
9953 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9954 else if (TREE_OPERAND (decl, 1)
9955 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9957 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9958 || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9960 /* This might be declaring a member of a template
9961 parm to be a friend. */
9963 dependant_name = TREE_OPERAND (decl, 1);
9965 else if (ctype == NULL_TREE)
9967 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9971 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9973 error ("type `%T' is not derived from type `%T'",
9981 /* It is valid to write:
9983 class C { void f(); };
9987 The standard is not clear about whether `typedef const C D' is
9988 legal; as of 2002-09-15 the committee is considering
9989 that question. EDG 3.0 allows that syntax.
9990 Therefore, we do as well. */
9992 ctype = TYPE_MAIN_VARIANT (ctype);
9993 /* Update the declarator so that when we process it
9994 again the correct type is present. */
9995 TREE_OPERAND (decl, 0) = ctype;
9997 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9998 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
10000 TREE_OPERAND (decl, 1) = constructor_name (ctype);
10001 next = &TREE_OPERAND (decl, 1);
10005 if (TREE_CODE (decl) == IDENTIFIER_NODE
10006 && constructor_name_p (decl, ctype))
10008 sfk = sfk_constructor;
10009 ctor_return_type = ctype;
10011 else if (TREE_CODE (decl) == BIT_NOT_EXPR
10012 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
10013 && constructor_name_p (TREE_OPERAND (decl, 0),
10016 sfk = sfk_destructor;
10017 ctor_return_type = ctype;
10019 TREE_OPERAND (decl, 0) = constructor_name (ctype);
10020 next = &TREE_OPERAND (decl, 0);
10031 /* Parse error puts this typespec where
10032 a declarator should go. */
10033 error ("`%T' specified as declarator-id", DECL_NAME (decl));
10034 if (TREE_TYPE (decl) == current_class_type)
10035 error (" perhaps you want `%T' for a constructor",
10036 current_class_name);
10037 dname = DECL_NAME (decl);
10038 name = IDENTIFIER_POINTER (dname);
10040 /* Avoid giving two errors for this. */
10041 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
10043 declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
10049 next = &BASELINK_FUNCTIONS (decl);
10052 case TEMPLATE_DECL:
10053 /* Sometimes, we see a template-name used as part of a
10054 decl-specifier like in
10055 std::allocator alloc;
10056 Handle that gracefully. */
10057 error ("invalid use of template-name '%E' in a declarator", decl);
10058 return error_mark_node;
10062 my_friendly_assert (0, 20020917);
10067 /* A function definition's declarator must have the form of
10068 a function declarator. */
10070 if (funcdef_flag && innermost_code != CALL_EXPR)
10073 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
10074 && innermost_code != CALL_EXPR
10075 && ! (ctype && declspecs == NULL_TREE))
10077 error ("declaration of `%D' as non-function", dname);
10078 return void_type_node;
10081 /* Anything declared one level down from the top level
10082 must be one of the parameters of a function
10083 (because the body is at least two levels down). */
10085 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10086 by not allowing C++ class definitions to specify their parameters
10087 with xdecls (must be spec.d in the parmlist).
10089 Since we now wait to push a class scope until we are sure that
10090 we are in a legitimate method context, we must set oldcname
10091 explicitly (since current_class_name is not yet alive).
10093 We also want to avoid calling this a PARM if it is in a namespace. */
10095 if (decl_context == NORMAL && !toplevel_bindings_p ())
10097 struct cp_binding_level *b = current_binding_level;
10098 current_binding_level = b->level_chain;
10099 if (current_binding_level != 0 && toplevel_bindings_p ())
10100 decl_context = PARM;
10101 current_binding_level = b;
10105 name = decl_context == PARM ? "parameter" : "type name";
10107 /* Look through the decl specs and record which ones appear.
10108 Some typespecs are defined as built-in typenames.
10109 Others, the ones that are modifiers of other types,
10110 are represented by bits in SPECBITS: set the bits for
10111 the modifiers that appear. Storage class keywords are also in SPECBITS.
10113 If there is a typedef name or a type, store the type in TYPE.
10114 This includes builtin typedefs such as `int'.
10116 Set EXPLICIT_INT if the type is `int' or `char' and did not
10117 come from a user typedef.
10119 Set LONGLONG if `long' is mentioned twice.
10121 For C++, constructors and destructors have their own fast treatment. */
10123 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10128 /* Certain parse errors slip through. For example,
10129 `int class;' is not caught by the parser. Try
10130 weakly to recover here. */
10131 if (TREE_CODE (spec) != TREE_LIST)
10134 id = TREE_VALUE (spec);
10136 /* If the entire declaration is itself tagged as deprecated then
10137 suppress reports of deprecated items. */
10138 if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10140 if (deprecated_state != DEPRECATED_SUPPRESS)
10141 warn_deprecated_use (id);
10144 if (TREE_CODE (id) == IDENTIFIER_NODE)
10146 if (id == ridpointers[(int) RID_INT]
10147 || id == ridpointers[(int) RID_CHAR]
10148 || id == ridpointers[(int) RID_BOOL]
10149 || id == ridpointers[(int) RID_WCHAR])
10153 if (id == ridpointers[(int) RID_BOOL])
10154 error ("`bool' is now a keyword");
10156 error ("extraneous `%T' ignored", id);
10160 if (id == ridpointers[(int) RID_INT])
10162 else if (id == ridpointers[(int) RID_CHAR])
10164 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10168 /* C++ aggregate types. */
10169 if (IDENTIFIER_HAS_TYPE_VALUE (id))
10172 error ("multiple declarations `%T' and `%T'", type, id);
10174 type = IDENTIFIER_TYPE_VALUE (id);
10178 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10180 if (ridpointers[i] == id)
10182 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10184 if (pedantic && ! in_system_header && warn_long_long)
10185 pedwarn ("ISO C++ does not support `long long'");
10187 error ("`long long long' is too long for GCC");
10191 else if (RIDBIT_SETP (i, specbits))
10192 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10194 /* Diagnose "__thread extern" or "__thread static". */
10195 if (RIDBIT_SETP (RID_THREAD, specbits))
10197 if (i == (int)RID_EXTERN)
10198 error ("`__thread' before `extern'");
10199 else if (i == (int)RID_STATIC)
10200 error ("`__thread' before `static'");
10203 if (i == (int)RID_EXTERN
10204 && TREE_PURPOSE (spec) == error_mark_node)
10205 /* This extern was part of a language linkage. */
10208 RIDBIT_SET (i, specbits);
10213 else if (TREE_CODE (id) == TYPE_DECL)
10216 error ("multiple declarations `%T' and `%T'", type,
10220 type = TREE_TYPE (id);
10221 TREE_VALUE (spec) = type;
10227 error ("two or more data types in declaration of `%s'", name);
10228 else if (TREE_CODE (id) == IDENTIFIER_NODE)
10230 register tree t = lookup_name (id, 1);
10231 if (!t || TREE_CODE (t) != TYPE_DECL)
10232 error ("`%s' fails to be a typedef or built in type",
10233 IDENTIFIER_POINTER (id));
10236 type = TREE_TYPE (t);
10240 else if (id != error_mark_node)
10241 /* Can't change CLASS nodes into RECORD nodes here! */
10248 /* See the code below that used this. */
10250 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10252 typedef_type = type;
10254 /* No type at all: default to `int', and set DEFAULTED_INT
10255 because it was not a user-defined typedef. */
10257 if (type == NULL_TREE
10258 && (RIDBIT_SETP (RID_SIGNED, specbits)
10259 || RIDBIT_SETP (RID_UNSIGNED, specbits)
10260 || RIDBIT_SETP (RID_LONG, specbits)
10261 || RIDBIT_SETP (RID_SHORT, specbits)))
10263 /* These imply 'int'. */
10264 type = integer_type_node;
10268 if (sfk != sfk_none)
10269 type = check_special_function_return_type (sfk, type,
10271 else if (type == NULL_TREE)
10277 /* We handle `main' specially here, because 'main () { }' is so
10278 common. With no options, it is allowed. With -Wreturn-type,
10279 it is a warning. It is only an error with -pedantic-errors. */
10280 is_main = (funcdef_flag
10281 && dname && MAIN_NAME_P (dname)
10282 && ctype == NULL_TREE
10283 && in_namespace == NULL_TREE
10284 && current_namespace == global_namespace);
10286 if (in_system_header || flag_ms_extensions)
10287 /* Allow it, sigh. */;
10288 else if (pedantic || ! is_main)
10289 pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10291 else if (warn_return_type)
10292 warning ("ISO C++ forbids declaration of `%s' with no type",
10295 type = integer_type_node;
10298 if (type && IMPLICIT_TYPENAME_P (type))
10300 /* The implicit typename extension is deprecated and will be
10301 removed. Warn about its use now. */
10302 warning ("`%T' is implicitly a typename", type);
10303 cp_deprecated ("implicit typename");
10305 /* Now remove its implicitness, so that we don't warn again.
10306 For instance this might be a typedef, and we do not want to
10307 warn on uses of the typedef itself. Simply clearing the
10308 TREE_TYPE is insufficient. */
10309 type = copy_node (type);
10310 TREE_TYPE (type) = NULL_TREE;
10315 /* Now process the modifiers that were specified
10316 and check for invalid combinations. */
10318 /* Long double is a special combination. */
10320 if (RIDBIT_SETP (RID_LONG, specbits)
10321 && TYPE_MAIN_VARIANT (type) == double_type_node)
10323 RIDBIT_RESET (RID_LONG, specbits);
10324 type = build_qualified_type (long_double_type_node,
10325 cp_type_quals (type));
10328 /* Check all other uses of type modifiers. */
10330 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10331 || RIDBIT_SETP (RID_SIGNED, specbits)
10332 || RIDBIT_SETP (RID_LONG, specbits)
10333 || RIDBIT_SETP (RID_SHORT, specbits))
10337 if (TREE_CODE (type) == REAL_TYPE)
10338 error ("short, signed or unsigned invalid for `%s'", name);
10339 else if (TREE_CODE (type) != INTEGER_TYPE)
10340 error ("long, short, signed or unsigned invalid for `%s'", name);
10341 else if (RIDBIT_SETP (RID_LONG, specbits)
10342 && RIDBIT_SETP (RID_SHORT, specbits))
10343 error ("long and short specified together for `%s'", name);
10344 else if ((RIDBIT_SETP (RID_LONG, specbits)
10345 || RIDBIT_SETP (RID_SHORT, specbits))
10347 error ("long or short specified with char for `%s'", name);
10348 else if ((RIDBIT_SETP (RID_LONG, specbits)
10349 || RIDBIT_SETP (RID_SHORT, specbits))
10350 && TREE_CODE (type) == REAL_TYPE)
10351 error ("long or short specified with floating type for `%s'", name);
10352 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10353 && RIDBIT_SETP (RID_UNSIGNED, specbits))
10354 error ("signed and unsigned given together for `%s'", name);
10358 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10360 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10362 if (flag_pedantic_errors)
10367 /* Discard the type modifiers if they are invalid. */
10370 RIDBIT_RESET (RID_UNSIGNED, specbits);
10371 RIDBIT_RESET (RID_SIGNED, specbits);
10372 RIDBIT_RESET (RID_LONG, specbits);
10373 RIDBIT_RESET (RID_SHORT, specbits);
10378 if (RIDBIT_SETP (RID_COMPLEX, specbits)
10379 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10381 error ("complex invalid for `%s'", name);
10382 RIDBIT_RESET (RID_COMPLEX, specbits);
10385 /* Decide whether an integer type is signed or not.
10386 Optionally treat bitfields as signed by default. */
10387 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10390 It is implementation-defined whether a plain (neither
10391 explicitly signed or unsigned) char, short, int, or long
10392 bit-field is signed or unsigned.
10394 Naturally, we extend this to long long as well. Note that
10395 this does not include wchar_t. */
10396 || (bitfield && !flag_signed_bitfields
10397 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10398 /* A typedef for plain `int' without `signed' can be
10399 controlled just like plain `int', but a typedef for
10400 `signed int' cannot be so controlled. */
10402 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10403 && (TREE_CODE (type) == INTEGER_TYPE
10404 || TREE_CODE (type) == CHAR_TYPE)
10405 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10408 type = long_long_unsigned_type_node;
10409 else if (RIDBIT_SETP (RID_LONG, specbits))
10410 type = long_unsigned_type_node;
10411 else if (RIDBIT_SETP (RID_SHORT, specbits))
10412 type = short_unsigned_type_node;
10413 else if (type == char_type_node)
10414 type = unsigned_char_type_node;
10415 else if (typedef_decl)
10416 type = c_common_unsigned_type (type);
10418 type = unsigned_type_node;
10420 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10421 && type == char_type_node)
10422 type = signed_char_type_node;
10424 type = long_long_integer_type_node;
10425 else if (RIDBIT_SETP (RID_LONG, specbits))
10426 type = long_integer_type_node;
10427 else if (RIDBIT_SETP (RID_SHORT, specbits))
10428 type = short_integer_type_node;
10430 if (RIDBIT_SETP (RID_COMPLEX, specbits))
10432 /* If we just have "complex", it is equivalent to
10433 "complex double", but if any modifiers at all are specified it is
10434 the complex form of TYPE. E.g, "complex short" is
10435 "complex short int". */
10437 if (defaulted_int && ! longlong
10438 && ! (RIDBIT_SETP (RID_LONG, specbits)
10439 || RIDBIT_SETP (RID_SHORT, specbits)
10440 || RIDBIT_SETP (RID_SIGNED, specbits)
10441 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10442 type = complex_double_type_node;
10443 else if (type == integer_type_node)
10444 type = complex_integer_type_node;
10445 else if (type == float_type_node)
10446 type = complex_float_type_node;
10447 else if (type == double_type_node)
10448 type = complex_double_type_node;
10449 else if (type == long_double_type_node)
10450 type = complex_long_double_type_node;
10452 type = build_complex_type (type);
10455 type_quals = TYPE_UNQUALIFIED;
10456 if (RIDBIT_SETP (RID_CONST, specbits))
10457 type_quals |= TYPE_QUAL_CONST;
10458 if (RIDBIT_SETP (RID_VOLATILE, specbits))
10459 type_quals |= TYPE_QUAL_VOLATILE;
10460 if (RIDBIT_SETP (RID_RESTRICT, specbits))
10461 type_quals |= TYPE_QUAL_RESTRICT;
10462 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10463 error ("qualifiers are not allowed on declaration of `operator %T'",
10466 type_quals |= cp_type_quals (type);
10467 type = cp_build_qualified_type_real
10468 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10469 ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10470 /* We might have ignored or rejected some of the qualifiers. */
10471 type_quals = cp_type_quals (type);
10474 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10475 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10476 RIDBIT_RESET (RID_VIRTUAL, specbits);
10477 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10478 RIDBIT_RESET (RID_EXPLICIT, specbits);
10480 if (RIDBIT_SETP (RID_STATIC, specbits))
10481 staticp = 1 + (decl_context == FIELD);
10483 if (virtualp && staticp == 2)
10485 error ("member `%D' cannot be declared both virtual and static",
10489 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10490 RIDBIT_RESET (RID_FRIEND, specbits);
10492 if (dependant_name && !friendp)
10494 error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10495 return void_type_node;
10498 /* Warn if two storage classes are given. Default to `auto'. */
10500 if (RIDBIT_ANY_SET (specbits))
10502 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10503 if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10504 if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10505 if (decl_context == PARM && nclasses > 0)
10506 error ("storage class specifiers invalid in parameter declarations");
10507 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10509 if (decl_context == PARM)
10510 error ("typedef declaration invalid in parameter declaration");
10513 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10514 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10515 if (!nclasses && !friendp && extern_langp)
10519 /* Give error if `virtual' is used outside of class declaration. */
10521 && (current_class_name == NULL_TREE || decl_context != FIELD))
10523 error ("virtual outside class declaration");
10527 /* Static anonymous unions are dealt with here. */
10528 if (staticp && decl_context == TYPENAME
10529 && TREE_CODE (declspecs) == TREE_LIST
10530 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10531 decl_context = FIELD;
10533 /* Warn about storage classes that are invalid for certain
10534 kinds of declarations (parameters, typenames, etc.). */
10536 /* "static __thread" and "extern __thread" are allowed. */
10538 && RIDBIT_SETP (RID_THREAD, specbits)
10539 && (RIDBIT_SETP (RID_EXTERN, specbits)
10540 || RIDBIT_SETP (RID_STATIC, specbits)))
10544 error ("multiple storage classes in declaration of `%s'", name);
10545 else if (decl_context != NORMAL && nclasses > 0)
10547 if ((decl_context == PARM || decl_context == CATCHPARM)
10548 && (RIDBIT_SETP (RID_REGISTER, specbits)
10549 || RIDBIT_SETP (RID_AUTO, specbits)))
10551 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10553 else if (decl_context == FIELD
10554 /* C++ allows static class elements */
10555 && RIDBIT_SETP (RID_STATIC, specbits))
10556 /* C++ also allows inlines and signed and unsigned elements,
10557 but in those cases we don't come in here. */
10561 if (decl_context == FIELD)
10563 tree tmp = NULL_TREE;
10564 register int op = 0;
10568 /* Avoid trying to get an operand off an identifier node. */
10569 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10572 tmp = TREE_OPERAND (declarator, 0);
10573 op = IDENTIFIER_OPNAME_P (tmp);
10574 if (IDENTIFIER_TYPENAME_P (tmp))
10576 if (IDENTIFIER_GLOBAL_VALUE (tmp)
10577 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10579 name = IDENTIFIER_POINTER (tmp);
10581 name = "<invalid operator>";
10584 error ("storage class specified for %s `%s'",
10585 op ? "member operator" : "field",
10590 if (decl_context == PARM || decl_context == CATCHPARM)
10591 error ("storage class specified for parameter `%s'", name);
10593 error ("storage class specified for typename");
10595 RIDBIT_RESET (RID_REGISTER, specbits);
10596 RIDBIT_RESET (RID_AUTO, specbits);
10597 RIDBIT_RESET (RID_EXTERN, specbits);
10598 RIDBIT_RESET (RID_THREAD, specbits);
10601 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10603 if (toplevel_bindings_p ())
10605 /* It's common practice (and completely valid) to have a const
10606 be initialized and declared extern. */
10607 if (!(type_quals & TYPE_QUAL_CONST))
10608 warning ("`%s' initialized and declared `extern'", name);
10611 error ("`%s' has both `extern' and initializer", name);
10613 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10614 && ! toplevel_bindings_p ())
10615 error ("nested function `%s' declared `extern'", name);
10616 else if (toplevel_bindings_p ())
10618 if (RIDBIT_SETP (RID_AUTO, specbits))
10619 error ("top-level declaration of `%s' specifies `auto'", name);
10621 else if (RIDBIT_SETP (RID_THREAD, specbits)
10622 && !RIDBIT_SETP (RID_EXTERN, specbits)
10623 && !RIDBIT_SETP (RID_STATIC, specbits))
10625 error ("function-scope `%s' implicitly auto and declared `__thread'",
10627 RIDBIT_RESET (RID_THREAD, specbits);
10630 if (nclasses > 0 && friendp)
10631 error ("storage class specifiers invalid in friend function declarations");
10633 scope = get_scope_of_declarator (declarator);
10635 /* Now figure out the structure of the declarator proper.
10636 Descend through it, creating more complex types, until we reach
10637 the declared identifier (or NULL_TREE, in an abstract declarator). */
10639 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10640 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10642 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10643 an INDIRECT_REF (for *...),
10644 a CALL_EXPR (for ...(...)),
10645 an identifier (for the name being declared)
10646 or a null pointer (for the place in an absolute declarator
10647 where the name was omitted).
10648 For the last two cases, we have just exited the loop.
10650 For C++ it could also be
10651 a SCOPE_REF (for class :: ...). In this case, we have converted
10652 sensible names to types, and those are the values we use to
10653 qualify the member name.
10654 an ADDR_EXPR (for &...),
10655 a BIT_NOT_EXPR (for destructors)
10657 At this point, TYPE is the type of elements of an array,
10658 or for a function to return, or for a pointer to point to.
10659 After this sequence of ifs, TYPE is the type of the
10660 array or function or pointer, and DECLARATOR has had its
10661 outermost layer removed. */
10663 if (type == error_mark_node)
10665 if (TREE_CODE (declarator) == SCOPE_REF)
10666 declarator = TREE_OPERAND (declarator, 1);
10668 declarator = TREE_OPERAND (declarator, 0);
10671 if (quals != NULL_TREE
10672 && (declarator == NULL_TREE
10673 || TREE_CODE (declarator) != SCOPE_REF))
10675 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10676 ctype = TYPE_METHOD_BASETYPE (type);
10677 if (ctype != NULL_TREE)
10679 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10680 grok_method_quals (ctype, dummy, quals);
10681 type = TREE_TYPE (dummy);
10686 switch (TREE_CODE (declarator))
10690 /* We encode a declarator with embedded attributes using
10692 tree attrs = TREE_PURPOSE (declarator);
10696 declarator = TREE_VALUE (declarator);
10697 inner_decl = declarator;
10698 while (inner_decl != NULL_TREE
10699 && TREE_CODE (inner_decl) == TREE_LIST)
10700 inner_decl = TREE_VALUE (inner_decl);
10702 if (inner_decl == NULL_TREE
10703 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10704 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10705 if (TREE_CODE (inner_decl) == CALL_EXPR)
10706 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10707 if (TREE_CODE (inner_decl) == ARRAY_REF)
10708 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10709 returned_attrs = decl_attributes (&type,
10710 chainon (returned_attrs, attrs),
10717 register tree size;
10719 size = TREE_OPERAND (declarator, 1);
10721 /* VC++ spells a zero-sized array with []. */
10722 if (size == NULL_TREE && decl_context == FIELD && ! staticp
10723 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
10724 size = integer_zero_node;
10726 declarator = TREE_OPERAND (declarator, 0);
10728 type = create_array_type_for_decl (dname, type, size);
10738 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10739 tree inner_decl = TREE_OPERAND (declarator, 0);
10741 /* Declaring a function type.
10742 Make sure we have a valid type for the function to return. */
10744 /* We now know that the TYPE_QUALS don't apply to the
10745 decl, but to its return type. */
10746 type_quals = TYPE_UNQUALIFIED;
10748 /* Warn about some types functions can't return. */
10750 if (TREE_CODE (type) == FUNCTION_TYPE)
10752 error ("`%s' declared as function returning a function", name);
10753 type = integer_type_node;
10755 if (TREE_CODE (type) == ARRAY_TYPE)
10757 error ("`%s' declared as function returning an array", name);
10758 type = integer_type_node;
10761 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10762 inner_decl = TREE_OPERAND (inner_decl, 1);
10764 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10765 inner_decl = dname;
10767 /* Pick up type qualifiers which should be applied to `this'. */
10768 quals = CALL_DECLARATOR_QUALS (declarator);
10770 /* Pick up the exception specifications. */
10771 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10773 /* Say it's a definition only for the CALL_EXPR
10774 closest to the identifier. */
10777 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10778 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10779 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10781 if (ctype == NULL_TREE
10782 && decl_context == FIELD
10784 && (friendp == 0 || dname == current_class_name))
10785 ctype = current_class_type;
10787 if (ctype && sfk == sfk_conversion)
10788 TYPE_HAS_CONVERSION (ctype) = 1;
10789 if (ctype && constructor_name_p (dname, ctype))
10791 /* We are within a class's scope. If our declarator name
10792 is the same as the class name, and we are defining
10793 a function, then it is a constructor/destructor, and
10794 therefore returns a void type. */
10796 if (flags == DTOR_FLAG)
10798 /* ISO C++ 12.4/2. A destructor may not be
10799 declared const or volatile. A destructor may
10802 error ("destructor cannot be static member function");
10805 error ("destructors may not be `%s'",
10806 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10809 if (decl_context == FIELD)
10811 if (! member_function_or_else (ctype,
10812 current_class_type,
10814 return void_type_node;
10817 else /* It's a constructor. */
10819 if (explicitp == 1)
10821 /* ISO C++ 12.1. A constructor may not be
10822 declared const or volatile. A constructor may
10823 not be virtual. A constructor may not be
10826 error ("constructor cannot be static member function");
10829 pedwarn ("constructors cannot be declared virtual");
10834 error ("constructors may not be `%s'",
10835 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10839 RID_BIT_TYPE tmp_bits;
10840 memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10841 RIDBIT_RESET (RID_INLINE, tmp_bits);
10842 RIDBIT_RESET (RID_STATIC, tmp_bits);
10843 if (RIDBIT_ANY_SET (tmp_bits))
10844 error ("return value type specifier for constructor ignored");
10846 if (decl_context == FIELD)
10848 if (! member_function_or_else (ctype,
10849 current_class_type,
10851 return void_type_node;
10852 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10853 if (sfk != sfk_constructor)
10857 if (decl_context == FIELD)
10863 error ("can't initialize friend function `%s'", name);
10866 /* Cannot be both friend and virtual. */
10867 error ("virtual functions cannot be friends");
10868 RIDBIT_RESET (RID_FRIEND, specbits);
10871 if (decl_context == NORMAL)
10872 error ("friend declaration not in class definition");
10873 if (current_function_decl && funcdef_flag)
10874 error ("can't define friend function `%s' in a local class definition",
10878 /* Construct the function type and go to the next
10879 inner layer of declarator. */
10881 declarator = TREE_OPERAND (declarator, 0);
10883 /* FIXME: This is where default args should be fully
10886 arg_types = grokparms (inner_parms);
10888 if (declarator && flags == DTOR_FLAG)
10890 /* A destructor declared in the body of a class will
10891 be represented as a BIT_NOT_EXPR. But, we just
10892 want the underlying IDENTIFIER. */
10893 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10894 declarator = TREE_OPERAND (declarator, 0);
10896 if (arg_types != void_list_node)
10898 error ("destructors may not have parameters");
10899 arg_types = void_list_node;
10900 last_function_parms = NULL_TREE;
10904 /* ANSI says that `const int foo ();'
10905 does not make the function foo const. */
10906 type = build_function_type (type, arg_types);
10912 /* Filter out pointers-to-references and references-to-references.
10913 We can get these if a TYPE_DECL is used. */
10915 if (TREE_CODE (type) == REFERENCE_TYPE)
10917 error (TREE_CODE (declarator) == ADDR_EXPR
10918 ? "cannot declare reference to `%#T'"
10919 : "cannot declare pointer to `%#T'", type);
10920 type = TREE_TYPE (type);
10922 else if (VOID_TYPE_P (type)
10923 && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10924 error (ctype ? "cannot declare pointer to `%#T' member"
10925 : "cannot declare reference to `%#T'", type);
10927 /* Merge any constancy or volatility into the target type
10928 for the pointer. */
10930 /* We now know that the TYPE_QUALS don't apply to the decl,
10931 but to the target of the pointer. */
10932 type_quals = TYPE_UNQUALIFIED;
10934 if (TREE_CODE (declarator) == ADDR_EXPR)
10936 if (!VOID_TYPE_P (type))
10937 type = build_reference_type (type);
10939 else if (TREE_CODE (type) == METHOD_TYPE)
10940 type = build_ptrmemfunc_type (build_pointer_type (type));
10942 type = build_ptrmem_type (ctype, type);
10944 type = build_pointer_type (type);
10946 /* Process a list of type modifier keywords (such as
10947 const or volatile) that were given inside the `*' or `&'. */
10949 if (TREE_TYPE (declarator))
10951 register tree typemodlist;
10957 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10958 typemodlist = TREE_CHAIN (typemodlist))
10960 tree qualifier = TREE_VALUE (typemodlist);
10962 if (qualifier == ridpointers[(int) RID_CONST])
10965 type_quals |= TYPE_QUAL_CONST;
10967 else if (qualifier == ridpointers[(int) RID_VOLATILE])
10970 type_quals |= TYPE_QUAL_VOLATILE;
10972 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10975 type_quals |= TYPE_QUAL_RESTRICT;
10980 error ("invalid type modifier within pointer declarator");
10984 pedwarn ("duplicate `const'");
10986 pedwarn ("duplicate `volatile'");
10988 pedwarn ("duplicate `restrict'");
10989 type = cp_build_qualified_type (type, type_quals);
10990 type_quals = cp_type_quals (type);
10992 declarator = TREE_OPERAND (declarator, 0);
10998 /* We have converted type names to NULL_TREE if the
10999 name was bogus, or to a _TYPE node, if not.
11001 The variable CTYPE holds the type we will ultimately
11002 resolve to. The code here just needs to build
11003 up appropriate member types. */
11004 tree sname = TREE_OPERAND (declarator, 1);
11007 /* Destructors can have their visibilities changed as well. */
11008 if (TREE_CODE (sname) == BIT_NOT_EXPR)
11009 sname = TREE_OPERAND (sname, 0);
11011 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
11013 /* We had a reference to a global decl, or
11014 perhaps we were given a non-aggregate typedef,
11015 in which case we cleared this out, and should just
11016 keep going as though it wasn't there. */
11017 declarator = sname;
11020 ctype = TREE_OPERAND (declarator, 0);
11023 while (t != NULL_TREE && CLASS_TYPE_P (t))
11025 /* You're supposed to have one `template <...>'
11026 for every template class, but you don't need one
11027 for a full specialization. For example:
11029 template <class T> struct S{};
11030 template <> struct S<int> { void f(); };
11031 void S<int>::f () {}
11033 is correct; there shouldn't be a `template <>' for
11034 the definition of `S<int>::f'. */
11035 if (CLASSTYPE_TEMPLATE_INFO (t)
11036 && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
11037 || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
11038 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
11039 template_count += 1;
11041 t = TYPE_MAIN_DECL (t);
11042 t = DECL_CONTEXT (t);
11045 if (sname == NULL_TREE)
11048 if (TREE_CODE (sname) == IDENTIFIER_NODE)
11050 /* This is the `standard' use of the scoping operator:
11051 basetype :: member . */
11053 if (ctype == current_class_type)
11059 Is this ill-formed? */
11062 pedwarn ("extra qualification `%T::' on member `%s' ignored",
11065 else if (TREE_CODE (type) == FUNCTION_TYPE)
11067 if (current_class_type == NULL_TREE || friendp)
11068 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11069 TYPE_ARG_TYPES (type));
11072 error ("cannot declare member function `%T::%s' within `%T'",
11073 ctype, name, current_class_type);
11074 return error_mark_node;
11077 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
11078 || COMPLETE_TYPE_P (complete_type (ctype)))
11080 /* Have to move this code elsewhere in this function.
11081 this code is used for i.e., typedef int A::M; M *pm;
11083 It is? How? jason 10/2/94 */
11085 if (current_class_type)
11087 error ("cannot declare member `%T::%s' within `%T'",
11088 ctype, name, current_class_type);
11089 return void_type_node;
11094 cxx_incomplete_type_error (NULL_TREE, ctype);
11095 return error_mark_node;
11098 declarator = sname;
11100 else if (TREE_CODE (sname) == SCOPE_REF)
11105 declarator = TREE_OPERAND (declarator, 1);
11106 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
11107 /* In this case, we will deal with it later. */
11109 else if (TREE_CODE (type) == FUNCTION_TYPE)
11110 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11111 TYPE_ARG_TYPES (type));
11117 declarator = TREE_OPERAND (declarator, 0);
11121 declarator = BASELINK_FUNCTIONS (declarator);
11126 case ENUMERAL_TYPE:
11127 declarator = NULL_TREE;
11131 declarator = NULL_TREE;
11139 if (returned_attrs)
11142 *attrlist = chainon (returned_attrs, *attrlist);
11144 attrlist = &returned_attrs;
11147 /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq that refer
11148 to ctype. They couldn't be resolved earlier because we hadn't
11149 pushed into the class yet.
11151 For example, consider:
11153 template <typename T>
11159 template <typename T>
11160 typename S<T>::X f() {}
11162 When parsing the decl-specifier-seq for the definition of `f',
11163 we construct a TYPENAME_TYPE for `S<T>::X'. By substituting
11164 here, we resolve it to the correct type. */
11165 if (scope && CLASS_TYPE_P (scope)
11166 && current_template_parms
11167 && uses_template_parms (scope))
11169 tree args = current_template_args ();
11170 push_scope (scope);
11171 type = tsubst (type, args, tf_error | tf_warning,
11176 /* Now TYPE has the actual type. */
11178 /* Did array size calculations overflow? */
11180 if (TREE_CODE (type) == ARRAY_TYPE
11181 && COMPLETE_TYPE_P (type)
11182 && TREE_OVERFLOW (TYPE_SIZE (type)))
11184 error ("size of array `%s' is too large", name);
11185 /* If we proceed with the array type as it is, we'll eventually
11186 crash in tree_low_cst(). */
11187 type = error_mark_node;
11190 if (decl_context == FIELD
11191 && !processing_template_decl
11192 && variably_modified_type_p (type))
11194 error ("data member may not have variably modified type `%T'", type);
11195 type = error_mark_node;
11198 if (explicitp == 1 || (explicitp && friendp))
11200 /* [dcl.fct.spec] The explicit specifier shall only be used in
11201 declarations of constructors within a class definition. */
11202 error ("only declarations of constructors can be `explicit'");
11206 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11208 if (current_class_name == NULL_TREE || decl_context == PARM || friendp)
11210 error ("non-member `%s' cannot be declared `mutable'", name);
11211 RIDBIT_RESET (RID_MUTABLE, specbits);
11213 else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11215 error ("non-object member `%s' cannot be declared `mutable'", name);
11216 RIDBIT_RESET (RID_MUTABLE, specbits);
11218 else if (TREE_CODE (type) == FUNCTION_TYPE
11219 || TREE_CODE (type) == METHOD_TYPE)
11221 error ("function `%s' cannot be declared `mutable'", name);
11222 RIDBIT_RESET (RID_MUTABLE, specbits);
11226 error ("static `%s' cannot be declared `mutable'", name);
11227 RIDBIT_RESET (RID_MUTABLE, specbits);
11229 else if (type_quals & TYPE_QUAL_CONST)
11231 error ("const `%s' cannot be declared `mutable'", name);
11232 RIDBIT_RESET (RID_MUTABLE, specbits);
11236 if (declarator == NULL_TREE
11237 || TREE_CODE (declarator) == IDENTIFIER_NODE
11238 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11239 && (TREE_CODE (type) == FUNCTION_TYPE
11240 || TREE_CODE (type) == METHOD_TYPE)))
11242 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11244 error ("template-id `%D' used as a declarator", declarator);
11245 declarator = dname;
11248 /* Unexpected declarator format. */
11251 /* If this is declaring a typedef name, return a TYPE_DECL. */
11253 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11257 /* Note that the grammar rejects storage classes
11258 in typenames, fields or parameters. */
11259 if (current_lang_name == lang_name_java)
11260 TYPE_FOR_JAVA (type) = 1;
11262 if (decl_context == FIELD)
11264 if (constructor_name_p (declarator, current_class_type))
11265 pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11267 decl = build_lang_decl (TYPE_DECL, declarator, type);
11271 decl = build_decl (TYPE_DECL, declarator, type);
11272 if (in_namespace || ctype)
11273 cp_error_at ("typedef name may not be a nested-name-specifier",
11275 if (!current_function_decl)
11276 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11279 /* If the user declares "typedef struct {...} foo" then the
11280 struct will have an anonymous name. Fill that name in now.
11281 Nothing can refer to it, so nothing needs know about the name
11283 if (type != error_mark_node
11285 && TYPE_NAME (type)
11286 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11287 && TYPE_ANONYMOUS_P (type)
11288 /* Don't do this if there are attributes. */
11289 && (!attrlist || !*attrlist)
11290 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11292 tree oldname = TYPE_NAME (type);
11295 /* Replace the anonymous name with the real name everywhere. */
11296 lookup_tag_reverse (type, declarator);
11297 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11298 if (TYPE_NAME (t) == oldname)
11299 TYPE_NAME (t) = decl;
11301 if (TYPE_LANG_SPECIFIC (type))
11302 TYPE_WAS_ANONYMOUS (type) = 1;
11304 /* If this is a typedef within a template class, the nested
11305 type is a (non-primary) template. The name for the
11306 template needs updating as well. */
11307 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11308 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11309 = TYPE_IDENTIFIER (type);
11311 /* FIXME remangle member functions; member functions of a
11312 type with external linkage have external linkage. */
11317 if (ctype == NULL_TREE)
11319 if (TREE_CODE (type) != METHOD_TYPE)
11320 cp_error_at ("invalid type qualifier for non-member function type", decl);
11322 ctype = TYPE_METHOD_BASETYPE (type);
11324 if (ctype != NULL_TREE)
11325 grok_method_quals (ctype, decl, quals);
11328 if (RIDBIT_SETP (RID_SIGNED, specbits)
11329 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11330 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11332 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11333 inlinep, friendp, raises != NULL_TREE);
11338 /* Detect the case of an array type of unspecified size
11339 which came, as such, direct from a typedef name.
11340 We must copy the type, so that the array's domain can be
11341 individually set by the object's initializer. */
11343 if (type && typedef_type
11344 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11345 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11346 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11348 /* Detect where we're using a typedef of function type to declare a
11349 function. last_function_parms will not be set, so we must create
11352 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11354 tree decls = NULL_TREE;
11357 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11359 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11361 TREE_CHAIN (decl) = decls;
11365 last_function_parms = nreverse (decls);
11368 /* If this is a type name (such as, in a cast or sizeof),
11369 compute the type and return it now. */
11371 if (decl_context == TYPENAME)
11373 /* Note that the grammar rejects storage classes
11374 in typenames, fields or parameters. */
11375 if (type_quals != TYPE_UNQUALIFIED)
11376 type_quals = TYPE_UNQUALIFIED;
11378 /* Special case: "friend class foo" looks like a TYPENAME context. */
11381 if (type_quals != TYPE_UNQUALIFIED)
11383 error ("type qualifiers specified for friend class declaration");
11384 type_quals = TYPE_UNQUALIFIED;
11388 error ("`inline' specified for friend class declaration");
11394 /* Don't allow friend declaration without a class-key. */
11395 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11396 pedwarn ("template parameters cannot be friends");
11397 else if (TREE_CODE (type) == TYPENAME_TYPE)
11398 pedwarn ("friend declaration requires class-key, "
11399 "i.e. `friend class %T::%D'",
11400 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11402 pedwarn ("friend declaration requires class-key, "
11403 "i.e. `friend %#T'",
11407 /* Only try to do this stuff if we didn't already give up. */
11408 if (type != integer_type_node)
11410 decl_type_access_control (TYPE_NAME (type));
11412 /* A friendly class? */
11413 if (current_class_type)
11414 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11416 error ("trying to make class `%T' a friend of global scope",
11419 type = void_type_node;
11424 if (ctype == NULL_TREE)
11426 if (TREE_CODE (type) != METHOD_TYPE)
11427 error ("invalid qualifiers on non-member function type");
11429 ctype = TYPE_METHOD_BASETYPE (type);
11433 tree dummy = build_decl (TYPE_DECL, declarator, type);
11434 grok_method_quals (ctype, dummy, quals);
11435 type = TREE_TYPE (dummy);
11441 else if (declarator == NULL_TREE && decl_context != PARM
11442 && decl_context != CATCHPARM
11443 && TREE_CODE (type) != UNION_TYPE
11446 error ("abstract declarator `%T' used as declaration", type);
11447 declarator = make_anon_name ();
11450 /* `void' at top level (not within pointer)
11451 is allowed only in typedefs or type names.
11452 We don't complain about parms either, but that is because
11453 a better error message can be made later. */
11455 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11458 error ("unnamed variable or field declared void");
11459 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11461 if (IDENTIFIER_OPNAME_P (declarator))
11464 error ("variable or field `%s' declared void", name);
11467 error ("variable or field declared void");
11468 type = integer_type_node;
11471 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11472 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11474 if (decl_context == PARM || decl_context == CATCHPARM)
11476 if (ctype || in_namespace)
11477 error ("cannot use `::' in parameter declaration");
11479 /* A parameter declared as an array of T is really a pointer to T.
11480 One declared as a function is really a pointer to a function.
11481 One declared as a member is really a pointer to member. */
11483 if (TREE_CODE (type) == ARRAY_TYPE)
11485 /* Transfer const-ness of array into that of type pointed to. */
11486 type = build_pointer_type (TREE_TYPE (type));
11487 type_quals = TYPE_UNQUALIFIED;
11489 else if (TREE_CODE (type) == FUNCTION_TYPE)
11490 type = build_pointer_type (type);
11491 else if (TREE_CODE (type) == OFFSET_TYPE)
11492 type = build_pointer_type (type);
11496 register tree decl;
11498 if (decl_context == PARM)
11500 decl = cp_build_parm_decl (declarator, type);
11502 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11503 inlinep, friendp, raises != NULL_TREE);
11505 else if (decl_context == FIELD)
11507 if (type == error_mark_node)
11509 /* Happens when declaring arrays of sizes which
11510 are error_mark_node, for example. */
11513 else if (in_namespace && !friendp)
11515 /* Something like struct S { int N::j; }; */
11516 error ("invalid use of `::'");
11519 else if (TREE_CODE (type) == FUNCTION_TYPE)
11522 tree function_context;
11524 /* We catch the others as conflicts with the builtin
11526 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11528 error ("function `%D' cannot be declared friend",
11535 if (ctype == NULL_TREE)
11536 ctype = current_class_type;
11538 if (ctype == NULL_TREE)
11540 error ("can't make `%D' into a method -- not in a class",
11542 return void_type_node;
11545 /* ``A union may [ ... ] not [ have ] virtual functions.''
11547 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11549 error ("function `%D' declared virtual inside a union",
11551 return void_type_node;
11554 if (declarator == ansi_opname (NEW_EXPR)
11555 || declarator == ansi_opname (VEC_NEW_EXPR)
11556 || declarator == ansi_opname (DELETE_EXPR)
11557 || declarator == ansi_opname (VEC_DELETE_EXPR))
11561 error ("`%D' cannot be declared virtual, since it is always static",
11566 else if (staticp < 2)
11567 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11568 TYPE_ARG_TYPES (type));
11571 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11572 function_context = (ctype != NULL_TREE) ?
11573 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11574 publicp = (! friendp || ! staticp)
11575 && function_context == NULL_TREE;
11576 decl = grokfndecl (ctype, type,
11577 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11578 ? declarator : dname,
11580 virtualp, flags, quals, raises,
11581 friendp ? -1 : 0, friendp, publicp, inlinep,
11582 funcdef_flag, template_count, in_namespace);
11583 if (decl == NULL_TREE)
11586 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11587 /* The decl and setting of decl_attr is also turned off. */
11588 decl = build_decl_attribute_variant (decl, decl_attr);
11591 /* [class.conv.ctor]
11593 A constructor declared without the function-specifier
11594 explicit that can be called with a single parameter
11595 specifies a conversion from the type of its first
11596 parameter to the type of its class. Such a constructor
11597 is called a converting constructor. */
11598 if (explicitp == 2)
11599 DECL_NONCONVERTING_P (decl) = 1;
11600 else if (DECL_CONSTRUCTOR_P (decl))
11602 /* The constructor can be called with exactly one
11603 parameter if there is at least one parameter, and
11604 any subsequent parameters have default arguments.
11605 Ignore any compiler-added parms. */
11606 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11608 if (arg_types == void_list_node
11610 && TREE_CHAIN (arg_types)
11611 && TREE_CHAIN (arg_types) != void_list_node
11612 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11613 DECL_NONCONVERTING_P (decl) = 1;
11616 else if (TREE_CODE (type) == METHOD_TYPE)
11618 /* We only get here for friend declarations of
11619 members of other classes. */
11620 /* All method decls are public, so tell grokfndecl to set
11621 TREE_PUBLIC, also. */
11622 decl = grokfndecl (ctype, type, declarator, declarator,
11623 virtualp, flags, quals, raises,
11624 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11625 template_count, in_namespace);
11626 if (decl == NULL_TREE)
11629 else if (!staticp && ! processing_template_decl
11630 && !COMPLETE_TYPE_P (complete_type (type))
11631 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11634 error ("field `%D' has incomplete type", declarator);
11636 error ("name `%T' has incomplete type", type);
11638 /* If we're instantiating a template, tell them which
11639 instantiation made the field's type be incomplete. */
11640 if (current_class_type
11641 && TYPE_NAME (current_class_type)
11642 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11643 && declspecs && TREE_VALUE (declspecs)
11644 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11645 error (" in instantiation of template `%T'",
11646 current_class_type);
11648 type = error_mark_node;
11655 error ("`%s' is neither function nor member function; cannot be declared friend",
11656 IDENTIFIER_POINTER (declarator));
11664 /* Friends are treated specially. */
11665 if (ctype == current_class_type)
11666 warning ("member functions are implicitly friends of their class");
11669 tree t = NULL_TREE;
11670 if (decl && DECL_NAME (decl))
11672 if (template_class_depth (current_class_type) == 0)
11675 = check_explicit_specialization
11677 template_count, 2 * (funcdef_flag != 0) + 4);
11678 if (decl == error_mark_node)
11679 return error_mark_node;
11682 t = do_friend (ctype, declarator, decl,
11683 last_function_parms, *attrlist,
11684 flags, quals, funcdef_flag);
11686 if (t && funcdef_flag)
11689 return void_type_node;
11693 /* Structure field. It may not be a function, except for C++ */
11695 if (decl == NULL_TREE)
11701 /* An attempt is being made to initialize a non-static
11702 member. But, from [class.mem]:
11704 4 A member-declarator can contain a
11705 constant-initializer only if it declares a static
11706 member (_class.static_) of integral or enumeration
11707 type, see _class.static.data_.
11709 This used to be relatively common practice, but
11710 the rest of the compiler does not correctly
11711 handle the initialization unless the member is
11712 static so we make it static below. */
11713 pedwarn ("ISO C++ forbids initialization of member `%D'",
11715 pedwarn ("making `%D' static", declarator);
11719 if (uses_template_parms (type))
11720 /* We'll check at instantiation time. */
11722 else if (check_static_variable_definition (declarator,
11724 /* If we just return the declaration, crashes
11725 will sometimes occur. We therefore return
11726 void_type_node, as if this was a friend
11727 declaration, to cause callers to completely
11728 ignore this declaration. */
11729 return void_type_node;
11732 /* 9.2p13 [class.mem] */
11733 if (constructor_name_p (declarator, current_class_type)
11734 /* The standard does not allow non-static data members
11735 here either, but we agreed at the 10/99 meeting
11736 to change that in TC 1 so that they are allowed in
11737 classes with no user-defined constructors. */
11739 pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11744 /* C++ allows static class members. All other work
11745 for this is done by grokfield. */
11746 decl = build_lang_decl (VAR_DECL, declarator, type);
11747 TREE_STATIC (decl) = 1;
11748 /* In class context, 'static' means public access. */
11749 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11753 decl = build_decl (FIELD_DECL, declarator, type);
11754 DECL_NONADDRESSABLE_P (decl) = bitfield;
11755 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11757 DECL_MUTABLE_P (decl) = 1;
11758 RIDBIT_RESET (RID_MUTABLE, specbits);
11762 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11763 inlinep, friendp, raises != NULL_TREE);
11766 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11768 tree original_name;
11774 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11775 original_name = dname;
11777 original_name = declarator;
11779 if (RIDBIT_SETP (RID_AUTO, specbits))
11780 error ("storage class `auto' invalid for function `%s'", name);
11781 else if (RIDBIT_SETP (RID_REGISTER, specbits))
11782 error ("storage class `register' invalid for function `%s'", name);
11783 else if (RIDBIT_SETP (RID_THREAD, specbits))
11784 error ("storage class `__thread' invalid for function `%s'", name);
11786 /* Function declaration not at top level.
11787 Storage classes other than `extern' are not allowed
11788 and `extern' makes no difference. */
11789 if (! toplevel_bindings_p ()
11790 && (RIDBIT_SETP (RID_STATIC, specbits)
11791 || RIDBIT_SETP (RID_INLINE, specbits))
11794 if (RIDBIT_SETP (RID_STATIC, specbits))
11795 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11797 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11800 if (ctype == NULL_TREE)
11804 error ("virtual non-class function `%s'", name);
11808 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11809 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11810 TYPE_ARG_TYPES (type));
11812 /* Record presence of `static'. */
11813 publicp = (ctype != NULL_TREE
11814 || RIDBIT_SETP (RID_EXTERN, specbits)
11815 || !RIDBIT_SETP (RID_STATIC, specbits));
11817 decl = grokfndecl (ctype, type, original_name, declarator,
11818 virtualp, flags, quals, raises,
11820 publicp, inlinep, funcdef_flag,
11821 template_count, in_namespace);
11822 if (decl == NULL_TREE)
11827 int invalid_static = 0;
11829 /* Don't allow a static member function in a class, and forbid
11830 declaring main to be static. */
11831 if (TREE_CODE (type) == METHOD_TYPE)
11833 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11834 invalid_static = 1;
11836 else if (current_function_decl)
11838 /* FIXME need arm citation */
11839 error ("cannot declare static function inside another function");
11840 invalid_static = 1;
11843 if (invalid_static)
11846 RIDBIT_RESET (RID_STATIC, specbits);
11852 /* It's a variable. */
11854 /* An uninitialized decl with `extern' is a reference. */
11855 decl = grokvardecl (type, declarator, &specbits,
11857 (type_quals & TYPE_QUAL_CONST) != 0,
11858 ctype ? ctype : in_namespace);
11859 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11860 inlinep, friendp, raises != NULL_TREE);
11864 DECL_CONTEXT (decl) = ctype;
11867 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11869 RIDBIT_RESET (RID_STATIC, specbits);
11871 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11873 error ("static member `%D' declared `register'", decl);
11874 RIDBIT_RESET (RID_REGISTER, specbits);
11876 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11878 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11880 RIDBIT_RESET (RID_EXTERN, specbits);
11885 my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11887 /* Record `register' declaration for warnings on &
11888 and in case doing stupid register allocation. */
11890 if (RIDBIT_SETP (RID_REGISTER, specbits))
11891 DECL_REGISTER (decl) = 1;
11893 if (RIDBIT_SETP (RID_EXTERN, specbits))
11894 DECL_THIS_EXTERN (decl) = 1;
11896 if (RIDBIT_SETP (RID_STATIC, specbits))
11897 DECL_THIS_STATIC (decl) = 1;
11899 /* Record constancy and volatility. There's no need to do this
11900 when processing a template; we'll do this for the instantiated
11901 declaration based on the type of DECL. */
11902 if (!processing_template_decl)
11903 c_apply_type_quals_to_decl (type_quals, decl);
11909 /* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
11910 An empty exprlist is a parmlist. An exprlist which
11911 contains only identifiers at the global level
11912 is a parmlist. Otherwise, it is an exprlist. */
11915 parmlist_is_exprlist (tree exprs)
11917 if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
11920 if (toplevel_bindings_p ())
11922 /* At the global level, if these are all identifiers,
11923 then it is a parmlist. */
11926 if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
11928 exprs = TREE_CHAIN (exprs);
11935 /* Subroutine of start_function. Ensure that each of the parameter
11936 types (as listed in PARMS) is complete, as is required for a
11937 function definition. */
11940 require_complete_types_for_parms (tree parms)
11942 for (; parms; parms = TREE_CHAIN (parms))
11944 if (VOID_TYPE_P (TREE_TYPE (parms)))
11945 /* grokparms will have already issued an error */
11946 TREE_TYPE (parms) = error_mark_node;
11947 else if (complete_type_or_else (TREE_TYPE (parms), parms))
11948 layout_decl (parms, 0);
11950 TREE_TYPE (parms) = error_mark_node;
11954 /* Returns nonzero if T is a local variable. */
11957 local_variable_p (tree t)
11959 if ((TREE_CODE (t) == VAR_DECL
11960 /* A VAR_DECL with a context that is a _TYPE is a static data
11962 && !TYPE_P (CP_DECL_CONTEXT (t))
11963 /* Any other non-local variable must be at namespace scope. */
11964 && !DECL_NAMESPACE_SCOPE_P (t))
11965 || (TREE_CODE (t) == PARM_DECL))
11971 /* Returns nonzero if T is an automatic local variable or a label.
11972 (These are the declarations that need to be remapped when the code
11973 containing them is duplicated.) */
11976 nonstatic_local_decl_p (tree t)
11978 return ((local_variable_p (t) && !TREE_STATIC (t))
11979 || TREE_CODE (t) == LABEL_DECL
11980 || TREE_CODE (t) == RESULT_DECL);
11983 /* Like local_variable_p, but suitable for use as a tree-walking
11987 local_variable_p_walkfn (tree* tp,
11988 int* walk_subtrees ATTRIBUTE_UNUSED ,
11989 void* data ATTRIBUTE_UNUSED )
11991 return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11992 ? *tp : NULL_TREE);
11995 /* Check that ARG, which is a default-argument expression for a
11996 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11997 something goes wrong. DECL may also be a _TYPE node, rather than a
11998 DECL, if there is no DECL available. */
12001 check_default_argument (tree decl, tree arg)
12006 if (TREE_CODE (arg) == DEFAULT_ARG)
12007 /* We get a DEFAULT_ARG when looking at an in-class declaration
12008 with a default argument. Ignore the argument for now; we'll
12009 deal with it after the class is complete. */
12012 if (processing_template_decl || uses_template_parms (arg))
12013 /* We don't do anything checking until instantiation-time. Note
12014 that there may be uninstantiated arguments even for an
12015 instantiated function, since default arguments are not
12016 instantiated until they are needed. */
12025 decl_type = TREE_TYPE (decl);
12027 if (arg == error_mark_node
12028 || decl == error_mark_node
12029 || TREE_TYPE (arg) == error_mark_node
12030 || decl_type == error_mark_node)
12031 /* Something already went wrong. There's no need to check
12033 return error_mark_node;
12035 /* [dcl.fct.default]
12037 A default argument expression is implicitly converted to the
12039 if (!TREE_TYPE (arg)
12040 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
12043 error ("default argument for `%#D' has type `%T'",
12044 decl, TREE_TYPE (arg));
12046 error ("default argument for parameter of type `%T' has type `%T'",
12047 decl_type, TREE_TYPE (arg));
12049 return error_mark_node;
12052 /* [dcl.fct.default]
12054 Local variables shall not be used in default argument
12057 The keyword `this' shall not be used in a default argument of a
12058 member function. */
12059 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
12063 error ("default argument `%E' uses local variable `%D'",
12065 return error_mark_node;
12072 /* Decode the list of parameter types for a function type.
12073 Given the list of things declared inside the parens,
12074 return a list of types.
12076 We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
12077 flag. If unset, we append void_list_node. A parmlist declared
12078 as `(void)' is accepted as the empty parmlist.
12080 Also set last_function_parms to the chain of PARM_DECLs. */
12083 grokparms (tree first_parm)
12085 tree result = NULL_TREE;
12086 tree decls = NULL_TREE;
12087 int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
12091 my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
12093 for (parm = first_parm; parm != NULL_TREE; parm = chain)
12095 tree type = NULL_TREE;
12096 tree decl = TREE_VALUE (parm);
12097 tree init = TREE_PURPOSE (parm);
12100 chain = TREE_CHAIN (parm);
12101 /* @@ weak defense against parse errors. */
12102 if (TREE_CODE (decl) != VOID_TYPE
12103 && TREE_CODE (decl) != TREE_LIST)
12105 /* Give various messages as the need arises. */
12106 if (TREE_CODE (decl) == STRING_CST)
12107 error ("invalid string constant `%E'", decl);
12108 else if (TREE_CODE (decl) == INTEGER_CST)
12109 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
12113 if (parm == void_list_node)
12116 split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
12117 decl = grokdeclarator (TREE_VALUE (decl), specs,
12118 PARM, init != NULL_TREE, &attrs);
12119 if (! decl || TREE_TYPE (decl) == error_mark_node)
12123 cplus_decl_attributes (&decl, attrs, 0);
12125 type = TREE_TYPE (decl);
12126 if (VOID_TYPE_P (type))
12128 if (same_type_p (type, void_type_node)
12129 && !DECL_NAME (decl) && !result && !chain && !ellipsis)
12130 /* this is a parmlist of `(void)', which is ok. */
12132 cxx_incomplete_type_error (decl, type);
12133 /* It's not a good idea to actually create parameters of
12134 type `void'; other parts of the compiler assume that a
12135 void type terminates the parameter list. */
12136 type = error_mark_node;
12137 TREE_TYPE (decl) = error_mark_node;
12140 if (type != error_mark_node)
12142 /* Top-level qualifiers on the parameters are
12143 ignored for function types. */
12144 type = TYPE_MAIN_VARIANT (type);
12145 if (TREE_CODE (type) == METHOD_TYPE)
12147 error ("parameter `%D' invalidly declared method type", decl);
12148 type = build_pointer_type (type);
12149 TREE_TYPE (decl) = type;
12151 else if (TREE_CODE (type) == OFFSET_TYPE)
12153 error ("parameter `%D' invalidly declared offset type", decl);
12154 type = build_pointer_type (type);
12155 TREE_TYPE (decl) = type;
12157 else if (abstract_virtuals_error (decl, type))
12158 any_error = 1; /* Seems like a good idea. */
12159 else if (POINTER_TYPE_P (type))
12161 /* [dcl.fct]/6, parameter types cannot contain pointers
12162 (references) to arrays of unknown bound. */
12163 tree t = TREE_TYPE (type);
12164 int ptr = TYPE_PTR_P (type);
12168 if (TYPE_PTR_P (t))
12170 else if (TREE_CODE (t) != ARRAY_TYPE)
12172 else if (!TYPE_DOMAIN (t))
12176 if (TREE_CODE (t) == ARRAY_TYPE)
12177 error ("parameter `%D' includes %s to array of unknown bound `%T'",
12178 decl, ptr ? "pointer" : "reference", t);
12181 if (!any_error && init)
12182 init = check_default_argument (decl, init);
12187 TREE_CHAIN (decl) = decls;
12189 result = tree_cons (init, type, result);
12191 decls = nreverse (decls);
12192 result = nreverse (result);
12194 result = chainon (result, void_list_node);
12195 last_function_parms = decls;
12201 /* D is a constructor or overloaded `operator='.
12203 Let T be the class in which D is declared. Then, this function
12206 -1 if D's is an ill-formed constructor or copy assignment operator
12207 whose first parameter is of type `T'.
12208 0 if D is not a copy constructor or copy assignment
12210 1 if D is a copy constructor or copy assignment operator whose
12211 first parameter is a reference to const qualified T.
12212 2 if D is a copy constructor or copy assignment operator whose
12213 first parameter is a reference to non-const qualified T.
12215 This function can be used as a predicate. Positive values indicate
12216 a copy constructor and nonzero values indicate a copy assignment
12226 my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12228 if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12229 /* Instantiations of template member functions are never copy
12230 functions. Note that member functions of templated classes are
12231 represented as template functions internally, and we must
12232 accept those as copy functions. */
12235 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12239 arg_type = TREE_VALUE (args);
12241 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12243 /* Pass by value copy assignment operator. */
12246 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12247 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12249 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12255 args = TREE_CHAIN (args);
12257 if (args && args != void_list_node && !TREE_PURPOSE (args))
12258 /* There are more non-optional args. */
12264 /* Remember any special properties of member function DECL. */
12266 void grok_special_member_properties (tree decl)
12268 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12269 ; /* Not special. */
12270 else if (DECL_CONSTRUCTOR_P (decl))
12272 int ctor = copy_fn_p (decl);
12278 A non-template constructor for class X is a copy
12279 constructor if its first parameter is of type X&, const
12280 X&, volatile X& or const volatile X&, and either there
12281 are no other parameters or else all other parameters have
12282 default arguments. */
12283 TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12285 TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12287 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12288 TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12290 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12294 A non-template assignment operator for class X is a copy
12295 assignment operator if its parameter is of type X, X&, const
12296 X&, volatile X& or const volatile X&. */
12298 int assop = copy_fn_p (decl);
12302 TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12304 TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12305 if (DECL_PURE_VIRTUAL_P (decl))
12306 TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12311 /* Check a constructor DECL has the correct form. Complains
12312 if the class has a constructor of the form X(X). */
12315 grok_ctor_properties (tree ctype, tree decl)
12317 int ctor_parm = copy_fn_p (decl);
12323 A declaration of a constructor for a class X is ill-formed if
12324 its first parameter is of type (optionally cv-qualified) X
12325 and either there are no other parameters or else all other
12326 parameters have default arguments.
12328 We *don't* complain about member template instantiations that
12329 have this form, though; they can occur as we try to decide
12330 what constructor to use during overload resolution. Since
12331 overload resolution will never prefer such a constructor to
12332 the non-template copy constructor (which is either explicitly
12333 or implicitly defined), there's no need to worry about their
12334 existence. Theoretically, they should never even be
12335 instantiated, but that's hard to forestall. */
12336 error ("invalid constructor; you probably meant `%T (const %T&)'",
12338 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12345 /* An operator with this code is unary, but can also be binary. */
12348 ambi_op_p (enum tree_code code)
12350 return (code == INDIRECT_REF
12351 || code == ADDR_EXPR
12352 || code == CONVERT_EXPR
12353 || code == NEGATE_EXPR
12354 || code == PREINCREMENT_EXPR
12355 || code == PREDECREMENT_EXPR);
12358 /* An operator with this name can only be unary. */
12361 unary_op_p (enum tree_code code)
12363 return (code == TRUTH_NOT_EXPR
12364 || code == BIT_NOT_EXPR
12365 || code == COMPONENT_REF
12366 || code == TYPE_EXPR);
12369 /* Do a little sanity-checking on how they declared their operator. */
12372 grok_op_properties (tree decl, int friendp)
12374 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12376 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12377 tree name = DECL_NAME (decl);
12378 enum tree_code operator_code;
12381 /* Count the number of arguments. */
12382 for (argtype = argtypes, arity = 0;
12383 argtype && argtype != void_list_node;
12384 argtype = TREE_CHAIN (argtype))
12387 if (current_class_type == NULL_TREE)
12390 if (DECL_CONV_FN_P (decl))
12391 operator_code = TYPE_EXPR;
12395 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12396 if (ansi_opname (CODE) == name) \
12398 operator_code = (CODE); \
12401 else if (ansi_assopname (CODE) == name) \
12403 operator_code = (CODE); \
12404 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12408 #include "operators.def"
12409 #undef DEF_OPERATOR
12414 my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12415 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12419 switch (operator_code)
12422 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12426 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12429 case COMPONENT_REF:
12431 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12435 TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12439 TYPE_GETS_DELETE (current_class_type) |= 1;
12443 TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12446 case VEC_DELETE_EXPR:
12447 TYPE_GETS_DELETE (current_class_type) |= 2;
12455 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12457 /* When the compiler encounters the definition of A::operator new, it
12458 doesn't look at the class declaration to find out if it's static. */
12460 revert_static_member_fn (decl);
12462 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12464 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12467 revert_static_member_fn (decl);
12469 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12473 /* An operator function must either be a non-static member function
12474 or have at least one parameter of a class, a reference to a class,
12475 an enumeration, or a reference to an enumeration. 13.4.0.6 */
12476 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12478 if (operator_code == TYPE_EXPR
12479 || operator_code == CALL_EXPR
12480 || operator_code == COMPONENT_REF
12481 || operator_code == ARRAY_REF
12482 || operator_code == NOP_EXPR)
12483 error ("`%D' must be a nonstatic member function", decl);
12488 if (DECL_STATIC_FUNCTION_P (decl))
12489 error ("`%D' must be either a non-static member function or a non-member function", decl);
12492 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
12494 tree arg = TREE_VALUE (p);
12495 if (TREE_CODE (arg) == REFERENCE_TYPE)
12496 arg = TREE_TYPE (arg);
12498 /* This lets bad template code slip through. */
12499 if (IS_AGGR_TYPE (arg)
12500 || TREE_CODE (arg) == ENUMERAL_TYPE
12501 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12502 || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
12506 ("`%D' must have an argument of class or enumerated type",
12513 if (operator_code == CALL_EXPR)
12514 return; /* No restrictions on args. */
12516 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12518 tree t = TREE_TYPE (name);
12521 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12522 const char *what = 0;
12525 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12527 if (TREE_CODE (t) == VOID_TYPE)
12529 else if (t == current_class_type)
12530 what = "the same type";
12531 /* Don't force t to be complete here. */
12532 else if (IS_AGGR_TYPE (t)
12533 && COMPLETE_TYPE_P (t)
12534 && DERIVED_FROM_P (t, current_class_type))
12535 what = "a base class";
12538 warning ("conversion to %s%s will never use a type conversion operator",
12539 ref ? "a reference to " : "", what);
12542 if (operator_code == COND_EXPR)
12545 error ("ISO C++ prohibits overloading operator ?:");
12547 else if (ambi_op_p (operator_code))
12550 /* We pick the one-argument operator codes by default, so
12551 we don't have to change anything. */
12553 else if (arity == 2)
12555 /* If we thought this was a unary operator, we now know
12556 it to be a binary operator. */
12557 switch (operator_code)
12560 operator_code = MULT_EXPR;
12564 operator_code = BIT_AND_EXPR;
12568 operator_code = PLUS_EXPR;
12572 operator_code = MINUS_EXPR;
12575 case PREINCREMENT_EXPR:
12576 operator_code = POSTINCREMENT_EXPR;
12579 case PREDECREMENT_EXPR:
12580 operator_code = POSTDECREMENT_EXPR;
12587 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12589 if ((operator_code == POSTINCREMENT_EXPR
12590 || operator_code == POSTDECREMENT_EXPR)
12591 && ! processing_template_decl
12592 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12595 error ("postfix `%D' must take `int' as its argument",
12599 ("postfix `%D' must take `int' as its second argument",
12606 error ("`%D' must take either zero or one argument", decl);
12608 error ("`%D' must take either one or two arguments", decl);
12611 /* More Effective C++ rule 6. */
12613 && (operator_code == POSTINCREMENT_EXPR
12614 || operator_code == POSTDECREMENT_EXPR
12615 || operator_code == PREINCREMENT_EXPR
12616 || operator_code == PREDECREMENT_EXPR))
12618 tree arg = TREE_VALUE (argtypes);
12619 tree ret = TREE_TYPE (TREE_TYPE (decl));
12620 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12621 arg = TREE_TYPE (arg);
12622 arg = TYPE_MAIN_VARIANT (arg);
12623 if (operator_code == PREINCREMENT_EXPR
12624 || operator_code == PREDECREMENT_EXPR)
12626 if (TREE_CODE (ret) != REFERENCE_TYPE
12627 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12629 warning ("prefix `%D' should return `%T'", decl,
12630 build_reference_type (arg));
12634 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12635 warning ("postfix `%D' should return `%T'", decl, arg);
12639 else if (unary_op_p (operator_code))
12644 error ("`%D' must take `void'", decl);
12646 error ("`%D' must take exactly one argument", decl);
12649 else /* if (binary_op_p (operator_code)) */
12654 error ("`%D' must take exactly one argument", decl);
12656 error ("`%D' must take exactly two arguments", decl);
12659 /* More Effective C++ rule 7. */
12661 && (operator_code == TRUTH_ANDIF_EXPR
12662 || operator_code == TRUTH_ORIF_EXPR
12663 || operator_code == COMPOUND_EXPR))
12664 warning ("user-defined `%D' always evaluates both arguments",
12668 /* Effective C++ rule 23. */
12671 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12672 && (operator_code == PLUS_EXPR
12673 || operator_code == MINUS_EXPR
12674 || operator_code == TRUNC_DIV_EXPR
12675 || operator_code == MULT_EXPR
12676 || operator_code == TRUNC_MOD_EXPR)
12677 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12678 warning ("`%D' should return by value", decl);
12680 /* [over.oper]/8 */
12681 for (; argtypes && argtypes != void_list_node;
12682 argtypes = TREE_CHAIN (argtypes))
12683 if (TREE_PURPOSE (argtypes))
12685 TREE_PURPOSE (argtypes) = NULL_TREE;
12686 if (operator_code == POSTINCREMENT_EXPR
12687 || operator_code == POSTDECREMENT_EXPR)
12690 pedwarn ("`%D' cannot have default arguments", decl);
12693 error ("`%D' cannot have default arguments", decl);
12699 static const char *
12700 tag_name (enum tag_types code)
12717 /* Get the struct, enum or union (CODE says which) with tag NAME.
12718 Define the tag as a forward-reference if it is not defined.
12720 C++: If a class derivation is given, process it here, and report
12721 an error if multiple derivation declarations are not identical.
12723 If this is a definition, come in through xref_tag and only look in
12724 the current frame for the name (since C++ allows new names in any
12728 xref_tag (enum tag_types tag_code, tree name, tree attributes,
12731 enum tree_code code;
12732 register tree ref, t;
12733 struct cp_binding_level *b = current_binding_level;
12734 tree context = NULL_TREE;
12740 code = RECORD_TYPE;
12746 code = ENUMERAL_TYPE;
12752 /* If a cross reference is requested, look up the type
12753 already defined for this tag and return it. */
12757 name = TYPE_IDENTIFIER (t);
12760 t = IDENTIFIER_TYPE_VALUE (name);
12762 /* Warn about 'friend struct Inherited;' doing the wrong thing. */
12763 if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
12765 static int explained;
12768 warning ("`%s %T' declares a new type at namespace scope",
12769 tag_name (tag_code), name);
12771 warning (" names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
12772 tag_name (tag_code),
12773 constructor_name (current_class_type),
12774 TYPE_IDENTIFIER (t));
12776 /* We need to remove the class scope binding for the
12777 TYPENAME_TYPE as otherwise poplevel_class gets confused. */
12778 for (shadowed = b->class_shadowed;
12780 shadowed = TREE_CHAIN (shadowed))
12781 if (TREE_TYPE (shadowed) == TYPE_NAME (t))
12783 TREE_PURPOSE (shadowed) = NULL_TREE;
12788 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12789 && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
12794 /* If we know we are defining this tag, only look it up in
12795 this scope and don't try to find it as a type. */
12796 ref = lookup_tag (code, name, b, 1);
12802 ref = follow_tag_typedef (t);
12804 /* [dcl.type.elab] If the identifier resolves to a
12805 typedef-name or a template type-parameter, the
12806 elaborated-type-specifier is ill-formed. */
12809 pedwarn ("using typedef-name `%D' after `%s'",
12810 TYPE_NAME (t), tag_name (tag_code));
12813 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12814 error ("using template type parameter `%T' after `%s'",
12815 t, tag_name (tag_code));
12818 ref = lookup_tag (code, name, b, 0);
12822 /* Try finding it as a type declaration. If that wins,
12824 ref = lookup_name (name, 1);
12826 if (ref != NULL_TREE
12827 && processing_template_decl
12828 && DECL_CLASS_TEMPLATE_P (ref)
12829 && template_class_depth (current_class_type) == 0)
12830 /* Since GLOBALIZE is true, we're declaring a global
12831 template, so we want this type. */
12832 ref = DECL_TEMPLATE_RESULT (ref);
12834 if (ref && TREE_CODE (ref) == TYPE_DECL
12835 && TREE_CODE (TREE_TYPE (ref)) == code)
12836 ref = TREE_TYPE (ref);
12841 if (ref && current_class_type
12842 && template_class_depth (current_class_type)
12843 && PROCESSING_REAL_TEMPLATE_DECL_P ())
12845 /* Since GLOBALIZE is nonzero, we are not looking at a
12846 definition of this tag. Since, in addition, we are currently
12847 processing a (member) template declaration of a template
12848 class, we must be very careful; consider:
12855 { template <class V>
12856 friend struct S1; };
12858 Here, the S2::S1 declaration should not be confused with the
12859 outer declaration. In particular, the inner version should
12860 have a template parameter of level 2, not level 1. This
12861 would be particularly important if the member declaration
12864 template <class V = U> friend struct S1;
12866 say, when we should tsubst into `U' when instantiating
12867 S2. On the other hand, when presented with:
12877 we must find the inner binding eventually. We
12878 accomplish this by making sure that the new type we
12879 create to represent this declaration has the right
12881 context = TYPE_CONTEXT (ref);
12888 /* If no such tag is yet defined, create a forward-reference node
12889 and record it as the "definition".
12890 When a real declaration of this type is found,
12891 the forward-reference will be altered into a real type. */
12892 if (code == ENUMERAL_TYPE)
12894 error ("use of enum `%#D' without previous declaration", name);
12896 ref = make_node (ENUMERAL_TYPE);
12898 /* Give the type a default layout like unsigned int
12899 to avoid crashing if it does not get defined. */
12900 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12901 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12902 TYPE_USER_ALIGN (ref) = 0;
12903 TREE_UNSIGNED (ref) = 1;
12904 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12905 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12906 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12908 /* Enable us to recognize when a type is created in class context.
12909 To do nested classes correctly, this should probably be cleared
12910 out when we leave this classes scope. Currently this in only
12911 done in `start_enum'. */
12913 pushtag (name, ref, globalize);
12917 struct cp_binding_level *old_b = class_binding_level;
12919 ref = make_aggr_type (code);
12920 TYPE_CONTEXT (ref) = context;
12922 #ifdef NONNESTED_CLASSES
12923 /* Class types don't nest the way enums do. */
12924 class_binding_level = (struct cp_binding_level *)0;
12926 pushtag (name, ref, globalize);
12927 class_binding_level = old_b;
12932 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12933 redeclare_class_template (ref, current_template_parms);
12936 TYPE_ATTRIBUTES (ref) = attributes;
12942 xref_tag_from_type (tree old, tree id, int globalize)
12944 enum tag_types tag_kind;
12946 if (TREE_CODE (old) == RECORD_TYPE)
12947 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12949 tag_kind = union_type;
12951 if (id == NULL_TREE)
12952 id = TYPE_IDENTIFIER (old);
12954 return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
12957 /* REF is a type (named NAME), for which we have just seen some
12958 baseclasses. BINFO is a list of those baseclasses; the
12959 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12960 the base-class. CODE_TYPE_NODE indicates whether REF is a class,
12961 struct, or union. */
12964 xref_basetypes (tree ref, tree binfo)
12966 /* In the declaration `A : X, Y, ... Z' we mark all the types
12967 (A, X, Y, ..., Z) so we can check for duplicates. */
12972 enum tag_types tag_code;
12974 if (TREE_CODE (ref) == UNION_TYPE)
12976 error ("derived union `%T' invalid", ref);
12980 tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12982 len = list_length (binfo);
12984 /* First, make sure that any templates in base-classes are
12985 instantiated. This ensures that if we call ourselves recursively
12986 we do not get confused about which classes are marked and which
12988 for (base = binfo; base; base = TREE_CHAIN (base))
12989 complete_type (TREE_VALUE (base));
12991 SET_CLASSTYPE_MARKED (ref);
12992 BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
12994 for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
12996 /* The base of a derived struct is public by default. */
12998 = (TREE_PURPOSE (binfo) == access_public_node
12999 || TREE_PURPOSE (binfo) == access_public_virtual_node
13000 || (tag_code != class_type
13001 && (TREE_PURPOSE (binfo) == access_default_node
13002 || TREE_PURPOSE (binfo) == access_default_virtual_node)));
13004 = (TREE_PURPOSE (binfo) == access_protected_node
13005 || TREE_PURPOSE (binfo) == access_protected_virtual_node);
13007 = (TREE_PURPOSE (binfo) == access_private_virtual_node
13008 || TREE_PURPOSE (binfo) == access_protected_virtual_node
13009 || TREE_PURPOSE (binfo) == access_public_virtual_node
13010 || TREE_PURPOSE (binfo) == access_default_virtual_node);
13011 tree basetype = TREE_VALUE (binfo);
13014 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
13015 basetype = TREE_TYPE (basetype);
13017 || (TREE_CODE (basetype) != RECORD_TYPE
13018 && TREE_CODE (basetype) != TYPENAME_TYPE
13019 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
13020 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
13022 error ("base type `%T' fails to be a struct or class type",
13023 TREE_VALUE (binfo));
13027 /* This code replaces similar code in layout_basetypes.
13028 We put the complete_type first for implicit `typename'. */
13029 if (!COMPLETE_TYPE_P (basetype)
13030 && ! (current_template_parms && uses_template_parms (basetype)))
13032 error ("base class `%T' has incomplete type", basetype);
13037 if (CLASSTYPE_MARKED (basetype))
13039 if (basetype == ref)
13040 error ("recursive type `%T' undefined", basetype);
13042 error ("duplicate base type `%T' invalid", basetype);
13046 if (TYPE_FOR_JAVA (basetype)
13047 && (current_lang_depth () == 0))
13048 TYPE_FOR_JAVA (ref) = 1;
13050 /* Note that the BINFO records which describe individual
13051 inheritances are *not* shared in the lattice! They
13052 cannot be shared because a given baseclass may be
13053 inherited with different `accessibility' by different
13054 derived classes. (Each BINFO record describing an
13055 individual inheritance contains flags which say what
13056 the `accessibility' of that particular inheritance is.) */
13059 = make_binfo (size_zero_node, basetype,
13060 CLASS_TYPE_P (basetype)
13061 ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
13062 CLASS_TYPE_P (basetype)
13063 ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
13065 TREE_VEC_ELT (binfos, i) = base_binfo;
13066 TREE_VIA_PUBLIC (base_binfo) = via_public;
13067 TREE_VIA_PROTECTED (base_binfo) = via_protected;
13068 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
13069 BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
13071 /* We need to unshare the binfos now so that lookups during class
13072 definition work. */
13073 unshare_base_binfos (base_binfo);
13075 SET_CLASSTYPE_MARKED (basetype);
13077 /* We are free to modify these bits because they are meaningless
13078 at top level, and BASETYPE is a top-level type. */
13079 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
13081 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
13082 /* Converting to a virtual base class requires looking
13083 up the offset of the virtual base. */
13084 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
13087 if (CLASS_TYPE_P (basetype))
13089 TYPE_HAS_NEW_OPERATOR (ref)
13090 |= TYPE_HAS_NEW_OPERATOR (basetype);
13091 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
13092 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
13093 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13094 /* If the base-class uses multiple inheritance, so do we. */
13095 TYPE_USES_MULTIPLE_INHERITANCE (ref)
13096 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
13097 /* Likewise, if converting to a base of the base may require
13098 code, then we may need to generate code to convert to a
13100 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
13101 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
13108 TREE_VEC_LENGTH (binfos) = i;
13110 BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
13114 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
13115 /* If there is more than one non-empty they cannot be at the same
13117 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
13120 /* Unmark all the types. */
13122 CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
13123 CLEAR_CLASSTYPE_MARKED (ref);
13125 /* Now that we know all the base-classes, set up the list of virtual
13127 get_vbase_types (ref);
13131 /* Begin compiling the definition of an enumeration type.
13132 NAME is its name (or null if anonymous).
13133 Returns the type object, as yet incomplete.
13134 Also records info about it so that build_enumerator
13135 may be used to declare the individual values as they are read. */
13138 start_enum (tree name)
13140 register tree enumtype = NULL_TREE;
13141 struct cp_binding_level *b = current_binding_level;
13143 /* If this is the real definition for a previous forward reference,
13144 fill in the contents in the same object that used to be the
13145 forward reference. */
13147 if (name != NULL_TREE)
13148 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
13150 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13152 error ("multiple definition of `%#T'", enumtype);
13153 cp_error_at ("previous definition here", enumtype);
13154 /* Clear out TYPE_VALUES, and start again. */
13155 TYPE_VALUES (enumtype) = NULL_TREE;
13159 enumtype = make_node (ENUMERAL_TYPE);
13160 pushtag (name, enumtype, 0);
13166 /* After processing and defining all the values of an enumeration type,
13167 install their decls in the enumeration type and finish it off.
13168 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
13171 finish_enum (tree enumtype)
13182 /* We built up the VALUES in reverse order. */
13183 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13187 Following the closing brace of an enum-specifier, each
13188 enumerator has the type of its enumeration. Prior to the
13189 closing brace, the type of each enumerator is the type of
13190 its initializing value. */
13191 for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13192 TREE_TYPE (TREE_VALUE (pair)) = enumtype;
13194 /* For an enum defined in a template, all further processing is
13195 postponed until the template is instantiated. */
13196 if (processing_template_decl)
13198 if (at_function_scope_p ())
13199 add_stmt (build_min (TAG_DEFN, enumtype));
13203 /* Figure out what the minimum and maximum values of the enumerators
13205 if (TYPE_VALUES (enumtype))
13207 minnode = maxnode = NULL_TREE;
13209 for (pair = TYPE_VALUES (enumtype);
13211 pair = TREE_CHAIN (pair))
13215 value = DECL_INITIAL (TREE_VALUE (pair));
13218 minnode = maxnode = value;
13219 else if (tree_int_cst_lt (maxnode, value))
13221 else if (tree_int_cst_lt (value, minnode))
13226 minnode = maxnode = integer_zero_node;
13228 /* Compute the number of bits require to represent all values of the
13229 enumeration. We must do this before the type of MINNODE and
13230 MAXNODE are transformed, since min_precision relies on the
13231 TREE_TYPE of the value it is passed. */
13232 unsignedp = tree_int_cst_sgn (minnode) >= 0;
13233 lowprec = min_precision (minnode, unsignedp);
13234 highprec = min_precision (maxnode, unsignedp);
13235 precision = MAX (lowprec, highprec);
13237 /* Set the TREE_TYPE for the values as well. That's so that when we
13238 call decl_constant_value we get an entity of the right type (but
13239 with the constant value). In addition, transform the TYPE_VALUES
13240 list to contain the values, rather than the CONST_DECLs for them. */
13241 for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13243 tree value = DECL_INITIAL (TREE_VALUE (pair));
13245 TREE_TYPE (value) = enumtype;
13246 TREE_VALUE (pair) = value;
13249 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
13250 TYPE_SIZE (enumtype) = NULL_TREE;
13251 TYPE_PRECISION (enumtype) = precision;
13253 fixup_unsigned_type (enumtype);
13255 fixup_signed_type (enumtype);
13257 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13258 /* Use the width of the narrowest normal C type which is wide
13260 TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size
13263 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
13265 TYPE_SIZE (enumtype) = NULL_TREE;
13266 layout_type (enumtype);
13268 /* Fix up all variant types of this enum type. */
13269 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13271 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13272 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13273 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13274 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13275 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13276 TYPE_MODE (t) = TYPE_MODE (enumtype);
13277 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13278 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13279 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13280 TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13283 /* Finish debugging output for this type. */
13284 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13287 /* Build and install a CONST_DECL for an enumeration constant of the
13288 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13289 Assignment of sequential values by default is handled here. */
13292 build_enumerator (tree name, tree value, tree enumtype)
13299 /* Remove no-op casts from the value. */
13301 STRIP_TYPE_NOPS (value);
13303 if (! processing_template_decl)
13305 /* Validate and default VALUE. */
13306 if (value != NULL_TREE)
13308 value = decl_constant_value (value);
13310 if (TREE_CODE (value) == INTEGER_CST)
13312 value = default_conversion (value);
13313 constant_expression_warning (value);
13317 error ("enumerator value for `%D' not integer constant", name);
13322 /* Default based on previous value. */
13323 if (value == NULL_TREE && ! processing_template_decl)
13327 if (TYPE_VALUES (enumtype))
13329 /* The next value is the previous value ... */
13330 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13331 /* ... plus one. */
13332 value = cp_build_binary_op (PLUS_EXPR,
13336 if (tree_int_cst_lt (value, prev_value))
13337 error ("overflow in enumeration values at `%D'", name);
13340 value = integer_zero_node;
13343 /* Remove no-op casts from the value. */
13345 STRIP_TYPE_NOPS (value);
13347 /* To fix MAX_VAL enum consts. (bkoz) */
13348 TREE_TYPE (value) = integer_type_node;
13352 /* We always have to copy here; not all INTEGER_CSTs are unshared.
13353 Even in other cases, we will later (in finish_enum) be setting
13354 the type of VALUE. But, we don't need to make a copy if this
13355 VALUE is one of the enumeration constants for this same
13356 enumeration type. */
13357 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
13358 if (TREE_VALUE (values) == value)
13360 /* If we didn't break out of the loop, then we do need a copy. */
13361 if (!values && value)
13362 value = copy_node (value);
13364 /* C++ associates enums with global, function, or class declarations. */
13365 context = current_scope ();
13367 /* Build the actual enumeration constant. Note that the enumeration
13368 constants have the type of their initializers until the
13369 enumeration is complete:
13373 Following the closing brace of an enum-specifier, each enumer-
13374 ator has the type of its enumeration. Prior to the closing
13375 brace, the type of each enumerator is the type of its
13376 initializing value.
13378 In finish_enum we will reset the type. Of course, if we're
13379 processing a template, there may be no value. */
13380 type = value ? TREE_TYPE (value) : NULL_TREE;
13382 if (context && context == current_class_type)
13383 /* This enum declaration is local to the class. We need the full
13384 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
13385 decl = build_lang_decl (CONST_DECL, name, type);
13387 /* It's a global enum, or it's local to a function. (Note local to
13388 a function could mean local to a class method. */
13389 decl = build_decl (CONST_DECL, name, type);
13391 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13392 DECL_INITIAL (decl) = value;
13393 TREE_READONLY (decl) = 1;
13395 if (context && context == current_class_type)
13396 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13397 on the TYPE_FIELDS list for `S'. (That's so that you can say
13398 things like `S::i' later.) */
13399 finish_member_declaration (decl);
13403 /* Add this enumeration constant to the list for this type. */
13404 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13408 /* We're defining DECL. Make sure that it's type is OK. */
13411 check_function_type (tree decl, tree current_function_parms)
13413 tree fntype = TREE_TYPE (decl);
13414 tree return_type = complete_type (TREE_TYPE (fntype));
13416 /* In a function definition, arg types must be complete. */
13417 require_complete_types_for_parms (current_function_parms);
13419 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13421 error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13423 /* Make it return void instead, but don't change the
13424 type of the DECL_RESULT, in case we have a named return value. */
13425 if (TREE_CODE (fntype) == METHOD_TYPE)
13427 tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13429 = build_cplus_method_type (ctype,
13431 FUNCTION_ARG_CHAIN (decl));
13435 = build_function_type (void_type_node,
13436 TYPE_ARG_TYPES (TREE_TYPE (decl)));
13438 = build_exception_variant (fntype,
13439 TYPE_RAISES_EXCEPTIONS (fntype));
13442 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13445 /* Create the FUNCTION_DECL for a function definition.
13446 DECLSPECS and DECLARATOR are the parts of the declaration;
13447 they describe the function's name and the type it returns,
13448 but twisted together in a fashion that parallels the syntax of C.
13450 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13451 DECLARATOR is really the DECL for the function we are about to
13452 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13453 indicating that the function is an inline defined in-class.
13455 This function creates a binding context for the function body
13456 as well as setting up the FUNCTION_DECL in current_function_decl.
13458 Returns 1 on success. If the DECLARATOR is not suitable for a function
13459 (it defines a datum instead), we return 0, which tells
13460 yyparse to report a parse error.
13462 For C++, we must first check whether that datum makes any sense.
13463 For example, "class A local_a(1,2);" means that variable local_a
13464 is an aggregate of type A, which should have a constructor
13465 applied to it with the argument list [1, 2]. */
13468 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13471 tree ctype = NULL_TREE;
13474 int doing_friend = 0;
13475 struct cp_binding_level *bl;
13476 tree current_function_parms;
13478 /* Sanity check. */
13479 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13480 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13482 /* This should only be done once on the top most decl. */
13483 if (have_extern_spec)
13485 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13486 have_extern_spec = false;
13489 if (flags & SF_PRE_PARSED)
13491 decl1 = declarator;
13493 fntype = TREE_TYPE (decl1);
13494 if (TREE_CODE (fntype) == METHOD_TYPE)
13495 ctype = TYPE_METHOD_BASETYPE (fntype);
13497 /* ISO C++ 11.4/5. A friend function defined in a class is in
13498 the (lexical) scope of the class in which it is defined. */
13499 if (!ctype && DECL_FRIEND_P (decl1))
13501 ctype = DECL_FRIEND_CONTEXT (decl1);
13503 /* CTYPE could be null here if we're dealing with a template;
13504 for example, `inline friend float foo()' inside a template
13505 will have no CTYPE set. */
13506 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13512 last_function_parms = DECL_ARGUMENTS (decl1);
13516 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13517 /* If the declarator is not suitable for a function definition,
13518 cause a syntax error. */
13519 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13522 cplus_decl_attributes (&decl1, attrs, 0);
13524 /* If #pragma weak was used, mark the decl weak now. */
13525 if (current_binding_level == global_binding_level)
13526 maybe_apply_pragma_weak (decl1);
13528 fntype = TREE_TYPE (decl1);
13530 restype = TREE_TYPE (fntype);
13531 if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
13533 error ("semicolon missing after declaration of `%#T'", restype);
13534 shadow_tag (build_tree_list (NULL_TREE, restype));
13535 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13536 if (TREE_CODE (fntype) == FUNCTION_TYPE)
13537 fntype = build_function_type (integer_type_node,
13538 TYPE_ARG_TYPES (fntype));
13540 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13542 TYPE_ARG_TYPES (fntype));
13543 TREE_TYPE (decl1) = fntype;
13546 if (TREE_CODE (fntype) == METHOD_TYPE)
13547 ctype = TYPE_METHOD_BASETYPE (fntype);
13548 else if (DECL_MAIN_P (decl1))
13550 /* If this doesn't return integer_type, complain. */
13551 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13553 if (pedantic || warn_return_type)
13554 pedwarn ("return type for `main' changed to `int'");
13555 TREE_TYPE (decl1) = fntype = default_function_type;
13560 if (DECL_DECLARED_INLINE_P (decl1)
13561 && lookup_attribute ("noinline", attrs))
13562 warning_with_decl (decl1,
13563 "inline function `%s' given attribute noinline");
13565 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13566 /* This is a constructor, we must ensure that any default args
13567 introduced by this definition are propagated to the clones
13568 now. The clones are used directly in overload resolution. */
13569 adjust_clone_args (decl1);
13571 /* Sometimes we don't notice that a function is a static member, and
13572 build a METHOD_TYPE for it. Fix that up now. */
13573 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13574 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13576 revert_static_member_fn (decl1);
13577 last_function_parms = TREE_CHAIN (last_function_parms);
13581 /* Warn if function was previously implicitly declared
13582 (but not if we warned then). */
13583 if (! warn_implicit
13584 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13585 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13587 /* Set up current_class_type, and enter the scope of the class, if
13590 push_nested_class (ctype, 1);
13591 else if (DECL_STATIC_FUNCTION_P (decl1))
13592 push_nested_class (DECL_CONTEXT (decl1), 2);
13594 /* Now that we have entered the scope of the class, we must restore
13595 the bindings for any template parameters surrounding DECL1, if it
13596 is an inline member template. (Order is important; consider the
13597 case where a template parameter has the same name as a field of
13598 the class.) It is not until after this point that
13599 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13600 if (flags & SF_INCLASS_INLINE)
13601 maybe_begin_member_template_processing (decl1);
13603 /* Effective C++ rule 15. */
13605 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13606 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13607 warning ("`operator=' should return a reference to `*this'");
13609 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13610 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13611 if (!DECL_INITIAL (decl1))
13612 DECL_INITIAL (decl1) = error_mark_node;
13614 /* This function exists in static storage.
13615 (This does not mean `static' in the C sense!) */
13616 TREE_STATIC (decl1) = 1;
13618 /* We must call push_template_decl after current_class_type is set
13619 up. (If we are processing inline definitions after exiting a
13620 class scope, current_class_type will be NULL_TREE until set above
13621 by push_nested_class.) */
13622 if (processing_template_decl)
13623 decl1 = push_template_decl (decl1);
13625 /* We are now in the scope of the function being defined. */
13626 current_function_decl = decl1;
13628 /* Save the parm names or decls from this function's declarator
13629 where store_parm_decls will find them. */
13630 current_function_parms = last_function_parms;
13632 /* Make sure the parameter and return types are reasonable. When
13633 you declare a function, these types can be incomplete, but they
13634 must be complete when you define the function. */
13635 if (! processing_template_decl)
13636 check_function_type (decl1, current_function_parms);
13638 /* Build the return declaration for the function. */
13639 restype = TREE_TYPE (fntype);
13640 /* Promote the value to int before returning it. */
13641 if (c_promoting_integer_type_p (restype))
13642 restype = type_promotes_to (restype);
13643 if (DECL_RESULT (decl1) == NULL_TREE)
13645 DECL_RESULT (decl1)
13646 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13647 c_apply_type_quals_to_decl (cp_type_quals (restype),
13648 DECL_RESULT (decl1));
13651 /* Initialize RTL machinery. We cannot do this until
13652 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13653 even when processing a template; this is how we get
13654 CFUN set up, and our per-function variables initialized.
13655 FIXME factor out the non-RTL stuff. */
13656 bl = current_binding_level;
13657 init_function_start (decl1, input_filename, lineno);
13658 current_binding_level = bl;
13660 /* Even though we're inside a function body, we still don't want to
13661 call expand_expr to calculate the size of a variable-sized array.
13662 We haven't necessarily assigned RTL to all variables yet, so it's
13663 not safe to try to expand expressions involving them. */
13664 immediate_size_expand = 0;
13665 cfun->x_dont_save_pending_sizes_p = 1;
13667 /* Start the statement-tree, start the tree now. */
13668 begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13670 /* Let the user know we're compiling this function. */
13671 announce_function (decl1);
13673 /* Record the decl so that the function name is defined.
13674 If we already have a decl for this name, and it is a FUNCTION_DECL,
13675 use the old decl. */
13676 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13678 /* A specialization is not used to guide overload resolution. */
13679 if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13680 && ! DECL_FUNCTION_MEMBER_P (decl1))
13681 decl1 = pushdecl (decl1);
13684 /* We need to set the DECL_CONTEXT. */
13685 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13686 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13687 /* And make sure we have enough default args. */
13688 check_default_args (decl1);
13690 fntype = TREE_TYPE (decl1);
13693 /* Reset these in case the call to pushdecl changed them. */
13694 current_function_decl = decl1;
13695 cfun->decl = decl1;
13697 /* If we are (erroneously) defining a function that we have already
13698 defined before, wipe out what we knew before. */
13699 if (!DECL_PENDING_INLINE_P (decl1))
13700 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13702 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13704 /* We know that this was set up by `grokclassfn'. We do not
13705 wait until `store_parm_decls', since evil parse errors may
13706 never get us to that point. Here we keep the consistency
13707 between `current_class_type' and `current_class_ptr'. */
13708 tree t = DECL_ARGUMENTS (decl1);
13710 my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13712 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13715 cp_function_chain->x_current_class_ref
13716 = build_indirect_ref (t, NULL);
13717 cp_function_chain->x_current_class_ptr = t;
13719 /* Constructors and destructors need to know whether they're "in
13720 charge" of initializing virtual base classes. */
13721 t = TREE_CHAIN (t);
13722 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13724 current_in_charge_parm = t;
13725 t = TREE_CHAIN (t);
13727 if (DECL_HAS_VTT_PARM_P (decl1))
13729 if (DECL_NAME (t) != vtt_parm_identifier)
13731 current_vtt_parm = t;
13735 if (DECL_INTERFACE_KNOWN (decl1))
13737 tree ctx = decl_function_context (decl1);
13739 if (DECL_NOT_REALLY_EXTERN (decl1))
13740 DECL_EXTERNAL (decl1) = 0;
13742 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13743 && TREE_PUBLIC (ctx))
13744 /* This is a function in a local class in an extern inline
13746 comdat_linkage (decl1);
13748 /* If this function belongs to an interface, it is public.
13749 If it belongs to someone else's interface, it is also external.
13750 This only affects inlines and template instantiations. */
13751 else if (interface_unknown == 0
13752 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13753 || flag_alt_external_templates))
13755 if (DECL_DECLARED_INLINE_P (decl1)
13756 || DECL_TEMPLATE_INSTANTIATION (decl1)
13757 || processing_template_decl)
13759 DECL_EXTERNAL (decl1)
13761 || (DECL_DECLARED_INLINE_P (decl1)
13762 && ! flag_implement_inlines
13763 && !DECL_VINDEX (decl1)));
13765 /* For WIN32 we also want to put these in linkonce sections. */
13766 maybe_make_one_only (decl1);
13769 DECL_EXTERNAL (decl1) = 0;
13770 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13771 DECL_INTERFACE_KNOWN (decl1) = 1;
13773 else if (interface_unknown && interface_only
13774 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13775 || flag_alt_external_templates))
13777 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13778 interface, we will have interface_only set but not
13779 interface_known. In that case, we don't want to use the normal
13780 heuristics because someone will supply a #pragma implementation
13781 elsewhere, and deducing it here would produce a conflict. */
13782 comdat_linkage (decl1);
13783 DECL_EXTERNAL (decl1) = 0;
13784 DECL_INTERFACE_KNOWN (decl1) = 1;
13785 DECL_DEFER_OUTPUT (decl1) = 1;
13789 /* This is a definition, not a reference.
13790 So clear DECL_EXTERNAL. */
13791 DECL_EXTERNAL (decl1) = 0;
13793 if ((DECL_DECLARED_INLINE_P (decl1)
13794 || DECL_TEMPLATE_INSTANTIATION (decl1))
13795 && ! DECL_INTERFACE_KNOWN (decl1)
13796 /* Don't try to defer nested functions for now. */
13797 && ! decl_function_context (decl1))
13798 DECL_DEFER_OUTPUT (decl1) = 1;
13800 DECL_INTERFACE_KNOWN (decl1) = 1;
13804 current_binding_level->parm_flag = 1;
13808 if (DECL_DESTRUCTOR_P (decl1))
13810 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13811 DECL_CONTEXT (dtor_label) = current_function_decl;
13814 start_fname_decls ();
13816 store_parm_decls (current_function_parms);
13821 /* Store the parameter declarations into the current function declaration.
13822 This is called after parsing the parameter declarations, before
13823 digesting the body of the function.
13825 Also install to binding contour return value identifier, if any. */
13828 store_parm_decls (tree current_function_parms)
13830 register tree fndecl = current_function_decl;
13831 register tree parm;
13833 /* This is a chain of any other decls that came in among the parm
13834 declarations. If a parm is declared with enum {foo, bar} x;
13835 then CONST_DECLs for foo and bar are put here. */
13836 tree nonparms = NULL_TREE;
13838 if (current_function_parms)
13840 /* This case is when the function was defined with an ANSI prototype.
13841 The parms already have decls, so we need not do anything here
13842 except record them as in effect
13843 and complain if any redundant old-style parm decls were written. */
13845 tree specparms = current_function_parms;
13848 /* Must clear this because it might contain TYPE_DECLs declared
13850 storedecls (NULL_TREE);
13852 /* If we're doing semantic analysis, then we'll call pushdecl
13853 for each of these. We must do them in reverse order so that
13854 they end in the correct forward order. */
13855 specparms = nreverse (specparms);
13857 for (parm = specparms; parm; parm = next)
13859 next = TREE_CHAIN (parm);
13860 if (TREE_CODE (parm) == PARM_DECL)
13862 if (DECL_NAME (parm) == NULL_TREE
13863 || TREE_CODE (parm) != VOID_TYPE)
13866 error ("parameter `%D' declared void", parm);
13870 /* If we find an enum constant or a type tag,
13871 put it aside for the moment. */
13872 TREE_CHAIN (parm) = NULL_TREE;
13873 nonparms = chainon (nonparms, parm);
13877 /* Get the decls in their original chain order and record in the
13878 function. This is all and only the PARM_DECLs that were
13879 pushed into scope by the loop above. */
13880 DECL_ARGUMENTS (fndecl) = getdecls ();
13881 storetags (gettags ());
13884 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13886 /* Now store the final chain of decls for the arguments
13887 as the decl-chain of the current lexical scope.
13888 Put the enumerators in as well, at the front so that
13889 DECL_ARGUMENTS is not modified. */
13890 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13892 /* Do the starting of the exception specifications, if we have any. */
13893 if (flag_exceptions && !processing_template_decl
13894 && flag_enforce_eh_specs
13895 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13896 current_eh_spec_block = begin_eh_spec_block ();
13900 /* We have finished doing semantic analysis on DECL, but have not yet
13901 generated RTL for its body. Save away our current state, so that
13902 when we want to generate RTL later we know what to do. */
13905 save_function_data (tree decl)
13907 struct language_function *f;
13909 /* Save the language-specific per-function data so that we can
13910 get it back when we really expand this function. */
13911 my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13915 f = ((struct language_function *)
13916 ggc_alloc (sizeof (struct language_function)));
13917 memcpy (f, cp_function_chain, sizeof (struct language_function));
13918 DECL_SAVED_FUNCTION_DATA (decl) = f;
13920 /* Clear out the bits we don't need. */
13921 f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13922 f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13923 f->x_named_label_uses = NULL;
13924 f->bindings = NULL;
13925 f->x_local_names = NULL;
13927 /* When we get back here again, we will be expanding. */
13928 f->x_expanding_p = 1;
13930 /* If we've already decided that we cannot inline this function, we
13931 must remember that fact when we actually go to expand the
13933 if (current_function_cannot_inline)
13935 f->cannot_inline = current_function_cannot_inline;
13936 DECL_INLINE (decl) = 0;
13940 /* Add a note to mark the beginning of the main body of the constructor.
13941 This is used to set up the data structures for the cleanup regions for
13942 fully-constructed bases and members. */
13945 begin_constructor_body (void)
13949 /* Add a note to mark the end of the main body of the constructor. This is
13950 used to end the cleanup regions for fully-constructed bases and
13954 finish_constructor_body (void)
13958 /* Do all the processing for the beginning of a destructor; set up the
13959 vtable pointers and cleanups for bases and members. */
13962 begin_destructor_body (void)
13965 tree compound_stmt;
13967 /* If the dtor is empty, and we know there is not any possible
13968 way we could use any vtable entries, before they are possibly
13969 set by a base class dtor, we don't have to setup the vtables,
13970 as we know that any base class dtor will set up any vtables
13971 it needs. We avoid MI, because one base class dtor can do a
13972 virtual dispatch to an overridden function that would need to
13973 have a non-related vtable set up, we cannot avoid setting up
13974 vtables in that case. We could change this to see if there
13975 is just one vtable.
13977 ??? In the destructor for a class, the vtables are set
13978 appropriately for that class. There will be no non-related
13979 vtables. jason 2001-12-11. */
13980 if_stmt = begin_if_stmt ();
13982 /* If it is not safe to avoid setting up the vtables, then
13983 someone will change the condition to be boolean_true_node.
13984 (Actually, for now, we do not have code to set the condition
13985 appropriately, so we just assume that we always need to
13986 initialize the vtables.) */
13987 finish_if_stmt_cond (boolean_true_node, if_stmt);
13989 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13991 /* Make all virtual function table pointers in non-virtual base
13992 classes point to CURRENT_CLASS_TYPE's virtual function
13994 initialize_vtbl_ptrs (current_class_ptr);
13996 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13997 finish_then_clause (if_stmt);
14000 /* And insert cleanups for our bases and members so that they
14001 will be properly destroyed if we throw. */
14002 push_base_cleanups ();
14005 /* At the end of every destructor we generate code to delete the object if
14006 necessary. Do that now. */
14009 finish_destructor_body (void)
14013 /* Any return from a destructor will end up here; that way all base
14014 and member cleanups will be run when the function returns. */
14015 add_stmt (build_stmt (LABEL_STMT, dtor_label));
14017 /* In a virtual destructor, we must call delete. */
14018 if (DECL_VIRTUAL_P (current_function_decl))
14021 tree virtual_size = cxx_sizeof (current_class_type);
14025 At the point of definition of a virtual destructor (including
14026 an implicit definition), non-placement operator delete shall
14027 be looked up in the scope of the destructor's class and if
14028 found shall be accessible and unambiguous. */
14029 exprstmt = build_op_delete_call
14030 (DELETE_EXPR, current_class_ptr, virtual_size,
14031 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
14033 if_stmt = begin_if_stmt ();
14034 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
14035 current_in_charge_parm,
14038 finish_expr_stmt (exprstmt);
14039 finish_then_clause (if_stmt);
14044 /* Do the necessary processing for the beginning of a function body, which
14045 in this case includes member-initializers, but not the catch clauses of
14046 a function-try-block. Currently, this means opening a binding level
14047 for the member-initializers (in a ctor) and member cleanups (in a dtor).
14048 In other functions, this isn't necessary, but it doesn't hurt. */
14051 begin_function_body (void)
14055 if (processing_template_decl)
14056 /* Do nothing now. */;
14058 /* Always keep the BLOCK node associated with the outermost pair of
14059 curly braces of a function. These are needed for correct
14060 operation of dwarfout.c. */
14061 keep_next_level (1);
14063 stmt = begin_compound_stmt (0);
14064 COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
14066 if (processing_template_decl)
14067 /* Do nothing now. */;
14068 else if (DECL_CONSTRUCTOR_P (current_function_decl))
14069 begin_constructor_body ();
14070 else if (DECL_DESTRUCTOR_P (current_function_decl))
14071 begin_destructor_body ();
14076 /* Do the processing for the end of a function body. Currently, this means
14077 closing out the cleanups for fully-constructed bases and members, and in
14078 the case of the destructor, deleting the object if desired. Again, this
14079 is only meaningful for [cd]tors, since they are the only functions where
14080 there is a significant distinction between the main body and any
14081 function catch clauses. Handling, say, main() return semantics here
14082 would be wrong, as flowing off the end of a function catch clause for
14083 main() would also need to return 0. */
14086 finish_function_body (tree compstmt)
14088 /* Close the block. */
14089 finish_compound_stmt (0, compstmt);
14091 if (processing_template_decl)
14092 /* Do nothing now. */;
14093 else if (DECL_CONSTRUCTOR_P (current_function_decl))
14094 finish_constructor_body ();
14095 else if (DECL_DESTRUCTOR_P (current_function_decl))
14096 finish_destructor_body ();
14099 /* Finish up a function declaration and compile that function
14100 all the way to assembler language output. The free the storage
14101 for the function definition.
14103 FLAGS is a bitwise or of the following values:
14105 We just finished processing the body of an in-class inline
14106 function definition. (This processing will have taken place
14107 after the class definition is complete.) */
14110 finish_function (int flags)
14112 register tree fndecl = current_function_decl;
14113 tree fntype, ctype = NULL_TREE;
14114 int inclass_inline = (flags & 2) != 0;
14117 /* When we get some parse errors, we can end up without a
14118 current_function_decl, so cope. */
14119 if (fndecl == NULL_TREE)
14120 return error_mark_node;
14122 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14123 && DECL_VIRTUAL_P (fndecl)
14124 && !processing_template_decl)
14126 tree fnclass = DECL_CONTEXT (fndecl);
14127 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14128 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14131 nested = function_depth > 1;
14132 fntype = TREE_TYPE (fndecl);
14134 /* TREE_READONLY (fndecl) = 1;
14135 This caused &foo to be of type ptr-to-const-function
14136 which then got a warning when stored in a ptr-to-function variable. */
14138 my_friendly_assert (building_stmt_tree (), 20000911);
14140 finish_fname_decls ();
14142 /* For a cloned function, we've already got all the code we need;
14143 there's no need to add any extra bits. */
14144 if (!DECL_CLONED_FUNCTION_P (fndecl))
14146 if (DECL_MAIN_P (current_function_decl))
14148 /* Make it so that `main' always returns 0 by default. */
14150 finish_return_stmt (integer_one_node);
14152 finish_return_stmt (integer_zero_node);
14156 /* Finish dealing with exception specifiers. */
14157 if (flag_exceptions && !processing_template_decl
14158 && flag_enforce_eh_specs
14159 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
14160 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14161 (TREE_TYPE (current_function_decl)),
14162 current_eh_spec_block);
14165 /* If we're saving up tree structure, tie off the function now. */
14166 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
14168 /* This must come after expand_function_end because cleanups might
14169 have declarations (from inline functions) that need to go into
14170 this function's blocks. */
14172 /* If the current binding level isn't the outermost binding level
14173 for this function, either there is a bug, or we have experienced
14174 syntax errors and the statement tree is malformed. */
14175 if (current_binding_level->parm_flag != 1)
14177 /* Make sure we have already experienced errors. */
14178 if (errorcount == 0)
14181 /* Throw away the broken statement tree and extra binding
14183 DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14185 while (current_binding_level->parm_flag != 1)
14187 if (current_binding_level->parm_flag == 2)
14188 pop_nested_class ();
14190 poplevel (0, 0, 0);
14193 poplevel (1, 0, 1);
14195 /* Set up the named return value optimization, if we can. Here, we
14196 eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14197 for the nrv. genrtl_start_function and declare_return_variable
14198 handle making the nrv and RESULT_DECL share space. */
14199 if (current_function_return_value)
14201 tree r = current_function_return_value;
14202 /* This is only worth doing for fns that return in memory--and
14203 simpler, since we don't have to worry about promoted modes. */
14204 if (r != error_mark_node
14205 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl))))
14207 DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14208 walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14209 nullify_returns_r, r);
14212 /* Clear it so genrtl_start_function and declare_return_variable
14213 know we're not optimizing. */
14214 current_function_return_value = NULL_TREE;
14217 /* Remember that we were in class scope. */
14218 if (current_class_name)
14219 ctype = current_class_type;
14221 /* Must mark the RESULT_DECL as being in this function. */
14222 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14224 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14225 to the FUNCTION_DECL node itself. */
14226 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14228 /* Save away current state, if appropriate. */
14229 if (!processing_template_decl)
14230 save_function_data (fndecl);
14232 /* If this function calls `setjmp' it cannot be inlined. When
14233 `longjmp' is called it is not guaranteed to restore the value of
14234 local variables that have been modified since the call to
14235 `setjmp'. So, if were to inline this function into some caller
14236 `c', then when we `longjmp', we might not restore all variables
14237 in `c'. (It might seem, at first blush, that there's no way for
14238 this function to modify local variables in `c', but their
14239 addresses may have been stored somewhere accessible to this
14241 if (!processing_template_decl && calls_setjmp_p (fndecl))
14242 DECL_UNINLINABLE (fndecl) = 1;
14244 /* Complain if there's just no return statement. */
14245 if (warn_return_type
14246 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14247 && !current_function_returns_value && !current_function_returns_null
14248 /* Don't complain if we abort or throw. */
14249 && !current_function_returns_abnormally
14250 && !DECL_NAME (DECL_RESULT (fndecl))
14251 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
14252 inline function, as we might never be compiled separately. */
14253 && (DECL_INLINE (fndecl) || processing_template_decl))
14254 warning ("no return statement in function returning non-void");
14256 /* Clear out memory we no longer need. */
14257 free_after_parsing (cfun);
14258 /* Since we never call rest_of_compilation, we never clear
14259 CFUN. Do so explicitly. */
14260 free_after_compilation (cfun);
14263 /* If this is an in-class inline definition, we may have to pop the
14264 bindings for the template parameters that we added in
14265 maybe_begin_member_template_processing when start_function was
14267 if (inclass_inline)
14268 maybe_end_member_template_processing ();
14270 /* Leave the scope of the class. */
14272 pop_nested_class ();
14278 /* Let the error reporting routines know that we're outside a
14279 function. For a nested function, this value is used in
14280 cxx_pop_function_context and then reset via pop_function_context. */
14281 current_function_decl = NULL_TREE;
14286 /* Create the FUNCTION_DECL for a function definition.
14287 DECLSPECS and DECLARATOR are the parts of the declaration;
14288 they describe the return type and the name of the function,
14289 but twisted together in a fashion that parallels the syntax of C.
14291 This function creates a binding context for the function body
14292 as well as setting up the FUNCTION_DECL in current_function_decl.
14294 Returns a FUNCTION_DECL on success.
14296 If the DECLARATOR is not suitable for a function (it defines a datum
14297 instead), we return 0, which tells yyparse to report a parse error.
14299 May return void_type_node indicating that this method is actually
14300 a friend. See grokfield for more details.
14302 Came here with a `.pushlevel' .
14304 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14305 CHANGES TO CODE IN `grokfield'. */
14308 start_method (tree declspecs, tree declarator, tree attrlist)
14310 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14313 if (fndecl == error_mark_node)
14314 return error_mark_node;
14316 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14318 error ("invalid member function declaration");
14319 return error_mark_node;
14323 cplus_decl_attributes (&fndecl, attrlist, 0);
14325 /* Pass friends other than inline friend functions back. */
14326 if (fndecl == void_type_node)
14329 if (DECL_IN_AGGR_P (fndecl))
14331 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14333 if (DECL_CONTEXT (fndecl)
14334 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14335 error ("`%D' is already defined in class `%T'", fndecl,
14336 DECL_CONTEXT (fndecl));
14338 return void_type_node;
14341 check_template_shadow (fndecl);
14343 DECL_DECLARED_INLINE_P (fndecl) = 1;
14345 if (flag_default_inline)
14346 DECL_INLINE (fndecl) = 1;
14348 /* We process method specializations in finish_struct_1. */
14349 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14350 fndecl = push_template_decl (fndecl);
14352 if (! DECL_FRIEND_P (fndecl))
14354 if (TREE_CHAIN (fndecl))
14356 fndecl = copy_node (fndecl);
14357 TREE_CHAIN (fndecl) = NULL_TREE;
14359 grok_special_member_properties (fndecl);
14362 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14364 /* Make a place for the parms */
14366 current_binding_level->parm_flag = 1;
14368 DECL_IN_AGGR_P (fndecl) = 1;
14372 /* Go through the motions of finishing a function definition.
14373 We don't compile this method until after the whole class has
14376 FINISH_METHOD must return something that looks as though it
14377 came from GROKFIELD (since we are defining a method, after all).
14379 This is called after parsing the body of the function definition.
14380 STMTS is the chain of statements that makes up the function body.
14382 DECL is the ..._DECL that `start_method' provided. */
14385 finish_method (tree decl)
14387 register tree fndecl = decl;
14390 register tree link;
14392 if (decl == void_type_node)
14395 old_initial = DECL_INITIAL (fndecl);
14397 /* Undo the level for the parms (from start_method).
14398 This is like poplevel, but it causes nothing to be
14399 saved. Saving information here confuses symbol-table
14400 output routines. Besides, this information will
14401 be correctly output when this method is actually
14404 /* Clear out the meanings of the local variables of this level;
14405 also record in each decl which block it belongs to. */
14407 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14409 if (DECL_NAME (link) != NULL_TREE)
14410 pop_binding (DECL_NAME (link), link);
14411 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14412 DECL_CONTEXT (link) = NULL_TREE;
14415 poplevel (0, 0, 0);
14417 DECL_INITIAL (fndecl) = old_initial;
14419 /* We used to check if the context of FNDECL was different from
14420 current_class_type as another way to get inside here. This didn't work
14421 for String.cc in libg++. */
14422 if (DECL_FRIEND_P (fndecl))
14424 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14425 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14426 decl = void_type_node;
14433 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14434 we can lay it out later, when and if its type becomes complete. */
14437 maybe_register_incomplete_var (tree var)
14439 my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14441 /* Keep track of variables with incomplete types. */
14442 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14443 && DECL_EXTERNAL (var))
14445 tree inner_type = TREE_TYPE (var);
14447 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14448 inner_type = TREE_TYPE (inner_type);
14449 inner_type = TYPE_MAIN_VARIANT (inner_type);
14451 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14452 /* RTTI TD entries are created while defining the type_info. */
14453 || (TYPE_LANG_SPECIFIC (inner_type)
14454 && TYPE_BEING_DEFINED (inner_type)))
14455 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14459 /* Called when a class type (given by TYPE) is defined. If there are
14460 any existing VAR_DECLs whose type hsa been completed by this
14461 declaration, update them now. */
14464 complete_vars (tree type)
14466 tree *list = &incomplete_vars;
14468 my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14471 if (same_type_p (type, TREE_PURPOSE (*list)))
14473 tree var = TREE_VALUE (*list);
14474 /* Complete the type of the variable. The VAR_DECL itself
14475 will be laid out in expand_expr. */
14476 complete_type (TREE_TYPE (var));
14477 /* Remove this entry from the list. */
14478 *list = TREE_CHAIN (*list);
14481 list = &TREE_CHAIN (*list);
14485 /* If DECL is of a type which needs a cleanup, build that cleanup
14489 cxx_maybe_build_cleanup (tree decl)
14491 tree type = TREE_TYPE (decl);
14493 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14495 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14498 if (TREE_CODE (type) == ARRAY_TYPE)
14502 cxx_mark_addressable (decl);
14503 rval = build_unary_op (ADDR_EXPR, decl, 0);
14506 /* Optimize for space over speed here. */
14507 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14508 || flag_expensive_optimizations)
14509 flags |= LOOKUP_NONVIRTUAL;
14511 rval = build_delete (TREE_TYPE (rval), rval,
14512 sfk_complete_destructor, flags, 0);
14514 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14515 && ! TYPE_HAS_DESTRUCTOR (type))
14516 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
14517 build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
14524 /* When a stmt has been parsed, this function is called. */
14529 /* Always assume this statement was not an expression statement. If
14530 it actually was an expression statement, its our callers
14531 responsibility to fix this up. */
14532 last_expr_type = NULL_TREE;
14535 /* DECL was originally constructed as a non-static member function,
14536 but turned out to be static. Update it accordingly. */
14539 revert_static_member_fn (tree decl)
14542 tree function = TREE_TYPE (decl);
14543 tree args = TYPE_ARG_TYPES (function);
14545 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14546 != TYPE_UNQUALIFIED)
14547 error ("static member function `%#D' declared with type qualifiers",
14550 args = TREE_CHAIN (args);
14551 tmp = build_function_type (TREE_TYPE (function), args);
14552 tmp = build_qualified_type (tmp, cp_type_quals (function));
14553 tmp = build_exception_variant (tmp,
14554 TYPE_RAISES_EXCEPTIONS (function));
14555 TREE_TYPE (decl) = tmp;
14556 if (DECL_ARGUMENTS (decl))
14557 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14558 DECL_STATIC_FUNCTION_P (decl) = 1;
14561 /* Initialize the variables used during compilation of a C++
14565 cxx_push_function_context (struct function * f)
14567 struct language_function *p
14568 = ((struct language_function *)
14569 ggc_alloc_cleared (sizeof (struct language_function)));
14572 /* It takes an explicit call to expand_body to generate RTL for a
14576 /* Whenever we start a new function, we destroy temporaries in the
14578 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14581 /* Free the language-specific parts of F, now that we've finished
14582 compiling the function. */
14585 cxx_pop_function_context (struct function * f)
14590 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14591 one of the language-independent trees. */
14593 enum cp_tree_node_structure_enum
14594 cp_tree_node_structure (union lang_tree_node * t)
14596 switch (TREE_CODE (&t->generic))
14598 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
14599 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
14600 case CPLUS_BINDING: return TS_CP_BINDING;
14601 case OVERLOAD: return TS_CP_OVERLOAD;
14602 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14603 case PTRMEM_CST: return TS_CP_PTRMEM;
14604 case WRAPPER: return TS_CP_WRAPPER;
14605 case SRCLOC: return TS_CP_SRCLOC;
14606 default: return TS_CP_GENERIC;
14610 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14611 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
14614 identifier_global_value (tree t)
14616 return IDENTIFIER_GLOBAL_VALUE (t);
14619 /* Build the void_list_node (void_type_node having been created). */
14621 build_void_list_node (void)
14623 tree t = build_tree_list (NULL_TREE, void_type_node);
14624 TREE_PARMLIST (t) = 1;
14629 cp_missing_noreturn_ok_p (tree decl)
14631 /* A missing noreturn is ok for the `main' function. */
14632 return DECL_MAIN_P (decl);
14635 #include "gt-cp-decl.h"
14636 #include "gtype-cp.h"