* decl.c (check_previous_goto_1): Adjust prototype.
[official-gcc.git] / gcc / cp / decl.c
blob2c911a50329e763d2a9a06aa16a2244ab773b34a
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)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 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. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "expr.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "lex.h"
43 #include "output.h"
44 #include "except.h"
45 #include "toplev.h"
46 #include "hashtab.h"
47 #include "tm_p.h"
48 #include "target.h"
49 #include "c-common.h"
50 #include "c-pragma.h"
51 #include "diagnostic.h"
52 #include "debug.h"
53 #include "timevar.h"
55 static tree grokparms (tree);
56 static const char *redeclaration_error_message (tree, tree);
58 static void push_binding_level (struct cp_binding_level *, int,
59 int);
60 static void pop_binding_level (void);
61 static void suspend_binding_level (void);
62 static void resume_binding_level (struct cp_binding_level *);
63 static struct cp_binding_level *make_binding_level (void);
64 static void declare_namespace_level (void);
65 static int decl_jump_unsafe (tree);
66 static void storedecls (tree);
67 static void require_complete_types_for_parms (tree);
68 static int ambi_op_p (enum tree_code);
69 static int unary_op_p (enum tree_code);
70 static cxx_saved_binding *store_bindings (tree, cxx_saved_binding *);
71 static tree lookup_tag_reverse (tree, tree);
72 static void push_local_name (tree);
73 static void warn_extern_redeclared_static (tree, tree);
74 static tree grok_reference_init (tree, tree, tree);
75 static tree grokfndecl (tree, tree, tree, tree, int,
76 enum overload_flags, tree,
77 tree, int, int, int, int, int, int, tree);
78 static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
79 static tree follow_tag_typedef (tree);
80 static tree lookup_tag (enum tree_code, tree,
81 struct cp_binding_level *, int);
82 static void set_identifier_type_value_with_scope
83 (tree, tree, struct cp_binding_level *);
84 static void record_unknown_type (tree, const char *);
85 static tree builtin_function_1 (const char *, tree, tree, int,
86 enum built_in_class, const char *,
87 tree);
88 static tree build_library_fn_1 (tree, enum tree_code, tree);
89 static int member_function_or_else (tree, tree, enum overload_flags);
90 static void bad_specifiers (tree, const char *, int, int, int, int,
91 int);
92 static tree maybe_process_template_type_declaration
93 (tree, int, struct cp_binding_level*);
94 static void check_for_uninitialized_const_var (tree);
95 static hashval_t typename_hash (const void *);
96 static int typename_compare (const void *, const void *);
97 static void push_binding (tree, tree, struct cp_binding_level*);
98 static int add_binding (tree, tree);
99 static void pop_binding (tree, tree);
100 static tree local_variable_p_walkfn (tree *, int *, void *);
101 static tree select_decl (cxx_binding *, 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 int walk_namespaces_r (tree, walk_namespaces_fn, void *);
109 static int walk_globals_r (tree, void*);
110 static int walk_vtables_r (tree, void*);
111 static void add_decl_to_level (tree, struct cp_binding_level *);
112 static tree make_label_decl (tree, int);
113 static void use_label (tree);
114 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
115 const location_t *);
116 static void check_previous_goto (struct named_label_use_list *);
117 static void check_switch_goto (struct cp_binding_level *);
118 static void check_previous_gotos (tree);
119 static void pop_label (tree, tree);
120 static void pop_labels (tree);
121 static void maybe_deduce_size_from_array_init (tree, tree);
122 static void layout_var_decl (tree);
123 static void maybe_commonize_var (tree);
124 static tree check_initializer (tree, tree, int);
125 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
126 static void save_function_data (tree);
127 static void check_function_type (tree, tree);
128 static void begin_constructor_body (void);
129 static void finish_constructor_body (void);
130 static void begin_destructor_body (void);
131 static void finish_destructor_body (void);
132 static tree create_array_type_for_decl (tree, tree, tree);
133 static tree get_atexit_node (void);
134 static tree get_dso_handle_node (void);
135 static tree start_cleanup_fn (void);
136 static void end_cleanup_fn (void);
137 static tree cp_make_fname_decl (tree, int);
138 static void initialize_predefined_identifiers (void);
139 static tree check_special_function_return_type
140 (special_function_kind, tree, tree);
141 static tree push_cp_library_fn (enum tree_code, tree);
142 static tree build_cp_library_fn (tree, enum tree_code, tree);
143 static void store_parm_decls (tree);
144 static int cp_missing_noreturn_ok_p (tree);
145 static void initialize_local_var (tree, tree);
146 static void expand_static_init (tree, tree);
147 static tree next_initializable_field (tree);
148 static tree reshape_init (tree, tree *);
149 static tree build_typename_type (tree, tree, tree);
151 #if defined (DEBUG_BINDING_LEVELS)
152 static void indent (void);
153 #endif
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.
161 C++ extensions
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[]'
179 tree vtbl_type_node;
180 tree vtbl_ptr_type_node;
182 Namespaces,
184 tree std_node;
185 tree abi_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.
191 tree abort_fndecl;
193 The FUNCTION_DECL for the default `::operator delete'.
195 tree global_delete_fndecl;
197 Used by RTTI
198 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
199 tree tinfo_var_id;
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;
216 tree names_in_scope;
217 tree label_decl;
218 location_t o_goto_locus;
219 struct named_label_use_list *next;
222 #define named_label_uses cp_function_chain->x_named_label_uses
224 #define local_names cp_function_chain->x_local_names
226 /* A list of objects which have constructors or destructors
227 which reside in the global scope. The decl is stored in
228 the TREE_VALUE slot and the initializer is stored
229 in the TREE_PURPOSE slot. */
230 tree static_aggregates;
232 /* -- end of C++ */
234 /* A node for the integer constants 2, and 3. */
236 tree integer_two_node, integer_three_node;
238 /* Similar, for last_function_parm_tags. */
239 tree last_function_parms;
241 /* A list of all LABEL_DECLs in the function that have names. Here so
242 we can clear out their names' definitions at the end of the
243 function, and so we can check the validity of jumps to these labels. */
245 struct named_label_list GTY(())
247 struct cp_binding_level *binding_level;
248 tree names_in_scope;
249 tree old_value;
250 tree label_decl;
251 tree bad_decls;
252 struct named_label_list *next;
253 unsigned int in_try_scope : 1;
254 unsigned int in_catch_scope : 1;
257 #define named_labels cp_function_chain->x_named_labels
259 /* The name of the anonymous namespace, throughout this translation
260 unit. */
261 tree anonymous_namespace_name;
263 /* The number of function bodies which we are currently processing.
264 (Zero if we are at namespace scope, one inside the body of a
265 function, two inside the body of a function in a local class, etc.) */
266 int function_depth;
268 /* States indicating how grokdeclarator() should handle declspecs marked
269 with __attribute__((deprecated)). An object declared as
270 __attribute__((deprecated)) suppresses warnings of uses of other
271 deprecated items. */
273 enum deprecated_states {
274 DEPRECATED_NORMAL,
275 DEPRECATED_SUPPRESS
278 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
280 /* Set by add_implicitly_declared_members() to keep those members from
281 being flagged as deprecated or reported as using deprecated
282 types. */
283 int adding_implicit_members = 0;
285 /* True if a declaration with an `extern' linkage specifier is being
286 processed. */
287 bool have_extern_spec;
290 /* For each binding contour we allocate a binding_level structure
291 which records the names defined in that contour.
292 Contours include:
293 0) the global one
294 1) one for each function definition,
295 where internal declarations of the parameters appear.
296 2) one for each compound statement,
297 to record its declarations.
299 The current meaning of a name can be found by searching the levels
300 from the current one out to the global one.
302 Off to the side, may be the class_binding_level. This exists only
303 to catch class-local declarations. It is otherwise nonexistent.
305 Also there may be binding levels that catch cleanups that must be
306 run when exceptions occur. Thus, to see whether a name is bound in
307 the current scope, it is not enough to look in the
308 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
309 instead. */
311 /* Note that the information in the `names' component of the global contour
312 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
314 struct cp_binding_level GTY(())
316 /* A chain of _DECL nodes for all variables, constants, functions,
317 and typedef types. These are in the reverse of the order
318 supplied. There may be OVERLOADs on this list, too, but they
319 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
320 tree names;
322 /* Count of elements in names chain. */
323 size_t names_size;
325 /* A chain of NAMESPACE_DECL nodes. */
326 tree namespaces;
328 /* An array of static functions and variables (for namespaces only) */
329 varray_type static_decls;
331 /* A chain of VTABLE_DECL nodes. */
332 tree vtables;
334 /* A list of structure, union and enum definitions, for looking up
335 tag names.
336 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
337 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
338 or ENUMERAL_TYPE node.
340 C++: the TREE_VALUE nodes can be simple types for
341 component_bindings. */
342 tree tags;
344 /* A list of USING_DECL nodes. */
345 tree usings;
347 /* A list of used namespaces. PURPOSE is the namespace,
348 VALUE the common ancestor with this binding_level's namespace. */
349 tree using_directives;
351 /* If this binding level is the binding level for a class, then
352 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
353 is the name of an entity bound in the class. The TREE_TYPE is
354 the DECL bound by this name in the class. */
355 tree class_shadowed;
357 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
358 is used for all binding levels. In addition the TREE_VALUE is the
359 IDENTIFIER_TYPE_VALUE before we entered the class. */
360 tree type_shadowed;
362 /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local
363 label in this scope. The TREE_PURPOSE is the previous value of
364 the IDENTIFIER_LABEL VALUE. */
365 tree shadowed_labels;
367 /* For each level (except not the global one),
368 a chain of BLOCK nodes for all the levels
369 that were entered and exited one level down. */
370 tree blocks;
372 /* The _TYPE node for this level, if parm_flag == 2. */
373 tree this_class;
375 /* The binding level which this one is contained in (inherits from). */
376 struct cp_binding_level *level_chain;
378 /* List of VAR_DECLS saved from a previous for statement.
379 These would be dead in ISO-conforming code, but might
380 be referenced in ARM-era code. These are stored in a
381 TREE_LIST; the TREE_VALUE is the actual declaration. */
382 tree dead_vars_from_for;
384 /* 1 for the level that holds the parameters of a function.
385 2 for the level that holds a class declaration. */
386 unsigned parm_flag : 2;
388 /* 1 means make a BLOCK for this level regardless of all else.
389 2 for temporary binding contours created by the compiler. */
390 unsigned keep : 2;
392 /* Nonzero if this level "doesn't exist" for tags. */
393 unsigned tag_transparent : 1;
395 /* Nonzero if this level can safely have additional
396 cleanup-needing variables added to it. */
397 unsigned more_cleanups_ok : 1;
398 unsigned have_cleanups : 1;
400 /* Nonzero if this scope is for storing the decls for template
401 parameters and generic decls; these decls will be discarded and
402 replaced with a TEMPLATE_DECL. */
403 unsigned template_parms_p : 1;
405 /* Nonzero if this scope corresponds to the `<>' in a
406 `template <>' clause. Whenever this flag is set,
407 TEMPLATE_PARMS_P will be set as well. */
408 unsigned template_spec_p : 1;
410 /* This is set for a namespace binding level. */
411 unsigned namespace_p : 1;
413 /* True if this level is that of a for-statement where we need to
414 worry about ambiguous (ARM or ISO) scope rules. */
415 unsigned is_for_scope : 1;
417 /* True if this level corresponds to a TRY block. Currently this
418 information is only available while building the tree structure. */
419 unsigned is_try_scope : 1;
421 /* True if this level corresponds to a CATCH block. Currently this
422 information is only available while building the tree structure. */
423 unsigned is_catch_scope : 1;
425 /* Three bits left for this word. */
427 /* Binding depth at which this level began. */
428 unsigned binding_depth;
431 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
433 /* The binding level currently in effect. */
435 #define current_binding_level \
436 (*(cfun && cp_function_chain->bindings \
437 ? &cp_function_chain->bindings \
438 : &scope_chain->bindings))
440 /* The binding level of the current class, if any. */
442 #define class_binding_level scope_chain->class_bindings
444 /* A chain of binding_level structures awaiting reuse. */
446 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
448 /* The outermost binding level, for names of file scope.
449 This is created when the compiler is started and exists
450 through the entire run. */
452 static GTY(()) struct cp_binding_level *global_binding_level;
454 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
456 static int keep_next_level_flag;
458 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
459 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
460 time the VAR_DECL was declared, the type was incomplete. */
462 static GTY(()) tree incomplete_vars;
464 #if defined(DEBUG_BINDING_LEVELS)
465 static int binding_depth = 0;
466 static int is_class_level = 0;
468 static void
469 indent (void)
471 register unsigned i;
473 for (i = 0; i < binding_depth*2; i++)
474 putc (' ', stderr);
476 #endif /* defined(DEBUG_BINDING_LEVELS) */
478 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
480 static void
481 push_binding_level (struct cp_binding_level *newlevel,
482 int tag_transparent,
483 int keep)
485 /* Add this level to the front of the chain (stack) of levels that
486 are active. */
487 memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
488 newlevel->level_chain = current_binding_level;
489 current_binding_level = newlevel;
490 newlevel->tag_transparent = tag_transparent;
491 newlevel->more_cleanups_ok = 1;
493 newlevel->keep = keep;
494 #if defined(DEBUG_BINDING_LEVELS)
495 newlevel->binding_depth = binding_depth;
496 indent ();
497 fprintf (stderr, "push %s level 0x%08x line %d\n",
498 (is_class_level) ? "class" : "block", newlevel, lineno);
499 is_class_level = 0;
500 binding_depth++;
501 #endif /* defined(DEBUG_BINDING_LEVELS) */
504 /* Find the innermost enclosing class scope, and reset
505 CLASS_BINDING_LEVEL appropriately. */
507 static void
508 find_class_binding_level (void)
510 struct cp_binding_level *level = current_binding_level;
512 while (level && level->parm_flag != 2)
513 level = level->level_chain;
514 if (level && level->parm_flag == 2)
515 class_binding_level = level;
516 else
517 class_binding_level = 0;
520 static void
521 pop_binding_level (void)
523 if (global_binding_level)
525 /* Cannot pop a level, if there are none left to pop. */
526 if (current_binding_level == global_binding_level)
527 abort ();
529 /* Pop the current level, and free the structure for reuse. */
530 #if defined(DEBUG_BINDING_LEVELS)
531 binding_depth--;
532 indent ();
533 fprintf (stderr, "pop %s level 0x%08x line %d\n",
534 (is_class_level) ? "class" : "block",
535 current_binding_level, lineno);
536 if (is_class_level != (current_binding_level == class_binding_level))
538 indent ();
539 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
541 is_class_level = 0;
542 #endif /* defined(DEBUG_BINDING_LEVELS) */
544 register struct cp_binding_level *level = current_binding_level;
545 current_binding_level = current_binding_level->level_chain;
546 level->level_chain = free_binding_level;
547 #if 0 /* defined(DEBUG_BINDING_LEVELS) */
548 if (level->binding_depth != binding_depth)
549 abort ();
550 #endif /* defined(DEBUG_BINDING_LEVELS) */
551 free_binding_level = level;
552 find_class_binding_level ();
556 static void
557 suspend_binding_level (void)
559 if (class_binding_level)
560 current_binding_level = class_binding_level;
562 if (global_binding_level)
564 /* Cannot suspend a level, if there are none left to suspend. */
565 if (current_binding_level == global_binding_level)
566 abort ();
568 /* Suspend the current level. */
569 #if defined(DEBUG_BINDING_LEVELS)
570 binding_depth--;
571 indent ();
572 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
573 (is_class_level) ? "class" : "block",
574 current_binding_level, lineno);
575 if (is_class_level != (current_binding_level == class_binding_level))
577 indent ();
578 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
580 is_class_level = 0;
581 #endif /* defined(DEBUG_BINDING_LEVELS) */
582 current_binding_level = current_binding_level->level_chain;
583 find_class_binding_level ();
586 static void
587 resume_binding_level (struct cp_binding_level* b)
589 /* Resuming binding levels is meant only for namespaces,
590 and those cannot nest into classes. */
591 my_friendly_assert(!class_binding_level, 386);
592 /* Also, resuming a non-directly nested namespace is a no-no. */
593 my_friendly_assert(b->level_chain == current_binding_level, 386);
594 current_binding_level = b;
595 #if defined(DEBUG_BINDING_LEVELS)
596 b->binding_depth = binding_depth;
597 indent ();
598 fprintf (stderr, "resume %s level 0x%08x line %d\n",
599 (is_class_level) ? "class" : "block", b, lineno);
600 is_class_level = 0;
601 binding_depth++;
602 #endif /* defined(DEBUG_BINDING_LEVELS) */
605 /* Create a new `struct cp_binding_level'. */
607 static
608 struct cp_binding_level *
609 make_binding_level (void)
611 /* NOSTRICT */
612 return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
615 /* Nonzero if we are currently in the global binding level. */
618 global_bindings_p (void)
620 return current_binding_level == global_binding_level;
623 /* Return the innermost binding level that is not for a class scope. */
625 static struct cp_binding_level *
626 innermost_nonclass_level (void)
628 struct cp_binding_level *b;
630 b = current_binding_level;
631 while (b->parm_flag == 2)
632 b = b->level_chain;
634 return b;
637 /* Nonzero if we are currently in a toplevel binding level. This
638 means either the global binding level or a namespace in a toplevel
639 binding level. Since there are no non-toplevel namespace levels,
640 this really means any namespace or template parameter level. We
641 also include a class whose context is toplevel. */
644 toplevel_bindings_p (void)
646 struct cp_binding_level *b = innermost_nonclass_level ();
648 return b->namespace_p || b->template_parms_p;
651 /* Nonzero if this is a namespace scope, or if we are defining a class
652 which is itself at namespace scope, or whose enclosing class is
653 such a class, etc. */
656 namespace_bindings_p (void)
658 struct cp_binding_level *b = innermost_nonclass_level ();
660 return b->namespace_p;
663 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
664 unconditionally. Otherwise, use the normal logic to decide whether
665 or not to create a BLOCK. */
667 void
668 keep_next_level (int keep)
670 keep_next_level_flag = keep;
673 /* Nonzero if the current level needs to have a BLOCK made. */
676 kept_level_p (void)
678 return (current_binding_level->blocks != NULL_TREE
679 || current_binding_level->keep
680 || current_binding_level->names != NULL_TREE
681 || (current_binding_level->tags != NULL_TREE
682 && !current_binding_level->tag_transparent));
685 static void
686 declare_namespace_level (void)
688 current_binding_level->namespace_p = 1;
691 /* Returns nonzero if this scope was created to store template
692 parameters. */
695 template_parm_scope_p (void)
697 return current_binding_level->template_parms_p;
700 /* Returns the kind of template specialization we are currently
701 processing, given that it's declaration contained N_CLASS_SCOPES
702 explicit scope qualifications. */
704 tmpl_spec_kind
705 current_tmpl_spec_kind (int n_class_scopes)
707 int n_template_parm_scopes = 0;
708 int seen_specialization_p = 0;
709 int innermost_specialization_p = 0;
710 struct cp_binding_level *b;
712 /* Scan through the template parameter scopes. */
713 for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
715 /* If we see a specialization scope inside a parameter scope,
716 then something is wrong. That corresponds to a declaration
717 like:
719 template <class T> template <> ...
721 which is always invalid since [temp.expl.spec] forbids the
722 specialization of a class member template if the enclosing
723 class templates are not explicitly specialized as well. */
724 if (b->template_spec_p)
726 if (n_template_parm_scopes == 0)
727 innermost_specialization_p = 1;
728 else
729 seen_specialization_p = 1;
731 else if (seen_specialization_p == 1)
732 return tsk_invalid_member_spec;
734 ++n_template_parm_scopes;
737 /* Handle explicit instantiations. */
738 if (processing_explicit_instantiation)
740 if (n_template_parm_scopes != 0)
741 /* We've seen a template parameter list during an explicit
742 instantiation. For example:
744 template <class T> template void f(int);
746 This is erroneous. */
747 return tsk_invalid_expl_inst;
748 else
749 return tsk_expl_inst;
752 if (n_template_parm_scopes < n_class_scopes)
753 /* We've not seen enough template headers to match all the
754 specialized classes present. For example:
756 template <class T> void R<T>::S<T>::f(int);
758 This is invalid; there needs to be one set of template
759 parameters for each class. */
760 return tsk_insufficient_parms;
761 else if (n_template_parm_scopes == n_class_scopes)
762 /* We're processing a non-template declaration (even though it may
763 be a member of a template class.) For example:
765 template <class T> void S<T>::f(int);
767 The `class T' maches the `S<T>', leaving no template headers
768 corresponding to the `f'. */
769 return tsk_none;
770 else if (n_template_parm_scopes > n_class_scopes + 1)
771 /* We've got too many template headers. For example:
773 template <> template <class T> void f (T);
775 There need to be more enclosing classes. */
776 return tsk_excessive_parms;
777 else
778 /* This must be a template. It's of the form:
780 template <class T> template <class U> void S<T>::f(U);
782 This is a specialization if the innermost level was a
783 specialization; otherwise it's just a definition of the
784 template. */
785 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
788 void
789 set_class_shadows (tree shadows)
791 class_binding_level->class_shadowed = shadows;
794 /* Enter a new binding level.
795 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
796 not for that of tags. */
798 void
799 pushlevel (int tag_transparent)
801 struct cp_binding_level *newlevel;
803 if (cfun && !doing_semantic_analysis_p ())
804 return;
806 /* Reuse or create a struct for this binding level. */
807 #if defined(DEBUG_BINDING_LEVELS)
808 if (0)
809 #else /* !defined(DEBUG_BINDING_LEVELS) */
810 if (free_binding_level)
811 #endif /* !defined(DEBUG_BINDING_LEVELS) */
813 newlevel = free_binding_level;
814 free_binding_level = free_binding_level->level_chain;
816 else
817 newlevel = make_binding_level ();
819 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
820 keep_next_level_flag = 0;
823 /* We're defining an object of type TYPE. If it needs a cleanup, but
824 we're not allowed to add any more objects with cleanups to the current
825 scope, create a new binding level. */
827 void
828 maybe_push_cleanup_level (tree type)
830 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
831 && current_binding_level->more_cleanups_ok == 0)
833 keep_next_level (2);
834 pushlevel (1);
835 clear_last_expr ();
836 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
840 /* Enter a new scope. The KIND indicates what kind of scope is being
841 created. */
843 void
844 begin_scope (scope_kind sk)
846 pushlevel (0);
848 switch (sk)
850 case sk_block:
851 break;
853 case sk_try:
854 current_binding_level->is_try_scope = 1;
855 break;
857 case sk_catch:
858 current_binding_level->is_catch_scope = 1;
859 break;
861 case sk_for:
862 current_binding_level->is_for_scope = 1;
863 break;
865 case sk_template_spec:
866 current_binding_level->template_spec_p = 1;
867 /* Fall through. */
869 case sk_template_parms:
870 current_binding_level->template_parms_p = 1;
871 break;
873 default:
874 abort ();
878 /* Exit the current scope. */
880 void
881 finish_scope (void)
883 poplevel (0, 0, 0);
886 /* Make DECL the innermost binding for ID. The LEVEL is the binding
887 level at which this declaration is being bound. */
889 static void
890 push_binding (tree id, tree decl, cxx_scope* level)
892 cxx_binding *binding = cxx_binding_make (decl, NULL);
894 /* Now, fill in the binding information. */
895 binding->previous = IDENTIFIER_BINDING (id);
896 BINDING_SCOPE (binding) = level;
897 INHERITED_VALUE_BINDING_P (binding) = 0;
898 LOCAL_BINDING_P (binding) = (level != class_binding_level);
900 /* And put it on the front of the list of bindings for ID. */
901 IDENTIFIER_BINDING (id) = binding;
904 /* ID is already bound in the current scope. But, DECL is an
905 additional binding for ID in the same scope. This is the `struct
906 stat' hack whereby a non-typedef class-name or enum-name can be
907 bound at the same level as some other kind of entity. It's the
908 responsibility of the caller to check that inserting this name is
909 valid here. Returns nonzero if the new binding was successful. */
910 static int
911 add_binding (tree id, tree decl)
913 cxx_binding *binding = IDENTIFIER_BINDING (id);
914 int ok = 1;
916 timevar_push (TV_NAME_LOOKUP);
917 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
918 /* The new name is the type name. */
919 BINDING_TYPE (binding) = decl;
920 else if (!BINDING_VALUE (binding))
921 /* This situation arises when push_class_level_binding moves an
922 inherited type-binding out of the way to make room for a new
923 value binding. */
924 BINDING_VALUE (binding) = decl;
925 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
926 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
928 /* The old binding was a type name. It was placed in
929 BINDING_VALUE because it was thought, at the point it was
930 declared, to be the only entity with such a name. Move the
931 type name into the type slot; it is now hidden by the new
932 binding. */
933 BINDING_TYPE (binding) = BINDING_VALUE (binding);
934 BINDING_VALUE (binding) = decl;
935 INHERITED_VALUE_BINDING_P (binding) = 0;
937 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
938 && TREE_CODE (decl) == TYPE_DECL
939 && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
940 && (same_type_p (TREE_TYPE (decl),
941 TREE_TYPE (BINDING_VALUE (binding)))
942 /* If either type involves template parameters, we must
943 wait until instantiation. */
944 || uses_template_parms (TREE_TYPE (decl))
945 || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
946 /* We have two typedef-names, both naming the same type to have
947 the same name. This is OK because of:
949 [dcl.typedef]
951 In a given scope, a typedef specifier can be used to redefine
952 the name of any type declared in that scope to refer to the
953 type to which it already refers. */
954 ok = 0;
955 /* There can be two block-scope declarations of the same variable,
956 so long as they are `extern' declarations. */
957 else if (TREE_CODE (decl) == VAR_DECL
958 && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
959 && DECL_EXTERNAL (decl)
960 && DECL_EXTERNAL (BINDING_VALUE (binding)))
962 duplicate_decls (decl, BINDING_VALUE (binding));
963 ok = 0;
965 else
967 error ("declaration of `%#D'", decl);
968 cp_error_at ("conflicts with previous declaration `%#D'",
969 BINDING_VALUE (binding));
970 ok = 0;
973 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
976 /* Add DECL to the list of things declared in B. */
978 static void
979 add_decl_to_level (tree decl,
980 struct cp_binding_level* b)
982 if (TREE_CODE (decl) == NAMESPACE_DECL
983 && !DECL_NAMESPACE_ALIAS (decl))
985 TREE_CHAIN (decl) = b->namespaces;
986 b->namespaces = decl;
988 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
990 TREE_CHAIN (decl) = b->vtables;
991 b->vtables = decl;
993 else
995 /* We build up the list in reverse order, and reverse it later if
996 necessary. */
997 TREE_CHAIN (decl) = b->names;
998 b->names = decl;
999 b->names_size++;
1001 /* If appropriate, add decl to separate list of statics */
1002 if (b->namespace_p)
1003 if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
1004 || (TREE_CODE (decl) == FUNCTION_DECL
1005 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
1006 VARRAY_PUSH_TREE (b->static_decls, decl);
1010 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1011 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1012 doesn't really belong to this binding level, that it got here
1013 through a using-declaration. */
1015 void
1016 push_local_binding (tree id, tree decl, int flags)
1018 struct cp_binding_level *b;
1020 /* Skip over any local classes. This makes sense if we call
1021 push_local_binding with a friend decl of a local class. */
1022 b = current_binding_level;
1023 while (b->parm_flag == 2)
1024 b = b->level_chain;
1026 if (lookup_name_current_level (id))
1028 /* Supplement the existing binding. */
1029 if (!add_binding (id, decl))
1030 /* It didn't work. Something else must be bound at this
1031 level. Do not add DECL to the list of things to pop
1032 later. */
1033 return;
1035 else
1036 /* Create a new binding. */
1037 push_binding (id, decl, b);
1039 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1040 /* We must put the OVERLOAD into a TREE_LIST since the
1041 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1042 decls that got here through a using-declaration. */
1043 decl = build_tree_list (NULL_TREE, decl);
1045 /* And put DECL on the list of things declared by the current
1046 binding level. */
1047 add_decl_to_level (decl, b);
1050 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1051 binding was successful. */
1054 push_class_binding (tree id, tree decl)
1056 int result = 1;
1057 cxx_binding *binding = IDENTIFIER_BINDING (id);
1058 tree context;
1060 timevar_push (TV_NAME_LOOKUP);
1061 /* Note that we declared this value so that we can issue an error if
1062 this is an invalid redeclaration of a name already used for some
1063 other purpose. */
1064 note_name_declared_in_class (id, decl);
1066 if (binding && BINDING_SCOPE (binding) == class_binding_level)
1067 /* Supplement the existing binding. */
1068 result = add_binding (id, decl);
1069 else
1070 /* Create a new binding. */
1071 push_binding (id, decl, class_binding_level);
1073 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1074 class-level declaration. Note that we do not use DECL here
1075 because of the possibility of the `struct stat' hack; if DECL is
1076 a class-name or enum-name we might prefer a field-name, or some
1077 such. */
1078 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1080 /* If this is a binding from a base class, mark it as such. */
1081 binding = IDENTIFIER_BINDING (id);
1082 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1084 if (TREE_CODE (decl) == OVERLOAD)
1085 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1086 else
1088 my_friendly_assert (DECL_P (decl), 0);
1089 context = context_for_name_lookup (decl);
1092 if (is_properly_derived_from (current_class_type, context))
1093 INHERITED_VALUE_BINDING_P (binding) = 1;
1094 else
1095 INHERITED_VALUE_BINDING_P (binding) = 0;
1097 else if (BINDING_VALUE (binding) == decl)
1098 /* We only encounter a TREE_LIST when push_class_decls detects an
1099 ambiguity. Such an ambiguity can be overridden by a definition
1100 in this class. */
1101 INHERITED_VALUE_BINDING_P (binding) = 1;
1103 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1106 /* Remove the binding for DECL which should be the innermost binding
1107 for ID. */
1109 static void
1110 pop_binding (tree id, tree decl)
1112 cxx_binding *binding;
1114 if (id == NULL_TREE)
1115 /* It's easiest to write the loops that call this function without
1116 checking whether or not the entities involved have names. We
1117 get here for such an entity. */
1118 return;
1120 /* Get the innermost binding for ID. */
1121 binding = IDENTIFIER_BINDING (id);
1123 /* The name should be bound. */
1124 my_friendly_assert (binding != NULL, 0);
1126 /* The DECL will be either the ordinary binding or the type
1127 binding for this identifier. Remove that binding. */
1128 if (BINDING_VALUE (binding) == decl)
1129 BINDING_VALUE (binding) = NULL_TREE;
1130 else if (BINDING_TYPE (binding) == decl)
1131 BINDING_TYPE (binding) = NULL_TREE;
1132 else
1133 abort ();
1135 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1137 /* We're completely done with the innermost binding for this
1138 identifier. Unhook it from the list of bindings. */
1139 IDENTIFIER_BINDING (id) = binding->previous;
1141 /* Add it to the free list. */
1142 cxx_binding_free (binding);
1144 /* Clear the BINDING_SCOPE so the garbage collector doesn't walk
1145 it. */
1146 BINDING_SCOPE (binding) = NULL;
1150 /* When a label goes out of scope, check to see if that label was used
1151 in a valid manner, and issue any appropriate warnings or errors. */
1153 static void
1154 pop_label (tree label, tree old_value)
1156 if (!processing_template_decl && doing_semantic_analysis_p ())
1158 if (DECL_INITIAL (label) == NULL_TREE)
1160 cp_error_at ("label `%D' used but not defined", label);
1161 /* Avoid crashing later. */
1162 define_label (input_filename, 1, DECL_NAME (label));
1164 else if (warn_unused_label && !TREE_USED (label))
1165 cp_warning_at ("label `%D' defined but not used", label);
1168 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1171 /* At the end of a function, all labels declared within the function
1172 go out of scope. BLOCK is the top-level block for the
1173 function. */
1175 static void
1176 pop_labels (tree block)
1178 struct named_label_list *link;
1180 /* Clear out the definitions of all label names, since their scopes
1181 end here. */
1182 for (link = named_labels; link; link = link->next)
1184 pop_label (link->label_decl, link->old_value);
1185 /* Put the labels into the "variables" of the top-level block,
1186 so debugger can see them. */
1187 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1188 BLOCK_VARS (block) = link->label_decl;
1191 named_labels = NULL;
1194 /* Exit a binding level.
1195 Pop the level off, and restore the state of the identifier-decl mappings
1196 that were in effect when this level was entered.
1198 If KEEP == 1, this level had explicit declarations, so
1199 and create a "block" (a BLOCK node) for the level
1200 to record its declarations and subblocks for symbol table output.
1202 If FUNCTIONBODY is nonzero, this level is the body of a function,
1203 so create a block as if KEEP were set and also clear out all
1204 label names.
1206 If REVERSE is nonzero, reverse the order of decls before putting
1207 them into the BLOCK. */
1209 tree
1210 poplevel (int keep, int reverse, int functionbody)
1212 register tree link;
1213 /* The chain of decls was accumulated in reverse order.
1214 Put it into forward order, just for cleanliness. */
1215 tree decls;
1216 int tmp = functionbody;
1217 int real_functionbody;
1218 tree tags;
1219 tree subblocks;
1220 tree block = NULL_TREE;
1221 tree decl;
1222 int leaving_for_scope;
1224 timevar_push (TV_NAME_LOOKUP);
1225 if (cfun && !doing_semantic_analysis_p ())
1226 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1228 my_friendly_assert (current_binding_level->parm_flag != 2,
1229 19990916);
1231 real_functionbody = (current_binding_level->keep == 2
1232 ? ((functionbody = 0), tmp) : functionbody);
1233 tags = functionbody >= 0 ? current_binding_level->tags : 0;
1234 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1236 my_friendly_assert (!current_binding_level->class_shadowed,
1237 19990414);
1239 /* We used to use KEEP == 2 to indicate that the new block should go
1240 at the beginning of the list of blocks at this binding level,
1241 rather than the end. This hack is no longer used. */
1242 my_friendly_assert (keep == 0 || keep == 1, 0);
1244 if (current_binding_level->keep == 1)
1245 keep = 1;
1247 /* Any uses of undefined labels, and any defined labels, now operate
1248 under constraints of next binding contour. */
1249 if (cfun && !functionbody)
1251 struct cp_binding_level *level_chain;
1252 level_chain = current_binding_level->level_chain;
1253 if (level_chain)
1255 struct named_label_use_list *uses;
1256 struct named_label_list *labels;
1257 for (labels = named_labels; labels; labels = labels->next)
1258 if (labels->binding_level == current_binding_level)
1260 tree decl;
1261 if (current_binding_level->is_try_scope)
1262 labels->in_try_scope = 1;
1263 if (current_binding_level->is_catch_scope)
1264 labels->in_catch_scope = 1;
1265 for (decl = labels->names_in_scope; decl;
1266 decl = TREE_CHAIN (decl))
1267 if (decl_jump_unsafe (decl))
1268 labels->bad_decls = tree_cons (NULL_TREE, decl,
1269 labels->bad_decls);
1270 labels->binding_level = level_chain;
1271 labels->names_in_scope = level_chain->names;
1274 for (uses = named_label_uses; uses; uses = uses->next)
1275 if (uses->binding_level == current_binding_level)
1277 uses->binding_level = level_chain;
1278 uses->names_in_scope = level_chain->names;
1283 /* Get the decls in the order they were written.
1284 Usually current_binding_level->names is in reverse order.
1285 But parameter decls were previously put in forward order. */
1287 if (reverse)
1288 current_binding_level->names
1289 = decls = nreverse (current_binding_level->names);
1290 else
1291 decls = current_binding_level->names;
1293 /* Output any nested inline functions within this block
1294 if they weren't already output. */
1295 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1296 if (TREE_CODE (decl) == FUNCTION_DECL
1297 && ! TREE_ASM_WRITTEN (decl)
1298 && DECL_INITIAL (decl) != NULL_TREE
1299 && TREE_ADDRESSABLE (decl)
1300 && decl_function_context (decl) == current_function_decl)
1302 /* If this decl was copied from a file-scope decl
1303 on account of a block-scope extern decl,
1304 propagate TREE_ADDRESSABLE to the file-scope decl. */
1305 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1306 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1307 else
1309 push_function_context ();
1310 output_inline_function (decl);
1311 pop_function_context ();
1315 /* When not in function-at-a-time mode, expand_end_bindings will
1316 warn about unused variables. But, in function-at-a-time mode
1317 expand_end_bindings is not passed the list of variables in the
1318 current scope, and therefore no warning is emitted. So, we
1319 explicitly warn here. */
1320 if (!processing_template_decl)
1321 warn_about_unused_variables (getdecls ());
1323 /* If there were any declarations or structure tags in that level,
1324 or if this level is a function body,
1325 create a BLOCK to record them for the life of this function. */
1326 block = NULL_TREE;
1327 if (keep == 1 || functionbody)
1328 block = make_node (BLOCK);
1329 if (block != NULL_TREE)
1331 BLOCK_VARS (block) = decls;
1332 BLOCK_SUBBLOCKS (block) = subblocks;
1335 /* In each subblock, record that this is its superior. */
1336 if (keep >= 0)
1337 for (link = subblocks; link; link = TREE_CHAIN (link))
1338 BLOCK_SUPERCONTEXT (link) = block;
1340 /* We still support the old for-scope rules, whereby the variables
1341 in a for-init statement were in scope after the for-statement
1342 ended. We only use the new rules if flag_new_for_scope is
1343 nonzero. */
1344 leaving_for_scope
1345 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1347 /* Remove declarations for all the DECLs in this level. */
1348 for (link = decls; link; link = TREE_CHAIN (link))
1350 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1351 && DECL_NAME (link))
1353 cxx_binding *outer_binding
1354 = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
1355 tree ns_binding;
1357 if (!outer_binding)
1358 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1359 else
1360 ns_binding = NULL_TREE;
1362 if (outer_binding
1363 && (BINDING_SCOPE (outer_binding)
1364 == current_binding_level->level_chain))
1365 /* We have something like:
1367 int i;
1368 for (int i; ;);
1370 and we are leaving the `for' scope. There's no reason to
1371 keep the binding of the inner `i' in this case. */
1372 pop_binding (DECL_NAME (link), link);
1373 else if ((outer_binding
1374 && (TREE_CODE (BINDING_VALUE (outer_binding))
1375 == TYPE_DECL))
1376 || (ns_binding
1377 && TREE_CODE (ns_binding) == TYPE_DECL))
1378 /* Here, we have something like:
1380 typedef int I;
1382 void f () {
1383 for (int I; ;);
1386 We must pop the for-scope binding so we know what's a
1387 type and what isn't. */
1388 pop_binding (DECL_NAME (link), link);
1389 else
1391 /* Mark this VAR_DECL as dead so that we can tell we left it
1392 there only for backward compatibility. */
1393 DECL_DEAD_FOR_LOCAL (link) = 1;
1395 /* Keep track of what should of have happenned when we
1396 popped the binding. */
1397 if (outer_binding && BINDING_VALUE (outer_binding))
1398 DECL_SHADOWED_FOR_VAR (link)
1399 = BINDING_VALUE (outer_binding);
1401 /* Add it to the list of dead variables in the next
1402 outermost binding to that we can remove these when we
1403 leave that binding. */
1404 current_binding_level->level_chain->dead_vars_from_for
1405 = tree_cons (NULL_TREE, link,
1406 current_binding_level->level_chain->
1407 dead_vars_from_for);
1409 /* Although we don't pop the cxx_binding, we do clear
1410 its BINDING_SCOPE since the level is going away now. */
1411 BINDING_SCOPE (IDENTIFIER_BINDING (DECL_NAME (link))) = 0;
1414 else
1416 /* Remove the binding. */
1417 decl = link;
1418 if (TREE_CODE (decl) == TREE_LIST)
1419 decl = TREE_VALUE (decl);
1420 if (DECL_P (decl))
1421 pop_binding (DECL_NAME (decl), decl);
1422 else if (TREE_CODE (decl) == OVERLOAD)
1423 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1424 else
1425 abort ();
1429 /* Remove declarations for any `for' variables from inner scopes
1430 that we kept around. */
1431 for (link = current_binding_level->dead_vars_from_for;
1432 link; link = TREE_CHAIN (link))
1433 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1435 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1436 for (link = current_binding_level->type_shadowed;
1437 link; link = TREE_CHAIN (link))
1438 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1440 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1441 for (link = current_binding_level->shadowed_labels;
1442 link;
1443 link = TREE_CHAIN (link))
1444 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1446 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1447 list if a `using' declaration put them there. The debugging
1448 back-ends won't understand OVERLOAD, so we remove them here.
1449 Because the BLOCK_VARS are (temporarily) shared with
1450 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1451 popped all the bindings. */
1452 if (block)
1454 tree* d;
1456 for (d = &BLOCK_VARS (block); *d; )
1458 if (TREE_CODE (*d) == TREE_LIST)
1459 *d = TREE_CHAIN (*d);
1460 else
1461 d = &TREE_CHAIN (*d);
1465 /* If the level being exited is the top level of a function,
1466 check over all the labels. */
1467 if (functionbody)
1469 /* Since this is the top level block of a function, the vars are
1470 the function's parameters. Don't leave them in the BLOCK
1471 because they are found in the FUNCTION_DECL instead. */
1472 BLOCK_VARS (block) = 0;
1473 pop_labels (block);
1476 tmp = current_binding_level->keep;
1478 pop_binding_level ();
1479 if (functionbody)
1480 DECL_INITIAL (current_function_decl) = block;
1481 else if (block)
1482 current_binding_level->blocks
1483 = chainon (current_binding_level->blocks, block);
1485 /* If we did not make a block for the level just exited,
1486 any blocks made for inner levels
1487 (since they cannot be recorded as subblocks in that level)
1488 must be carried forward so they will later become subblocks
1489 of something else. */
1490 else if (subblocks)
1491 current_binding_level->blocks
1492 = chainon (current_binding_level->blocks, subblocks);
1494 /* Each and every BLOCK node created here in `poplevel' is important
1495 (e.g. for proper debugging information) so if we created one
1496 earlier, mark it as "used". */
1497 if (block)
1498 TREE_USED (block) = 1;
1500 /* Take care of compiler's internal binding structures. */
1501 if (tmp == 2)
1503 tree scope_stmts;
1505 scope_stmts
1506 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1507 if (block)
1509 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1510 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1513 block = poplevel (keep, reverse, functionbody);
1516 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1519 /* Delete the node BLOCK from the current binding level.
1520 This is used for the block inside a stmt expr ({...})
1521 so that the block can be reinserted where appropriate. */
1523 void
1524 delete_block (tree block)
1526 tree t;
1527 if (current_binding_level->blocks == block)
1528 current_binding_level->blocks = TREE_CHAIN (block);
1529 for (t = current_binding_level->blocks; t;)
1531 if (TREE_CHAIN (t) == block)
1532 TREE_CHAIN (t) = TREE_CHAIN (block);
1533 else
1534 t = TREE_CHAIN (t);
1536 TREE_CHAIN (block) = NULL_TREE;
1537 /* Clear TREE_USED which is always set by poplevel.
1538 The flag is set again if insert_block is called. */
1539 TREE_USED (block) = 0;
1542 /* Insert BLOCK at the end of the list of subblocks of the
1543 current binding level. This is used when a BIND_EXPR is expanded,
1544 to handle the BLOCK node inside the BIND_EXPR. */
1546 void
1547 insert_block (tree block)
1549 TREE_USED (block) = 1;
1550 current_binding_level->blocks
1551 = chainon (current_binding_level->blocks, block);
1554 /* Set the BLOCK node for the innermost scope
1555 (the one we are currently in). */
1557 void
1558 set_block (tree block ATTRIBUTE_UNUSED )
1560 /* The RTL expansion machinery requires us to provide this callback,
1561 but it is not applicable in function-at-a-time mode. */
1562 my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1565 /* Do a pushlevel for class declarations. */
1567 void
1568 pushlevel_class (void)
1570 register struct cp_binding_level *newlevel;
1572 /* Reuse or create a struct for this binding level. */
1573 #if defined(DEBUG_BINDING_LEVELS)
1574 if (0)
1575 #else /* !defined(DEBUG_BINDING_LEVELS) */
1576 if (free_binding_level)
1577 #endif /* !defined(DEBUG_BINDING_LEVELS) */
1579 newlevel = free_binding_level;
1580 free_binding_level = free_binding_level->level_chain;
1582 else
1583 newlevel = make_binding_level ();
1585 #if defined(DEBUG_BINDING_LEVELS)
1586 is_class_level = 1;
1587 #endif /* defined(DEBUG_BINDING_LEVELS) */
1589 push_binding_level (newlevel, 0, 0);
1591 class_binding_level = current_binding_level;
1592 class_binding_level->parm_flag = 2;
1593 class_binding_level->this_class = current_class_type;
1596 /* ...and a poplevel for class declarations. */
1598 void
1599 poplevel_class (void)
1601 register struct cp_binding_level *level = class_binding_level;
1602 tree shadowed;
1604 timevar_push (TV_NAME_LOOKUP);
1605 my_friendly_assert (level != 0, 354);
1607 /* If we're leaving a toplevel class, don't bother to do the setting
1608 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1609 shouldn't even be used when current_class_type isn't set, and second,
1610 if we don't touch it here, we're able to use the cache effect if the
1611 next time we're entering a class scope, it is the same class. */
1612 if (current_class_depth != 1)
1614 struct cp_binding_level* b;
1616 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1617 for (shadowed = level->class_shadowed;
1618 shadowed;
1619 shadowed = TREE_CHAIN (shadowed))
1620 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1622 /* Find the next enclosing class, and recreate
1623 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1624 b = level->level_chain;
1625 while (b && b->parm_flag != 2)
1626 b = b->level_chain;
1628 if (b)
1629 for (shadowed = b->class_shadowed;
1630 shadowed;
1631 shadowed = TREE_CHAIN (shadowed))
1633 cxx_binding *binding;
1635 binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1636 while (binding && BINDING_SCOPE (binding) != b)
1637 binding = binding->previous;
1639 if (binding)
1640 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1641 = BINDING_VALUE (binding);
1644 else
1645 /* Remember to save what IDENTIFIER's were bound in this scope so we
1646 can recover from cache misses. */
1648 previous_class_type = current_class_type;
1649 previous_class_values = class_binding_level->class_shadowed;
1651 for (shadowed = level->type_shadowed;
1652 shadowed;
1653 shadowed = TREE_CHAIN (shadowed))
1654 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1656 /* Remove the bindings for all of the class-level declarations. */
1657 for (shadowed = level->class_shadowed;
1658 shadowed;
1659 shadowed = TREE_CHAIN (shadowed))
1660 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1662 /* Now, pop out of the binding level which we created up in the
1663 `pushlevel_class' routine. */
1664 #if defined(DEBUG_BINDING_LEVELS)
1665 is_class_level = 1;
1666 #endif /* defined(DEBUG_BINDING_LEVELS) */
1668 pop_binding_level ();
1669 timevar_pop (TV_NAME_LOOKUP);
1672 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1673 for any names in enclosing classes. */
1675 void
1676 clear_identifier_class_values (void)
1678 tree t;
1680 if (!class_binding_level)
1681 return;
1683 for (t = class_binding_level->class_shadowed;
1685 t = TREE_CHAIN (t))
1686 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1689 /* Returns nonzero if T is a virtual function table. */
1692 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1694 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1697 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1698 functions. */
1701 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1703 return (TREE_CODE (t) == TYPE_DECL
1704 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1705 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1708 /* Return the declarations that are members of the namespace NS. */
1710 tree
1711 cp_namespace_decls (tree ns)
1713 return NAMESPACE_LEVEL (ns)->names;
1716 struct walk_globals_data {
1717 walk_globals_pred p;
1718 walk_globals_fn f;
1719 void *data;
1722 /* Walk the vtable declarations in NAMESPACE. Whenever one is found
1723 for which P returns nonzero, call F with its address. If any call
1724 to F returns a nonzero value, return a nonzero value. */
1726 static int
1727 walk_vtables_r (tree namespace, void* data)
1729 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1730 walk_globals_fn f = wgd->f;
1731 void *d = wgd->data;
1732 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1733 int result = 0;
1735 for (; decl ; decl = TREE_CHAIN (decl))
1736 result |= (*f) (&decl, d);
1738 return result;
1741 /* Walk the vtable declarations. Whenever one is found for which P
1742 returns nonzero, call F with its address. If any call to F
1743 returns a nonzero value, return a nonzero value. */
1744 bool
1745 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1747 struct walk_globals_data wgd;
1748 wgd.p = p;
1749 wgd.f = f;
1750 wgd.data = data;
1752 return walk_namespaces (walk_vtables_r, &wgd);
1755 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1756 itself, calling F for each. The DATA is passed to F as well. */
1758 static int
1759 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1761 int result = 0;
1762 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
1764 result |= (*f) (namespace, data);
1766 for (; current; current = TREE_CHAIN (current))
1767 result |= walk_namespaces_r (current, f, data);
1769 return result;
1772 /* Walk all the namespaces, calling F for each. The DATA is passed to
1773 F as well. */
1776 walk_namespaces (walk_namespaces_fn f, void* data)
1778 return walk_namespaces_r (global_namespace, f, data);
1781 /* Walk the global declarations in NAMESPACE. Whenever one is found
1782 for which P returns nonzero, call F with its address. If any call
1783 to F returns a nonzero value, return a nonzero value. */
1785 static int
1786 walk_globals_r (tree namespace, void* data)
1788 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1789 walk_globals_pred p = wgd->p;
1790 walk_globals_fn f = wgd->f;
1791 void *d = wgd->data;
1792 tree *t;
1793 int result = 0;
1795 t = &NAMESPACE_LEVEL (namespace)->names;
1797 while (*t)
1799 tree glbl = *t;
1801 if ((*p) (glbl, d))
1802 result |= (*f) (t, d);
1804 /* If F changed *T, then *T still points at the next item to
1805 examine. */
1806 if (*t == glbl)
1807 t = &TREE_CHAIN (*t);
1810 return result;
1813 /* Walk the global declarations. Whenever one is found for which P
1814 returns true, call F with its address. If any call to F
1815 returns true, return true. */
1817 bool
1818 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1820 struct walk_globals_data wgd;
1821 wgd.p = p;
1822 wgd.f = f;
1823 wgd.data = data;
1825 return walk_namespaces (walk_globals_r, &wgd);
1828 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1829 DATA is non-NULL, this is the last time we will call
1830 wrapup_global_declarations for this NAMESPACE. */
1833 wrapup_globals_for_namespace (tree namespace, void* data)
1835 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
1836 varray_type statics = level->static_decls;
1837 tree *vec = &VARRAY_TREE (statics, 0);
1838 int len = VARRAY_ACTIVE_SIZE (statics);
1839 int last_time = (data != 0);
1841 if (last_time)
1843 check_global_declarations (vec, len);
1844 return 0;
1847 /* Write out any globals that need to be output. */
1848 return wrapup_global_declarations (vec, len);
1852 /* For debugging. */
1853 static int no_print_functions = 0;
1854 static int no_print_builtins = 0;
1856 void
1857 print_binding_level (struct cp_binding_level* lvl)
1859 tree t;
1860 int i = 0, len;
1861 fprintf (stderr, " blocks=");
1862 fprintf (stderr, HOST_PTR_PRINTF, (void *) lvl->blocks);
1863 if (lvl->tag_transparent)
1864 fprintf (stderr, " tag-transparent");
1865 if (lvl->more_cleanups_ok)
1866 fprintf (stderr, " more-cleanups-ok");
1867 if (lvl->have_cleanups)
1868 fprintf (stderr, " have-cleanups");
1869 fprintf (stderr, "\n");
1870 if (lvl->names)
1872 fprintf (stderr, " names:\t");
1873 /* We can probably fit 3 names to a line? */
1874 for (t = lvl->names; t; t = TREE_CHAIN (t))
1876 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1877 continue;
1878 if (no_print_builtins
1879 && (TREE_CODE (t) == TYPE_DECL)
1880 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1881 continue;
1883 /* Function decls tend to have longer names. */
1884 if (TREE_CODE (t) == FUNCTION_DECL)
1885 len = 3;
1886 else
1887 len = 2;
1888 i += len;
1889 if (i > 6)
1891 fprintf (stderr, "\n\t");
1892 i = len;
1894 print_node_brief (stderr, "", t, 0);
1895 if (t == error_mark_node)
1896 break;
1898 if (i)
1899 fprintf (stderr, "\n");
1901 if (lvl->tags)
1903 fprintf (stderr, " tags:\t");
1904 i = 0;
1905 for (t = lvl->tags; t; t = TREE_CHAIN (t))
1907 if (TREE_PURPOSE (t) == NULL_TREE)
1908 len = 3;
1909 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1910 len = 2;
1911 else
1912 len = 4;
1913 i += len;
1914 if (i > 5)
1916 fprintf (stderr, "\n\t");
1917 i = len;
1919 if (TREE_PURPOSE (t) == NULL_TREE)
1921 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1922 fprintf (stderr, ">");
1924 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1925 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1926 else
1928 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1929 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1930 fprintf (stderr, ">");
1933 if (i)
1934 fprintf (stderr, "\n");
1936 if (lvl->class_shadowed)
1938 fprintf (stderr, " class-shadowed:");
1939 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1941 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1943 fprintf (stderr, "\n");
1945 if (lvl->type_shadowed)
1947 fprintf (stderr, " type-shadowed:");
1948 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1950 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1952 fprintf (stderr, "\n");
1956 void
1957 print_other_binding_stack (struct cp_binding_level *stack)
1959 struct cp_binding_level *level;
1960 for (level = stack; level != global_binding_level; level = level->level_chain)
1962 fprintf (stderr, "binding level ");
1963 fprintf (stderr, HOST_PTR_PRINTF, (void *) level);
1964 fprintf (stderr, "\n");
1965 print_binding_level (level);
1969 void
1970 print_binding_stack (void)
1972 struct cp_binding_level *b;
1973 fprintf (stderr, "current_binding_level=");
1974 fprintf (stderr, HOST_PTR_PRINTF, (void *) current_binding_level);
1975 fprintf (stderr, "\nclass_binding_level=");
1976 fprintf (stderr, HOST_PTR_PRINTF, (void *) class_binding_level);
1977 fprintf (stderr, "\nglobal_binding_level=");
1978 fprintf (stderr, HOST_PTR_PRINTF, (void *) global_binding_level);
1979 fprintf (stderr, "\n");
1980 if (class_binding_level)
1982 for (b = class_binding_level; b; b = b->level_chain)
1983 if (b == current_binding_level)
1984 break;
1985 if (b)
1986 b = class_binding_level;
1987 else
1988 b = current_binding_level;
1990 else
1991 b = current_binding_level;
1992 print_other_binding_stack (b);
1993 fprintf (stderr, "global:\n");
1994 print_binding_level (global_binding_level);
1997 /* Namespace binding access routines: The namespace_bindings field of
1998 the identifier is polymorphic, with three possible values:
1999 NULL_TREE, a list of "cxx_binding"s. */
2001 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2002 select a name that is unique to this compilation unit. */
2004 void
2005 push_namespace (tree name)
2007 tree d = NULL_TREE;
2008 int need_new = 1;
2009 int implicit_use = 0;
2010 int global = 0;
2012 timevar_push (TV_NAME_LOOKUP);
2014 if (!global_namespace)
2016 /* This must be ::. */
2017 my_friendly_assert (name == get_identifier ("::"), 377);
2018 global = 1;
2020 else if (!name)
2022 /* The name of anonymous namespace is unique for the translation
2023 unit. */
2024 if (!anonymous_namespace_name)
2025 anonymous_namespace_name = get_file_function_name ('N');
2026 name = anonymous_namespace_name;
2027 d = IDENTIFIER_NAMESPACE_VALUE (name);
2028 if (d)
2029 /* Reopening anonymous namespace. */
2030 need_new = 0;
2031 implicit_use = 1;
2033 else
2035 /* Check whether this is an extended namespace definition. */
2036 d = IDENTIFIER_NAMESPACE_VALUE (name);
2037 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2039 need_new = 0;
2040 if (DECL_NAMESPACE_ALIAS (d))
2042 error ("namespace alias `%D' not allowed here, assuming `%D'",
2043 d, DECL_NAMESPACE_ALIAS (d));
2044 d = DECL_NAMESPACE_ALIAS (d);
2049 if (need_new)
2051 /* Make a new namespace, binding the name to it. */
2052 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2053 /* The global namespace is not pushed, and the global binding
2054 level is set elsewhere. */
2055 if (!global)
2057 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2058 d = pushdecl (d);
2059 pushlevel (0);
2060 declare_namespace_level ();
2061 NAMESPACE_LEVEL (d) = current_binding_level;
2062 VARRAY_TREE_INIT (current_binding_level->static_decls,
2063 name != std_identifier ? 10 : 200,
2064 "Static declarations");
2067 else
2068 resume_binding_level (NAMESPACE_LEVEL (d));
2070 if (implicit_use)
2071 do_using_directive (d);
2072 /* Enter the name space. */
2073 current_namespace = d;
2075 timevar_pop (TV_NAME_LOOKUP);
2078 /* Pop from the scope of the current namespace. */
2080 void
2081 pop_namespace (void)
2083 my_friendly_assert (current_namespace != global_namespace, 20010801);
2084 current_namespace = CP_DECL_CONTEXT (current_namespace);
2085 /* The binding level is not popped, as it might be re-opened later. */
2086 suspend_binding_level ();
2089 /* Push into the scope of the namespace NS, even if it is deeply
2090 nested within another namespace. */
2092 void
2093 push_nested_namespace (tree ns)
2095 if (ns == global_namespace)
2096 push_to_top_level ();
2097 else
2099 push_nested_namespace (CP_DECL_CONTEXT (ns));
2100 push_namespace (DECL_NAME (ns));
2104 /* Pop back from the scope of the namespace NS, which was previously
2105 entered with push_nested_namespace. */
2107 void
2108 pop_nested_namespace (tree ns)
2110 timevar_push (TV_NAME_LOOKUP);
2111 while (ns != global_namespace)
2113 pop_namespace ();
2114 ns = CP_DECL_CONTEXT (ns);
2117 pop_from_top_level ();
2118 timevar_pop (TV_NAME_LOOKUP);
2122 /* Allocate storage for saving a C++ binding. */
2123 #define cxx_saved_binding_make() \
2124 (ggc_alloc (sizeof (cxx_saved_binding)))
2126 struct cxx_saved_binding GTY(())
2128 /* Link that chains saved C++ bindings for a given name into a stack. */
2129 cxx_saved_binding *previous;
2130 /* The name of the current binding. */
2131 tree identifier;
2132 /* The binding we're saving. */
2133 cxx_binding *binding;
2134 tree class_value;
2135 tree real_type_value;
2138 /* Subroutines for reverting temporarily to top-level for instantiation
2139 of templates and such. We actually need to clear out the class- and
2140 local-value slots of all identifiers, so that only the global values
2141 are at all visible. Simply setting current_binding_level to the global
2142 scope isn't enough, because more binding levels may be pushed. */
2143 struct saved_scope *scope_chain;
2145 static cxx_saved_binding *
2146 store_bindings (tree names, cxx_saved_binding *old_bindings)
2148 tree t;
2149 cxx_saved_binding *search_bindings = old_bindings;
2151 timevar_push (TV_NAME_LOOKUP);
2152 for (t = names; t; t = TREE_CHAIN (t))
2154 tree id;
2155 cxx_saved_binding *saved;
2156 cxx_saved_binding *t1;
2158 if (TREE_CODE (t) == TREE_LIST)
2159 id = TREE_PURPOSE (t);
2160 else
2161 id = DECL_NAME (t);
2163 if (!id
2164 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2165 we have no IDENTIFIER_BINDING if we have left the class
2166 scope, but cached the class-level declarations. */
2167 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2168 continue;
2170 for (t1 = search_bindings; t1; t1 = t1->previous)
2171 if (t1->identifier == id)
2172 goto skip_it;
2174 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2175 saved = cxx_saved_binding_make ();
2176 saved->previous = old_bindings;
2177 saved->identifier = id;
2178 saved->binding = IDENTIFIER_BINDING (id);
2179 saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2180 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2181 IDENTIFIER_BINDING (id) = NULL;
2182 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2183 old_bindings = saved;
2184 skip_it:
2187 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2190 void
2191 maybe_push_to_top_level (int pseudo)
2193 struct saved_scope *s;
2194 struct cp_binding_level *b;
2195 cxx_saved_binding *old_bindings;
2196 int need_pop;
2198 timevar_push (TV_NAME_LOOKUP);
2199 s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2201 b = scope_chain ? current_binding_level : 0;
2203 /* If we're in the middle of some function, save our state. */
2204 if (cfun)
2206 need_pop = 1;
2207 push_function_context_to (NULL_TREE);
2209 else
2210 need_pop = 0;
2212 old_bindings = NULL;
2213 if (scope_chain && previous_class_type)
2214 old_bindings = store_bindings (previous_class_values, old_bindings);
2216 /* Have to include global_binding_level, because class-level decls
2217 aren't listed anywhere useful. */
2218 for (; b; b = b->level_chain)
2220 tree t;
2222 /* Template IDs are inserted into the global level. If they were
2223 inserted into namespace level, finish_file wouldn't find them
2224 when doing pending instantiations. Therefore, don't stop at
2225 namespace level, but continue until :: . */
2226 if (b == global_binding_level || (pseudo && b->template_parms_p))
2227 break;
2229 old_bindings = store_bindings (b->names, old_bindings);
2230 /* We also need to check class_shadowed to save class-level type
2231 bindings, since pushclass doesn't fill in b->names. */
2232 if (b->parm_flag == 2)
2233 old_bindings = store_bindings (b->class_shadowed, old_bindings);
2235 /* Unwind type-value slots back to top level. */
2236 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2237 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2239 s->prev = scope_chain;
2240 s->old_bindings = old_bindings;
2241 s->bindings = b;
2242 s->need_pop_function_context = need_pop;
2243 s->function_decl = current_function_decl;
2244 s->last_parms = last_function_parms;
2245 s->check_access = flag_access_control;
2247 scope_chain = s;
2248 current_function_decl = NULL_TREE;
2249 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2250 current_lang_name = lang_name_cplusplus;
2251 current_namespace = global_namespace;
2252 timevar_pop (TV_NAME_LOOKUP);
2255 void
2256 push_to_top_level (void)
2258 maybe_push_to_top_level (0);
2261 void
2262 pop_from_top_level (void)
2264 struct saved_scope *s = scope_chain;
2265 cxx_saved_binding *saved;
2267 timevar_push (TV_NAME_LOOKUP);
2268 /* Clear out class-level bindings cache. */
2269 if (previous_class_type)
2270 invalidate_class_lookup_cache ();
2272 current_lang_base = 0;
2274 scope_chain = s->prev;
2275 for (saved = s->old_bindings; saved; saved = saved->previous)
2277 tree id = saved->identifier;
2279 IDENTIFIER_BINDING (id) = saved->binding;
2280 IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2281 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2284 /* If we were in the middle of compiling a function, restore our
2285 state. */
2286 if (s->need_pop_function_context)
2287 pop_function_context_from (NULL_TREE);
2288 current_function_decl = s->function_decl;
2289 last_function_parms = s->last_parms;
2290 timevar_pop (TV_NAME_LOOKUP);
2293 /* Push a definition of struct, union or enum tag "name".
2294 into binding_level "b". "type" should be the type node,
2295 We assume that the tag "name" is not already defined.
2297 Note that the definition may really be just a forward reference.
2298 In that case, the TYPE_SIZE will be a NULL_TREE.
2300 C++ gratuitously puts all these tags in the name space. */
2302 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2303 record the shadowed value for this binding contour. TYPE is
2304 the type that ID maps to. */
2306 static void
2307 set_identifier_type_value_with_scope (tree id,
2308 tree type,
2309 struct cp_binding_level* b)
2311 if (!b->namespace_p)
2313 /* Shadow the marker, not the real thing, so that the marker
2314 gets restored later. */
2315 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2316 b->type_shadowed
2317 = tree_cons (id, old_type_value, b->type_shadowed);
2319 else
2321 cxx_binding *binding =
2322 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2323 BINDING_TYPE (binding) = type;
2324 /* Store marker instead of real type. */
2325 type = global_type_node;
2327 SET_IDENTIFIER_TYPE_VALUE (id, type);
2330 /* As set_identifier_type_value_with_scope, but using current_binding_level. */
2332 void
2333 set_identifier_type_value (tree id, tree type)
2335 set_identifier_type_value_with_scope (id, type, current_binding_level);
2338 /* Return the type associated with id. */
2340 tree
2341 identifier_type_value (tree id)
2343 timevar_push (TV_NAME_LOOKUP);
2344 /* There is no type with that name, anywhere. */
2345 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2346 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2347 /* This is not the type marker, but the real thing. */
2348 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2349 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2350 /* Have to search for it. It must be on the global level, now.
2351 Ask lookup_name not to return non-types. */
2352 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2353 if (id)
2354 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2355 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2358 /* Pop off extraneous binding levels left over due to syntax errors.
2360 We don't pop past namespaces, as they might be valid. */
2362 void
2363 pop_everything (void)
2365 #ifdef DEBUG_BINDING_LEVELS
2366 fprintf (stderr, "XXX entering pop_everything ()\n");
2367 #endif
2368 while (!toplevel_bindings_p ())
2370 if (current_binding_level->parm_flag == 2)
2371 pop_nested_class ();
2372 else
2373 poplevel (0, 0, 0);
2375 #ifdef DEBUG_BINDING_LEVELS
2376 fprintf (stderr, "XXX leaving pop_everything ()\n");
2377 #endif
2380 /* The type TYPE is being declared. If it is a class template, or a
2381 specialization of a class template, do any processing required and
2382 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
2383 being declared a friend. B is the binding level at which this TYPE
2384 should be bound.
2386 Returns the TYPE_DECL for TYPE, which may have been altered by this
2387 processing. */
2389 static tree
2390 maybe_process_template_type_declaration (tree type,
2391 int globalize,
2392 struct cp_binding_level* b)
2394 tree decl = TYPE_NAME (type);
2396 if (processing_template_parmlist)
2397 /* You can't declare a new template type in a template parameter
2398 list. But, you can declare a non-template type:
2400 template <class A*> struct S;
2402 is a forward-declaration of `A'. */
2404 else
2406 maybe_check_template_type (type);
2408 my_friendly_assert (IS_AGGR_TYPE (type)
2409 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2412 if (processing_template_decl)
2414 /* This may change after the call to
2415 push_template_decl_real, but we want the original value. */
2416 tree name = DECL_NAME (decl);
2418 decl = push_template_decl_real (decl, globalize);
2419 /* If the current binding level is the binding level for the
2420 template parameters (see the comment in
2421 begin_template_parm_list) and the enclosing level is a class
2422 scope, and we're not looking at a friend, push the
2423 declaration of the member class into the class scope. In the
2424 friend case, push_template_decl will already have put the
2425 friend into global scope, if appropriate. */
2426 if (TREE_CODE (type) != ENUMERAL_TYPE
2427 && !globalize && b->template_parms_p
2428 && b->level_chain->parm_flag == 2)
2430 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2431 /* Put this tag on the list of tags for the class, since
2432 that won't happen below because B is not the class
2433 binding level, but is instead the pseudo-global level. */
2434 b->level_chain->tags =
2435 tree_cons (name, type, b->level_chain->tags);
2436 if (!COMPLETE_TYPE_P (current_class_type))
2438 maybe_add_class_template_decl_list (current_class_type,
2439 type, /*friend_p=*/0);
2440 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2446 return decl;
2449 /* In C++, you don't have to write `struct S' to refer to `S'; you
2450 can just use `S'. We accomplish this by creating a TYPE_DECL as
2451 if the user had written `typedef struct S S'. Create and return
2452 the TYPE_DECL for TYPE. */
2454 tree
2455 create_implicit_typedef (tree name, tree type)
2457 tree decl;
2459 decl = build_decl (TYPE_DECL, name, type);
2460 DECL_ARTIFICIAL (decl) = 1;
2461 /* There are other implicit type declarations, like the one *within*
2462 a class that allows you to write `S::S'. We must distinguish
2463 amongst these. */
2464 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2465 TYPE_NAME (type) = decl;
2467 return decl;
2470 /* Remember a local name for name-mangling purposes. */
2472 static void
2473 push_local_name (tree decl)
2475 size_t i, nelts;
2476 tree t, name;
2478 timevar_push (TV_NAME_LOOKUP);
2479 if (!local_names)
2480 VARRAY_TREE_INIT (local_names, 8, "local_names");
2482 name = DECL_NAME (decl);
2484 nelts = VARRAY_ACTIVE_SIZE (local_names);
2485 for (i = 0; i < nelts; i++)
2487 t = VARRAY_TREE (local_names, i);
2488 if (DECL_NAME (t) == name)
2490 if (!DECL_LANG_SPECIFIC (decl))
2491 retrofit_lang_decl (decl);
2492 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2493 if (DECL_LANG_SPECIFIC (t))
2494 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2495 else
2496 DECL_DISCRIMINATOR (decl) = 1;
2498 VARRAY_TREE (local_names, i) = decl;
2499 timevar_pop (TV_NAME_LOOKUP);
2500 return;
2504 VARRAY_PUSH_TREE (local_names, decl);
2505 timevar_pop (TV_NAME_LOOKUP);
2508 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2509 Normally put it into the inner-most non-tag-transparent scope,
2510 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2511 The latter is needed for implicit declarations. */
2513 void
2514 pushtag (tree name, tree type, int globalize)
2516 register struct cp_binding_level *b;
2518 timevar_push (TV_NAME_LOOKUP);
2519 b = current_binding_level;
2520 while (b->tag_transparent
2521 || (b->parm_flag == 2
2522 && (globalize
2523 /* We may be defining a new type in the initializer
2524 of a static member variable. We allow this when
2525 not pedantic, and it is particularly useful for
2526 type punning via an anonymous union. */
2527 || COMPLETE_TYPE_P (b->this_class))))
2528 b = b->level_chain;
2530 b->tags = tree_cons (name, type, b->tags);
2532 if (name)
2534 /* Do C++ gratuitous typedefing. */
2535 if (IDENTIFIER_TYPE_VALUE (name) != type)
2537 register tree d = NULL_TREE;
2538 int in_class = 0;
2539 tree context = TYPE_CONTEXT (type);
2541 if (! context)
2543 tree cs = current_scope ();
2545 if (! globalize)
2546 context = cs;
2547 else if (cs != NULL_TREE && TYPE_P (cs))
2548 /* When declaring a friend class of a local class, we want
2549 to inject the newly named class into the scope
2550 containing the local class, not the namespace scope. */
2551 context = decl_function_context (get_type_decl (cs));
2553 if (!context)
2554 context = current_namespace;
2556 if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2557 || b->parm_flag == 2)
2558 in_class = 1;
2560 if (current_lang_name == lang_name_java)
2561 TYPE_FOR_JAVA (type) = 1;
2563 d = create_implicit_typedef (name, type);
2564 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2565 if (! in_class)
2566 set_identifier_type_value_with_scope (name, type, b);
2568 d = maybe_process_template_type_declaration (type,
2569 globalize, b);
2571 if (b->parm_flag == 2)
2573 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2574 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2575 class. But if it's a member template class, we
2576 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2577 is done later. */
2578 finish_member_declaration (d);
2579 else
2580 pushdecl_class_level (d);
2582 else
2583 d = pushdecl_with_scope (d, b);
2585 /* FIXME what if it gets a name from typedef? */
2586 if (ANON_AGGRNAME_P (name))
2587 DECL_IGNORED_P (d) = 1;
2589 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2591 /* If this is a local class, keep track of it. We need this
2592 information for name-mangling, and so that it is possible to find
2593 all function definitions in a translation unit in a convenient
2594 way. (It's otherwise tricky to find a member function definition
2595 it's only pointed to from within a local class.) */
2596 if (TYPE_CONTEXT (type)
2597 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2598 && !processing_template_decl)
2599 VARRAY_PUSH_TREE (local_classes, type);
2601 if (b->parm_flag == 2)
2603 if (!COMPLETE_TYPE_P (current_class_type))
2605 maybe_add_class_template_decl_list (current_class_type,
2606 type, /*friend_p=*/0);
2607 CLASSTYPE_TAGS (current_class_type) = b->tags;
2612 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2613 /* Use the canonical TYPE_DECL for this node. */
2614 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2615 else
2617 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2618 will be the tagged type we just added to the current
2619 binding level. This fake NULL-named TYPE_DECL node helps
2620 dwarfout.c to know when it needs to output a
2621 representation of a tagged type, and it also gives us a
2622 convenient place to record the "scope start" address for
2623 the tagged type. */
2625 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2626 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2628 timevar_pop (TV_NAME_LOOKUP);
2631 /* Counter used to create anonymous type names. */
2633 static GTY(()) int anon_cnt;
2635 /* Return an IDENTIFIER which can be used as a name for
2636 anonymous structs and unions. */
2638 tree
2639 make_anon_name (void)
2641 char buf[32];
2643 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2644 return get_identifier (buf);
2647 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2648 This keeps dbxout from getting confused. */
2650 void
2651 clear_anon_tags (void)
2653 register struct cp_binding_level *b;
2654 register tree tags;
2655 static int last_cnt = 0;
2657 /* Fast out if no new anon names were declared. */
2658 if (last_cnt == anon_cnt)
2659 return;
2661 b = current_binding_level;
2662 while (b->tag_transparent)
2663 b = b->level_chain;
2664 tags = b->tags;
2665 while (tags)
2667 /* A NULL purpose means we have already processed all tags
2668 from here to the end of the list. */
2669 if (TREE_PURPOSE (tags) == NULL_TREE)
2670 break;
2671 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2672 TREE_PURPOSE (tags) = NULL_TREE;
2673 tags = TREE_CHAIN (tags);
2675 last_cnt = anon_cnt;
2678 /* Subroutine of duplicate_decls: return truthvalue of whether
2679 or not types of these decls match.
2681 For C++, we must compare the parameter list so that `int' can match
2682 `int&' in a parameter position, but `int&' is not confused with
2683 `const int&'. */
2686 decls_match (tree newdecl, tree olddecl)
2688 int types_match;
2690 if (newdecl == olddecl)
2691 return 1;
2693 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2694 /* If the two DECLs are not even the same kind of thing, we're not
2695 interested in their types. */
2696 return 0;
2698 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2700 tree f1 = TREE_TYPE (newdecl);
2701 tree f2 = TREE_TYPE (olddecl);
2702 tree p1 = TYPE_ARG_TYPES (f1);
2703 tree p2 = TYPE_ARG_TYPES (f2);
2705 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2706 && ! (DECL_EXTERN_C_P (newdecl)
2707 && DECL_EXTERN_C_P (olddecl)))
2708 return 0;
2710 if (TREE_CODE (f1) != TREE_CODE (f2))
2711 return 0;
2713 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2715 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2716 && (DECL_BUILT_IN (olddecl)
2717 #ifndef NO_IMPLICIT_EXTERN_C
2718 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2719 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2720 #endif
2723 types_match = self_promoting_args_p (p1);
2724 if (p1 == void_list_node)
2725 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2727 #ifndef NO_IMPLICIT_EXTERN_C
2728 else if (p1 == NULL_TREE
2729 && (DECL_EXTERN_C_P (olddecl)
2730 && DECL_IN_SYSTEM_HEADER (olddecl)
2731 && !DECL_CLASS_SCOPE_P (olddecl))
2732 && (DECL_EXTERN_C_P (newdecl)
2733 && DECL_IN_SYSTEM_HEADER (newdecl)
2734 && !DECL_CLASS_SCOPE_P (newdecl)))
2736 types_match = self_promoting_args_p (p2);
2737 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2739 #endif
2740 else
2741 types_match = compparms (p1, p2);
2743 else
2744 types_match = 0;
2746 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2748 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2749 DECL_TEMPLATE_PARMS (olddecl)))
2750 return 0;
2752 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2753 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2754 return 0;
2756 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2757 types_match = 1;
2758 else
2759 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2760 DECL_TEMPLATE_RESULT (newdecl));
2762 else
2764 if (TREE_TYPE (newdecl) == error_mark_node)
2765 types_match = TREE_TYPE (olddecl) == error_mark_node;
2766 else if (TREE_TYPE (olddecl) == NULL_TREE)
2767 types_match = TREE_TYPE (newdecl) == NULL_TREE;
2768 else if (TREE_TYPE (newdecl) == NULL_TREE)
2769 types_match = 0;
2770 else
2771 types_match = comptypes (TREE_TYPE (newdecl),
2772 TREE_TYPE (olddecl),
2773 COMPARE_REDECLARATION);
2776 return types_match;
2779 /* If NEWDECL is `static' and an `extern' was seen previously,
2780 warn about it. OLDDECL is the previous declaration.
2782 Note that this does not apply to the C++ case of declaring
2783 a variable `extern const' and then later `const'.
2785 Don't complain about built-in functions, since they are beyond
2786 the user's control. */
2788 static void
2789 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2791 static const char *const explicit_extern_static_warning
2792 = "`%D' was declared `extern' and later `static'";
2793 static const char *const implicit_extern_static_warning
2794 = "`%D' was declared implicitly `extern' and later `static'";
2796 tree name;
2798 if (TREE_CODE (newdecl) == TYPE_DECL
2799 || TREE_CODE (newdecl) == TEMPLATE_DECL
2800 || TREE_CODE (newdecl) == CONST_DECL)
2801 return;
2803 /* Don't get confused by static member functions; that's a different
2804 use of `static'. */
2805 if (TREE_CODE (newdecl) == FUNCTION_DECL
2806 && DECL_STATIC_FUNCTION_P (newdecl))
2807 return;
2809 /* If the old declaration was `static', or the new one isn't, then
2810 then everything is OK. */
2811 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2812 return;
2814 /* It's OK to declare a builtin function as `static'. */
2815 if (TREE_CODE (olddecl) == FUNCTION_DECL
2816 && DECL_ARTIFICIAL (olddecl))
2817 return;
2819 name = DECL_ASSEMBLER_NAME (newdecl);
2820 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2821 ? implicit_extern_static_warning
2822 : explicit_extern_static_warning, newdecl);
2823 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2826 /* Handle when a new declaration NEWDECL has the same name as an old
2827 one OLDDECL in the same binding contour. Prints an error message
2828 if appropriate.
2830 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2831 Otherwise, return 0. */
2834 duplicate_decls (tree newdecl, tree olddecl)
2836 unsigned olddecl_uid = DECL_UID (olddecl);
2837 int olddecl_friend = 0, types_match = 0;
2838 int new_defines_function = 0;
2840 if (newdecl == olddecl)
2841 return 1;
2843 types_match = decls_match (newdecl, olddecl);
2845 /* If either the type of the new decl or the type of the old decl is an
2846 error_mark_node, then that implies that we have already issued an
2847 error (earlier) for some bogus type specification, and in that case,
2848 it is rather pointless to harass the user with yet more error message
2849 about the same declaration, so just pretend the types match here. */
2850 if (TREE_TYPE (newdecl) == error_mark_node
2851 || TREE_TYPE (olddecl) == error_mark_node)
2852 types_match = 1;
2854 if (DECL_P (olddecl)
2855 && TREE_CODE (newdecl) == FUNCTION_DECL
2856 && TREE_CODE (olddecl) == FUNCTION_DECL
2857 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2859 if (DECL_DECLARED_INLINE_P (newdecl)
2860 && DECL_UNINLINABLE (newdecl)
2861 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2862 /* Already warned elsewhere. */;
2863 else if (DECL_DECLARED_INLINE_P (olddecl)
2864 && DECL_UNINLINABLE (olddecl)
2865 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2866 /* Already warned. */;
2867 else if (DECL_DECLARED_INLINE_P (newdecl)
2868 && DECL_UNINLINABLE (olddecl)
2869 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2871 warning ("%Hfunction '%D' redeclared as inline",
2872 &DECL_SOURCE_LOCATION (newdecl), newdecl);
2873 warning ("%Hprevious declaration of '%D' with attribute noinline",
2874 &DECL_SOURCE_LOCATION (olddecl), olddecl);
2876 else if (DECL_DECLARED_INLINE_P (olddecl)
2877 && DECL_UNINLINABLE (newdecl)
2878 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2880 warning ("%Hfunction '%D' redeclared with attribute noinline",
2881 &DECL_SOURCE_LOCATION (newdecl), newdecl);
2882 warning ("%Hprevious declaration of '%D' was inline",
2883 &DECL_SOURCE_LOCATION (olddecl), olddecl);
2887 /* Check for redeclaration and other discrepancies. */
2888 if (TREE_CODE (olddecl) == FUNCTION_DECL
2889 && DECL_ARTIFICIAL (olddecl))
2891 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2893 /* Avoid warnings redeclaring anticipated built-ins. */
2894 if (DECL_ANTICIPATED (olddecl))
2895 return 0;
2897 /* If you declare a built-in or predefined function name as static,
2898 the old definition is overridden, but optionally warn this was a
2899 bad choice of name. */
2900 if (! TREE_PUBLIC (newdecl))
2902 if (warn_shadow)
2903 warning ("shadowing %s function `%#D'",
2904 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2905 olddecl);
2906 /* Discard the old built-in function. */
2907 return 0;
2909 /* If the built-in is not ansi, then programs can override
2910 it even globally without an error. */
2911 else if (! DECL_BUILT_IN (olddecl))
2912 warning ("library function `%#D' redeclared as non-function `%#D'",
2913 olddecl, newdecl);
2914 else
2916 error ("declaration of `%#D'", newdecl);
2917 error ("conflicts with built-in declaration `%#D'",
2918 olddecl);
2920 return 0;
2922 else if (!types_match)
2924 /* Avoid warnings redeclaring anticipated built-ins. */
2925 if (DECL_ANTICIPATED (olddecl))
2926 ; /* Do nothing yet. */
2927 else if ((DECL_EXTERN_C_P (newdecl)
2928 && DECL_EXTERN_C_P (olddecl))
2929 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2930 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2932 /* A near match; override the builtin. */
2934 if (TREE_PUBLIC (newdecl))
2936 warning ("new declaration `%#D'", newdecl);
2937 warning ("ambiguates built-in declaration `%#D'",
2938 olddecl);
2940 else if (warn_shadow)
2941 warning ("shadowing %s function `%#D'",
2942 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2943 olddecl);
2945 else
2946 /* Discard the old built-in function. */
2947 return 0;
2949 /* Replace the old RTL to avoid problems with inlining. */
2950 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2952 /* Even if the types match, prefer the new declarations type
2953 for anitipated built-ins, for exception lists, etc... */
2954 else if (DECL_ANTICIPATED (olddecl))
2955 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2957 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2959 /* If a builtin function is redeclared as `static', merge
2960 the declarations, but make the original one static. */
2961 DECL_THIS_STATIC (olddecl) = 1;
2962 TREE_PUBLIC (olddecl) = 0;
2964 /* Make the old declaration consistent with the new one so
2965 that all remnants of the builtin-ness of this function
2966 will be banished. */
2967 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2968 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2971 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2973 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
2974 && TREE_CODE (newdecl) != TYPE_DECL
2975 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
2976 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
2977 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
2978 && TREE_CODE (olddecl) != TYPE_DECL
2979 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
2980 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2981 == TYPE_DECL))))
2983 /* We do nothing special here, because C++ does such nasty
2984 things with TYPE_DECLs. Instead, just let the TYPE_DECL
2985 get shadowed, and know that if we need to find a TYPE_DECL
2986 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2987 slot of the identifier. */
2988 return 0;
2991 if ((TREE_CODE (newdecl) == FUNCTION_DECL
2992 && DECL_FUNCTION_TEMPLATE_P (olddecl))
2993 || (TREE_CODE (olddecl) == FUNCTION_DECL
2994 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2995 return 0;
2997 error ("`%#D' redeclared as different kind of symbol", newdecl);
2998 if (TREE_CODE (olddecl) == TREE_LIST)
2999 olddecl = TREE_VALUE (olddecl);
3000 cp_error_at ("previous declaration of `%#D'", olddecl);
3002 /* New decl is completely inconsistent with the old one =>
3003 tell caller to replace the old one. */
3005 return 0;
3007 else if (!types_match)
3009 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3010 /* These are certainly not duplicate declarations; they're
3011 from different scopes. */
3012 return 0;
3014 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3016 /* The name of a class template may not be declared to refer to
3017 any other template, class, function, object, namespace, value,
3018 or type in the same scope. */
3019 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3020 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3022 error ("declaration of template `%#D'", newdecl);
3023 cp_error_at ("conflicts with previous declaration `%#D'",
3024 olddecl);
3026 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3027 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3028 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3029 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3030 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3031 DECL_TEMPLATE_PARMS (olddecl))
3032 /* Template functions can be disambiguated by
3033 return type. */
3034 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3035 TREE_TYPE (TREE_TYPE (olddecl))))
3037 error ("new declaration `%#D'", newdecl);
3038 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3040 return 0;
3042 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3044 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3046 error ("declaration of C function `%#D' conflicts with",
3047 newdecl);
3048 cp_error_at ("previous declaration `%#D' here", olddecl);
3050 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3051 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3053 error ("new declaration `%#D'", newdecl);
3054 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3056 else
3057 return 0;
3060 /* Already complained about this, so don't do so again. */
3061 else if (current_class_type == NULL_TREE
3062 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3064 error ("conflicting types for `%#D'", newdecl);
3065 cp_error_at ("previous declaration as `%#D'", olddecl);
3068 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3069 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3070 && (!DECL_TEMPLATE_INFO (newdecl)
3071 || (DECL_TI_TEMPLATE (newdecl)
3072 != DECL_TI_TEMPLATE (olddecl))))
3073 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3074 && (!DECL_TEMPLATE_INFO (olddecl)
3075 || (DECL_TI_TEMPLATE (olddecl)
3076 != DECL_TI_TEMPLATE (newdecl))))))
3077 /* It's OK to have a template specialization and a non-template
3078 with the same type, or to have specializations of two
3079 different templates with the same type. Note that if one is a
3080 specialization, and the other is an instantiation of the same
3081 template, that we do not exit at this point. That situation
3082 can occur if we instantiate a template class, and then
3083 specialize one of its methods. This situation is valid, but
3084 the declarations must be merged in the usual way. */
3085 return 0;
3086 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3087 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3088 && !DECL_USE_TEMPLATE (newdecl))
3089 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3090 && !DECL_USE_TEMPLATE (olddecl))))
3091 /* One of the declarations is a template instantiation, and the
3092 other is not a template at all. That's OK. */
3093 return 0;
3094 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3095 && DECL_NAMESPACE_ALIAS (newdecl)
3096 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3097 /* Redeclaration of namespace alias, ignore it. */
3098 return 1;
3099 else
3101 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3102 if (errmsg)
3104 error (errmsg, newdecl);
3105 if (DECL_NAME (olddecl) != NULL_TREE)
3106 cp_error_at ((DECL_INITIAL (olddecl)
3107 && namespace_bindings_p ())
3108 ? "`%#D' previously defined here"
3109 : "`%#D' previously declared here", olddecl);
3110 return 0;
3112 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3113 && DECL_INITIAL (olddecl) != NULL_TREE
3114 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3115 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3117 /* Prototype decl follows defn w/o prototype. */
3118 cp_warning_at ("prototype for `%#D'", newdecl);
3119 cp_warning_at ("follows non-prototype definition here", olddecl);
3121 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3122 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3124 /* extern "C" int foo ();
3125 int foo () { bar (); }
3126 is OK. */
3127 if (current_lang_depth () == 0)
3128 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3129 else
3131 cp_error_at ("previous declaration of `%#D' with %L linkage",
3132 olddecl, DECL_LANGUAGE (olddecl));
3133 error ("conflicts with new declaration with %L linkage",
3134 DECL_LANGUAGE (newdecl));
3138 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3140 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3142 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3143 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3144 int i = 1;
3146 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3147 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3149 for (; t1 && t1 != void_list_node;
3150 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3151 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3153 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3154 TREE_PURPOSE (t2)))
3156 pedwarn ("default argument given for parameter %d of `%#D'",
3157 i, newdecl);
3158 cp_pedwarn_at ("after previous specification in `%#D'",
3159 olddecl);
3161 else
3163 error ("default argument given for parameter %d of `%#D'",
3164 i, newdecl);
3165 cp_error_at ("after previous specification in `%#D'",
3166 olddecl);
3170 if (DECL_DECLARED_INLINE_P (newdecl)
3171 && ! DECL_DECLARED_INLINE_P (olddecl)
3172 && TREE_ADDRESSABLE (olddecl) && warn_inline)
3174 warning ("`%#D' was used before it was declared inline",
3175 newdecl);
3176 cp_warning_at ("previous non-inline declaration here",
3177 olddecl);
3182 /* Do not merge an implicit typedef with an explicit one. In:
3184 class A;
3186 typedef class A A __attribute__ ((foo));
3188 the attribute should apply only to the typedef. */
3189 if (TREE_CODE (olddecl) == TYPE_DECL
3190 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3191 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3192 return 0;
3194 /* If new decl is `static' and an `extern' was seen previously,
3195 warn about it. */
3196 warn_extern_redeclared_static (newdecl, olddecl);
3198 /* We have committed to returning 1 at this point. */
3199 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3201 /* Now that functions must hold information normally held
3202 by field decls, there is extra work to do so that
3203 declaration information does not get destroyed during
3204 definition. */
3205 if (DECL_VINDEX (olddecl))
3206 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3207 if (DECL_CONTEXT (olddecl))
3208 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3209 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3210 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3211 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3212 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3213 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3214 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3215 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3216 SET_OVERLOADED_OPERATOR_CODE
3217 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3218 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3220 /* Optionally warn about more than one declaration for the same
3221 name, but don't warn about a function declaration followed by a
3222 definition. */
3223 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3224 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3225 /* Don't warn about extern decl followed by definition. */
3226 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3227 /* Don't warn about friends, let add_friend take care of it. */
3228 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3230 warning ("redundant redeclaration of `%D' in same scope", newdecl);
3231 cp_warning_at ("previous declaration of `%D'", olddecl);
3235 /* Deal with C++: must preserve virtual function table size. */
3236 if (TREE_CODE (olddecl) == TYPE_DECL)
3238 register tree newtype = TREE_TYPE (newdecl);
3239 register tree oldtype = TREE_TYPE (olddecl);
3241 if (newtype != error_mark_node && oldtype != error_mark_node
3242 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3243 CLASSTYPE_FRIEND_CLASSES (newtype)
3244 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3246 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3249 /* Copy all the DECL_... slots specified in the new decl
3250 except for any that we copy here from the old type. */
3251 DECL_ATTRIBUTES (newdecl)
3252 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3254 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3256 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3257 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3258 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3259 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3261 /* If the new declaration is a definition, update the file and
3262 line information on the declaration. */
3263 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3264 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3266 DECL_SOURCE_LOCATION (olddecl)
3267 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3268 = DECL_SOURCE_LOCATION (newdecl);
3271 return 1;
3274 if (types_match)
3276 /* Automatically handles default parameters. */
3277 tree oldtype = TREE_TYPE (olddecl);
3278 tree newtype;
3280 /* Merge the data types specified in the two decls. */
3281 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3283 /* If merge_types produces a non-typedef type, just use the old type. */
3284 if (TREE_CODE (newdecl) == TYPE_DECL
3285 && newtype == DECL_ORIGINAL_TYPE (newdecl))
3286 newtype = oldtype;
3288 if (TREE_CODE (newdecl) == VAR_DECL)
3290 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3291 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3294 /* Do this after calling `merge_types' so that default
3295 parameters don't confuse us. */
3296 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3297 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3298 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3300 TREE_TYPE (newdecl) = build_exception_variant (newtype,
3301 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3302 TREE_TYPE (olddecl) = build_exception_variant (newtype,
3303 TYPE_RAISES_EXCEPTIONS (oldtype));
3305 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3306 && DECL_SOURCE_LINE (olddecl) != 0
3307 && flag_exceptions
3308 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3309 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3311 error ("declaration of `%F' throws different exceptions",
3312 newdecl);
3313 cp_error_at ("than previous declaration `%F'", olddecl);
3316 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3318 /* Lay the type out, unless already done. */
3319 if (! same_type_p (newtype, oldtype)
3320 && TREE_TYPE (newdecl) != error_mark_node
3321 && !(processing_template_decl && uses_template_parms (newdecl)))
3322 layout_type (TREE_TYPE (newdecl));
3324 if ((TREE_CODE (newdecl) == VAR_DECL
3325 || TREE_CODE (newdecl) == PARM_DECL
3326 || TREE_CODE (newdecl) == RESULT_DECL
3327 || TREE_CODE (newdecl) == FIELD_DECL
3328 || TREE_CODE (newdecl) == TYPE_DECL)
3329 && !(processing_template_decl && uses_template_parms (newdecl)))
3330 layout_decl (newdecl, 0);
3332 /* Merge the type qualifiers. */
3333 if (TREE_READONLY (newdecl))
3334 TREE_READONLY (olddecl) = 1;
3335 if (TREE_THIS_VOLATILE (newdecl))
3336 TREE_THIS_VOLATILE (olddecl) = 1;
3338 /* Merge the initialization information. */
3339 if (DECL_INITIAL (newdecl) == NULL_TREE
3340 && DECL_INITIAL (olddecl) != NULL_TREE)
3342 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3343 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3344 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3345 && DECL_LANG_SPECIFIC (newdecl)
3346 && DECL_LANG_SPECIFIC (olddecl))
3347 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3350 /* Merge the section attribute.
3351 We want to issue an error if the sections conflict but that must be
3352 done later in decl_attributes since we are called before attributes
3353 are assigned. */
3354 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3355 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3357 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3359 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3360 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3361 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
3362 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
3363 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
3364 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
3365 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
3366 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
3367 /* Keep the old RTL. */
3368 COPY_DECL_RTL (olddecl, newdecl);
3370 else if (TREE_CODE (newdecl) == VAR_DECL
3371 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3373 /* Keep the old RTL. We cannot keep the old RTL if the old
3374 declaration was for an incomplete object and the new
3375 declaration is not since many attributes of the RTL will
3376 change. */
3377 COPY_DECL_RTL (olddecl, newdecl);
3380 /* If cannot merge, then use the new type and qualifiers,
3381 and don't preserve the old rtl. */
3382 else
3384 /* Clean out any memory we had of the old declaration. */
3385 tree oldstatic = value_member (olddecl, static_aggregates);
3386 if (oldstatic)
3387 TREE_VALUE (oldstatic) = error_mark_node;
3389 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3390 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3391 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3392 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3395 /* Merge the storage class information. */
3396 merge_weak (newdecl, olddecl);
3398 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3399 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3400 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3401 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3402 if (! DECL_EXTERNAL (olddecl))
3403 DECL_EXTERNAL (newdecl) = 0;
3405 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3407 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3408 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3409 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3410 DECL_TEMPLATE_INSTANTIATED (newdecl)
3411 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3412 /* Don't really know how much of the language-specific
3413 values we should copy from old to new. */
3414 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3415 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
3416 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3417 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3418 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3419 DECL_INITIALIZED_IN_CLASS_P (newdecl)
3420 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3421 olddecl_friend = DECL_FRIEND_P (olddecl);
3423 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3424 if (TREE_CODE (newdecl) == FUNCTION_DECL
3425 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3427 DECL_BEFRIENDING_CLASSES (newdecl)
3428 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3429 DECL_BEFRIENDING_CLASSES (olddecl));
3430 /* DECL_THUNKS is only valid for virtual functions,
3431 otherwise it is a DECL_FRIEND_CONTEXT. */
3432 if (DECL_VIRTUAL_P (newdecl))
3433 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3437 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3439 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3440 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3442 /* If newdecl is not a specialization, then it is not a
3443 template-related function at all. And that means that we
3444 should have exited above, returning 0. */
3445 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3448 if (TREE_USED (olddecl))
3449 /* From [temp.expl.spec]:
3451 If a template, a member template or the member of a class
3452 template is explicitly specialized then that
3453 specialization shall be declared before the first use of
3454 that specialization that would cause an implicit
3455 instantiation to take place, in every translation unit in
3456 which such a use occurs. */
3457 error ("explicit specialization of %D after first use",
3458 olddecl);
3460 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3462 /* [temp.expl.spec/14] We don't inline explicit specialization
3463 just because the primary template says so. */
3465 else
3467 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3468 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3470 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3472 /* If either decl says `inline', this fn is inline, unless
3473 its definition was passed already. */
3474 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3475 DECL_INLINE (olddecl) = 1;
3476 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3478 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3479 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3482 /* Preserve abstractness on cloned [cd]tors. */
3483 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3485 if (! types_match)
3487 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3488 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3489 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3491 if (! types_match || new_defines_function)
3493 /* These need to be copied so that the names are available.
3494 Note that if the types do match, we'll preserve inline
3495 info and other bits, but if not, we won't. */
3496 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3497 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3499 if (new_defines_function)
3500 /* If defining a function declared with other language
3501 linkage, use the previously declared language linkage. */
3502 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3503 else if (types_match)
3505 /* If redeclaring a builtin function, and not a definition,
3506 it stays built in. */
3507 if (DECL_BUILT_IN (olddecl))
3509 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3510 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3511 /* If we're keeping the built-in definition, keep the rtl,
3512 regardless of declaration matches. */
3513 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3515 else
3516 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3518 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3519 /* Don't clear out the arguments if we're redefining a function. */
3520 if (DECL_ARGUMENTS (olddecl))
3521 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3524 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3525 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3527 /* Now preserve various other info from the definition. */
3528 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3529 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3530 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3531 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3533 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3535 int function_size;
3537 function_size = sizeof (struct tree_decl);
3539 memcpy ((char *) olddecl + sizeof (struct tree_common),
3540 (char *) newdecl + sizeof (struct tree_common),
3541 function_size - sizeof (struct tree_common));
3543 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3545 /* If newdecl is a template instantiation, it is possible that
3546 the following sequence of events has occurred:
3548 o A friend function was declared in a class template. The
3549 class template was instantiated.
3551 o The instantiation of the friend declaration was
3552 recorded on the instantiation list, and is newdecl.
3554 o Later, however, instantiate_class_template called pushdecl
3555 on the newdecl to perform name injection. But, pushdecl in
3556 turn called duplicate_decls when it discovered that another
3557 declaration of a global function with the same name already
3558 existed.
3560 o Here, in duplicate_decls, we decided to clobber newdecl.
3562 If we're going to do that, we'd better make sure that
3563 olddecl, and not newdecl, is on the list of
3564 instantiations so that if we try to do the instantiation
3565 again we won't get the clobbered declaration. */
3567 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3568 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3570 for (; decls; decls = TREE_CHAIN (decls))
3571 if (TREE_VALUE (decls) == newdecl)
3572 TREE_VALUE (decls) = olddecl;
3575 else
3577 memcpy ((char *) olddecl + sizeof (struct tree_common),
3578 (char *) newdecl + sizeof (struct tree_common),
3579 sizeof (struct tree_decl) - sizeof (struct tree_common)
3580 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3583 DECL_UID (olddecl) = olddecl_uid;
3584 if (olddecl_friend)
3585 DECL_FRIEND_P (olddecl) = 1;
3587 /* NEWDECL contains the merged attribute lists.
3588 Update OLDDECL to be the same. */
3589 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3591 return 1;
3594 /* Record a decl-node X as belonging to the current lexical scope.
3595 Check for errors (such as an incompatible declaration for the same
3596 name already seen in the same scope).
3598 Returns either X or an old decl for the same name.
3599 If an old decl is returned, it may have been smashed
3600 to agree with what X says. */
3602 tree
3603 pushdecl (tree x)
3605 register tree t;
3606 register tree name;
3607 int need_new_binding;
3609 timevar_push (TV_NAME_LOOKUP);
3610 /* We shouldn't be calling pushdecl when we're generating RTL for a
3611 function that we already did semantic analysis on previously. */
3612 my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3613 19990913);
3615 need_new_binding = 1;
3617 if (DECL_TEMPLATE_PARM_P (x))
3618 /* Template parameters have no context; they are not X::T even
3619 when declared within a class or namespace. */
3621 else
3623 if (current_function_decl && x != current_function_decl
3624 /* A local declaration for a function doesn't constitute
3625 nesting. */
3626 && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3627 /* A local declaration for an `extern' variable is in the
3628 scope of the current namespace, not the current
3629 function. */
3630 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3631 && !DECL_CONTEXT (x))
3632 DECL_CONTEXT (x) = current_function_decl;
3634 /* If this is the declaration for a namespace-scope function,
3635 but the declaration itself is in a local scope, mark the
3636 declaration. */
3637 if (TREE_CODE (x) == FUNCTION_DECL
3638 && DECL_NAMESPACE_SCOPE_P (x)
3639 && current_function_decl
3640 && x != current_function_decl)
3641 DECL_LOCAL_FUNCTION_P (x) = 1;
3644 name = DECL_NAME (x);
3645 if (name)
3647 int different_binding_level = 0;
3649 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3650 name = TREE_OPERAND (name, 0);
3652 /* In case this decl was explicitly namespace-qualified, look it
3653 up in its namespace context. */
3654 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3655 && namespace_bindings_p ())
3656 t = namespace_binding (name, DECL_CONTEXT (x));
3657 else
3658 t = lookup_name_current_level (name);
3660 /* [basic.link] If there is a visible declaration of an entity
3661 with linkage having the same name and type, ignoring entities
3662 declared outside the innermost enclosing namespace scope, the
3663 block scope declaration declares that same entity and
3664 receives the linkage of the previous declaration. */
3665 if (! t && current_function_decl && x != current_function_decl
3666 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3667 && DECL_EXTERNAL (x))
3669 /* Look in block scope. */
3670 t = IDENTIFIER_VALUE (name);
3671 /* Or in the innermost namespace. */
3672 if (! t)
3673 t = namespace_binding (name, DECL_CONTEXT (x));
3674 /* Does it have linkage? Note that if this isn't a DECL, it's an
3675 OVERLOAD, which is OK. */
3676 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3677 t = NULL_TREE;
3678 if (t)
3679 different_binding_level = 1;
3682 /* If we are declaring a function, and the result of name-lookup
3683 was an OVERLOAD, look for an overloaded instance that is
3684 actually the same as the function we are declaring. (If
3685 there is one, we have to merge our declaration with the
3686 previous declaration.) */
3687 if (t && TREE_CODE (t) == OVERLOAD)
3689 tree match;
3691 if (TREE_CODE (x) == FUNCTION_DECL)
3692 for (match = t; match; match = OVL_NEXT (match))
3694 if (decls_match (OVL_CURRENT (match), x))
3695 break;
3697 else
3698 /* Just choose one. */
3699 match = t;
3701 if (match)
3702 t = OVL_CURRENT (match);
3703 else
3704 t = NULL_TREE;
3707 if (t == error_mark_node)
3709 /* error_mark_node is 0 for a while during initialization! */
3710 t = NULL_TREE;
3711 cp_error_at ("`%#D' used prior to declaration", x);
3713 else if (t != NULL_TREE)
3715 if (different_binding_level)
3717 if (decls_match (x, t))
3718 /* The standard only says that the local extern
3719 inherits linkage from the previous decl; in
3720 particular, default args are not shared. It would
3721 be nice to propagate inlining info, though. FIXME. */
3722 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3724 else if (TREE_CODE (t) == PARM_DECL)
3726 if (DECL_CONTEXT (t) == NULL_TREE)
3727 /* This is probaby caused by too many errors, but calling
3728 abort will say that if errors have occurred. */
3729 abort ();
3731 /* Check for duplicate params. */
3732 if (duplicate_decls (x, t))
3733 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3735 else if ((DECL_EXTERN_C_FUNCTION_P (x)
3736 || DECL_FUNCTION_TEMPLATE_P (x))
3737 && is_overloaded_fn (t))
3738 /* Don't do anything just yet. */;
3739 else if (t == wchar_decl_node)
3741 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3742 pedwarn ("redeclaration of `wchar_t' as `%T'",
3743 TREE_TYPE (x));
3745 /* Throw away the redeclaration. */
3746 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3748 else if (TREE_CODE (t) != TREE_CODE (x))
3750 if (duplicate_decls (x, t))
3751 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3753 else if (duplicate_decls (x, t))
3755 if (TREE_CODE (t) == TYPE_DECL)
3756 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3757 else if (TREE_CODE (t) == FUNCTION_DECL)
3758 check_default_args (t);
3760 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3762 else if (DECL_MAIN_P (x))
3764 /* A redeclaration of main, but not a duplicate of the
3765 previous one.
3767 [basic.start.main]
3769 This function shall not be overloaded. */
3770 cp_error_at ("invalid redeclaration of `%D'", t);
3771 error ("as `%D'", x);
3772 /* We don't try to push this declaration since that
3773 causes a crash. */
3774 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3778 check_template_shadow (x);
3780 /* If this is a function conjured up by the backend, massage it
3781 so it looks friendly. */
3782 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3784 retrofit_lang_decl (x);
3785 SET_DECL_LANGUAGE (x, lang_c);
3788 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3790 t = push_overloaded_decl (x, PUSH_LOCAL);
3791 if (t != x)
3792 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3793 if (!namespace_bindings_p ())
3794 /* We do not need to create a binding for this name;
3795 push_overloaded_decl will have already done so if
3796 necessary. */
3797 need_new_binding = 0;
3799 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3801 t = push_overloaded_decl (x, PUSH_GLOBAL);
3802 if (t == x)
3803 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3804 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3807 /* If declaring a type as a typedef, copy the type (unless we're
3808 at line 0), and install this TYPE_DECL as the new type's typedef
3809 name. See the extensive comment in ../c-decl.c (pushdecl). */
3810 if (TREE_CODE (x) == TYPE_DECL)
3812 tree type = TREE_TYPE (x);
3813 if (DECL_SOURCE_LINE (x) == 0)
3815 if (TYPE_NAME (type) == 0)
3816 TYPE_NAME (type) = x;
3818 else if (type != error_mark_node && TYPE_NAME (type) != x
3819 /* We don't want to copy the type when all we're
3820 doing is making a TYPE_DECL for the purposes of
3821 inlining. */
3822 && (!TYPE_NAME (type)
3823 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3825 DECL_ORIGINAL_TYPE (x) = type;
3826 type = build_type_copy (type);
3827 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3828 TYPE_NAME (type) = x;
3829 TREE_TYPE (x) = type;
3832 if (type != error_mark_node
3833 && TYPE_NAME (type)
3834 && TYPE_IDENTIFIER (type))
3835 set_identifier_type_value_with_scope (DECL_NAME (x), type,
3836 current_binding_level);
3840 /* Multiple external decls of the same identifier ought to match.
3842 We get warnings about inline functions where they are defined.
3843 We get warnings about other functions from push_overloaded_decl.
3845 Avoid duplicate warnings where they are used. */
3846 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3848 tree decl;
3850 decl = IDENTIFIER_NAMESPACE_VALUE (name);
3851 if (decl && TREE_CODE (decl) == OVERLOAD)
3852 decl = OVL_FUNCTION (decl);
3854 if (decl && decl != error_mark_node
3855 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3856 /* If different sort of thing, we already gave an error. */
3857 && TREE_CODE (decl) == TREE_CODE (x)
3858 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3860 pedwarn ("type mismatch with previous external decl", x);
3861 cp_pedwarn_at ("previous external decl of `%#D'", decl);
3865 /* This name is new in its binding level.
3866 Install the new declaration and return it. */
3867 if (namespace_bindings_p ())
3869 /* Install a global value. */
3871 /* If the first global decl has external linkage,
3872 warn if we later see static one. */
3873 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3874 TREE_PUBLIC (name) = 1;
3876 /* Bind the name for the entity. */
3877 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3878 && t != NULL_TREE)
3879 && (TREE_CODE (x) == TYPE_DECL
3880 || TREE_CODE (x) == VAR_DECL
3881 || TREE_CODE (x) == ALIAS_DECL
3882 || TREE_CODE (x) == NAMESPACE_DECL
3883 || TREE_CODE (x) == CONST_DECL
3884 || TREE_CODE (x) == TEMPLATE_DECL))
3885 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3887 /* Don't forget if the function was used via an implicit decl. */
3888 if (IDENTIFIER_IMPLICIT_DECL (name)
3889 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3890 TREE_USED (x) = 1;
3892 /* Don't forget if its address was taken in that way. */
3893 if (IDENTIFIER_IMPLICIT_DECL (name)
3894 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3895 TREE_ADDRESSABLE (x) = 1;
3897 /* Warn about mismatches against previous implicit decl. */
3898 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3899 /* If this real decl matches the implicit, don't complain. */
3900 && ! (TREE_CODE (x) == FUNCTION_DECL
3901 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3902 warning
3903 ("`%D' was previously implicitly declared to return `int'", x);
3905 /* If new decl is `static' and an `extern' was seen previously,
3906 warn about it. */
3907 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3908 warn_extern_redeclared_static (x, t);
3910 else
3912 /* Here to install a non-global value. */
3913 tree oldlocal = IDENTIFIER_VALUE (name);
3914 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3916 if (need_new_binding)
3918 push_local_binding (name, x, 0);
3919 /* Because push_local_binding will hook X on to the
3920 current_binding_level's name list, we don't want to
3921 do that again below. */
3922 need_new_binding = 0;
3925 /* If this is a TYPE_DECL, push it into the type value slot. */
3926 if (TREE_CODE (x) == TYPE_DECL)
3927 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
3928 current_binding_level);
3930 /* Clear out any TYPE_DECL shadowed by a namespace so that
3931 we won't think this is a type. The C struct hack doesn't
3932 go through namespaces. */
3933 if (TREE_CODE (x) == NAMESPACE_DECL)
3934 set_identifier_type_value_with_scope (name, NULL_TREE,
3935 current_binding_level);
3937 if (oldlocal)
3939 tree d = oldlocal;
3941 while (oldlocal
3942 && TREE_CODE (oldlocal) == VAR_DECL
3943 && DECL_DEAD_FOR_LOCAL (oldlocal))
3944 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
3946 if (oldlocal == NULL_TREE)
3947 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
3950 /* If this is an extern function declaration, see if we
3951 have a global definition or declaration for the function. */
3952 if (oldlocal == NULL_TREE
3953 && DECL_EXTERNAL (x)
3954 && oldglobal != NULL_TREE
3955 && TREE_CODE (x) == FUNCTION_DECL
3956 && TREE_CODE (oldglobal) == FUNCTION_DECL)
3958 /* We have one. Their types must agree. */
3959 if (decls_match (x, oldglobal))
3960 /* OK */;
3961 else
3963 warning ("extern declaration of `%#D' doesn't match", x);
3964 cp_warning_at ("global declaration `%#D'", oldglobal);
3967 /* If we have a local external declaration,
3968 and no file-scope declaration has yet been seen,
3969 then if we later have a file-scope decl it must not be static. */
3970 if (oldlocal == NULL_TREE
3971 && oldglobal == NULL_TREE
3972 && DECL_EXTERNAL (x)
3973 && TREE_PUBLIC (x))
3974 TREE_PUBLIC (name) = 1;
3976 /* Warn if shadowing an argument at the top level of the body. */
3977 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3978 /* Inline decls shadow nothing. */
3979 && !DECL_FROM_INLINE (x)
3980 && TREE_CODE (oldlocal) == PARM_DECL
3981 /* Don't check the `this' parameter. */
3982 && !DECL_ARTIFICIAL (oldlocal))
3984 bool err = false;
3986 /* Don't complain if it's from an enclosing function. */
3987 if (DECL_CONTEXT (oldlocal) == current_function_decl
3988 && TREE_CODE (x) != PARM_DECL)
3990 /* Go to where the parms should be and see if we find
3991 them there. */
3992 struct cp_binding_level *b = current_binding_level->level_chain;
3994 /* Skip the ctor/dtor cleanup level. */
3995 b = b->level_chain;
3997 /* ARM $8.3 */
3998 if (b->parm_flag == 1)
4000 error ("declaration of `%#D' shadows a parameter",
4001 name);
4002 err = true;
4006 if (warn_shadow && !err)
4007 shadow_warning (SW_PARAM,
4008 IDENTIFIER_POINTER (name), oldlocal);
4011 /* Maybe warn if shadowing something else. */
4012 else if (warn_shadow && !DECL_EXTERNAL (x)
4013 /* No shadow warnings for internally generated vars. */
4014 && ! DECL_ARTIFICIAL (x)
4015 /* No shadow warnings for vars made for inlining. */
4016 && ! DECL_FROM_INLINE (x))
4018 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4019 && current_class_ptr
4020 && !TREE_STATIC (name))
4021 warning ("declaration of `%s' shadows a member of `this'",
4022 IDENTIFIER_POINTER (name));
4023 else if (oldlocal != NULL_TREE
4024 && TREE_CODE (oldlocal) == VAR_DECL)
4025 shadow_warning (SW_LOCAL,
4026 IDENTIFIER_POINTER (name), oldlocal);
4027 else if (oldglobal != NULL_TREE
4028 && TREE_CODE (oldglobal) == VAR_DECL)
4029 /* XXX shadow warnings in outer-more namespaces */
4030 shadow_warning (SW_GLOBAL,
4031 IDENTIFIER_POINTER (name), oldglobal);
4035 if (TREE_CODE (x) == FUNCTION_DECL)
4036 check_default_args (x);
4038 if (TREE_CODE (x) == VAR_DECL)
4039 maybe_register_incomplete_var (x);
4042 if (need_new_binding)
4043 add_decl_to_level (x,
4044 DECL_NAMESPACE_SCOPE_P (x)
4045 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4046 : current_binding_level);
4048 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4051 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4052 caller to set DECL_CONTEXT properly. */
4054 static tree
4055 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4057 register struct cp_binding_level *b;
4058 tree function_decl = current_function_decl;
4060 timevar_push (TV_NAME_LOOKUP);
4061 current_function_decl = NULL_TREE;
4062 if (level->parm_flag == 2)
4064 b = class_binding_level;
4065 class_binding_level = level;
4066 pushdecl_class_level (x);
4067 class_binding_level = b;
4069 else
4071 b = current_binding_level;
4072 current_binding_level = level;
4073 x = pushdecl (x);
4074 current_binding_level = b;
4076 current_function_decl = function_decl;
4077 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4080 /* Like pushdecl, only it places X in the current namespace,
4081 if appropriate. */
4083 tree
4084 pushdecl_namespace_level (tree x)
4086 register struct cp_binding_level *b = current_binding_level;
4087 register tree t;
4089 timevar_push (TV_NAME_LOOKUP);
4090 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4092 /* Now, the type_shadowed stack may screw us. Munge it so it does
4093 what we want. */
4094 if (TREE_CODE (x) == TYPE_DECL)
4096 tree name = DECL_NAME (x);
4097 tree newval;
4098 tree *ptr = (tree *)0;
4099 for (; b != global_binding_level; b = b->level_chain)
4101 tree shadowed = b->type_shadowed;
4102 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4103 if (TREE_PURPOSE (shadowed) == name)
4105 ptr = &TREE_VALUE (shadowed);
4106 /* Can't break out of the loop here because sometimes
4107 a binding level will have duplicate bindings for
4108 PT names. It's gross, but I haven't time to fix it. */
4111 newval = TREE_TYPE (x);
4112 if (ptr == (tree *)0)
4114 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4115 up here if this is changed to an assertion. --KR */
4116 SET_IDENTIFIER_TYPE_VALUE (name, newval);
4118 else
4120 *ptr = newval;
4123 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4126 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4127 if appropriate. */
4129 tree
4130 pushdecl_top_level (tree x)
4132 timevar_push (TV_NAME_LOOKUP);
4133 push_to_top_level ();
4134 x = pushdecl_namespace_level (x);
4135 pop_from_top_level ();
4136 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4139 /* Make the declaration of X appear in CLASS scope. */
4141 void
4142 pushdecl_class_level (tree x)
4144 tree name;
4146 timevar_push (TV_NAME_LOOKUP);
4147 /* Get the name of X. */
4148 if (TREE_CODE (x) == OVERLOAD)
4149 name = DECL_NAME (get_first_fn (x));
4150 else
4151 name = DECL_NAME (x);
4153 if (name)
4155 push_class_level_binding (name, x);
4156 if (TREE_CODE (x) == TYPE_DECL)
4157 set_identifier_type_value (name, TREE_TYPE (x));
4159 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4161 /* If X is an anonymous aggregate, all of its members are
4162 treated as if they were members of the class containing the
4163 aggregate, for naming purposes. */
4164 tree f;
4166 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4167 pushdecl_class_level (f);
4169 timevar_pop (TV_NAME_LOOKUP);
4172 /* Enter DECL into the symbol table, if that's appropriate. Returns
4173 DECL, or a modified version thereof. */
4175 tree
4176 maybe_push_decl (tree decl)
4178 tree type = TREE_TYPE (decl);
4180 /* Add this decl to the current binding level, but not if it comes
4181 from another scope, e.g. a static member variable. TEM may equal
4182 DECL or it may be a previous decl of the same name. */
4183 if (decl == error_mark_node
4184 || (TREE_CODE (decl) != PARM_DECL
4185 && DECL_CONTEXT (decl) != NULL_TREE
4186 /* Definitions of namespace members outside their namespace are
4187 possible. */
4188 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4189 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4190 || TREE_CODE (type) == UNKNOWN_TYPE
4191 /* The declaration of a template specialization does not affect
4192 the functions available for overload resolution, so we do not
4193 call pushdecl. */
4194 || (TREE_CODE (decl) == FUNCTION_DECL
4195 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4196 return decl;
4197 else
4198 return pushdecl (decl);
4201 /* Make the declaration(s) of X appear in CLASS scope
4202 under the name NAME. */
4204 void
4205 push_class_level_binding (tree name, tree x)
4207 cxx_binding *binding;
4208 timevar_push (TV_NAME_LOOKUP);
4209 /* The class_binding_level will be NULL if x is a template
4210 parameter name in a member template. */
4211 if (!class_binding_level)
4213 timevar_pop (TV_NAME_LOOKUP);
4214 return;
4217 /* Make sure that this new member does not have the same name
4218 as a template parameter. */
4219 if (TYPE_BEING_DEFINED (current_class_type))
4220 check_template_shadow (x);
4222 /* If this declaration shadows a declaration from an enclosing
4223 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4224 we leave this class. Record the shadowed declaration here. */
4225 binding = IDENTIFIER_BINDING (name);
4226 if (binding
4227 && ((TREE_CODE (x) == OVERLOAD
4228 && BINDING_VALUE (binding)
4229 && is_overloaded_fn (BINDING_VALUE (binding)))
4230 || INHERITED_VALUE_BINDING_P (binding)))
4232 tree shadow;
4233 tree old_decl;
4235 /* If the old binding was from a base class, and was for a tag
4236 name, slide it over to make room for the new binding. The
4237 old binding is still visible if explicitly qualified with a
4238 class-key. */
4239 if (INHERITED_VALUE_BINDING_P (binding)
4240 && BINDING_VALUE (binding)
4241 && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4242 && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4243 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4245 old_decl = BINDING_TYPE (binding);
4246 BINDING_TYPE (binding) = BINDING_VALUE (binding);
4247 BINDING_VALUE (binding) = NULL_TREE;
4248 INHERITED_VALUE_BINDING_P (binding) = 0;
4250 else
4251 old_decl = BINDING_VALUE (binding);
4253 /* Find the previous binding of name on the class-shadowed
4254 list, and update it. */
4255 for (shadow = class_binding_level->class_shadowed;
4256 shadow;
4257 shadow = TREE_CHAIN (shadow))
4258 if (TREE_PURPOSE (shadow) == name
4259 && TREE_TYPE (shadow) == old_decl)
4261 BINDING_VALUE (binding) = x;
4262 INHERITED_VALUE_BINDING_P (binding) = 0;
4263 TREE_TYPE (shadow) = x;
4264 IDENTIFIER_CLASS_VALUE (name) = x;
4265 timevar_pop (TV_NAME_LOOKUP);
4266 return;
4270 /* If we didn't replace an existing binding, put the binding on the
4271 stack of bindings for the identifier, and update the shadowed list. */
4272 if (push_class_binding (name, x))
4274 class_binding_level->class_shadowed
4275 = tree_cons (name, NULL,
4276 class_binding_level->class_shadowed);
4277 /* Record the value we are binding NAME to so that we can know
4278 what to pop later. */
4279 TREE_TYPE (class_binding_level->class_shadowed) = x;
4281 timevar_pop (TV_NAME_LOOKUP);
4284 /* Insert another USING_DECL into the current binding level, returning
4285 this declaration. If this is a redeclaration, do nothing, and
4286 return NULL_TREE if this not in namespace scope (in namespace
4287 scope, a using decl might extend any previous bindings). */
4289 tree
4290 push_using_decl (tree scope, tree name)
4292 tree decl;
4294 timevar_push (TV_NAME_LOOKUP);
4295 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4296 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4297 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4298 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4299 break;
4300 if (decl)
4301 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4302 namespace_bindings_p () ? decl : NULL_TREE);
4303 decl = build_lang_decl (USING_DECL, name, void_type_node);
4304 DECL_INITIAL (decl) = scope;
4305 TREE_CHAIN (decl) = current_binding_level->usings;
4306 current_binding_level->usings = decl;
4307 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4310 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4311 changed (i.e. there was already a directive), or the fresh
4312 TREE_LIST otherwise. */
4314 tree
4315 push_using_directive (tree used)
4317 tree ud = current_binding_level->using_directives;
4318 tree iter, ancestor;
4320 timevar_push (TV_NAME_LOOKUP);
4321 /* Check if we already have this. */
4322 if (purpose_member (used, ud) != NULL_TREE)
4323 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4325 ancestor = namespace_ancestor (current_decl_namespace (), used);
4326 ud = current_binding_level->using_directives;
4327 ud = tree_cons (used, ancestor, ud);
4328 current_binding_level->using_directives = ud;
4330 /* Recursively add all namespaces used. */
4331 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4332 push_using_directive (TREE_PURPOSE (iter));
4334 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4337 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4338 other definitions already in place. We get around this by making
4339 the value of the identifier point to a list of all the things that
4340 want to be referenced by that name. It is then up to the users of
4341 that name to decide what to do with that list.
4343 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4344 DECL_TEMPLATE_RESULT. It is dealt with the same way.
4346 FLAGS is a bitwise-or of the following values:
4347 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4348 namespace scope.
4349 PUSH_USING: DECL is being pushed as the result of a using
4350 declaration.
4352 The value returned may be a previous declaration if we guessed wrong
4353 about what language DECL should belong to (C or C++). Otherwise,
4354 it's always DECL (and never something that's not a _DECL). */
4356 tree
4357 push_overloaded_decl (tree decl, int flags)
4359 tree name = DECL_NAME (decl);
4360 tree old;
4361 tree new_binding;
4362 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4364 timevar_push (TV_NAME_LOOKUP);
4365 if (doing_global)
4366 old = namespace_binding (name, DECL_CONTEXT (decl));
4367 else
4368 old = lookup_name_current_level (name);
4370 if (old)
4372 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4374 tree t = TREE_TYPE (old);
4375 if (IS_AGGR_TYPE (t) && warn_shadow
4376 && (! DECL_IN_SYSTEM_HEADER (decl)
4377 || ! DECL_IN_SYSTEM_HEADER (old)))
4378 warning ("`%#D' hides constructor for `%#T'", decl, t);
4379 old = NULL_TREE;
4381 else if (is_overloaded_fn (old))
4383 tree tmp;
4385 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4387 tree fn = OVL_CURRENT (tmp);
4389 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4390 && !(flags & PUSH_USING)
4391 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4392 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4393 error ("`%#D' conflicts with previous using declaration `%#D'",
4394 decl, fn);
4396 if (duplicate_decls (decl, fn))
4397 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4400 else if (old == error_mark_node)
4401 /* Ignore the undefined symbol marker. */
4402 old = NULL_TREE;
4403 else
4405 cp_error_at ("previous non-function declaration `%#D'", old);
4406 error ("conflicts with function declaration `%#D'", decl);
4407 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4411 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4413 if (old && TREE_CODE (old) != OVERLOAD)
4414 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4415 else
4416 new_binding = ovl_cons (decl, old);
4417 if (flags & PUSH_USING)
4418 OVL_USED (new_binding) = 1;
4420 else
4421 /* NAME is not ambiguous. */
4422 new_binding = decl;
4424 if (doing_global)
4425 set_namespace_binding (name, current_namespace, new_binding);
4426 else
4428 /* We only create an OVERLOAD if there was a previous binding at
4429 this level, or if decl is a template. In the former case, we
4430 need to remove the old binding and replace it with the new
4431 binding. We must also run through the NAMES on the binding
4432 level where the name was bound to update the chain. */
4434 if (TREE_CODE (new_binding) == OVERLOAD && old)
4436 tree *d;
4438 for (d = &BINDING_SCOPE (IDENTIFIER_BINDING (name))->names;
4440 d = &TREE_CHAIN (*d))
4441 if (*d == old
4442 || (TREE_CODE (*d) == TREE_LIST
4443 && TREE_VALUE (*d) == old))
4445 if (TREE_CODE (*d) == TREE_LIST)
4446 /* Just replace the old binding with the new. */
4447 TREE_VALUE (*d) = new_binding;
4448 else
4449 /* Build a TREE_LIST to wrap the OVERLOAD. */
4450 *d = tree_cons (NULL_TREE, new_binding,
4451 TREE_CHAIN (*d));
4453 /* And update the cxx_binding node. */
4454 BINDING_VALUE (IDENTIFIER_BINDING (name))
4455 = new_binding;
4456 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4459 /* We should always find a previous binding in this case. */
4460 abort ();
4463 /* Install the new binding. */
4464 push_local_binding (name, new_binding, flags);
4467 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4470 /* Generate an implicit declaration for identifier FUNCTIONID
4471 as a function of type int (). Print a warning if appropriate. */
4473 tree
4474 implicitly_declare (tree functionid)
4476 register tree decl;
4478 /* We used to reuse an old implicit decl here,
4479 but this loses with inline functions because it can clobber
4480 the saved decl chains. */
4481 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4483 DECL_EXTERNAL (decl) = 1;
4484 TREE_PUBLIC (decl) = 1;
4486 /* ISO standard says implicit declarations are in the innermost block.
4487 So we record the decl in the standard fashion. */
4488 pushdecl (decl);
4489 rest_of_decl_compilation (decl, NULL, 0, 0);
4491 if (warn_implicit
4492 /* Only one warning per identifier. */
4493 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4495 pedwarn ("implicit declaration of function `%#D'", decl);
4498 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4500 return decl;
4503 /* Return zero if the declaration NEWDECL is valid
4504 when the declaration OLDDECL (assumed to be for the same name)
4505 has already been seen.
4506 Otherwise return an error message format string with a %s
4507 where the identifier should go. */
4509 static const char *
4510 redeclaration_error_message (tree newdecl, tree olddecl)
4512 if (TREE_CODE (newdecl) == TYPE_DECL)
4514 /* Because C++ can put things into name space for free,
4515 constructs like "typedef struct foo { ... } foo"
4516 would look like an erroneous redeclaration. */
4517 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4518 return 0;
4519 else
4520 return "redefinition of `%#D'";
4522 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4524 /* If this is a pure function, its olddecl will actually be
4525 the original initialization to `0' (which we force to call
4526 abort()). Don't complain about redefinition in this case. */
4527 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4528 return 0;
4530 /* If both functions come from different namespaces, this is not
4531 a redeclaration - this is a conflict with a used function. */
4532 if (DECL_NAMESPACE_SCOPE_P (olddecl)
4533 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4534 return "`%D' conflicts with used function";
4536 /* We'll complain about linkage mismatches in
4537 warn_extern_redeclared_static. */
4539 /* Defining the same name twice is no good. */
4540 if (DECL_INITIAL (olddecl) != NULL_TREE
4541 && DECL_INITIAL (newdecl) != NULL_TREE)
4543 if (DECL_NAME (olddecl) == NULL_TREE)
4544 return "`%#D' not declared in class";
4545 else
4546 return "redefinition of `%#D'";
4548 return 0;
4550 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4552 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4553 && (DECL_TEMPLATE_RESULT (newdecl)
4554 != DECL_TEMPLATE_RESULT (olddecl))
4555 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4556 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4557 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4558 && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4559 && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4560 return "redefinition of `%#D'";
4561 return 0;
4563 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4565 /* Objects declared at top level: */
4566 /* If at least one is a reference, it's ok. */
4567 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4568 return 0;
4569 /* Reject two definitions. */
4570 return "redefinition of `%#D'";
4572 else
4574 /* Objects declared with block scope: */
4575 /* Reject two definitions, and reject a definition
4576 together with an external reference. */
4577 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4578 return "redeclaration of `%#D'";
4579 return 0;
4583 /* Create a new label, named ID. */
4585 static tree
4586 make_label_decl (tree id, int local_p)
4588 tree decl;
4590 decl = build_decl (LABEL_DECL, id, void_type_node);
4591 if (expanding_p)
4592 /* Make sure every label has an rtx. */
4593 label_rtx (decl);
4595 DECL_CONTEXT (decl) = current_function_decl;
4596 DECL_MODE (decl) = VOIDmode;
4597 C_DECLARED_LABEL_FLAG (decl) = local_p;
4599 /* Say where one reference is to the label, for the sake of the
4600 error if it is not defined. */
4601 DECL_SOURCE_LINE (decl) = lineno;
4602 DECL_SOURCE_FILE (decl) = input_filename;
4604 /* Record the fact that this identifier is bound to this label. */
4605 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4607 return decl;
4610 /* Record this label on the list of used labels so that we can check
4611 at the end of the function to see whether or not the label was
4612 actually defined, and so we can check when the label is defined whether
4613 this use is valid. */
4615 static void
4616 use_label (tree decl)
4618 if (named_label_uses == NULL
4619 || named_label_uses->names_in_scope != current_binding_level->names
4620 || named_label_uses->label_decl != decl)
4622 struct named_label_use_list *new_ent;
4623 new_ent = ((struct named_label_use_list *)
4624 ggc_alloc (sizeof (struct named_label_use_list)));
4625 new_ent->label_decl = decl;
4626 new_ent->names_in_scope = current_binding_level->names;
4627 new_ent->binding_level = current_binding_level;
4628 new_ent->o_goto_locus.line = lineno;
4629 new_ent->o_goto_locus.file = input_filename;
4630 new_ent->next = named_label_uses;
4631 named_label_uses = new_ent;
4635 /* Look for a label named ID in the current function. If one cannot
4636 be found, create one. (We keep track of used, but undefined,
4637 labels, and complain about them at the end of a function.) */
4639 tree
4640 lookup_label (tree id)
4642 tree decl;
4643 struct named_label_list *ent;
4645 timevar_push (TV_NAME_LOOKUP);
4646 /* You can't use labels at global scope. */
4647 if (current_function_decl == NULL_TREE)
4649 error ("label `%s' referenced outside of any function",
4650 IDENTIFIER_POINTER (id));
4651 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4654 /* See if we've already got this label. */
4655 decl = IDENTIFIER_LABEL_VALUE (id);
4656 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4657 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4659 /* Record this label on the list of labels used in this function.
4660 We do this before calling make_label_decl so that we get the
4661 IDENTIFIER_LABEL_VALUE before the new label is declared. */
4662 ent = ((struct named_label_list *)
4663 ggc_alloc_cleared (sizeof (struct named_label_list)));
4664 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4665 ent->next = named_labels;
4666 named_labels = ent;
4668 /* We need a new label. */
4669 decl = make_label_decl (id, /*local_p=*/0);
4671 /* Now fill in the information we didn't have before. */
4672 ent->label_decl = decl;
4674 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4677 /* Declare a local label named ID. */
4679 tree
4680 declare_local_label (tree id)
4682 tree decl;
4684 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4685 this scope we can restore the old value of
4686 IDENTIFIER_TYPE_VALUE. */
4687 current_binding_level->shadowed_labels
4688 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4689 current_binding_level->shadowed_labels);
4690 /* Look for the label. */
4691 decl = make_label_decl (id, /*local_p=*/1);
4692 /* Now fill in the information we didn't have before. */
4693 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4695 return decl;
4698 /* Returns nonzero if it is ill-formed to jump past the declaration of
4699 DECL. Returns 2 if it's also a real problem. */
4701 static int
4702 decl_jump_unsafe (tree decl)
4704 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4705 return 0;
4707 if (DECL_INITIAL (decl) == NULL_TREE
4708 && pod_type_p (TREE_TYPE (decl)))
4709 return 0;
4711 /* This is really only important if we're crossing an initialization.
4712 The POD stuff is just pedantry; why should it matter if the class
4713 contains a field of pointer to member type? */
4714 if (DECL_INITIAL (decl)
4715 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4716 return 2;
4717 return 1;
4720 /* Check that a single previously seen jump to a newly defined label
4721 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4722 the jump context; NAMES are the names in scope in LEVEL at the jump
4723 context; FILE and LINE are the source position of the jump or 0. */
4725 static void
4726 check_previous_goto_1 (tree decl,
4727 struct cp_binding_level* level,
4728 tree names, const location_t *locus)
4730 int identified = 0;
4731 int saw_eh = 0;
4732 struct cp_binding_level *b = current_binding_level;
4733 for (; b; b = b->level_chain)
4735 tree new_decls = b->names;
4736 tree old_decls = (b == level ? names : NULL_TREE);
4737 for (; new_decls != old_decls;
4738 new_decls = TREE_CHAIN (new_decls))
4740 int problem = decl_jump_unsafe (new_decls);
4741 if (! problem)
4742 continue;
4744 if (! identified)
4746 if (decl)
4747 pedwarn ("jump to label `%D'", decl);
4748 else
4749 pedwarn ("jump to case label");
4751 if (locus)
4752 pedwarn ("%H from here", locus);
4753 identified = 1;
4756 if (problem > 1)
4757 cp_error_at (" crosses initialization of `%#D'",
4758 new_decls);
4759 else
4760 cp_pedwarn_at (" enters scope of non-POD `%#D'",
4761 new_decls);
4764 if (b == level)
4765 break;
4766 if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
4768 if (! identified)
4770 if (decl)
4771 pedwarn ("jump to label `%D'", decl);
4772 else
4773 pedwarn ("jump to case label");
4775 if (locus)
4776 pedwarn ("%H from here", locus);
4777 identified = 1;
4779 if (b->is_try_scope)
4780 error (" enters try block");
4781 else
4782 error (" enters catch block");
4783 saw_eh = 1;
4788 static void
4789 check_previous_goto (struct named_label_use_list* use)
4791 check_previous_goto_1 (use->label_decl, use->binding_level,
4792 use->names_in_scope, &use->o_goto_locus);
4795 static void
4796 check_switch_goto (struct cp_binding_level* level)
4798 check_previous_goto_1 (NULL_TREE, level, level->names, NULL);
4801 /* Check that any previously seen jumps to a newly defined label DECL
4802 are OK. Called by define_label. */
4804 static void
4805 check_previous_gotos (tree decl)
4807 struct named_label_use_list **usep;
4809 if (! TREE_USED (decl))
4810 return;
4812 for (usep = &named_label_uses; *usep; )
4814 struct named_label_use_list *use = *usep;
4815 if (use->label_decl == decl)
4817 check_previous_goto (use);
4818 *usep = use->next;
4820 else
4821 usep = &(use->next);
4825 /* Check that a new jump to a label DECL is OK. Called by
4826 finish_goto_stmt. */
4828 void
4829 check_goto (tree decl)
4831 int identified = 0;
4832 tree bad;
4833 struct named_label_list *lab;
4835 /* We can't know where a computed goto is jumping. So we assume
4836 that it's OK. */
4837 if (! DECL_P (decl))
4838 return;
4840 /* If the label hasn't been defined yet, defer checking. */
4841 if (! DECL_INITIAL (decl))
4843 use_label (decl);
4844 return;
4847 for (lab = named_labels; lab; lab = lab->next)
4848 if (decl == lab->label_decl)
4849 break;
4851 /* If the label is not on named_labels it's a gcc local label, so
4852 it must be in an outer scope, so jumping to it is always OK. */
4853 if (lab == 0)
4854 return;
4856 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4857 && !identified)
4859 cp_pedwarn_at ("jump to label `%D'", decl);
4860 pedwarn (" from here");
4861 identified = 1;
4864 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4866 tree b = TREE_VALUE (bad);
4867 int u = decl_jump_unsafe (b);
4869 if (u > 1 && DECL_ARTIFICIAL (b))
4870 /* Can't skip init of __exception_info. */
4871 cp_error_at (" enters catch block", b);
4872 else if (u > 1)
4873 cp_error_at (" skips initialization of `%#D'", b);
4874 else
4875 cp_pedwarn_at (" enters scope of non-POD `%#D'", b);
4878 if (lab->in_try_scope)
4879 error (" enters try block");
4880 else if (lab->in_catch_scope)
4881 error (" enters catch block");
4884 /* Define a label, specifying the location in the source file.
4885 Return the LABEL_DECL node for the label, if the definition is valid.
4886 Otherwise return 0. */
4888 tree
4889 define_label (const char* filename, int line, tree name)
4891 tree decl = lookup_label (name);
4892 struct named_label_list *ent;
4893 register struct cp_binding_level *p;
4895 timevar_push (TV_NAME_LOOKUP);
4896 for (ent = named_labels; ent; ent = ent->next)
4897 if (ent->label_decl == decl)
4898 break;
4900 /* After labels, make any new cleanups in the function go into their
4901 own new (temporary) binding contour. */
4902 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
4903 p->more_cleanups_ok = 0;
4905 if (name == get_identifier ("wchar_t"))
4906 pedwarn ("label named wchar_t");
4908 if (DECL_INITIAL (decl) != NULL_TREE)
4910 error ("duplicate label `%D'", decl);
4911 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4913 else
4915 /* Mark label as having been defined. */
4916 DECL_INITIAL (decl) = error_mark_node;
4917 /* Say where in the source. */
4918 DECL_SOURCE_FILE (decl) = filename;
4919 DECL_SOURCE_LINE (decl) = line;
4920 if (ent)
4922 ent->names_in_scope = current_binding_level->names;
4923 ent->binding_level = current_binding_level;
4925 check_previous_gotos (decl);
4926 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4928 timevar_pop (TV_NAME_LOOKUP);
4931 struct cp_switch
4933 struct cp_binding_level *level;
4934 struct cp_switch *next;
4935 /* The SWITCH_STMT being built. */
4936 tree switch_stmt;
4937 /* A splay-tree mapping the low element of a case range to the high
4938 element, or NULL_TREE if there is no high element. Used to
4939 determine whether or not a new case label duplicates an old case
4940 label. We need a tree, rather than simply a hash table, because
4941 of the GNU case range extension. */
4942 splay_tree cases;
4945 /* A stack of the currently active switch statements. The innermost
4946 switch statement is on the top of the stack. There is no need to
4947 mark the stack for garbage collection because it is only active
4948 during the processing of the body of a function, and we never
4949 collect at that point. */
4951 static struct cp_switch *switch_stack;
4953 /* Called right after a switch-statement condition is parsed.
4954 SWITCH_STMT is the switch statement being parsed. */
4956 void
4957 push_switch (tree switch_stmt)
4959 struct cp_switch *p
4960 = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
4961 p->level = current_binding_level;
4962 p->next = switch_stack;
4963 p->switch_stmt = switch_stmt;
4964 p->cases = splay_tree_new (case_compare, NULL, NULL);
4965 switch_stack = p;
4968 void
4969 pop_switch (void)
4971 struct cp_switch *cs;
4973 cs = switch_stack;
4974 splay_tree_delete (cs->cases);
4975 switch_stack = switch_stack->next;
4976 free (cs);
4979 /* Note that we've seen a definition of a case label, and complain if this
4980 is a bad place for one. */
4982 tree
4983 finish_case_label (tree low_value, tree high_value)
4985 tree cond, r;
4986 register struct cp_binding_level *p;
4988 if (! switch_stack)
4990 if (high_value)
4991 error ("case label not within a switch statement");
4992 else if (low_value)
4993 error ("case label `%E' not within a switch statement",
4994 low_value);
4995 else
4996 error ("`default' label not within a switch statement");
4997 return NULL_TREE;
5000 if (processing_template_decl)
5002 tree label;
5004 /* For templates, just add the case label; we'll do semantic
5005 analysis at instantiation-time. */
5006 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5007 return add_stmt (build_case_label (low_value, high_value, label));
5010 /* Find the condition on which this switch statement depends. */
5011 cond = SWITCH_COND (switch_stack->switch_stmt);
5012 if (cond && TREE_CODE (cond) == TREE_LIST)
5013 cond = TREE_VALUE (cond);
5015 r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5017 check_switch_goto (switch_stack->level);
5019 /* After labels, make any new cleanups in the function go into their
5020 own new (temporary) binding contour. */
5021 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5022 p->more_cleanups_ok = 0;
5024 return r;
5027 /* Return the list of declarations of the current level.
5028 Note that this list is in reverse order unless/until
5029 you nreverse it; and when you do nreverse it, you must
5030 store the result back using `storedecls' or you will lose. */
5032 tree
5033 getdecls (void)
5035 return current_binding_level->names;
5038 /* Return the list of type-tags (for structs, etc) of the current level. */
5040 tree
5041 gettags (void)
5043 return current_binding_level->tags;
5046 /* Store the list of declarations of the current level.
5047 This is done for the parameter declarations of a function being defined,
5048 after they are modified in the light of any missing parameters. */
5050 static void
5051 storedecls (tree decls)
5053 current_binding_level->names = decls;
5056 /* Similarly, store the list of tags of the current level. */
5058 void
5059 storetags (tree tags)
5061 current_binding_level->tags = tags;
5064 /* Return the type that should be used when TYPE's name is preceded
5065 by a tag such as 'struct' or 'union', or null if the name cannot
5066 be used in this way.
5068 For example, when processing the third line of:
5070 struct A;
5071 typedef struct A A;
5072 struct A;
5074 lookup of A will find the typedef. Given A's typedef, this function
5075 will return the type associated with "struct A". For the tag to be
5076 anything other than TYPE, TYPE must be a typedef whose original type
5077 has the same name and context as TYPE itself.
5079 It is not valid for a typedef of an anonymous type to be used with
5080 an explicit tag:
5082 typedef struct { ... } B;
5083 struct B;
5085 Return null for this case. */
5087 static tree
5088 follow_tag_typedef (tree type)
5090 tree original;
5092 original = original_type (type);
5093 if (! TYPE_NAME (original))
5094 return NULL_TREE;
5095 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5096 && (CP_DECL_CONTEXT (TYPE_NAME (original))
5097 == CP_DECL_CONTEXT (TYPE_NAME (type)))
5098 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5099 return original;
5100 else
5101 return NULL_TREE;
5104 /* Given NAME, an IDENTIFIER_NODE,
5105 return the structure (or union or enum) definition for that name.
5106 Searches binding levels from BINDING_SCOPE up to the global level.
5107 If THISLEVEL_ONLY is nonzero, searches only the specified context
5108 (but skips any tag-transparent contexts to find one that is
5109 meaningful for tags).
5110 FORM says which kind of type the caller wants;
5111 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5112 If the wrong kind of type is found, and it's not a template, an error is
5113 reported. */
5115 static tree
5116 lookup_tag (enum tree_code form, tree name,
5117 struct cp_binding_level* binding_level, int thislevel_only)
5119 register struct cp_binding_level *level;
5120 /* Nonzero if, we should look past a template parameter level, even
5121 if THISLEVEL_ONLY. */
5122 int allow_template_parms_p = 1;
5124 timevar_push (TV_NAME_LOOKUP);
5125 for (level = binding_level; level; level = level->level_chain)
5127 register tree tail;
5128 if (ANON_AGGRNAME_P (name))
5129 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5131 /* There's no need for error checking here, because
5132 anon names are unique throughout the compilation. */
5133 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5134 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5136 else if (level->namespace_p)
5137 /* Do namespace lookup. */
5138 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5140 cxx_binding *binding =
5141 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
5142 tree old;
5144 /* If we just skipped past a template parameter level,
5145 even though THISLEVEL_ONLY, and we find a template
5146 class declaration, then we use the _TYPE node for the
5147 template. See the example below. */
5148 if (thislevel_only && !allow_template_parms_p
5149 && binding && BINDING_VALUE (binding)
5150 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (binding)))
5151 old = TREE_TYPE (BINDING_VALUE (binding));
5152 else if (binding)
5153 old = BINDING_TYPE (binding);
5154 else
5155 old = NULL_TREE;
5157 if (old)
5159 /* We've found something at this binding level. If it is
5160 a typedef, extract the tag it refers to. Lookup fails
5161 if the typedef doesn't refer to a taggable type. */
5162 old = follow_tag_typedef (old);
5163 if (!old)
5164 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5165 if (TREE_CODE (old) != form
5166 && (form == ENUMERAL_TYPE
5167 || TREE_CODE (old) == ENUMERAL_TYPE))
5169 error ("`%#D' redeclared as %C", old, form);
5170 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5172 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
5174 if (thislevel_only || tail == global_namespace)
5175 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5177 else
5178 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5180 if (TREE_PURPOSE (tail) == name)
5182 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5184 if (code != form
5185 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5187 /* Definition isn't the kind we were looking for. */
5188 error ("`%#D' redeclared as %C", TREE_VALUE (tail), form);
5189 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5191 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5194 if (thislevel_only && ! level->tag_transparent)
5196 if (level->template_parms_p && allow_template_parms_p)
5198 /* We must deal with cases like this:
5200 template <class T> struct S;
5201 template <class T> struct S {};
5203 When looking up `S', for the second declaration, we
5204 would like to find the first declaration. But, we
5205 are in the pseudo-global level created for the
5206 template parameters, rather than the (surrounding)
5207 namespace level. Thus, we keep going one more level,
5208 even though THISLEVEL_ONLY is nonzero. */
5209 allow_template_parms_p = 0;
5210 continue;
5212 else
5213 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5216 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5219 /* Given a type, find the tag that was defined for it and return the tag name.
5220 Otherwise return 0. However, the value can never be 0
5221 in the cases in which this is used.
5223 C++: If NAME is nonzero, this is the new name to install. This is
5224 done when replacing anonymous tags with real tag names. */
5226 static tree
5227 lookup_tag_reverse (tree type, tree name)
5229 register struct cp_binding_level *level;
5231 timevar_push (TV_NAME_LOOKUP);
5232 for (level = current_binding_level; level; level = level->level_chain)
5234 register tree tail;
5235 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5237 if (TREE_VALUE (tail) == type)
5239 if (name)
5240 TREE_PURPOSE (tail) = name;
5241 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_PURPOSE (tail));
5245 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5248 /* Look up NAME in the NAMESPACE. */
5250 tree
5251 lookup_namespace_name (tree namespace, tree name)
5253 tree val;
5254 tree template_id = NULL_TREE;
5255 cxx_binding binding;
5257 timevar_push (TV_NAME_LOOKUP);
5258 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5260 if (TREE_CODE (name) == NAMESPACE_DECL)
5261 /* This happens for A::B<int> when B is a namespace. */
5262 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5263 else if (TREE_CODE (name) == TEMPLATE_DECL)
5265 /* This happens for A::B where B is a template, and there are no
5266 template arguments. */
5267 error ("invalid use of `%D'", name);
5268 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5271 namespace = ORIGINAL_NAMESPACE (namespace);
5273 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5275 template_id = name;
5276 name = TREE_OPERAND (name, 0);
5277 if (TREE_CODE (name) == OVERLOAD)
5278 name = DECL_NAME (OVL_CURRENT (name));
5279 else if (DECL_P (name))
5280 name = DECL_NAME (name);
5283 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5285 cxx_binding_clear (&binding);
5286 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
5287 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5289 if (binding.value)
5291 val = binding.value;
5293 if (template_id)
5295 if (DECL_CLASS_TEMPLATE_P (val))
5296 val = lookup_template_class (val,
5297 TREE_OPERAND (template_id, 1),
5298 /*in_decl=*/NULL_TREE,
5299 /*context=*/NULL_TREE,
5300 /*entering_scope=*/0,
5301 tf_error | tf_warning);
5302 else if (DECL_FUNCTION_TEMPLATE_P (val)
5303 || TREE_CODE (val) == OVERLOAD)
5304 val = lookup_template_function (val,
5305 TREE_OPERAND (template_id, 1));
5306 else
5308 error ("`%D::%D' is not a template",
5309 namespace, name);
5310 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5314 /* If we have a single function from a using decl, pull it out. */
5315 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5316 val = OVL_FUNCTION (val);
5318 /* Ignore built-in functions that haven't been prototyped yet. */
5319 if (!val || !DECL_P(val)
5320 || !DECL_LANG_SPECIFIC(val)
5321 || !DECL_ANTICIPATED (val))
5322 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5325 error ("`%D' undeclared in namespace `%D'", name, namespace);
5326 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5329 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5331 static hashval_t
5332 typename_hash (const void* k)
5334 hashval_t hash;
5335 tree t = (tree) k;
5337 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5338 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5340 return hash;
5343 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5345 static int
5346 typename_compare (const void * k1, const void * k2)
5348 tree t1;
5349 tree t2;
5350 tree d1;
5351 tree d2;
5353 t1 = (tree) k1;
5354 t2 = (tree) k2;
5355 d1 = TYPE_NAME (t1);
5356 d2 = TYPE_NAME (t2);
5358 return (DECL_NAME (d1) == DECL_NAME (d2)
5359 && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5360 && ((TREE_TYPE (t1) != NULL_TREE)
5361 == (TREE_TYPE (t2) != NULL_TREE))
5362 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5363 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5366 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5367 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5368 is non-NULL, this type is being created by the implicit typename
5369 extension, and BASE_TYPE is a type named `t' in some base class of
5370 `T' which depends on template parameters.
5372 Returns the new TYPENAME_TYPE. */
5374 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5376 tree
5377 build_typename_type (tree context, tree name, tree fullname)
5379 tree t;
5380 tree d;
5381 PTR *e;
5383 if (typename_htab == NULL)
5385 typename_htab = htab_create_ggc (61, &typename_hash,
5386 &typename_compare, NULL);
5389 /* Build the TYPENAME_TYPE. */
5390 t = make_aggr_type (TYPENAME_TYPE);
5391 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5392 TYPENAME_TYPE_FULLNAME (t) = fullname;
5394 /* Build the corresponding TYPE_DECL. */
5395 d = build_decl (TYPE_DECL, name, t);
5396 TYPE_NAME (TREE_TYPE (d)) = d;
5397 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5398 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5399 DECL_ARTIFICIAL (d) = 1;
5401 /* See if we already have this type. */
5402 e = htab_find_slot (typename_htab, t, INSERT);
5403 if (*e)
5404 t = (tree) *e;
5405 else
5406 *e = t;
5408 return t;
5411 /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type,
5412 unless an error occurs, in which case error_mark_node is returned.
5413 If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5414 set, we return that, rather than the _TYPE it corresponds to, in
5415 other cases we look through the type decl. If TF_ERROR is set,
5416 complain about errors, otherwise be quiet. */
5418 tree
5419 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5421 tree fullname;
5423 if (TYPE_P (name))
5425 if (!(TYPE_LANG_SPECIFIC (name)
5426 && (CLASSTYPE_IS_TEMPLATE (name)
5427 || CLASSTYPE_USE_TEMPLATE (name))))
5428 name = TYPE_IDENTIFIER (name);
5429 else
5430 /* Create a TEMPLATE_ID_EXPR for the type. */
5431 name = build_nt (TEMPLATE_ID_EXPR,
5432 CLASSTYPE_TI_TEMPLATE (name),
5433 CLASSTYPE_TI_ARGS (name));
5435 else if (TREE_CODE (name) == TYPE_DECL)
5436 name = DECL_NAME (name);
5438 fullname = name;
5440 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5442 name = TREE_OPERAND (name, 0);
5443 if (TREE_CODE (name) == TEMPLATE_DECL)
5444 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5446 if (TREE_CODE (name) == TEMPLATE_DECL)
5448 error ("`%D' used without template parameters", name);
5449 return error_mark_node;
5451 if (TREE_CODE (name) != IDENTIFIER_NODE)
5452 abort ();
5454 if (TREE_CODE (context) == NAMESPACE_DECL)
5456 /* We can get here from typename_sub0 in the explicit_template_type
5457 expansion. Just fail. */
5458 if (complain & tf_error)
5459 error ("no class template named `%#T' in `%#T'",
5460 name, context);
5461 return error_mark_node;
5464 if (! uses_template_parms (context)
5465 || currently_open_class (context))
5467 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5469 tree tmpl = NULL_TREE;
5470 if (IS_AGGR_TYPE (context))
5471 tmpl = lookup_field (context, name, 0, false);
5472 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5474 if (complain & tf_error)
5475 error ("no class template named `%#T' in `%#T'",
5476 name, context);
5477 return error_mark_node;
5480 if (complain & tf_error)
5482 if (complain & tf_parsing)
5483 perform_or_defer_access_check (context, tmpl);
5484 else
5485 enforce_access (context, tmpl);
5488 return lookup_template_class (tmpl,
5489 TREE_OPERAND (fullname, 1),
5490 NULL_TREE, context,
5491 /*entering_scope=*/0,
5492 tf_error | tf_warning);
5494 else
5496 tree t;
5498 if (!IS_AGGR_TYPE (context))
5500 if (complain & tf_error)
5501 error ("no type named `%#T' in `%#T'", name, context);
5502 return error_mark_node;
5505 t = lookup_field (context, name, 0, true);
5506 if (t)
5508 if (TREE_CODE (t) != TYPE_DECL)
5510 if (complain & tf_error)
5511 error ("no type named `%#T' in `%#T'", name, context);
5512 return error_mark_node;
5515 if (complain & tf_error)
5517 if (complain & tf_parsing)
5518 perform_or_defer_access_check (context, t);
5519 else
5520 enforce_access (context, t);
5523 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5524 t = TREE_TYPE (t);
5526 return t;
5531 /* If the CONTEXT is not a template type, then either the field is
5532 there now or its never going to be. */
5533 if (!uses_template_parms (context))
5535 if (complain & tf_error)
5536 error ("no type named `%#T' in `%#T'", name, context);
5537 return error_mark_node;
5540 return build_typename_type (context, name, fullname);
5543 /* Resolve `CONTEXT::template NAME'. Returns an appropriate type,
5544 unless an error occurs, in which case error_mark_node is returned.
5545 If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5546 corresponds to. If COMPLAIN zero, don't complain about any errors
5547 that occur. */
5549 tree
5550 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5552 tree t;
5553 tree d;
5555 if (TYPE_P (name))
5556 name = TYPE_IDENTIFIER (name);
5557 else if (DECL_P (name))
5558 name = DECL_NAME (name);
5559 if (TREE_CODE (name) != IDENTIFIER_NODE)
5560 abort ();
5562 if (!uses_template_parms (context)
5563 || currently_open_class (context))
5565 tree tmpl = NULL_TREE;
5567 if (IS_AGGR_TYPE (context))
5568 tmpl = lookup_field (context, name, 0, false);
5570 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5572 if (complain & tf_error)
5573 error ("no class template named `%#T' in `%#T'", name, context);
5574 return error_mark_node;
5577 if (complain & tf_error)
5579 if (complain & tf_parsing)
5580 perform_or_defer_access_check (context, tmpl);
5581 else
5582 enforce_access (context, tmpl);
5585 return tmpl;
5588 /* Build the UNBOUND_CLASS_TEMPLATE. */
5589 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5590 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5591 TREE_TYPE (t) = NULL_TREE;
5593 /* Build the corresponding TEMPLATE_DECL. */
5594 d = build_decl (TEMPLATE_DECL, name, t);
5595 TYPE_NAME (TREE_TYPE (d)) = d;
5596 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5597 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5598 DECL_ARTIFICIAL (d) = 1;
5600 return t;
5603 /* Select the right _DECL from multiple choices. */
5605 static tree
5606 select_decl (cxx_binding *binding, int flags)
5608 tree val;
5609 val = BINDING_VALUE (binding);
5611 timevar_push (TV_NAME_LOOKUP);
5612 if (LOOKUP_NAMESPACES_ONLY (flags))
5614 /* We are not interested in types. */
5615 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5616 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5617 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5620 /* If we could have a type and
5621 we have nothing or we need a type and have none. */
5622 if (BINDING_TYPE (binding)
5623 && (!val || ((flags & LOOKUP_PREFER_TYPES)
5624 && TREE_CODE (val) != TYPE_DECL)))
5625 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5626 /* Don't return non-types if we really prefer types. */
5627 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
5628 && (TREE_CODE (val) != TEMPLATE_DECL
5629 || !DECL_CLASS_TEMPLATE_P (val)))
5630 val = NULL_TREE;
5632 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5635 /* Unscoped lookup of a global: iterate over current namespaces,
5636 considering using-directives. If SPACESP is non-NULL, store a list
5637 of the namespaces we've considered in it. */
5639 tree
5640 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5642 tree initial = current_decl_namespace ();
5643 tree scope = initial;
5644 tree siter;
5645 struct cp_binding_level *level;
5646 tree val = NULL_TREE;
5647 cxx_binding binding;
5649 timevar_push (TV_NAME_LOOKUP);
5650 cxx_binding_clear (&binding);
5651 if (spacesp)
5652 *spacesp = NULL_TREE;
5654 for (; !val; scope = CP_DECL_CONTEXT (scope))
5656 cxx_binding *b =
5657 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
5658 if (spacesp)
5659 *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5661 /* Ignore anticipated built-in functions. */
5662 if (b && BINDING_VALUE (b)
5663 && DECL_P (BINDING_VALUE (b))
5664 && DECL_LANG_SPECIFIC (BINDING_VALUE (b))
5665 && DECL_ANTICIPATED (BINDING_VALUE (b)))
5666 /* Keep binding cleared. */;
5667 else if (b)
5669 /* Initialize binding for this context. */
5670 binding.value = BINDING_VALUE (b);
5671 binding.type = BINDING_TYPE (b);
5674 /* Add all _DECLs seen through local using-directives. */
5675 for (level = current_binding_level;
5676 !level->namespace_p;
5677 level = level->level_chain)
5678 if (!lookup_using_namespace (name, &binding, level->using_directives,
5679 scope, flags, spacesp))
5680 /* Give up because of error. */
5681 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5683 /* Add all _DECLs seen through global using-directives. */
5684 /* XXX local and global using lists should work equally. */
5685 siter = initial;
5686 while (1)
5688 if (!lookup_using_namespace (name, &binding,
5689 DECL_NAMESPACE_USING (siter),
5690 scope, flags, spacesp))
5691 /* Give up because of error. */
5692 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5693 if (siter == scope) break;
5694 siter = CP_DECL_CONTEXT (siter);
5697 val = select_decl (&binding, flags);
5698 if (scope == global_namespace)
5699 break;
5701 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5704 /* Combine prefer_type and namespaces_only into flags. */
5706 static int
5707 lookup_flags (int prefer_type, int namespaces_only)
5709 if (namespaces_only)
5710 return LOOKUP_PREFER_NAMESPACES;
5711 if (prefer_type > 1)
5712 return LOOKUP_PREFER_TYPES;
5713 if (prefer_type > 0)
5714 return LOOKUP_PREFER_BOTH;
5715 return 0;
5718 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5719 ignore it or not. Subroutine of lookup_name_real. */
5721 static tree
5722 qualify_lookup (tree val, int flags)
5724 if (val == NULL_TREE)
5725 return val;
5726 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5727 return val;
5728 if ((flags & LOOKUP_PREFER_TYPES) && TREE_CODE (val) == TYPE_DECL)
5729 return val;
5730 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5731 return NULL_TREE;
5732 return val;
5735 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5736 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
5737 bindings.
5739 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5740 declaration found. */
5742 tree
5743 lookup_qualified_name (tree scope, tree name, bool is_type_p, int flags)
5745 if (TREE_CODE (scope) == NAMESPACE_DECL)
5747 cxx_binding binding;
5749 cxx_binding_clear (&binding);
5750 flags |= LOOKUP_COMPLAIN;
5751 if (is_type_p)
5752 flags |= LOOKUP_PREFER_TYPES;
5753 if (!qualified_lookup_using_namespace (name, scope, &binding, flags))
5754 return NULL_TREE;
5755 return select_decl (&binding, flags);
5757 else
5758 return lookup_member (scope, name, 0, is_type_p);
5761 /* Check to see whether or not DECL is a variable that would have been
5762 in scope under the ARM, but is not in scope under the ANSI/ISO
5763 standard. If so, issue an error message. If name lookup would
5764 work in both cases, but return a different result, this function
5765 returns the result of ANSI/ISO lookup. Otherwise, it returns
5766 DECL. */
5768 tree
5769 check_for_out_of_scope_variable (tree decl)
5771 tree shadowed;
5773 /* We only care about out of scope variables. */
5774 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5775 return decl;
5777 shadowed = DECL_SHADOWED_FOR_VAR (decl);
5778 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5779 && DECL_DEAD_FOR_LOCAL (shadowed))
5780 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5781 if (!shadowed)
5782 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5783 if (shadowed)
5785 if (!DECL_ERROR_REPORTED (decl))
5787 warning ("name lookup of `%D' changed",
5788 DECL_NAME (decl));
5789 cp_warning_at (" matches this `%D' under ISO standard rules",
5790 shadowed);
5791 cp_warning_at (" matches this `%D' under old rules", decl);
5792 DECL_ERROR_REPORTED (decl) = 1;
5794 return shadowed;
5797 /* If we have already complained about this declaration, there's no
5798 need to do it again. */
5799 if (DECL_ERROR_REPORTED (decl))
5800 return decl;
5802 DECL_ERROR_REPORTED (decl) = 1;
5803 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5805 error ("name lookup of `%D' changed for new ISO `for' scoping",
5806 DECL_NAME (decl));
5807 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl);
5808 return error_mark_node;
5810 else
5812 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5813 DECL_NAME (decl));
5814 cp_pedwarn_at (" using obsolete binding at `%D'", decl);
5817 return decl;
5820 /* Look up NAME in the current binding level and its superiors in the
5821 namespace of variables, functions and typedefs. Return a ..._DECL
5822 node of some kind representing its definition if there is only one
5823 such declaration, or return a TREE_LIST with all the overloaded
5824 definitions if there are many, or return 0 if it is undefined.
5826 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5827 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5828 Otherwise we prefer non-TYPE_DECLs.
5830 If NONCLASS is nonzero, we don't look for the NAME in class scope,
5831 using IDENTIFIER_CLASS_VALUE. */
5833 tree
5834 lookup_name_real (tree name, int prefer_type, int nonclass,
5835 int namespaces_only, int flags)
5837 cxx_binding *iter;
5838 tree val = NULL_TREE;
5840 timevar_push (TV_NAME_LOOKUP);
5841 /* Conversion operators are handled specially because ordinary
5842 unqualified name lookup will not find template conversion
5843 operators. */
5844 if (IDENTIFIER_TYPENAME_P (name))
5846 struct cp_binding_level *level;
5848 for (level = current_binding_level;
5849 level && !level->namespace_p;
5850 level = level->level_chain)
5852 tree class_type;
5853 tree operators;
5855 /* A conversion operator can only be declared in a class
5856 scope. */
5857 if (level->parm_flag != 2)
5858 continue;
5860 /* Lookup the conversion operator in the class. */
5861 class_type = level->this_class;
5862 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5863 if (operators)
5864 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
5867 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5870 flags |= lookup_flags (prefer_type, namespaces_only);
5872 /* First, look in non-namespace scopes. */
5874 if (current_class_type == NULL_TREE)
5875 nonclass = 1;
5877 for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
5879 tree binding;
5881 if (!LOCAL_BINDING_P (iter) && nonclass)
5882 /* We're not looking for class-scoped bindings, so keep going. */
5883 continue;
5885 /* If this is the kind of thing we're looking for, we're done. */
5886 if (qualify_lookup (BINDING_VALUE (iter), flags))
5887 binding = BINDING_VALUE (iter);
5888 else if ((flags & LOOKUP_PREFER_TYPES)
5889 && qualify_lookup (BINDING_TYPE (iter), flags))
5890 binding = BINDING_TYPE (iter);
5891 else
5892 binding = NULL_TREE;
5894 if (binding)
5896 val = binding;
5897 break;
5901 /* Now lookup in namespace scopes. */
5902 if (!val)
5904 tree t = unqualified_namespace_lookup (name, flags, 0);
5905 if (t)
5906 val = t;
5909 if (val)
5911 /* If we have a single function from a using decl, pull it out. */
5912 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5913 val = OVL_FUNCTION (val);
5916 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5919 tree
5920 lookup_name_nonclass (tree name)
5922 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
5925 tree
5926 lookup_function_nonclass (tree name, tree args)
5928 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5931 tree
5932 lookup_name (tree name, int prefer_type)
5934 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
5937 /* Similar to `lookup_name' but look only in the innermost non-class
5938 binding level. */
5940 tree
5941 lookup_name_current_level (tree name)
5943 struct cp_binding_level *b;
5944 tree t = NULL_TREE;
5946 timevar_push (TV_NAME_LOOKUP);
5947 b = current_binding_level;
5948 while (b->parm_flag == 2)
5949 b = b->level_chain;
5951 if (b->namespace_p)
5953 t = IDENTIFIER_NAMESPACE_VALUE (name);
5955 /* extern "C" function() */
5956 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5957 t = TREE_VALUE (t);
5959 else if (IDENTIFIER_BINDING (name)
5960 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5962 while (1)
5964 if (BINDING_SCOPE (IDENTIFIER_BINDING (name)) == b)
5965 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
5967 if (b->keep == 2)
5968 b = b->level_chain;
5969 else
5970 break;
5974 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5977 /* Like lookup_name_current_level, but for types. */
5979 tree
5980 lookup_type_current_level (tree name)
5982 register tree t = NULL_TREE;
5984 timevar_push (TV_NAME_LOOKUP);
5985 my_friendly_assert (! current_binding_level->namespace_p, 980716);
5987 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5988 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5990 struct cp_binding_level *b = current_binding_level;
5991 while (1)
5993 if (purpose_member (name, b->type_shadowed))
5994 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
5995 REAL_IDENTIFIER_TYPE_VALUE (name));
5996 if (b->keep == 2)
5997 b = b->level_chain;
5998 else
5999 break;
6003 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6007 /* Push the declarations of builtin types into the namespace.
6008 RID_INDEX is the index of the builtin type
6009 in the array RID_POINTERS. NAME is the name used when looking
6010 up the builtin type. TYPE is the _TYPE node for the builtin type. */
6012 void
6013 record_builtin_type (enum rid rid_index,
6014 const char* name,
6015 tree type)
6017 tree rname = NULL_TREE, tname = NULL_TREE;
6018 tree tdecl = NULL_TREE;
6020 if ((int) rid_index < (int) RID_MAX)
6021 rname = ridpointers[(int) rid_index];
6022 if (name)
6023 tname = get_identifier (name);
6025 if (tname)
6027 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6028 set_identifier_type_value (tname, NULL_TREE);
6029 if ((int) rid_index < (int) RID_MAX)
6030 /* Built-in types live in the global namespace. */
6031 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6033 if (rname != NULL_TREE)
6035 if (tname != NULL_TREE)
6037 set_identifier_type_value (rname, NULL_TREE);
6038 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6040 else
6042 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6043 set_identifier_type_value (rname, NULL_TREE);
6048 /* Record one of the standard Java types.
6049 * Declare it as having the given NAME.
6050 * If SIZE > 0, it is the size of one of the integral types;
6051 * otherwise it is the negative of the size of one of the other types. */
6053 static tree
6054 record_builtin_java_type (const char* name, int size)
6056 tree type, decl;
6057 if (size > 0)
6058 type = make_signed_type (size);
6059 else if (size > -32)
6060 { /* "__java_char" or ""__java_boolean". */
6061 type = make_unsigned_type (-size);
6062 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6064 else
6065 { /* "__java_float" or ""__java_double". */
6066 type = make_node (REAL_TYPE);
6067 TYPE_PRECISION (type) = - size;
6068 layout_type (type);
6070 record_builtin_type (RID_MAX, name, type);
6071 decl = TYPE_NAME (type);
6073 /* Suppress generate debug symbol entries for these types,
6074 since for normal C++ they are just clutter.
6075 However, push_lang_context undoes this if extern "Java" is seen. */
6076 DECL_IGNORED_P (decl) = 1;
6078 TYPE_FOR_JAVA (type) = 1;
6079 return type;
6082 /* Push a type into the namespace so that the back-ends ignore it. */
6084 static void
6085 record_unknown_type (tree type, const char* name)
6087 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6088 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
6089 DECL_IGNORED_P (decl) = 1;
6090 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6091 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6092 TYPE_ALIGN (type) = 1;
6093 TYPE_USER_ALIGN (type) = 0;
6094 TYPE_MODE (type) = TYPE_MODE (void_type_node);
6097 /* An string for which we should create an IDENTIFIER_NODE at
6098 startup. */
6100 typedef struct predefined_identifier
6102 /* The name of the identifier. */
6103 const char *const name;
6104 /* The place where the IDENTIFIER_NODE should be stored. */
6105 tree *const node;
6106 /* Nonzero if this is the name of a constructor or destructor. */
6107 const int ctor_or_dtor_p;
6108 } predefined_identifier;
6110 /* Create all the predefined identifiers. */
6112 static void
6113 initialize_predefined_identifiers (void)
6115 const predefined_identifier *pid;
6117 /* A table of identifiers to create at startup. */
6118 static const predefined_identifier predefined_identifiers[] = {
6119 { "C++", &lang_name_cplusplus, 0 },
6120 { "C", &lang_name_c, 0 },
6121 { "Java", &lang_name_java, 0 },
6122 { CTOR_NAME, &ctor_identifier, 1 },
6123 { "__base_ctor", &base_ctor_identifier, 1 },
6124 { "__comp_ctor", &complete_ctor_identifier, 1 },
6125 { DTOR_NAME, &dtor_identifier, 1 },
6126 { "__comp_dtor", &complete_dtor_identifier, 1 },
6127 { "__base_dtor", &base_dtor_identifier, 1 },
6128 { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6129 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6130 { "nelts", &nelts_identifier, 0 },
6131 { THIS_NAME, &this_identifier, 0 },
6132 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6133 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6134 { "_vptr", &vptr_identifier, 0 },
6135 { "__vtt_parm", &vtt_parm_identifier, 0 },
6136 { "std", &std_identifier, 0 },
6137 { NULL, NULL, 0 }
6140 for (pid = predefined_identifiers; pid->name; ++pid)
6142 *pid->node = get_identifier (pid->name);
6143 if (pid->ctor_or_dtor_p)
6144 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6148 /* Create the predefined scalar types of C,
6149 and some nodes representing standard constants (0, 1, (void *)0).
6150 Initialize the global binding level.
6151 Make definitions for built-in primitive functions. */
6153 void
6154 cxx_init_decl_processing (void)
6156 tree void_ftype;
6157 tree void_ftype_ptr;
6159 /* Create all the identifiers we need. */
6160 initialize_predefined_identifiers ();
6162 /* Fill in back-end hooks. */
6163 lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6165 /* Create the global variables. */
6166 push_to_top_level ();
6168 /* Enter the global namespace. */
6169 my_friendly_assert (global_namespace == NULL_TREE, 375);
6170 push_namespace (get_identifier ("::"));
6171 global_namespace = current_namespace;
6172 current_lang_name = NULL_TREE;
6174 /* Adjust various flags based on command-line settings. */
6175 if (! flag_permissive && ! pedantic)
6176 flag_pedantic_errors = 1;
6177 if (!flag_no_inline)
6179 flag_inline_trees = 1;
6180 flag_no_inline = 1;
6182 if (flag_inline_functions)
6184 flag_inline_trees = 2;
6185 flag_inline_functions = 0;
6188 /* Force minimum function alignment if using the least significant
6189 bit of function pointers to store the virtual bit. */
6190 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6191 && force_align_functions_log < 1)
6192 force_align_functions_log = 1;
6194 /* Initially, C. */
6195 current_lang_name = lang_name_c;
6197 current_function_decl = NULL_TREE;
6198 current_binding_level = NULL_BINDING_LEVEL;
6199 free_binding_level = NULL_BINDING_LEVEL;
6201 build_common_tree_nodes (flag_signed_char);
6203 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6204 TREE_TYPE (error_mark_list) = error_mark_node;
6206 /* Make the binding_level structure for global names. */
6207 pushlevel (0);
6208 global_binding_level = current_binding_level;
6209 /* The global level is the namespace level of ::. */
6210 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6211 declare_namespace_level ();
6213 VARRAY_TREE_INIT (global_binding_level->static_decls,
6214 200,
6215 "Static declarations");
6217 /* Create the `std' namespace. */
6218 push_namespace (std_identifier);
6219 std_node = current_namespace;
6220 pop_namespace ();
6222 c_common_nodes_and_builtins ();
6224 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6225 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6226 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6227 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6228 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6229 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6230 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6231 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6233 integer_two_node = build_int_2 (2, 0);
6234 TREE_TYPE (integer_two_node) = integer_type_node;
6235 integer_three_node = build_int_2 (3, 0);
6236 TREE_TYPE (integer_three_node) = integer_type_node;
6238 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6239 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6240 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6241 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6242 TYPE_PRECISION (boolean_type_node) = 1;
6243 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6244 boolean_false_node = build_int_2 (0, 0);
6245 TREE_TYPE (boolean_false_node) = boolean_type_node;
6246 boolean_true_node = build_int_2 (1, 0);
6247 TREE_TYPE (boolean_true_node) = boolean_type_node;
6249 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6251 #if 0
6252 record_builtin_type (RID_MAX, NULL, string_type_node);
6253 #endif
6255 delta_type_node = ptrdiff_type_node;
6256 vtable_index_type = ptrdiff_type_node;
6258 vtt_parm_type = build_pointer_type (const_ptr_type_node);
6259 void_ftype = build_function_type (void_type_node, void_list_node);
6260 void_ftype_ptr = build_function_type (void_type_node,
6261 tree_cons (NULL_TREE,
6262 ptr_type_node,
6263 void_list_node));
6264 void_ftype_ptr
6265 = build_exception_variant (void_ftype_ptr, empty_except_spec);
6267 /* C++ extensions */
6269 unknown_type_node = make_node (UNKNOWN_TYPE);
6270 record_unknown_type (unknown_type_node, "unknown type");
6272 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6273 TREE_TYPE (unknown_type_node) = unknown_type_node;
6275 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6276 result. */
6277 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6278 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6281 /* Make sure we get a unique function type, so we can give
6282 its pointer type a name. (This wins for gdb.) */
6283 tree vfunc_type = make_node (FUNCTION_TYPE);
6284 TREE_TYPE (vfunc_type) = integer_type_node;
6285 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6286 layout_type (vfunc_type);
6288 vtable_entry_type = build_pointer_type (vfunc_type);
6290 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6292 vtbl_type_node
6293 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6294 layout_type (vtbl_type_node);
6295 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6296 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6297 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6298 layout_type (vtbl_ptr_type_node);
6299 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6301 push_namespace (get_identifier ("__cxxabiv1"));
6302 abi_node = current_namespace;
6303 pop_namespace ();
6305 global_type_node = make_node (LANG_TYPE);
6306 record_unknown_type (global_type_node, "global type");
6308 /* Now, C++. */
6309 current_lang_name = lang_name_cplusplus;
6312 tree bad_alloc_type_node, newtype, deltype;
6313 tree ptr_ftype_sizetype;
6315 push_namespace (std_identifier);
6316 bad_alloc_type_node
6317 = xref_tag (class_type, get_identifier ("bad_alloc"),
6318 /*attributes=*/NULL_TREE, 1);
6319 pop_namespace ();
6320 ptr_ftype_sizetype
6321 = build_function_type (ptr_type_node,
6322 tree_cons (NULL_TREE,
6323 size_type_node,
6324 void_list_node));
6325 newtype = build_exception_variant
6326 (ptr_ftype_sizetype, add_exception_specifier
6327 (NULL_TREE, bad_alloc_type_node, -1));
6328 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6329 push_cp_library_fn (NEW_EXPR, newtype);
6330 push_cp_library_fn (VEC_NEW_EXPR, newtype);
6331 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6332 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6335 abort_fndecl
6336 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6338 /* Perform other language dependent initializations. */
6339 init_class_processing ();
6340 init_search_processing ();
6341 init_rtti_processing ();
6343 if (flag_exceptions)
6344 init_exception_processing ();
6346 if (! supports_one_only ())
6347 flag_weak = 0;
6349 make_fname_decl = cp_make_fname_decl;
6350 start_fname_decls ();
6352 /* Show we use EH for cleanups. */
6353 using_eh_for_cleanups ();
6355 /* Maintain consistency. Perhaps we should just complain if they
6356 say -fwritable-strings? */
6357 if (flag_writable_strings)
6358 flag_const_strings = 0;
6361 /* Generate an initializer for a function naming variable from
6362 NAME. NAME may be NULL, in which case we generate a special
6363 ERROR_MARK node which should be replaced later. */
6365 tree
6366 cp_fname_init (const char* name)
6368 tree domain = NULL_TREE;
6369 tree type;
6370 tree init = NULL_TREE;
6371 size_t length = 0;
6373 if (name)
6375 length = strlen (name);
6376 domain = build_index_type (size_int (length));
6377 init = build_string (length + 1, name);
6380 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6381 type = build_cplus_array_type (type, domain);
6383 if (init)
6384 TREE_TYPE (init) = type;
6385 else
6386 /* We don't know the value until instantiation time. Make
6387 something which will be digested now, but replaced later. */
6388 init = build (ERROR_MARK, type);
6390 return init;
6393 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6394 decl, NAME is the initialization string and TYPE_DEP indicates whether
6395 NAME depended on the type of the function. We make use of that to detect
6396 __PRETTY_FUNCTION__ inside a template fn. This is being done
6397 lazily at the point of first use, so we musn't push the decl now. */
6399 static tree
6400 cp_make_fname_decl (tree id, int type_dep)
6402 const char *const name = (type_dep && processing_template_decl
6403 ? NULL : fname_as_string (type_dep));
6404 tree init = cp_fname_init (name);
6405 tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6407 /* As we're using pushdecl_with_scope, we must set the context. */
6408 DECL_CONTEXT (decl) = current_function_decl;
6409 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6411 TREE_STATIC (decl) = 1;
6412 TREE_READONLY (decl) = 1;
6413 DECL_ARTIFICIAL (decl) = 1;
6414 DECL_INITIAL (decl) = init;
6416 TREE_USED (decl) = 1;
6418 if (current_function_decl)
6420 struct cp_binding_level *b = current_binding_level;
6421 while (b->level_chain->parm_flag == 0)
6422 b = b->level_chain;
6423 pushdecl_with_scope (decl, b);
6426 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6428 return decl;
6431 /* Make a definition for a builtin function named NAME in the current
6432 namespace, whose data type is TYPE and whose context is CONTEXT.
6433 TYPE should be a function type with argument types.
6435 CLASS and CODE tell later passes how to compile calls to this function.
6436 See tree.h for possible values.
6438 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6439 the name to be called if we can't opencode the function.
6440 If ATTRS is nonzero, use that for the function's attribute
6441 list. */
6443 static tree
6444 builtin_function_1 (const char* name,
6445 tree type,
6446 tree context,
6447 int code,
6448 enum built_in_class class,
6449 const char* libname,
6450 tree attrs)
6452 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6453 DECL_BUILT_IN_CLASS (decl) = class;
6454 DECL_FUNCTION_CODE (decl) = code;
6455 DECL_CONTEXT (decl) = context;
6457 pushdecl (decl);
6459 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6460 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6461 function in the namespace. */
6462 if (libname)
6463 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6464 make_decl_rtl (decl, NULL);
6466 /* Warn if a function in the namespace for users
6467 is used without an occasion to consider it declared. */
6468 if (name[0] != '_' || name[1] != '_')
6469 DECL_ANTICIPATED (decl) = 1;
6471 /* Possibly apply some default attributes to this built-in function. */
6472 if (attrs)
6473 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6474 else
6475 decl_attributes (&decl, NULL_TREE, 0);
6477 return decl;
6480 /* Entry point for the benefit of c_common_nodes_and_builtins.
6482 Make a defintion for a builtin function named NAME and whose data type
6483 is TYPE. TYPE should be a function type with argument types. This
6484 function places the anticipated declaration in the global namespace
6485 and additionally in the std namespace if appropriate.
6487 CLASS and CODE tell later passes how to compile calls to this function.
6488 See tree.h for possible values.
6490 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6491 the name to be called if we can't opencode the function.
6493 If ATTRS is nonzero, use that for the function's attribute
6494 list. */
6496 tree
6497 builtin_function (const char* name,
6498 tree type,
6499 int code,
6500 enum built_in_class class,
6501 const char* libname,
6502 tree attrs)
6504 /* All builtins that don't begin with an '_' should additionally
6505 go in the 'std' namespace. */
6506 if (name[0] != '_')
6508 push_namespace (std_identifier);
6509 builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6510 pop_namespace ();
6513 return builtin_function_1 (name, type, NULL_TREE, code,
6514 class, libname, attrs);
6517 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6518 function. Not called directly. */
6520 static tree
6521 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6523 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6524 DECL_EXTERNAL (fn) = 1;
6525 TREE_PUBLIC (fn) = 1;
6526 DECL_ARTIFICIAL (fn) = 1;
6527 TREE_NOTHROW (fn) = 1;
6528 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6529 SET_DECL_LANGUAGE (fn, lang_c);
6530 return fn;
6533 /* Returns the _DECL for a library function with C linkage.
6534 We assume that such functions never throw; if this is incorrect,
6535 callers should unset TREE_NOTHROW. */
6537 tree
6538 build_library_fn (tree name, tree type)
6540 return build_library_fn_1 (name, ERROR_MARK, type);
6543 /* Returns the _DECL for a library function with C++ linkage. */
6545 static tree
6546 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6548 tree fn = build_library_fn_1 (name, operator_code, type);
6549 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6550 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6551 SET_DECL_LANGUAGE (fn, lang_cplusplus);
6552 set_mangled_name_for_decl (fn);
6553 return fn;
6556 /* Like build_library_fn, but takes a C string instead of an
6557 IDENTIFIER_NODE. */
6559 tree
6560 build_library_fn_ptr (const char* name, tree type)
6562 return build_library_fn (get_identifier (name), type);
6565 /* Like build_cp_library_fn, but takes a C string instead of an
6566 IDENTIFIER_NODE. */
6568 tree
6569 build_cp_library_fn_ptr (const char* name, tree type)
6571 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6574 /* Like build_library_fn, but also pushes the function so that we will
6575 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
6577 tree
6578 push_library_fn (tree name, tree type)
6580 tree fn = build_library_fn (name, type);
6581 pushdecl_top_level (fn);
6582 return fn;
6585 /* Like build_cp_library_fn, but also pushes the function so that it
6586 will be found by normal lookup. */
6588 static tree
6589 push_cp_library_fn (enum tree_code operator_code, tree type)
6591 tree fn = build_cp_library_fn (ansi_opname (operator_code),
6592 operator_code,
6593 type);
6594 pushdecl (fn);
6595 return fn;
6598 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6599 a FUNCTION_TYPE. */
6601 tree
6602 push_void_library_fn (tree name, tree parmtypes)
6604 tree type = build_function_type (void_type_node, parmtypes);
6605 return push_library_fn (name, type);
6608 /* Like push_library_fn, but also note that this function throws
6609 and does not return. Used for __throw_foo and the like. */
6611 tree
6612 push_throw_library_fn (tree name, tree type)
6614 tree fn = push_library_fn (name, type);
6615 TREE_THIS_VOLATILE (fn) = 1;
6616 TREE_NOTHROW (fn) = 0;
6617 return fn;
6620 /* Apply default attributes to a function, if a system function with default
6621 attributes. */
6623 void
6624 cxx_insert_default_attributes (tree decl)
6626 if (!DECL_EXTERN_C_FUNCTION_P (decl))
6627 return;
6628 if (!TREE_PUBLIC (decl))
6629 return;
6630 c_common_insert_default_attributes (decl);
6633 /* When we call finish_struct for an anonymous union, we create
6634 default copy constructors and such. But, an anonymous union
6635 shouldn't have such things; this function undoes the damage to the
6636 anonymous union type T.
6638 (The reason that we create the synthesized methods is that we don't
6639 distinguish `union { int i; }' from `typedef union { int i; } U'.
6640 The first is an anonymous union; the second is just an ordinary
6641 union type.) */
6643 void
6644 fixup_anonymous_aggr (tree t)
6646 tree *q;
6648 /* Wipe out memory of synthesized methods */
6649 TYPE_HAS_CONSTRUCTOR (t) = 0;
6650 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6651 TYPE_HAS_INIT_REF (t) = 0;
6652 TYPE_HAS_CONST_INIT_REF (t) = 0;
6653 TYPE_HAS_ASSIGN_REF (t) = 0;
6654 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6656 /* Splice the implicitly generated functions out of the TYPE_METHODS
6657 list. */
6658 q = &TYPE_METHODS (t);
6659 while (*q)
6661 if (DECL_ARTIFICIAL (*q))
6662 *q = TREE_CHAIN (*q);
6663 else
6664 q = &TREE_CHAIN (*q);
6667 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
6668 if (TYPE_METHODS (t))
6669 cp_error_at ("an anonymous union cannot have function members", t);
6671 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6672 assignment operators (because they cannot have these methods themselves).
6673 For anonymous unions this is already checked because they are not allowed
6674 in any union, otherwise we have to check it. */
6675 if (TREE_CODE (t) != UNION_TYPE)
6677 tree field, type;
6679 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6680 if (TREE_CODE (field) == FIELD_DECL)
6682 type = TREE_TYPE (field);
6683 if (CLASS_TYPE_P (type))
6685 if (TYPE_NEEDS_CONSTRUCTING (type))
6686 cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6687 field);
6688 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6689 cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6690 field);
6691 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6692 cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6693 field);
6699 /* Make sure that a declaration with no declarator is well-formed, i.e.
6700 just declares a tagged type or anonymous union.
6702 Returns the type declared; or NULL_TREE if none. */
6704 tree
6705 check_tag_decl (tree declspecs)
6707 int found_type = 0;
6708 int saw_friend = 0;
6709 int saw_typedef = 0;
6710 tree ob_modifier = NULL_TREE;
6711 register tree link;
6712 /* If a class, struct, or enum type is declared by the DECLSPECS
6713 (i.e, if a class-specifier, enum-specifier, or non-typename
6714 elaborated-type-specifier appears in the DECLSPECS),
6715 DECLARED_TYPE is set to the corresponding type. */
6716 tree declared_type = NULL_TREE;
6717 bool error_p = false;
6719 for (link = declspecs; link; link = TREE_CHAIN (link))
6721 tree value = TREE_VALUE (link);
6723 if (TYPE_P (value)
6724 || TREE_CODE (value) == TYPE_DECL
6725 || (TREE_CODE (value) == IDENTIFIER_NODE
6726 && IDENTIFIER_GLOBAL_VALUE (value)
6727 && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6729 ++found_type;
6731 if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6733 if (! in_system_header)
6734 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6735 return NULL_TREE;
6738 if (TYPE_P (value)
6739 && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6740 || TREE_CODE (value) == ENUMERAL_TYPE))
6742 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6743 declared_type = value;
6746 else if (value == ridpointers[(int) RID_TYPEDEF])
6747 saw_typedef = 1;
6748 else if (value == ridpointers[(int) RID_FRIEND])
6750 if (current_class_type == NULL_TREE
6751 || current_scope () != current_class_type)
6752 ob_modifier = value;
6753 else
6754 saw_friend = 1;
6756 else if (value == ridpointers[(int) RID_STATIC]
6757 || value == ridpointers[(int) RID_EXTERN]
6758 || value == ridpointers[(int) RID_AUTO]
6759 || value == ridpointers[(int) RID_REGISTER]
6760 || value == ridpointers[(int) RID_INLINE]
6761 || value == ridpointers[(int) RID_VIRTUAL]
6762 || value == ridpointers[(int) RID_CONST]
6763 || value == ridpointers[(int) RID_VOLATILE]
6764 || value == ridpointers[(int) RID_EXPLICIT]
6765 || value == ridpointers[(int) RID_THREAD])
6766 ob_modifier = value;
6767 else if (value == error_mark_node)
6768 error_p = true;
6771 if (found_type > 1)
6772 error ("multiple types in one declaration");
6774 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6775 pedwarn ("declaration does not declare anything");
6776 /* Check for an anonymous union. */
6777 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6778 && TYPE_ANONYMOUS_P (declared_type))
6780 /* 7/3 In a simple-declaration, the optional init-declarator-list
6781 can be omitted only when declaring a class (clause 9) or
6782 enumeration (7.2), that is, when the decl-specifier-seq contains
6783 either a class-specifier, an elaborated-type-specifier with
6784 a class-key (9.1), or an enum-specifier. In these cases and
6785 whenever a class-specifier or enum-specifier is present in the
6786 decl-specifier-seq, the identifiers in these specifiers are among
6787 the names being declared by the declaration (as class-name,
6788 enum-names, or enumerators, depending on the syntax). In such
6789 cases, and except for the declaration of an unnamed bit-field (9.6),
6790 the decl-specifier-seq shall introduce one or more names into the
6791 program, or shall redeclare a name introduced by a previous
6792 declaration. [Example:
6793 enum { }; // ill-formed
6794 typedef class { }; // ill-formed
6795 --end example] */
6796 if (saw_typedef)
6798 error ("missing type-name in typedef-declaration");
6799 return NULL_TREE;
6801 /* Anonymous unions are objects, so they can have specifiers. */;
6802 SET_ANON_AGGR_TYPE_P (declared_type);
6804 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
6805 && !in_system_header)
6806 pedwarn ("ISO C++ prohibits anonymous structs");
6809 else if (ob_modifier)
6811 if (ob_modifier == ridpointers[(int) RID_INLINE]
6812 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6813 error ("`%D' can only be specified for functions", ob_modifier);
6814 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6815 error ("`%D' can only be specified inside a class", ob_modifier);
6816 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6817 error ("`%D' can only be specified for constructors",
6818 ob_modifier);
6819 else
6820 error ("`%D' can only be specified for objects and functions",
6821 ob_modifier);
6824 return declared_type;
6827 /* Called when a declaration is seen that contains no names to declare.
6828 If its type is a reference to a structure, union or enum inherited
6829 from a containing scope, shadow that tag name for the current scope
6830 with a forward reference.
6831 If its type defines a new named structure or union
6832 or defines an enum, it is valid but we need not do anything here.
6833 Otherwise, it is an error.
6835 C++: may have to grok the declspecs to learn about static,
6836 complain for anonymous unions.
6838 Returns the TYPE declared -- or NULL_TREE if none. */
6840 tree
6841 shadow_tag (tree declspecs)
6843 tree t = check_tag_decl (declspecs);
6845 if (!t)
6846 return NULL_TREE;
6848 maybe_process_partial_specialization (t);
6850 /* This is where the variables in an anonymous union are
6851 declared. An anonymous union declaration looks like:
6852 union { ... } ;
6853 because there is no declarator after the union, the parser
6854 sends that declaration here. */
6855 if (ANON_AGGR_TYPE_P (t))
6857 fixup_anonymous_aggr (t);
6859 if (TYPE_FIELDS (t))
6861 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6862 NULL);
6863 finish_anon_union (decl);
6867 return t;
6870 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6872 tree
6873 groktypename (tree typename)
6875 tree specs, attrs;
6876 tree type;
6877 if (TREE_CODE (typename) != TREE_LIST)
6878 return typename;
6879 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6880 type = grokdeclarator (TREE_VALUE (typename), specs,
6881 TYPENAME, 0, &attrs);
6882 if (attrs)
6883 cplus_decl_attributes (&type, attrs, 0);
6884 return type;
6887 /* Decode a declarator in an ordinary declaration or data definition.
6888 This is called as soon as the type information and variable name
6889 have been parsed, before parsing the initializer if any.
6890 Here we create the ..._DECL node, fill in its type,
6891 and put it on the list of decls for the current context.
6892 The ..._DECL node is returned as the value.
6894 Exception: for arrays where the length is not specified,
6895 the type is left null, to be filled in by `cp_finish_decl'.
6897 Function definitions do not come here; they go to start_function
6898 instead. However, external and forward declarations of functions
6899 do go through here. Structure field declarations are done by
6900 grokfield and not through here. */
6902 tree
6903 start_decl (tree declarator,
6904 tree declspecs,
6905 int initialized,
6906 tree attributes,
6907 tree prefix_attributes)
6909 tree decl;
6910 register tree type, tem;
6911 tree context;
6913 /* This should only be done once on the top most decl. */
6914 if (have_extern_spec)
6916 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
6917 declspecs);
6918 have_extern_spec = false;
6921 /* An object declared as __attribute__((deprecated)) suppresses
6922 warnings of uses of other deprecated items. */
6923 if (lookup_attribute ("deprecated", attributes))
6924 deprecated_state = DEPRECATED_SUPPRESS;
6926 attributes = chainon (attributes, prefix_attributes);
6928 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6929 &attributes);
6931 deprecated_state = DEPRECATED_NORMAL;
6933 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
6934 return NULL_TREE;
6936 type = TREE_TYPE (decl);
6938 if (type == error_mark_node)
6939 return NULL_TREE;
6941 context = DECL_CONTEXT (decl);
6943 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6944 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6946 /* When parsing the initializer, lookup should use the object's
6947 namespace. */
6948 push_decl_namespace (context);
6951 /* We are only interested in class contexts, later. */
6952 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6953 context = NULL_TREE;
6955 if (initialized)
6956 /* Is it valid for this decl to have an initializer at all?
6957 If not, set INITIALIZED to zero, which will indirectly
6958 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
6959 switch (TREE_CODE (decl))
6961 case TYPE_DECL:
6962 error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
6963 initialized = 0;
6964 break;
6966 case FUNCTION_DECL:
6967 error ("function `%#D' is initialized like a variable", decl);
6968 initialized = 0;
6969 break;
6971 default:
6972 break;
6975 if (initialized)
6977 if (! toplevel_bindings_p ()
6978 && DECL_EXTERNAL (decl))
6979 warning ("declaration of `%#D' has `extern' and is initialized",
6980 decl);
6981 DECL_EXTERNAL (decl) = 0;
6982 if (toplevel_bindings_p ())
6983 TREE_STATIC (decl) = 1;
6985 /* Tell `pushdecl' this is an initialized decl
6986 even though we don't yet have the initializer expression.
6987 Also tell `cp_finish_decl' it may store the real initializer. */
6988 DECL_INITIAL (decl) = error_mark_node;
6991 /* Set attributes here so if duplicate decl, will have proper attributes. */
6992 cplus_decl_attributes (&decl, attributes, 0);
6994 /* If #pragma weak was used, mark the decl weak now. */
6995 if (current_binding_level == global_binding_level)
6996 maybe_apply_pragma_weak (decl);
6998 if (TREE_CODE (decl) == FUNCTION_DECL
6999 && DECL_DECLARED_INLINE_P (decl)
7000 && DECL_UNINLINABLE (decl)
7001 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
7002 warning ("%Hinline function '%D' given attribute noinline",
7003 &DECL_SOURCE_LOCATION (decl), decl);
7005 if (context && COMPLETE_TYPE_P (complete_type (context)))
7007 push_nested_class (context);
7009 if (TREE_CODE (decl) == VAR_DECL)
7011 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
7012 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7013 error ("`%#D' is not a static member of `%#T'", decl, context);
7014 else
7016 if (DECL_CONTEXT (field) != context)
7018 if (!same_type_p (DECL_CONTEXT (field), context))
7019 pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7020 DECL_CONTEXT (field), DECL_NAME (decl),
7021 context, DECL_NAME (decl));
7022 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7024 /* Static data member are tricky; an in-class initialization
7025 still doesn't provide a definition, so the in-class
7026 declaration will have DECL_EXTERNAL set, but will have an
7027 initialization. Thus, duplicate_decls won't warn
7028 about this situation, and so we check here. */
7029 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7030 error ("duplicate initialization of %D", decl);
7031 if (duplicate_decls (decl, field))
7032 decl = field;
7035 else
7037 tree field = check_classfn (context, decl);
7038 if (field && duplicate_decls (decl, field))
7039 decl = field;
7042 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
7043 DECL_IN_AGGR_P (decl) = 0;
7044 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7045 || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7047 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7048 /* [temp.expl.spec] An explicit specialization of a static data
7049 member of a template is a definition if the declaration
7050 includes an initializer; otherwise, it is a declaration.
7052 We check for processing_specialization so this only applies
7053 to the new specialization syntax. */
7054 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7055 DECL_EXTERNAL (decl) = 1;
7058 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7059 pedwarn ("declaration of `%#D' outside of class is not definition",
7060 decl);
7063 /* Enter this declaration into the symbol table. */
7064 tem = maybe_push_decl (decl);
7066 if (processing_template_decl)
7067 tem = push_template_decl (tem);
7069 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7070 /* Tell the back-end to use or not use .common as appropriate. If we say
7071 -fconserve-space, we want this to save .data space, at the expense of
7072 wrong semantics. If we say -fno-conserve-space, we want this to
7073 produce errors about redefs; to do this we force variables into the
7074 data segment. */
7075 DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7076 || !DECL_THREAD_LOCAL (tem))
7077 && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7078 #endif
7080 if (! processing_template_decl)
7081 start_decl_1 (tem);
7083 return tem;
7086 void
7087 start_decl_1 (tree decl)
7089 tree type = TREE_TYPE (decl);
7090 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7092 if (type == error_mark_node)
7093 return;
7095 maybe_push_cleanup_level (type);
7097 if (initialized)
7098 /* Is it valid for this decl to have an initializer at all?
7099 If not, set INITIALIZED to zero, which will indirectly
7100 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
7102 /* Don't allow initializations for incomplete types except for
7103 arrays which might be completed by the initialization. */
7104 if (COMPLETE_TYPE_P (complete_type (type)))
7105 ; /* A complete type is ok. */
7106 else if (TREE_CODE (type) != ARRAY_TYPE)
7108 error ("variable `%#D' has initializer but incomplete type",
7109 decl);
7110 initialized = 0;
7111 type = TREE_TYPE (decl) = error_mark_node;
7113 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7115 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7116 error ("elements of array `%#D' have incomplete type", decl);
7117 /* else we already gave an error in start_decl. */
7118 initialized = 0;
7122 if (!initialized
7123 && TREE_CODE (decl) != TYPE_DECL
7124 && TREE_CODE (decl) != TEMPLATE_DECL
7125 && type != error_mark_node
7126 && IS_AGGR_TYPE (type)
7127 && ! DECL_EXTERNAL (decl))
7129 if ((! processing_template_decl || ! uses_template_parms (type))
7130 && !COMPLETE_TYPE_P (complete_type (type)))
7132 error ("aggregate `%#D' has incomplete type and cannot be defined",
7133 decl);
7134 /* Change the type so that assemble_variable will give
7135 DECL an rtl we can live with: (mem (const_int 0)). */
7136 type = TREE_TYPE (decl) = error_mark_node;
7138 else
7140 /* If any base type in the hierarchy of TYPE needs a constructor,
7141 then we set initialized to 1. This way any nodes which are
7142 created for the purposes of initializing this aggregate
7143 will live as long as it does. This is necessary for global
7144 aggregates which do not have their initializers processed until
7145 the end of the file. */
7146 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7150 if (! initialized)
7151 DECL_INITIAL (decl) = NULL_TREE;
7154 /* Handle initialization of references.
7155 These three arguments are from `cp_finish_decl', and have the
7156 same meaning here that they do there.
7158 Quotes on semantics can be found in ARM 8.4.3. */
7160 static tree
7161 grok_reference_init (tree decl, tree type, tree init)
7163 tree tmp;
7165 if (init == NULL_TREE)
7167 if ((DECL_LANG_SPECIFIC (decl) == 0
7168 || DECL_IN_AGGR_P (decl) == 0)
7169 && ! DECL_THIS_EXTERN (decl))
7170 error ("`%D' declared as reference but not initialized", decl);
7171 return NULL_TREE;
7174 if (TREE_CODE (init) == CONSTRUCTOR)
7176 error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7177 return NULL_TREE;
7180 if (TREE_CODE (init) == TREE_LIST)
7181 init = build_compound_expr (init);
7183 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7184 init = convert_from_reference (init);
7186 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7187 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7189 /* Note: default conversion is only called in very special cases. */
7190 init = default_conversion (init);
7193 /* Convert INIT to the reference type TYPE. This may involve the
7194 creation of a temporary, whose lifetime must be the same as that
7195 of the reference. If so, a DECL_STMT for the temporary will be
7196 added just after the DECL_STMT for DECL. That's why we don't set
7197 DECL_INITIAL for local references (instead assigning to them
7198 explicitly); we need to allow the temporary to be initialized
7199 first. */
7200 tmp = initialize_reference (type, init, decl);
7202 if (tmp == error_mark_node)
7203 return NULL_TREE;
7204 else if (tmp == NULL_TREE)
7206 error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7207 return NULL_TREE;
7210 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7211 return tmp;
7213 DECL_INITIAL (decl) = tmp;
7215 return NULL_TREE;
7218 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7219 array until we finish parsing the initializer. If that's the
7220 situation we're in, update DECL accordingly. */
7222 static void
7223 maybe_deduce_size_from_array_init (tree decl, tree init)
7225 tree type = TREE_TYPE (decl);
7227 if (TREE_CODE (type) == ARRAY_TYPE
7228 && TYPE_DOMAIN (type) == NULL_TREE
7229 && TREE_CODE (decl) != TYPE_DECL)
7231 /* do_default is really a C-ism to deal with tentative definitions.
7232 But let's leave it here to ease the eventual merge. */
7233 int do_default = !DECL_EXTERNAL (decl);
7234 tree initializer = init ? init : DECL_INITIAL (decl);
7235 int failure = complete_array_type (type, initializer, do_default);
7237 if (failure == 1)
7238 error ("initializer fails to determine size of `%D'", decl);
7240 if (failure == 2)
7242 if (do_default)
7243 error ("array size missing in `%D'", decl);
7244 /* If a `static' var's size isn't known, make it extern as
7245 well as static, so it does not get allocated. If it's not
7246 `static', then don't mark it extern; finish_incomplete_decl
7247 will give it a default size and it will get allocated. */
7248 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7249 DECL_EXTERNAL (decl) = 1;
7252 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7253 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7254 integer_zero_node))
7255 error ("zero-size array `%D'", decl);
7257 layout_decl (decl, 0);
7261 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7262 any appropriate error messages regarding the layout. */
7264 static void
7265 layout_var_decl (tree decl)
7267 tree type = TREE_TYPE (decl);
7268 #if 0
7269 tree ttype = target_type (type);
7270 #endif
7272 /* If we haven't already layed out this declaration, do so now.
7273 Note that we must not call complete type for an external object
7274 because it's type might involve templates that we are not
7275 supposed to isntantiate yet. (And it's perfectly valid to say
7276 `extern X x' for some incomplete type `X'.) */
7277 if (!DECL_EXTERNAL (decl))
7278 complete_type (type);
7279 if (!DECL_SIZE (decl)
7280 && TREE_TYPE (decl) != error_mark_node
7281 && (COMPLETE_TYPE_P (type)
7282 || (TREE_CODE (type) == ARRAY_TYPE
7283 && !TYPE_DOMAIN (type)
7284 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7285 layout_decl (decl, 0);
7287 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7289 /* An automatic variable with an incomplete type: that is an error.
7290 Don't talk about array types here, since we took care of that
7291 message in grokdeclarator. */
7292 error ("storage size of `%D' isn't known", decl);
7293 TREE_TYPE (decl) = error_mark_node;
7295 #if 0
7296 /* Keep this code around in case we later want to control debug info
7297 based on whether a type is "used". (jason 1999-11-11) */
7299 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7300 /* Let debugger know it should output info for this type. */
7301 note_debug_info_needed (ttype);
7303 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7304 note_debug_info_needed (DECL_CONTEXT (decl));
7305 #endif
7307 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7308 && DECL_SIZE (decl) != NULL_TREE
7309 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7311 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7312 constant_expression_warning (DECL_SIZE (decl));
7313 else
7314 error ("storage size of `%D' isn't constant", decl);
7317 if (TREE_STATIC (decl)
7318 && !DECL_ARTIFICIAL (decl)
7319 && current_function_decl
7320 && DECL_CONTEXT (decl) == current_function_decl)
7321 push_local_name (decl);
7324 /* If a local static variable is declared in an inline function, or if
7325 we have a weak definition, we must endeavor to create only one
7326 instance of the variable at link-time. */
7328 static void
7329 maybe_commonize_var (tree decl)
7331 /* Static data in a function with comdat linkage also has comdat
7332 linkage. */
7333 if (TREE_STATIC (decl)
7334 /* Don't mess with __FUNCTION__. */
7335 && ! DECL_ARTIFICIAL (decl)
7336 && DECL_FUNCTION_SCOPE_P (decl)
7337 /* Unfortunately, import_export_decl has not always been called
7338 before the function is processed, so we cannot simply check
7339 DECL_COMDAT. */
7340 && (DECL_COMDAT (DECL_CONTEXT (decl))
7341 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
7342 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
7343 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
7345 if (flag_weak)
7347 /* With weak symbols, we simply make the variable COMDAT;
7348 that will cause copies in multiple translations units to
7349 be merged. */
7350 comdat_linkage (decl);
7352 else
7354 if (DECL_INITIAL (decl) == NULL_TREE
7355 || DECL_INITIAL (decl) == error_mark_node)
7357 /* Without weak symbols, we can use COMMON to merge
7358 uninitialized variables. */
7359 TREE_PUBLIC (decl) = 1;
7360 DECL_COMMON (decl) = 1;
7362 else
7364 /* While for initialized variables, we must use internal
7365 linkage -- which means that multiple copies will not
7366 be merged. */
7367 TREE_PUBLIC (decl) = 0;
7368 DECL_COMMON (decl) = 0;
7369 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7370 cp_warning_at (" you can work around this by removing the initializer", decl);
7374 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7375 /* Set it up again; we might have set DECL_INITIAL since the last
7376 time. */
7377 comdat_linkage (decl);
7380 /* Issue an error message if DECL is an uninitialized const variable. */
7382 static void
7383 check_for_uninitialized_const_var (tree decl)
7385 tree type = TREE_TYPE (decl);
7387 /* ``Unless explicitly declared extern, a const object does not have
7388 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7389 7.1.6 */
7390 if (TREE_CODE (decl) == VAR_DECL
7391 && TREE_CODE (type) != REFERENCE_TYPE
7392 && CP_TYPE_CONST_P (type)
7393 && !TYPE_NEEDS_CONSTRUCTING (type)
7394 && !DECL_INITIAL (decl))
7395 error ("uninitialized const `%D'", decl);
7398 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
7399 returned is the next FIELD_DECL (possibly FIELD itself) that can be
7400 initialized. If there are no more such fields, the return value
7401 will be NULL. */
7403 static tree
7404 next_initializable_field (tree field)
7406 while (field
7407 && (TREE_CODE (field) != FIELD_DECL
7408 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7409 || DECL_ARTIFICIAL (field)))
7410 field = TREE_CHAIN (field);
7412 return field;
7415 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7416 brace-enclosed aggregate initializer.
7418 *INITP is one of a list of initializers describing a brace-enclosed
7419 initializer for an entity of the indicated aggregate TYPE. It may
7420 not presently match the shape of the TYPE; for example:
7422 struct S { int a; int b; };
7423 struct S a[] = { 1, 2, 3, 4 };
7425 Here *INITP will point to TREE_LIST of four elements, rather than a
7426 list of two elements, each itself a list of two elements. This
7427 routine transforms INIT from the former form into the latter. The
7428 revised initializer is returned. */
7430 static tree
7431 reshape_init (tree type, tree *initp)
7433 tree inits;
7434 tree old_init;
7435 tree old_init_value;
7436 tree new_init;
7437 bool brace_enclosed_p;
7439 old_init = *initp;
7440 old_init_value = (TREE_CODE (*initp) == TREE_LIST
7441 ? TREE_VALUE (*initp) : old_init);
7443 /* For some parse errors, OLD_INIT_VALUE may be NULL. */
7444 if (!old_init_value)
7446 my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
7447 TREE_VALUE (old_init) = error_mark_node;
7448 return old_init;
7451 /* If the initializer is brace-enclosed, pull initializers from the
7452 enclosed elements. Advance past the brace-enclosed initializer
7453 now. */
7454 if (TREE_CODE (old_init_value) == CONSTRUCTOR
7455 && TREE_HAS_CONSTRUCTOR (old_init_value))
7457 *initp = TREE_CHAIN (old_init);
7458 TREE_CHAIN (old_init) = NULL_TREE;
7459 inits = CONSTRUCTOR_ELTS (old_init_value);
7460 initp = &inits;
7461 brace_enclosed_p = true;
7463 else
7465 inits = NULL_TREE;
7466 brace_enclosed_p = false;
7469 /* A non-aggregate type is always initialized with a single
7470 initializer. */
7471 if (!CP_AGGREGATE_TYPE_P (type))
7473 *initp = TREE_CHAIN (old_init);
7474 TREE_CHAIN (old_init) = NULL_TREE;
7475 /* It is invalid to initialize a non-aggregate type with a
7476 brace-enclosed initializer. */
7477 if (brace_enclosed_p)
7479 error ("brace-enclosed initializer used to initialize `%T'",
7480 type);
7481 if (TREE_CODE (old_init) == TREE_LIST)
7482 TREE_VALUE (old_init) = error_mark_node;
7483 else
7484 old_init = error_mark_node;
7487 return old_init;
7490 /* [dcl.init.aggr]
7492 All implicit type conversions (clause _conv_) are considered when
7493 initializing the aggregate member with an initializer from an
7494 initializer-list. If the initializer can initialize a member,
7495 the member is initialized. Otherwise, if the member is itself a
7496 non-empty subaggregate, brace elision is assumed and the
7497 initializer is considered for the initialization of the first
7498 member of the subaggregate. */
7499 if (CLASS_TYPE_P (type)
7500 && !brace_enclosed_p
7501 && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7503 *initp = TREE_CHAIN (old_init);
7504 TREE_CHAIN (old_init) = NULL_TREE;
7505 return old_init;
7508 if (TREE_CODE (old_init_value) == STRING_CST
7509 && TREE_CODE (type) == ARRAY_TYPE
7510 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7512 /* [dcl.init.string]
7514 A char array (whether plain char, signed char, or unsigned char)
7515 can be initialized by a string-literal (optionally enclosed in
7516 braces); a wchar_t array can be initialized by a wide
7517 string-literal (optionally enclosed in braces). */
7518 new_init = old_init;
7519 /* Move past the initializer. */
7520 *initp = TREE_CHAIN (old_init);
7521 TREE_CHAIN (old_init) = NULL_TREE;
7523 else
7525 /* Build a CONSTRUCTOR to hold the contents of the aggregate. */
7526 new_init = build_constructor (type, NULL_TREE);
7527 TREE_HAS_CONSTRUCTOR (new_init) = 1;
7529 if (CLASS_TYPE_P (type))
7531 tree field;
7533 field = next_initializable_field (TYPE_FIELDS (type));
7535 if (!field)
7537 /* [dcl.init.aggr]
7539 An initializer for an aggregate member that is an
7540 empty class shall have the form of an empty
7541 initializer-list {}. */
7542 if (!brace_enclosed_p)
7543 error ("initializer for `%T' must be brace-enclosed",
7544 type);
7546 else
7548 /* Loop through the initializable fields, gathering
7549 initializers. */
7550 while (*initp)
7552 tree field_init;
7554 /* Handle designated initializers, as an extension. */
7555 if (TREE_PURPOSE (*initp))
7557 if (pedantic)
7558 pedwarn ("ISO C++ does not allow designated initializers");
7559 field = lookup_field_1 (type, TREE_PURPOSE (*initp),
7560 /*want_type=*/false);
7561 if (!field || TREE_CODE (field) != FIELD_DECL)
7562 error ("`%T' has no non-static data member named `%D'",
7563 type, TREE_PURPOSE (*initp));
7565 if (!field)
7566 break;
7568 field_init = reshape_init (TREE_TYPE (field), initp);
7569 TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7570 CONSTRUCTOR_ELTS (new_init) = field_init;
7571 /* [dcl.init.aggr]
7573 When a union is initialized with a brace-enclosed
7574 initializer, the braces shall only contain an
7575 initializer for the first member of the union. */
7576 if (TREE_CODE (type) == UNION_TYPE)
7577 break;
7578 field = next_initializable_field (TREE_CHAIN (field));
7582 else if (TREE_CODE (type) == ARRAY_TYPE)
7584 tree index;
7585 tree max_index;
7587 /* If the bound of the array is known, take no more initializers
7588 than are allowed. */
7589 max_index = (TYPE_DOMAIN (type)
7590 ? array_type_nelts (type) : NULL_TREE);
7591 /* Loop through the array elements, gathering initializers. */
7592 for (index = size_zero_node;
7593 *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7594 index = size_binop (PLUS_EXPR, index, size_one_node))
7596 tree element_init;
7598 element_init = reshape_init (TREE_TYPE (type), initp);
7599 TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7600 CONSTRUCTOR_ELTS (new_init) = element_init;
7601 if (TREE_PURPOSE (element_init))
7602 index = TREE_PURPOSE (element_init);
7605 else
7606 abort ();
7608 /* The initializers were placed in reverse order in the
7609 CONSTRUCTOR. */
7610 CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7612 if (TREE_CODE (old_init) == TREE_LIST)
7613 new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7616 /* If this was a brace-enclosed initializer and all of the
7617 initializers were not used up, there is a problem. */
7618 if (brace_enclosed_p && *initp)
7619 error ("too many initializers for `%T'", type);
7621 return new_init;
7624 /* Verify INIT (the initializer for DECL), and record the
7625 initialization in DECL_INITIAL, if appropriate.
7627 If the return value is non-NULL, it is an expression that must be
7628 evaluated dynamically to initialize DECL. */
7630 static tree
7631 check_initializer (tree decl, tree init, int flags)
7633 tree type = TREE_TYPE (decl);
7635 /* If `start_decl' didn't like having an initialization, ignore it now. */
7636 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7637 init = NULL_TREE;
7639 /* If an initializer is present, DECL_INITIAL has been
7640 error_mark_node, to indicate that an as-of-yet unevaluated
7641 initialization will occur. From now on, DECL_INITIAL reflects
7642 the static initialization -- if any -- of DECL. */
7643 DECL_INITIAL (decl) = NULL_TREE;
7645 /* Things that are going to be initialized need to have complete
7646 type. */
7647 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7649 if (type == error_mark_node)
7650 /* We will have already complained. */
7651 init = NULL_TREE;
7652 else if (init && COMPLETE_TYPE_P (type)
7653 && !TREE_CONSTANT (TYPE_SIZE (type)))
7655 error ("variable-sized object `%D' may not be initialized", decl);
7656 init = NULL_TREE;
7658 else if (TREE_CODE (type) == ARRAY_TYPE
7659 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7661 error ("elements of array `%#D' have incomplete type", decl);
7662 init = NULL_TREE;
7664 else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7666 error ("`%D' has incomplete type", decl);
7667 TREE_TYPE (decl) = error_mark_node;
7668 init = NULL_TREE;
7671 if (TREE_CODE (decl) == CONST_DECL)
7673 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7675 DECL_INITIAL (decl) = init;
7677 my_friendly_assert (init != NULL_TREE, 149);
7678 init = NULL_TREE;
7680 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7681 init = grok_reference_init (decl, type, init);
7682 else if (init)
7684 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7686 /* [dcl.init] paragraph 13,
7687 If T is a scalar type, then a declaration of the form
7688 T x = { a };
7689 is equivalent to
7690 T x = a;
7692 reshape_init will complain about the extra braces,
7693 and doesn't do anything useful in the case where TYPE is
7694 scalar, so just don't call it. */
7695 if (CP_AGGREGATE_TYPE_P (type))
7696 init = reshape_init (type, &init);
7698 if ((*targetm.vector_opaque_p) (type))
7700 error ("opaque vector types cannot be initialized");
7701 init = error_mark_node;
7705 /* If DECL has an array type without a specific bound, deduce the
7706 array size from the initializer. */
7707 maybe_deduce_size_from_array_init (decl, init);
7708 type = TREE_TYPE (decl);
7709 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7710 TREE_TYPE (init) = type;
7712 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7714 if (TREE_CODE (type) == ARRAY_TYPE)
7715 goto initialize_aggr;
7716 else if (TREE_CODE (init) == CONSTRUCTOR
7717 && TREE_HAS_CONSTRUCTOR (init))
7719 if (TYPE_NON_AGGREGATE_CLASS (type))
7721 error ("`%D' must be initialized by constructor, not by `{...}'",
7722 decl);
7723 init = error_mark_node;
7725 else
7726 goto dont_use_constructor;
7728 else
7730 int saved_stmts_are_full_exprs_p;
7732 initialize_aggr:
7733 saved_stmts_are_full_exprs_p = 0;
7734 if (building_stmt_tree ())
7736 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7737 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7739 init = build_aggr_init (decl, init, flags);
7740 if (building_stmt_tree ())
7741 current_stmt_tree ()->stmts_are_full_exprs_p =
7742 saved_stmts_are_full_exprs_p;
7743 return init;
7746 else
7748 dont_use_constructor:
7749 if (TREE_CODE (init) != TREE_VEC)
7750 init = store_init_value (decl, init);
7753 else if (DECL_EXTERNAL (decl))
7755 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7756 goto initialize_aggr;
7757 else if (IS_AGGR_TYPE (type))
7759 tree core_type = strip_array_types (type);
7761 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7762 error ("structure `%D' with uninitialized const members", decl);
7763 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7764 error ("structure `%D' with uninitialized reference members",
7765 decl);
7767 check_for_uninitialized_const_var (decl);
7769 else
7770 check_for_uninitialized_const_var (decl);
7772 if (init && init != error_mark_node)
7773 init = build (INIT_EXPR, type, decl, init);
7775 return init;
7778 /* If DECL is not a local variable, give it RTL. */
7780 static void
7781 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7783 int toplev = toplevel_bindings_p ();
7784 int defer_p;
7786 /* Handle non-variables up front. */
7787 if (TREE_CODE (decl) != VAR_DECL)
7789 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7790 return;
7793 /* If we see a class member here, it should be a static data
7794 member. */
7795 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7797 my_friendly_assert (TREE_STATIC (decl), 19990828);
7798 /* An in-class declaration of a static data member should be
7799 external; it is only a declaration, and not a definition. */
7800 if (init == NULL_TREE)
7801 my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7804 /* Set the DECL_ASSEMBLER_NAME for the variable. */
7805 if (asmspec)
7807 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7808 /* The `register' keyword, when used together with an
7809 asm-specification, indicates that the variable should be
7810 placed in a particular register. */
7811 if (DECL_REGISTER (decl))
7812 DECL_C_HARD_REGISTER (decl) = 1;
7815 /* We don't create any RTL for local variables. */
7816 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7817 return;
7819 /* We defer emission of local statics until the corresponding
7820 DECL_STMT is expanded. */
7821 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7823 /* We try to defer namespace-scope static constants so that they are
7824 not emitted into the object file unnecessarily. */
7825 if (!DECL_VIRTUAL_P (decl)
7826 && TREE_READONLY (decl)
7827 && DECL_INITIAL (decl) != NULL_TREE
7828 && DECL_INITIAL (decl) != error_mark_node
7829 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7830 && toplev
7831 && !TREE_PUBLIC (decl))
7833 /* Fool with the linkage of static consts according to #pragma
7834 interface. */
7835 if (!interface_unknown && !TREE_PUBLIC (decl))
7837 TREE_PUBLIC (decl) = 1;
7838 DECL_EXTERNAL (decl) = interface_only;
7841 defer_p = 1;
7843 /* Likewise for template instantiations. */
7844 else if (DECL_COMDAT (decl))
7845 defer_p = 1;
7847 /* If we're deferring the variable, we only need to make RTL if
7848 there's an ASMSPEC. Otherwise, we'll lazily create it later when
7849 we need it. (There's no way to lazily create RTL for things that
7850 have assembly specs because the information about the specifier
7851 isn't stored in the tree, yet) */
7852 if (defer_p && asmspec)
7853 make_decl_rtl (decl, asmspec);
7854 /* If we're not deferring, go ahead and assemble the variable. */
7855 else if (!defer_p)
7856 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7859 /* The old ARM scoping rules injected variables declared in the
7860 initialization statement of a for-statement into the surrounding
7861 scope. We support this usage, in order to be backward-compatible.
7862 DECL is a just-declared VAR_DECL; if necessary inject its
7863 declaration into the surrounding scope. */
7865 void
7866 maybe_inject_for_scope_var (tree decl)
7868 timevar_push (TV_NAME_LOOKUP);
7869 if (!DECL_NAME (decl))
7871 timevar_pop (TV_NAME_LOOKUP);
7872 return;
7875 /* Declarations of __FUNCTION__ and its ilk appear magically when
7876 the variable is first used. If that happens to be inside a
7877 for-loop, we don't want to do anything special. */
7878 if (DECL_PRETTY_FUNCTION_P (decl))
7880 timevar_pop (TV_NAME_LOOKUP);
7881 return;
7884 if (current_binding_level->is_for_scope)
7886 struct cp_binding_level *outer
7887 = current_binding_level->level_chain;
7889 /* Check to see if the same name is already bound at the outer
7890 level, either because it was directly declared, or because a
7891 dead for-decl got preserved. In either case, the code would
7892 not have been valid under the ARM scope rules, so clear
7893 is_for_scope for the current_binding_level.
7895 Otherwise, we need to preserve the temp slot for decl to last
7896 into the outer binding level. */
7898 cxx_binding *outer_binding
7899 = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
7901 if (outer_binding && BINDING_SCOPE (outer_binding) == outer
7902 && (TREE_CODE (BINDING_VALUE (outer_binding)) == VAR_DECL)
7903 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7905 BINDING_VALUE (outer_binding)
7906 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7907 current_binding_level->is_for_scope = 0;
7910 timevar_pop (TV_NAME_LOOKUP);
7913 /* Generate code to initialize DECL (a local variable). */
7915 static void
7916 initialize_local_var (tree decl, tree init)
7918 tree type = TREE_TYPE (decl);
7920 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
7921 || TREE_CODE (decl) == RESULT_DECL,
7922 20021010);
7923 my_friendly_assert (!TREE_STATIC (decl), 20021010);
7925 if (DECL_SIZE (decl) == NULL_TREE)
7927 /* If we used it already as memory, it must stay in memory. */
7928 DECL_INITIAL (decl) = NULL_TREE;
7929 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7932 if (DECL_SIZE (decl) && type != error_mark_node)
7934 int already_used;
7936 /* Compute and store the initial value. */
7937 already_used = TREE_USED (decl) || TREE_USED (type);
7939 /* Perform the initialization. */
7940 if (init)
7942 int saved_stmts_are_full_exprs_p;
7944 my_friendly_assert (building_stmt_tree (), 20000906);
7945 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7946 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7947 finish_expr_stmt (init);
7948 current_stmt_tree ()->stmts_are_full_exprs_p =
7949 saved_stmts_are_full_exprs_p;
7952 /* Set this to 0 so we can tell whether an aggregate which was
7953 initialized was ever used. Don't do this if it has a
7954 destructor, so we don't complain about the 'resource
7955 allocation is initialization' idiom. Now set
7956 attribute((unused)) on types so decls of that type will be
7957 marked used. (see TREE_USED, above.) */
7958 if (TYPE_NEEDS_CONSTRUCTING (type)
7959 && ! already_used
7960 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7961 && DECL_NAME (decl))
7962 TREE_USED (decl) = 0;
7963 else if (already_used)
7964 TREE_USED (decl) = 1;
7967 /* Generate a cleanup, if necessary. */
7968 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7970 tree cleanup;
7972 /* Compute the cleanup. */
7973 cleanup = cxx_maybe_build_cleanup (decl);
7975 /* Record the cleanup required for this declaration. */
7976 if (DECL_SIZE (decl) && cleanup)
7977 finish_decl_cleanup (decl, cleanup);
7981 /* Finish processing of a declaration;
7982 install its line number and initial value.
7983 If the length of an array type is not known before,
7984 it must be determined now, from the initial value, or it is an error.
7986 INIT holds the value of an initializer that should be allowed to escape
7987 the normal rules.
7989 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7990 if the (init) syntax was used. */
7992 void
7993 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
7995 register tree type;
7996 tree ttype = NULL_TREE;
7997 const char *asmspec = NULL;
7998 int was_readonly = 0;
8000 if (! decl)
8002 if (init)
8003 error ("assignment (not initialization) in declaration");
8004 return;
8007 /* If a name was specified, get the string. */
8008 if (current_binding_level == global_binding_level)
8009 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8010 if (asmspec_tree)
8011 asmspec = TREE_STRING_POINTER (asmspec_tree);
8013 if (init && TREE_CODE (init) == NAMESPACE_DECL)
8015 error ("cannot initialize `%D' to namespace `%D'",
8016 decl, init);
8017 init = NULL_TREE;
8020 if (current_class_type
8021 && CP_DECL_CONTEXT (decl) == current_class_type
8022 && TYPE_BEING_DEFINED (current_class_type)
8023 && (DECL_INITIAL (decl) || init))
8024 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8026 if (TREE_CODE (decl) == VAR_DECL
8027 && DECL_CONTEXT (decl)
8028 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8029 && DECL_CONTEXT (decl) != current_namespace
8030 && init)
8032 /* Leave the namespace of the object. */
8033 pop_decl_namespace ();
8036 type = TREE_TYPE (decl);
8038 if (type == error_mark_node)
8039 return;
8041 if (TYPE_HAS_MUTABLE_P (type))
8042 TREE_READONLY (decl) = 0;
8044 if (processing_template_decl)
8046 /* Add this declaration to the statement-tree. */
8047 if (at_function_scope_p ()
8048 && TREE_CODE (decl) != RESULT_DECL)
8049 add_decl_stmt (decl);
8051 if (init && DECL_INITIAL (decl))
8052 DECL_INITIAL (decl) = init;
8053 goto finish_end0;
8056 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8057 my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8059 /* Take care of TYPE_DECLs up front. */
8060 if (TREE_CODE (decl) == TYPE_DECL)
8062 if (type != error_mark_node
8063 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8065 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8066 warning ("shadowing previous type declaration of `%#D'", decl);
8067 set_identifier_type_value (DECL_NAME (decl), type);
8068 CLASSTYPE_GOT_SEMICOLON (type) = 1;
8071 /* If we have installed this as the canonical typedef for this
8072 type, and that type has not been defined yet, delay emitting
8073 the debug information for it, as we will emit it later. */
8074 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8075 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8076 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8078 rest_of_decl_compilation (decl, NULL,
8079 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8080 goto finish_end;
8083 if (TREE_CODE (decl) != FUNCTION_DECL)
8084 ttype = target_type (type);
8086 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8087 && TYPE_NEEDS_CONSTRUCTING (type))
8089 /* Currently, GNU C++ puts constants in text space, making them
8090 impossible to initialize. In the future, one would hope for
8091 an operating system which understood the difference between
8092 initialization and the running of a program. */
8093 was_readonly = 1;
8094 TREE_READONLY (decl) = 0;
8097 if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8099 /* This must override the asm specifier which was placed by
8100 grokclassfn. Lay this out fresh. */
8101 SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8102 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8103 make_decl_rtl (decl, asmspec);
8105 else if (TREE_CODE (decl) == RESULT_DECL)
8106 init = check_initializer (decl, init, flags);
8107 else if (TREE_CODE (decl) == VAR_DECL)
8109 /* Only PODs can have thread-local storage. Other types may require
8110 various kinds of non-trivial initialization. */
8111 if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8112 error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8113 decl, TREE_TYPE (decl));
8114 /* Convert the initializer to the type of DECL, if we have not
8115 already initialized DECL. */
8116 if (!DECL_INITIALIZED_P (decl)
8117 /* If !DECL_EXTERNAL then DECL is being defined. In the
8118 case of a static data member initialized inside the
8119 class-specifier, there can be an initializer even if DECL
8120 is *not* defined. */
8121 && (!DECL_EXTERNAL (decl) || init))
8123 init = check_initializer (decl, init, flags);
8124 /* Thread-local storage cannot be dynamically initialized. */
8125 if (DECL_THREAD_LOCAL (decl) && init)
8127 error ("`%D' is thread-local and so cannot be dynamically "
8128 "initialized", decl);
8129 init = NULL_TREE;
8131 /* Handle:
8133 [dcl.init]
8135 The memory occupied by any object of static storage
8136 duration is zero-initialized at program startup before
8137 any other initialization takes place.
8139 We cannot create an appropriate initializer until after
8140 the type of DECL is finalized. If DECL_INITIAL is set,
8141 then the DECL is statically initialized, and any
8142 necessary zero-initialization has already been performed. */
8143 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8144 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8145 /*nelts=*/NULL_TREE,
8146 /*static_storage_p=*/true);
8147 /* Remember that the initialization for this variable has
8148 taken place. */
8149 DECL_INITIALIZED_P (decl) = 1;
8151 /* If the variable has an array type, lay out the type, even if
8152 there is no initializer. It is valid to index through the
8153 array, and we must get TYPE_ALIGN set correctly on the array
8154 type. */
8155 else if (TREE_CODE (type) == ARRAY_TYPE)
8156 layout_type (type);
8159 /* Add this declaration to the statement-tree. This needs to happen
8160 after the call to check_initializer so that the DECL_STMT for a
8161 reference temp is added before the DECL_STMT for the reference itself. */
8162 if (building_stmt_tree ()
8163 && at_function_scope_p ()
8164 && TREE_CODE (decl) != RESULT_DECL)
8165 add_decl_stmt (decl);
8167 if (TREE_CODE (decl) == VAR_DECL)
8168 layout_var_decl (decl);
8170 /* Output the assembler code and/or RTL code for variables and functions,
8171 unless the type is an undefined structure or union.
8172 If not, it will get done when the type is completed. */
8173 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8174 || TREE_CODE (decl) == RESULT_DECL)
8176 if (TREE_CODE (decl) == VAR_DECL)
8177 maybe_commonize_var (decl);
8179 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8181 if (TREE_CODE (type) == FUNCTION_TYPE
8182 || TREE_CODE (type) == METHOD_TYPE)
8183 abstract_virtuals_error (decl,
8184 strip_array_types (TREE_TYPE (type)));
8185 else
8186 abstract_virtuals_error (decl, strip_array_types (type));
8188 if (TREE_CODE (decl) == FUNCTION_DECL
8189 || TREE_TYPE (decl) == error_mark_node)
8190 /* No initialization required. */
8192 else if (DECL_EXTERNAL (decl)
8193 && ! (DECL_LANG_SPECIFIC (decl)
8194 && DECL_NOT_REALLY_EXTERN (decl)))
8196 if (init)
8197 DECL_INITIAL (decl) = init;
8199 else
8201 /* A variable definition. */
8202 if (DECL_FUNCTION_SCOPE_P (decl))
8204 /* This is a local declaration. */
8205 if (doing_semantic_analysis_p ())
8206 maybe_inject_for_scope_var (decl);
8207 /* Initialize the local variable. */
8208 if (processing_template_decl)
8210 if (init || DECL_INITIAL (decl) == error_mark_node)
8211 DECL_INITIAL (decl) = init;
8213 else if (!TREE_STATIC (decl))
8214 initialize_local_var (decl, init);
8217 if (TREE_STATIC (decl))
8218 expand_static_init (decl, init);
8220 finish_end0:
8222 /* Undo call to `pushclass' that was done in `start_decl'
8223 due to initialization of qualified member variable.
8224 I.e., Foo::x = 10; */
8226 tree context = CP_DECL_CONTEXT (decl);
8227 if (context
8228 && TYPE_P (context)
8229 && (TREE_CODE (decl) == VAR_DECL
8230 /* We also have a pushclass done that we need to undo here
8231 if we're at top level and declare a method. */
8232 || TREE_CODE (decl) == FUNCTION_DECL)
8233 /* If size hasn't been set, we're still defining it,
8234 and therefore inside the class body; don't pop
8235 the binding level.. */
8236 && COMPLETE_TYPE_P (context)
8237 && context == current_class_type)
8238 pop_nested_class ();
8242 finish_end:
8244 if (was_readonly)
8245 TREE_READONLY (decl) = 1;
8248 /* This is here for a midend callback from c-common.c */
8250 void
8251 finish_decl (tree decl, tree init, tree asmspec_tree)
8253 cp_finish_decl (decl, init, asmspec_tree, 0);
8256 /* Returns a declaration for a VAR_DECL as if:
8258 extern "C" TYPE NAME;
8260 had been seen. Used to create compiler-generated global
8261 variables. */
8263 tree
8264 declare_global_var (tree name, tree type)
8266 tree decl;
8268 push_to_top_level ();
8269 decl = build_decl (VAR_DECL, name, type);
8270 TREE_PUBLIC (decl) = 1;
8271 DECL_EXTERNAL (decl) = 1;
8272 DECL_ARTIFICIAL (decl) = 1;
8273 pushdecl (decl);
8274 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8275 pop_from_top_level ();
8277 return decl;
8280 /* Returns a pointer to the `atexit' function. Note that if
8281 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8282 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8284 static tree
8285 get_atexit_node (void)
8287 tree atexit_fndecl;
8288 tree arg_types;
8289 tree fn_type;
8290 tree fn_ptr_type;
8291 const char *name;
8293 if (atexit_node)
8294 return atexit_node;
8296 if (flag_use_cxa_atexit)
8298 /* The declaration for `__cxa_atexit' is:
8300 int __cxa_atexit (void (*)(void *), void *, void *)
8302 We build up the argument types and then then function type
8303 itself. */
8305 /* First, build the pointer-to-function type for the first
8306 argument. */
8307 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8308 fn_type = build_function_type (void_type_node, arg_types);
8309 fn_ptr_type = build_pointer_type (fn_type);
8310 /* Then, build the rest of the argument types. */
8311 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8312 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8313 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8314 /* And the final __cxa_atexit type. */
8315 fn_type = build_function_type (integer_type_node, arg_types);
8316 fn_ptr_type = build_pointer_type (fn_type);
8317 name = "__cxa_atexit";
8319 else
8321 /* The declaration for `atexit' is:
8323 int atexit (void (*)());
8325 We build up the argument types and then then function type
8326 itself. */
8327 fn_type = build_function_type (void_type_node, void_list_node);
8328 fn_ptr_type = build_pointer_type (fn_type);
8329 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8330 /* Build the final atexit type. */
8331 fn_type = build_function_type (integer_type_node, arg_types);
8332 name = "atexit";
8335 /* Now, build the function declaration. */
8336 push_lang_context (lang_name_c);
8337 atexit_fndecl = build_library_fn_ptr (name, fn_type);
8338 mark_used (atexit_fndecl);
8339 pop_lang_context ();
8340 atexit_node = default_conversion (atexit_fndecl);
8342 return atexit_node;
8345 /* Returns the __dso_handle VAR_DECL. */
8347 static tree
8348 get_dso_handle_node (void)
8350 if (dso_handle_node)
8351 return dso_handle_node;
8353 /* Declare the variable. */
8354 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8355 ptr_type_node);
8357 return dso_handle_node;
8360 /* Begin a new function with internal linkage whose job will be simply
8361 to destroy some particular variable. */
8363 static tree
8364 start_cleanup_fn (void)
8366 static int counter = 0;
8367 int old_interface_only = interface_only;
8368 int old_interface_unknown = interface_unknown;
8369 char name[32];
8370 tree parmtypes;
8371 tree fntype;
8372 tree fndecl;
8374 push_to_top_level ();
8376 /* No need to mangle this. */
8377 push_lang_context (lang_name_c);
8379 interface_only = 0;
8380 interface_unknown = 1;
8382 /* Build the parameter-types. */
8383 parmtypes = void_list_node;
8384 /* Functions passed to __cxa_atexit take an additional parameter.
8385 We'll just ignore it. After we implement the new calling
8386 convention for destructors, we can eliminate the use of
8387 additional cleanup functions entirely in the -fnew-abi case. */
8388 if (flag_use_cxa_atexit)
8389 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8390 /* Build the function type itself. */
8391 fntype = build_function_type (void_type_node, parmtypes);
8392 /* Build the name of the function. */
8393 sprintf (name, "__tcf_%d", counter++);
8394 /* Build the function declaration. */
8395 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8396 /* It's a function with internal linkage, generated by the
8397 compiler. */
8398 TREE_PUBLIC (fndecl) = 0;
8399 DECL_ARTIFICIAL (fndecl) = 1;
8400 /* Make the function `inline' so that it is only emitted if it is
8401 actually needed. It is unlikely that it will be inlined, since
8402 it is only called via a function pointer, but we avoid unnecessary
8403 emissions this way. */
8404 DECL_INLINE (fndecl) = 1;
8405 /* Build the parameter. */
8406 if (flag_use_cxa_atexit)
8408 tree parmdecl;
8410 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8411 DECL_CONTEXT (parmdecl) = fndecl;
8412 TREE_USED (parmdecl) = 1;
8413 DECL_ARGUMENTS (fndecl) = parmdecl;
8416 pushdecl (fndecl);
8417 start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8419 interface_unknown = old_interface_unknown;
8420 interface_only = old_interface_only;
8422 pop_lang_context ();
8424 return current_function_decl;
8427 /* Finish the cleanup function begun by start_cleanup_fn. */
8429 static void
8430 end_cleanup_fn (void)
8432 expand_body (finish_function (0));
8434 pop_from_top_level ();
8437 /* Generate code to handle the destruction of DECL, an object with
8438 static storage duration. */
8440 void
8441 register_dtor_fn (tree decl)
8443 tree cleanup;
8444 tree compound_stmt;
8445 tree args;
8446 tree fcall;
8447 int saved_flag_access_control;
8449 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8450 return;
8452 /* Call build_cleanup before we enter the anonymous function so that
8453 any access checks will be done relative to the current scope,
8454 rather than the scope of the anonymous function. */
8455 build_cleanup (decl);
8457 /* Now start the function. */
8458 cleanup = start_cleanup_fn ();
8460 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8461 to the original function, rather than the anonymous one. That
8462 will make the back-end think that nested functions are in use,
8463 which causes confusion. */
8464 saved_flag_access_control = flag_access_control;
8465 scope_chain->check_access = flag_access_control = 0;
8466 fcall = build_cleanup (decl);
8467 scope_chain->check_access = flag_access_control = saved_flag_access_control;
8469 /* Create the body of the anonymous function. */
8470 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
8471 finish_expr_stmt (fcall);
8472 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
8473 end_cleanup_fn ();
8475 /* Call atexit with the cleanup function. */
8476 cxx_mark_addressable (cleanup);
8477 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8478 if (flag_use_cxa_atexit)
8480 args = tree_cons (NULL_TREE,
8481 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8482 NULL_TREE);
8483 args = tree_cons (NULL_TREE, null_pointer_node, args);
8484 args = tree_cons (NULL_TREE, cleanup, args);
8486 else
8487 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8488 finish_expr_stmt (build_function_call (get_atexit_node (), args));
8491 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8492 is its initializer. Generate code to handle the construction
8493 and destruction of DECL. */
8495 static void
8496 expand_static_init (tree decl, tree init)
8498 tree oldstatic;
8500 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8501 my_friendly_assert (TREE_STATIC (decl), 20021010);
8503 /* Some variables require no initialization. */
8504 if (!init
8505 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8506 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8507 return;
8509 oldstatic = value_member (decl, static_aggregates);
8511 if (oldstatic)
8513 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8514 error ("multiple initializations given for `%D'", decl);
8516 else if (! toplevel_bindings_p ())
8518 /* Emit code to perform this initialization but once. */
8519 tree if_stmt;
8520 tree then_clause;
8521 tree assignment;
8522 tree guard;
8523 tree guard_init;
8525 /* Emit code to perform this initialization but once. This code
8526 looks like:
8528 static int guard = 0;
8529 if (!guard) {
8530 // Do initialization.
8531 guard = 1;
8532 // Register variable for destruction at end of program.
8535 Note that the `temp' variable is only set to 1 *after* the
8536 initialization is complete. This ensures that an exception,
8537 thrown during the construction, will cause the variable to
8538 reinitialized when we pass through this code again, as per:
8540 [stmt.dcl]
8542 If the initialization exits by throwing an exception, the
8543 initialization is not complete, so it will be tried again
8544 the next time control enters the declaration.
8546 In theory, this process should be thread-safe, too; multiple
8547 threads should not be able to initialize the variable more
8548 than once. We don't yet attempt to ensure thread-safety. */
8550 /* Create the guard variable. */
8551 guard = get_guard (decl);
8553 /* Begin the conditional initialization. */
8554 if_stmt = begin_if_stmt ();
8555 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8556 then_clause = begin_compound_stmt (/*has_no_scope=*/0);
8558 /* Do the initialization itself. */
8559 assignment = init ? init : NULL_TREE;
8561 /* Once the assignment is complete, set TEMP to 1. Since the
8562 construction of the static object is complete at this point,
8563 we want to make sure TEMP is set to 1 even if a temporary
8564 constructed during the initialization throws an exception
8565 when it is destroyed. So, we combine the initialization and
8566 the assignment to TEMP into a single expression, ensuring
8567 that when we call finish_expr_stmt the cleanups will not be
8568 run until after TEMP is set to 1. */
8569 guard_init = set_guard (guard);
8570 if (assignment)
8572 assignment = tree_cons (NULL_TREE, assignment,
8573 build_tree_list (NULL_TREE,
8574 guard_init));
8575 assignment = build_compound_expr (assignment);
8577 else
8578 assignment = guard_init;
8579 finish_expr_stmt (assignment);
8581 /* Use atexit to register a function for destroying this static
8582 variable. */
8583 register_dtor_fn (decl);
8585 finish_compound_stmt (/*has_no_scope=*/0, then_clause);
8586 finish_then_clause (if_stmt);
8587 finish_if_stmt ();
8589 else
8590 static_aggregates = tree_cons (init, decl, static_aggregates);
8593 /* Finish the declaration of a catch-parameter. */
8595 tree
8596 start_handler_parms (tree declspecs, tree declarator)
8598 tree decl;
8599 if (declspecs)
8601 decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8602 1, NULL);
8603 if (decl == NULL_TREE)
8604 error ("invalid catch parameter");
8606 else
8607 decl = NULL_TREE;
8609 return decl;
8613 /* Make TYPE a complete type based on INITIAL_VALUE.
8614 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8615 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8618 complete_array_type (tree type, tree initial_value, int do_default)
8620 register tree maxindex = NULL_TREE;
8621 int value = 0;
8623 if (initial_value)
8625 /* An array of character type can be initialized from a
8626 brace-enclosed string constant. */
8627 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8628 && TREE_CODE (initial_value) == CONSTRUCTOR
8629 && CONSTRUCTOR_ELTS (initial_value)
8630 && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8631 == STRING_CST)
8632 && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8633 initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8635 /* Note MAXINDEX is really the maximum index, one less than the
8636 size. */
8637 if (TREE_CODE (initial_value) == STRING_CST)
8639 int eltsize
8640 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8641 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8642 / eltsize) - 1, 0);
8644 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8646 tree elts = CONSTRUCTOR_ELTS (initial_value);
8648 maxindex = ssize_int (-1);
8649 for (; elts; elts = TREE_CHAIN (elts))
8651 if (TREE_PURPOSE (elts))
8652 maxindex = TREE_PURPOSE (elts);
8653 else
8654 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8656 maxindex = copy_node (maxindex);
8658 else
8660 /* Make an error message unless that happened already. */
8661 if (initial_value != error_mark_node)
8662 value = 1;
8663 else
8664 initial_value = NULL_TREE;
8666 /* Prevent further error messages. */
8667 maxindex = build_int_2 (0, 0);
8671 if (!maxindex)
8673 if (do_default)
8674 maxindex = build_int_2 (0, 0);
8675 value = 2;
8678 if (maxindex)
8680 tree itype;
8681 tree domain;
8683 domain = build_index_type (maxindex);
8684 TYPE_DOMAIN (type) = domain;
8686 if (! TREE_TYPE (maxindex))
8687 TREE_TYPE (maxindex) = domain;
8688 if (initial_value)
8689 itype = TREE_TYPE (initial_value);
8690 else
8691 itype = NULL;
8692 if (itype && !TYPE_DOMAIN (itype))
8693 TYPE_DOMAIN (itype) = domain;
8694 /* The type of the main variant should never be used for arrays
8695 of different sizes. It should only ever be completed with the
8696 size of the array. */
8697 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8698 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8701 /* Lay out the type now that we can get the real answer. */
8703 layout_type (type);
8705 return value;
8708 /* Return zero if something is declared to be a member of type
8709 CTYPE when in the context of CUR_TYPE. STRING is the error
8710 message to print in that case. Otherwise, quietly return 1. */
8712 static int
8713 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8715 if (ctype && ctype != cur_type)
8717 if (flags == DTOR_FLAG)
8718 error ("destructor for alien class `%T' cannot be a member",
8719 ctype);
8720 else
8721 error ("constructor for alien class `%T' cannot be a member",
8722 ctype);
8723 return 0;
8725 return 1;
8728 /* Subroutine of `grokdeclarator'. */
8730 /* Generate errors possibly applicable for a given set of specifiers.
8731 This is for ARM $7.1.2. */
8733 static void
8734 bad_specifiers (tree object,
8735 const char* type,
8736 int virtualp,
8737 int quals,
8738 int inlinep,
8739 int friendp,
8740 int raises)
8742 if (virtualp)
8743 error ("`%D' declared as a `virtual' %s", object, type);
8744 if (inlinep)
8745 error ("`%D' declared as an `inline' %s", object, type);
8746 if (quals)
8747 error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8748 object, type);
8749 if (friendp)
8750 cp_error_at ("`%D' declared as a friend", object);
8751 if (raises
8752 && (TREE_CODE (object) == TYPE_DECL
8753 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8754 && !TYPE_REFFN_P (TREE_TYPE (object))
8755 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8756 cp_error_at ("`%D' declared with an exception specification", object);
8759 /* CTYPE is class type, or null if non-class.
8760 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8761 or METHOD_TYPE.
8762 DECLARATOR is the function's name.
8763 VIRTUALP is truthvalue of whether the function is virtual or not.
8764 FLAGS are to be passed through to `grokclassfn'.
8765 QUALS are qualifiers indicating whether the function is `const'
8766 or `volatile'.
8767 RAISES is a list of exceptions that this function can raise.
8768 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8769 not look, and -1 if we should not call `grokclassfn' at all.
8771 Returns `NULL_TREE' if something goes wrong, after issuing
8772 applicable error messages. */
8774 static tree
8775 grokfndecl (tree ctype,
8776 tree type,
8777 tree declarator,
8778 tree orig_declarator,
8779 int virtualp,
8780 enum overload_flags flags,
8781 tree quals,
8782 tree raises,
8783 int check,
8784 int friendp,
8785 int publicp,
8786 int inlinep,
8787 int funcdef_flag,
8788 int template_count,
8789 tree in_namespace)
8791 tree decl;
8792 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8793 int has_default_arg = 0;
8794 tree t;
8796 if (raises)
8797 type = build_exception_variant (type, raises);
8799 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8800 /* Propagate volatile out from type to decl. */
8801 if (TYPE_VOLATILE (type))
8802 TREE_THIS_VOLATILE (decl) = 1;
8804 /* If this decl has namespace scope, set that up. */
8805 if (in_namespace)
8806 set_decl_namespace (decl, in_namespace, friendp);
8807 else if (!ctype)
8808 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8810 /* `main' and builtins have implicit 'C' linkage. */
8811 if ((MAIN_NAME_P (declarator)
8812 || (IDENTIFIER_LENGTH (declarator) > 10
8813 && IDENTIFIER_POINTER (declarator)[0] == '_'
8814 && IDENTIFIER_POINTER (declarator)[1] == '_'
8815 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8816 && current_lang_name == lang_name_cplusplus
8817 && ctype == NULL_TREE
8818 /* NULL_TREE means global namespace. */
8819 && DECL_CONTEXT (decl) == NULL_TREE)
8820 SET_DECL_LANGUAGE (decl, lang_c);
8822 /* Should probably propagate const out from type to decl I bet (mrs). */
8823 if (staticp)
8825 DECL_STATIC_FUNCTION_P (decl) = 1;
8826 DECL_CONTEXT (decl) = ctype;
8829 if (ctype)
8830 DECL_CONTEXT (decl) = ctype;
8832 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8834 if (processing_template_decl)
8835 error ("cannot declare `::main' to be a template");
8836 if (inlinep)
8837 error ("cannot declare `::main' to be inline");
8838 if (!publicp)
8839 error ("cannot declare `::main' to be static");
8840 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8841 integer_type_node))
8842 error ("`main' must return `int'");
8843 inlinep = 0;
8844 publicp = 1;
8847 /* Members of anonymous types and local classes have no linkage; make
8848 them internal. */
8849 /* FIXME what if it gets a name from typedef? */
8850 if (ctype && (TYPE_ANONYMOUS_P (ctype)
8851 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8852 publicp = 0;
8854 if (publicp)
8856 /* [basic.link]: A name with no linkage (notably, the name of a class
8857 or enumeration declared in a local scope) shall not be used to
8858 declare an entity with linkage.
8860 Only check this for public decls for now. */
8861 t = no_linkage_check (TREE_TYPE (decl));
8862 if (t)
8864 if (TYPE_ANONYMOUS_P (t))
8866 if (DECL_EXTERN_C_P (decl))
8867 /* Allow this; it's pretty common in C. */;
8868 else
8870 pedwarn ("non-local function `%#D' uses anonymous type",
8871 decl);
8872 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8873 cp_pedwarn_at ("\
8874 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8875 TYPE_NAME (t));
8878 else
8879 pedwarn ("non-local function `%#D' uses local type `%T'",
8880 decl, t);
8884 TREE_PUBLIC (decl) = publicp;
8885 if (! publicp)
8887 DECL_INTERFACE_KNOWN (decl) = 1;
8888 DECL_NOT_REALLY_EXTERN (decl) = 1;
8891 DID_INLINE_FUNC (decl) = 0;
8892 /* If the declaration was declared inline, mark it as such. */
8893 if (inlinep)
8894 DECL_DECLARED_INLINE_P (decl) = 1;
8895 /* We inline functions that are explicitly declared inline, or, when
8896 the user explicitly asks us to, all functions. */
8897 if (DECL_DECLARED_INLINE_P (decl))
8898 DECL_INLINE (decl) = 1;
8899 if (flag_inline_trees == 2 && !DECL_INLINE (decl))
8901 DID_INLINE_FUNC (decl) = 1;
8902 DECL_INLINE (decl) = 1;
8905 DECL_EXTERNAL (decl) = 1;
8906 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8908 error ("%smember function `%D' cannot have `%T' method qualifier",
8909 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8910 quals = NULL_TREE;
8913 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8914 grok_op_properties (decl, friendp);
8916 if (ctype && decl_function_context (decl))
8917 DECL_NO_STATIC_CHAIN (decl) = 1;
8919 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8920 if (TREE_PURPOSE (t)
8921 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8923 has_default_arg = 1;
8924 break;
8927 if (friendp
8928 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8930 if (funcdef_flag)
8931 error
8932 ("defining explicit specialization `%D' in friend declaration",
8933 orig_declarator);
8934 else
8936 tree fns = TREE_OPERAND (orig_declarator, 0);
8937 tree args = TREE_OPERAND (orig_declarator, 1);
8939 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8941 /* Something like `template <class T> friend void f<T>()'. */
8942 error ("invalid use of template-id `%D' in declaration of primary template",
8943 orig_declarator);
8944 return NULL_TREE;
8948 /* A friend declaration of the form friend void f<>(). Record
8949 the information in the TEMPLATE_ID_EXPR. */
8950 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8952 if (TREE_CODE (fns) == COMPONENT_REF)
8954 /* Due to bison parser ickiness, we will have already looked
8955 up an operator_name or PFUNCNAME within the current class
8956 (see template_id in parse.y). If the current class contains
8957 such a name, we'll get a COMPONENT_REF here. Undo that. */
8959 my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
8960 == current_class_type, 20001120);
8961 fns = TREE_OPERAND (fns, 1);
8963 my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
8964 || TREE_CODE (fns) == LOOKUP_EXPR
8965 || TREE_CODE (fns) == OVERLOAD, 20001120);
8966 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
8968 if (has_default_arg)
8970 error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8971 decl);
8972 return NULL_TREE;
8975 if (inlinep)
8977 error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8978 decl);
8979 return NULL_TREE;
8984 if (funcdef_flag)
8985 /* Make the init_value nonzero so pushdecl knows this is not
8986 tentative. error_mark_node is replaced later with the BLOCK. */
8987 DECL_INITIAL (decl) = error_mark_node;
8989 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8990 TREE_NOTHROW (decl) = 1;
8992 /* Caller will do the rest of this. */
8993 if (check < 0)
8994 return decl;
8996 if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
8997 DECL_CONSTRUCTOR_P (decl) = 1;
8999 /* Function gets the ugly name, field gets the nice one. This call
9000 may change the type of the function (because of default
9001 parameters)! */
9002 if (ctype != NULL_TREE)
9003 grokclassfn (ctype, decl, flags, quals);
9005 decl = check_explicit_specialization (orig_declarator, decl,
9006 template_count,
9007 2 * (funcdef_flag != 0) +
9008 4 * (friendp != 0));
9009 if (decl == error_mark_node)
9010 return NULL_TREE;
9012 if (ctype != NULL_TREE
9013 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9014 && check)
9016 tree old_decl;
9018 old_decl = check_classfn (ctype, decl);
9020 if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9021 /* Because grokfndecl is always supposed to return a
9022 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9023 here. We depend on our callers to figure out that its
9024 really a template that's being returned. */
9025 old_decl = DECL_TEMPLATE_RESULT (old_decl);
9027 if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9028 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9030 /* Remove the `this' parm added by grokclassfn.
9031 XXX Isn't this done in start_function, too? */
9032 revert_static_member_fn (decl);
9033 last_function_parms = TREE_CHAIN (last_function_parms);
9035 if (old_decl && DECL_ARTIFICIAL (old_decl))
9036 error ("definition of implicitly-declared `%D'", old_decl);
9038 if (old_decl)
9040 bool ok;
9042 /* Since we've smashed OLD_DECL to its
9043 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9044 if (TREE_CODE (decl) == TEMPLATE_DECL)
9045 decl = DECL_TEMPLATE_RESULT (decl);
9047 /* Attempt to merge the declarations. This can fail, in
9048 the case of some invalid specialization declarations. */
9049 push_scope (ctype);
9050 ok = duplicate_decls (decl, old_decl);
9051 pop_scope (ctype);
9052 if (!ok)
9054 error ("no `%#D' member function declared in class `%T'",
9055 decl, ctype);
9056 return NULL_TREE;
9058 return old_decl;
9062 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9063 return NULL_TREE;
9065 if (ctype == NULL_TREE || check)
9066 return decl;
9068 if (virtualp)
9069 DECL_VIRTUAL_P (decl) = 1;
9071 return decl;
9074 /* Create a VAR_DECL named NAME with the indicated TYPE.
9076 If SCOPE is non-NULL, it is the class type or namespace containing
9077 the variable. If SCOPE is NULL, the variable should is created in
9078 the innermost enclosings scope. */
9080 static tree
9081 grokvardecl (tree type,
9082 tree name,
9083 RID_BIT_TYPE * specbits_in,
9084 int initialized,
9085 int constp,
9086 tree scope)
9088 tree decl;
9089 RID_BIT_TYPE specbits;
9091 my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE,
9092 20020808);
9094 specbits = *specbits_in;
9096 /* Compute the scope in which to place the variable. */
9097 if (!scope)
9099 /* An explicit "extern" specifier indicates a namespace-scope
9100 variable. */
9101 if (RIDBIT_SETP (RID_EXTERN, specbits))
9102 scope = current_namespace;
9103 else if (!at_function_scope_p ())
9105 scope = current_scope ();
9106 if (!scope)
9107 scope = current_namespace;
9111 if (scope
9112 && (/* If the variable is a namespace-scope variable declared in a
9113 template, we need DECL_LANG_SPECIFIC. */
9114 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9115 /* Similarly for namespace-scope variables with language linkage
9116 other than C++. */
9117 || (TREE_CODE (scope) == NAMESPACE_DECL
9118 && current_lang_name != lang_name_cplusplus)
9119 /* Similarly for static data members. */
9120 || TYPE_P (scope)))
9121 decl = build_lang_decl (VAR_DECL, name, type);
9122 else
9123 decl = build_decl (VAR_DECL, name, type);
9125 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9126 set_decl_namespace (decl, scope, 0);
9127 else
9128 DECL_CONTEXT (decl) = scope;
9130 if (name && scope && current_lang_name != lang_name_c)
9131 /* We can't mangle lazily here because we don't have any
9132 way to recover whether or not a variable was `extern
9133 "C"' later. */
9134 mangle_decl (decl);
9136 if (RIDBIT_SETP (RID_EXTERN, specbits))
9138 DECL_THIS_EXTERN (decl) = 1;
9139 DECL_EXTERNAL (decl) = !initialized;
9142 /* In class context, static means one per class,
9143 public access, and static storage. */
9144 if (DECL_CLASS_SCOPE_P (decl))
9146 TREE_PUBLIC (decl) = 1;
9147 TREE_STATIC (decl) = 1;
9148 DECL_EXTERNAL (decl) = 0;
9150 /* At top level, either `static' or no s.c. makes a definition
9151 (perhaps tentative), and absence of `static' makes it public. */
9152 else if (toplevel_bindings_p ())
9154 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9155 && (DECL_THIS_EXTERN (decl) || ! constp));
9156 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9158 /* Not at top level, only `static' makes a static definition. */
9159 else
9161 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9162 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9165 if (RIDBIT_SETP (RID_THREAD, specbits))
9167 if (targetm.have_tls)
9168 DECL_THREAD_LOCAL (decl) = 1;
9169 else
9170 /* A mere warning is sure to result in improper semantics
9171 at runtime. Don't bother to allow this to compile. */
9172 error ("thread-local storage not supported for this target");
9175 if (TREE_PUBLIC (decl))
9177 /* [basic.link]: A name with no linkage (notably, the name of a class
9178 or enumeration declared in a local scope) shall not be used to
9179 declare an entity with linkage.
9181 Only check this for public decls for now. */
9182 tree t = no_linkage_check (TREE_TYPE (decl));
9183 if (t)
9185 if (TYPE_ANONYMOUS_P (t))
9186 /* Ignore for now; `enum { foo } e' is pretty common. */;
9187 else
9188 pedwarn ("non-local variable `%#D' uses local type `%T'",
9189 decl, t);
9193 return decl;
9196 /* Create and return a canonical pointer to member function type, for
9197 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9199 tree
9200 build_ptrmemfunc_type (tree type)
9202 tree field, fields;
9203 tree t;
9204 tree unqualified_variant = NULL_TREE;
9206 if (type == error_mark_node)
9207 return type;
9209 /* If a canonical type already exists for this type, use it. We use
9210 this method instead of type_hash_canon, because it only does a
9211 simple equality check on the list of field members. */
9213 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9214 return t;
9216 /* Make sure that we always have the unqualified pointer-to-member
9217 type first. */
9218 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9219 unqualified_variant
9220 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9222 t = make_aggr_type (RECORD_TYPE);
9223 /* Let the front-end know this is a pointer to member function... */
9224 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9225 /* ... and not really an aggregate. */
9226 SET_IS_AGGR_TYPE (t, 0);
9228 field = build_decl (FIELD_DECL, pfn_identifier, type);
9229 fields = field;
9231 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9232 TREE_CHAIN (field) = fields;
9233 fields = field;
9235 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9237 /* Zap out the name so that the back-end will give us the debugging
9238 information for this anonymous RECORD_TYPE. */
9239 TYPE_NAME (t) = NULL_TREE;
9241 /* If this is not the unqualified form of this pointer-to-member
9242 type, set the TYPE_MAIN_VARIANT for this type to be the
9243 unqualified type. Since they are actually RECORD_TYPEs that are
9244 not variants of each other, we must do this manually. */
9245 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9247 t = build_qualified_type (t, cp_type_quals (type));
9248 TYPE_MAIN_VARIANT (t) = unqualified_variant;
9249 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9250 TYPE_NEXT_VARIANT (unqualified_variant) = t;
9253 /* Cache this pointer-to-member type so that we can find it again
9254 later. */
9255 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9257 /* Seems to be wanted. */
9258 CLASSTYPE_GOT_SEMICOLON (t) = 1;
9260 return t;
9263 /* Create and return a pointer to data member type. */
9265 tree
9266 build_ptrmem_type (tree class_type, tree member_type)
9268 return build_pointer_type (build_offset_type (class_type, member_type));
9271 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9272 Check to see that the definition is valid. Issue appropriate error
9273 messages. Return 1 if the definition is particularly bad, or 0
9274 otherwise. */
9277 check_static_variable_definition (tree decl, tree type)
9279 /* Motion 10 at San Diego: If a static const integral data member is
9280 initialized with an integral constant expression, the initializer
9281 may appear either in the declaration (within the class), or in
9282 the definition, but not both. If it appears in the class, the
9283 member is a member constant. The file-scope definition is always
9284 required. */
9285 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9287 error ("invalid in-class initialization of static data member of non-integral type `%T'",
9288 type);
9289 /* If we just return the declaration, crashes will sometimes
9290 occur. We therefore return void_type_node, as if this was a
9291 friend declaration, to cause callers to completely ignore
9292 this declaration. */
9293 return 1;
9295 else if (!CP_TYPE_CONST_P (type))
9296 error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9297 decl);
9298 else if (pedantic && !INTEGRAL_TYPE_P (type))
9299 pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9301 return 0;
9304 /* Given the SIZE (i.e., number of elements) in an array, compute an
9305 appropriate index type for the array. If non-NULL, NAME is the
9306 name of the thing being declared. */
9308 tree
9309 compute_array_index_type (tree name, tree size)
9311 tree itype;
9313 /* If this involves a template parameter, it will be a constant at
9314 instantiation time, but we don't know what the value is yet.
9315 Even if no template parameters are involved, we may an expression
9316 that is not a constant; we don't even simplify `1 + 2' when
9317 processing a template. */
9318 if (processing_template_decl)
9320 /* Resolve a qualified reference to an enumerator or static
9321 const data member of ours. */
9322 if (TREE_CODE (size) == SCOPE_REF
9323 && TREE_OPERAND (size, 0) == current_class_type)
9325 tree t = lookup_field (current_class_type,
9326 TREE_OPERAND (size, 1), 0, false);
9327 if (t)
9328 size = t;
9331 return build_index_type (build_min (MINUS_EXPR, sizetype,
9332 size, integer_one_node));
9335 /* The size might be the result of a cast. */
9336 STRIP_TYPE_NOPS (size);
9338 /* It might be a const variable or enumeration constant. */
9339 size = decl_constant_value (size);
9341 /* The array bound must be an integer type. */
9342 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9343 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9344 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9346 if (name)
9347 error ("size of array `%D' has non-integer type", name);
9348 else
9349 error ("size of array has non-integer type");
9350 size = integer_one_node;
9353 /* Normally, the array-bound will be a constant. */
9354 if (TREE_CODE (size) == INTEGER_CST)
9356 /* Check to see if the array bound overflowed. Make that an
9357 error, no matter how generous we're being. */
9358 int old_flag_pedantic_errors = flag_pedantic_errors;
9359 int old_pedantic = pedantic;
9360 pedantic = flag_pedantic_errors = 1;
9361 constant_expression_warning (size);
9362 pedantic = old_pedantic;
9363 flag_pedantic_errors = old_flag_pedantic_errors;
9365 /* An array must have a positive number of elements. */
9366 if (INT_CST_LT (size, integer_zero_node))
9368 if (name)
9369 error ("size of array `%D' is negative", name);
9370 else
9371 error ("size of array is negative");
9372 size = integer_one_node;
9374 /* Except that an extension we allow zero-sized arrays. We
9375 always allow them in system headers because glibc uses
9376 them. */
9377 else if (integer_zerop (size) && pedantic && !in_system_header)
9379 if (name)
9380 pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9381 else
9382 pedwarn ("ISO C++ forbids zero-size array");
9385 else if (TREE_CONSTANT (size))
9387 /* `(int) &fn' is not a valid array bound. */
9388 if (name)
9389 error ("size of array `%D' is not an integral constant-expression",
9390 name);
9391 else
9392 error ("size of array is not an integral constant-expression");
9395 /* Compute the index of the largest element in the array. It is
9396 one less than the number of elements in the array. */
9397 itype
9398 = fold (cp_build_binary_op (MINUS_EXPR,
9399 cp_convert (ssizetype, size),
9400 cp_convert (ssizetype,
9401 integer_one_node)));
9403 /* Check for variable-sized arrays. We allow such things as an
9404 extension, even though they are not allowed in ANSI/ISO C++. */
9405 if (!TREE_CONSTANT (itype))
9407 if (pedantic)
9409 if (name)
9410 pedwarn ("ISO C++ forbids variable-size array `%D'",
9411 name);
9412 else
9413 pedwarn ("ISO C++ forbids variable-size array");
9416 /* Create a variable-sized array index type. */
9417 itype = variable_size (itype);
9419 /* Make sure that there was no overflow when creating to a signed
9420 index type. (For example, on a 32-bit machine, an array with
9421 size 2^32 - 1 is too big.) */
9422 else if (TREE_OVERFLOW (itype))
9424 error ("overflow in array dimension");
9425 TREE_OVERFLOW (itype) = 0;
9428 /* Create and return the appropriate index type. */
9429 return build_index_type (itype);
9432 /* Returns the scope (if any) in which the entity declared by
9433 DECLARATOR will be located. If the entity was declared with an
9434 unqualified name, NULL_TREE is returned. */
9436 tree
9437 get_scope_of_declarator (tree declarator)
9439 if (!declarator)
9440 return NULL_TREE;
9442 switch (TREE_CODE (declarator))
9444 case CALL_EXPR:
9445 case ARRAY_REF:
9446 case INDIRECT_REF:
9447 case ADDR_EXPR:
9448 /* For any of these, the main declarator is the first operand. */
9449 return get_scope_of_declarator (TREE_OPERAND
9450 (declarator, 0));
9452 case SCOPE_REF:
9453 /* For a pointer-to-member, continue descending. */
9454 if (TREE_CODE (TREE_OPERAND (declarator, 1))
9455 == INDIRECT_REF)
9456 return get_scope_of_declarator (TREE_OPERAND
9457 (declarator, 1));
9458 /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9459 which the declaration occurs is the first operand. */
9460 return TREE_OPERAND (declarator, 0);
9462 case TREE_LIST:
9463 /* Attributes to be applied. The declarator is TREE_VALUE. */
9464 return get_scope_of_declarator (TREE_VALUE (declarator));
9466 default:
9467 /* Otherwise, we have a declarator-id which is not a qualified
9468 name; the entity will be declared in the current scope. */
9469 return NULL_TREE;
9473 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9474 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9475 with this type. */
9477 static tree
9478 create_array_type_for_decl (tree name, tree type, tree size)
9480 tree itype = NULL_TREE;
9481 const char* error_msg;
9483 /* If things have already gone awry, bail now. */
9484 if (type == error_mark_node || size == error_mark_node)
9485 return error_mark_node;
9487 /* Assume that everything will go OK. */
9488 error_msg = NULL;
9490 /* There are some types which cannot be array elements. */
9491 switch (TREE_CODE (type))
9493 case VOID_TYPE:
9494 error_msg = "array of void";
9495 break;
9497 case FUNCTION_TYPE:
9498 error_msg = "array of functions";
9499 break;
9501 case REFERENCE_TYPE:
9502 error_msg = "array of references";
9503 break;
9505 case OFFSET_TYPE:
9506 error_msg = "array of data members";
9507 break;
9509 case METHOD_TYPE:
9510 error_msg = "array of function members";
9511 break;
9513 default:
9514 break;
9517 /* If something went wrong, issue an error-message and return. */
9518 if (error_msg)
9520 if (name)
9521 error ("declaration of `%D' as %s", name, error_msg);
9522 else
9523 error ("creating %s", error_msg);
9525 return error_mark_node;
9528 /* [dcl.array]
9530 The constant expressions that specify the bounds of the arrays
9531 can be omitted only for the first member of the sequence. */
9532 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9534 if (name)
9535 error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9536 name);
9537 else
9538 error ("multidimensional array must have bounds for all dimensions except the first");
9540 return error_mark_node;
9543 /* Figure out the index type for the array. */
9544 if (size)
9545 itype = compute_array_index_type (name, size);
9547 return build_cplus_array_type (type, itype);
9550 /* Check that it's OK to declare a function with the indicated TYPE.
9551 SFK indicates the kind of special function (if any) that this
9552 function is. OPTYPE is the type given in a conversion operator
9553 declaration. Returns the actual return type of the function; that
9554 may be different than TYPE if an error occurs, or for certain
9555 special functions. */
9557 static tree
9558 check_special_function_return_type (special_function_kind sfk,
9559 tree type,
9560 tree optype)
9562 switch (sfk)
9564 case sfk_constructor:
9565 if (type)
9566 error ("return type specification for constructor invalid");
9568 type = void_type_node;
9569 break;
9571 case sfk_destructor:
9572 if (type)
9573 error ("return type specification for destructor invalid");
9574 type = void_type_node;
9575 break;
9577 case sfk_conversion:
9578 if (type && !same_type_p (type, optype))
9579 error ("operator `%T' declared to return `%T'", optype, type);
9580 else if (type)
9581 pedwarn ("return type specified for `operator %T'", optype);
9582 type = optype;
9583 break;
9585 default:
9586 abort ();
9587 break;
9590 return type;
9593 /* Given declspecs and a declarator (abstract or otherwise), determine
9594 the name and type of the object declared and construct a DECL node
9595 for it.
9597 DECLSPECS is a chain of tree_list nodes whose value fields
9598 are the storage classes and type specifiers.
9600 DECL_CONTEXT says which syntactic context this declaration is in:
9601 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9602 FUNCDEF for a function definition. Like NORMAL but a few different
9603 error messages in each case. Return value may be zero meaning
9604 this definition is too screwy to try to parse.
9605 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9606 handle member functions (which have FIELD context).
9607 Return value may be zero meaning this definition is too screwy to
9608 try to parse.
9609 PARM for a parameter declaration (either within a function prototype
9610 or before a function body). Make a PARM_DECL, or return void_type_node.
9611 CATCHPARM for a parameter declaration before a catch clause.
9612 TYPENAME if for a typename (in a cast or sizeof).
9613 Don't make a DECL node; just return the ..._TYPE node.
9614 FIELD for a struct or union field; make a FIELD_DECL.
9615 BITFIELD for a field with specified width.
9616 INITIALIZED is 1 if the decl has an initializer.
9618 ATTRLIST is a pointer to the list of attributes, which may be NULL
9619 if there are none; *ATTRLIST may be modified if attributes from inside
9620 the declarator should be applied to the declaration.
9622 When this function is called, scoping variables (such as
9623 CURRENT_CLASS_TYPE) should reflect the scope in which the
9624 declaration occurs, not the scope in which the new declaration will
9625 be placed. For example, on:
9627 void S::f() { ... }
9629 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9630 should not be `S'. */
9632 tree
9633 grokdeclarator (tree declarator,
9634 tree declspecs,
9635 enum decl_context decl_context,
9636 int initialized,
9637 tree* attrlist)
9639 RID_BIT_TYPE specbits;
9640 int nclasses = 0;
9641 tree spec;
9642 tree type = NULL_TREE;
9643 int longlong = 0;
9644 int type_quals;
9645 int virtualp, explicitp, friendp, inlinep, staticp;
9646 int explicit_int = 0;
9647 int explicit_char = 0;
9648 int defaulted_int = 0;
9649 int extern_langp = 0;
9650 tree dependant_name = NULL_TREE;
9652 tree typedef_decl = NULL_TREE;
9653 const char *name;
9654 tree typedef_type = NULL_TREE;
9655 int funcdef_flag = 0;
9656 enum tree_code innermost_code = ERROR_MARK;
9657 int bitfield = 0;
9658 #if 0
9659 /* See the code below that used this. */
9660 tree decl_attr = NULL_TREE;
9661 #endif
9663 /* Keep track of what sort of function is being processed
9664 so that we can warn about default return values, or explicit
9665 return values which do not match prescribed defaults. */
9666 special_function_kind sfk = sfk_none;
9668 tree dname = NULL_TREE;
9669 tree ctype = current_class_type;
9670 tree ctor_return_type = NULL_TREE;
9671 enum overload_flags flags = NO_SPECIAL;
9672 tree quals = NULL_TREE;
9673 tree raises = NULL_TREE;
9674 int template_count = 0;
9675 tree in_namespace = NULL_TREE;
9676 tree returned_attrs = NULL_TREE;
9677 tree scope = NULL_TREE;
9679 RIDBIT_RESET_ALL (specbits);
9680 if (decl_context == FUNCDEF)
9681 funcdef_flag = 1, decl_context = NORMAL;
9682 else if (decl_context == MEMFUNCDEF)
9683 funcdef_flag = -1, decl_context = FIELD;
9684 else if (decl_context == BITFIELD)
9685 bitfield = 1, decl_context = FIELD;
9687 /* Look inside a declarator for the name being declared
9688 and get it as a string, for an error message. */
9690 tree *next = &declarator;
9691 register tree decl;
9692 name = NULL;
9694 while (next && *next)
9696 decl = *next;
9697 switch (TREE_CODE (decl))
9699 case TREE_LIST:
9700 /* For attributes. */
9701 next = &TREE_VALUE (decl);
9702 break;
9704 case COND_EXPR:
9705 ctype = NULL_TREE;
9706 next = &TREE_OPERAND (decl, 0);
9707 break;
9709 case BIT_NOT_EXPR: /* For C++ destructors! */
9711 tree name = TREE_OPERAND (decl, 0);
9712 tree rename = NULL_TREE;
9714 my_friendly_assert (flags == NO_SPECIAL, 152);
9715 flags = DTOR_FLAG;
9716 sfk = sfk_destructor;
9717 if (TYPE_P (name))
9718 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9719 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9720 if (ctype == NULL_TREE)
9722 if (current_class_type == NULL_TREE)
9724 error ("destructors must be member functions");
9725 flags = NO_SPECIAL;
9727 else
9729 tree t = constructor_name (current_class_type);
9730 if (t != name)
9731 rename = t;
9734 else
9736 tree t = constructor_name (ctype);
9737 if (t != name)
9738 rename = t;
9741 if (rename)
9743 error ("destructor `%T' must match class name `%T'",
9744 name, rename);
9745 TREE_OPERAND (decl, 0) = rename;
9747 next = &name;
9749 break;
9751 case ADDR_EXPR: /* C++ reference declaration */
9752 /* Fall through. */
9753 case ARRAY_REF:
9754 case INDIRECT_REF:
9755 ctype = NULL_TREE;
9756 innermost_code = TREE_CODE (decl);
9757 next = &TREE_OPERAND (decl, 0);
9758 break;
9760 case CALL_EXPR:
9761 innermost_code = TREE_CODE (decl);
9762 if (decl_context == FIELD && ctype == NULL_TREE)
9763 ctype = current_class_type;
9764 if (ctype
9765 && TREE_OPERAND (decl, 0)
9766 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9767 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9768 ctype)))
9769 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9770 next = &TREE_OPERAND (decl, 0);
9771 decl = *next;
9772 if (ctype != NULL_TREE
9773 && decl != NULL_TREE && flags != DTOR_FLAG
9774 && constructor_name_p (decl, ctype))
9776 sfk = sfk_constructor;
9777 ctor_return_type = ctype;
9779 ctype = NULL_TREE;
9780 break;
9782 case TEMPLATE_ID_EXPR:
9784 tree fns = TREE_OPERAND (decl, 0);
9786 if (TREE_CODE (fns) == LOOKUP_EXPR)
9787 fns = TREE_OPERAND (fns, 0);
9789 dname = fns;
9790 if (TREE_CODE (dname) == COMPONENT_REF)
9791 dname = TREE_OPERAND (dname, 1);
9792 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9794 my_friendly_assert (is_overloaded_fn (dname),
9795 19990331);
9796 dname = DECL_NAME (get_first_fn (dname));
9799 /* Fall through. */
9801 case IDENTIFIER_NODE:
9802 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9803 dname = decl;
9805 next = 0;
9807 if (C_IS_RESERVED_WORD (dname))
9809 error ("declarator-id missing; using reserved word `%D'",
9810 dname);
9811 name = IDENTIFIER_POINTER (dname);
9813 else if (!IDENTIFIER_TYPENAME_P (dname))
9814 name = IDENTIFIER_POINTER (dname);
9815 else
9817 my_friendly_assert (flags == NO_SPECIAL, 154);
9818 flags = TYPENAME_FLAG;
9819 ctor_return_type = TREE_TYPE (dname);
9820 sfk = sfk_conversion;
9821 if (IDENTIFIER_GLOBAL_VALUE (dname)
9822 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9823 == TYPE_DECL))
9824 name = IDENTIFIER_POINTER (dname);
9825 else
9826 name = "<invalid operator>";
9828 break;
9830 /* C++ extension */
9831 case SCOPE_REF:
9833 /* Perform error checking, and decide on a ctype. */
9834 tree cname = TREE_OPERAND (decl, 0);
9835 if (cname == NULL_TREE)
9836 ctype = NULL_TREE;
9837 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9839 ctype = NULL_TREE;
9840 in_namespace = TREE_OPERAND (decl, 0);
9842 else if (! is_aggr_type (cname, 1))
9843 ctype = NULL_TREE;
9844 /* Must test TREE_OPERAND (decl, 1), in case user gives
9845 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9846 else if (TREE_OPERAND (decl, 1)
9847 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9848 ctype = cname;
9849 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9850 || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9852 /* This might be declaring a member of a template
9853 parm to be a friend. */
9854 ctype = cname;
9855 dependant_name = TREE_OPERAND (decl, 1);
9857 else if (ctype == NULL_TREE)
9858 ctype = cname;
9859 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9861 else
9863 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9865 error ("type `%T' is not derived from type `%T'",
9866 cname, ctype);
9867 ctype = NULL_TREE;
9869 else
9870 ctype = cname;
9873 /* It is valid to write:
9875 class C { void f(); };
9876 typedef C D;
9877 void D::f();
9879 The standard is not clear about whether `typedef const C D' is
9880 legal; as of 2002-09-15 the committee is considering
9881 that question. EDG 3.0 allows that syntax.
9882 Therefore, we do as well. */
9883 if (ctype)
9884 ctype = TYPE_MAIN_VARIANT (ctype);
9885 /* Update the declarator so that when we process it
9886 again the correct type is present. */
9887 TREE_OPERAND (decl, 0) = ctype;
9889 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9890 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9891 ctype))
9892 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9893 next = &TREE_OPERAND (decl, 1);
9894 decl = *next;
9895 if (ctype)
9897 if (TREE_CODE (decl) == IDENTIFIER_NODE
9898 && constructor_name_p (decl, ctype))
9900 sfk = sfk_constructor;
9901 ctor_return_type = ctype;
9903 else if (TREE_CODE (decl) == BIT_NOT_EXPR
9904 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9905 && constructor_name_p (TREE_OPERAND (decl, 0),
9906 ctype))
9908 sfk = sfk_destructor;
9909 ctor_return_type = ctype;
9910 flags = DTOR_FLAG;
9911 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9912 next = &TREE_OPERAND (decl, 0);
9916 break;
9918 case ERROR_MARK:
9919 next = 0;
9920 break;
9922 case TYPE_DECL:
9923 /* Parse error puts this typespec where
9924 a declarator should go. */
9925 error ("`%T' specified as declarator-id", DECL_NAME (decl));
9926 if (TREE_TYPE (decl) == current_class_type)
9927 error (" perhaps you want `%T' for a constructor",
9928 current_class_name);
9929 dname = DECL_NAME (decl);
9930 name = IDENTIFIER_POINTER (dname);
9932 /* Avoid giving two errors for this. */
9933 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9935 declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
9936 *next = dname;
9937 next = 0;
9938 break;
9940 case BASELINK:
9941 next = &BASELINK_FUNCTIONS (decl);
9942 break;
9944 case TEMPLATE_DECL:
9945 /* Sometimes, we see a template-name used as part of a
9946 decl-specifier like in
9947 std::allocator alloc;
9948 Handle that gracefully. */
9949 error ("invalid use of template-name '%E' in a declarator", decl);
9950 return error_mark_node;
9951 break;
9953 default:
9954 my_friendly_assert (0, 20020917);
9959 /* A function definition's declarator must have the form of
9960 a function declarator. */
9962 if (funcdef_flag && innermost_code != CALL_EXPR)
9963 return 0;
9965 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9966 && innermost_code != CALL_EXPR
9967 && ! (ctype && declspecs == NULL_TREE))
9969 error ("declaration of `%D' as non-function", dname);
9970 return void_type_node;
9973 /* Anything declared one level down from the top level
9974 must be one of the parameters of a function
9975 (because the body is at least two levels down). */
9977 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9978 by not allowing C++ class definitions to specify their parameters
9979 with xdecls (must be spec.d in the parmlist).
9981 Since we now wait to push a class scope until we are sure that
9982 we are in a legitimate method context, we must set oldcname
9983 explicitly (since current_class_name is not yet alive).
9985 We also want to avoid calling this a PARM if it is in a namespace. */
9987 if (decl_context == NORMAL && !toplevel_bindings_p ())
9989 struct cp_binding_level *b = current_binding_level;
9990 current_binding_level = b->level_chain;
9991 if (current_binding_level != 0 && toplevel_bindings_p ())
9992 decl_context = PARM;
9993 current_binding_level = b;
9996 if (name == NULL)
9997 name = decl_context == PARM ? "parameter" : "type name";
9999 /* Look through the decl specs and record which ones appear.
10000 Some typespecs are defined as built-in typenames.
10001 Others, the ones that are modifiers of other types,
10002 are represented by bits in SPECBITS: set the bits for
10003 the modifiers that appear. Storage class keywords are also in SPECBITS.
10005 If there is a typedef name or a type, store the type in TYPE.
10006 This includes builtin typedefs such as `int'.
10008 Set EXPLICIT_INT if the type is `int' or `char' and did not
10009 come from a user typedef.
10011 Set LONGLONG if `long' is mentioned twice.
10013 For C++, constructors and destructors have their own fast treatment. */
10015 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10017 register int i;
10018 register tree id;
10020 /* Certain parse errors slip through. For example,
10021 `int class;' is not caught by the parser. Try
10022 weakly to recover here. */
10023 if (TREE_CODE (spec) != TREE_LIST)
10024 return 0;
10026 id = TREE_VALUE (spec);
10028 /* If the entire declaration is itself tagged as deprecated then
10029 suppress reports of deprecated items. */
10030 if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10032 if (deprecated_state != DEPRECATED_SUPPRESS)
10033 warn_deprecated_use (id);
10036 if (TREE_CODE (id) == IDENTIFIER_NODE)
10038 if (id == ridpointers[(int) RID_INT]
10039 || id == ridpointers[(int) RID_CHAR]
10040 || id == ridpointers[(int) RID_BOOL]
10041 || id == ridpointers[(int) RID_WCHAR])
10043 if (type)
10045 if (id == ridpointers[(int) RID_BOOL])
10046 error ("`bool' is now a keyword");
10047 else
10048 error ("extraneous `%T' ignored", id);
10050 else
10052 if (id == ridpointers[(int) RID_INT])
10053 explicit_int = 1;
10054 else if (id == ridpointers[(int) RID_CHAR])
10055 explicit_char = 1;
10056 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10058 goto found;
10060 /* C++ aggregate types. */
10061 if (IDENTIFIER_HAS_TYPE_VALUE (id))
10063 if (type)
10064 error ("multiple declarations `%T' and `%T'", type, id);
10065 else
10066 type = IDENTIFIER_TYPE_VALUE (id);
10067 goto found;
10070 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10072 if (ridpointers[i] == id)
10074 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10076 if (pedantic && ! in_system_header && warn_long_long)
10077 pedwarn ("ISO C++ does not support `long long'");
10078 if (longlong)
10079 error ("`long long long' is too long for GCC");
10080 else
10081 longlong = 1;
10083 else if (RIDBIT_SETP (i, specbits))
10084 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10086 /* Diagnose "__thread extern" or "__thread static". */
10087 if (RIDBIT_SETP (RID_THREAD, specbits))
10089 if (i == (int)RID_EXTERN)
10090 error ("`__thread' before `extern'");
10091 else if (i == (int)RID_STATIC)
10092 error ("`__thread' before `static'");
10095 if (i == (int)RID_EXTERN
10096 && TREE_PURPOSE (spec) == error_mark_node)
10097 /* This extern was part of a language linkage. */
10098 extern_langp = 1;
10100 RIDBIT_SET (i, specbits);
10101 goto found;
10105 else if (TREE_CODE (id) == TYPE_DECL)
10107 if (type)
10108 error ("multiple declarations `%T' and `%T'", type,
10109 TREE_TYPE (id));
10110 else
10112 type = TREE_TYPE (id);
10113 TREE_VALUE (spec) = type;
10114 typedef_decl = id;
10116 goto found;
10118 if (type)
10119 error ("two or more data types in declaration of `%s'", name);
10120 else if (TREE_CODE (id) == IDENTIFIER_NODE)
10122 register tree t = lookup_name (id, 1);
10123 if (!t || TREE_CODE (t) != TYPE_DECL)
10124 error ("`%s' fails to be a typedef or built in type",
10125 IDENTIFIER_POINTER (id));
10126 else
10128 type = TREE_TYPE (t);
10129 typedef_decl = t;
10132 else if (id != error_mark_node)
10133 /* Can't change CLASS nodes into RECORD nodes here! */
10134 type = id;
10136 found: ;
10139 #if 0
10140 /* See the code below that used this. */
10141 if (typedef_decl)
10142 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10143 #endif
10144 typedef_type = type;
10146 /* No type at all: default to `int', and set DEFAULTED_INT
10147 because it was not a user-defined typedef. */
10149 if (type == NULL_TREE
10150 && (RIDBIT_SETP (RID_SIGNED, specbits)
10151 || RIDBIT_SETP (RID_UNSIGNED, specbits)
10152 || RIDBIT_SETP (RID_LONG, specbits)
10153 || RIDBIT_SETP (RID_SHORT, specbits)))
10155 /* These imply 'int'. */
10156 type = integer_type_node;
10157 defaulted_int = 1;
10160 if (sfk != sfk_none)
10161 type = check_special_function_return_type (sfk, type,
10162 ctor_return_type);
10163 else if (type == NULL_TREE)
10165 int is_main;
10167 explicit_int = -1;
10169 /* We handle `main' specially here, because 'main () { }' is so
10170 common. With no options, it is allowed. With -Wreturn-type,
10171 it is a warning. It is only an error with -pedantic-errors. */
10172 is_main = (funcdef_flag
10173 && dname && MAIN_NAME_P (dname)
10174 && ctype == NULL_TREE
10175 && in_namespace == NULL_TREE
10176 && current_namespace == global_namespace);
10178 if (in_system_header || flag_ms_extensions)
10179 /* Allow it, sigh. */;
10180 else if (pedantic || ! is_main)
10181 pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10182 name);
10183 else if (warn_return_type)
10184 warning ("ISO C++ forbids declaration of `%s' with no type",
10185 name);
10187 type = integer_type_node;
10190 ctype = NULL_TREE;
10192 /* Now process the modifiers that were specified
10193 and check for invalid combinations. */
10195 /* Long double is a special combination. */
10197 if (RIDBIT_SETP (RID_LONG, specbits)
10198 && TYPE_MAIN_VARIANT (type) == double_type_node)
10200 RIDBIT_RESET (RID_LONG, specbits);
10201 type = build_qualified_type (long_double_type_node,
10202 cp_type_quals (type));
10205 /* Check all other uses of type modifiers. */
10207 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10208 || RIDBIT_SETP (RID_SIGNED, specbits)
10209 || RIDBIT_SETP (RID_LONG, specbits)
10210 || RIDBIT_SETP (RID_SHORT, specbits))
10212 int ok = 0;
10214 if (TREE_CODE (type) == REAL_TYPE)
10215 error ("short, signed or unsigned invalid for `%s'", name);
10216 else if (TREE_CODE (type) != INTEGER_TYPE)
10217 error ("long, short, signed or unsigned invalid for `%s'", name);
10218 else if (RIDBIT_SETP (RID_LONG, specbits)
10219 && RIDBIT_SETP (RID_SHORT, specbits))
10220 error ("long and short specified together for `%s'", name);
10221 else if ((RIDBIT_SETP (RID_LONG, specbits)
10222 || RIDBIT_SETP (RID_SHORT, specbits))
10223 && explicit_char)
10224 error ("long or short specified with char for `%s'", name);
10225 else if ((RIDBIT_SETP (RID_LONG, specbits)
10226 || RIDBIT_SETP (RID_SHORT, specbits))
10227 && TREE_CODE (type) == REAL_TYPE)
10228 error ("long or short specified with floating type for `%s'", name);
10229 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10230 && RIDBIT_SETP (RID_UNSIGNED, specbits))
10231 error ("signed and unsigned given together for `%s'", name);
10232 else
10234 ok = 1;
10235 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10237 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10238 name);
10239 if (flag_pedantic_errors)
10240 ok = 0;
10244 /* Discard the type modifiers if they are invalid. */
10245 if (! ok)
10247 RIDBIT_RESET (RID_UNSIGNED, specbits);
10248 RIDBIT_RESET (RID_SIGNED, specbits);
10249 RIDBIT_RESET (RID_LONG, specbits);
10250 RIDBIT_RESET (RID_SHORT, specbits);
10251 longlong = 0;
10255 if (RIDBIT_SETP (RID_COMPLEX, specbits)
10256 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10258 error ("complex invalid for `%s'", name);
10259 RIDBIT_RESET (RID_COMPLEX, specbits);
10262 /* Decide whether an integer type is signed or not.
10263 Optionally treat bitfields as signed by default. */
10264 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10265 /* [class.bit]
10267 It is implementation-defined whether a plain (neither
10268 explicitly signed or unsigned) char, short, int, or long
10269 bit-field is signed or unsigned.
10271 Naturally, we extend this to long long as well. Note that
10272 this does not include wchar_t. */
10273 || (bitfield && !flag_signed_bitfields
10274 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10275 /* A typedef for plain `int' without `signed' can be
10276 controlled just like plain `int', but a typedef for
10277 `signed int' cannot be so controlled. */
10278 && !(typedef_decl
10279 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10280 && (TREE_CODE (type) == INTEGER_TYPE
10281 || TREE_CODE (type) == CHAR_TYPE)
10282 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10284 if (longlong)
10285 type = long_long_unsigned_type_node;
10286 else if (RIDBIT_SETP (RID_LONG, specbits))
10287 type = long_unsigned_type_node;
10288 else if (RIDBIT_SETP (RID_SHORT, specbits))
10289 type = short_unsigned_type_node;
10290 else if (type == char_type_node)
10291 type = unsigned_char_type_node;
10292 else if (typedef_decl)
10293 type = c_common_unsigned_type (type);
10294 else
10295 type = unsigned_type_node;
10297 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10298 && type == char_type_node)
10299 type = signed_char_type_node;
10300 else if (longlong)
10301 type = long_long_integer_type_node;
10302 else if (RIDBIT_SETP (RID_LONG, specbits))
10303 type = long_integer_type_node;
10304 else if (RIDBIT_SETP (RID_SHORT, specbits))
10305 type = short_integer_type_node;
10307 if (RIDBIT_SETP (RID_COMPLEX, specbits))
10309 /* If we just have "complex", it is equivalent to
10310 "complex double", but if any modifiers at all are specified it is
10311 the complex form of TYPE. E.g, "complex short" is
10312 "complex short int". */
10314 if (defaulted_int && ! longlong
10315 && ! (RIDBIT_SETP (RID_LONG, specbits)
10316 || RIDBIT_SETP (RID_SHORT, specbits)
10317 || RIDBIT_SETP (RID_SIGNED, specbits)
10318 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10319 type = complex_double_type_node;
10320 else if (type == integer_type_node)
10321 type = complex_integer_type_node;
10322 else if (type == float_type_node)
10323 type = complex_float_type_node;
10324 else if (type == double_type_node)
10325 type = complex_double_type_node;
10326 else if (type == long_double_type_node)
10327 type = complex_long_double_type_node;
10328 else
10329 type = build_complex_type (type);
10332 type_quals = TYPE_UNQUALIFIED;
10333 if (RIDBIT_SETP (RID_CONST, specbits))
10334 type_quals |= TYPE_QUAL_CONST;
10335 if (RIDBIT_SETP (RID_VOLATILE, specbits))
10336 type_quals |= TYPE_QUAL_VOLATILE;
10337 if (RIDBIT_SETP (RID_RESTRICT, specbits))
10338 type_quals |= TYPE_QUAL_RESTRICT;
10339 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10340 error ("qualifiers are not allowed on declaration of `operator %T'",
10341 ctor_return_type);
10343 type_quals |= cp_type_quals (type);
10344 type = cp_build_qualified_type_real
10345 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10346 ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10347 /* We might have ignored or rejected some of the qualifiers. */
10348 type_quals = cp_type_quals (type);
10350 staticp = 0;
10351 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10352 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10353 RIDBIT_RESET (RID_VIRTUAL, specbits);
10354 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10355 RIDBIT_RESET (RID_EXPLICIT, specbits);
10357 if (RIDBIT_SETP (RID_STATIC, specbits))
10358 staticp = 1 + (decl_context == FIELD);
10360 if (virtualp && staticp == 2)
10362 error ("member `%D' cannot be declared both virtual and static",
10363 dname);
10364 staticp = 0;
10366 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10367 RIDBIT_RESET (RID_FRIEND, specbits);
10369 if (dependant_name && !friendp)
10371 error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10372 return void_type_node;
10375 /* Warn if two storage classes are given. Default to `auto'. */
10377 if (RIDBIT_ANY_SET (specbits))
10379 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10380 if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10381 if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10382 if (decl_context == PARM && nclasses > 0)
10383 error ("storage class specifiers invalid in parameter declarations");
10384 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10386 if (decl_context == PARM)
10387 error ("typedef declaration invalid in parameter declaration");
10388 nclasses++;
10390 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10391 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10392 if (!nclasses && !friendp && extern_langp)
10393 nclasses++;
10396 /* Give error if `virtual' is used outside of class declaration. */
10397 if (virtualp
10398 && (current_class_name == NULL_TREE || decl_context != FIELD))
10400 error ("virtual outside class declaration");
10401 virtualp = 0;
10404 /* Static anonymous unions are dealt with here. */
10405 if (staticp && decl_context == TYPENAME
10406 && TREE_CODE (declspecs) == TREE_LIST
10407 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10408 decl_context = FIELD;
10410 /* Warn about storage classes that are invalid for certain
10411 kinds of declarations (parameters, typenames, etc.). */
10413 /* "static __thread" and "extern __thread" are allowed. */
10414 if (nclasses == 2
10415 && RIDBIT_SETP (RID_THREAD, specbits)
10416 && (RIDBIT_SETP (RID_EXTERN, specbits)
10417 || RIDBIT_SETP (RID_STATIC, specbits)))
10418 nclasses = 1;
10420 if (nclasses > 1)
10421 error ("multiple storage classes in declaration of `%s'", name);
10422 else if (decl_context != NORMAL && nclasses > 0)
10424 if ((decl_context == PARM || decl_context == CATCHPARM)
10425 && (RIDBIT_SETP (RID_REGISTER, specbits)
10426 || RIDBIT_SETP (RID_AUTO, specbits)))
10428 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10430 else if (decl_context == FIELD
10431 /* C++ allows static class elements */
10432 && RIDBIT_SETP (RID_STATIC, specbits))
10433 /* C++ also allows inlines and signed and unsigned elements,
10434 but in those cases we don't come in here. */
10436 else
10438 if (decl_context == FIELD)
10440 tree tmp = NULL_TREE;
10441 register int op = 0;
10443 if (declarator)
10445 /* Avoid trying to get an operand off an identifier node. */
10446 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10447 tmp = declarator;
10448 else
10449 tmp = TREE_OPERAND (declarator, 0);
10450 op = IDENTIFIER_OPNAME_P (tmp);
10451 if (IDENTIFIER_TYPENAME_P (tmp))
10453 if (IDENTIFIER_GLOBAL_VALUE (tmp)
10454 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10455 == TYPE_DECL))
10456 name = IDENTIFIER_POINTER (tmp);
10457 else
10458 name = "<invalid operator>";
10461 error ("storage class specified for %s `%s'",
10462 op ? "member operator" : "field",
10463 name);
10465 else
10467 if (decl_context == PARM || decl_context == CATCHPARM)
10468 error ("storage class specified for parameter `%s'", name);
10469 else
10470 error ("storage class specified for typename");
10472 RIDBIT_RESET (RID_REGISTER, specbits);
10473 RIDBIT_RESET (RID_AUTO, specbits);
10474 RIDBIT_RESET (RID_EXTERN, specbits);
10475 RIDBIT_RESET (RID_THREAD, specbits);
10478 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10480 if (toplevel_bindings_p ())
10482 /* It's common practice (and completely valid) to have a const
10483 be initialized and declared extern. */
10484 if (!(type_quals & TYPE_QUAL_CONST))
10485 warning ("`%s' initialized and declared `extern'", name);
10487 else
10488 error ("`%s' has both `extern' and initializer", name);
10490 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10491 && ! toplevel_bindings_p ())
10492 error ("nested function `%s' declared `extern'", name);
10493 else if (toplevel_bindings_p ())
10495 if (RIDBIT_SETP (RID_AUTO, specbits))
10496 error ("top-level declaration of `%s' specifies `auto'", name);
10498 else if (RIDBIT_SETP (RID_THREAD, specbits)
10499 && !RIDBIT_SETP (RID_EXTERN, specbits)
10500 && !RIDBIT_SETP (RID_STATIC, specbits))
10502 error ("function-scope `%s' implicitly auto and declared `__thread'",
10503 name);
10504 RIDBIT_RESET (RID_THREAD, specbits);
10507 if (nclasses > 0 && friendp)
10508 error ("storage class specifiers invalid in friend function declarations");
10510 scope = get_scope_of_declarator (declarator);
10512 /* Now figure out the structure of the declarator proper.
10513 Descend through it, creating more complex types, until we reach
10514 the declared identifier (or NULL_TREE, in an abstract declarator). */
10516 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10517 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10519 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10520 an INDIRECT_REF (for *...),
10521 a CALL_EXPR (for ...(...)),
10522 an identifier (for the name being declared)
10523 or a null pointer (for the place in an absolute declarator
10524 where the name was omitted).
10525 For the last two cases, we have just exited the loop.
10527 For C++ it could also be
10528 a SCOPE_REF (for class :: ...). In this case, we have converted
10529 sensible names to types, and those are the values we use to
10530 qualify the member name.
10531 an ADDR_EXPR (for &...),
10532 a BIT_NOT_EXPR (for destructors)
10534 At this point, TYPE is the type of elements of an array,
10535 or for a function to return, or for a pointer to point to.
10536 After this sequence of ifs, TYPE is the type of the
10537 array or function or pointer, and DECLARATOR has had its
10538 outermost layer removed. */
10540 if (type == error_mark_node)
10542 if (TREE_CODE (declarator) == SCOPE_REF)
10543 declarator = TREE_OPERAND (declarator, 1);
10544 else
10545 declarator = TREE_OPERAND (declarator, 0);
10546 continue;
10548 if (quals != NULL_TREE
10549 && (declarator == NULL_TREE
10550 || TREE_CODE (declarator) != SCOPE_REF))
10552 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10553 ctype = TYPE_METHOD_BASETYPE (type);
10554 if (ctype != NULL_TREE)
10556 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10557 grok_method_quals (ctype, dummy, quals);
10558 type = TREE_TYPE (dummy);
10559 quals = NULL_TREE;
10563 switch (TREE_CODE (declarator))
10565 case TREE_LIST:
10567 /* We encode a declarator with embedded attributes using
10568 a TREE_LIST. */
10569 tree attrs = TREE_PURPOSE (declarator);
10570 tree inner_decl;
10571 int attr_flags;
10573 declarator = TREE_VALUE (declarator);
10574 inner_decl = declarator;
10575 while (inner_decl != NULL_TREE
10576 && TREE_CODE (inner_decl) == TREE_LIST)
10577 inner_decl = TREE_VALUE (inner_decl);
10578 attr_flags = 0;
10579 if (inner_decl == NULL_TREE
10580 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10581 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10582 if (TREE_CODE (inner_decl) == CALL_EXPR)
10583 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10584 if (TREE_CODE (inner_decl) == ARRAY_REF)
10585 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10586 returned_attrs = decl_attributes (&type,
10587 chainon (returned_attrs, attrs),
10588 attr_flags);
10590 break;
10592 case ARRAY_REF:
10594 register tree size;
10596 size = TREE_OPERAND (declarator, 1);
10598 /* VC++ spells a zero-sized array with []. */
10599 if (size == NULL_TREE && decl_context == FIELD && ! staticp
10600 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
10601 size = integer_zero_node;
10603 declarator = TREE_OPERAND (declarator, 0);
10605 type = create_array_type_for_decl (dname, type, size);
10607 ctype = NULL_TREE;
10609 break;
10611 case CALL_EXPR:
10613 tree arg_types;
10614 int funcdecl_p;
10615 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10616 tree inner_decl = TREE_OPERAND (declarator, 0);
10618 /* Declaring a function type.
10619 Make sure we have a valid type for the function to return. */
10621 /* We now know that the TYPE_QUALS don't apply to the
10622 decl, but to its return type. */
10623 type_quals = TYPE_UNQUALIFIED;
10625 /* Warn about some types functions can't return. */
10627 if (TREE_CODE (type) == FUNCTION_TYPE)
10629 error ("`%s' declared as function returning a function", name);
10630 type = integer_type_node;
10632 if (TREE_CODE (type) == ARRAY_TYPE)
10634 error ("`%s' declared as function returning an array", name);
10635 type = integer_type_node;
10638 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10639 inner_decl = TREE_OPERAND (inner_decl, 1);
10641 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10642 inner_decl = dname;
10644 /* Pick up type qualifiers which should be applied to `this'. */
10645 quals = CALL_DECLARATOR_QUALS (declarator);
10647 /* Pick up the exception specifications. */
10648 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10650 /* Say it's a definition only for the CALL_EXPR
10651 closest to the identifier. */
10652 funcdecl_p
10653 = inner_decl
10654 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10655 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10656 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10658 if (ctype == NULL_TREE
10659 && decl_context == FIELD
10660 && funcdecl_p
10661 && (friendp == 0 || dname == current_class_name))
10662 ctype = current_class_type;
10664 if (ctype && sfk == sfk_conversion)
10665 TYPE_HAS_CONVERSION (ctype) = 1;
10666 if (ctype && constructor_name_p (dname, ctype))
10668 /* We are within a class's scope. If our declarator name
10669 is the same as the class name, and we are defining
10670 a function, then it is a constructor/destructor, and
10671 therefore returns a void type. */
10673 if (flags == DTOR_FLAG)
10675 /* ISO C++ 12.4/2. A destructor may not be
10676 declared const or volatile. A destructor may
10677 not be static. */
10678 if (staticp == 2)
10679 error ("destructor cannot be static member function");
10680 if (quals)
10682 error ("destructors may not be `%s'",
10683 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10684 quals = NULL_TREE;
10686 if (decl_context == FIELD)
10688 if (! member_function_or_else (ctype,
10689 current_class_type,
10690 flags))
10691 return void_type_node;
10694 else /* It's a constructor. */
10696 if (explicitp == 1)
10697 explicitp = 2;
10698 /* ISO C++ 12.1. A constructor may not be
10699 declared const or volatile. A constructor may
10700 not be virtual. A constructor may not be
10701 static. */
10702 if (staticp == 2)
10703 error ("constructor cannot be static member function");
10704 if (virtualp)
10706 pedwarn ("constructors cannot be declared virtual");
10707 virtualp = 0;
10709 if (quals)
10711 error ("constructors may not be `%s'",
10712 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10713 quals = NULL_TREE;
10716 RID_BIT_TYPE tmp_bits;
10717 memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10718 RIDBIT_RESET (RID_INLINE, tmp_bits);
10719 RIDBIT_RESET (RID_STATIC, tmp_bits);
10720 if (RIDBIT_ANY_SET (tmp_bits))
10721 error ("return value type specifier for constructor ignored");
10723 if (decl_context == FIELD)
10725 if (! member_function_or_else (ctype,
10726 current_class_type,
10727 flags))
10728 return void_type_node;
10729 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10730 if (sfk != sfk_constructor)
10731 return NULL_TREE;
10734 if (decl_context == FIELD)
10735 staticp = 0;
10737 else if (friendp)
10739 if (initialized)
10740 error ("can't initialize friend function `%s'", name);
10741 if (virtualp)
10743 /* Cannot be both friend and virtual. */
10744 error ("virtual functions cannot be friends");
10745 RIDBIT_RESET (RID_FRIEND, specbits);
10746 friendp = 0;
10748 if (decl_context == NORMAL)
10749 error ("friend declaration not in class definition");
10750 if (current_function_decl && funcdef_flag)
10751 error ("can't define friend function `%s' in a local class definition",
10752 name);
10755 /* Construct the function type and go to the next
10756 inner layer of declarator. */
10758 declarator = TREE_OPERAND (declarator, 0);
10760 /* FIXME: This is where default args should be fully
10761 processed. */
10763 arg_types = grokparms (inner_parms);
10765 if (declarator && flags == DTOR_FLAG)
10767 /* A destructor declared in the body of a class will
10768 be represented as a BIT_NOT_EXPR. But, we just
10769 want the underlying IDENTIFIER. */
10770 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10771 declarator = TREE_OPERAND (declarator, 0);
10773 if (arg_types != void_list_node)
10775 error ("destructors may not have parameters");
10776 arg_types = void_list_node;
10777 last_function_parms = NULL_TREE;
10781 /* ANSI says that `const int foo ();'
10782 does not make the function foo const. */
10783 type = build_function_type (type, arg_types);
10785 break;
10787 case ADDR_EXPR:
10788 case INDIRECT_REF:
10789 /* Filter out pointers-to-references and references-to-references.
10790 We can get these if a TYPE_DECL is used. */
10792 if (TREE_CODE (type) == REFERENCE_TYPE)
10794 error (TREE_CODE (declarator) == ADDR_EXPR
10795 ? "cannot declare reference to `%#T'"
10796 : "cannot declare pointer to `%#T'", type);
10797 type = TREE_TYPE (type);
10799 else if (VOID_TYPE_P (type)
10800 && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10801 error (ctype ? "cannot declare pointer to `%#T' member"
10802 : "cannot declare reference to `%#T'", type);
10804 /* Merge any constancy or volatility into the target type
10805 for the pointer. */
10807 /* We now know that the TYPE_QUALS don't apply to the decl,
10808 but to the target of the pointer. */
10809 type_quals = TYPE_UNQUALIFIED;
10811 if (TREE_CODE (declarator) == ADDR_EXPR)
10813 if (!VOID_TYPE_P (type))
10814 type = build_reference_type (type);
10816 else if (TREE_CODE (type) == METHOD_TYPE)
10817 type = build_ptrmemfunc_type (build_pointer_type (type));
10818 else if (ctype)
10819 type = build_ptrmem_type (ctype, type);
10820 else
10821 type = build_pointer_type (type);
10823 /* Process a list of type modifier keywords (such as
10824 const or volatile) that were given inside the `*' or `&'. */
10826 if (TREE_TYPE (declarator))
10828 register tree typemodlist;
10829 int erred = 0;
10830 int constp = 0;
10831 int volatilep = 0;
10832 int restrictp = 0;
10834 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10835 typemodlist = TREE_CHAIN (typemodlist))
10837 tree qualifier = TREE_VALUE (typemodlist);
10839 if (qualifier == ridpointers[(int) RID_CONST])
10841 constp++;
10842 type_quals |= TYPE_QUAL_CONST;
10844 else if (qualifier == ridpointers[(int) RID_VOLATILE])
10846 volatilep++;
10847 type_quals |= TYPE_QUAL_VOLATILE;
10849 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10851 restrictp++;
10852 type_quals |= TYPE_QUAL_RESTRICT;
10854 else if (!erred)
10856 erred = 1;
10857 error ("invalid type modifier within pointer declarator");
10860 if (constp > 1)
10861 pedwarn ("duplicate `const'");
10862 if (volatilep > 1)
10863 pedwarn ("duplicate `volatile'");
10864 if (restrictp > 1)
10865 pedwarn ("duplicate `restrict'");
10866 type = cp_build_qualified_type (type, type_quals);
10867 type_quals = cp_type_quals (type);
10869 declarator = TREE_OPERAND (declarator, 0);
10870 ctype = NULL_TREE;
10871 break;
10873 case SCOPE_REF:
10875 /* We have converted type names to NULL_TREE if the
10876 name was bogus, or to a _TYPE node, if not.
10878 The variable CTYPE holds the type we will ultimately
10879 resolve to. The code here just needs to build
10880 up appropriate member types. */
10881 tree sname = TREE_OPERAND (declarator, 1);
10882 tree t;
10884 /* Destructors can have their visibilities changed as well. */
10885 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10886 sname = TREE_OPERAND (sname, 0);
10888 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10890 /* We had a reference to a global decl, or
10891 perhaps we were given a non-aggregate typedef,
10892 in which case we cleared this out, and should just
10893 keep going as though it wasn't there. */
10894 declarator = sname;
10895 continue;
10897 ctype = TREE_OPERAND (declarator, 0);
10899 t = ctype;
10900 while (t != NULL_TREE && CLASS_TYPE_P (t))
10902 /* You're supposed to have one `template <...>'
10903 for every template class, but you don't need one
10904 for a full specialization. For example:
10906 template <class T> struct S{};
10907 template <> struct S<int> { void f(); };
10908 void S<int>::f () {}
10910 is correct; there shouldn't be a `template <>' for
10911 the definition of `S<int>::f'. */
10912 if (CLASSTYPE_TEMPLATE_INFO (t)
10913 && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
10914 || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10915 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
10916 template_count += 1;
10918 t = TYPE_MAIN_DECL (t);
10919 t = DECL_CONTEXT (t);
10922 if (sname == NULL_TREE)
10923 goto done_scoping;
10925 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10927 /* This is the `standard' use of the scoping operator:
10928 basetype :: member . */
10930 if (ctype == current_class_type)
10932 /* class A {
10933 void A::f ();
10936 Is this ill-formed? */
10938 if (pedantic)
10939 pedwarn ("extra qualification `%T::' on member `%s' ignored",
10940 ctype, name);
10942 else if (TREE_CODE (type) == FUNCTION_TYPE)
10944 if (current_class_type == NULL_TREE || friendp)
10945 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10946 TYPE_ARG_TYPES (type));
10947 else
10949 error ("cannot declare member function `%T::%s' within `%T'",
10950 ctype, name, current_class_type);
10951 return error_mark_node;
10954 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10955 || COMPLETE_TYPE_P (complete_type (ctype)))
10957 /* Have to move this code elsewhere in this function.
10958 this code is used for i.e., typedef int A::M; M *pm;
10960 It is? How? jason 10/2/94 */
10962 if (current_class_type)
10964 error ("cannot declare member `%T::%s' within `%T'",
10965 ctype, name, current_class_type);
10966 return void_type_node;
10969 else
10971 cxx_incomplete_type_error (NULL_TREE, ctype);
10972 return error_mark_node;
10975 declarator = sname;
10977 else if (TREE_CODE (sname) == SCOPE_REF)
10978 abort ();
10979 else
10981 done_scoping:
10982 declarator = TREE_OPERAND (declarator, 1);
10983 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10984 /* In this case, we will deal with it later. */
10986 else if (TREE_CODE (type) == FUNCTION_TYPE)
10987 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10988 TYPE_ARG_TYPES (type));
10991 break;
10993 case BIT_NOT_EXPR:
10994 declarator = TREE_OPERAND (declarator, 0);
10995 break;
10997 case BASELINK:
10998 declarator = BASELINK_FUNCTIONS (declarator);
10999 break;
11001 case RECORD_TYPE:
11002 case UNION_TYPE:
11003 case ENUMERAL_TYPE:
11004 declarator = NULL_TREE;
11005 break;
11007 case ERROR_MARK:
11008 declarator = NULL_TREE;
11009 break;
11011 default:
11012 abort ();
11016 if (returned_attrs)
11018 if (attrlist)
11019 *attrlist = chainon (returned_attrs, *attrlist);
11020 else
11021 attrlist = &returned_attrs;
11024 /* Now TYPE has the actual type. */
11026 /* Did array size calculations overflow? */
11028 if (TREE_CODE (type) == ARRAY_TYPE
11029 && COMPLETE_TYPE_P (type)
11030 && TREE_OVERFLOW (TYPE_SIZE (type)))
11032 error ("size of array `%s' is too large", name);
11033 /* If we proceed with the array type as it is, we'll eventually
11034 crash in tree_low_cst(). */
11035 type = error_mark_node;
11038 if ((decl_context == FIELD || decl_context == PARM)
11039 && !processing_template_decl
11040 && variably_modified_type_p (type))
11042 if (decl_context == FIELD)
11043 error ("data member may not have variably modified type `%T'", type);
11044 else
11045 error ("parameter may not have variably modified type `%T'", type);
11046 type = error_mark_node;
11049 if (explicitp == 1 || (explicitp && friendp))
11051 /* [dcl.fct.spec] The explicit specifier shall only be used in
11052 declarations of constructors within a class definition. */
11053 error ("only declarations of constructors can be `explicit'");
11054 explicitp = 0;
11057 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11059 if (decl_context != FIELD || friendp)
11061 error ("non-member `%s' cannot be declared `mutable'", name);
11062 RIDBIT_RESET (RID_MUTABLE, specbits);
11064 else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11066 error ("non-object member `%s' cannot be declared `mutable'", name);
11067 RIDBIT_RESET (RID_MUTABLE, specbits);
11069 else if (TREE_CODE (type) == FUNCTION_TYPE
11070 || TREE_CODE (type) == METHOD_TYPE)
11072 error ("function `%s' cannot be declared `mutable'", name);
11073 RIDBIT_RESET (RID_MUTABLE, specbits);
11075 else if (staticp)
11077 error ("static `%s' cannot be declared `mutable'", name);
11078 RIDBIT_RESET (RID_MUTABLE, specbits);
11080 else if (type_quals & TYPE_QUAL_CONST)
11082 error ("const `%s' cannot be declared `mutable'", name);
11083 RIDBIT_RESET (RID_MUTABLE, specbits);
11087 if (declarator == NULL_TREE
11088 || TREE_CODE (declarator) == IDENTIFIER_NODE
11089 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11090 && (TREE_CODE (type) == FUNCTION_TYPE
11091 || TREE_CODE (type) == METHOD_TYPE)))
11092 /* OK */;
11093 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11095 error ("template-id `%D' used as a declarator", declarator);
11096 declarator = dname;
11098 else
11099 /* Unexpected declarator format. */
11100 abort ();
11102 /* If this is declaring a typedef name, return a TYPE_DECL. */
11104 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11106 tree decl;
11108 /* Note that the grammar rejects storage classes
11109 in typenames, fields or parameters. */
11110 if (current_lang_name == lang_name_java)
11111 TYPE_FOR_JAVA (type) = 1;
11113 if (decl_context == FIELD)
11115 if (constructor_name_p (declarator, current_class_type))
11116 pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11117 declarator);
11118 decl = build_lang_decl (TYPE_DECL, declarator, type);
11120 else
11122 decl = build_decl (TYPE_DECL, declarator, type);
11123 if (in_namespace || ctype)
11124 cp_error_at ("typedef name may not be a nested-name-specifier",
11125 decl);
11126 if (!current_function_decl)
11127 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11130 /* If the user declares "typedef struct {...} foo" then the
11131 struct will have an anonymous name. Fill that name in now.
11132 Nothing can refer to it, so nothing needs know about the name
11133 change. */
11134 if (type != error_mark_node
11135 && declarator
11136 && TYPE_NAME (type)
11137 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11138 && TYPE_ANONYMOUS_P (type)
11139 /* Don't do this if there are attributes. */
11140 && (!attrlist || !*attrlist)
11141 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11143 tree oldname = TYPE_NAME (type);
11144 tree t;
11146 /* Replace the anonymous name with the real name everywhere. */
11147 lookup_tag_reverse (type, declarator);
11148 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11149 if (TYPE_NAME (t) == oldname)
11150 TYPE_NAME (t) = decl;
11152 if (TYPE_LANG_SPECIFIC (type))
11153 TYPE_WAS_ANONYMOUS (type) = 1;
11155 /* If this is a typedef within a template class, the nested
11156 type is a (non-primary) template. The name for the
11157 template needs updating as well. */
11158 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11159 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11160 = TYPE_IDENTIFIER (type);
11162 /* FIXME remangle member functions; member functions of a
11163 type with external linkage have external linkage. */
11166 if (quals)
11168 if (ctype == NULL_TREE)
11170 if (TREE_CODE (type) != METHOD_TYPE)
11171 cp_error_at ("invalid type qualifier for non-member function type", decl);
11172 else
11173 ctype = TYPE_METHOD_BASETYPE (type);
11175 if (ctype != NULL_TREE)
11176 grok_method_quals (ctype, decl, quals);
11179 if (RIDBIT_SETP (RID_SIGNED, specbits)
11180 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11181 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11183 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11184 inlinep, friendp, raises != NULL_TREE);
11186 return decl;
11189 /* Detect the case of an array type of unspecified size
11190 which came, as such, direct from a typedef name.
11191 We must copy the type, so that the array's domain can be
11192 individually set by the object's initializer. */
11194 if (type && typedef_type
11195 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11196 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11197 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11199 /* Detect where we're using a typedef of function type to declare a
11200 function. last_function_parms will not be set, so we must create
11201 it now. */
11203 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11205 tree decls = NULL_TREE;
11206 tree args;
11208 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11210 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11212 TREE_CHAIN (decl) = decls;
11213 decls = decl;
11216 last_function_parms = nreverse (decls);
11219 /* If this is a type name (such as, in a cast or sizeof),
11220 compute the type and return it now. */
11222 if (decl_context == TYPENAME)
11224 /* Note that the grammar rejects storage classes
11225 in typenames, fields or parameters. */
11226 if (type_quals != TYPE_UNQUALIFIED)
11227 type_quals = TYPE_UNQUALIFIED;
11229 /* Special case: "friend class foo" looks like a TYPENAME context. */
11230 if (friendp)
11232 if (type_quals != TYPE_UNQUALIFIED)
11234 error ("type qualifiers specified for friend class declaration");
11235 type_quals = TYPE_UNQUALIFIED;
11237 if (inlinep)
11239 error ("`inline' specified for friend class declaration");
11240 inlinep = 0;
11243 if (!current_aggr)
11245 /* Don't allow friend declaration without a class-key. */
11246 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11247 pedwarn ("template parameters cannot be friends");
11248 else if (TREE_CODE (type) == TYPENAME_TYPE)
11249 pedwarn ("friend declaration requires class-key, "
11250 "i.e. `friend class %T::%D'",
11251 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11252 else
11253 pedwarn ("friend declaration requires class-key, "
11254 "i.e. `friend %#T'",
11255 type);
11258 /* Only try to do this stuff if we didn't already give up. */
11259 if (type != integer_type_node)
11261 /* A friendly class? */
11262 if (current_class_type)
11263 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11264 else
11265 error ("trying to make class `%T' a friend of global scope",
11266 type);
11268 type = void_type_node;
11271 else if (quals)
11273 if (ctype == NULL_TREE)
11275 if (TREE_CODE (type) != METHOD_TYPE)
11276 error ("invalid qualifiers on non-member function type");
11277 else
11278 ctype = TYPE_METHOD_BASETYPE (type);
11280 if (ctype)
11282 tree dummy = build_decl (TYPE_DECL, declarator, type);
11283 grok_method_quals (ctype, dummy, quals);
11284 type = TREE_TYPE (dummy);
11288 return type;
11290 else if (declarator == NULL_TREE && decl_context != PARM
11291 && decl_context != CATCHPARM
11292 && TREE_CODE (type) != UNION_TYPE
11293 && ! bitfield)
11295 error ("abstract declarator `%T' used as declaration", type);
11296 declarator = make_anon_name ();
11299 /* `void' at top level (not within pointer)
11300 is allowed only in typedefs or type names.
11301 We don't complain about parms either, but that is because
11302 a better error message can be made later. */
11304 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11306 if (! declarator)
11307 error ("unnamed variable or field declared void");
11308 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11310 if (IDENTIFIER_OPNAME_P (declarator))
11311 abort ();
11312 else
11313 error ("variable or field `%s' declared void", name);
11315 else
11316 error ("variable or field declared void");
11317 type = integer_type_node;
11320 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11321 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11323 if (decl_context == PARM || decl_context == CATCHPARM)
11325 if (ctype || in_namespace)
11326 error ("cannot use `::' in parameter declaration");
11328 /* A parameter declared as an array of T is really a pointer to T.
11329 One declared as a function is really a pointer to a function.
11330 One declared as a member is really a pointer to member. */
11332 if (TREE_CODE (type) == ARRAY_TYPE)
11334 /* Transfer const-ness of array into that of type pointed to. */
11335 type = build_pointer_type (TREE_TYPE (type));
11336 type_quals = TYPE_UNQUALIFIED;
11338 else if (TREE_CODE (type) == FUNCTION_TYPE)
11339 type = build_pointer_type (type);
11340 else if (TREE_CODE (type) == OFFSET_TYPE)
11341 type = build_pointer_type (type);
11345 register tree decl;
11347 if (decl_context == PARM)
11349 decl = cp_build_parm_decl (declarator, type);
11351 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11352 inlinep, friendp, raises != NULL_TREE);
11354 else if (decl_context == FIELD)
11356 if (type == error_mark_node)
11358 /* Happens when declaring arrays of sizes which
11359 are error_mark_node, for example. */
11360 decl = NULL_TREE;
11362 else if (in_namespace && !friendp)
11364 /* Something like struct S { int N::j; }; */
11365 error ("invalid use of `::'");
11366 decl = NULL_TREE;
11368 else if (TREE_CODE (type) == FUNCTION_TYPE)
11370 int publicp = 0;
11371 tree function_context;
11373 /* We catch the others as conflicts with the builtin
11374 typedefs. */
11375 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11377 error ("function `%D' cannot be declared friend",
11378 declarator);
11379 friendp = 0;
11382 if (friendp == 0)
11384 if (ctype == NULL_TREE)
11385 ctype = current_class_type;
11387 if (ctype == NULL_TREE)
11389 error ("can't make `%D' into a method -- not in a class",
11390 declarator);
11391 return void_type_node;
11394 /* ``A union may [ ... ] not [ have ] virtual functions.''
11395 ARM 9.5 */
11396 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11398 error ("function `%D' declared virtual inside a union",
11399 declarator);
11400 return void_type_node;
11403 if (declarator == ansi_opname (NEW_EXPR)
11404 || declarator == ansi_opname (VEC_NEW_EXPR)
11405 || declarator == ansi_opname (DELETE_EXPR)
11406 || declarator == ansi_opname (VEC_DELETE_EXPR))
11408 if (virtualp)
11410 error ("`%D' cannot be declared virtual, since it is always static",
11411 declarator);
11412 virtualp = 0;
11415 else if (staticp < 2)
11416 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11417 TYPE_ARG_TYPES (type));
11420 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11421 function_context = (ctype != NULL_TREE) ?
11422 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11423 publicp = (! friendp || ! staticp)
11424 && function_context == NULL_TREE;
11425 decl = grokfndecl (ctype, type,
11426 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11427 ? declarator : dname,
11428 declarator,
11429 virtualp, flags, quals, raises,
11430 friendp ? -1 : 0, friendp, publicp, inlinep,
11431 funcdef_flag, template_count, in_namespace);
11432 if (decl == NULL_TREE)
11433 return decl;
11434 #if 0
11435 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11436 /* The decl and setting of decl_attr is also turned off. */
11437 decl = build_decl_attribute_variant (decl, decl_attr);
11438 #endif
11440 /* [class.conv.ctor]
11442 A constructor declared without the function-specifier
11443 explicit that can be called with a single parameter
11444 specifies a conversion from the type of its first
11445 parameter to the type of its class. Such a constructor
11446 is called a converting constructor. */
11447 if (explicitp == 2)
11448 DECL_NONCONVERTING_P (decl) = 1;
11449 else if (DECL_CONSTRUCTOR_P (decl))
11451 /* The constructor can be called with exactly one
11452 parameter if there is at least one parameter, and
11453 any subsequent parameters have default arguments.
11454 Ignore any compiler-added parms. */
11455 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11457 if (arg_types == void_list_node
11458 || (arg_types
11459 && TREE_CHAIN (arg_types)
11460 && TREE_CHAIN (arg_types) != void_list_node
11461 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11462 DECL_NONCONVERTING_P (decl) = 1;
11465 else if (TREE_CODE (type) == METHOD_TYPE)
11467 /* We only get here for friend declarations of
11468 members of other classes. */
11469 /* All method decls are public, so tell grokfndecl to set
11470 TREE_PUBLIC, also. */
11471 decl = grokfndecl (ctype, type, declarator, declarator,
11472 virtualp, flags, quals, raises,
11473 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11474 template_count, in_namespace);
11475 if (decl == NULL_TREE)
11476 return NULL_TREE;
11478 else if (!staticp && ! processing_template_decl
11479 && !COMPLETE_TYPE_P (complete_type (type))
11480 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11482 if (declarator)
11483 error ("field `%D' has incomplete type", declarator);
11484 else
11485 error ("name `%T' has incomplete type", type);
11487 /* If we're instantiating a template, tell them which
11488 instantiation made the field's type be incomplete. */
11489 if (current_class_type
11490 && TYPE_NAME (current_class_type)
11491 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11492 && declspecs && TREE_VALUE (declspecs)
11493 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11494 error (" in instantiation of template `%T'",
11495 current_class_type);
11497 type = error_mark_node;
11498 decl = NULL_TREE;
11500 else
11502 if (friendp)
11504 error ("`%s' is neither function nor member function; cannot be declared friend",
11505 IDENTIFIER_POINTER (declarator));
11506 friendp = 0;
11508 decl = NULL_TREE;
11511 if (friendp)
11513 /* Friends are treated specially. */
11514 if (ctype == current_class_type)
11515 warning ("member functions are implicitly friends of their class");
11516 else
11518 tree t = NULL_TREE;
11519 if (decl && DECL_NAME (decl))
11521 if (template_class_depth (current_class_type) == 0)
11523 decl
11524 = check_explicit_specialization
11525 (declarator, decl,
11526 template_count, 2 * (funcdef_flag != 0) + 4);
11527 if (decl == error_mark_node)
11528 return error_mark_node;
11531 t = do_friend (ctype, declarator, decl,
11532 last_function_parms, *attrlist,
11533 flags, quals, funcdef_flag);
11535 if (t && funcdef_flag)
11536 return t;
11538 return void_type_node;
11542 /* Structure field. It may not be a function, except for C++ */
11544 if (decl == NULL_TREE)
11546 if (initialized)
11548 if (!staticp)
11550 /* An attempt is being made to initialize a non-static
11551 member. But, from [class.mem]:
11553 4 A member-declarator can contain a
11554 constant-initializer only if it declares a static
11555 member (_class.static_) of integral or enumeration
11556 type, see _class.static.data_.
11558 This used to be relatively common practice, but
11559 the rest of the compiler does not correctly
11560 handle the initialization unless the member is
11561 static so we make it static below. */
11562 pedwarn ("ISO C++ forbids initialization of member `%D'",
11563 declarator);
11564 pedwarn ("making `%D' static", declarator);
11565 staticp = 1;
11568 if (uses_template_parms (type))
11569 /* We'll check at instantiation time. */
11571 else if (check_static_variable_definition (declarator,
11572 type))
11573 /* If we just return the declaration, crashes
11574 will sometimes occur. We therefore return
11575 void_type_node, as if this was a friend
11576 declaration, to cause callers to completely
11577 ignore this declaration. */
11578 return void_type_node;
11581 if (staticp)
11583 /* [class.mem] forbids static data members with the
11584 same name as the enclosing class. Non-static data
11585 members are checked in check_field_decls. */
11586 if (constructor_name_p (declarator, current_class_type))
11587 pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11588 declarator);
11590 /* C++ allows static class members. All other work
11591 for this is done by grokfield. */
11592 decl = build_lang_decl (VAR_DECL, declarator, type);
11593 TREE_STATIC (decl) = 1;
11594 /* In class context, 'static' means public access. */
11595 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11597 else
11599 decl = build_decl (FIELD_DECL, declarator, type);
11600 DECL_NONADDRESSABLE_P (decl) = bitfield;
11601 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11603 DECL_MUTABLE_P (decl) = 1;
11604 RIDBIT_RESET (RID_MUTABLE, specbits);
11608 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11609 inlinep, friendp, raises != NULL_TREE);
11612 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11614 tree original_name;
11615 int publicp = 0;
11617 if (! declarator)
11618 return NULL_TREE;
11620 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11621 original_name = dname;
11622 else
11623 original_name = declarator;
11625 if (RIDBIT_SETP (RID_AUTO, specbits))
11626 error ("storage class `auto' invalid for function `%s'", name);
11627 else if (RIDBIT_SETP (RID_REGISTER, specbits))
11628 error ("storage class `register' invalid for function `%s'", name);
11629 else if (RIDBIT_SETP (RID_THREAD, specbits))
11630 error ("storage class `__thread' invalid for function `%s'", name);
11632 /* Function declaration not at top level.
11633 Storage classes other than `extern' are not allowed
11634 and `extern' makes no difference. */
11635 if (! toplevel_bindings_p ()
11636 && (RIDBIT_SETP (RID_STATIC, specbits)
11637 || RIDBIT_SETP (RID_INLINE, specbits))
11638 && pedantic)
11640 if (RIDBIT_SETP (RID_STATIC, specbits))
11641 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11642 else
11643 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11646 if (ctype == NULL_TREE)
11648 if (virtualp)
11650 error ("virtual non-class function `%s'", name);
11651 virtualp = 0;
11654 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11655 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11656 TYPE_ARG_TYPES (type));
11658 /* Record presence of `static'. */
11659 publicp = (ctype != NULL_TREE
11660 || RIDBIT_SETP (RID_EXTERN, specbits)
11661 || !RIDBIT_SETP (RID_STATIC, specbits));
11663 decl = grokfndecl (ctype, type, original_name, declarator,
11664 virtualp, flags, quals, raises,
11665 1, friendp,
11666 publicp, inlinep, funcdef_flag,
11667 template_count, in_namespace);
11668 if (decl == NULL_TREE)
11669 return NULL_TREE;
11671 if (staticp == 1)
11673 int invalid_static = 0;
11675 /* Don't allow a static member function in a class, and forbid
11676 declaring main to be static. */
11677 if (TREE_CODE (type) == METHOD_TYPE)
11679 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11680 invalid_static = 1;
11682 else if (current_function_decl)
11684 /* FIXME need arm citation */
11685 error ("cannot declare static function inside another function");
11686 invalid_static = 1;
11689 if (invalid_static)
11691 staticp = 0;
11692 RIDBIT_RESET (RID_STATIC, specbits);
11696 else
11698 /* It's a variable. */
11700 /* An uninitialized decl with `extern' is a reference. */
11701 decl = grokvardecl (type, declarator, &specbits,
11702 initialized,
11703 (type_quals & TYPE_QUAL_CONST) != 0,
11704 ctype ? ctype : in_namespace);
11705 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11706 inlinep, friendp, raises != NULL_TREE);
11708 if (ctype)
11710 DECL_CONTEXT (decl) = ctype;
11711 if (staticp == 1)
11713 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11714 staticp = 0;
11715 RIDBIT_RESET (RID_STATIC, specbits);
11717 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11719 error ("static member `%D' declared `register'", decl);
11720 RIDBIT_RESET (RID_REGISTER, specbits);
11722 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11724 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11725 decl);
11726 RIDBIT_RESET (RID_EXTERN, specbits);
11731 my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11733 /* Record `register' declaration for warnings on &
11734 and in case doing stupid register allocation. */
11736 if (RIDBIT_SETP (RID_REGISTER, specbits))
11737 DECL_REGISTER (decl) = 1;
11739 if (RIDBIT_SETP (RID_EXTERN, specbits))
11740 DECL_THIS_EXTERN (decl) = 1;
11742 if (RIDBIT_SETP (RID_STATIC, specbits))
11743 DECL_THIS_STATIC (decl) = 1;
11745 /* Record constancy and volatility. There's no need to do this
11746 when processing a template; we'll do this for the instantiated
11747 declaration based on the type of DECL. */
11748 if (!processing_template_decl)
11749 c_apply_type_quals_to_decl (type_quals, decl);
11751 return decl;
11755 /* Subroutine of start_function. Ensure that each of the parameter
11756 types (as listed in PARMS) is complete, as is required for a
11757 function definition. */
11759 static void
11760 require_complete_types_for_parms (tree parms)
11762 for (; parms; parms = TREE_CHAIN (parms))
11764 if (VOID_TYPE_P (TREE_TYPE (parms)))
11765 /* grokparms will have already issued an error */
11766 TREE_TYPE (parms) = error_mark_node;
11767 else if (complete_type_or_else (TREE_TYPE (parms), parms))
11769 layout_decl (parms, 0);
11770 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11772 else
11773 TREE_TYPE (parms) = error_mark_node;
11777 /* Returns nonzero if T is a local variable. */
11780 local_variable_p (tree t)
11782 if ((TREE_CODE (t) == VAR_DECL
11783 /* A VAR_DECL with a context that is a _TYPE is a static data
11784 member. */
11785 && !TYPE_P (CP_DECL_CONTEXT (t))
11786 /* Any other non-local variable must be at namespace scope. */
11787 && !DECL_NAMESPACE_SCOPE_P (t))
11788 || (TREE_CODE (t) == PARM_DECL))
11789 return 1;
11791 return 0;
11794 /* Returns nonzero if T is an automatic local variable or a label.
11795 (These are the declarations that need to be remapped when the code
11796 containing them is duplicated.) */
11799 nonstatic_local_decl_p (tree t)
11801 return ((local_variable_p (t) && !TREE_STATIC (t))
11802 || TREE_CODE (t) == LABEL_DECL
11803 || TREE_CODE (t) == RESULT_DECL);
11806 /* Like local_variable_p, but suitable for use as a tree-walking
11807 function. */
11809 static tree
11810 local_variable_p_walkfn (tree* tp,
11811 int* walk_subtrees ATTRIBUTE_UNUSED ,
11812 void* data ATTRIBUTE_UNUSED )
11814 return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11815 ? *tp : NULL_TREE);
11818 /* Check that ARG, which is a default-argument expression for a
11819 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11820 something goes wrong. DECL may also be a _TYPE node, rather than a
11821 DECL, if there is no DECL available. */
11823 tree
11824 check_default_argument (tree decl, tree arg)
11826 tree var;
11827 tree decl_type;
11829 if (TREE_CODE (arg) == DEFAULT_ARG)
11830 /* We get a DEFAULT_ARG when looking at an in-class declaration
11831 with a default argument. Ignore the argument for now; we'll
11832 deal with it after the class is complete. */
11833 return arg;
11835 if (processing_template_decl || uses_template_parms (arg))
11836 /* We don't do anything checking until instantiation-time. Note
11837 that there may be uninstantiated arguments even for an
11838 instantiated function, since default arguments are not
11839 instantiated until they are needed. */
11840 return arg;
11842 if (TYPE_P (decl))
11844 decl_type = decl;
11845 decl = NULL_TREE;
11847 else
11848 decl_type = TREE_TYPE (decl);
11850 if (arg == error_mark_node
11851 || decl == error_mark_node
11852 || TREE_TYPE (arg) == error_mark_node
11853 || decl_type == error_mark_node)
11854 /* Something already went wrong. There's no need to check
11855 further. */
11856 return error_mark_node;
11858 /* [dcl.fct.default]
11860 A default argument expression is implicitly converted to the
11861 parameter type. */
11862 if (!TREE_TYPE (arg)
11863 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11865 if (decl)
11866 error ("default argument for `%#D' has type `%T'",
11867 decl, TREE_TYPE (arg));
11868 else
11869 error ("default argument for parameter of type `%T' has type `%T'",
11870 decl_type, TREE_TYPE (arg));
11872 return error_mark_node;
11875 /* [dcl.fct.default]
11877 Local variables shall not be used in default argument
11878 expressions.
11880 The keyword `this' shall not be used in a default argument of a
11881 member function. */
11882 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11883 NULL);
11884 if (var)
11886 error ("default argument `%E' uses local variable `%D'",
11887 arg, var);
11888 return error_mark_node;
11891 /* All is well. */
11892 return arg;
11895 /* Decode the list of parameter types for a function type.
11896 Given the list of things declared inside the parens,
11897 return a list of types.
11899 We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11900 flag. If unset, we append void_list_node. A parmlist declared
11901 as `(void)' is accepted as the empty parmlist.
11903 Also set last_function_parms to the chain of PARM_DECLs. */
11905 static tree
11906 grokparms (tree first_parm)
11908 tree result = NULL_TREE;
11909 tree decls = NULL_TREE;
11910 int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11911 tree parm, chain;
11912 int any_error = 0;
11914 my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11916 for (parm = first_parm; parm != NULL_TREE; parm = chain)
11918 tree type = NULL_TREE;
11919 tree decl = TREE_VALUE (parm);
11920 tree init = TREE_PURPOSE (parm);
11921 tree specs, attrs;
11923 chain = TREE_CHAIN (parm);
11924 /* @@ weak defense against parse errors. */
11925 if (TREE_CODE (decl) != VOID_TYPE
11926 && TREE_CODE (decl) != TREE_LIST)
11928 /* Give various messages as the need arises. */
11929 if (TREE_CODE (decl) == STRING_CST)
11930 error ("invalid string constant `%E'", decl);
11931 else if (TREE_CODE (decl) == INTEGER_CST)
11932 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11933 continue;
11936 if (parm == void_list_node)
11937 break;
11939 split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
11940 decl = grokdeclarator (TREE_VALUE (decl), specs,
11941 PARM, init != NULL_TREE, &attrs);
11942 if (! decl || TREE_TYPE (decl) == error_mark_node)
11943 continue;
11945 if (attrs)
11946 cplus_decl_attributes (&decl, attrs, 0);
11948 type = TREE_TYPE (decl);
11949 if (VOID_TYPE_P (type))
11951 if (same_type_p (type, void_type_node)
11952 && !DECL_NAME (decl) && !result && !chain && !ellipsis)
11953 /* this is a parmlist of `(void)', which is ok. */
11954 break;
11955 cxx_incomplete_type_error (decl, type);
11956 /* It's not a good idea to actually create parameters of
11957 type `void'; other parts of the compiler assume that a
11958 void type terminates the parameter list. */
11959 type = error_mark_node;
11960 TREE_TYPE (decl) = error_mark_node;
11963 if (type != error_mark_node)
11965 /* Top-level qualifiers on the parameters are
11966 ignored for function types. */
11967 type = TYPE_MAIN_VARIANT (type);
11968 if (TREE_CODE (type) == METHOD_TYPE)
11970 error ("parameter `%D' invalidly declared method type", decl);
11971 type = build_pointer_type (type);
11972 TREE_TYPE (decl) = type;
11974 else if (TREE_CODE (type) == OFFSET_TYPE)
11976 error ("parameter `%D' invalidly declared offset type", decl);
11977 type = build_pointer_type (type);
11978 TREE_TYPE (decl) = type;
11980 else if (abstract_virtuals_error (decl, type))
11981 any_error = 1; /* Seems like a good idea. */
11982 else if (POINTER_TYPE_P (type))
11984 /* [dcl.fct]/6, parameter types cannot contain pointers
11985 (references) to arrays of unknown bound. */
11986 tree t = TREE_TYPE (type);
11987 int ptr = TYPE_PTR_P (type);
11989 while (1)
11991 if (TYPE_PTR_P (t))
11992 ptr = 1;
11993 else if (TREE_CODE (t) != ARRAY_TYPE)
11994 break;
11995 else if (!TYPE_DOMAIN (t))
11996 break;
11997 t = TREE_TYPE (t);
11999 if (TREE_CODE (t) == ARRAY_TYPE)
12000 error ("parameter `%D' includes %s to array of unknown bound `%T'",
12001 decl, ptr ? "pointer" : "reference", t);
12004 if (!any_error && init)
12005 init = check_default_argument (decl, init);
12006 else
12007 init = NULL_TREE;
12010 TREE_CHAIN (decl) = decls;
12011 decls = decl;
12012 result = tree_cons (init, type, result);
12014 decls = nreverse (decls);
12015 result = nreverse (result);
12016 if (!ellipsis)
12017 result = chainon (result, void_list_node);
12018 last_function_parms = decls;
12020 return result;
12024 /* D is a constructor or overloaded `operator='.
12026 Let T be the class in which D is declared. Then, this function
12027 returns:
12029 -1 if D's is an ill-formed constructor or copy assignment operator
12030 whose first parameter is of type `T'.
12031 0 if D is not a copy constructor or copy assignment
12032 operator.
12033 1 if D is a copy constructor or copy assignment operator whose
12034 first parameter is a reference to const qualified T.
12035 2 if D is a copy constructor or copy assignment operator whose
12036 first parameter is a reference to non-const qualified T.
12038 This function can be used as a predicate. Positive values indicate
12039 a copy constructor and nonzero values indicate a copy assignment
12040 operator. */
12043 copy_fn_p (tree d)
12045 tree args;
12046 tree arg_type;
12047 int result = 1;
12049 my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12051 if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12052 /* Instantiations of template member functions are never copy
12053 functions. Note that member functions of templated classes are
12054 represented as template functions internally, and we must
12055 accept those as copy functions. */
12056 return 0;
12058 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12059 if (!args)
12060 return 0;
12062 arg_type = TREE_VALUE (args);
12064 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12066 /* Pass by value copy assignment operator. */
12067 result = -1;
12069 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12070 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12072 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12073 result = 2;
12075 else
12076 return 0;
12078 args = TREE_CHAIN (args);
12080 if (args && args != void_list_node && !TREE_PURPOSE (args))
12081 /* There are more non-optional args. */
12082 return 0;
12084 return result;
12087 /* Remember any special properties of member function DECL. */
12089 void grok_special_member_properties (tree decl)
12091 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12092 ; /* Not special. */
12093 else if (DECL_CONSTRUCTOR_P (decl))
12095 int ctor = copy_fn_p (decl);
12097 if (ctor > 0)
12099 /* [class.copy]
12101 A non-template constructor for class X is a copy
12102 constructor if its first parameter is of type X&, const
12103 X&, volatile X& or const volatile X&, and either there
12104 are no other parameters or else all other parameters have
12105 default arguments. */
12106 TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12107 if (ctor > 1)
12108 TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12110 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12111 TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12113 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12115 /* [class.copy]
12117 A non-template assignment operator for class X is a copy
12118 assignment operator if its parameter is of type X, X&, const
12119 X&, volatile X& or const volatile X&. */
12121 int assop = copy_fn_p (decl);
12123 if (assop)
12125 TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12126 if (assop != 1)
12127 TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12128 if (DECL_PURE_VIRTUAL_P (decl))
12129 TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12134 /* Check a constructor DECL has the correct form. Complains
12135 if the class has a constructor of the form X(X). */
12138 grok_ctor_properties (tree ctype, tree decl)
12140 int ctor_parm = copy_fn_p (decl);
12142 if (ctor_parm < 0)
12144 /* [class.copy]
12146 A declaration of a constructor for a class X is ill-formed if
12147 its first parameter is of type (optionally cv-qualified) X
12148 and either there are no other parameters or else all other
12149 parameters have default arguments.
12151 We *don't* complain about member template instantiations that
12152 have this form, though; they can occur as we try to decide
12153 what constructor to use during overload resolution. Since
12154 overload resolution will never prefer such a constructor to
12155 the non-template copy constructor (which is either explicitly
12156 or implicitly defined), there's no need to worry about their
12157 existence. Theoretically, they should never even be
12158 instantiated, but that's hard to forestall. */
12159 error ("invalid constructor; you probably meant `%T (const %T&)'",
12160 ctype, ctype);
12161 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12162 return 0;
12165 return 1;
12168 /* An operator with this code is unary, but can also be binary. */
12170 static int
12171 ambi_op_p (enum tree_code code)
12173 return (code == INDIRECT_REF
12174 || code == ADDR_EXPR
12175 || code == CONVERT_EXPR
12176 || code == NEGATE_EXPR
12177 || code == PREINCREMENT_EXPR
12178 || code == PREDECREMENT_EXPR);
12181 /* An operator with this name can only be unary. */
12183 static int
12184 unary_op_p (enum tree_code code)
12186 return (code == TRUTH_NOT_EXPR
12187 || code == BIT_NOT_EXPR
12188 || code == COMPONENT_REF
12189 || code == TYPE_EXPR);
12192 /* Do a little sanity-checking on how they declared their operator. */
12194 void
12195 grok_op_properties (tree decl, int friendp)
12197 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12198 tree argtype;
12199 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12200 tree name = DECL_NAME (decl);
12201 enum tree_code operator_code;
12202 int arity;
12204 /* Count the number of arguments. */
12205 for (argtype = argtypes, arity = 0;
12206 argtype && argtype != void_list_node;
12207 argtype = TREE_CHAIN (argtype))
12208 ++arity;
12210 if (current_class_type == NULL_TREE)
12211 friendp = 1;
12213 if (DECL_CONV_FN_P (decl))
12214 operator_code = TYPE_EXPR;
12215 else
12218 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12219 if (ansi_opname (CODE) == name) \
12221 operator_code = (CODE); \
12222 break; \
12224 else if (ansi_assopname (CODE) == name) \
12226 operator_code = (CODE); \
12227 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12228 break; \
12231 #include "operators.def"
12232 #undef DEF_OPERATOR
12234 abort ();
12236 while (0);
12237 my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12238 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12240 if (! friendp)
12242 switch (operator_code)
12244 case CALL_EXPR:
12245 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12246 break;
12248 case ARRAY_REF:
12249 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12250 break;
12252 case COMPONENT_REF:
12253 case MEMBER_REF:
12254 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12255 break;
12257 case NEW_EXPR:
12258 TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12259 break;
12261 case DELETE_EXPR:
12262 TYPE_GETS_DELETE (current_class_type) |= 1;
12263 break;
12265 case VEC_NEW_EXPR:
12266 TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12267 break;
12269 case VEC_DELETE_EXPR:
12270 TYPE_GETS_DELETE (current_class_type) |= 2;
12271 break;
12273 default:
12274 break;
12278 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12280 /* When the compiler encounters the definition of A::operator new, it
12281 doesn't look at the class declaration to find out if it's static. */
12282 if (methodp)
12283 revert_static_member_fn (decl);
12285 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12287 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12289 if (methodp)
12290 revert_static_member_fn (decl);
12292 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12294 else
12296 /* An operator function must either be a non-static member function
12297 or have at least one parameter of a class, a reference to a class,
12298 an enumeration, or a reference to an enumeration. 13.4.0.6 */
12299 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12301 if (operator_code == TYPE_EXPR
12302 || operator_code == CALL_EXPR
12303 || operator_code == COMPONENT_REF
12304 || operator_code == ARRAY_REF
12305 || operator_code == NOP_EXPR)
12306 error ("`%D' must be a nonstatic member function", decl);
12307 else
12309 tree p = argtypes;
12311 if (DECL_STATIC_FUNCTION_P (decl))
12312 error ("`%D' must be either a non-static member function or a non-member function", decl);
12314 if (p)
12315 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
12317 tree arg = TREE_VALUE (p);
12318 if (TREE_CODE (arg) == REFERENCE_TYPE)
12319 arg = TREE_TYPE (arg);
12321 /* This lets bad template code slip through. */
12322 if (IS_AGGR_TYPE (arg)
12323 || TREE_CODE (arg) == ENUMERAL_TYPE
12324 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12325 || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
12326 goto foundaggr;
12328 error
12329 ("`%D' must have an argument of class or enumerated type",
12330 decl);
12331 foundaggr:
12336 if (operator_code == CALL_EXPR)
12337 return; /* No restrictions on args. */
12339 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12341 tree t = TREE_TYPE (name);
12342 if (! friendp)
12344 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12345 const char *what = 0;
12347 if (ref)
12348 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12350 if (TREE_CODE (t) == VOID_TYPE)
12351 what = "void";
12352 else if (t == current_class_type)
12353 what = "the same type";
12354 /* Don't force t to be complete here. */
12355 else if (IS_AGGR_TYPE (t)
12356 && COMPLETE_TYPE_P (t)
12357 && DERIVED_FROM_P (t, current_class_type))
12358 what = "a base class";
12360 if (what)
12361 warning ("conversion to %s%s will never use a type conversion operator",
12362 ref ? "a reference to " : "", what);
12365 if (operator_code == COND_EXPR)
12367 /* 13.4.0.3 */
12368 error ("ISO C++ prohibits overloading operator ?:");
12370 else if (ambi_op_p (operator_code))
12372 if (arity == 1)
12373 /* We pick the one-argument operator codes by default, so
12374 we don't have to change anything. */
12376 else if (arity == 2)
12378 /* If we thought this was a unary operator, we now know
12379 it to be a binary operator. */
12380 switch (operator_code)
12382 case INDIRECT_REF:
12383 operator_code = MULT_EXPR;
12384 break;
12386 case ADDR_EXPR:
12387 operator_code = BIT_AND_EXPR;
12388 break;
12390 case CONVERT_EXPR:
12391 operator_code = PLUS_EXPR;
12392 break;
12394 case NEGATE_EXPR:
12395 operator_code = MINUS_EXPR;
12396 break;
12398 case PREINCREMENT_EXPR:
12399 operator_code = POSTINCREMENT_EXPR;
12400 break;
12402 case PREDECREMENT_EXPR:
12403 operator_code = POSTDECREMENT_EXPR;
12404 break;
12406 default:
12407 abort ();
12410 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12412 if ((operator_code == POSTINCREMENT_EXPR
12413 || operator_code == POSTDECREMENT_EXPR)
12414 && ! processing_template_decl
12415 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12417 if (methodp)
12418 error ("postfix `%D' must take `int' as its argument",
12419 decl);
12420 else
12421 error
12422 ("postfix `%D' must take `int' as its second argument",
12423 decl);
12426 else
12428 if (methodp)
12429 error ("`%D' must take either zero or one argument", decl);
12430 else
12431 error ("`%D' must take either one or two arguments", decl);
12434 /* More Effective C++ rule 6. */
12435 if (warn_ecpp
12436 && (operator_code == POSTINCREMENT_EXPR
12437 || operator_code == POSTDECREMENT_EXPR
12438 || operator_code == PREINCREMENT_EXPR
12439 || operator_code == PREDECREMENT_EXPR))
12441 tree arg = TREE_VALUE (argtypes);
12442 tree ret = TREE_TYPE (TREE_TYPE (decl));
12443 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12444 arg = TREE_TYPE (arg);
12445 arg = TYPE_MAIN_VARIANT (arg);
12446 if (operator_code == PREINCREMENT_EXPR
12447 || operator_code == PREDECREMENT_EXPR)
12449 if (TREE_CODE (ret) != REFERENCE_TYPE
12450 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12451 arg))
12452 warning ("prefix `%D' should return `%T'", decl,
12453 build_reference_type (arg));
12455 else
12457 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12458 warning ("postfix `%D' should return `%T'", decl, arg);
12462 else if (unary_op_p (operator_code))
12464 if (arity != 1)
12466 if (methodp)
12467 error ("`%D' must take `void'", decl);
12468 else
12469 error ("`%D' must take exactly one argument", decl);
12472 else /* if (binary_op_p (operator_code)) */
12474 if (arity != 2)
12476 if (methodp)
12477 error ("`%D' must take exactly one argument", decl);
12478 else
12479 error ("`%D' must take exactly two arguments", decl);
12482 /* More Effective C++ rule 7. */
12483 if (warn_ecpp
12484 && (operator_code == TRUTH_ANDIF_EXPR
12485 || operator_code == TRUTH_ORIF_EXPR
12486 || operator_code == COMPOUND_EXPR))
12487 warning ("user-defined `%D' always evaluates both arguments",
12488 decl);
12491 /* Effective C++ rule 23. */
12492 if (warn_ecpp
12493 && arity == 2
12494 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12495 && (operator_code == PLUS_EXPR
12496 || operator_code == MINUS_EXPR
12497 || operator_code == TRUNC_DIV_EXPR
12498 || operator_code == MULT_EXPR
12499 || operator_code == TRUNC_MOD_EXPR)
12500 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12501 warning ("`%D' should return by value", decl);
12503 /* [over.oper]/8 */
12504 for (; argtypes && argtypes != void_list_node;
12505 argtypes = TREE_CHAIN (argtypes))
12506 if (TREE_PURPOSE (argtypes))
12508 TREE_PURPOSE (argtypes) = NULL_TREE;
12509 if (operator_code == POSTINCREMENT_EXPR
12510 || operator_code == POSTDECREMENT_EXPR)
12512 if (pedantic)
12513 pedwarn ("`%D' cannot have default arguments", decl);
12515 else
12516 error ("`%D' cannot have default arguments", decl);
12522 static const char *
12523 tag_name (enum tag_types code)
12525 switch (code)
12527 case record_type:
12528 return "struct";
12529 case class_type:
12530 return "class";
12531 case union_type:
12532 return "union ";
12533 case enum_type:
12534 return "enum";
12535 default:
12536 abort ();
12540 /* Name lookup in an elaborated-type-specifier (after the keyword
12541 indicated by TAG_CODE) has found TYPE. If the
12542 elaborated-type-specifier is invalid, issue a diagnostic and return
12543 error_mark_node; otherwise, return TYPE itself. */
12545 static tree
12546 check_elaborated_type_specifier (enum tag_types tag_code,
12547 tree type)
12549 tree t;
12551 t = follow_tag_typedef (type);
12553 /* [dcl.type.elab] If the identifier resolves to a typedef-name or a
12554 template type-parameter, the elaborated-type-specifier is
12555 ill-formed. */
12556 if (!t)
12558 error ("using typedef-name `%D' after `%s'",
12559 TYPE_NAME (type), tag_name (tag_code));
12560 t = error_mark_node;
12562 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12564 error ("using template type parameter `%T' after `%s'",
12565 type, tag_name (tag_code));
12566 t = error_mark_node;
12569 return t;
12572 /* Get the struct, enum or union (CODE says which) with tag NAME.
12573 Define the tag as a forward-reference if it is not defined.
12575 C++: If a class derivation is given, process it here, and report
12576 an error if multiple derivation declarations are not identical.
12578 If this is a definition, come in through xref_tag and only look in
12579 the current frame for the name (since C++ allows new names in any
12580 scope.) */
12582 tree
12583 xref_tag (enum tag_types tag_code, tree name, tree attributes,
12584 bool globalize)
12586 enum tree_code code;
12587 register tree ref, t;
12588 struct cp_binding_level *b = current_binding_level;
12589 tree context = NULL_TREE;
12591 timevar_push (TV_NAME_LOOKUP);
12592 switch (tag_code)
12594 case record_type:
12595 case class_type:
12596 code = RECORD_TYPE;
12597 break;
12598 case union_type:
12599 code = UNION_TYPE;
12600 break;
12601 case enum_type:
12602 code = ENUMERAL_TYPE;
12603 break;
12604 default:
12605 abort ();
12608 /* If a cross reference is requested, look up the type
12609 already defined for this tag and return it. */
12610 if (TYPE_P (name))
12612 t = name;
12613 name = TYPE_IDENTIFIER (t);
12615 else
12616 t = IDENTIFIER_TYPE_VALUE (name);
12618 /* Warn about 'friend struct Inherited;' doing the wrong thing. */
12619 if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
12621 static int explained;
12622 tree shadowed;
12624 warning ("`%s %T' declares a new type at namespace scope",
12625 tag_name (tag_code), name);
12626 if (!explained++)
12627 warning (" names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
12628 tag_name (tag_code),
12629 constructor_name (current_class_type),
12630 TYPE_IDENTIFIER (t));
12632 /* We need to remove the class scope binding for the
12633 TYPENAME_TYPE as otherwise poplevel_class gets confused. */
12634 for (shadowed = b->class_shadowed;
12635 shadowed;
12636 shadowed = TREE_CHAIN (shadowed))
12637 if (TREE_TYPE (shadowed) == TYPE_NAME (t))
12639 TREE_PURPOSE (shadowed) = NULL_TREE;
12640 break;
12644 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12645 && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
12646 t = NULL_TREE;
12648 if (! globalize)
12650 /* If we know we are defining this tag, only look it up in
12651 this scope and don't try to find it as a type. */
12652 ref = lookup_tag (code, name, b, 1);
12654 else
12656 if (t)
12658 ref = check_elaborated_type_specifier (tag_code, t);
12659 if (ref == error_mark_node)
12660 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12662 else
12663 ref = lookup_tag (code, name, b, 0);
12665 if (! ref)
12667 /* Try finding it as a type declaration. If that wins,
12668 use it. */
12669 ref = lookup_name (name, 1);
12671 if (ref != NULL_TREE
12672 && processing_template_decl
12673 && DECL_CLASS_TEMPLATE_P (ref)
12674 && template_class_depth (current_class_type) == 0)
12675 /* Since GLOBALIZE is true, we're declaring a global
12676 template, so we want this type. */
12677 ref = DECL_TEMPLATE_RESULT (ref);
12679 if (ref && TREE_CODE (ref) == TYPE_DECL)
12681 ref = check_elaborated_type_specifier (tag_code,
12682 TREE_TYPE (ref));
12683 if (ref == error_mark_node)
12684 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12685 if (ref && TREE_CODE (ref) != code)
12686 ref = NULL_TREE;
12688 else
12689 ref = NULL_TREE;
12692 if (ref && current_class_type
12693 && template_class_depth (current_class_type)
12694 && PROCESSING_REAL_TEMPLATE_DECL_P ())
12696 /* Since GLOBALIZE is nonzero, we are not looking at a
12697 definition of this tag. Since, in addition, we are currently
12698 processing a (member) template declaration of a template
12699 class, we must be very careful; consider:
12701 template <class X>
12702 struct S1
12704 template <class U>
12705 struct S2
12706 { template <class V>
12707 friend struct S1; };
12709 Here, the S2::S1 declaration should not be confused with the
12710 outer declaration. In particular, the inner version should
12711 have a template parameter of level 2, not level 1. This
12712 would be particularly important if the member declaration
12713 were instead:
12715 template <class V = U> friend struct S1;
12717 say, when we should tsubst into `U' when instantiating
12718 S2. On the other hand, when presented with:
12720 template <class T>
12721 struct S1 {
12722 template <class U>
12723 struct S2 {};
12724 template <class U>
12725 friend struct S2;
12728 we must find the inner binding eventually. We
12729 accomplish this by making sure that the new type we
12730 create to represent this declaration has the right
12731 TYPE_CONTEXT. */
12732 context = TYPE_CONTEXT (ref);
12733 ref = NULL_TREE;
12737 if (! ref)
12739 /* If no such tag is yet defined, create a forward-reference node
12740 and record it as the "definition".
12741 When a real declaration of this type is found,
12742 the forward-reference will be altered into a real type. */
12743 if (code == ENUMERAL_TYPE)
12745 error ("use of enum `%#D' without previous declaration", name);
12747 ref = make_node (ENUMERAL_TYPE);
12749 /* Give the type a default layout like unsigned int
12750 to avoid crashing if it does not get defined. */
12751 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12752 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12753 TYPE_USER_ALIGN (ref) = 0;
12754 TREE_UNSIGNED (ref) = 1;
12755 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12756 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12757 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12759 /* Enable us to recognize when a type is created in class context.
12760 To do nested classes correctly, this should probably be cleared
12761 out when we leave this classes scope. Currently this in only
12762 done in `start_enum'. */
12764 pushtag (name, ref, globalize);
12766 else
12768 struct cp_binding_level *old_b = class_binding_level;
12770 ref = make_aggr_type (code);
12771 TYPE_CONTEXT (ref) = context;
12773 #ifdef NONNESTED_CLASSES
12774 /* Class types don't nest the way enums do. */
12775 class_binding_level = (struct cp_binding_level *)0;
12776 #endif
12777 pushtag (name, ref, globalize);
12778 class_binding_level = old_b;
12781 else
12783 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12784 redeclare_class_template (ref, current_template_parms);
12787 TYPE_ATTRIBUTES (ref) = attributes;
12789 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ref);
12792 tree
12793 xref_tag_from_type (tree old, tree id, int globalize)
12795 enum tag_types tag_kind;
12797 if (TREE_CODE (old) == RECORD_TYPE)
12798 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12799 else
12800 tag_kind = union_type;
12802 if (id == NULL_TREE)
12803 id = TYPE_IDENTIFIER (old);
12805 return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
12808 /* REF is a type (named NAME), for which we have just seen some
12809 baseclasses. BASE_LIST is a list of those baseclasses; the
12810 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12811 the base-class. TREE_VIA_VIRTUAL indicates virtual
12812 inheritance. CODE_TYPE_NODE indicates whether REF is a class,
12813 struct, or union. */
12815 void
12816 xref_basetypes (tree ref, tree base_list)
12818 /* In the declaration `A : X, Y, ... Z' we mark all the types
12819 (A, X, Y, ..., Z) so we can check for duplicates. */
12820 tree *basep;
12822 int i;
12823 enum tag_types tag_code;
12825 if (TREE_CODE (ref) == UNION_TYPE)
12827 error ("derived union `%T' invalid", ref);
12828 return;
12831 tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12833 /* First, make sure that any templates in base-classes are
12834 instantiated. This ensures that if we call ourselves recursively
12835 we do not get confused about which classes are marked and which
12836 are not. */
12837 basep = &base_list;
12838 while (*basep)
12840 tree basetype = TREE_VALUE (*basep);
12841 if (!(processing_template_decl && uses_template_parms (basetype))
12842 && !complete_type_or_else (basetype, NULL))
12843 /* An incomplete type. Remove it from the list. */
12844 *basep = TREE_CHAIN (*basep);
12845 else
12846 basep = &TREE_CHAIN (*basep);
12849 SET_CLASSTYPE_MARKED (ref);
12850 i = list_length (base_list);
12851 if (i)
12853 tree binfo = TYPE_BINFO (ref);
12854 tree binfos = make_tree_vec (i);
12855 tree accesses = make_tree_vec (i);
12857 BINFO_BASETYPES (binfo) = binfos;
12858 BINFO_BASEACCESSES (binfo) = accesses;
12860 for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
12862 tree access = TREE_PURPOSE (base_list);
12863 int via_virtual = TREE_VIA_VIRTUAL (base_list);
12864 tree basetype = TREE_VALUE (base_list);
12865 tree base_binfo;
12867 if (access == access_default_node)
12868 /* The base of a derived struct is public by default. */
12869 access = (tag_code == class_type
12870 ? access_private_node : access_public_node);
12872 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12873 basetype = TREE_TYPE (basetype);
12874 if (!basetype
12875 || (TREE_CODE (basetype) != RECORD_TYPE
12876 && TREE_CODE (basetype) != TYPENAME_TYPE
12877 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12878 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12880 error ("base type `%T' fails to be a struct or class type",
12881 basetype);
12882 continue;
12885 if (CLASSTYPE_MARKED (basetype))
12887 if (basetype == ref)
12888 error ("recursive type `%T' undefined", basetype);
12889 else
12890 error ("duplicate base type `%T' invalid", basetype);
12891 continue;
12894 if (TYPE_FOR_JAVA (basetype)
12895 && (current_lang_depth () == 0))
12896 TYPE_FOR_JAVA (ref) = 1;
12898 if (CLASS_TYPE_P (basetype))
12900 base_binfo = TYPE_BINFO (basetype);
12901 /* This flag will be in the binfo of the base type, we must
12902 clear it after copying the base binfos. */
12903 BINFO_DEPENDENT_BASE_P (base_binfo)
12904 = dependent_type_p (basetype);
12906 else
12907 base_binfo = make_binfo (size_zero_node, basetype,
12908 NULL_TREE, NULL_TREE);
12910 TREE_VEC_ELT (binfos, i) = base_binfo;
12911 TREE_VEC_ELT (accesses, i) = access;
12912 /* This flag will be in the binfo of the base type, we must
12913 clear it after copying the base binfos. */
12914 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12916 SET_CLASSTYPE_MARKED (basetype);
12918 /* We are free to modify these bits because they are meaningless
12919 at top level, and BASETYPE is a top-level type. */
12920 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12922 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12923 /* Converting to a virtual base class requires looking
12924 up the offset of the virtual base. */
12925 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12928 if (CLASS_TYPE_P (basetype))
12930 TYPE_HAS_NEW_OPERATOR (ref)
12931 |= TYPE_HAS_NEW_OPERATOR (basetype);
12932 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12933 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12934 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12935 /* If the base-class uses multiple inheritance, so do we. */
12936 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12937 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12938 /* Likewise, if converting to a base of the base may require
12939 code, then we may need to generate code to convert to a
12940 base as well. */
12941 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12942 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12944 i++;
12946 if (i)
12947 TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
12948 else
12949 BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
12951 if (i > 1)
12953 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12954 /* If there is more than one non-empty they cannot be at the same
12955 address. */
12956 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12960 /* Copy the base binfos, collect the virtual bases and set the
12961 inheritance order chain. */
12962 copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
12963 CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
12965 /* Unmark all the types. */
12966 while (i--)
12968 tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
12970 CLEAR_CLASSTYPE_MARKED (basetype);
12971 if (CLASS_TYPE_P (basetype))
12973 TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
12974 BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
12977 CLEAR_CLASSTYPE_MARKED (ref);
12981 /* Begin compiling the definition of an enumeration type.
12982 NAME is its name (or null if anonymous).
12983 Returns the type object, as yet incomplete.
12984 Also records info about it so that build_enumerator
12985 may be used to declare the individual values as they are read. */
12987 tree
12988 start_enum (tree name)
12990 register tree enumtype = NULL_TREE;
12991 struct cp_binding_level *b = current_binding_level;
12993 /* If this is the real definition for a previous forward reference,
12994 fill in the contents in the same object that used to be the
12995 forward reference. */
12997 if (name != NULL_TREE)
12998 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
13000 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13002 error ("multiple definition of `%#T'", enumtype);
13003 cp_error_at ("previous definition here", enumtype);
13004 /* Clear out TYPE_VALUES, and start again. */
13005 TYPE_VALUES (enumtype) = NULL_TREE;
13007 else
13009 enumtype = make_node (ENUMERAL_TYPE);
13010 pushtag (name, enumtype, 0);
13013 return enumtype;
13016 /* After processing and defining all the values of an enumeration type,
13017 install their decls in the enumeration type and finish it off.
13018 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
13020 void
13021 finish_enum (tree enumtype)
13023 tree pair;
13024 tree minnode;
13025 tree maxnode;
13026 tree t;
13027 bool unsignedp;
13028 int lowprec;
13029 int highprec;
13030 int precision;
13032 /* We built up the VALUES in reverse order. */
13033 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13035 /* For an enum defined in a template, just set the type of the values;
13036 all further processing is postponed until the template is
13037 instantiated. We need to set the type so that tsubst of a CONST_DECL
13038 works. */
13039 if (processing_template_decl)
13041 for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13042 TREE_TYPE (TREE_VALUE (pair)) = enumtype;
13043 if (at_function_scope_p ())
13044 add_stmt (build_min (TAG_DEFN, enumtype));
13045 return;
13048 if (TYPE_VALUES (enumtype))
13050 minnode = maxnode = NULL_TREE;
13052 for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13054 tree decl = TREE_VALUE (pair);
13055 tree value = DECL_INITIAL (decl);
13057 /* [dcl.enum]: Following the closing brace of an enum-specifier,
13058 each enumerator has the type of its enumeration. Prior to the
13059 closing brace, the type of each enumerator is the type of its
13060 initializing value. */
13061 TREE_TYPE (decl) = enumtype;
13063 /* Figure out what the minimum and maximum values of the
13064 enumerators are. */
13065 if (!minnode)
13066 minnode = maxnode = value;
13067 else if (tree_int_cst_lt (maxnode, value))
13068 maxnode = value;
13069 else if (tree_int_cst_lt (value, minnode))
13070 minnode = value;
13072 /* Set the TREE_TYPE for the values as well. That's so that when
13073 we call decl_constant_value we get an entity of the right type
13074 (but with the constant value). But first make a copy so we
13075 don't clobber shared INTEGER_CSTs. */
13076 if (TREE_TYPE (value) != enumtype)
13078 value = DECL_INITIAL (decl) = copy_node (value);
13079 TREE_TYPE (value) = enumtype;
13082 /* In addition, transform the TYPE_VALUES list to contain the
13083 values, rather than the CONST_DECLs for them. */
13084 TREE_VALUE (pair) = value;
13087 else
13088 minnode = maxnode = integer_zero_node;
13090 /* Compute the number of bits require to represent all values of the
13091 enumeration. We must do this before the type of MINNODE and
13092 MAXNODE are transformed, since min_precision relies on the
13093 TREE_TYPE of the value it is passed. */
13094 unsignedp = tree_int_cst_sgn (minnode) >= 0;
13095 lowprec = min_precision (minnode, unsignedp);
13096 highprec = min_precision (maxnode, unsignedp);
13097 precision = MAX (lowprec, highprec);
13099 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
13100 TYPE_SIZE (enumtype) = NULL_TREE;
13101 TYPE_PRECISION (enumtype) = precision;
13102 if (unsignedp)
13103 fixup_unsigned_type (enumtype);
13104 else
13105 fixup_signed_type (enumtype);
13107 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13108 /* Use the width of the narrowest normal C type which is wide
13109 enough. */
13110 TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size
13111 (precision, 1));
13112 else
13113 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
13115 TYPE_SIZE (enumtype) = NULL_TREE;
13116 layout_type (enumtype);
13118 /* Fix up all variant types of this enum type. */
13119 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13121 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13122 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13123 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13124 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13125 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13126 TYPE_MODE (t) = TYPE_MODE (enumtype);
13127 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13128 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13129 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13130 TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13133 /* Finish debugging output for this type. */
13134 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13137 /* Build and install a CONST_DECL for an enumeration constant of the
13138 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13139 Assignment of sequential values by default is handled here. */
13141 void
13142 build_enumerator (tree name, tree value, tree enumtype)
13144 tree decl;
13145 tree context;
13146 tree type;
13148 /* Remove no-op casts from the value. */
13149 if (value)
13150 STRIP_TYPE_NOPS (value);
13152 if (! processing_template_decl)
13154 /* Validate and default VALUE. */
13155 if (value != NULL_TREE)
13157 value = decl_constant_value (value);
13159 if (TREE_CODE (value) == INTEGER_CST)
13161 value = default_conversion (value);
13162 constant_expression_warning (value);
13164 else
13166 error ("enumerator value for `%D' not integer constant", name);
13167 value = NULL_TREE;
13171 /* Default based on previous value. */
13172 if (value == NULL_TREE)
13174 tree prev_value;
13176 if (TYPE_VALUES (enumtype))
13178 /* The next value is the previous value ... */
13179 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13180 /* ... plus one. */
13181 value = cp_build_binary_op (PLUS_EXPR,
13182 prev_value,
13183 integer_one_node);
13185 if (tree_int_cst_lt (value, prev_value))
13186 error ("overflow in enumeration values at `%D'", name);
13188 else
13189 value = integer_zero_node;
13192 /* Remove no-op casts from the value. */
13193 STRIP_TYPE_NOPS (value);
13196 /* C++ associates enums with global, function, or class declarations. */
13197 context = current_scope ();
13199 /* Build the actual enumeration constant. Note that the enumeration
13200 constants have the type of their initializers until the
13201 enumeration is complete:
13203 [ dcl.enum ]
13205 Following the closing brace of an enum-specifier, each enumer-
13206 ator has the type of its enumeration. Prior to the closing
13207 brace, the type of each enumerator is the type of its
13208 initializing value.
13210 In finish_enum we will reset the type. Of course, if we're
13211 processing a template, there may be no value. */
13212 type = value ? TREE_TYPE (value) : NULL_TREE;
13214 if (context && context == current_class_type)
13215 /* This enum declaration is local to the class. We need the full
13216 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
13217 decl = build_lang_decl (CONST_DECL, name, type);
13218 else
13219 /* It's a global enum, or it's local to a function. (Note local to
13220 a function could mean local to a class method. */
13221 decl = build_decl (CONST_DECL, name, type);
13223 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13224 TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
13225 DECL_INITIAL (decl) = value;
13227 if (context && context == current_class_type)
13228 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13229 on the TYPE_FIELDS list for `S'. (That's so that you can say
13230 things like `S::i' later.) */
13231 finish_member_declaration (decl);
13232 else
13233 pushdecl (decl);
13235 /* Add this enumeration constant to the list for this type. */
13236 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13240 /* We're defining DECL. Make sure that it's type is OK. */
13242 static void
13243 check_function_type (tree decl, tree current_function_parms)
13245 tree fntype = TREE_TYPE (decl);
13246 tree return_type = complete_type (TREE_TYPE (fntype));
13248 /* In a function definition, arg types must be complete. */
13249 require_complete_types_for_parms (current_function_parms);
13251 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13253 error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13255 /* Make it return void instead, but don't change the
13256 type of the DECL_RESULT, in case we have a named return value. */
13257 if (TREE_CODE (fntype) == METHOD_TYPE)
13259 tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13260 TREE_TYPE (decl)
13261 = build_cplus_method_type (ctype,
13262 void_type_node,
13263 FUNCTION_ARG_CHAIN (decl));
13265 else
13266 TREE_TYPE (decl)
13267 = build_function_type (void_type_node,
13268 TYPE_ARG_TYPES (TREE_TYPE (decl)));
13269 TREE_TYPE (decl)
13270 = build_exception_variant (fntype,
13271 TYPE_RAISES_EXCEPTIONS (fntype));
13273 else
13274 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13277 /* Create the FUNCTION_DECL for a function definition.
13278 DECLSPECS and DECLARATOR are the parts of the declaration;
13279 they describe the function's name and the type it returns,
13280 but twisted together in a fashion that parallels the syntax of C.
13282 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13283 DECLARATOR is really the DECL for the function we are about to
13284 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13285 indicating that the function is an inline defined in-class.
13287 This function creates a binding context for the function body
13288 as well as setting up the FUNCTION_DECL in current_function_decl.
13290 Returns 1 on success. If the DECLARATOR is not suitable for a function
13291 (it defines a datum instead), we return 0, which tells
13292 yyparse to report a parse error.
13294 For C++, we must first check whether that datum makes any sense.
13295 For example, "class A local_a(1,2);" means that variable local_a
13296 is an aggregate of type A, which should have a constructor
13297 applied to it with the argument list [1, 2]. */
13300 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13302 tree decl1;
13303 tree ctype = NULL_TREE;
13304 tree fntype;
13305 tree restype;
13306 int doing_friend = 0;
13307 struct cp_binding_level *bl;
13308 tree current_function_parms;
13310 /* Sanity check. */
13311 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13312 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13314 /* This should only be done once on the top most decl. */
13315 if (have_extern_spec)
13317 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13318 have_extern_spec = false;
13321 if (flags & SF_PRE_PARSED)
13323 decl1 = declarator;
13325 fntype = TREE_TYPE (decl1);
13326 if (TREE_CODE (fntype) == METHOD_TYPE)
13327 ctype = TYPE_METHOD_BASETYPE (fntype);
13329 /* ISO C++ 11.4/5. A friend function defined in a class is in
13330 the (lexical) scope of the class in which it is defined. */
13331 if (!ctype && DECL_FRIEND_P (decl1))
13333 ctype = DECL_FRIEND_CONTEXT (decl1);
13335 /* CTYPE could be null here if we're dealing with a template;
13336 for example, `inline friend float foo()' inside a template
13337 will have no CTYPE set. */
13338 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13339 ctype = NULL_TREE;
13340 else
13341 doing_friend = 1;
13344 last_function_parms = DECL_ARGUMENTS (decl1);
13346 else
13348 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13349 /* If the declarator is not suitable for a function definition,
13350 cause a syntax error. */
13351 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13352 return 0;
13354 cplus_decl_attributes (&decl1, attrs, 0);
13356 /* If #pragma weak was used, mark the decl weak now. */
13357 if (current_binding_level == global_binding_level)
13358 maybe_apply_pragma_weak (decl1);
13360 fntype = TREE_TYPE (decl1);
13362 restype = TREE_TYPE (fntype);
13363 if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
13365 error ("semicolon missing after declaration of `%#T'", restype);
13366 shadow_tag (build_tree_list (NULL_TREE, restype));
13367 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13368 if (TREE_CODE (fntype) == FUNCTION_TYPE)
13369 fntype = build_function_type (integer_type_node,
13370 TYPE_ARG_TYPES (fntype));
13371 else
13372 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13373 integer_type_node,
13374 TYPE_ARG_TYPES (fntype));
13375 TREE_TYPE (decl1) = fntype;
13378 if (TREE_CODE (fntype) == METHOD_TYPE)
13379 ctype = TYPE_METHOD_BASETYPE (fntype);
13380 else if (DECL_MAIN_P (decl1))
13382 /* If this doesn't return integer_type, complain. */
13383 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13385 if (pedantic || warn_return_type)
13386 pedwarn ("return type for `main' changed to `int'");
13387 TREE_TYPE (decl1) = fntype = default_function_type;
13392 if (DECL_DECLARED_INLINE_P (decl1)
13393 && lookup_attribute ("noinline", attrs))
13394 warning ("%Hinline function '%D' given attribute noinline",
13395 &DECL_SOURCE_LOCATION (decl1), decl1);
13397 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13398 /* This is a constructor, we must ensure that any default args
13399 introduced by this definition are propagated to the clones
13400 now. The clones are used directly in overload resolution. */
13401 adjust_clone_args (decl1);
13403 /* Sometimes we don't notice that a function is a static member, and
13404 build a METHOD_TYPE for it. Fix that up now. */
13405 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13406 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13408 revert_static_member_fn (decl1);
13409 last_function_parms = TREE_CHAIN (last_function_parms);
13410 ctype = NULL_TREE;
13413 /* Warn if function was previously implicitly declared
13414 (but not if we warned then). */
13415 if (! warn_implicit
13416 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13417 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13419 /* Set up current_class_type, and enter the scope of the class, if
13420 appropriate. */
13421 if (ctype)
13422 push_nested_class (ctype);
13423 else if (DECL_STATIC_FUNCTION_P (decl1))
13424 push_nested_class (DECL_CONTEXT (decl1));
13426 /* Now that we have entered the scope of the class, we must restore
13427 the bindings for any template parameters surrounding DECL1, if it
13428 is an inline member template. (Order is important; consider the
13429 case where a template parameter has the same name as a field of
13430 the class.) It is not until after this point that
13431 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13432 if (flags & SF_INCLASS_INLINE)
13433 maybe_begin_member_template_processing (decl1);
13435 /* Effective C++ rule 15. */
13436 if (warn_ecpp
13437 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13438 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13439 warning ("`operator=' should return a reference to `*this'");
13441 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13442 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13443 if (!DECL_INITIAL (decl1))
13444 DECL_INITIAL (decl1) = error_mark_node;
13446 /* This function exists in static storage.
13447 (This does not mean `static' in the C sense!) */
13448 TREE_STATIC (decl1) = 1;
13450 /* We must call push_template_decl after current_class_type is set
13451 up. (If we are processing inline definitions after exiting a
13452 class scope, current_class_type will be NULL_TREE until set above
13453 by push_nested_class.) */
13454 if (processing_template_decl)
13455 decl1 = push_template_decl (decl1);
13457 /* We are now in the scope of the function being defined. */
13458 current_function_decl = decl1;
13460 /* Save the parm names or decls from this function's declarator
13461 where store_parm_decls will find them. */
13462 current_function_parms = last_function_parms;
13464 /* Make sure the parameter and return types are reasonable. When
13465 you declare a function, these types can be incomplete, but they
13466 must be complete when you define the function. */
13467 if (! processing_template_decl)
13468 check_function_type (decl1, current_function_parms);
13470 /* Build the return declaration for the function. */
13471 restype = TREE_TYPE (fntype);
13472 /* Promote the value to int before returning it. */
13473 if (c_promoting_integer_type_p (restype))
13474 restype = type_promotes_to (restype);
13475 if (DECL_RESULT (decl1) == NULL_TREE)
13477 DECL_RESULT (decl1)
13478 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13479 c_apply_type_quals_to_decl (cp_type_quals (restype),
13480 DECL_RESULT (decl1));
13483 /* Initialize RTL machinery. We cannot do this until
13484 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13485 even when processing a template; this is how we get
13486 CFUN set up, and our per-function variables initialized.
13487 FIXME factor out the non-RTL stuff. */
13488 bl = current_binding_level;
13489 init_function_start (decl1, input_filename, lineno);
13490 current_binding_level = bl;
13492 /* Even though we're inside a function body, we still don't want to
13493 call expand_expr to calculate the size of a variable-sized array.
13494 We haven't necessarily assigned RTL to all variables yet, so it's
13495 not safe to try to expand expressions involving them. */
13496 immediate_size_expand = 0;
13497 cfun->x_dont_save_pending_sizes_p = 1;
13499 /* Start the statement-tree, start the tree now. */
13500 begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13502 /* Don't double-count statements in templates. */
13503 DECL_NUM_STMTS (decl1) = 0;
13505 /* Let the user know we're compiling this function. */
13506 announce_function (decl1);
13508 /* Record the decl so that the function name is defined.
13509 If we already have a decl for this name, and it is a FUNCTION_DECL,
13510 use the old decl. */
13511 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13513 /* A specialization is not used to guide overload resolution. */
13514 if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13515 && ! DECL_FUNCTION_MEMBER_P (decl1))
13516 decl1 = pushdecl (decl1);
13517 else
13519 /* We need to set the DECL_CONTEXT. */
13520 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13521 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13522 /* And make sure we have enough default args. */
13523 check_default_args (decl1);
13525 fntype = TREE_TYPE (decl1);
13528 /* Reset these in case the call to pushdecl changed them. */
13529 current_function_decl = decl1;
13530 cfun->decl = decl1;
13532 /* If we are (erroneously) defining a function that we have already
13533 defined before, wipe out what we knew before. */
13534 if (!DECL_PENDING_INLINE_P (decl1))
13535 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13537 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13539 /* We know that this was set up by `grokclassfn'. We do not
13540 wait until `store_parm_decls', since evil parse errors may
13541 never get us to that point. Here we keep the consistency
13542 between `current_class_type' and `current_class_ptr'. */
13543 tree t = DECL_ARGUMENTS (decl1);
13545 my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13546 162);
13547 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13548 19990811);
13550 cp_function_chain->x_current_class_ref
13551 = build_indirect_ref (t, NULL);
13552 cp_function_chain->x_current_class_ptr = t;
13554 /* Constructors and destructors need to know whether they're "in
13555 charge" of initializing virtual base classes. */
13556 t = TREE_CHAIN (t);
13557 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13559 current_in_charge_parm = t;
13560 t = TREE_CHAIN (t);
13562 if (DECL_HAS_VTT_PARM_P (decl1))
13564 if (DECL_NAME (t) != vtt_parm_identifier)
13565 abort ();
13566 current_vtt_parm = t;
13570 if (DECL_INTERFACE_KNOWN (decl1))
13572 tree ctx = decl_function_context (decl1);
13574 if (DECL_NOT_REALLY_EXTERN (decl1))
13575 DECL_EXTERNAL (decl1) = 0;
13577 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13578 && TREE_PUBLIC (ctx))
13579 /* This is a function in a local class in an extern inline
13580 function. */
13581 comdat_linkage (decl1);
13583 /* If this function belongs to an interface, it is public.
13584 If it belongs to someone else's interface, it is also external.
13585 This only affects inlines and template instantiations. */
13586 else if (interface_unknown == 0
13587 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13588 || flag_alt_external_templates))
13590 if (DECL_DECLARED_INLINE_P (decl1)
13591 || DECL_TEMPLATE_INSTANTIATION (decl1)
13592 || processing_template_decl)
13594 DECL_EXTERNAL (decl1)
13595 = (interface_only
13596 || (DECL_DECLARED_INLINE_P (decl1)
13597 && ! flag_implement_inlines
13598 && !DECL_VINDEX (decl1)));
13600 /* For WIN32 we also want to put these in linkonce sections. */
13601 maybe_make_one_only (decl1);
13603 else
13604 DECL_EXTERNAL (decl1) = 0;
13605 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13606 DECL_INTERFACE_KNOWN (decl1) = 1;
13608 else if (interface_unknown && interface_only
13609 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13610 || flag_alt_external_templates))
13612 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13613 interface, we will have interface_only set but not
13614 interface_known. In that case, we don't want to use the normal
13615 heuristics because someone will supply a #pragma implementation
13616 elsewhere, and deducing it here would produce a conflict. */
13617 comdat_linkage (decl1);
13618 DECL_EXTERNAL (decl1) = 0;
13619 DECL_INTERFACE_KNOWN (decl1) = 1;
13620 DECL_DEFER_OUTPUT (decl1) = 1;
13622 else
13624 /* This is a definition, not a reference.
13625 So clear DECL_EXTERNAL. */
13626 DECL_EXTERNAL (decl1) = 0;
13628 if ((DECL_DECLARED_INLINE_P (decl1)
13629 || DECL_TEMPLATE_INSTANTIATION (decl1))
13630 && ! DECL_INTERFACE_KNOWN (decl1)
13631 /* Don't try to defer nested functions for now. */
13632 && ! decl_function_context (decl1))
13633 DECL_DEFER_OUTPUT (decl1) = 1;
13634 else
13635 DECL_INTERFACE_KNOWN (decl1) = 1;
13638 pushlevel (0);
13639 current_binding_level->parm_flag = 1;
13641 ++function_depth;
13643 if (DECL_DESTRUCTOR_P (decl1))
13645 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13646 DECL_CONTEXT (dtor_label) = current_function_decl;
13649 start_fname_decls ();
13651 store_parm_decls (current_function_parms);
13653 return 1;
13656 /* Store the parameter declarations into the current function declaration.
13657 This is called after parsing the parameter declarations, before
13658 digesting the body of the function.
13660 Also install to binding contour return value identifier, if any. */
13662 static void
13663 store_parm_decls (tree current_function_parms)
13665 register tree fndecl = current_function_decl;
13666 register tree parm;
13668 /* This is a chain of any other decls that came in among the parm
13669 declarations. If a parm is declared with enum {foo, bar} x;
13670 then CONST_DECLs for foo and bar are put here. */
13671 tree nonparms = NULL_TREE;
13673 if (current_function_parms)
13675 /* This case is when the function was defined with an ANSI prototype.
13676 The parms already have decls, so we need not do anything here
13677 except record them as in effect
13678 and complain if any redundant old-style parm decls were written. */
13680 tree specparms = current_function_parms;
13681 tree next;
13683 /* Must clear this because it might contain TYPE_DECLs declared
13684 at class level. */
13685 storedecls (NULL_TREE);
13687 /* If we're doing semantic analysis, then we'll call pushdecl
13688 for each of these. We must do them in reverse order so that
13689 they end in the correct forward order. */
13690 specparms = nreverse (specparms);
13692 for (parm = specparms; parm; parm = next)
13694 next = TREE_CHAIN (parm);
13695 if (TREE_CODE (parm) == PARM_DECL)
13697 if (DECL_NAME (parm) == NULL_TREE
13698 || TREE_CODE (parm) != VOID_TYPE)
13699 pushdecl (parm);
13700 else
13701 error ("parameter `%D' declared void", parm);
13703 else
13705 /* If we find an enum constant or a type tag,
13706 put it aside for the moment. */
13707 TREE_CHAIN (parm) = NULL_TREE;
13708 nonparms = chainon (nonparms, parm);
13712 /* Get the decls in their original chain order and record in the
13713 function. This is all and only the PARM_DECLs that were
13714 pushed into scope by the loop above. */
13715 DECL_ARGUMENTS (fndecl) = getdecls ();
13716 storetags (gettags ());
13718 else
13719 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13721 /* Now store the final chain of decls for the arguments
13722 as the decl-chain of the current lexical scope.
13723 Put the enumerators in as well, at the front so that
13724 DECL_ARGUMENTS is not modified. */
13725 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13727 /* Do the starting of the exception specifications, if we have any. */
13728 if (flag_exceptions && !processing_template_decl
13729 && flag_enforce_eh_specs
13730 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13731 current_eh_spec_block = begin_eh_spec_block ();
13735 /* We have finished doing semantic analysis on DECL, but have not yet
13736 generated RTL for its body. Save away our current state, so that
13737 when we want to generate RTL later we know what to do. */
13739 static void
13740 save_function_data (tree decl)
13742 struct language_function *f;
13744 /* Save the language-specific per-function data so that we can
13745 get it back when we really expand this function. */
13746 my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13747 19990908);
13749 /* Make a copy. */
13750 f = ((struct language_function *)
13751 ggc_alloc (sizeof (struct language_function)));
13752 memcpy (f, cp_function_chain, sizeof (struct language_function));
13753 DECL_SAVED_FUNCTION_DATA (decl) = f;
13755 /* Clear out the bits we don't need. */
13756 f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13757 f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13758 f->x_named_label_uses = NULL;
13759 f->bindings = NULL;
13760 f->x_local_names = NULL;
13762 /* When we get back here again, we will be expanding. */
13763 f->x_expanding_p = 1;
13765 /* If we've already decided that we cannot inline this function, we
13766 must remember that fact when we actually go to expand the
13767 function. */
13768 if (current_function_cannot_inline)
13770 f->cannot_inline = current_function_cannot_inline;
13771 DECL_INLINE (decl) = 0;
13775 /* Add a note to mark the beginning of the main body of the constructor.
13776 This is used to set up the data structures for the cleanup regions for
13777 fully-constructed bases and members. */
13779 static void
13780 begin_constructor_body (void)
13784 /* Add a note to mark the end of the main body of the constructor. This is
13785 used to end the cleanup regions for fully-constructed bases and
13786 members. */
13788 static void
13789 finish_constructor_body (void)
13793 /* Do all the processing for the beginning of a destructor; set up the
13794 vtable pointers and cleanups for bases and members. */
13796 static void
13797 begin_destructor_body (void)
13799 tree if_stmt;
13800 tree compound_stmt;
13802 /* If the dtor is empty, and we know there is not any possible
13803 way we could use any vtable entries, before they are possibly
13804 set by a base class dtor, we don't have to setup the vtables,
13805 as we know that any base class dtor will set up any vtables
13806 it needs. We avoid MI, because one base class dtor can do a
13807 virtual dispatch to an overridden function that would need to
13808 have a non-related vtable set up, we cannot avoid setting up
13809 vtables in that case. We could change this to see if there
13810 is just one vtable.
13812 ??? In the destructor for a class, the vtables are set
13813 appropriately for that class. There will be no non-related
13814 vtables. jason 2001-12-11. */
13815 if_stmt = begin_if_stmt ();
13817 /* If it is not safe to avoid setting up the vtables, then
13818 someone will change the condition to be boolean_true_node.
13819 (Actually, for now, we do not have code to set the condition
13820 appropriately, so we just assume that we always need to
13821 initialize the vtables.) */
13822 finish_if_stmt_cond (boolean_true_node, if_stmt);
13824 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13826 /* Make all virtual function table pointers in non-virtual base
13827 classes point to CURRENT_CLASS_TYPE's virtual function
13828 tables. */
13829 initialize_vtbl_ptrs (current_class_ptr);
13831 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13832 finish_then_clause (if_stmt);
13833 finish_if_stmt ();
13835 /* And insert cleanups for our bases and members so that they
13836 will be properly destroyed if we throw. */
13837 push_base_cleanups ();
13840 /* At the end of every destructor we generate code to delete the object if
13841 necessary. Do that now. */
13843 static void
13844 finish_destructor_body (void)
13846 tree exprstmt;
13848 /* Any return from a destructor will end up here; that way all base
13849 and member cleanups will be run when the function returns. */
13850 add_stmt (build_stmt (LABEL_STMT, dtor_label));
13852 /* In a virtual destructor, we must call delete. */
13853 if (DECL_VIRTUAL_P (current_function_decl))
13855 tree if_stmt;
13856 tree virtual_size = cxx_sizeof (current_class_type);
13858 /* [class.dtor]
13860 At the point of definition of a virtual destructor (including
13861 an implicit definition), non-placement operator delete shall
13862 be looked up in the scope of the destructor's class and if
13863 found shall be accessible and unambiguous. */
13864 exprstmt = build_op_delete_call
13865 (DELETE_EXPR, current_class_ptr, virtual_size,
13866 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13868 if_stmt = begin_if_stmt ();
13869 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13870 current_in_charge_parm,
13871 integer_one_node),
13872 if_stmt);
13873 finish_expr_stmt (exprstmt);
13874 finish_then_clause (if_stmt);
13875 finish_if_stmt ();
13879 /* Do the necessary processing for the beginning of a function body, which
13880 in this case includes member-initializers, but not the catch clauses of
13881 a function-try-block. Currently, this means opening a binding level
13882 for the member-initializers (in a ctor) and member cleanups (in a dtor).
13883 In other functions, this isn't necessary, but it doesn't hurt. */
13885 tree
13886 begin_function_body (void)
13888 tree stmt;
13890 if (processing_template_decl)
13891 /* Do nothing now. */;
13892 else
13893 /* Always keep the BLOCK node associated with the outermost pair of
13894 curly braces of a function. These are needed for correct
13895 operation of dwarfout.c. */
13896 keep_next_level (1);
13898 stmt = begin_compound_stmt (0);
13899 COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13901 if (processing_template_decl)
13902 /* Do nothing now. */;
13903 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13904 begin_constructor_body ();
13905 else if (DECL_DESTRUCTOR_P (current_function_decl))
13906 begin_destructor_body ();
13908 return stmt;
13911 /* Do the processing for the end of a function body. Currently, this means
13912 closing out the cleanups for fully-constructed bases and members, and in
13913 the case of the destructor, deleting the object if desired. Again, this
13914 is only meaningful for [cd]tors, since they are the only functions where
13915 there is a significant distinction between the main body and any
13916 function catch clauses. Handling, say, main() return semantics here
13917 would be wrong, as flowing off the end of a function catch clause for
13918 main() would also need to return 0. */
13920 void
13921 finish_function_body (tree compstmt)
13923 /* Close the block. */
13924 finish_compound_stmt (0, compstmt);
13926 if (processing_template_decl)
13927 /* Do nothing now. */;
13928 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13929 finish_constructor_body ();
13930 else if (DECL_DESTRUCTOR_P (current_function_decl))
13931 finish_destructor_body ();
13934 /* Finish up a function declaration and compile that function
13935 all the way to assembler language output. The free the storage
13936 for the function definition.
13938 FLAGS is a bitwise or of the following values:
13939 2 - INCLASS_INLINE
13940 We just finished processing the body of an in-class inline
13941 function definition. (This processing will have taken place
13942 after the class definition is complete.) */
13944 tree
13945 finish_function (int flags)
13947 register tree fndecl = current_function_decl;
13948 tree fntype, ctype = NULL_TREE;
13949 int inclass_inline = (flags & 2) != 0;
13950 int nested;
13952 /* When we get some parse errors, we can end up without a
13953 current_function_decl, so cope. */
13954 if (fndecl == NULL_TREE)
13955 return error_mark_node;
13957 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13958 && DECL_VIRTUAL_P (fndecl)
13959 && !processing_template_decl)
13961 tree fnclass = DECL_CONTEXT (fndecl);
13962 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
13963 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
13966 nested = function_depth > 1;
13967 fntype = TREE_TYPE (fndecl);
13969 /* TREE_READONLY (fndecl) = 1;
13970 This caused &foo to be of type ptr-to-const-function
13971 which then got a warning when stored in a ptr-to-function variable. */
13973 my_friendly_assert (building_stmt_tree (), 20000911);
13975 finish_fname_decls ();
13977 /* For a cloned function, we've already got all the code we need;
13978 there's no need to add any extra bits. */
13979 if (!DECL_CLONED_FUNCTION_P (fndecl))
13981 if (DECL_MAIN_P (current_function_decl))
13983 /* Make it so that `main' always returns 0 by default. */
13984 #if VMS_TARGET
13985 finish_return_stmt (integer_one_node);
13986 #else
13987 finish_return_stmt (integer_zero_node);
13988 #endif
13991 /* Finish dealing with exception specifiers. */
13992 if (flag_exceptions && !processing_template_decl
13993 && flag_enforce_eh_specs
13994 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13995 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
13996 (TREE_TYPE (current_function_decl)),
13997 current_eh_spec_block);
14000 /* If we're saving up tree structure, tie off the function now. */
14001 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
14003 /* If this function can't throw any exceptions, remember that. */
14004 if (!processing_template_decl
14005 && !cp_function_chain->can_throw
14006 && !flag_non_call_exceptions)
14007 TREE_NOTHROW (fndecl) = 1;
14009 /* This must come after expand_function_end because cleanups might
14010 have declarations (from inline functions) that need to go into
14011 this function's blocks. */
14013 /* If the current binding level isn't the outermost binding level
14014 for this function, either there is a bug, or we have experienced
14015 syntax errors and the statement tree is malformed. */
14016 if (current_binding_level->parm_flag != 1)
14018 /* Make sure we have already experienced errors. */
14019 if (errorcount == 0)
14020 abort ();
14022 /* Throw away the broken statement tree and extra binding
14023 levels. */
14024 DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14026 while (current_binding_level->parm_flag != 1)
14028 if (current_binding_level->parm_flag == 2)
14029 pop_nested_class ();
14030 else
14031 poplevel (0, 0, 0);
14034 poplevel (1, 0, 1);
14036 /* Set up the named return value optimization, if we can. Here, we
14037 eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14038 for the nrv. genrtl_start_function and declare_return_variable
14039 handle making the nrv and RESULT_DECL share space. */
14040 if (current_function_return_value)
14042 tree r = current_function_return_value;
14043 tree outer;
14045 if (r != error_mark_node
14046 /* This is only worth doing for fns that return in memory--and
14047 simpler, since we don't have to worry about promoted modes. */
14048 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)))
14049 /* Only allow this for variables declared in the outer scope of
14050 the function so we know that their lifetime always ends with a
14051 return; see g++.dg/opt/nrv6.C. We could be more flexible if
14052 we were to do this optimization in tree-ssa. */
14053 /* Skip the artificial function body block. */
14054 && (outer = BLOCK_SUBBLOCKS (BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl))),
14055 chain_member (r, BLOCK_VARS (outer))))
14058 DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14059 walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14060 nullify_returns_r, r);
14062 else
14063 /* Clear it so genrtl_start_function and declare_return_variable
14064 know we're not optimizing. */
14065 current_function_return_value = NULL_TREE;
14068 /* Remember that we were in class scope. */
14069 if (current_class_name)
14070 ctype = current_class_type;
14072 /* Must mark the RESULT_DECL as being in this function. */
14073 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14075 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14076 to the FUNCTION_DECL node itself. */
14077 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14079 /* Save away current state, if appropriate. */
14080 if (!processing_template_decl)
14081 save_function_data (fndecl);
14083 /* If this function calls `setjmp' it cannot be inlined. When
14084 `longjmp' is called it is not guaranteed to restore the value of
14085 local variables that have been modified since the call to
14086 `setjmp'. So, if were to inline this function into some caller
14087 `c', then when we `longjmp', we might not restore all variables
14088 in `c'. (It might seem, at first blush, that there's no way for
14089 this function to modify local variables in `c', but their
14090 addresses may have been stored somewhere accessible to this
14091 function.) */
14092 if (!processing_template_decl && calls_setjmp_p (fndecl))
14093 DECL_UNINLINABLE (fndecl) = 1;
14095 /* Complain if there's just no return statement. */
14096 if (warn_return_type
14097 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14098 && !current_function_returns_value && !current_function_returns_null
14099 /* Don't complain if we abort or throw. */
14100 && !current_function_returns_abnormally
14101 && !DECL_NAME (DECL_RESULT (fndecl))
14102 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
14103 inline function, as we might never be compiled separately. */
14104 && (DECL_INLINE (fndecl) || processing_template_decl))
14105 warning ("no return statement in function returning non-void");
14107 /* Clear out memory we no longer need. */
14108 free_after_parsing (cfun);
14109 /* Since we never call rest_of_compilation, we never clear
14110 CFUN. Do so explicitly. */
14111 free_after_compilation (cfun);
14112 cfun = NULL;
14114 /* If this is an in-class inline definition, we may have to pop the
14115 bindings for the template parameters that we added in
14116 maybe_begin_member_template_processing when start_function was
14117 called. */
14118 if (inclass_inline)
14119 maybe_end_member_template_processing ();
14121 /* Leave the scope of the class. */
14122 if (ctype)
14123 pop_nested_class ();
14125 --function_depth;
14127 /* Clean up. */
14128 if (! nested)
14129 /* Let the error reporting routines know that we're outside a
14130 function. For a nested function, this value is used in
14131 cxx_pop_function_context and then reset via pop_function_context. */
14132 current_function_decl = NULL_TREE;
14134 return fndecl;
14137 /* Create the FUNCTION_DECL for a function definition.
14138 DECLSPECS and DECLARATOR are the parts of the declaration;
14139 they describe the return type and the name of the function,
14140 but twisted together in a fashion that parallels the syntax of C.
14142 This function creates a binding context for the function body
14143 as well as setting up the FUNCTION_DECL in current_function_decl.
14145 Returns a FUNCTION_DECL on success.
14147 If the DECLARATOR is not suitable for a function (it defines a datum
14148 instead), we return 0, which tells yyparse to report a parse error.
14150 May return void_type_node indicating that this method is actually
14151 a friend. See grokfield for more details.
14153 Came here with a `.pushlevel' .
14155 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14156 CHANGES TO CODE IN `grokfield'. */
14158 tree
14159 start_method (tree declspecs, tree declarator, tree attrlist)
14161 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14162 &attrlist);
14164 if (fndecl == error_mark_node)
14165 return error_mark_node;
14167 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14169 error ("invalid member function declaration");
14170 return error_mark_node;
14173 if (attrlist)
14174 cplus_decl_attributes (&fndecl, attrlist, 0);
14176 /* Pass friends other than inline friend functions back. */
14177 if (fndecl == void_type_node)
14178 return fndecl;
14180 if (DECL_IN_AGGR_P (fndecl))
14182 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14184 if (DECL_CONTEXT (fndecl)
14185 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14186 error ("`%D' is already defined in class `%T'", fndecl,
14187 DECL_CONTEXT (fndecl));
14189 return void_type_node;
14192 check_template_shadow (fndecl);
14194 DECL_DECLARED_INLINE_P (fndecl) = 1;
14196 DID_INLINE_FUNC (fndecl) = 0;
14197 if (flag_default_inline)
14198 DECL_INLINE (fndecl) = 1;
14200 /* We process method specializations in finish_struct_1. */
14201 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14202 fndecl = push_template_decl (fndecl);
14204 if (! DECL_FRIEND_P (fndecl))
14206 if (TREE_CHAIN (fndecl))
14208 fndecl = copy_node (fndecl);
14209 TREE_CHAIN (fndecl) = NULL_TREE;
14211 grok_special_member_properties (fndecl);
14214 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14216 /* Make a place for the parms */
14217 pushlevel (0);
14218 current_binding_level->parm_flag = 1;
14220 DECL_IN_AGGR_P (fndecl) = 1;
14221 return fndecl;
14224 /* Go through the motions of finishing a function definition.
14225 We don't compile this method until after the whole class has
14226 been processed.
14228 FINISH_METHOD must return something that looks as though it
14229 came from GROKFIELD (since we are defining a method, after all).
14231 This is called after parsing the body of the function definition.
14232 STMTS is the chain of statements that makes up the function body.
14234 DECL is the ..._DECL that `start_method' provided. */
14236 tree
14237 finish_method (tree decl)
14239 register tree fndecl = decl;
14240 tree old_initial;
14242 register tree link;
14244 if (decl == void_type_node)
14245 return decl;
14247 old_initial = DECL_INITIAL (fndecl);
14249 /* Undo the level for the parms (from start_method).
14250 This is like poplevel, but it causes nothing to be
14251 saved. Saving information here confuses symbol-table
14252 output routines. Besides, this information will
14253 be correctly output when this method is actually
14254 compiled. */
14256 /* Clear out the meanings of the local variables of this level;
14257 also record in each decl which block it belongs to. */
14259 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14261 if (DECL_NAME (link) != NULL_TREE)
14262 pop_binding (DECL_NAME (link), link);
14263 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14264 DECL_CONTEXT (link) = NULL_TREE;
14267 poplevel (0, 0, 0);
14269 DECL_INITIAL (fndecl) = old_initial;
14271 /* We used to check if the context of FNDECL was different from
14272 current_class_type as another way to get inside here. This didn't work
14273 for String.cc in libg++. */
14274 if (DECL_FRIEND_P (fndecl))
14276 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14277 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14278 decl = void_type_node;
14281 return decl;
14285 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14286 we can lay it out later, when and if its type becomes complete. */
14288 void
14289 maybe_register_incomplete_var (tree var)
14291 my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14293 /* Keep track of variables with incomplete types. */
14294 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14295 && DECL_EXTERNAL (var))
14297 tree inner_type = TREE_TYPE (var);
14299 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14300 inner_type = TREE_TYPE (inner_type);
14301 inner_type = TYPE_MAIN_VARIANT (inner_type);
14303 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14304 /* RTTI TD entries are created while defining the type_info. */
14305 || (TYPE_LANG_SPECIFIC (inner_type)
14306 && TYPE_BEING_DEFINED (inner_type)))
14307 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14311 /* Called when a class type (given by TYPE) is defined. If there are
14312 any existing VAR_DECLs whose type hsa been completed by this
14313 declaration, update them now. */
14315 void
14316 complete_vars (tree type)
14318 tree *list = &incomplete_vars;
14320 my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14321 while (*list)
14323 if (same_type_p (type, TREE_PURPOSE (*list)))
14325 tree var = TREE_VALUE (*list);
14326 /* Complete the type of the variable. The VAR_DECL itself
14327 will be laid out in expand_expr. */
14328 complete_type (TREE_TYPE (var));
14329 /* Remove this entry from the list. */
14330 *list = TREE_CHAIN (*list);
14332 else
14333 list = &TREE_CHAIN (*list);
14337 /* If DECL is of a type which needs a cleanup, build that cleanup
14338 here. */
14340 tree
14341 cxx_maybe_build_cleanup (tree decl)
14343 tree type = TREE_TYPE (decl);
14345 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14347 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14348 tree rval;
14350 if (TREE_CODE (type) == ARRAY_TYPE)
14351 rval = decl;
14352 else
14354 cxx_mark_addressable (decl);
14355 rval = build_unary_op (ADDR_EXPR, decl, 0);
14358 /* Optimize for space over speed here. */
14359 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14360 || flag_expensive_optimizations)
14361 flags |= LOOKUP_NONVIRTUAL;
14363 rval = build_delete (TREE_TYPE (rval), rval,
14364 sfk_complete_destructor, flags, 0);
14366 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14367 && ! TYPE_HAS_DESTRUCTOR (type))
14368 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
14369 build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
14371 return rval;
14373 return NULL_TREE;
14376 /* When a stmt has been parsed, this function is called. */
14378 void
14379 finish_stmt (void)
14381 /* Always assume this statement was not an expression statement. If
14382 it actually was an expression statement, its our callers
14383 responsibility to fix this up. */
14384 last_expr_type = NULL_TREE;
14387 /* DECL was originally constructed as a non-static member function,
14388 but turned out to be static. Update it accordingly. */
14390 void
14391 revert_static_member_fn (tree decl)
14393 tree tmp;
14394 tree function = TREE_TYPE (decl);
14395 tree args = TYPE_ARG_TYPES (function);
14397 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14398 != TYPE_UNQUALIFIED)
14399 error ("static member function `%#D' declared with type qualifiers",
14400 decl);
14402 args = TREE_CHAIN (args);
14403 tmp = build_function_type (TREE_TYPE (function), args);
14404 tmp = build_qualified_type (tmp, cp_type_quals (function));
14405 tmp = build_exception_variant (tmp,
14406 TYPE_RAISES_EXCEPTIONS (function));
14407 TREE_TYPE (decl) = tmp;
14408 if (DECL_ARGUMENTS (decl))
14409 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14410 DECL_STATIC_FUNCTION_P (decl) = 1;
14413 /* Initialize the variables used during compilation of a C++
14414 function. */
14416 void
14417 cxx_push_function_context (struct function * f)
14419 struct language_function *p
14420 = ((struct language_function *)
14421 ggc_alloc_cleared (sizeof (struct language_function)));
14422 f->language = p;
14424 /* It takes an explicit call to expand_body to generate RTL for a
14425 function. */
14426 expanding_p = 0;
14428 /* Whenever we start a new function, we destroy temporaries in the
14429 usual way. */
14430 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14433 /* Free the language-specific parts of F, now that we've finished
14434 compiling the function. */
14436 void
14437 cxx_pop_function_context (struct function * f)
14439 f->language = 0;
14442 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14443 one of the language-independent trees. */
14445 enum cp_tree_node_structure_enum
14446 cp_tree_node_structure (union lang_tree_node * t)
14448 switch (TREE_CODE (&t->generic))
14450 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
14451 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
14452 case OVERLOAD: return TS_CP_OVERLOAD;
14453 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14454 case PTRMEM_CST: return TS_CP_PTRMEM;
14455 case BASELINK: return TS_CP_BASELINK;
14456 case WRAPPER: return TS_CP_WRAPPER;
14457 default: return TS_CP_GENERIC;
14461 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14462 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
14464 tree
14465 identifier_global_value (tree t)
14467 return IDENTIFIER_GLOBAL_VALUE (t);
14470 /* Build the void_list_node (void_type_node having been created). */
14471 tree
14472 build_void_list_node (void)
14474 tree t = build_tree_list (NULL_TREE, void_type_node);
14475 TREE_PARMLIST (t) = 1;
14476 return t;
14479 static int
14480 cp_missing_noreturn_ok_p (tree decl)
14482 /* A missing noreturn is ok for the `main' function. */
14483 return DECL_MAIN_P (decl);
14486 #include "gt-cp-decl.h"
14487 #include "gtype-cp.h"