* config/arm/elf.h (ASM_OUTPUT_ALIGNED_COMMON): Remove definition.
[official-gcc.git] / gcc / cp / decl.c
blob6fe39c831335436088ec31424e64b888b6f92bd6
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 char *, int);
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 const char *filename_o_goto;
219 int lineno_o_goto;
220 struct named_label_use_list *next;
223 #define named_label_uses cp_function_chain->x_named_label_uses
225 #define local_names cp_function_chain->x_local_names
227 /* A list of objects which have constructors or destructors
228 which reside in the global scope. The decl is stored in
229 the TREE_VALUE slot and the initializer is stored
230 in the TREE_PURPOSE slot. */
231 tree static_aggregates;
233 /* -- end of C++ */
235 /* A node for the integer constants 2, and 3. */
237 tree integer_two_node, integer_three_node;
239 /* Similar, for last_function_parm_tags. */
240 tree last_function_parms;
242 /* A list of all LABEL_DECLs in the function that have names. Here so
243 we can clear out their names' definitions at the end of the
244 function, and so we can check the validity of jumps to these labels. */
246 struct named_label_list GTY(())
248 struct cp_binding_level *binding_level;
249 tree names_in_scope;
250 tree old_value;
251 tree label_decl;
252 tree bad_decls;
253 struct named_label_list *next;
254 unsigned int in_try_scope : 1;
255 unsigned int in_catch_scope : 1;
258 #define named_labels cp_function_chain->x_named_labels
260 /* The name of the anonymous namespace, throughout this translation
261 unit. */
262 tree anonymous_namespace_name;
264 /* The number of function bodies which we are currently processing.
265 (Zero if we are at namespace scope, one inside the body of a
266 function, two inside the body of a function in a local class, etc.) */
267 int function_depth;
269 /* States indicating how grokdeclarator() should handle declspecs marked
270 with __attribute__((deprecated)). An object declared as
271 __attribute__((deprecated)) suppresses warnings of uses of other
272 deprecated items. */
274 enum deprecated_states {
275 DEPRECATED_NORMAL,
276 DEPRECATED_SUPPRESS
279 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
281 /* Set by add_implicitly_declared_members() to keep those members from
282 being flagged as deprecated or reported as using deprecated
283 types. */
284 int adding_implicit_members = 0;
286 /* True if a declaration with an `extern' linkage specifier is being
287 processed. */
288 bool have_extern_spec;
291 /* For each binding contour we allocate a binding_level structure
292 which records the names defined in that contour.
293 Contours include:
294 0) the global one
295 1) one for each function definition,
296 where internal declarations of the parameters appear.
297 2) one for each compound statement,
298 to record its declarations.
300 The current meaning of a name can be found by searching the levels
301 from the current one out to the global one.
303 Off to the side, may be the class_binding_level. This exists only
304 to catch class-local declarations. It is otherwise nonexistent.
306 Also there may be binding levels that catch cleanups that must be
307 run when exceptions occur. Thus, to see whether a name is bound in
308 the current scope, it is not enough to look in the
309 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
310 instead. */
312 /* Note that the information in the `names' component of the global contour
313 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
315 struct cp_binding_level GTY(())
317 /* A chain of _DECL nodes for all variables, constants, functions,
318 and typedef types. These are in the reverse of the order
319 supplied. There may be OVERLOADs on this list, too, but they
320 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
321 tree names;
323 /* Count of elements in names chain. */
324 size_t names_size;
326 /* A chain of NAMESPACE_DECL nodes. */
327 tree namespaces;
329 /* An array of static functions and variables (for namespaces only) */
330 varray_type static_decls;
332 /* A chain of VTABLE_DECL nodes. */
333 tree vtables;
335 /* A list of structure, union and enum definitions, for looking up
336 tag names.
337 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
338 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
339 or ENUMERAL_TYPE node.
341 C++: the TREE_VALUE nodes can be simple types for
342 component_bindings. */
343 tree tags;
345 /* A list of USING_DECL nodes. */
346 tree usings;
348 /* A list of used namespaces. PURPOSE is the namespace,
349 VALUE the common ancestor with this binding_level's namespace. */
350 tree using_directives;
352 /* If this binding level is the binding level for a class, then
353 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
354 is the name of an entity bound in the class. The TREE_TYPE is
355 the DECL bound by this name in the class. */
356 tree class_shadowed;
358 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
359 is used for all binding levels. In addition the TREE_VALUE is the
360 IDENTIFIER_TYPE_VALUE before we entered the class. */
361 tree type_shadowed;
363 /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local
364 label in this scope. The TREE_PURPOSE is the previous value of
365 the IDENTIFIER_LABEL VALUE. */
366 tree shadowed_labels;
368 /* For each level (except not the global one),
369 a chain of BLOCK nodes for all the levels
370 that were entered and exited one level down. */
371 tree blocks;
373 /* The _TYPE node for this level, if parm_flag == 2. */
374 tree this_class;
376 /* The binding level which this one is contained in (inherits from). */
377 struct cp_binding_level *level_chain;
379 /* List of VAR_DECLS saved from a previous for statement.
380 These would be dead in ISO-conforming code, but might
381 be referenced in ARM-era code. These are stored in a
382 TREE_LIST; the TREE_VALUE is the actual declaration. */
383 tree dead_vars_from_for;
385 /* 1 for the level that holds the parameters of a function.
386 2 for the level that holds a class declaration. */
387 unsigned parm_flag : 2;
389 /* 1 means make a BLOCK for this level regardless of all else.
390 2 for temporary binding contours created by the compiler. */
391 unsigned keep : 2;
393 /* Nonzero if this level "doesn't exist" for tags. */
394 unsigned tag_transparent : 1;
396 /* Nonzero if this level can safely have additional
397 cleanup-needing variables added to it. */
398 unsigned more_cleanups_ok : 1;
399 unsigned have_cleanups : 1;
401 /* Nonzero if this scope is for storing the decls for template
402 parameters and generic decls; these decls will be discarded and
403 replaced with a TEMPLATE_DECL. */
404 unsigned template_parms_p : 1;
406 /* Nonzero if this scope corresponds to the `<>' in a
407 `template <>' clause. Whenever this flag is set,
408 TEMPLATE_PARMS_P will be set as well. */
409 unsigned template_spec_p : 1;
411 /* This is set for a namespace binding level. */
412 unsigned namespace_p : 1;
414 /* True if this level is that of a for-statement where we need to
415 worry about ambiguous (ARM or ISO) scope rules. */
416 unsigned is_for_scope : 1;
418 /* True if this level corresponds to a TRY block. Currently this
419 information is only available while building the tree structure. */
420 unsigned is_try_scope : 1;
422 /* True if this level corresponds to a CATCH block. Currently this
423 information is only available while building the tree structure. */
424 unsigned is_catch_scope : 1;
426 /* Three bits left for this word. */
428 /* Binding depth at which this level began. */
429 unsigned binding_depth;
432 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
434 /* The binding level currently in effect. */
436 #define current_binding_level \
437 (*(cfun && cp_function_chain->bindings \
438 ? &cp_function_chain->bindings \
439 : &scope_chain->bindings))
441 /* The binding level of the current class, if any. */
443 #define class_binding_level scope_chain->class_bindings
445 /* A chain of binding_level structures awaiting reuse. */
447 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
449 /* The outermost binding level, for names of file scope.
450 This is created when the compiler is started and exists
451 through the entire run. */
453 static GTY(()) struct cp_binding_level *global_binding_level;
455 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
457 static int keep_next_level_flag;
459 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
460 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
461 time the VAR_DECL was declared, the type was incomplete. */
463 static GTY(()) tree incomplete_vars;
465 #if defined(DEBUG_BINDING_LEVELS)
466 static int binding_depth = 0;
467 static int is_class_level = 0;
469 static void
470 indent (void)
472 register unsigned i;
474 for (i = 0; i < binding_depth*2; i++)
475 putc (' ', stderr);
477 #endif /* defined(DEBUG_BINDING_LEVELS) */
479 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
481 static void
482 push_binding_level (struct cp_binding_level *newlevel,
483 int tag_transparent,
484 int keep)
486 /* Add this level to the front of the chain (stack) of levels that
487 are active. */
488 memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
489 newlevel->level_chain = current_binding_level;
490 current_binding_level = newlevel;
491 newlevel->tag_transparent = tag_transparent;
492 newlevel->more_cleanups_ok = 1;
494 newlevel->keep = keep;
495 #if defined(DEBUG_BINDING_LEVELS)
496 newlevel->binding_depth = binding_depth;
497 indent ();
498 fprintf (stderr, "push %s level 0x%08x line %d\n",
499 (is_class_level) ? "class" : "block", newlevel, lineno);
500 is_class_level = 0;
501 binding_depth++;
502 #endif /* defined(DEBUG_BINDING_LEVELS) */
505 /* Find the innermost enclosing class scope, and reset
506 CLASS_BINDING_LEVEL appropriately. */
508 static void
509 find_class_binding_level (void)
511 struct cp_binding_level *level = current_binding_level;
513 while (level && level->parm_flag != 2)
514 level = level->level_chain;
515 if (level && level->parm_flag == 2)
516 class_binding_level = level;
517 else
518 class_binding_level = 0;
521 static void
522 pop_binding_level (void)
524 if (global_binding_level)
526 /* Cannot pop a level, if there are none left to pop. */
527 if (current_binding_level == global_binding_level)
528 abort ();
530 /* Pop the current level, and free the structure for reuse. */
531 #if defined(DEBUG_BINDING_LEVELS)
532 binding_depth--;
533 indent ();
534 fprintf (stderr, "pop %s level 0x%08x line %d\n",
535 (is_class_level) ? "class" : "block",
536 current_binding_level, lineno);
537 if (is_class_level != (current_binding_level == class_binding_level))
539 indent ();
540 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
542 is_class_level = 0;
543 #endif /* defined(DEBUG_BINDING_LEVELS) */
545 register struct cp_binding_level *level = current_binding_level;
546 current_binding_level = current_binding_level->level_chain;
547 level->level_chain = free_binding_level;
548 #if 0 /* defined(DEBUG_BINDING_LEVELS) */
549 if (level->binding_depth != binding_depth)
550 abort ();
551 #endif /* defined(DEBUG_BINDING_LEVELS) */
552 free_binding_level = level;
553 find_class_binding_level ();
557 static void
558 suspend_binding_level (void)
560 if (class_binding_level)
561 current_binding_level = class_binding_level;
563 if (global_binding_level)
565 /* Cannot suspend a level, if there are none left to suspend. */
566 if (current_binding_level == global_binding_level)
567 abort ();
569 /* Suspend the current level. */
570 #if defined(DEBUG_BINDING_LEVELS)
571 binding_depth--;
572 indent ();
573 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
574 (is_class_level) ? "class" : "block",
575 current_binding_level, lineno);
576 if (is_class_level != (current_binding_level == class_binding_level))
578 indent ();
579 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
581 is_class_level = 0;
582 #endif /* defined(DEBUG_BINDING_LEVELS) */
583 current_binding_level = current_binding_level->level_chain;
584 find_class_binding_level ();
587 static void
588 resume_binding_level (struct cp_binding_level* b)
590 /* Resuming binding levels is meant only for namespaces,
591 and those cannot nest into classes. */
592 my_friendly_assert(!class_binding_level, 386);
593 /* Also, resuming a non-directly nested namespace is a no-no. */
594 my_friendly_assert(b->level_chain == current_binding_level, 386);
595 current_binding_level = b;
596 #if defined(DEBUG_BINDING_LEVELS)
597 b->binding_depth = binding_depth;
598 indent ();
599 fprintf (stderr, "resume %s level 0x%08x line %d\n",
600 (is_class_level) ? "class" : "block", b, lineno);
601 is_class_level = 0;
602 binding_depth++;
603 #endif /* defined(DEBUG_BINDING_LEVELS) */
606 /* Create a new `struct cp_binding_level'. */
608 static
609 struct cp_binding_level *
610 make_binding_level (void)
612 /* NOSTRICT */
613 return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
616 /* Nonzero if we are currently in the global binding level. */
619 global_bindings_p (void)
621 return current_binding_level == global_binding_level;
624 /* Return the innermost binding level that is not for a class scope. */
626 static struct cp_binding_level *
627 innermost_nonclass_level (void)
629 struct cp_binding_level *b;
631 b = current_binding_level;
632 while (b->parm_flag == 2)
633 b = b->level_chain;
635 return b;
638 /* Nonzero if we are currently in a toplevel binding level. This
639 means either the global binding level or a namespace in a toplevel
640 binding level. Since there are no non-toplevel namespace levels,
641 this really means any namespace or template parameter level. We
642 also include a class whose context is toplevel. */
645 toplevel_bindings_p (void)
647 struct cp_binding_level *b = innermost_nonclass_level ();
649 return b->namespace_p || b->template_parms_p;
652 /* Nonzero if this is a namespace scope, or if we are defining a class
653 which is itself at namespace scope, or whose enclosing class is
654 such a class, etc. */
657 namespace_bindings_p (void)
659 struct cp_binding_level *b = innermost_nonclass_level ();
661 return b->namespace_p;
664 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
665 unconditionally. Otherwise, use the normal logic to decide whether
666 or not to create a BLOCK. */
668 void
669 keep_next_level (int keep)
671 keep_next_level_flag = keep;
674 /* Nonzero if the current level needs to have a BLOCK made. */
677 kept_level_p (void)
679 return (current_binding_level->blocks != NULL_TREE
680 || current_binding_level->keep
681 || current_binding_level->names != NULL_TREE
682 || (current_binding_level->tags != NULL_TREE
683 && !current_binding_level->tag_transparent));
686 static void
687 declare_namespace_level (void)
689 current_binding_level->namespace_p = 1;
692 /* Returns nonzero if this scope was created to store template
693 parameters. */
696 template_parm_scope_p (void)
698 return current_binding_level->template_parms_p;
701 /* Returns the kind of template specialization we are currently
702 processing, given that it's declaration contained N_CLASS_SCOPES
703 explicit scope qualifications. */
705 tmpl_spec_kind
706 current_tmpl_spec_kind (int n_class_scopes)
708 int n_template_parm_scopes = 0;
709 int seen_specialization_p = 0;
710 int innermost_specialization_p = 0;
711 struct cp_binding_level *b;
713 /* Scan through the template parameter scopes. */
714 for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
716 /* If we see a specialization scope inside a parameter scope,
717 then something is wrong. That corresponds to a declaration
718 like:
720 template <class T> template <> ...
722 which is always invalid since [temp.expl.spec] forbids the
723 specialization of a class member template if the enclosing
724 class templates are not explicitly specialized as well. */
725 if (b->template_spec_p)
727 if (n_template_parm_scopes == 0)
728 innermost_specialization_p = 1;
729 else
730 seen_specialization_p = 1;
732 else if (seen_specialization_p == 1)
733 return tsk_invalid_member_spec;
735 ++n_template_parm_scopes;
738 /* Handle explicit instantiations. */
739 if (processing_explicit_instantiation)
741 if (n_template_parm_scopes != 0)
742 /* We've seen a template parameter list during an explicit
743 instantiation. For example:
745 template <class T> template void f(int);
747 This is erroneous. */
748 return tsk_invalid_expl_inst;
749 else
750 return tsk_expl_inst;
753 if (n_template_parm_scopes < n_class_scopes)
754 /* We've not seen enough template headers to match all the
755 specialized classes present. For example:
757 template <class T> void R<T>::S<T>::f(int);
759 This is invalid; there needs to be one set of template
760 parameters for each class. */
761 return tsk_insufficient_parms;
762 else if (n_template_parm_scopes == n_class_scopes)
763 /* We're processing a non-template declaration (even though it may
764 be a member of a template class.) For example:
766 template <class T> void S<T>::f(int);
768 The `class T' maches the `S<T>', leaving no template headers
769 corresponding to the `f'. */
770 return tsk_none;
771 else if (n_template_parm_scopes > n_class_scopes + 1)
772 /* We've got too many template headers. For example:
774 template <> template <class T> void f (T);
776 There need to be more enclosing classes. */
777 return tsk_excessive_parms;
778 else
779 /* This must be a template. It's of the form:
781 template <class T> template <class U> void S<T>::f(U);
783 This is a specialization if the innermost level was a
784 specialization; otherwise it's just a definition of the
785 template. */
786 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
789 void
790 set_class_shadows (tree shadows)
792 class_binding_level->class_shadowed = shadows;
795 /* Enter a new binding level.
796 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
797 not for that of tags. */
799 void
800 pushlevel (int tag_transparent)
802 struct cp_binding_level *newlevel;
804 if (cfun && !doing_semantic_analysis_p ())
805 return;
807 /* Reuse or create a struct for this binding level. */
808 #if defined(DEBUG_BINDING_LEVELS)
809 if (0)
810 #else /* !defined(DEBUG_BINDING_LEVELS) */
811 if (free_binding_level)
812 #endif /* !defined(DEBUG_BINDING_LEVELS) */
814 newlevel = free_binding_level;
815 free_binding_level = free_binding_level->level_chain;
817 else
818 newlevel = make_binding_level ();
820 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
821 keep_next_level_flag = 0;
824 /* We're defining an object of type TYPE. If it needs a cleanup, but
825 we're not allowed to add any more objects with cleanups to the current
826 scope, create a new binding level. */
828 void
829 maybe_push_cleanup_level (tree type)
831 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
832 && current_binding_level->more_cleanups_ok == 0)
834 keep_next_level (2);
835 pushlevel (1);
836 clear_last_expr ();
837 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
841 /* Enter a new scope. The KIND indicates what kind of scope is being
842 created. */
844 void
845 begin_scope (scope_kind sk)
847 pushlevel (0);
849 switch (sk)
851 case sk_block:
852 break;
854 case sk_try:
855 current_binding_level->is_try_scope = 1;
856 break;
858 case sk_catch:
859 current_binding_level->is_catch_scope = 1;
860 break;
862 case sk_for:
863 current_binding_level->is_for_scope = 1;
864 break;
866 case sk_template_spec:
867 current_binding_level->template_spec_p = 1;
868 /* Fall through. */
870 case sk_template_parms:
871 current_binding_level->template_parms_p = 1;
872 break;
874 default:
875 abort ();
879 /* Exit the current scope. */
881 void
882 finish_scope (void)
884 poplevel (0, 0, 0);
887 /* Make DECL the innermost binding for ID. The LEVEL is the binding
888 level at which this declaration is being bound. */
890 static void
891 push_binding (tree id, tree decl, cxx_scope* level)
893 cxx_binding *binding = cxx_binding_make (decl, NULL);
895 /* Now, fill in the binding information. */
896 binding->previous = IDENTIFIER_BINDING (id);
897 BINDING_SCOPE (binding) = level;
898 INHERITED_VALUE_BINDING_P (binding) = 0;
899 LOCAL_BINDING_P (binding) = (level != class_binding_level);
901 /* And put it on the front of the list of bindings for ID. */
902 IDENTIFIER_BINDING (id) = binding;
905 /* ID is already bound in the current scope. But, DECL is an
906 additional binding for ID in the same scope. This is the `struct
907 stat' hack whereby a non-typedef class-name or enum-name can be
908 bound at the same level as some other kind of entity. It's the
909 responsibility of the caller to check that inserting this name is
910 valid here. Returns nonzero if the new binding was successful. */
911 static int
912 add_binding (tree id, tree decl)
914 cxx_binding *binding = IDENTIFIER_BINDING (id);
915 int ok = 1;
917 timevar_push (TV_NAME_LOOKUP);
918 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
919 /* The new name is the type name. */
920 BINDING_TYPE (binding) = decl;
921 else if (!BINDING_VALUE (binding))
922 /* This situation arises when push_class_level_binding moves an
923 inherited type-binding out of the way to make room for a new
924 value binding. */
925 BINDING_VALUE (binding) = decl;
926 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
927 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
929 /* The old binding was a type name. It was placed in
930 BINDING_VALUE because it was thought, at the point it was
931 declared, to be the only entity with such a name. Move the
932 type name into the type slot; it is now hidden by the new
933 binding. */
934 BINDING_TYPE (binding) = BINDING_VALUE (binding);
935 BINDING_VALUE (binding) = decl;
936 INHERITED_VALUE_BINDING_P (binding) = 0;
938 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
939 && TREE_CODE (decl) == TYPE_DECL
940 && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
941 && (same_type_p (TREE_TYPE (decl),
942 TREE_TYPE (BINDING_VALUE (binding)))
943 /* If either type involves template parameters, we must
944 wait until instantiation. */
945 || uses_template_parms (TREE_TYPE (decl))
946 || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
947 /* We have two typedef-names, both naming the same type to have
948 the same name. This is OK because of:
950 [dcl.typedef]
952 In a given scope, a typedef specifier can be used to redefine
953 the name of any type declared in that scope to refer to the
954 type to which it already refers. */
955 ok = 0;
956 /* There can be two block-scope declarations of the same variable,
957 so long as they are `extern' declarations. */
958 else if (TREE_CODE (decl) == VAR_DECL
959 && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
960 && DECL_EXTERNAL (decl)
961 && DECL_EXTERNAL (BINDING_VALUE (binding)))
963 duplicate_decls (decl, BINDING_VALUE (binding));
964 ok = 0;
966 else
968 error ("declaration of `%#D'", decl);
969 cp_error_at ("conflicts with previous declaration `%#D'",
970 BINDING_VALUE (binding));
971 ok = 0;
974 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
977 /* Add DECL to the list of things declared in B. */
979 static void
980 add_decl_to_level (tree decl,
981 struct cp_binding_level* b)
983 if (TREE_CODE (decl) == NAMESPACE_DECL
984 && !DECL_NAMESPACE_ALIAS (decl))
986 TREE_CHAIN (decl) = b->namespaces;
987 b->namespaces = decl;
989 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
991 TREE_CHAIN (decl) = b->vtables;
992 b->vtables = decl;
994 else
996 /* We build up the list in reverse order, and reverse it later if
997 necessary. */
998 TREE_CHAIN (decl) = b->names;
999 b->names = decl;
1000 b->names_size++;
1002 /* If appropriate, add decl to separate list of statics */
1003 if (b->namespace_p)
1004 if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
1005 || (TREE_CODE (decl) == FUNCTION_DECL
1006 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
1007 VARRAY_PUSH_TREE (b->static_decls, decl);
1011 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1012 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1013 doesn't really belong to this binding level, that it got here
1014 through a using-declaration. */
1016 void
1017 push_local_binding (tree id, tree decl, int flags)
1019 struct cp_binding_level *b;
1021 /* Skip over any local classes. This makes sense if we call
1022 push_local_binding with a friend decl of a local class. */
1023 b = current_binding_level;
1024 while (b->parm_flag == 2)
1025 b = b->level_chain;
1027 if (lookup_name_current_level (id))
1029 /* Supplement the existing binding. */
1030 if (!add_binding (id, decl))
1031 /* It didn't work. Something else must be bound at this
1032 level. Do not add DECL to the list of things to pop
1033 later. */
1034 return;
1036 else
1037 /* Create a new binding. */
1038 push_binding (id, decl, b);
1040 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1041 /* We must put the OVERLOAD into a TREE_LIST since the
1042 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1043 decls that got here through a using-declaration. */
1044 decl = build_tree_list (NULL_TREE, decl);
1046 /* And put DECL on the list of things declared by the current
1047 binding level. */
1048 add_decl_to_level (decl, b);
1051 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1052 binding was successful. */
1055 push_class_binding (tree id, tree decl)
1057 int result = 1;
1058 cxx_binding *binding = IDENTIFIER_BINDING (id);
1059 tree context;
1061 timevar_push (TV_NAME_LOOKUP);
1062 /* Note that we declared this value so that we can issue an error if
1063 this is an invalid redeclaration of a name already used for some
1064 other purpose. */
1065 note_name_declared_in_class (id, decl);
1067 if (binding && BINDING_SCOPE (binding) == class_binding_level)
1068 /* Supplement the existing binding. */
1069 result = add_binding (id, decl);
1070 else
1071 /* Create a new binding. */
1072 push_binding (id, decl, class_binding_level);
1074 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1075 class-level declaration. Note that we do not use DECL here
1076 because of the possibility of the `struct stat' hack; if DECL is
1077 a class-name or enum-name we might prefer a field-name, or some
1078 such. */
1079 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1081 /* If this is a binding from a base class, mark it as such. */
1082 binding = IDENTIFIER_BINDING (id);
1083 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1085 if (TREE_CODE (decl) == OVERLOAD)
1086 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1087 else
1089 my_friendly_assert (DECL_P (decl), 0);
1090 context = context_for_name_lookup (decl);
1093 if (is_properly_derived_from (current_class_type, context))
1094 INHERITED_VALUE_BINDING_P (binding) = 1;
1095 else
1096 INHERITED_VALUE_BINDING_P (binding) = 0;
1098 else if (BINDING_VALUE (binding) == decl)
1099 /* We only encounter a TREE_LIST when push_class_decls detects an
1100 ambiguity. Such an ambiguity can be overridden by a definition
1101 in this class. */
1102 INHERITED_VALUE_BINDING_P (binding) = 1;
1104 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1107 /* Remove the binding for DECL which should be the innermost binding
1108 for ID. */
1110 static void
1111 pop_binding (tree id, tree decl)
1113 cxx_binding *binding;
1115 if (id == NULL_TREE)
1116 /* It's easiest to write the loops that call this function without
1117 checking whether or not the entities involved have names. We
1118 get here for such an entity. */
1119 return;
1121 /* Get the innermost binding for ID. */
1122 binding = IDENTIFIER_BINDING (id);
1124 /* The name should be bound. */
1125 my_friendly_assert (binding != NULL, 0);
1127 /* The DECL will be either the ordinary binding or the type
1128 binding for this identifier. Remove that binding. */
1129 if (BINDING_VALUE (binding) == decl)
1130 BINDING_VALUE (binding) = NULL_TREE;
1131 else if (BINDING_TYPE (binding) == decl)
1132 BINDING_TYPE (binding) = NULL_TREE;
1133 else
1134 abort ();
1136 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1138 /* We're completely done with the innermost binding for this
1139 identifier. Unhook it from the list of bindings. */
1140 IDENTIFIER_BINDING (id) = binding->previous;
1142 /* Add it to the free list. */
1143 cxx_binding_free (binding);
1145 /* Clear the BINDING_SCOPE so the garbage collector doesn't walk
1146 it. */
1147 BINDING_SCOPE (binding) = NULL;
1151 /* When a label goes out of scope, check to see if that label was used
1152 in a valid manner, and issue any appropriate warnings or errors. */
1154 static void
1155 pop_label (tree label, tree old_value)
1157 if (!processing_template_decl && doing_semantic_analysis_p ())
1159 if (DECL_INITIAL (label) == NULL_TREE)
1161 cp_error_at ("label `%D' used but not defined", label);
1162 /* Avoid crashing later. */
1163 define_label (input_filename, 1, DECL_NAME (label));
1165 else if (warn_unused_label && !TREE_USED (label))
1166 cp_warning_at ("label `%D' defined but not used", label);
1169 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1172 /* At the end of a function, all labels declared within the function
1173 go out of scope. BLOCK is the top-level block for the
1174 function. */
1176 static void
1177 pop_labels (tree block)
1179 struct named_label_list *link;
1181 /* Clear out the definitions of all label names, since their scopes
1182 end here. */
1183 for (link = named_labels; link; link = link->next)
1185 pop_label (link->label_decl, link->old_value);
1186 /* Put the labels into the "variables" of the top-level block,
1187 so debugger can see them. */
1188 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1189 BLOCK_VARS (block) = link->label_decl;
1192 named_labels = NULL;
1195 /* Exit a binding level.
1196 Pop the level off, and restore the state of the identifier-decl mappings
1197 that were in effect when this level was entered.
1199 If KEEP == 1, this level had explicit declarations, so
1200 and create a "block" (a BLOCK node) for the level
1201 to record its declarations and subblocks for symbol table output.
1203 If FUNCTIONBODY is nonzero, this level is the body of a function,
1204 so create a block as if KEEP were set and also clear out all
1205 label names.
1207 If REVERSE is nonzero, reverse the order of decls before putting
1208 them into the BLOCK. */
1210 tree
1211 poplevel (int keep, int reverse, int functionbody)
1213 register tree link;
1214 /* The chain of decls was accumulated in reverse order.
1215 Put it into forward order, just for cleanliness. */
1216 tree decls;
1217 int tmp = functionbody;
1218 int real_functionbody;
1219 tree tags;
1220 tree subblocks;
1221 tree block = NULL_TREE;
1222 tree decl;
1223 int leaving_for_scope;
1225 timevar_push (TV_NAME_LOOKUP);
1226 if (cfun && !doing_semantic_analysis_p ())
1227 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1229 my_friendly_assert (current_binding_level->parm_flag != 2,
1230 19990916);
1232 real_functionbody = (current_binding_level->keep == 2
1233 ? ((functionbody = 0), tmp) : functionbody);
1234 tags = functionbody >= 0 ? current_binding_level->tags : 0;
1235 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1237 my_friendly_assert (!current_binding_level->class_shadowed,
1238 19990414);
1240 /* We used to use KEEP == 2 to indicate that the new block should go
1241 at the beginning of the list of blocks at this binding level,
1242 rather than the end. This hack is no longer used. */
1243 my_friendly_assert (keep == 0 || keep == 1, 0);
1245 if (current_binding_level->keep == 1)
1246 keep = 1;
1248 /* Any uses of undefined labels, and any defined labels, now operate
1249 under constraints of next binding contour. */
1250 if (cfun && !functionbody)
1252 struct cp_binding_level *level_chain;
1253 level_chain = current_binding_level->level_chain;
1254 if (level_chain)
1256 struct named_label_use_list *uses;
1257 struct named_label_list *labels;
1258 for (labels = named_labels; labels; labels = labels->next)
1259 if (labels->binding_level == current_binding_level)
1261 tree decl;
1262 if (current_binding_level->is_try_scope)
1263 labels->in_try_scope = 1;
1264 if (current_binding_level->is_catch_scope)
1265 labels->in_catch_scope = 1;
1266 for (decl = labels->names_in_scope; decl;
1267 decl = TREE_CHAIN (decl))
1268 if (decl_jump_unsafe (decl))
1269 labels->bad_decls = tree_cons (NULL_TREE, decl,
1270 labels->bad_decls);
1271 labels->binding_level = level_chain;
1272 labels->names_in_scope = level_chain->names;
1275 for (uses = named_label_uses; uses; uses = uses->next)
1276 if (uses->binding_level == current_binding_level)
1278 uses->binding_level = level_chain;
1279 uses->names_in_scope = level_chain->names;
1284 /* Get the decls in the order they were written.
1285 Usually current_binding_level->names is in reverse order.
1286 But parameter decls were previously put in forward order. */
1288 if (reverse)
1289 current_binding_level->names
1290 = decls = nreverse (current_binding_level->names);
1291 else
1292 decls = current_binding_level->names;
1294 /* Output any nested inline functions within this block
1295 if they weren't already output. */
1296 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1297 if (TREE_CODE (decl) == FUNCTION_DECL
1298 && ! TREE_ASM_WRITTEN (decl)
1299 && DECL_INITIAL (decl) != NULL_TREE
1300 && TREE_ADDRESSABLE (decl)
1301 && decl_function_context (decl) == current_function_decl)
1303 /* If this decl was copied from a file-scope decl
1304 on account of a block-scope extern decl,
1305 propagate TREE_ADDRESSABLE to the file-scope decl. */
1306 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1307 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1308 else
1310 push_function_context ();
1311 output_inline_function (decl);
1312 pop_function_context ();
1316 /* When not in function-at-a-time mode, expand_end_bindings will
1317 warn about unused variables. But, in function-at-a-time mode
1318 expand_end_bindings is not passed the list of variables in the
1319 current scope, and therefore no warning is emitted. So, we
1320 explicitly warn here. */
1321 if (!processing_template_decl)
1322 warn_about_unused_variables (getdecls ());
1324 /* If there were any declarations or structure tags in that level,
1325 or if this level is a function body,
1326 create a BLOCK to record them for the life of this function. */
1327 block = NULL_TREE;
1328 if (keep == 1 || functionbody)
1329 block = make_node (BLOCK);
1330 if (block != NULL_TREE)
1332 BLOCK_VARS (block) = decls;
1333 BLOCK_SUBBLOCKS (block) = subblocks;
1336 /* In each subblock, record that this is its superior. */
1337 if (keep >= 0)
1338 for (link = subblocks; link; link = TREE_CHAIN (link))
1339 BLOCK_SUPERCONTEXT (link) = block;
1341 /* We still support the old for-scope rules, whereby the variables
1342 in a for-init statement were in scope after the for-statement
1343 ended. We only use the new rules if flag_new_for_scope is
1344 nonzero. */
1345 leaving_for_scope
1346 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1348 /* Remove declarations for all the DECLs in this level. */
1349 for (link = decls; link; link = TREE_CHAIN (link))
1351 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1352 && DECL_NAME (link))
1354 cxx_binding *outer_binding
1355 = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
1356 tree ns_binding;
1358 if (!outer_binding)
1359 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1360 else
1361 ns_binding = NULL_TREE;
1363 if (outer_binding
1364 && (BINDING_SCOPE (outer_binding)
1365 == current_binding_level->level_chain))
1366 /* We have something like:
1368 int i;
1369 for (int i; ;);
1371 and we are leaving the `for' scope. There's no reason to
1372 keep the binding of the inner `i' in this case. */
1373 pop_binding (DECL_NAME (link), link);
1374 else if ((outer_binding
1375 && (TREE_CODE (BINDING_VALUE (outer_binding))
1376 == TYPE_DECL))
1377 || (ns_binding
1378 && TREE_CODE (ns_binding) == TYPE_DECL))
1379 /* Here, we have something like:
1381 typedef int I;
1383 void f () {
1384 for (int I; ;);
1387 We must pop the for-scope binding so we know what's a
1388 type and what isn't. */
1389 pop_binding (DECL_NAME (link), link);
1390 else
1392 /* Mark this VAR_DECL as dead so that we can tell we left it
1393 there only for backward compatibility. */
1394 DECL_DEAD_FOR_LOCAL (link) = 1;
1396 /* Keep track of what should of have happenned when we
1397 popped the binding. */
1398 if (outer_binding && BINDING_VALUE (outer_binding))
1399 DECL_SHADOWED_FOR_VAR (link)
1400 = BINDING_VALUE (outer_binding);
1402 /* Add it to the list of dead variables in the next
1403 outermost binding to that we can remove these when we
1404 leave that binding. */
1405 current_binding_level->level_chain->dead_vars_from_for
1406 = tree_cons (NULL_TREE, link,
1407 current_binding_level->level_chain->
1408 dead_vars_from_for);
1410 /* Although we don't pop the cxx_binding, we do clear
1411 its BINDING_SCOPE since the level is going away now. */
1412 BINDING_SCOPE (IDENTIFIER_BINDING (DECL_NAME (link))) = 0;
1415 else
1417 /* Remove the binding. */
1418 decl = link;
1419 if (TREE_CODE (decl) == TREE_LIST)
1420 decl = TREE_VALUE (decl);
1421 if (DECL_P (decl))
1422 pop_binding (DECL_NAME (decl), decl);
1423 else if (TREE_CODE (decl) == OVERLOAD)
1424 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1425 else
1426 abort ();
1430 /* Remove declarations for any `for' variables from inner scopes
1431 that we kept around. */
1432 for (link = current_binding_level->dead_vars_from_for;
1433 link; link = TREE_CHAIN (link))
1434 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1436 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1437 for (link = current_binding_level->type_shadowed;
1438 link; link = TREE_CHAIN (link))
1439 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1441 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1442 for (link = current_binding_level->shadowed_labels;
1443 link;
1444 link = TREE_CHAIN (link))
1445 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1447 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1448 list if a `using' declaration put them there. The debugging
1449 back-ends won't understand OVERLOAD, so we remove them here.
1450 Because the BLOCK_VARS are (temporarily) shared with
1451 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1452 popped all the bindings. */
1453 if (block)
1455 tree* d;
1457 for (d = &BLOCK_VARS (block); *d; )
1459 if (TREE_CODE (*d) == TREE_LIST)
1460 *d = TREE_CHAIN (*d);
1461 else
1462 d = &TREE_CHAIN (*d);
1466 /* If the level being exited is the top level of a function,
1467 check over all the labels. */
1468 if (functionbody)
1470 /* Since this is the top level block of a function, the vars are
1471 the function's parameters. Don't leave them in the BLOCK
1472 because they are found in the FUNCTION_DECL instead. */
1473 BLOCK_VARS (block) = 0;
1474 pop_labels (block);
1477 tmp = current_binding_level->keep;
1479 pop_binding_level ();
1480 if (functionbody)
1481 DECL_INITIAL (current_function_decl) = block;
1482 else if (block)
1483 current_binding_level->blocks
1484 = chainon (current_binding_level->blocks, block);
1486 /* If we did not make a block for the level just exited,
1487 any blocks made for inner levels
1488 (since they cannot be recorded as subblocks in that level)
1489 must be carried forward so they will later become subblocks
1490 of something else. */
1491 else if (subblocks)
1492 current_binding_level->blocks
1493 = chainon (current_binding_level->blocks, subblocks);
1495 /* Each and every BLOCK node created here in `poplevel' is important
1496 (e.g. for proper debugging information) so if we created one
1497 earlier, mark it as "used". */
1498 if (block)
1499 TREE_USED (block) = 1;
1501 /* Take care of compiler's internal binding structures. */
1502 if (tmp == 2)
1504 tree scope_stmts;
1506 scope_stmts
1507 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1508 if (block)
1510 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1511 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1514 block = poplevel (keep, reverse, functionbody);
1517 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1520 /* Delete the node BLOCK from the current binding level.
1521 This is used for the block inside a stmt expr ({...})
1522 so that the block can be reinserted where appropriate. */
1524 void
1525 delete_block (tree block)
1527 tree t;
1528 if (current_binding_level->blocks == block)
1529 current_binding_level->blocks = TREE_CHAIN (block);
1530 for (t = current_binding_level->blocks; t;)
1532 if (TREE_CHAIN (t) == block)
1533 TREE_CHAIN (t) = TREE_CHAIN (block);
1534 else
1535 t = TREE_CHAIN (t);
1537 TREE_CHAIN (block) = NULL_TREE;
1538 /* Clear TREE_USED which is always set by poplevel.
1539 The flag is set again if insert_block is called. */
1540 TREE_USED (block) = 0;
1543 /* Insert BLOCK at the end of the list of subblocks of the
1544 current binding level. This is used when a BIND_EXPR is expanded,
1545 to handle the BLOCK node inside the BIND_EXPR. */
1547 void
1548 insert_block (tree block)
1550 TREE_USED (block) = 1;
1551 current_binding_level->blocks
1552 = chainon (current_binding_level->blocks, block);
1555 /* Set the BLOCK node for the innermost scope
1556 (the one we are currently in). */
1558 void
1559 set_block (tree block ATTRIBUTE_UNUSED )
1561 /* The RTL expansion machinery requires us to provide this callback,
1562 but it is not applicable in function-at-a-time mode. */
1563 my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1566 /* Do a pushlevel for class declarations. */
1568 void
1569 pushlevel_class (void)
1571 register struct cp_binding_level *newlevel;
1573 /* Reuse or create a struct for this binding level. */
1574 #if defined(DEBUG_BINDING_LEVELS)
1575 if (0)
1576 #else /* !defined(DEBUG_BINDING_LEVELS) */
1577 if (free_binding_level)
1578 #endif /* !defined(DEBUG_BINDING_LEVELS) */
1580 newlevel = free_binding_level;
1581 free_binding_level = free_binding_level->level_chain;
1583 else
1584 newlevel = make_binding_level ();
1586 #if defined(DEBUG_BINDING_LEVELS)
1587 is_class_level = 1;
1588 #endif /* defined(DEBUG_BINDING_LEVELS) */
1590 push_binding_level (newlevel, 0, 0);
1592 class_binding_level = current_binding_level;
1593 class_binding_level->parm_flag = 2;
1594 class_binding_level->this_class = current_class_type;
1597 /* ...and a poplevel for class declarations. */
1599 void
1600 poplevel_class (void)
1602 register struct cp_binding_level *level = class_binding_level;
1603 tree shadowed;
1605 timevar_push (TV_NAME_LOOKUP);
1606 my_friendly_assert (level != 0, 354);
1608 /* If we're leaving a toplevel class, don't bother to do the setting
1609 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1610 shouldn't even be used when current_class_type isn't set, and second,
1611 if we don't touch it here, we're able to use the cache effect if the
1612 next time we're entering a class scope, it is the same class. */
1613 if (current_class_depth != 1)
1615 struct cp_binding_level* b;
1617 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1618 for (shadowed = level->class_shadowed;
1619 shadowed;
1620 shadowed = TREE_CHAIN (shadowed))
1621 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1623 /* Find the next enclosing class, and recreate
1624 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1625 b = level->level_chain;
1626 while (b && b->parm_flag != 2)
1627 b = b->level_chain;
1629 if (b)
1630 for (shadowed = b->class_shadowed;
1631 shadowed;
1632 shadowed = TREE_CHAIN (shadowed))
1634 cxx_binding *binding;
1636 binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1637 while (binding && BINDING_SCOPE (binding) != b)
1638 binding = binding->previous;
1640 if (binding)
1641 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1642 = BINDING_VALUE (binding);
1645 else
1646 /* Remember to save what IDENTIFIER's were bound in this scope so we
1647 can recover from cache misses. */
1649 previous_class_type = current_class_type;
1650 previous_class_values = class_binding_level->class_shadowed;
1652 for (shadowed = level->type_shadowed;
1653 shadowed;
1654 shadowed = TREE_CHAIN (shadowed))
1655 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1657 /* Remove the bindings for all of the class-level declarations. */
1658 for (shadowed = level->class_shadowed;
1659 shadowed;
1660 shadowed = TREE_CHAIN (shadowed))
1661 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1663 /* Now, pop out of the binding level which we created up in the
1664 `pushlevel_class' routine. */
1665 #if defined(DEBUG_BINDING_LEVELS)
1666 is_class_level = 1;
1667 #endif /* defined(DEBUG_BINDING_LEVELS) */
1669 pop_binding_level ();
1670 timevar_pop (TV_NAME_LOOKUP);
1673 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1674 for any names in enclosing classes. */
1676 void
1677 clear_identifier_class_values (void)
1679 tree t;
1681 if (!class_binding_level)
1682 return;
1684 for (t = class_binding_level->class_shadowed;
1686 t = TREE_CHAIN (t))
1687 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1690 /* Returns nonzero if T is a virtual function table. */
1693 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1695 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1698 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1699 functions. */
1702 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1704 return (TREE_CODE (t) == TYPE_DECL
1705 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1706 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1709 /* Return the declarations that are members of the namespace NS. */
1711 tree
1712 cp_namespace_decls (tree ns)
1714 return NAMESPACE_LEVEL (ns)->names;
1717 struct walk_globals_data {
1718 walk_globals_pred p;
1719 walk_globals_fn f;
1720 void *data;
1723 /* Walk the vtable declarations in NAMESPACE. Whenever one is found
1724 for which P returns nonzero, call F with its address. If any call
1725 to F returns a nonzero value, return a nonzero value. */
1727 static int
1728 walk_vtables_r (tree namespace, void* data)
1730 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1731 walk_globals_fn f = wgd->f;
1732 void *d = wgd->data;
1733 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1734 int result = 0;
1736 for (; decl ; decl = TREE_CHAIN (decl))
1737 result |= (*f) (&decl, d);
1739 return result;
1742 /* Walk the vtable declarations. Whenever one is found for which P
1743 returns nonzero, call F with its address. If any call to F
1744 returns a nonzero value, return a nonzero value. */
1745 bool
1746 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1748 struct walk_globals_data wgd;
1749 wgd.p = p;
1750 wgd.f = f;
1751 wgd.data = data;
1753 return walk_namespaces (walk_vtables_r, &wgd);
1756 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1757 itself, calling F for each. The DATA is passed to F as well. */
1759 static int
1760 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1762 int result = 0;
1763 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
1765 result |= (*f) (namespace, data);
1767 for (; current; current = TREE_CHAIN (current))
1768 result |= walk_namespaces_r (current, f, data);
1770 return result;
1773 /* Walk all the namespaces, calling F for each. The DATA is passed to
1774 F as well. */
1777 walk_namespaces (walk_namespaces_fn f, void* data)
1779 return walk_namespaces_r (global_namespace, f, data);
1782 /* Walk the global declarations in NAMESPACE. Whenever one is found
1783 for which P returns nonzero, call F with its address. If any call
1784 to F returns a nonzero value, return a nonzero value. */
1786 static int
1787 walk_globals_r (tree namespace, void* data)
1789 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1790 walk_globals_pred p = wgd->p;
1791 walk_globals_fn f = wgd->f;
1792 void *d = wgd->data;
1793 tree *t;
1794 int result = 0;
1796 t = &NAMESPACE_LEVEL (namespace)->names;
1798 while (*t)
1800 tree glbl = *t;
1802 if ((*p) (glbl, d))
1803 result |= (*f) (t, d);
1805 /* If F changed *T, then *T still points at the next item to
1806 examine. */
1807 if (*t == glbl)
1808 t = &TREE_CHAIN (*t);
1811 return result;
1814 /* Walk the global declarations. Whenever one is found for which P
1815 returns true, call F with its address. If any call to F
1816 returns true, return true. */
1818 bool
1819 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1821 struct walk_globals_data wgd;
1822 wgd.p = p;
1823 wgd.f = f;
1824 wgd.data = data;
1826 return walk_namespaces (walk_globals_r, &wgd);
1829 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1830 DATA is non-NULL, this is the last time we will call
1831 wrapup_global_declarations for this NAMESPACE. */
1834 wrapup_globals_for_namespace (tree namespace, void* data)
1836 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
1837 varray_type statics = level->static_decls;
1838 tree *vec = &VARRAY_TREE (statics, 0);
1839 int len = VARRAY_ACTIVE_SIZE (statics);
1840 int last_time = (data != 0);
1842 if (last_time)
1844 check_global_declarations (vec, len);
1845 return 0;
1848 /* Write out any globals that need to be output. */
1849 return wrapup_global_declarations (vec, len);
1853 /* For debugging. */
1854 static int no_print_functions = 0;
1855 static int no_print_builtins = 0;
1857 void
1858 print_binding_level (struct cp_binding_level* lvl)
1860 tree t;
1861 int i = 0, len;
1862 fprintf (stderr, " blocks=");
1863 fprintf (stderr, HOST_PTR_PRINTF, (void *) lvl->blocks);
1864 if (lvl->tag_transparent)
1865 fprintf (stderr, " tag-transparent");
1866 if (lvl->more_cleanups_ok)
1867 fprintf (stderr, " more-cleanups-ok");
1868 if (lvl->have_cleanups)
1869 fprintf (stderr, " have-cleanups");
1870 fprintf (stderr, "\n");
1871 if (lvl->names)
1873 fprintf (stderr, " names:\t");
1874 /* We can probably fit 3 names to a line? */
1875 for (t = lvl->names; t; t = TREE_CHAIN (t))
1877 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1878 continue;
1879 if (no_print_builtins
1880 && (TREE_CODE (t) == TYPE_DECL)
1881 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1882 continue;
1884 /* Function decls tend to have longer names. */
1885 if (TREE_CODE (t) == FUNCTION_DECL)
1886 len = 3;
1887 else
1888 len = 2;
1889 i += len;
1890 if (i > 6)
1892 fprintf (stderr, "\n\t");
1893 i = len;
1895 print_node_brief (stderr, "", t, 0);
1896 if (t == error_mark_node)
1897 break;
1899 if (i)
1900 fprintf (stderr, "\n");
1902 if (lvl->tags)
1904 fprintf (stderr, " tags:\t");
1905 i = 0;
1906 for (t = lvl->tags; t; t = TREE_CHAIN (t))
1908 if (TREE_PURPOSE (t) == NULL_TREE)
1909 len = 3;
1910 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1911 len = 2;
1912 else
1913 len = 4;
1914 i += len;
1915 if (i > 5)
1917 fprintf (stderr, "\n\t");
1918 i = len;
1920 if (TREE_PURPOSE (t) == NULL_TREE)
1922 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1923 fprintf (stderr, ">");
1925 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1926 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1927 else
1929 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1930 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1931 fprintf (stderr, ">");
1934 if (i)
1935 fprintf (stderr, "\n");
1937 if (lvl->class_shadowed)
1939 fprintf (stderr, " class-shadowed:");
1940 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1942 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1944 fprintf (stderr, "\n");
1946 if (lvl->type_shadowed)
1948 fprintf (stderr, " type-shadowed:");
1949 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1951 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1953 fprintf (stderr, "\n");
1957 void
1958 print_other_binding_stack (struct cp_binding_level *stack)
1960 struct cp_binding_level *level;
1961 for (level = stack; level != global_binding_level; level = level->level_chain)
1963 fprintf (stderr, "binding level ");
1964 fprintf (stderr, HOST_PTR_PRINTF, (void *) level);
1965 fprintf (stderr, "\n");
1966 print_binding_level (level);
1970 void
1971 print_binding_stack (void)
1973 struct cp_binding_level *b;
1974 fprintf (stderr, "current_binding_level=");
1975 fprintf (stderr, HOST_PTR_PRINTF, (void *) current_binding_level);
1976 fprintf (stderr, "\nclass_binding_level=");
1977 fprintf (stderr, HOST_PTR_PRINTF, (void *) class_binding_level);
1978 fprintf (stderr, "\nglobal_binding_level=");
1979 fprintf (stderr, HOST_PTR_PRINTF, (void *) global_binding_level);
1980 fprintf (stderr, "\n");
1981 if (class_binding_level)
1983 for (b = class_binding_level; b; b = b->level_chain)
1984 if (b == current_binding_level)
1985 break;
1986 if (b)
1987 b = class_binding_level;
1988 else
1989 b = current_binding_level;
1991 else
1992 b = current_binding_level;
1993 print_other_binding_stack (b);
1994 fprintf (stderr, "global:\n");
1995 print_binding_level (global_binding_level);
1998 /* Namespace binding access routines: The namespace_bindings field of
1999 the identifier is polymorphic, with three possible values:
2000 NULL_TREE, a list of "cxx_binding"s. */
2002 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2003 select a name that is unique to this compilation unit. */
2005 void
2006 push_namespace (tree name)
2008 tree d = NULL_TREE;
2009 int need_new = 1;
2010 int implicit_use = 0;
2011 int global = 0;
2013 timevar_push (TV_NAME_LOOKUP);
2015 if (!global_namespace)
2017 /* This must be ::. */
2018 my_friendly_assert (name == get_identifier ("::"), 377);
2019 global = 1;
2021 else if (!name)
2023 /* The name of anonymous namespace is unique for the translation
2024 unit. */
2025 if (!anonymous_namespace_name)
2026 anonymous_namespace_name = get_file_function_name ('N');
2027 name = anonymous_namespace_name;
2028 d = IDENTIFIER_NAMESPACE_VALUE (name);
2029 if (d)
2030 /* Reopening anonymous namespace. */
2031 need_new = 0;
2032 implicit_use = 1;
2034 else
2036 /* Check whether this is an extended namespace definition. */
2037 d = IDENTIFIER_NAMESPACE_VALUE (name);
2038 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2040 need_new = 0;
2041 if (DECL_NAMESPACE_ALIAS (d))
2043 error ("namespace alias `%D' not allowed here, assuming `%D'",
2044 d, DECL_NAMESPACE_ALIAS (d));
2045 d = DECL_NAMESPACE_ALIAS (d);
2050 if (need_new)
2052 /* Make a new namespace, binding the name to it. */
2053 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2054 /* The global namespace is not pushed, and the global binding
2055 level is set elsewhere. */
2056 if (!global)
2058 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2059 d = pushdecl (d);
2060 pushlevel (0);
2061 declare_namespace_level ();
2062 NAMESPACE_LEVEL (d) = current_binding_level;
2063 VARRAY_TREE_INIT (current_binding_level->static_decls,
2064 name != std_identifier ? 10 : 200,
2065 "Static declarations");
2068 else
2069 resume_binding_level (NAMESPACE_LEVEL (d));
2071 if (implicit_use)
2072 do_using_directive (d);
2073 /* Enter the name space. */
2074 current_namespace = d;
2076 timevar_pop (TV_NAME_LOOKUP);
2079 /* Pop from the scope of the current namespace. */
2081 void
2082 pop_namespace (void)
2084 my_friendly_assert (current_namespace != global_namespace, 20010801);
2085 current_namespace = CP_DECL_CONTEXT (current_namespace);
2086 /* The binding level is not popped, as it might be re-opened later. */
2087 suspend_binding_level ();
2090 /* Push into the scope of the namespace NS, even if it is deeply
2091 nested within another namespace. */
2093 void
2094 push_nested_namespace (tree ns)
2096 if (ns == global_namespace)
2097 push_to_top_level ();
2098 else
2100 push_nested_namespace (CP_DECL_CONTEXT (ns));
2101 push_namespace (DECL_NAME (ns));
2105 /* Pop back from the scope of the namespace NS, which was previously
2106 entered with push_nested_namespace. */
2108 void
2109 pop_nested_namespace (tree ns)
2111 timevar_push (TV_NAME_LOOKUP);
2112 while (ns != global_namespace)
2114 pop_namespace ();
2115 ns = CP_DECL_CONTEXT (ns);
2118 pop_from_top_level ();
2119 timevar_pop (TV_NAME_LOOKUP);
2123 /* Allocate storage for saving a C++ binding. */
2124 #define cxx_saved_binding_make() \
2125 (ggc_alloc (sizeof (cxx_saved_binding)))
2127 struct cxx_saved_binding GTY(())
2129 /* Link that chains saved C++ bindings for a given name into a stack. */
2130 cxx_saved_binding *previous;
2131 /* The name of the current binding. */
2132 tree identifier;
2133 /* The binding we're saving. */
2134 cxx_binding *binding;
2135 tree class_value;
2136 tree real_type_value;
2139 /* Subroutines for reverting temporarily to top-level for instantiation
2140 of templates and such. We actually need to clear out the class- and
2141 local-value slots of all identifiers, so that only the global values
2142 are at all visible. Simply setting current_binding_level to the global
2143 scope isn't enough, because more binding levels may be pushed. */
2144 struct saved_scope *scope_chain;
2146 static cxx_saved_binding *
2147 store_bindings (tree names, cxx_saved_binding *old_bindings)
2149 tree t;
2150 cxx_saved_binding *search_bindings = old_bindings;
2152 timevar_push (TV_NAME_LOOKUP);
2153 for (t = names; t; t = TREE_CHAIN (t))
2155 tree id;
2156 cxx_saved_binding *saved;
2157 cxx_saved_binding *t1;
2159 if (TREE_CODE (t) == TREE_LIST)
2160 id = TREE_PURPOSE (t);
2161 else
2162 id = DECL_NAME (t);
2164 if (!id
2165 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2166 we have no IDENTIFIER_BINDING if we have left the class
2167 scope, but cached the class-level declarations. */
2168 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2169 continue;
2171 for (t1 = search_bindings; t1; t1 = t1->previous)
2172 if (t1->identifier == id)
2173 goto skip_it;
2175 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2176 saved = cxx_saved_binding_make ();
2177 saved->previous = old_bindings;
2178 saved->identifier = id;
2179 saved->binding = IDENTIFIER_BINDING (id);
2180 saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2181 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2182 IDENTIFIER_BINDING (id) = NULL;
2183 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2184 old_bindings = saved;
2185 skip_it:
2188 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2191 void
2192 maybe_push_to_top_level (int pseudo)
2194 struct saved_scope *s;
2195 struct cp_binding_level *b;
2196 cxx_saved_binding *old_bindings;
2197 int need_pop;
2199 timevar_push (TV_NAME_LOOKUP);
2200 s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2202 b = scope_chain ? current_binding_level : 0;
2204 /* If we're in the middle of some function, save our state. */
2205 if (cfun)
2207 need_pop = 1;
2208 push_function_context_to (NULL_TREE);
2210 else
2211 need_pop = 0;
2213 old_bindings = NULL;
2214 if (scope_chain && previous_class_type)
2215 old_bindings = store_bindings (previous_class_values, old_bindings);
2217 /* Have to include global_binding_level, because class-level decls
2218 aren't listed anywhere useful. */
2219 for (; b; b = b->level_chain)
2221 tree t;
2223 /* Template IDs are inserted into the global level. If they were
2224 inserted into namespace level, finish_file wouldn't find them
2225 when doing pending instantiations. Therefore, don't stop at
2226 namespace level, but continue until :: . */
2227 if (b == global_binding_level || (pseudo && b->template_parms_p))
2228 break;
2230 old_bindings = store_bindings (b->names, old_bindings);
2231 /* We also need to check class_shadowed to save class-level type
2232 bindings, since pushclass doesn't fill in b->names. */
2233 if (b->parm_flag == 2)
2234 old_bindings = store_bindings (b->class_shadowed, old_bindings);
2236 /* Unwind type-value slots back to top level. */
2237 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2238 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2240 s->prev = scope_chain;
2241 s->old_bindings = old_bindings;
2242 s->bindings = b;
2243 s->need_pop_function_context = need_pop;
2244 s->function_decl = current_function_decl;
2245 s->last_parms = last_function_parms;
2246 s->check_access = flag_access_control;
2248 scope_chain = s;
2249 current_function_decl = NULL_TREE;
2250 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2251 current_lang_name = lang_name_cplusplus;
2252 current_namespace = global_namespace;
2253 timevar_pop (TV_NAME_LOOKUP);
2256 void
2257 push_to_top_level (void)
2259 maybe_push_to_top_level (0);
2262 void
2263 pop_from_top_level (void)
2265 struct saved_scope *s = scope_chain;
2266 cxx_saved_binding *saved;
2268 timevar_push (TV_NAME_LOOKUP);
2269 /* Clear out class-level bindings cache. */
2270 if (previous_class_type)
2271 invalidate_class_lookup_cache ();
2273 current_lang_base = 0;
2275 scope_chain = s->prev;
2276 for (saved = s->old_bindings; saved; saved = saved->previous)
2278 tree id = saved->identifier;
2280 IDENTIFIER_BINDING (id) = saved->binding;
2281 IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2282 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2285 /* If we were in the middle of compiling a function, restore our
2286 state. */
2287 if (s->need_pop_function_context)
2288 pop_function_context_from (NULL_TREE);
2289 current_function_decl = s->function_decl;
2290 last_function_parms = s->last_parms;
2291 timevar_pop (TV_NAME_LOOKUP);
2294 /* Push a definition of struct, union or enum tag "name".
2295 into binding_level "b". "type" should be the type node,
2296 We assume that the tag "name" is not already defined.
2298 Note that the definition may really be just a forward reference.
2299 In that case, the TYPE_SIZE will be a NULL_TREE.
2301 C++ gratuitously puts all these tags in the name space. */
2303 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2304 record the shadowed value for this binding contour. TYPE is
2305 the type that ID maps to. */
2307 static void
2308 set_identifier_type_value_with_scope (tree id,
2309 tree type,
2310 struct cp_binding_level* b)
2312 if (!b->namespace_p)
2314 /* Shadow the marker, not the real thing, so that the marker
2315 gets restored later. */
2316 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2317 b->type_shadowed
2318 = tree_cons (id, old_type_value, b->type_shadowed);
2320 else
2322 cxx_binding *binding =
2323 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2324 BINDING_TYPE (binding) = type;
2325 /* Store marker instead of real type. */
2326 type = global_type_node;
2328 SET_IDENTIFIER_TYPE_VALUE (id, type);
2331 /* As set_identifier_type_value_with_scope, but using current_binding_level. */
2333 void
2334 set_identifier_type_value (tree id, tree type)
2336 set_identifier_type_value_with_scope (id, type, current_binding_level);
2339 /* Return the type associated with id. */
2341 tree
2342 identifier_type_value (tree id)
2344 timevar_push (TV_NAME_LOOKUP);
2345 /* There is no type with that name, anywhere. */
2346 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2347 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2348 /* This is not the type marker, but the real thing. */
2349 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2350 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2351 /* Have to search for it. It must be on the global level, now.
2352 Ask lookup_name not to return non-types. */
2353 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2354 if (id)
2355 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2356 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2359 /* Pop off extraneous binding levels left over due to syntax errors.
2361 We don't pop past namespaces, as they might be valid. */
2363 void
2364 pop_everything (void)
2366 #ifdef DEBUG_BINDING_LEVELS
2367 fprintf (stderr, "XXX entering pop_everything ()\n");
2368 #endif
2369 while (!toplevel_bindings_p ())
2371 if (current_binding_level->parm_flag == 2)
2372 pop_nested_class ();
2373 else
2374 poplevel (0, 0, 0);
2376 #ifdef DEBUG_BINDING_LEVELS
2377 fprintf (stderr, "XXX leaving pop_everything ()\n");
2378 #endif
2381 /* The type TYPE is being declared. If it is a class template, or a
2382 specialization of a class template, do any processing required and
2383 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
2384 being declared a friend. B is the binding level at which this TYPE
2385 should be bound.
2387 Returns the TYPE_DECL for TYPE, which may have been altered by this
2388 processing. */
2390 static tree
2391 maybe_process_template_type_declaration (tree type,
2392 int globalize,
2393 struct cp_binding_level* b)
2395 tree decl = TYPE_NAME (type);
2397 if (processing_template_parmlist)
2398 /* You can't declare a new template type in a template parameter
2399 list. But, you can declare a non-template type:
2401 template <class A*> struct S;
2403 is a forward-declaration of `A'. */
2405 else
2407 maybe_check_template_type (type);
2409 my_friendly_assert (IS_AGGR_TYPE (type)
2410 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2413 if (processing_template_decl)
2415 /* This may change after the call to
2416 push_template_decl_real, but we want the original value. */
2417 tree name = DECL_NAME (decl);
2419 decl = push_template_decl_real (decl, globalize);
2420 /* If the current binding level is the binding level for the
2421 template parameters (see the comment in
2422 begin_template_parm_list) and the enclosing level is a class
2423 scope, and we're not looking at a friend, push the
2424 declaration of the member class into the class scope. In the
2425 friend case, push_template_decl will already have put the
2426 friend into global scope, if appropriate. */
2427 if (TREE_CODE (type) != ENUMERAL_TYPE
2428 && !globalize && b->template_parms_p
2429 && b->level_chain->parm_flag == 2)
2431 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2432 /* Put this tag on the list of tags for the class, since
2433 that won't happen below because B is not the class
2434 binding level, but is instead the pseudo-global level. */
2435 b->level_chain->tags =
2436 tree_cons (name, type, b->level_chain->tags);
2437 if (!COMPLETE_TYPE_P (current_class_type))
2439 maybe_add_class_template_decl_list (current_class_type,
2440 type, /*friend_p=*/0);
2441 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2447 return decl;
2450 /* In C++, you don't have to write `struct S' to refer to `S'; you
2451 can just use `S'. We accomplish this by creating a TYPE_DECL as
2452 if the user had written `typedef struct S S'. Create and return
2453 the TYPE_DECL for TYPE. */
2455 tree
2456 create_implicit_typedef (tree name, tree type)
2458 tree decl;
2460 decl = build_decl (TYPE_DECL, name, type);
2461 DECL_ARTIFICIAL (decl) = 1;
2462 /* There are other implicit type declarations, like the one *within*
2463 a class that allows you to write `S::S'. We must distinguish
2464 amongst these. */
2465 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2466 TYPE_NAME (type) = decl;
2468 return decl;
2471 /* Remember a local name for name-mangling purposes. */
2473 static void
2474 push_local_name (tree decl)
2476 size_t i, nelts;
2477 tree t, name;
2479 timevar_push (TV_NAME_LOOKUP);
2480 if (!local_names)
2481 VARRAY_TREE_INIT (local_names, 8, "local_names");
2483 name = DECL_NAME (decl);
2485 nelts = VARRAY_ACTIVE_SIZE (local_names);
2486 for (i = 0; i < nelts; i++)
2488 t = VARRAY_TREE (local_names, i);
2489 if (DECL_NAME (t) == name)
2491 if (!DECL_LANG_SPECIFIC (decl))
2492 retrofit_lang_decl (decl);
2493 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2494 if (DECL_LANG_SPECIFIC (t))
2495 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2496 else
2497 DECL_DISCRIMINATOR (decl) = 1;
2499 VARRAY_TREE (local_names, i) = decl;
2500 timevar_pop (TV_NAME_LOOKUP);
2501 return;
2505 VARRAY_PUSH_TREE (local_names, decl);
2506 timevar_pop (TV_NAME_LOOKUP);
2509 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2510 Normally put it into the inner-most non-tag-transparent scope,
2511 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2512 The latter is needed for implicit declarations. */
2514 void
2515 pushtag (tree name, tree type, int globalize)
2517 register struct cp_binding_level *b;
2519 timevar_push (TV_NAME_LOOKUP);
2520 b = current_binding_level;
2521 while (b->tag_transparent
2522 || (b->parm_flag == 2
2523 && (globalize
2524 /* We may be defining a new type in the initializer
2525 of a static member variable. We allow this when
2526 not pedantic, and it is particularly useful for
2527 type punning via an anonymous union. */
2528 || COMPLETE_TYPE_P (b->this_class))))
2529 b = b->level_chain;
2531 b->tags = tree_cons (name, type, b->tags);
2533 if (name)
2535 /* Do C++ gratuitous typedefing. */
2536 if (IDENTIFIER_TYPE_VALUE (name) != type)
2538 register tree d = NULL_TREE;
2539 int in_class = 0;
2540 tree context = TYPE_CONTEXT (type);
2542 if (! context)
2544 tree cs = current_scope ();
2546 if (! globalize)
2547 context = cs;
2548 else if (cs != NULL_TREE && TYPE_P (cs))
2549 /* When declaring a friend class of a local class, we want
2550 to inject the newly named class into the scope
2551 containing the local class, not the namespace scope. */
2552 context = decl_function_context (get_type_decl (cs));
2554 if (!context)
2555 context = current_namespace;
2557 if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2558 || b->parm_flag == 2)
2559 in_class = 1;
2561 if (current_lang_name == lang_name_java)
2562 TYPE_FOR_JAVA (type) = 1;
2564 d = create_implicit_typedef (name, type);
2565 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2566 if (! in_class)
2567 set_identifier_type_value_with_scope (name, type, b);
2569 d = maybe_process_template_type_declaration (type,
2570 globalize, b);
2572 if (b->parm_flag == 2)
2574 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2575 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2576 class. But if it's a member template class, we
2577 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2578 is done later. */
2579 finish_member_declaration (d);
2580 else
2581 pushdecl_class_level (d);
2583 else
2584 d = pushdecl_with_scope (d, b);
2586 /* FIXME what if it gets a name from typedef? */
2587 if (ANON_AGGRNAME_P (name))
2588 DECL_IGNORED_P (d) = 1;
2590 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2592 /* If this is a local class, keep track of it. We need this
2593 information for name-mangling, and so that it is possible to find
2594 all function definitions in a translation unit in a convenient
2595 way. (It's otherwise tricky to find a member function definition
2596 it's only pointed to from within a local class.) */
2597 if (TYPE_CONTEXT (type)
2598 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2599 && !processing_template_decl)
2600 VARRAY_PUSH_TREE (local_classes, type);
2602 if (b->parm_flag == 2)
2604 if (!COMPLETE_TYPE_P (current_class_type))
2606 maybe_add_class_template_decl_list (current_class_type,
2607 type, /*friend_p=*/0);
2608 CLASSTYPE_TAGS (current_class_type) = b->tags;
2613 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2614 /* Use the canonical TYPE_DECL for this node. */
2615 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2616 else
2618 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2619 will be the tagged type we just added to the current
2620 binding level. This fake NULL-named TYPE_DECL node helps
2621 dwarfout.c to know when it needs to output a
2622 representation of a tagged type, and it also gives us a
2623 convenient place to record the "scope start" address for
2624 the tagged type. */
2626 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2627 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2629 timevar_pop (TV_NAME_LOOKUP);
2632 /* Counter used to create anonymous type names. */
2634 static GTY(()) int anon_cnt;
2636 /* Return an IDENTIFIER which can be used as a name for
2637 anonymous structs and unions. */
2639 tree
2640 make_anon_name (void)
2642 char buf[32];
2644 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2645 return get_identifier (buf);
2648 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2649 This keeps dbxout from getting confused. */
2651 void
2652 clear_anon_tags (void)
2654 register struct cp_binding_level *b;
2655 register tree tags;
2656 static int last_cnt = 0;
2658 /* Fast out if no new anon names were declared. */
2659 if (last_cnt == anon_cnt)
2660 return;
2662 b = current_binding_level;
2663 while (b->tag_transparent)
2664 b = b->level_chain;
2665 tags = b->tags;
2666 while (tags)
2668 /* A NULL purpose means we have already processed all tags
2669 from here to the end of the list. */
2670 if (TREE_PURPOSE (tags) == NULL_TREE)
2671 break;
2672 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2673 TREE_PURPOSE (tags) = NULL_TREE;
2674 tags = TREE_CHAIN (tags);
2676 last_cnt = anon_cnt;
2679 /* Subroutine of duplicate_decls: return truthvalue of whether
2680 or not types of these decls match.
2682 For C++, we must compare the parameter list so that `int' can match
2683 `int&' in a parameter position, but `int&' is not confused with
2684 `const int&'. */
2687 decls_match (tree newdecl, tree olddecl)
2689 int types_match;
2691 if (newdecl == olddecl)
2692 return 1;
2694 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2695 /* If the two DECLs are not even the same kind of thing, we're not
2696 interested in their types. */
2697 return 0;
2699 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2701 tree f1 = TREE_TYPE (newdecl);
2702 tree f2 = TREE_TYPE (olddecl);
2703 tree p1 = TYPE_ARG_TYPES (f1);
2704 tree p2 = TYPE_ARG_TYPES (f2);
2706 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2707 && ! (DECL_EXTERN_C_P (newdecl)
2708 && DECL_EXTERN_C_P (olddecl)))
2709 return 0;
2711 if (TREE_CODE (f1) != TREE_CODE (f2))
2712 return 0;
2714 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2716 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2717 && (DECL_BUILT_IN (olddecl)
2718 #ifndef NO_IMPLICIT_EXTERN_C
2719 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2720 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2721 #endif
2724 types_match = self_promoting_args_p (p1);
2725 if (p1 == void_list_node)
2726 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2728 #ifndef NO_IMPLICIT_EXTERN_C
2729 else if (p1 == NULL_TREE
2730 && (DECL_EXTERN_C_P (olddecl)
2731 && DECL_IN_SYSTEM_HEADER (olddecl)
2732 && !DECL_CLASS_SCOPE_P (olddecl))
2733 && (DECL_EXTERN_C_P (newdecl)
2734 && DECL_IN_SYSTEM_HEADER (newdecl)
2735 && !DECL_CLASS_SCOPE_P (newdecl)))
2737 types_match = self_promoting_args_p (p2);
2738 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2740 #endif
2741 else
2742 types_match = compparms (p1, p2);
2744 else
2745 types_match = 0;
2747 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2749 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2750 DECL_TEMPLATE_PARMS (olddecl)))
2751 return 0;
2753 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2754 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2755 return 0;
2757 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2758 types_match = 1;
2759 else
2760 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2761 DECL_TEMPLATE_RESULT (newdecl));
2763 else
2765 if (TREE_TYPE (newdecl) == error_mark_node)
2766 types_match = TREE_TYPE (olddecl) == error_mark_node;
2767 else if (TREE_TYPE (olddecl) == NULL_TREE)
2768 types_match = TREE_TYPE (newdecl) == NULL_TREE;
2769 else if (TREE_TYPE (newdecl) == NULL_TREE)
2770 types_match = 0;
2771 else
2772 types_match = comptypes (TREE_TYPE (newdecl),
2773 TREE_TYPE (olddecl),
2774 COMPARE_REDECLARATION);
2777 return types_match;
2780 /* If NEWDECL is `static' and an `extern' was seen previously,
2781 warn about it. OLDDECL is the previous declaration.
2783 Note that this does not apply to the C++ case of declaring
2784 a variable `extern const' and then later `const'.
2786 Don't complain about built-in functions, since they are beyond
2787 the user's control. */
2789 static void
2790 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2792 static const char *const explicit_extern_static_warning
2793 = "`%D' was declared `extern' and later `static'";
2794 static const char *const implicit_extern_static_warning
2795 = "`%D' was declared implicitly `extern' and later `static'";
2797 tree name;
2799 if (TREE_CODE (newdecl) == TYPE_DECL
2800 || TREE_CODE (newdecl) == TEMPLATE_DECL
2801 || TREE_CODE (newdecl) == CONST_DECL)
2802 return;
2804 /* Don't get confused by static member functions; that's a different
2805 use of `static'. */
2806 if (TREE_CODE (newdecl) == FUNCTION_DECL
2807 && DECL_STATIC_FUNCTION_P (newdecl))
2808 return;
2810 /* If the old declaration was `static', or the new one isn't, then
2811 then everything is OK. */
2812 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2813 return;
2815 /* It's OK to declare a builtin function as `static'. */
2816 if (TREE_CODE (olddecl) == FUNCTION_DECL
2817 && DECL_ARTIFICIAL (olddecl))
2818 return;
2820 name = DECL_ASSEMBLER_NAME (newdecl);
2821 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2822 ? implicit_extern_static_warning
2823 : explicit_extern_static_warning, newdecl);
2824 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2827 /* Handle when a new declaration NEWDECL has the same name as an old
2828 one OLDDECL in the same binding contour. Prints an error message
2829 if appropriate.
2831 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2832 Otherwise, return 0. */
2835 duplicate_decls (tree newdecl, tree olddecl)
2837 unsigned olddecl_uid = DECL_UID (olddecl);
2838 int olddecl_friend = 0, types_match = 0;
2839 int new_defines_function = 0;
2841 if (newdecl == olddecl)
2842 return 1;
2844 types_match = decls_match (newdecl, olddecl);
2846 /* If either the type of the new decl or the type of the old decl is an
2847 error_mark_node, then that implies that we have already issued an
2848 error (earlier) for some bogus type specification, and in that case,
2849 it is rather pointless to harass the user with yet more error message
2850 about the same declaration, so just pretend the types match here. */
2851 if (TREE_TYPE (newdecl) == error_mark_node
2852 || TREE_TYPE (olddecl) == error_mark_node)
2853 types_match = 1;
2855 if (DECL_P (olddecl)
2856 && TREE_CODE (newdecl) == FUNCTION_DECL
2857 && TREE_CODE (olddecl) == FUNCTION_DECL
2858 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2860 if (DECL_DECLARED_INLINE_P (newdecl)
2861 && DECL_UNINLINABLE (newdecl)
2862 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2863 /* Already warned elsewhere. */;
2864 else if (DECL_DECLARED_INLINE_P (olddecl)
2865 && DECL_UNINLINABLE (olddecl)
2866 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2867 /* Already warned. */;
2868 else if (DECL_DECLARED_INLINE_P (newdecl)
2869 && DECL_UNINLINABLE (olddecl)
2870 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2872 warning ("%Hfunction '%D' redeclared as inline",
2873 &DECL_SOURCE_LOCATION (newdecl), newdecl);
2874 warning ("%Hprevious declaration of '%D' with attribute noinline",
2875 &DECL_SOURCE_LOCATION (olddecl), olddecl);
2877 else if (DECL_DECLARED_INLINE_P (olddecl)
2878 && DECL_UNINLINABLE (newdecl)
2879 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2881 warning ("%Hfunction '%D' redeclared with attribute noinline",
2882 &DECL_SOURCE_LOCATION (newdecl), newdecl);
2883 warning ("%Hprevious declaration of '%D' was inline",
2884 &DECL_SOURCE_LOCATION (olddecl), olddecl);
2888 /* Check for redeclaration and other discrepancies. */
2889 if (TREE_CODE (olddecl) == FUNCTION_DECL
2890 && DECL_ARTIFICIAL (olddecl))
2892 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2894 /* Avoid warnings redeclaring anticipated built-ins. */
2895 if (DECL_ANTICIPATED (olddecl))
2896 return 0;
2898 /* If you declare a built-in or predefined function name as static,
2899 the old definition is overridden, but optionally warn this was a
2900 bad choice of name. */
2901 if (! TREE_PUBLIC (newdecl))
2903 if (warn_shadow)
2904 warning ("shadowing %s function `%#D'",
2905 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2906 olddecl);
2907 /* Discard the old built-in function. */
2908 return 0;
2910 /* If the built-in is not ansi, then programs can override
2911 it even globally without an error. */
2912 else if (! DECL_BUILT_IN (olddecl))
2913 warning ("library function `%#D' redeclared as non-function `%#D'",
2914 olddecl, newdecl);
2915 else
2917 error ("declaration of `%#D'", newdecl);
2918 error ("conflicts with built-in declaration `%#D'",
2919 olddecl);
2921 return 0;
2923 else if (!types_match)
2925 /* Avoid warnings redeclaring anticipated built-ins. */
2926 if (DECL_ANTICIPATED (olddecl))
2927 ; /* Do nothing yet. */
2928 else if ((DECL_EXTERN_C_P (newdecl)
2929 && DECL_EXTERN_C_P (olddecl))
2930 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2931 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2933 /* A near match; override the builtin. */
2935 if (TREE_PUBLIC (newdecl))
2937 warning ("new declaration `%#D'", newdecl);
2938 warning ("ambiguates built-in declaration `%#D'",
2939 olddecl);
2941 else if (warn_shadow)
2942 warning ("shadowing %s function `%#D'",
2943 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2944 olddecl);
2946 else
2947 /* Discard the old built-in function. */
2948 return 0;
2950 /* Replace the old RTL to avoid problems with inlining. */
2951 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2953 /* Even if the types match, prefer the new declarations type
2954 for anitipated built-ins, for exception lists, etc... */
2955 else if (DECL_ANTICIPATED (olddecl))
2956 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2958 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2960 /* If a builtin function is redeclared as `static', merge
2961 the declarations, but make the original one static. */
2962 DECL_THIS_STATIC (olddecl) = 1;
2963 TREE_PUBLIC (olddecl) = 0;
2965 /* Make the old declaration consistent with the new one so
2966 that all remnants of the builtin-ness of this function
2967 will be banished. */
2968 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2969 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2972 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2974 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
2975 && TREE_CODE (newdecl) != TYPE_DECL
2976 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
2977 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
2978 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
2979 && TREE_CODE (olddecl) != TYPE_DECL
2980 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
2981 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2982 == TYPE_DECL))))
2984 /* We do nothing special here, because C++ does such nasty
2985 things with TYPE_DECLs. Instead, just let the TYPE_DECL
2986 get shadowed, and know that if we need to find a TYPE_DECL
2987 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2988 slot of the identifier. */
2989 return 0;
2992 if ((TREE_CODE (newdecl) == FUNCTION_DECL
2993 && DECL_FUNCTION_TEMPLATE_P (olddecl))
2994 || (TREE_CODE (olddecl) == FUNCTION_DECL
2995 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2996 return 0;
2998 error ("`%#D' redeclared as different kind of symbol", newdecl);
2999 if (TREE_CODE (olddecl) == TREE_LIST)
3000 olddecl = TREE_VALUE (olddecl);
3001 cp_error_at ("previous declaration of `%#D'", olddecl);
3003 /* New decl is completely inconsistent with the old one =>
3004 tell caller to replace the old one. */
3006 return 0;
3008 else if (!types_match)
3010 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3011 /* These are certainly not duplicate declarations; they're
3012 from different scopes. */
3013 return 0;
3015 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3017 /* The name of a class template may not be declared to refer to
3018 any other template, class, function, object, namespace, value,
3019 or type in the same scope. */
3020 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3021 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3023 error ("declaration of template `%#D'", newdecl);
3024 cp_error_at ("conflicts with previous declaration `%#D'",
3025 olddecl);
3027 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3028 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3029 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3030 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3031 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3032 DECL_TEMPLATE_PARMS (olddecl))
3033 /* Template functions can be disambiguated by
3034 return type. */
3035 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3036 TREE_TYPE (TREE_TYPE (olddecl))))
3038 error ("new declaration `%#D'", newdecl);
3039 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3041 return 0;
3043 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3045 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3047 error ("declaration of C function `%#D' conflicts with",
3048 newdecl);
3049 cp_error_at ("previous declaration `%#D' here", olddecl);
3051 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3052 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3054 error ("new declaration `%#D'", newdecl);
3055 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3057 else
3058 return 0;
3061 /* Already complained about this, so don't do so again. */
3062 else if (current_class_type == NULL_TREE
3063 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3065 error ("conflicting types for `%#D'", newdecl);
3066 cp_error_at ("previous declaration as `%#D'", olddecl);
3069 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3070 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3071 && (!DECL_TEMPLATE_INFO (newdecl)
3072 || (DECL_TI_TEMPLATE (newdecl)
3073 != DECL_TI_TEMPLATE (olddecl))))
3074 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3075 && (!DECL_TEMPLATE_INFO (olddecl)
3076 || (DECL_TI_TEMPLATE (olddecl)
3077 != DECL_TI_TEMPLATE (newdecl))))))
3078 /* It's OK to have a template specialization and a non-template
3079 with the same type, or to have specializations of two
3080 different templates with the same type. Note that if one is a
3081 specialization, and the other is an instantiation of the same
3082 template, that we do not exit at this point. That situation
3083 can occur if we instantiate a template class, and then
3084 specialize one of its methods. This situation is valid, but
3085 the declarations must be merged in the usual way. */
3086 return 0;
3087 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3088 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3089 && !DECL_USE_TEMPLATE (newdecl))
3090 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3091 && !DECL_USE_TEMPLATE (olddecl))))
3092 /* One of the declarations is a template instantiation, and the
3093 other is not a template at all. That's OK. */
3094 return 0;
3095 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3096 && DECL_NAMESPACE_ALIAS (newdecl)
3097 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3098 /* Redeclaration of namespace alias, ignore it. */
3099 return 1;
3100 else
3102 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3103 if (errmsg)
3105 error (errmsg, newdecl);
3106 if (DECL_NAME (olddecl) != NULL_TREE)
3107 cp_error_at ((DECL_INITIAL (olddecl)
3108 && namespace_bindings_p ())
3109 ? "`%#D' previously defined here"
3110 : "`%#D' previously declared here", olddecl);
3111 return 0;
3113 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3114 && DECL_INITIAL (olddecl) != NULL_TREE
3115 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3116 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3118 /* Prototype decl follows defn w/o prototype. */
3119 cp_warning_at ("prototype for `%#D'", newdecl);
3120 cp_warning_at ("follows non-prototype definition here", olddecl);
3122 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3123 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3125 /* extern "C" int foo ();
3126 int foo () { bar (); }
3127 is OK. */
3128 if (current_lang_depth () == 0)
3129 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3130 else
3132 cp_error_at ("previous declaration of `%#D' with %L linkage",
3133 olddecl, DECL_LANGUAGE (olddecl));
3134 error ("conflicts with new declaration with %L linkage",
3135 DECL_LANGUAGE (newdecl));
3139 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3141 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3143 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3144 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3145 int i = 1;
3147 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3148 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3150 for (; t1 && t1 != void_list_node;
3151 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3152 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3154 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3155 TREE_PURPOSE (t2)))
3157 pedwarn ("default argument given for parameter %d of `%#D'",
3158 i, newdecl);
3159 cp_pedwarn_at ("after previous specification in `%#D'",
3160 olddecl);
3162 else
3164 error ("default argument given for parameter %d of `%#D'",
3165 i, newdecl);
3166 cp_error_at ("after previous specification in `%#D'",
3167 olddecl);
3171 if (DECL_DECLARED_INLINE_P (newdecl)
3172 && ! DECL_DECLARED_INLINE_P (olddecl)
3173 && TREE_ADDRESSABLE (olddecl) && warn_inline)
3175 warning ("`%#D' was used before it was declared inline",
3176 newdecl);
3177 cp_warning_at ("previous non-inline declaration here",
3178 olddecl);
3183 /* Do not merge an implicit typedef with an explicit one. In:
3185 class A;
3187 typedef class A A __attribute__ ((foo));
3189 the attribute should apply only to the typedef. */
3190 if (TREE_CODE (olddecl) == TYPE_DECL
3191 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3192 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3193 return 0;
3195 /* If new decl is `static' and an `extern' was seen previously,
3196 warn about it. */
3197 warn_extern_redeclared_static (newdecl, olddecl);
3199 /* We have committed to returning 1 at this point. */
3200 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3202 /* Now that functions must hold information normally held
3203 by field decls, there is extra work to do so that
3204 declaration information does not get destroyed during
3205 definition. */
3206 if (DECL_VINDEX (olddecl))
3207 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3208 if (DECL_CONTEXT (olddecl))
3209 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3210 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3211 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3212 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3213 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3214 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3215 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3216 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3217 SET_OVERLOADED_OPERATOR_CODE
3218 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3219 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3221 /* Optionally warn about more than one declaration for the same
3222 name, but don't warn about a function declaration followed by a
3223 definition. */
3224 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3225 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3226 /* Don't warn about extern decl followed by definition. */
3227 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3228 /* Don't warn about friends, let add_friend take care of it. */
3229 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3231 warning ("redundant redeclaration of `%D' in same scope", newdecl);
3232 cp_warning_at ("previous declaration of `%D'", olddecl);
3236 /* Deal with C++: must preserve virtual function table size. */
3237 if (TREE_CODE (olddecl) == TYPE_DECL)
3239 register tree newtype = TREE_TYPE (newdecl);
3240 register tree oldtype = TREE_TYPE (olddecl);
3242 if (newtype != error_mark_node && oldtype != error_mark_node
3243 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3244 CLASSTYPE_FRIEND_CLASSES (newtype)
3245 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3247 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3250 /* Copy all the DECL_... slots specified in the new decl
3251 except for any that we copy here from the old type. */
3252 DECL_ATTRIBUTES (newdecl)
3253 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3255 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3257 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3258 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3259 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3260 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3262 /* If the new declaration is a definition, update the file and
3263 line information on the declaration. */
3264 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3265 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3267 DECL_SOURCE_LOCATION (olddecl)
3268 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3269 = DECL_SOURCE_LOCATION (newdecl);
3272 return 1;
3275 if (types_match)
3277 /* Automatically handles default parameters. */
3278 tree oldtype = TREE_TYPE (olddecl);
3279 tree newtype;
3281 /* Merge the data types specified in the two decls. */
3282 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3284 /* If merge_types produces a non-typedef type, just use the old type. */
3285 if (TREE_CODE (newdecl) == TYPE_DECL
3286 && newtype == DECL_ORIGINAL_TYPE (newdecl))
3287 newtype = oldtype;
3289 if (TREE_CODE (newdecl) == VAR_DECL)
3291 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3292 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3295 /* Do this after calling `merge_types' so that default
3296 parameters don't confuse us. */
3297 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3298 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3299 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3301 TREE_TYPE (newdecl) = build_exception_variant (newtype,
3302 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3303 TREE_TYPE (olddecl) = build_exception_variant (newtype,
3304 TYPE_RAISES_EXCEPTIONS (oldtype));
3306 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3307 && DECL_SOURCE_LINE (olddecl) != 0
3308 && flag_exceptions
3309 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3310 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3312 error ("declaration of `%F' throws different exceptions",
3313 newdecl);
3314 cp_error_at ("than previous declaration `%F'", olddecl);
3317 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3319 /* Lay the type out, unless already done. */
3320 if (! same_type_p (newtype, oldtype)
3321 && TREE_TYPE (newdecl) != error_mark_node
3322 && !(processing_template_decl && uses_template_parms (newdecl)))
3323 layout_type (TREE_TYPE (newdecl));
3325 if ((TREE_CODE (newdecl) == VAR_DECL
3326 || TREE_CODE (newdecl) == PARM_DECL
3327 || TREE_CODE (newdecl) == RESULT_DECL
3328 || TREE_CODE (newdecl) == FIELD_DECL
3329 || TREE_CODE (newdecl) == TYPE_DECL)
3330 && !(processing_template_decl && uses_template_parms (newdecl)))
3331 layout_decl (newdecl, 0);
3333 /* Merge the type qualifiers. */
3334 if (TREE_READONLY (newdecl))
3335 TREE_READONLY (olddecl) = 1;
3336 if (TREE_THIS_VOLATILE (newdecl))
3337 TREE_THIS_VOLATILE (olddecl) = 1;
3339 /* Merge the initialization information. */
3340 if (DECL_INITIAL (newdecl) == NULL_TREE
3341 && DECL_INITIAL (olddecl) != NULL_TREE)
3343 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3344 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3345 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3346 && DECL_LANG_SPECIFIC (newdecl)
3347 && DECL_LANG_SPECIFIC (olddecl))
3348 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3351 /* Merge the section attribute.
3352 We want to issue an error if the sections conflict but that must be
3353 done later in decl_attributes since we are called before attributes
3354 are assigned. */
3355 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3356 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3358 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3360 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3361 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3362 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
3363 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
3364 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
3365 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
3366 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
3367 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
3368 /* Keep the old RTL. */
3369 COPY_DECL_RTL (olddecl, newdecl);
3371 else if (TREE_CODE (newdecl) == VAR_DECL
3372 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3374 /* Keep the old RTL. We cannot keep the old RTL if the old
3375 declaration was for an incomplete object and the new
3376 declaration is not since many attributes of the RTL will
3377 change. */
3378 COPY_DECL_RTL (olddecl, newdecl);
3381 /* If cannot merge, then use the new type and qualifiers,
3382 and don't preserve the old rtl. */
3383 else
3385 /* Clean out any memory we had of the old declaration. */
3386 tree oldstatic = value_member (olddecl, static_aggregates);
3387 if (oldstatic)
3388 TREE_VALUE (oldstatic) = error_mark_node;
3390 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3391 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3392 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3393 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3396 /* Merge the storage class information. */
3397 merge_weak (newdecl, olddecl);
3399 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3400 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3401 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3402 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3403 if (! DECL_EXTERNAL (olddecl))
3404 DECL_EXTERNAL (newdecl) = 0;
3406 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3408 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3409 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3410 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3411 DECL_TEMPLATE_INSTANTIATED (newdecl)
3412 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3413 /* Don't really know how much of the language-specific
3414 values we should copy from old to new. */
3415 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3416 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
3417 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3418 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3419 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3420 DECL_INITIALIZED_IN_CLASS_P (newdecl)
3421 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3422 olddecl_friend = DECL_FRIEND_P (olddecl);
3424 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3425 if (TREE_CODE (newdecl) == FUNCTION_DECL
3426 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3428 DECL_BEFRIENDING_CLASSES (newdecl)
3429 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3430 DECL_BEFRIENDING_CLASSES (olddecl));
3431 /* DECL_THUNKS is only valid for virtual functions,
3432 otherwise it is a DECL_FRIEND_CONTEXT. */
3433 if (DECL_VIRTUAL_P (newdecl))
3434 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3438 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3440 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3441 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3443 /* If newdecl is not a specialization, then it is not a
3444 template-related function at all. And that means that we
3445 should have exited above, returning 0. */
3446 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3449 if (TREE_USED (olddecl))
3450 /* From [temp.expl.spec]:
3452 If a template, a member template or the member of a class
3453 template is explicitly specialized then that
3454 specialization shall be declared before the first use of
3455 that specialization that would cause an implicit
3456 instantiation to take place, in every translation unit in
3457 which such a use occurs. */
3458 error ("explicit specialization of %D after first use",
3459 olddecl);
3461 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3463 /* [temp.expl.spec/14] We don't inline explicit specialization
3464 just because the primary template says so. */
3466 else
3468 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3469 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3471 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3473 /* If either decl says `inline', this fn is inline, unless
3474 its definition was passed already. */
3475 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3476 DECL_INLINE (olddecl) = 1;
3477 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3479 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3480 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3483 /* Preserve abstractness on cloned [cd]tors. */
3484 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3486 if (! types_match)
3488 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3489 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3490 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3492 if (! types_match || new_defines_function)
3494 /* These need to be copied so that the names are available.
3495 Note that if the types do match, we'll preserve inline
3496 info and other bits, but if not, we won't. */
3497 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3498 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3500 if (new_defines_function)
3501 /* If defining a function declared with other language
3502 linkage, use the previously declared language linkage. */
3503 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3504 else if (types_match)
3506 /* If redeclaring a builtin function, and not a definition,
3507 it stays built in. */
3508 if (DECL_BUILT_IN (olddecl))
3510 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3511 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3512 /* If we're keeping the built-in definition, keep the rtl,
3513 regardless of declaration matches. */
3514 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3516 else
3517 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3519 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3520 /* Don't clear out the arguments if we're redefining a function. */
3521 if (DECL_ARGUMENTS (olddecl))
3522 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3525 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3526 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3528 /* Now preserve various other info from the definition. */
3529 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3530 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3531 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3532 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3534 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3536 int function_size;
3538 function_size = sizeof (struct tree_decl);
3540 memcpy ((char *) olddecl + sizeof (struct tree_common),
3541 (char *) newdecl + sizeof (struct tree_common),
3542 function_size - sizeof (struct tree_common));
3544 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3546 /* If newdecl is a template instantiation, it is possible that
3547 the following sequence of events has occurred:
3549 o A friend function was declared in a class template. The
3550 class template was instantiated.
3552 o The instantiation of the friend declaration was
3553 recorded on the instantiation list, and is newdecl.
3555 o Later, however, instantiate_class_template called pushdecl
3556 on the newdecl to perform name injection. But, pushdecl in
3557 turn called duplicate_decls when it discovered that another
3558 declaration of a global function with the same name already
3559 existed.
3561 o Here, in duplicate_decls, we decided to clobber newdecl.
3563 If we're going to do that, we'd better make sure that
3564 olddecl, and not newdecl, is on the list of
3565 instantiations so that if we try to do the instantiation
3566 again we won't get the clobbered declaration. */
3568 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3569 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3571 for (; decls; decls = TREE_CHAIN (decls))
3572 if (TREE_VALUE (decls) == newdecl)
3573 TREE_VALUE (decls) = olddecl;
3576 else
3578 memcpy ((char *) olddecl + sizeof (struct tree_common),
3579 (char *) newdecl + sizeof (struct tree_common),
3580 sizeof (struct tree_decl) - sizeof (struct tree_common)
3581 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3584 DECL_UID (olddecl) = olddecl_uid;
3585 if (olddecl_friend)
3586 DECL_FRIEND_P (olddecl) = 1;
3588 /* NEWDECL contains the merged attribute lists.
3589 Update OLDDECL to be the same. */
3590 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3592 return 1;
3595 /* Record a decl-node X as belonging to the current lexical scope.
3596 Check for errors (such as an incompatible declaration for the same
3597 name already seen in the same scope).
3599 Returns either X or an old decl for the same name.
3600 If an old decl is returned, it may have been smashed
3601 to agree with what X says. */
3603 tree
3604 pushdecl (tree x)
3606 register tree t;
3607 register tree name;
3608 int need_new_binding;
3610 timevar_push (TV_NAME_LOOKUP);
3611 /* We shouldn't be calling pushdecl when we're generating RTL for a
3612 function that we already did semantic analysis on previously. */
3613 my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3614 19990913);
3616 need_new_binding = 1;
3618 if (DECL_TEMPLATE_PARM_P (x))
3619 /* Template parameters have no context; they are not X::T even
3620 when declared within a class or namespace. */
3622 else
3624 if (current_function_decl && x != current_function_decl
3625 /* A local declaration for a function doesn't constitute
3626 nesting. */
3627 && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3628 /* A local declaration for an `extern' variable is in the
3629 scope of the current namespace, not the current
3630 function. */
3631 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3632 && !DECL_CONTEXT (x))
3633 DECL_CONTEXT (x) = current_function_decl;
3635 /* If this is the declaration for a namespace-scope function,
3636 but the declaration itself is in a local scope, mark the
3637 declaration. */
3638 if (TREE_CODE (x) == FUNCTION_DECL
3639 && DECL_NAMESPACE_SCOPE_P (x)
3640 && current_function_decl
3641 && x != current_function_decl)
3642 DECL_LOCAL_FUNCTION_P (x) = 1;
3645 name = DECL_NAME (x);
3646 if (name)
3648 int different_binding_level = 0;
3650 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3651 name = TREE_OPERAND (name, 0);
3653 /* In case this decl was explicitly namespace-qualified, look it
3654 up in its namespace context. */
3655 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3656 && namespace_bindings_p ())
3657 t = namespace_binding (name, DECL_CONTEXT (x));
3658 else
3659 t = lookup_name_current_level (name);
3661 /* [basic.link] If there is a visible declaration of an entity
3662 with linkage having the same name and type, ignoring entities
3663 declared outside the innermost enclosing namespace scope, the
3664 block scope declaration declares that same entity and
3665 receives the linkage of the previous declaration. */
3666 if (! t && current_function_decl && x != current_function_decl
3667 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3668 && DECL_EXTERNAL (x))
3670 /* Look in block scope. */
3671 t = IDENTIFIER_VALUE (name);
3672 /* Or in the innermost namespace. */
3673 if (! t)
3674 t = namespace_binding (name, DECL_CONTEXT (x));
3675 /* Does it have linkage? Note that if this isn't a DECL, it's an
3676 OVERLOAD, which is OK. */
3677 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3678 t = NULL_TREE;
3679 if (t)
3680 different_binding_level = 1;
3683 /* If we are declaring a function, and the result of name-lookup
3684 was an OVERLOAD, look for an overloaded instance that is
3685 actually the same as the function we are declaring. (If
3686 there is one, we have to merge our declaration with the
3687 previous declaration.) */
3688 if (t && TREE_CODE (t) == OVERLOAD)
3690 tree match;
3692 if (TREE_CODE (x) == FUNCTION_DECL)
3693 for (match = t; match; match = OVL_NEXT (match))
3695 if (decls_match (OVL_CURRENT (match), x))
3696 break;
3698 else
3699 /* Just choose one. */
3700 match = t;
3702 if (match)
3703 t = OVL_CURRENT (match);
3704 else
3705 t = NULL_TREE;
3708 if (t == error_mark_node)
3710 /* error_mark_node is 0 for a while during initialization! */
3711 t = NULL_TREE;
3712 cp_error_at ("`%#D' used prior to declaration", x);
3714 else if (t != NULL_TREE)
3716 if (different_binding_level)
3718 if (decls_match (x, t))
3719 /* The standard only says that the local extern
3720 inherits linkage from the previous decl; in
3721 particular, default args are not shared. It would
3722 be nice to propagate inlining info, though. FIXME. */
3723 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3725 else if (TREE_CODE (t) == PARM_DECL)
3727 if (DECL_CONTEXT (t) == NULL_TREE)
3728 /* This is probaby caused by too many errors, but calling
3729 abort will say that if errors have occurred. */
3730 abort ();
3732 /* Check for duplicate params. */
3733 if (duplicate_decls (x, t))
3734 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3736 else if ((DECL_EXTERN_C_FUNCTION_P (x)
3737 || DECL_FUNCTION_TEMPLATE_P (x))
3738 && is_overloaded_fn (t))
3739 /* Don't do anything just yet. */;
3740 else if (t == wchar_decl_node)
3742 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3743 pedwarn ("redeclaration of `wchar_t' as `%T'",
3744 TREE_TYPE (x));
3746 /* Throw away the redeclaration. */
3747 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3749 else if (TREE_CODE (t) != TREE_CODE (x))
3751 if (duplicate_decls (x, t))
3752 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3754 else if (duplicate_decls (x, t))
3756 if (TREE_CODE (t) == TYPE_DECL)
3757 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3758 else if (TREE_CODE (t) == FUNCTION_DECL)
3759 check_default_args (t);
3761 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3763 else if (DECL_MAIN_P (x))
3765 /* A redeclaration of main, but not a duplicate of the
3766 previous one.
3768 [basic.start.main]
3770 This function shall not be overloaded. */
3771 cp_error_at ("invalid redeclaration of `%D'", t);
3772 error ("as `%D'", x);
3773 /* We don't try to push this declaration since that
3774 causes a crash. */
3775 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3779 check_template_shadow (x);
3781 /* If this is a function conjured up by the backend, massage it
3782 so it looks friendly. */
3783 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3785 retrofit_lang_decl (x);
3786 SET_DECL_LANGUAGE (x, lang_c);
3789 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3791 t = push_overloaded_decl (x, PUSH_LOCAL);
3792 if (t != x)
3793 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3794 if (!namespace_bindings_p ())
3795 /* We do not need to create a binding for this name;
3796 push_overloaded_decl will have already done so if
3797 necessary. */
3798 need_new_binding = 0;
3800 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3802 t = push_overloaded_decl (x, PUSH_GLOBAL);
3803 if (t == x)
3804 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3805 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3808 /* If declaring a type as a typedef, copy the type (unless we're
3809 at line 0), and install this TYPE_DECL as the new type's typedef
3810 name. See the extensive comment in ../c-decl.c (pushdecl). */
3811 if (TREE_CODE (x) == TYPE_DECL)
3813 tree type = TREE_TYPE (x);
3814 if (DECL_SOURCE_LINE (x) == 0)
3816 if (TYPE_NAME (type) == 0)
3817 TYPE_NAME (type) = x;
3819 else if (type != error_mark_node && TYPE_NAME (type) != x
3820 /* We don't want to copy the type when all we're
3821 doing is making a TYPE_DECL for the purposes of
3822 inlining. */
3823 && (!TYPE_NAME (type)
3824 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3826 DECL_ORIGINAL_TYPE (x) = type;
3827 type = build_type_copy (type);
3828 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3829 TYPE_NAME (type) = x;
3830 TREE_TYPE (x) = type;
3833 if (type != error_mark_node
3834 && TYPE_NAME (type)
3835 && TYPE_IDENTIFIER (type))
3836 set_identifier_type_value_with_scope (DECL_NAME (x), type,
3837 current_binding_level);
3841 /* Multiple external decls of the same identifier ought to match.
3843 We get warnings about inline functions where they are defined.
3844 We get warnings about other functions from push_overloaded_decl.
3846 Avoid duplicate warnings where they are used. */
3847 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3849 tree decl;
3851 decl = IDENTIFIER_NAMESPACE_VALUE (name);
3852 if (decl && TREE_CODE (decl) == OVERLOAD)
3853 decl = OVL_FUNCTION (decl);
3855 if (decl && decl != error_mark_node
3856 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3857 /* If different sort of thing, we already gave an error. */
3858 && TREE_CODE (decl) == TREE_CODE (x)
3859 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3861 pedwarn ("type mismatch with previous external decl", x);
3862 cp_pedwarn_at ("previous external decl of `%#D'", decl);
3866 /* This name is new in its binding level.
3867 Install the new declaration and return it. */
3868 if (namespace_bindings_p ())
3870 /* Install a global value. */
3872 /* If the first global decl has external linkage,
3873 warn if we later see static one. */
3874 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3875 TREE_PUBLIC (name) = 1;
3877 /* Bind the name for the entity. */
3878 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3879 && t != NULL_TREE)
3880 && (TREE_CODE (x) == TYPE_DECL
3881 || TREE_CODE (x) == VAR_DECL
3882 || TREE_CODE (x) == ALIAS_DECL
3883 || TREE_CODE (x) == NAMESPACE_DECL
3884 || TREE_CODE (x) == CONST_DECL
3885 || TREE_CODE (x) == TEMPLATE_DECL))
3886 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3888 /* Don't forget if the function was used via an implicit decl. */
3889 if (IDENTIFIER_IMPLICIT_DECL (name)
3890 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3891 TREE_USED (x) = 1;
3893 /* Don't forget if its address was taken in that way. */
3894 if (IDENTIFIER_IMPLICIT_DECL (name)
3895 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3896 TREE_ADDRESSABLE (x) = 1;
3898 /* Warn about mismatches against previous implicit decl. */
3899 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3900 /* If this real decl matches the implicit, don't complain. */
3901 && ! (TREE_CODE (x) == FUNCTION_DECL
3902 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3903 warning
3904 ("`%D' was previously implicitly declared to return `int'", x);
3906 /* If new decl is `static' and an `extern' was seen previously,
3907 warn about it. */
3908 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3909 warn_extern_redeclared_static (x, t);
3911 else
3913 /* Here to install a non-global value. */
3914 tree oldlocal = IDENTIFIER_VALUE (name);
3915 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3917 if (need_new_binding)
3919 push_local_binding (name, x, 0);
3920 /* Because push_local_binding will hook X on to the
3921 current_binding_level's name list, we don't want to
3922 do that again below. */
3923 need_new_binding = 0;
3926 /* If this is a TYPE_DECL, push it into the type value slot. */
3927 if (TREE_CODE (x) == TYPE_DECL)
3928 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
3929 current_binding_level);
3931 /* Clear out any TYPE_DECL shadowed by a namespace so that
3932 we won't think this is a type. The C struct hack doesn't
3933 go through namespaces. */
3934 if (TREE_CODE (x) == NAMESPACE_DECL)
3935 set_identifier_type_value_with_scope (name, NULL_TREE,
3936 current_binding_level);
3938 if (oldlocal)
3940 tree d = oldlocal;
3942 while (oldlocal
3943 && TREE_CODE (oldlocal) == VAR_DECL
3944 && DECL_DEAD_FOR_LOCAL (oldlocal))
3945 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
3947 if (oldlocal == NULL_TREE)
3948 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
3951 /* If this is an extern function declaration, see if we
3952 have a global definition or declaration for the function. */
3953 if (oldlocal == NULL_TREE
3954 && DECL_EXTERNAL (x)
3955 && oldglobal != NULL_TREE
3956 && TREE_CODE (x) == FUNCTION_DECL
3957 && TREE_CODE (oldglobal) == FUNCTION_DECL)
3959 /* We have one. Their types must agree. */
3960 if (decls_match (x, oldglobal))
3961 /* OK */;
3962 else
3964 warning ("extern declaration of `%#D' doesn't match", x);
3965 cp_warning_at ("global declaration `%#D'", oldglobal);
3968 /* If we have a local external declaration,
3969 and no file-scope declaration has yet been seen,
3970 then if we later have a file-scope decl it must not be static. */
3971 if (oldlocal == NULL_TREE
3972 && oldglobal == NULL_TREE
3973 && DECL_EXTERNAL (x)
3974 && TREE_PUBLIC (x))
3975 TREE_PUBLIC (name) = 1;
3977 /* Warn if shadowing an argument at the top level of the body. */
3978 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3979 /* Inline decls shadow nothing. */
3980 && !DECL_FROM_INLINE (x)
3981 && TREE_CODE (oldlocal) == PARM_DECL
3982 /* Don't check the `this' parameter. */
3983 && !DECL_ARTIFICIAL (oldlocal))
3985 bool err = false;
3987 /* Don't complain if it's from an enclosing function. */
3988 if (DECL_CONTEXT (oldlocal) == current_function_decl
3989 && TREE_CODE (x) != PARM_DECL)
3991 /* Go to where the parms should be and see if we find
3992 them there. */
3993 struct cp_binding_level *b = current_binding_level->level_chain;
3995 /* Skip the ctor/dtor cleanup level. */
3996 b = b->level_chain;
3998 /* ARM $8.3 */
3999 if (b->parm_flag == 1)
4001 error ("declaration of `%#D' shadows a parameter",
4002 name);
4003 err = true;
4007 if (warn_shadow && !err)
4008 shadow_warning (SW_PARAM,
4009 IDENTIFIER_POINTER (name), oldlocal);
4012 /* Maybe warn if shadowing something else. */
4013 else if (warn_shadow && !DECL_EXTERNAL (x)
4014 /* No shadow warnings for internally generated vars. */
4015 && ! DECL_ARTIFICIAL (x)
4016 /* No shadow warnings for vars made for inlining. */
4017 && ! DECL_FROM_INLINE (x))
4019 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4020 && current_class_ptr
4021 && !TREE_STATIC (name))
4022 warning ("declaration of `%s' shadows a member of `this'",
4023 IDENTIFIER_POINTER (name));
4024 else if (oldlocal != NULL_TREE
4025 && TREE_CODE (oldlocal) == VAR_DECL)
4026 shadow_warning (SW_LOCAL,
4027 IDENTIFIER_POINTER (name), oldlocal);
4028 else if (oldglobal != NULL_TREE
4029 && TREE_CODE (oldglobal) == VAR_DECL)
4030 /* XXX shadow warnings in outer-more namespaces */
4031 shadow_warning (SW_GLOBAL,
4032 IDENTIFIER_POINTER (name), oldglobal);
4036 if (TREE_CODE (x) == FUNCTION_DECL)
4037 check_default_args (x);
4039 if (TREE_CODE (x) == VAR_DECL)
4040 maybe_register_incomplete_var (x);
4043 if (need_new_binding)
4044 add_decl_to_level (x,
4045 DECL_NAMESPACE_SCOPE_P (x)
4046 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4047 : current_binding_level);
4049 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4052 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4053 caller to set DECL_CONTEXT properly. */
4055 static tree
4056 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4058 register struct cp_binding_level *b;
4059 tree function_decl = current_function_decl;
4061 timevar_push (TV_NAME_LOOKUP);
4062 current_function_decl = NULL_TREE;
4063 if (level->parm_flag == 2)
4065 b = class_binding_level;
4066 class_binding_level = level;
4067 pushdecl_class_level (x);
4068 class_binding_level = b;
4070 else
4072 b = current_binding_level;
4073 current_binding_level = level;
4074 x = pushdecl (x);
4075 current_binding_level = b;
4077 current_function_decl = function_decl;
4078 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4081 /* Like pushdecl, only it places X in the current namespace,
4082 if appropriate. */
4084 tree
4085 pushdecl_namespace_level (tree x)
4087 register struct cp_binding_level *b = current_binding_level;
4088 register tree t;
4090 timevar_push (TV_NAME_LOOKUP);
4091 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4093 /* Now, the type_shadowed stack may screw us. Munge it so it does
4094 what we want. */
4095 if (TREE_CODE (x) == TYPE_DECL)
4097 tree name = DECL_NAME (x);
4098 tree newval;
4099 tree *ptr = (tree *)0;
4100 for (; b != global_binding_level; b = b->level_chain)
4102 tree shadowed = b->type_shadowed;
4103 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4104 if (TREE_PURPOSE (shadowed) == name)
4106 ptr = &TREE_VALUE (shadowed);
4107 /* Can't break out of the loop here because sometimes
4108 a binding level will have duplicate bindings for
4109 PT names. It's gross, but I haven't time to fix it. */
4112 newval = TREE_TYPE (x);
4113 if (ptr == (tree *)0)
4115 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4116 up here if this is changed to an assertion. --KR */
4117 SET_IDENTIFIER_TYPE_VALUE (name, newval);
4119 else
4121 *ptr = newval;
4124 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4127 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4128 if appropriate. */
4130 tree
4131 pushdecl_top_level (tree x)
4133 timevar_push (TV_NAME_LOOKUP);
4134 push_to_top_level ();
4135 x = pushdecl_namespace_level (x);
4136 pop_from_top_level ();
4137 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4140 /* Make the declaration of X appear in CLASS scope. */
4142 void
4143 pushdecl_class_level (tree x)
4145 tree name;
4147 timevar_push (TV_NAME_LOOKUP);
4148 /* Get the name of X. */
4149 if (TREE_CODE (x) == OVERLOAD)
4150 name = DECL_NAME (get_first_fn (x));
4151 else
4152 name = DECL_NAME (x);
4154 if (name)
4156 push_class_level_binding (name, x);
4157 if (TREE_CODE (x) == TYPE_DECL)
4158 set_identifier_type_value (name, TREE_TYPE (x));
4160 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4162 /* If X is an anonymous aggregate, all of its members are
4163 treated as if they were members of the class containing the
4164 aggregate, for naming purposes. */
4165 tree f;
4167 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4168 pushdecl_class_level (f);
4170 timevar_pop (TV_NAME_LOOKUP);
4173 /* Enter DECL into the symbol table, if that's appropriate. Returns
4174 DECL, or a modified version thereof. */
4176 tree
4177 maybe_push_decl (tree decl)
4179 tree type = TREE_TYPE (decl);
4181 /* Add this decl to the current binding level, but not if it comes
4182 from another scope, e.g. a static member variable. TEM may equal
4183 DECL or it may be a previous decl of the same name. */
4184 if (decl == error_mark_node
4185 || (TREE_CODE (decl) != PARM_DECL
4186 && DECL_CONTEXT (decl) != NULL_TREE
4187 /* Definitions of namespace members outside their namespace are
4188 possible. */
4189 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4190 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4191 || TREE_CODE (type) == UNKNOWN_TYPE
4192 /* The declaration of a template specialization does not affect
4193 the functions available for overload resolution, so we do not
4194 call pushdecl. */
4195 || (TREE_CODE (decl) == FUNCTION_DECL
4196 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4197 return decl;
4198 else
4199 return pushdecl (decl);
4202 /* Make the declaration(s) of X appear in CLASS scope
4203 under the name NAME. */
4205 void
4206 push_class_level_binding (tree name, tree x)
4208 cxx_binding *binding;
4209 timevar_push (TV_NAME_LOOKUP);
4210 /* The class_binding_level will be NULL if x is a template
4211 parameter name in a member template. */
4212 if (!class_binding_level)
4214 timevar_pop (TV_NAME_LOOKUP);
4215 return;
4218 /* Make sure that this new member does not have the same name
4219 as a template parameter. */
4220 if (TYPE_BEING_DEFINED (current_class_type))
4221 check_template_shadow (x);
4223 /* If this declaration shadows a declaration from an enclosing
4224 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4225 we leave this class. Record the shadowed declaration here. */
4226 binding = IDENTIFIER_BINDING (name);
4227 if (binding
4228 && ((TREE_CODE (x) == OVERLOAD
4229 && BINDING_VALUE (binding)
4230 && is_overloaded_fn (BINDING_VALUE (binding)))
4231 || INHERITED_VALUE_BINDING_P (binding)))
4233 tree shadow;
4234 tree old_decl;
4236 /* If the old binding was from a base class, and was for a tag
4237 name, slide it over to make room for the new binding. The
4238 old binding is still visible if explicitly qualified with a
4239 class-key. */
4240 if (INHERITED_VALUE_BINDING_P (binding)
4241 && BINDING_VALUE (binding)
4242 && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4243 && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4244 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4246 old_decl = BINDING_TYPE (binding);
4247 BINDING_TYPE (binding) = BINDING_VALUE (binding);
4248 BINDING_VALUE (binding) = NULL_TREE;
4249 INHERITED_VALUE_BINDING_P (binding) = 0;
4251 else
4252 old_decl = BINDING_VALUE (binding);
4254 /* Find the previous binding of name on the class-shadowed
4255 list, and update it. */
4256 for (shadow = class_binding_level->class_shadowed;
4257 shadow;
4258 shadow = TREE_CHAIN (shadow))
4259 if (TREE_PURPOSE (shadow) == name
4260 && TREE_TYPE (shadow) == old_decl)
4262 BINDING_VALUE (binding) = x;
4263 INHERITED_VALUE_BINDING_P (binding) = 0;
4264 TREE_TYPE (shadow) = x;
4265 IDENTIFIER_CLASS_VALUE (name) = x;
4266 timevar_pop (TV_NAME_LOOKUP);
4267 return;
4271 /* If we didn't replace an existing binding, put the binding on the
4272 stack of bindings for the identifier, and update the shadowed list. */
4273 if (push_class_binding (name, x))
4275 class_binding_level->class_shadowed
4276 = tree_cons (name, NULL,
4277 class_binding_level->class_shadowed);
4278 /* Record the value we are binding NAME to so that we can know
4279 what to pop later. */
4280 TREE_TYPE (class_binding_level->class_shadowed) = x;
4282 timevar_pop (TV_NAME_LOOKUP);
4285 /* Insert another USING_DECL into the current binding level, returning
4286 this declaration. If this is a redeclaration, do nothing, and
4287 return NULL_TREE if this not in namespace scope (in namespace
4288 scope, a using decl might extend any previous bindings). */
4290 tree
4291 push_using_decl (tree scope, tree name)
4293 tree decl;
4295 timevar_push (TV_NAME_LOOKUP);
4296 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4297 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4298 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4299 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4300 break;
4301 if (decl)
4302 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4303 namespace_bindings_p () ? decl : NULL_TREE);
4304 decl = build_lang_decl (USING_DECL, name, void_type_node);
4305 DECL_INITIAL (decl) = scope;
4306 TREE_CHAIN (decl) = current_binding_level->usings;
4307 current_binding_level->usings = decl;
4308 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4311 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4312 changed (i.e. there was already a directive), or the fresh
4313 TREE_LIST otherwise. */
4315 tree
4316 push_using_directive (tree used)
4318 tree ud = current_binding_level->using_directives;
4319 tree iter, ancestor;
4321 timevar_push (TV_NAME_LOOKUP);
4322 /* Check if we already have this. */
4323 if (purpose_member (used, ud) != NULL_TREE)
4324 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4326 ancestor = namespace_ancestor (current_decl_namespace (), used);
4327 ud = current_binding_level->using_directives;
4328 ud = tree_cons (used, ancestor, ud);
4329 current_binding_level->using_directives = ud;
4331 /* Recursively add all namespaces used. */
4332 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4333 push_using_directive (TREE_PURPOSE (iter));
4335 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4338 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4339 other definitions already in place. We get around this by making
4340 the value of the identifier point to a list of all the things that
4341 want to be referenced by that name. It is then up to the users of
4342 that name to decide what to do with that list.
4344 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4345 DECL_TEMPLATE_RESULT. It is dealt with the same way.
4347 FLAGS is a bitwise-or of the following values:
4348 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4349 namespace scope.
4350 PUSH_USING: DECL is being pushed as the result of a using
4351 declaration.
4353 The value returned may be a previous declaration if we guessed wrong
4354 about what language DECL should belong to (C or C++). Otherwise,
4355 it's always DECL (and never something that's not a _DECL). */
4357 tree
4358 push_overloaded_decl (tree decl, int flags)
4360 tree name = DECL_NAME (decl);
4361 tree old;
4362 tree new_binding;
4363 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4365 timevar_push (TV_NAME_LOOKUP);
4366 if (doing_global)
4367 old = namespace_binding (name, DECL_CONTEXT (decl));
4368 else
4369 old = lookup_name_current_level (name);
4371 if (old)
4373 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4375 tree t = TREE_TYPE (old);
4376 if (IS_AGGR_TYPE (t) && warn_shadow
4377 && (! DECL_IN_SYSTEM_HEADER (decl)
4378 || ! DECL_IN_SYSTEM_HEADER (old)))
4379 warning ("`%#D' hides constructor for `%#T'", decl, t);
4380 old = NULL_TREE;
4382 else if (is_overloaded_fn (old))
4384 tree tmp;
4386 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4388 tree fn = OVL_CURRENT (tmp);
4390 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4391 && !(flags & PUSH_USING)
4392 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4393 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4394 error ("`%#D' conflicts with previous using declaration `%#D'",
4395 decl, fn);
4397 if (duplicate_decls (decl, fn))
4398 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4401 else if (old == error_mark_node)
4402 /* Ignore the undefined symbol marker. */
4403 old = NULL_TREE;
4404 else
4406 cp_error_at ("previous non-function declaration `%#D'", old);
4407 error ("conflicts with function declaration `%#D'", decl);
4408 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4412 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4414 if (old && TREE_CODE (old) != OVERLOAD)
4415 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4416 else
4417 new_binding = ovl_cons (decl, old);
4418 if (flags & PUSH_USING)
4419 OVL_USED (new_binding) = 1;
4421 else
4422 /* NAME is not ambiguous. */
4423 new_binding = decl;
4425 if (doing_global)
4426 set_namespace_binding (name, current_namespace, new_binding);
4427 else
4429 /* We only create an OVERLOAD if there was a previous binding at
4430 this level, or if decl is a template. In the former case, we
4431 need to remove the old binding and replace it with the new
4432 binding. We must also run through the NAMES on the binding
4433 level where the name was bound to update the chain. */
4435 if (TREE_CODE (new_binding) == OVERLOAD && old)
4437 tree *d;
4439 for (d = &BINDING_SCOPE (IDENTIFIER_BINDING (name))->names;
4441 d = &TREE_CHAIN (*d))
4442 if (*d == old
4443 || (TREE_CODE (*d) == TREE_LIST
4444 && TREE_VALUE (*d) == old))
4446 if (TREE_CODE (*d) == TREE_LIST)
4447 /* Just replace the old binding with the new. */
4448 TREE_VALUE (*d) = new_binding;
4449 else
4450 /* Build a TREE_LIST to wrap the OVERLOAD. */
4451 *d = tree_cons (NULL_TREE, new_binding,
4452 TREE_CHAIN (*d));
4454 /* And update the cxx_binding node. */
4455 BINDING_VALUE (IDENTIFIER_BINDING (name))
4456 = new_binding;
4457 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4460 /* We should always find a previous binding in this case. */
4461 abort ();
4464 /* Install the new binding. */
4465 push_local_binding (name, new_binding, flags);
4468 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4471 /* Generate an implicit declaration for identifier FUNCTIONID
4472 as a function of type int (). Print a warning if appropriate. */
4474 tree
4475 implicitly_declare (tree functionid)
4477 register tree decl;
4479 /* We used to reuse an old implicit decl here,
4480 but this loses with inline functions because it can clobber
4481 the saved decl chains. */
4482 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4484 DECL_EXTERNAL (decl) = 1;
4485 TREE_PUBLIC (decl) = 1;
4487 /* ISO standard says implicit declarations are in the innermost block.
4488 So we record the decl in the standard fashion. */
4489 pushdecl (decl);
4490 rest_of_decl_compilation (decl, NULL, 0, 0);
4492 if (warn_implicit
4493 /* Only one warning per identifier. */
4494 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4496 pedwarn ("implicit declaration of function `%#D'", decl);
4499 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4501 return decl;
4504 /* Return zero if the declaration NEWDECL is valid
4505 when the declaration OLDDECL (assumed to be for the same name)
4506 has already been seen.
4507 Otherwise return an error message format string with a %s
4508 where the identifier should go. */
4510 static const char *
4511 redeclaration_error_message (tree newdecl, tree olddecl)
4513 if (TREE_CODE (newdecl) == TYPE_DECL)
4515 /* Because C++ can put things into name space for free,
4516 constructs like "typedef struct foo { ... } foo"
4517 would look like an erroneous redeclaration. */
4518 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4519 return 0;
4520 else
4521 return "redefinition of `%#D'";
4523 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4525 /* If this is a pure function, its olddecl will actually be
4526 the original initialization to `0' (which we force to call
4527 abort()). Don't complain about redefinition in this case. */
4528 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4529 return 0;
4531 /* If both functions come from different namespaces, this is not
4532 a redeclaration - this is a conflict with a used function. */
4533 if (DECL_NAMESPACE_SCOPE_P (olddecl)
4534 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4535 return "`%D' conflicts with used function";
4537 /* We'll complain about linkage mismatches in
4538 warn_extern_redeclared_static. */
4540 /* Defining the same name twice is no good. */
4541 if (DECL_INITIAL (olddecl) != NULL_TREE
4542 && DECL_INITIAL (newdecl) != NULL_TREE)
4544 if (DECL_NAME (olddecl) == NULL_TREE)
4545 return "`%#D' not declared in class";
4546 else
4547 return "redefinition of `%#D'";
4549 return 0;
4551 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4553 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4554 && (DECL_TEMPLATE_RESULT (newdecl)
4555 != DECL_TEMPLATE_RESULT (olddecl))
4556 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4557 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4558 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4559 && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4560 && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4561 return "redefinition of `%#D'";
4562 return 0;
4564 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4566 /* Objects declared at top level: */
4567 /* If at least one is a reference, it's ok. */
4568 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4569 return 0;
4570 /* Reject two definitions. */
4571 return "redefinition of `%#D'";
4573 else
4575 /* Objects declared with block scope: */
4576 /* Reject two definitions, and reject a definition
4577 together with an external reference. */
4578 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4579 return "redeclaration of `%#D'";
4580 return 0;
4584 /* Create a new label, named ID. */
4586 static tree
4587 make_label_decl (tree id, int local_p)
4589 tree decl;
4591 decl = build_decl (LABEL_DECL, id, void_type_node);
4592 if (expanding_p)
4593 /* Make sure every label has an rtx. */
4594 label_rtx (decl);
4596 DECL_CONTEXT (decl) = current_function_decl;
4597 DECL_MODE (decl) = VOIDmode;
4598 C_DECLARED_LABEL_FLAG (decl) = local_p;
4600 /* Say where one reference is to the label, for the sake of the
4601 error if it is not defined. */
4602 DECL_SOURCE_LINE (decl) = lineno;
4603 DECL_SOURCE_FILE (decl) = input_filename;
4605 /* Record the fact that this identifier is bound to this label. */
4606 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4608 return decl;
4611 /* Record this label on the list of used labels so that we can check
4612 at the end of the function to see whether or not the label was
4613 actually defined, and so we can check when the label is defined whether
4614 this use is valid. */
4616 static void
4617 use_label (tree decl)
4619 if (named_label_uses == NULL
4620 || named_label_uses->names_in_scope != current_binding_level->names
4621 || named_label_uses->label_decl != decl)
4623 struct named_label_use_list *new_ent;
4624 new_ent = ((struct named_label_use_list *)
4625 ggc_alloc (sizeof (struct named_label_use_list)));
4626 new_ent->label_decl = decl;
4627 new_ent->names_in_scope = current_binding_level->names;
4628 new_ent->binding_level = current_binding_level;
4629 new_ent->lineno_o_goto = lineno;
4630 new_ent->filename_o_goto = input_filename;
4631 new_ent->next = named_label_uses;
4632 named_label_uses = new_ent;
4636 /* Look for a label named ID in the current function. If one cannot
4637 be found, create one. (We keep track of used, but undefined,
4638 labels, and complain about them at the end of a function.) */
4640 tree
4641 lookup_label (tree id)
4643 tree decl;
4644 struct named_label_list *ent;
4646 timevar_push (TV_NAME_LOOKUP);
4647 /* You can't use labels at global scope. */
4648 if (current_function_decl == NULL_TREE)
4650 error ("label `%s' referenced outside of any function",
4651 IDENTIFIER_POINTER (id));
4652 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4655 /* See if we've already got this label. */
4656 decl = IDENTIFIER_LABEL_VALUE (id);
4657 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4658 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4660 /* Record this label on the list of labels used in this function.
4661 We do this before calling make_label_decl so that we get the
4662 IDENTIFIER_LABEL_VALUE before the new label is declared. */
4663 ent = ((struct named_label_list *)
4664 ggc_alloc_cleared (sizeof (struct named_label_list)));
4665 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4666 ent->next = named_labels;
4667 named_labels = ent;
4669 /* We need a new label. */
4670 decl = make_label_decl (id, /*local_p=*/0);
4672 /* Now fill in the information we didn't have before. */
4673 ent->label_decl = decl;
4675 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4678 /* Declare a local label named ID. */
4680 tree
4681 declare_local_label (tree id)
4683 tree decl;
4685 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4686 this scope we can restore the old value of
4687 IDENTIFIER_TYPE_VALUE. */
4688 current_binding_level->shadowed_labels
4689 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4690 current_binding_level->shadowed_labels);
4691 /* Look for the label. */
4692 decl = make_label_decl (id, /*local_p=*/1);
4693 /* Now fill in the information we didn't have before. */
4694 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4696 return decl;
4699 /* Returns nonzero if it is ill-formed to jump past the declaration of
4700 DECL. Returns 2 if it's also a real problem. */
4702 static int
4703 decl_jump_unsafe (tree decl)
4705 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4706 return 0;
4708 if (DECL_INITIAL (decl) == NULL_TREE
4709 && pod_type_p (TREE_TYPE (decl)))
4710 return 0;
4712 /* This is really only important if we're crossing an initialization.
4713 The POD stuff is just pedantry; why should it matter if the class
4714 contains a field of pointer to member type? */
4715 if (DECL_INITIAL (decl)
4716 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4717 return 2;
4718 return 1;
4721 /* Check that a single previously seen jump to a newly defined label
4722 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4723 the jump context; NAMES are the names in scope in LEVEL at the jump
4724 context; FILE and LINE are the source position of the jump or 0. */
4726 static void
4727 check_previous_goto_1 (tree decl,
4728 struct cp_binding_level* level,
4729 tree names,
4730 const char* file,
4731 int line)
4733 int identified = 0;
4734 int saw_eh = 0;
4735 struct cp_binding_level *b = current_binding_level;
4736 for (; b; b = b->level_chain)
4738 tree new_decls = b->names;
4739 tree old_decls = (b == level ? names : NULL_TREE);
4740 for (; new_decls != old_decls;
4741 new_decls = TREE_CHAIN (new_decls))
4743 int problem = decl_jump_unsafe (new_decls);
4744 if (! problem)
4745 continue;
4747 if (! identified)
4749 if (decl)
4750 pedwarn ("jump to label `%D'", decl);
4751 else
4752 pedwarn ("jump to case label");
4754 if (file)
4755 pedwarn_with_file_and_line (file, line, " from here");
4756 identified = 1;
4759 if (problem > 1)
4760 cp_error_at (" crosses initialization of `%#D'",
4761 new_decls);
4762 else
4763 cp_pedwarn_at (" enters scope of non-POD `%#D'",
4764 new_decls);
4767 if (b == level)
4768 break;
4769 if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
4771 if (! identified)
4773 if (decl)
4774 pedwarn ("jump to label `%D'", decl);
4775 else
4776 pedwarn ("jump to case label");
4778 if (file)
4779 pedwarn_with_file_and_line (file, line, " from here");
4780 identified = 1;
4782 if (b->is_try_scope)
4783 error (" enters try block");
4784 else
4785 error (" enters catch block");
4786 saw_eh = 1;
4791 static void
4792 check_previous_goto (struct named_label_use_list* use)
4794 check_previous_goto_1 (use->label_decl, use->binding_level,
4795 use->names_in_scope, use->filename_o_goto,
4796 use->lineno_o_goto);
4799 static void
4800 check_switch_goto (struct cp_binding_level* level)
4802 check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0);
4805 /* Check that any previously seen jumps to a newly defined label DECL
4806 are OK. Called by define_label. */
4808 static void
4809 check_previous_gotos (tree decl)
4811 struct named_label_use_list **usep;
4813 if (! TREE_USED (decl))
4814 return;
4816 for (usep = &named_label_uses; *usep; )
4818 struct named_label_use_list *use = *usep;
4819 if (use->label_decl == decl)
4821 check_previous_goto (use);
4822 *usep = use->next;
4824 else
4825 usep = &(use->next);
4829 /* Check that a new jump to a label DECL is OK. Called by
4830 finish_goto_stmt. */
4832 void
4833 check_goto (tree decl)
4835 int identified = 0;
4836 tree bad;
4837 struct named_label_list *lab;
4839 /* We can't know where a computed goto is jumping. So we assume
4840 that it's OK. */
4841 if (! DECL_P (decl))
4842 return;
4844 /* If the label hasn't been defined yet, defer checking. */
4845 if (! DECL_INITIAL (decl))
4847 use_label (decl);
4848 return;
4851 for (lab = named_labels; lab; lab = lab->next)
4852 if (decl == lab->label_decl)
4853 break;
4855 /* If the label is not on named_labels it's a gcc local label, so
4856 it must be in an outer scope, so jumping to it is always OK. */
4857 if (lab == 0)
4858 return;
4860 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4861 && !identified)
4863 cp_pedwarn_at ("jump to label `%D'", decl);
4864 pedwarn (" from here");
4865 identified = 1;
4868 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4870 tree b = TREE_VALUE (bad);
4871 int u = decl_jump_unsafe (b);
4873 if (u > 1 && DECL_ARTIFICIAL (b))
4874 /* Can't skip init of __exception_info. */
4875 cp_error_at (" enters catch block", b);
4876 else if (u > 1)
4877 cp_error_at (" skips initialization of `%#D'", b);
4878 else
4879 cp_pedwarn_at (" enters scope of non-POD `%#D'", b);
4882 if (lab->in_try_scope)
4883 error (" enters try block");
4884 else if (lab->in_catch_scope)
4885 error (" enters catch block");
4888 /* Define a label, specifying the location in the source file.
4889 Return the LABEL_DECL node for the label, if the definition is valid.
4890 Otherwise return 0. */
4892 tree
4893 define_label (const char* filename, int line, tree name)
4895 tree decl = lookup_label (name);
4896 struct named_label_list *ent;
4897 register struct cp_binding_level *p;
4899 timevar_push (TV_NAME_LOOKUP);
4900 for (ent = named_labels; ent; ent = ent->next)
4901 if (ent->label_decl == decl)
4902 break;
4904 /* After labels, make any new cleanups in the function go into their
4905 own new (temporary) binding contour. */
4906 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
4907 p->more_cleanups_ok = 0;
4909 if (name == get_identifier ("wchar_t"))
4910 pedwarn ("label named wchar_t");
4912 if (DECL_INITIAL (decl) != NULL_TREE)
4914 error ("duplicate label `%D'", decl);
4915 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4917 else
4919 /* Mark label as having been defined. */
4920 DECL_INITIAL (decl) = error_mark_node;
4921 /* Say where in the source. */
4922 DECL_SOURCE_FILE (decl) = filename;
4923 DECL_SOURCE_LINE (decl) = line;
4924 if (ent)
4926 ent->names_in_scope = current_binding_level->names;
4927 ent->binding_level = current_binding_level;
4929 check_previous_gotos (decl);
4930 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4932 timevar_pop (TV_NAME_LOOKUP);
4935 struct cp_switch
4937 struct cp_binding_level *level;
4938 struct cp_switch *next;
4939 /* The SWITCH_STMT being built. */
4940 tree switch_stmt;
4941 /* A splay-tree mapping the low element of a case range to the high
4942 element, or NULL_TREE if there is no high element. Used to
4943 determine whether or not a new case label duplicates an old case
4944 label. We need a tree, rather than simply a hash table, because
4945 of the GNU case range extension. */
4946 splay_tree cases;
4949 /* A stack of the currently active switch statements. The innermost
4950 switch statement is on the top of the stack. There is no need to
4951 mark the stack for garbage collection because it is only active
4952 during the processing of the body of a function, and we never
4953 collect at that point. */
4955 static struct cp_switch *switch_stack;
4957 /* Called right after a switch-statement condition is parsed.
4958 SWITCH_STMT is the switch statement being parsed. */
4960 void
4961 push_switch (tree switch_stmt)
4963 struct cp_switch *p
4964 = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
4965 p->level = current_binding_level;
4966 p->next = switch_stack;
4967 p->switch_stmt = switch_stmt;
4968 p->cases = splay_tree_new (case_compare, NULL, NULL);
4969 switch_stack = p;
4972 void
4973 pop_switch (void)
4975 struct cp_switch *cs;
4977 cs = switch_stack;
4978 splay_tree_delete (cs->cases);
4979 switch_stack = switch_stack->next;
4980 free (cs);
4983 /* Note that we've seen a definition of a case label, and complain if this
4984 is a bad place for one. */
4986 tree
4987 finish_case_label (tree low_value, tree high_value)
4989 tree cond, r;
4990 register struct cp_binding_level *p;
4992 if (! switch_stack)
4994 if (high_value)
4995 error ("case label not within a switch statement");
4996 else if (low_value)
4997 error ("case label `%E' not within a switch statement",
4998 low_value);
4999 else
5000 error ("`default' label not within a switch statement");
5001 return NULL_TREE;
5004 if (processing_template_decl)
5006 tree label;
5008 /* For templates, just add the case label; we'll do semantic
5009 analysis at instantiation-time. */
5010 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5011 return add_stmt (build_case_label (low_value, high_value, label));
5014 /* Find the condition on which this switch statement depends. */
5015 cond = SWITCH_COND (switch_stack->switch_stmt);
5016 if (cond && TREE_CODE (cond) == TREE_LIST)
5017 cond = TREE_VALUE (cond);
5019 r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5021 check_switch_goto (switch_stack->level);
5023 /* After labels, make any new cleanups in the function go into their
5024 own new (temporary) binding contour. */
5025 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5026 p->more_cleanups_ok = 0;
5028 return r;
5031 /* Return the list of declarations of the current level.
5032 Note that this list is in reverse order unless/until
5033 you nreverse it; and when you do nreverse it, you must
5034 store the result back using `storedecls' or you will lose. */
5036 tree
5037 getdecls (void)
5039 return current_binding_level->names;
5042 /* Return the list of type-tags (for structs, etc) of the current level. */
5044 tree
5045 gettags (void)
5047 return current_binding_level->tags;
5050 /* Store the list of declarations of the current level.
5051 This is done for the parameter declarations of a function being defined,
5052 after they are modified in the light of any missing parameters. */
5054 static void
5055 storedecls (tree decls)
5057 current_binding_level->names = decls;
5060 /* Similarly, store the list of tags of the current level. */
5062 void
5063 storetags (tree tags)
5065 current_binding_level->tags = tags;
5068 /* Return the type that should be used when TYPE's name is preceded
5069 by a tag such as 'struct' or 'union', or null if the name cannot
5070 be used in this way.
5072 For example, when processing the third line of:
5074 struct A;
5075 typedef struct A A;
5076 struct A;
5078 lookup of A will find the typedef. Given A's typedef, this function
5079 will return the type associated with "struct A". For the tag to be
5080 anything other than TYPE, TYPE must be a typedef whose original type
5081 has the same name and context as TYPE itself.
5083 It is not valid for a typedef of an anonymous type to be used with
5084 an explicit tag:
5086 typedef struct { ... } B;
5087 struct B;
5089 Return null for this case. */
5091 static tree
5092 follow_tag_typedef (tree type)
5094 tree original;
5096 original = original_type (type);
5097 if (! TYPE_NAME (original))
5098 return NULL_TREE;
5099 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5100 && (CP_DECL_CONTEXT (TYPE_NAME (original))
5101 == CP_DECL_CONTEXT (TYPE_NAME (type)))
5102 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5103 return original;
5104 else
5105 return NULL_TREE;
5108 /* Given NAME, an IDENTIFIER_NODE,
5109 return the structure (or union or enum) definition for that name.
5110 Searches binding levels from BINDING_SCOPE up to the global level.
5111 If THISLEVEL_ONLY is nonzero, searches only the specified context
5112 (but skips any tag-transparent contexts to find one that is
5113 meaningful for tags).
5114 FORM says which kind of type the caller wants;
5115 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5116 If the wrong kind of type is found, and it's not a template, an error is
5117 reported. */
5119 static tree
5120 lookup_tag (enum tree_code form, tree name,
5121 struct cp_binding_level* binding_level, int thislevel_only)
5123 register struct cp_binding_level *level;
5124 /* Nonzero if, we should look past a template parameter level, even
5125 if THISLEVEL_ONLY. */
5126 int allow_template_parms_p = 1;
5128 timevar_push (TV_NAME_LOOKUP);
5129 for (level = binding_level; level; level = level->level_chain)
5131 register tree tail;
5132 if (ANON_AGGRNAME_P (name))
5133 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5135 /* There's no need for error checking here, because
5136 anon names are unique throughout the compilation. */
5137 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5138 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5140 else if (level->namespace_p)
5141 /* Do namespace lookup. */
5142 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5144 cxx_binding *binding =
5145 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
5146 tree old;
5148 /* If we just skipped past a template parameter level,
5149 even though THISLEVEL_ONLY, and we find a template
5150 class declaration, then we use the _TYPE node for the
5151 template. See the example below. */
5152 if (thislevel_only && !allow_template_parms_p
5153 && binding && BINDING_VALUE (binding)
5154 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (binding)))
5155 old = TREE_TYPE (BINDING_VALUE (binding));
5156 else if (binding)
5157 old = BINDING_TYPE (binding);
5158 else
5159 old = NULL_TREE;
5161 if (old)
5163 /* We've found something at this binding level. If it is
5164 a typedef, extract the tag it refers to. Lookup fails
5165 if the typedef doesn't refer to a taggable type. */
5166 old = follow_tag_typedef (old);
5167 if (!old)
5168 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5169 if (TREE_CODE (old) != form
5170 && (form == ENUMERAL_TYPE
5171 || TREE_CODE (old) == ENUMERAL_TYPE))
5173 error ("`%#D' redeclared as %C", old, form);
5174 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5176 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
5178 if (thislevel_only || tail == global_namespace)
5179 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5181 else
5182 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5184 if (TREE_PURPOSE (tail) == name)
5186 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5188 if (code != form
5189 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5191 /* Definition isn't the kind we were looking for. */
5192 error ("`%#D' redeclared as %C", TREE_VALUE (tail), form);
5193 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5195 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5198 if (thislevel_only && ! level->tag_transparent)
5200 if (level->template_parms_p && allow_template_parms_p)
5202 /* We must deal with cases like this:
5204 template <class T> struct S;
5205 template <class T> struct S {};
5207 When looking up `S', for the second declaration, we
5208 would like to find the first declaration. But, we
5209 are in the pseudo-global level created for the
5210 template parameters, rather than the (surrounding)
5211 namespace level. Thus, we keep going one more level,
5212 even though THISLEVEL_ONLY is nonzero. */
5213 allow_template_parms_p = 0;
5214 continue;
5216 else
5217 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5220 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5223 /* Given a type, find the tag that was defined for it and return the tag name.
5224 Otherwise return 0. However, the value can never be 0
5225 in the cases in which this is used.
5227 C++: If NAME is nonzero, this is the new name to install. This is
5228 done when replacing anonymous tags with real tag names. */
5230 static tree
5231 lookup_tag_reverse (tree type, tree name)
5233 register struct cp_binding_level *level;
5235 timevar_push (TV_NAME_LOOKUP);
5236 for (level = current_binding_level; level; level = level->level_chain)
5238 register tree tail;
5239 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5241 if (TREE_VALUE (tail) == type)
5243 if (name)
5244 TREE_PURPOSE (tail) = name;
5245 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_PURPOSE (tail));
5249 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5252 /* Look up NAME in the NAMESPACE. */
5254 tree
5255 lookup_namespace_name (tree namespace, tree name)
5257 tree val;
5258 tree template_id = NULL_TREE;
5259 cxx_binding binding;
5261 timevar_push (TV_NAME_LOOKUP);
5262 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5264 if (TREE_CODE (name) == NAMESPACE_DECL)
5265 /* This happens for A::B<int> when B is a namespace. */
5266 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5267 else if (TREE_CODE (name) == TEMPLATE_DECL)
5269 /* This happens for A::B where B is a template, and there are no
5270 template arguments. */
5271 error ("invalid use of `%D'", name);
5272 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5275 namespace = ORIGINAL_NAMESPACE (namespace);
5277 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5279 template_id = name;
5280 name = TREE_OPERAND (name, 0);
5281 if (TREE_CODE (name) == OVERLOAD)
5282 name = DECL_NAME (OVL_CURRENT (name));
5283 else if (DECL_P (name))
5284 name = DECL_NAME (name);
5287 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5289 cxx_binding_clear (&binding);
5290 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
5291 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5293 if (binding.value)
5295 val = binding.value;
5297 if (template_id)
5299 if (DECL_CLASS_TEMPLATE_P (val))
5300 val = lookup_template_class (val,
5301 TREE_OPERAND (template_id, 1),
5302 /*in_decl=*/NULL_TREE,
5303 /*context=*/NULL_TREE,
5304 /*entering_scope=*/0,
5305 tf_error | tf_warning);
5306 else if (DECL_FUNCTION_TEMPLATE_P (val)
5307 || TREE_CODE (val) == OVERLOAD)
5308 val = lookup_template_function (val,
5309 TREE_OPERAND (template_id, 1));
5310 else
5312 error ("`%D::%D' is not a template",
5313 namespace, name);
5314 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5318 /* If we have a single function from a using decl, pull it out. */
5319 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5320 val = OVL_FUNCTION (val);
5322 /* Ignore built-in functions that haven't been prototyped yet. */
5323 if (!val || !DECL_P(val)
5324 || !DECL_LANG_SPECIFIC(val)
5325 || !DECL_ANTICIPATED (val))
5326 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5329 error ("`%D' undeclared in namespace `%D'", name, namespace);
5330 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5333 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5335 static hashval_t
5336 typename_hash (const void* k)
5338 hashval_t hash;
5339 tree t = (tree) k;
5341 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5342 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5344 return hash;
5347 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5349 static int
5350 typename_compare (const void * k1, const void * k2)
5352 tree t1;
5353 tree t2;
5354 tree d1;
5355 tree d2;
5357 t1 = (tree) k1;
5358 t2 = (tree) k2;
5359 d1 = TYPE_NAME (t1);
5360 d2 = TYPE_NAME (t2);
5362 return (DECL_NAME (d1) == DECL_NAME (d2)
5363 && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5364 && ((TREE_TYPE (t1) != NULL_TREE)
5365 == (TREE_TYPE (t2) != NULL_TREE))
5366 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5367 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5370 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5371 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5372 is non-NULL, this type is being created by the implicit typename
5373 extension, and BASE_TYPE is a type named `t' in some base class of
5374 `T' which depends on template parameters.
5376 Returns the new TYPENAME_TYPE. */
5378 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5380 tree
5381 build_typename_type (tree context, tree name, tree fullname)
5383 tree t;
5384 tree d;
5385 PTR *e;
5387 if (typename_htab == NULL)
5389 typename_htab = htab_create_ggc (61, &typename_hash,
5390 &typename_compare, NULL);
5393 /* Build the TYPENAME_TYPE. */
5394 t = make_aggr_type (TYPENAME_TYPE);
5395 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5396 TYPENAME_TYPE_FULLNAME (t) = fullname;
5398 /* Build the corresponding TYPE_DECL. */
5399 d = build_decl (TYPE_DECL, name, t);
5400 TYPE_NAME (TREE_TYPE (d)) = d;
5401 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5402 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5403 DECL_ARTIFICIAL (d) = 1;
5405 /* See if we already have this type. */
5406 e = htab_find_slot (typename_htab, t, INSERT);
5407 if (*e)
5408 t = (tree) *e;
5409 else
5410 *e = t;
5412 return t;
5415 /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type,
5416 unless an error occurs, in which case error_mark_node is returned.
5417 If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5418 set, we return that, rather than the _TYPE it corresponds to, in
5419 other cases we look through the type decl. If TF_ERROR is set,
5420 complain about errors, otherwise be quiet. */
5422 tree
5423 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5425 tree fullname;
5427 if (TYPE_P (name))
5429 if (!(TYPE_LANG_SPECIFIC (name)
5430 && (CLASSTYPE_IS_TEMPLATE (name)
5431 || CLASSTYPE_USE_TEMPLATE (name))))
5432 name = TYPE_IDENTIFIER (name);
5433 else
5434 /* Create a TEMPLATE_ID_EXPR for the type. */
5435 name = build_nt (TEMPLATE_ID_EXPR,
5436 CLASSTYPE_TI_TEMPLATE (name),
5437 CLASSTYPE_TI_ARGS (name));
5439 else if (TREE_CODE (name) == TYPE_DECL)
5440 name = DECL_NAME (name);
5442 fullname = name;
5444 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5446 name = TREE_OPERAND (name, 0);
5447 if (TREE_CODE (name) == TEMPLATE_DECL)
5448 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5450 if (TREE_CODE (name) == TEMPLATE_DECL)
5452 error ("`%D' used without template parameters", name);
5453 return error_mark_node;
5455 if (TREE_CODE (name) != IDENTIFIER_NODE)
5456 abort ();
5458 if (TREE_CODE (context) == NAMESPACE_DECL)
5460 /* We can get here from typename_sub0 in the explicit_template_type
5461 expansion. Just fail. */
5462 if (complain & tf_error)
5463 error ("no class template named `%#T' in `%#T'",
5464 name, context);
5465 return error_mark_node;
5468 if (! uses_template_parms (context)
5469 || currently_open_class (context))
5471 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5473 tree tmpl = NULL_TREE;
5474 if (IS_AGGR_TYPE (context))
5475 tmpl = lookup_field (context, name, 0, false);
5476 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5478 if (complain & tf_error)
5479 error ("no class template named `%#T' in `%#T'",
5480 name, context);
5481 return error_mark_node;
5484 if (complain & tf_error)
5486 if (complain & tf_parsing)
5487 perform_or_defer_access_check (context, tmpl);
5488 else
5489 enforce_access (context, tmpl);
5492 return lookup_template_class (tmpl,
5493 TREE_OPERAND (fullname, 1),
5494 NULL_TREE, context,
5495 /*entering_scope=*/0,
5496 tf_error | tf_warning);
5498 else
5500 tree t;
5502 if (!IS_AGGR_TYPE (context))
5504 if (complain & tf_error)
5505 error ("no type named `%#T' in `%#T'", name, context);
5506 return error_mark_node;
5509 t = lookup_field (context, name, 0, true);
5510 if (t)
5512 if (TREE_CODE (t) != TYPE_DECL)
5514 if (complain & tf_error)
5515 error ("no type named `%#T' in `%#T'", name, context);
5516 return error_mark_node;
5519 if (complain & tf_error)
5521 if (complain & tf_parsing)
5522 perform_or_defer_access_check (context, t);
5523 else
5524 enforce_access (context, t);
5527 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5528 t = TREE_TYPE (t);
5530 return t;
5535 /* If the CONTEXT is not a template type, then either the field is
5536 there now or its never going to be. */
5537 if (!uses_template_parms (context))
5539 if (complain & tf_error)
5540 error ("no type named `%#T' in `%#T'", name, context);
5541 return error_mark_node;
5544 return build_typename_type (context, name, fullname);
5547 /* Resolve `CONTEXT::template NAME'. Returns an appropriate type,
5548 unless an error occurs, in which case error_mark_node is returned.
5549 If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5550 corresponds to. If COMPLAIN zero, don't complain about any errors
5551 that occur. */
5553 tree
5554 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5556 tree t;
5557 tree d;
5559 if (TYPE_P (name))
5560 name = TYPE_IDENTIFIER (name);
5561 else if (DECL_P (name))
5562 name = DECL_NAME (name);
5563 if (TREE_CODE (name) != IDENTIFIER_NODE)
5564 abort ();
5566 if (!uses_template_parms (context)
5567 || currently_open_class (context))
5569 tree tmpl = NULL_TREE;
5571 if (IS_AGGR_TYPE (context))
5572 tmpl = lookup_field (context, name, 0, false);
5574 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5576 if (complain & tf_error)
5577 error ("no class template named `%#T' in `%#T'", name, context);
5578 return error_mark_node;
5581 if (complain & tf_error)
5583 if (complain & tf_parsing)
5584 perform_or_defer_access_check (context, tmpl);
5585 else
5586 enforce_access (context, tmpl);
5589 return tmpl;
5592 /* Build the UNBOUND_CLASS_TEMPLATE. */
5593 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5594 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5595 TREE_TYPE (t) = NULL_TREE;
5597 /* Build the corresponding TEMPLATE_DECL. */
5598 d = build_decl (TEMPLATE_DECL, name, t);
5599 TYPE_NAME (TREE_TYPE (d)) = d;
5600 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5601 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5602 DECL_ARTIFICIAL (d) = 1;
5604 return t;
5607 /* Select the right _DECL from multiple choices. */
5609 static tree
5610 select_decl (cxx_binding *binding, int flags)
5612 tree val;
5613 val = BINDING_VALUE (binding);
5615 timevar_push (TV_NAME_LOOKUP);
5616 if (LOOKUP_NAMESPACES_ONLY (flags))
5618 /* We are not interested in types. */
5619 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5620 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5621 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5624 /* If we could have a type and
5625 we have nothing or we need a type and have none. */
5626 if (BINDING_TYPE (binding)
5627 && (!val || ((flags & LOOKUP_PREFER_TYPES)
5628 && TREE_CODE (val) != TYPE_DECL)))
5629 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5630 /* Don't return non-types if we really prefer types. */
5631 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
5632 && (TREE_CODE (val) != TEMPLATE_DECL
5633 || !DECL_CLASS_TEMPLATE_P (val)))
5634 val = NULL_TREE;
5636 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5639 /* Unscoped lookup of a global: iterate over current namespaces,
5640 considering using-directives. If SPACESP is non-NULL, store a list
5641 of the namespaces we've considered in it. */
5643 tree
5644 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5646 tree initial = current_decl_namespace ();
5647 tree scope = initial;
5648 tree siter;
5649 struct cp_binding_level *level;
5650 tree val = NULL_TREE;
5651 cxx_binding binding;
5653 timevar_push (TV_NAME_LOOKUP);
5654 cxx_binding_clear (&binding);
5655 if (spacesp)
5656 *spacesp = NULL_TREE;
5658 for (; !val; scope = CP_DECL_CONTEXT (scope))
5660 cxx_binding *b =
5661 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
5662 if (spacesp)
5663 *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5665 /* Ignore anticipated built-in functions. */
5666 if (b && BINDING_VALUE (b)
5667 && DECL_P (BINDING_VALUE (b))
5668 && DECL_LANG_SPECIFIC (BINDING_VALUE (b))
5669 && DECL_ANTICIPATED (BINDING_VALUE (b)))
5670 /* Keep binding cleared. */;
5671 else if (b)
5673 /* Initialize binding for this context. */
5674 binding.value = BINDING_VALUE (b);
5675 binding.type = BINDING_TYPE (b);
5678 /* Add all _DECLs seen through local using-directives. */
5679 for (level = current_binding_level;
5680 !level->namespace_p;
5681 level = level->level_chain)
5682 if (!lookup_using_namespace (name, &binding, level->using_directives,
5683 scope, flags, spacesp))
5684 /* Give up because of error. */
5685 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5687 /* Add all _DECLs seen through global using-directives. */
5688 /* XXX local and global using lists should work equally. */
5689 siter = initial;
5690 while (1)
5692 if (!lookup_using_namespace (name, &binding,
5693 DECL_NAMESPACE_USING (siter),
5694 scope, flags, spacesp))
5695 /* Give up because of error. */
5696 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5697 if (siter == scope) break;
5698 siter = CP_DECL_CONTEXT (siter);
5701 val = select_decl (&binding, flags);
5702 if (scope == global_namespace)
5703 break;
5705 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5708 /* Combine prefer_type and namespaces_only into flags. */
5710 static int
5711 lookup_flags (int prefer_type, int namespaces_only)
5713 if (namespaces_only)
5714 return LOOKUP_PREFER_NAMESPACES;
5715 if (prefer_type > 1)
5716 return LOOKUP_PREFER_TYPES;
5717 if (prefer_type > 0)
5718 return LOOKUP_PREFER_BOTH;
5719 return 0;
5722 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5723 ignore it or not. Subroutine of lookup_name_real. */
5725 static tree
5726 qualify_lookup (tree val, int flags)
5728 if (val == NULL_TREE)
5729 return val;
5730 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5731 return val;
5732 if ((flags & LOOKUP_PREFER_TYPES) && TREE_CODE (val) == TYPE_DECL)
5733 return val;
5734 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5735 return NULL_TREE;
5736 return val;
5739 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5740 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
5741 bindings.
5743 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5744 declaration found. */
5746 tree
5747 lookup_qualified_name (tree scope, tree name, bool is_type_p, int flags)
5749 if (TREE_CODE (scope) == NAMESPACE_DECL)
5751 cxx_binding binding;
5753 cxx_binding_clear (&binding);
5754 flags |= LOOKUP_COMPLAIN;
5755 if (is_type_p)
5756 flags |= LOOKUP_PREFER_TYPES;
5757 if (!qualified_lookup_using_namespace (name, scope, &binding, flags))
5758 return NULL_TREE;
5759 return select_decl (&binding, flags);
5761 else
5762 return lookup_member (scope, name, 0, is_type_p);
5765 /* Check to see whether or not DECL is a variable that would have been
5766 in scope under the ARM, but is not in scope under the ANSI/ISO
5767 standard. If so, issue an error message. If name lookup would
5768 work in both cases, but return a different result, this function
5769 returns the result of ANSI/ISO lookup. Otherwise, it returns
5770 DECL. */
5772 tree
5773 check_for_out_of_scope_variable (tree decl)
5775 tree shadowed;
5777 /* We only care about out of scope variables. */
5778 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5779 return decl;
5781 shadowed = DECL_SHADOWED_FOR_VAR (decl);
5782 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5783 && DECL_DEAD_FOR_LOCAL (shadowed))
5784 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5785 if (!shadowed)
5786 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5787 if (shadowed)
5789 if (!DECL_ERROR_REPORTED (decl))
5791 warning ("name lookup of `%D' changed",
5792 DECL_NAME (decl));
5793 cp_warning_at (" matches this `%D' under ISO standard rules",
5794 shadowed);
5795 cp_warning_at (" matches this `%D' under old rules", decl);
5796 DECL_ERROR_REPORTED (decl) = 1;
5798 return shadowed;
5801 /* If we have already complained about this declaration, there's no
5802 need to do it again. */
5803 if (DECL_ERROR_REPORTED (decl))
5804 return decl;
5806 DECL_ERROR_REPORTED (decl) = 1;
5807 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5809 error ("name lookup of `%D' changed for new ISO `for' scoping",
5810 DECL_NAME (decl));
5811 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl);
5812 return error_mark_node;
5814 else
5816 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5817 DECL_NAME (decl));
5818 cp_pedwarn_at (" using obsolete binding at `%D'", decl);
5821 return decl;
5824 /* Look up NAME in the current binding level and its superiors in the
5825 namespace of variables, functions and typedefs. Return a ..._DECL
5826 node of some kind representing its definition if there is only one
5827 such declaration, or return a TREE_LIST with all the overloaded
5828 definitions if there are many, or return 0 if it is undefined.
5830 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5831 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5832 Otherwise we prefer non-TYPE_DECLs.
5834 If NONCLASS is nonzero, we don't look for the NAME in class scope,
5835 using IDENTIFIER_CLASS_VALUE. */
5837 tree
5838 lookup_name_real (tree name, int prefer_type, int nonclass,
5839 int namespaces_only, int flags)
5841 cxx_binding *iter;
5842 tree val = NULL_TREE;
5844 timevar_push (TV_NAME_LOOKUP);
5845 /* Conversion operators are handled specially because ordinary
5846 unqualified name lookup will not find template conversion
5847 operators. */
5848 if (IDENTIFIER_TYPENAME_P (name))
5850 struct cp_binding_level *level;
5852 for (level = current_binding_level;
5853 level && !level->namespace_p;
5854 level = level->level_chain)
5856 tree class_type;
5857 tree operators;
5859 /* A conversion operator can only be declared in a class
5860 scope. */
5861 if (level->parm_flag != 2)
5862 continue;
5864 /* Lookup the conversion operator in the class. */
5865 class_type = level->this_class;
5866 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5867 if (operators)
5868 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
5871 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5874 flags |= lookup_flags (prefer_type, namespaces_only);
5876 /* First, look in non-namespace scopes. */
5878 if (current_class_type == NULL_TREE)
5879 nonclass = 1;
5881 for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
5883 tree binding;
5885 if (!LOCAL_BINDING_P (iter) && nonclass)
5886 /* We're not looking for class-scoped bindings, so keep going. */
5887 continue;
5889 /* If this is the kind of thing we're looking for, we're done. */
5890 if (qualify_lookup (BINDING_VALUE (iter), flags))
5891 binding = BINDING_VALUE (iter);
5892 else if ((flags & LOOKUP_PREFER_TYPES)
5893 && qualify_lookup (BINDING_TYPE (iter), flags))
5894 binding = BINDING_TYPE (iter);
5895 else
5896 binding = NULL_TREE;
5898 if (binding)
5900 val = binding;
5901 break;
5905 /* Now lookup in namespace scopes. */
5906 if (!val)
5908 tree t = unqualified_namespace_lookup (name, flags, 0);
5909 if (t)
5910 val = t;
5913 if (val)
5915 /* If we have a single function from a using decl, pull it out. */
5916 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5917 val = OVL_FUNCTION (val);
5920 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5923 tree
5924 lookup_name_nonclass (tree name)
5926 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
5929 tree
5930 lookup_function_nonclass (tree name, tree args)
5932 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5935 tree
5936 lookup_name (tree name, int prefer_type)
5938 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
5941 /* Similar to `lookup_name' but look only in the innermost non-class
5942 binding level. */
5944 tree
5945 lookup_name_current_level (tree name)
5947 struct cp_binding_level *b;
5948 tree t = NULL_TREE;
5950 timevar_push (TV_NAME_LOOKUP);
5951 b = current_binding_level;
5952 while (b->parm_flag == 2)
5953 b = b->level_chain;
5955 if (b->namespace_p)
5957 t = IDENTIFIER_NAMESPACE_VALUE (name);
5959 /* extern "C" function() */
5960 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5961 t = TREE_VALUE (t);
5963 else if (IDENTIFIER_BINDING (name)
5964 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5966 while (1)
5968 if (BINDING_SCOPE (IDENTIFIER_BINDING (name)) == b)
5969 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
5971 if (b->keep == 2)
5972 b = b->level_chain;
5973 else
5974 break;
5978 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5981 /* Like lookup_name_current_level, but for types. */
5983 tree
5984 lookup_type_current_level (tree name)
5986 register tree t = NULL_TREE;
5988 timevar_push (TV_NAME_LOOKUP);
5989 my_friendly_assert (! current_binding_level->namespace_p, 980716);
5991 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5992 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5994 struct cp_binding_level *b = current_binding_level;
5995 while (1)
5997 if (purpose_member (name, b->type_shadowed))
5998 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
5999 REAL_IDENTIFIER_TYPE_VALUE (name));
6000 if (b->keep == 2)
6001 b = b->level_chain;
6002 else
6003 break;
6007 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6011 /* Push the declarations of builtin types into the namespace.
6012 RID_INDEX is the index of the builtin type
6013 in the array RID_POINTERS. NAME is the name used when looking
6014 up the builtin type. TYPE is the _TYPE node for the builtin type. */
6016 void
6017 record_builtin_type (enum rid rid_index,
6018 const char* name,
6019 tree type)
6021 tree rname = NULL_TREE, tname = NULL_TREE;
6022 tree tdecl = NULL_TREE;
6024 if ((int) rid_index < (int) RID_MAX)
6025 rname = ridpointers[(int) rid_index];
6026 if (name)
6027 tname = get_identifier (name);
6029 if (tname)
6031 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6032 set_identifier_type_value (tname, NULL_TREE);
6033 if ((int) rid_index < (int) RID_MAX)
6034 /* Built-in types live in the global namespace. */
6035 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6037 if (rname != NULL_TREE)
6039 if (tname != NULL_TREE)
6041 set_identifier_type_value (rname, NULL_TREE);
6042 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6044 else
6046 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6047 set_identifier_type_value (rname, NULL_TREE);
6052 /* Record one of the standard Java types.
6053 * Declare it as having the given NAME.
6054 * If SIZE > 0, it is the size of one of the integral types;
6055 * otherwise it is the negative of the size of one of the other types. */
6057 static tree
6058 record_builtin_java_type (const char* name, int size)
6060 tree type, decl;
6061 if (size > 0)
6062 type = make_signed_type (size);
6063 else if (size > -32)
6064 { /* "__java_char" or ""__java_boolean". */
6065 type = make_unsigned_type (-size);
6066 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6068 else
6069 { /* "__java_float" or ""__java_double". */
6070 type = make_node (REAL_TYPE);
6071 TYPE_PRECISION (type) = - size;
6072 layout_type (type);
6074 record_builtin_type (RID_MAX, name, type);
6075 decl = TYPE_NAME (type);
6077 /* Suppress generate debug symbol entries for these types,
6078 since for normal C++ they are just clutter.
6079 However, push_lang_context undoes this if extern "Java" is seen. */
6080 DECL_IGNORED_P (decl) = 1;
6082 TYPE_FOR_JAVA (type) = 1;
6083 return type;
6086 /* Push a type into the namespace so that the back-ends ignore it. */
6088 static void
6089 record_unknown_type (tree type, const char* name)
6091 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6092 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
6093 DECL_IGNORED_P (decl) = 1;
6094 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6095 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6096 TYPE_ALIGN (type) = 1;
6097 TYPE_USER_ALIGN (type) = 0;
6098 TYPE_MODE (type) = TYPE_MODE (void_type_node);
6101 /* An string for which we should create an IDENTIFIER_NODE at
6102 startup. */
6104 typedef struct predefined_identifier
6106 /* The name of the identifier. */
6107 const char *const name;
6108 /* The place where the IDENTIFIER_NODE should be stored. */
6109 tree *const node;
6110 /* Nonzero if this is the name of a constructor or destructor. */
6111 const int ctor_or_dtor_p;
6112 } predefined_identifier;
6114 /* Create all the predefined identifiers. */
6116 static void
6117 initialize_predefined_identifiers (void)
6119 const predefined_identifier *pid;
6121 /* A table of identifiers to create at startup. */
6122 static const predefined_identifier predefined_identifiers[] = {
6123 { "C++", &lang_name_cplusplus, 0 },
6124 { "C", &lang_name_c, 0 },
6125 { "Java", &lang_name_java, 0 },
6126 { CTOR_NAME, &ctor_identifier, 1 },
6127 { "__base_ctor", &base_ctor_identifier, 1 },
6128 { "__comp_ctor", &complete_ctor_identifier, 1 },
6129 { DTOR_NAME, &dtor_identifier, 1 },
6130 { "__comp_dtor", &complete_dtor_identifier, 1 },
6131 { "__base_dtor", &base_dtor_identifier, 1 },
6132 { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6133 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6134 { "nelts", &nelts_identifier, 0 },
6135 { THIS_NAME, &this_identifier, 0 },
6136 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6137 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6138 { "_vptr", &vptr_identifier, 0 },
6139 { "__vtt_parm", &vtt_parm_identifier, 0 },
6140 { "std", &std_identifier, 0 },
6141 { NULL, NULL, 0 }
6144 for (pid = predefined_identifiers; pid->name; ++pid)
6146 *pid->node = get_identifier (pid->name);
6147 if (pid->ctor_or_dtor_p)
6148 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6152 /* Create the predefined scalar types of C,
6153 and some nodes representing standard constants (0, 1, (void *)0).
6154 Initialize the global binding level.
6155 Make definitions for built-in primitive functions. */
6157 void
6158 cxx_init_decl_processing (void)
6160 tree void_ftype;
6161 tree void_ftype_ptr;
6163 /* Create all the identifiers we need. */
6164 initialize_predefined_identifiers ();
6166 /* Fill in back-end hooks. */
6167 lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6169 /* Create the global variables. */
6170 push_to_top_level ();
6172 /* Enter the global namespace. */
6173 my_friendly_assert (global_namespace == NULL_TREE, 375);
6174 push_namespace (get_identifier ("::"));
6175 global_namespace = current_namespace;
6176 current_lang_name = NULL_TREE;
6178 /* Adjust various flags based on command-line settings. */
6179 if (! flag_permissive && ! pedantic)
6180 flag_pedantic_errors = 1;
6181 if (!flag_no_inline)
6183 flag_inline_trees = 1;
6184 flag_no_inline = 1;
6186 if (flag_inline_functions)
6188 flag_inline_trees = 2;
6189 flag_inline_functions = 0;
6192 /* Force minimum function alignment if using the least significant
6193 bit of function pointers to store the virtual bit. */
6194 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6195 && force_align_functions_log < 1)
6196 force_align_functions_log = 1;
6198 /* Initially, C. */
6199 current_lang_name = lang_name_c;
6201 current_function_decl = NULL_TREE;
6202 current_binding_level = NULL_BINDING_LEVEL;
6203 free_binding_level = NULL_BINDING_LEVEL;
6205 build_common_tree_nodes (flag_signed_char);
6207 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6208 TREE_TYPE (error_mark_list) = error_mark_node;
6210 /* Make the binding_level structure for global names. */
6211 pushlevel (0);
6212 global_binding_level = current_binding_level;
6213 /* The global level is the namespace level of ::. */
6214 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6215 declare_namespace_level ();
6217 VARRAY_TREE_INIT (global_binding_level->static_decls,
6218 200,
6219 "Static declarations");
6221 /* Create the `std' namespace. */
6222 push_namespace (std_identifier);
6223 std_node = current_namespace;
6224 pop_namespace ();
6226 c_common_nodes_and_builtins ();
6228 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6229 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6230 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6231 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6232 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6233 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6234 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6235 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6237 integer_two_node = build_int_2 (2, 0);
6238 TREE_TYPE (integer_two_node) = integer_type_node;
6239 integer_three_node = build_int_2 (3, 0);
6240 TREE_TYPE (integer_three_node) = integer_type_node;
6242 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6243 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6244 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6245 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6246 TYPE_PRECISION (boolean_type_node) = 1;
6247 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6248 boolean_false_node = build_int_2 (0, 0);
6249 TREE_TYPE (boolean_false_node) = boolean_type_node;
6250 boolean_true_node = build_int_2 (1, 0);
6251 TREE_TYPE (boolean_true_node) = boolean_type_node;
6253 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6255 #if 0
6256 record_builtin_type (RID_MAX, NULL, string_type_node);
6257 #endif
6259 delta_type_node = ptrdiff_type_node;
6260 vtable_index_type = ptrdiff_type_node;
6262 vtt_parm_type = build_pointer_type (const_ptr_type_node);
6263 void_ftype = build_function_type (void_type_node, void_list_node);
6264 void_ftype_ptr = build_function_type (void_type_node,
6265 tree_cons (NULL_TREE,
6266 ptr_type_node,
6267 void_list_node));
6268 void_ftype_ptr
6269 = build_exception_variant (void_ftype_ptr, empty_except_spec);
6271 /* C++ extensions */
6273 unknown_type_node = make_node (UNKNOWN_TYPE);
6274 record_unknown_type (unknown_type_node, "unknown type");
6276 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6277 TREE_TYPE (unknown_type_node) = unknown_type_node;
6279 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6280 result. */
6281 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6282 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6285 /* Make sure we get a unique function type, so we can give
6286 its pointer type a name. (This wins for gdb.) */
6287 tree vfunc_type = make_node (FUNCTION_TYPE);
6288 TREE_TYPE (vfunc_type) = integer_type_node;
6289 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6290 layout_type (vfunc_type);
6292 vtable_entry_type = build_pointer_type (vfunc_type);
6294 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6296 vtbl_type_node
6297 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6298 layout_type (vtbl_type_node);
6299 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6300 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6301 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6302 layout_type (vtbl_ptr_type_node);
6303 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6305 push_namespace (get_identifier ("__cxxabiv1"));
6306 abi_node = current_namespace;
6307 pop_namespace ();
6309 global_type_node = make_node (LANG_TYPE);
6310 record_unknown_type (global_type_node, "global type");
6312 /* Now, C++. */
6313 current_lang_name = lang_name_cplusplus;
6316 tree bad_alloc_type_node, newtype, deltype;
6317 tree ptr_ftype_sizetype;
6319 push_namespace (std_identifier);
6320 bad_alloc_type_node
6321 = xref_tag (class_type, get_identifier ("bad_alloc"),
6322 /*attributes=*/NULL_TREE, 1);
6323 pop_namespace ();
6324 ptr_ftype_sizetype
6325 = build_function_type (ptr_type_node,
6326 tree_cons (NULL_TREE,
6327 size_type_node,
6328 void_list_node));
6329 newtype = build_exception_variant
6330 (ptr_ftype_sizetype, add_exception_specifier
6331 (NULL_TREE, bad_alloc_type_node, -1));
6332 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6333 push_cp_library_fn (NEW_EXPR, newtype);
6334 push_cp_library_fn (VEC_NEW_EXPR, newtype);
6335 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6336 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6339 abort_fndecl
6340 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6342 /* Perform other language dependent initializations. */
6343 init_class_processing ();
6344 init_search_processing ();
6345 init_rtti_processing ();
6347 if (flag_exceptions)
6348 init_exception_processing ();
6350 if (! supports_one_only ())
6351 flag_weak = 0;
6353 make_fname_decl = cp_make_fname_decl;
6354 start_fname_decls ();
6356 /* Show we use EH for cleanups. */
6357 using_eh_for_cleanups ();
6359 /* Maintain consistency. Perhaps we should just complain if they
6360 say -fwritable-strings? */
6361 if (flag_writable_strings)
6362 flag_const_strings = 0;
6365 /* Generate an initializer for a function naming variable from
6366 NAME. NAME may be NULL, in which case we generate a special
6367 ERROR_MARK node which should be replaced later. */
6369 tree
6370 cp_fname_init (const char* name)
6372 tree domain = NULL_TREE;
6373 tree type;
6374 tree init = NULL_TREE;
6375 size_t length = 0;
6377 if (name)
6379 length = strlen (name);
6380 domain = build_index_type (size_int (length));
6381 init = build_string (length + 1, name);
6384 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6385 type = build_cplus_array_type (type, domain);
6387 if (init)
6388 TREE_TYPE (init) = type;
6389 else
6390 /* We don't know the value until instantiation time. Make
6391 something which will be digested now, but replaced later. */
6392 init = build (ERROR_MARK, type);
6394 return init;
6397 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6398 decl, NAME is the initialization string and TYPE_DEP indicates whether
6399 NAME depended on the type of the function. We make use of that to detect
6400 __PRETTY_FUNCTION__ inside a template fn. This is being done
6401 lazily at the point of first use, so we musn't push the decl now. */
6403 static tree
6404 cp_make_fname_decl (tree id, int type_dep)
6406 const char *const name = (type_dep && processing_template_decl
6407 ? NULL : fname_as_string (type_dep));
6408 tree init = cp_fname_init (name);
6409 tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6411 /* As we're using pushdecl_with_scope, we must set the context. */
6412 DECL_CONTEXT (decl) = current_function_decl;
6413 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6415 TREE_STATIC (decl) = 1;
6416 TREE_READONLY (decl) = 1;
6417 DECL_ARTIFICIAL (decl) = 1;
6418 DECL_INITIAL (decl) = init;
6420 TREE_USED (decl) = 1;
6422 if (current_function_decl)
6424 struct cp_binding_level *b = current_binding_level;
6425 while (b->level_chain->parm_flag == 0)
6426 b = b->level_chain;
6427 pushdecl_with_scope (decl, b);
6430 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6432 return decl;
6435 /* Make a definition for a builtin function named NAME in the current
6436 namespace, whose data type is TYPE and whose context is CONTEXT.
6437 TYPE should be a function type with argument types.
6439 CLASS and CODE tell later passes how to compile calls to this function.
6440 See tree.h for possible values.
6442 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6443 the name to be called if we can't opencode the function.
6444 If ATTRS is nonzero, use that for the function's attribute
6445 list. */
6447 static tree
6448 builtin_function_1 (const char* name,
6449 tree type,
6450 tree context,
6451 int code,
6452 enum built_in_class class,
6453 const char* libname,
6454 tree attrs)
6456 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6457 DECL_BUILT_IN_CLASS (decl) = class;
6458 DECL_FUNCTION_CODE (decl) = code;
6459 DECL_CONTEXT (decl) = context;
6461 pushdecl (decl);
6463 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6464 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6465 function in the namespace. */
6466 if (libname)
6467 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6468 make_decl_rtl (decl, NULL);
6470 /* Warn if a function in the namespace for users
6471 is used without an occasion to consider it declared. */
6472 if (name[0] != '_' || name[1] != '_')
6473 DECL_ANTICIPATED (decl) = 1;
6475 /* Possibly apply some default attributes to this built-in function. */
6476 if (attrs)
6477 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6478 else
6479 decl_attributes (&decl, NULL_TREE, 0);
6481 return decl;
6484 /* Entry point for the benefit of c_common_nodes_and_builtins.
6486 Make a defintion for a builtin function named NAME and whose data type
6487 is TYPE. TYPE should be a function type with argument types. This
6488 function places the anticipated declaration in the global namespace
6489 and additionally in the std namespace if appropriate.
6491 CLASS and CODE tell later passes how to compile calls to this function.
6492 See tree.h for possible values.
6494 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6495 the name to be called if we can't opencode the function.
6497 If ATTRS is nonzero, use that for the function's attribute
6498 list. */
6500 tree
6501 builtin_function (const char* name,
6502 tree type,
6503 int code,
6504 enum built_in_class class,
6505 const char* libname,
6506 tree attrs)
6508 /* All builtins that don't begin with an '_' should additionally
6509 go in the 'std' namespace. */
6510 if (name[0] != '_')
6512 push_namespace (std_identifier);
6513 builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6514 pop_namespace ();
6517 return builtin_function_1 (name, type, NULL_TREE, code,
6518 class, libname, attrs);
6521 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6522 function. Not called directly. */
6524 static tree
6525 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6527 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6528 DECL_EXTERNAL (fn) = 1;
6529 TREE_PUBLIC (fn) = 1;
6530 DECL_ARTIFICIAL (fn) = 1;
6531 TREE_NOTHROW (fn) = 1;
6532 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6533 SET_DECL_LANGUAGE (fn, lang_c);
6534 return fn;
6537 /* Returns the _DECL for a library function with C linkage.
6538 We assume that such functions never throw; if this is incorrect,
6539 callers should unset TREE_NOTHROW. */
6541 tree
6542 build_library_fn (tree name, tree type)
6544 return build_library_fn_1 (name, ERROR_MARK, type);
6547 /* Returns the _DECL for a library function with C++ linkage. */
6549 static tree
6550 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6552 tree fn = build_library_fn_1 (name, operator_code, type);
6553 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6554 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6555 SET_DECL_LANGUAGE (fn, lang_cplusplus);
6556 set_mangled_name_for_decl (fn);
6557 return fn;
6560 /* Like build_library_fn, but takes a C string instead of an
6561 IDENTIFIER_NODE. */
6563 tree
6564 build_library_fn_ptr (const char* name, tree type)
6566 return build_library_fn (get_identifier (name), type);
6569 /* Like build_cp_library_fn, but takes a C string instead of an
6570 IDENTIFIER_NODE. */
6572 tree
6573 build_cp_library_fn_ptr (const char* name, tree type)
6575 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6578 /* Like build_library_fn, but also pushes the function so that we will
6579 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
6581 tree
6582 push_library_fn (tree name, tree type)
6584 tree fn = build_library_fn (name, type);
6585 pushdecl_top_level (fn);
6586 return fn;
6589 /* Like build_cp_library_fn, but also pushes the function so that it
6590 will be found by normal lookup. */
6592 static tree
6593 push_cp_library_fn (enum tree_code operator_code, tree type)
6595 tree fn = build_cp_library_fn (ansi_opname (operator_code),
6596 operator_code,
6597 type);
6598 pushdecl (fn);
6599 return fn;
6602 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6603 a FUNCTION_TYPE. */
6605 tree
6606 push_void_library_fn (tree name, tree parmtypes)
6608 tree type = build_function_type (void_type_node, parmtypes);
6609 return push_library_fn (name, type);
6612 /* Like push_library_fn, but also note that this function throws
6613 and does not return. Used for __throw_foo and the like. */
6615 tree
6616 push_throw_library_fn (tree name, tree type)
6618 tree fn = push_library_fn (name, type);
6619 TREE_THIS_VOLATILE (fn) = 1;
6620 TREE_NOTHROW (fn) = 0;
6621 return fn;
6624 /* Apply default attributes to a function, if a system function with default
6625 attributes. */
6627 void
6628 cxx_insert_default_attributes (tree decl)
6630 if (!DECL_EXTERN_C_FUNCTION_P (decl))
6631 return;
6632 if (!TREE_PUBLIC (decl))
6633 return;
6634 c_common_insert_default_attributes (decl);
6637 /* When we call finish_struct for an anonymous union, we create
6638 default copy constructors and such. But, an anonymous union
6639 shouldn't have such things; this function undoes the damage to the
6640 anonymous union type T.
6642 (The reason that we create the synthesized methods is that we don't
6643 distinguish `union { int i; }' from `typedef union { int i; } U'.
6644 The first is an anonymous union; the second is just an ordinary
6645 union type.) */
6647 void
6648 fixup_anonymous_aggr (tree t)
6650 tree *q;
6652 /* Wipe out memory of synthesized methods */
6653 TYPE_HAS_CONSTRUCTOR (t) = 0;
6654 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6655 TYPE_HAS_INIT_REF (t) = 0;
6656 TYPE_HAS_CONST_INIT_REF (t) = 0;
6657 TYPE_HAS_ASSIGN_REF (t) = 0;
6658 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6660 /* Splice the implicitly generated functions out of the TYPE_METHODS
6661 list. */
6662 q = &TYPE_METHODS (t);
6663 while (*q)
6665 if (DECL_ARTIFICIAL (*q))
6666 *q = TREE_CHAIN (*q);
6667 else
6668 q = &TREE_CHAIN (*q);
6671 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
6672 if (TYPE_METHODS (t))
6673 cp_error_at ("an anonymous union cannot have function members", t);
6675 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6676 assignment operators (because they cannot have these methods themselves).
6677 For anonymous unions this is already checked because they are not allowed
6678 in any union, otherwise we have to check it. */
6679 if (TREE_CODE (t) != UNION_TYPE)
6681 tree field, type;
6683 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6684 if (TREE_CODE (field) == FIELD_DECL)
6686 type = TREE_TYPE (field);
6687 if (CLASS_TYPE_P (type))
6689 if (TYPE_NEEDS_CONSTRUCTING (type))
6690 cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6691 field);
6692 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6693 cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6694 field);
6695 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6696 cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6697 field);
6703 /* Make sure that a declaration with no declarator is well-formed, i.e.
6704 just declares a tagged type or anonymous union.
6706 Returns the type declared; or NULL_TREE if none. */
6708 tree
6709 check_tag_decl (tree declspecs)
6711 int found_type = 0;
6712 int saw_friend = 0;
6713 int saw_typedef = 0;
6714 tree ob_modifier = NULL_TREE;
6715 register tree link;
6716 /* If a class, struct, or enum type is declared by the DECLSPECS
6717 (i.e, if a class-specifier, enum-specifier, or non-typename
6718 elaborated-type-specifier appears in the DECLSPECS),
6719 DECLARED_TYPE is set to the corresponding type. */
6720 tree declared_type = NULL_TREE;
6721 bool error_p = false;
6723 for (link = declspecs; link; link = TREE_CHAIN (link))
6725 tree value = TREE_VALUE (link);
6727 if (TYPE_P (value)
6728 || TREE_CODE (value) == TYPE_DECL
6729 || (TREE_CODE (value) == IDENTIFIER_NODE
6730 && IDENTIFIER_GLOBAL_VALUE (value)
6731 && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6733 ++found_type;
6735 if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6737 if (! in_system_header)
6738 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6739 return NULL_TREE;
6742 if (TYPE_P (value)
6743 && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6744 || TREE_CODE (value) == ENUMERAL_TYPE))
6746 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6747 declared_type = value;
6750 else if (value == ridpointers[(int) RID_TYPEDEF])
6751 saw_typedef = 1;
6752 else if (value == ridpointers[(int) RID_FRIEND])
6754 if (current_class_type == NULL_TREE
6755 || current_scope () != current_class_type)
6756 ob_modifier = value;
6757 else
6758 saw_friend = 1;
6760 else if (value == ridpointers[(int) RID_STATIC]
6761 || value == ridpointers[(int) RID_EXTERN]
6762 || value == ridpointers[(int) RID_AUTO]
6763 || value == ridpointers[(int) RID_REGISTER]
6764 || value == ridpointers[(int) RID_INLINE]
6765 || value == ridpointers[(int) RID_VIRTUAL]
6766 || value == ridpointers[(int) RID_CONST]
6767 || value == ridpointers[(int) RID_VOLATILE]
6768 || value == ridpointers[(int) RID_EXPLICIT]
6769 || value == ridpointers[(int) RID_THREAD])
6770 ob_modifier = value;
6771 else if (value == error_mark_node)
6772 error_p = true;
6775 if (found_type > 1)
6776 error ("multiple types in one declaration");
6778 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6779 pedwarn ("declaration does not declare anything");
6780 /* Check for an anonymous union. */
6781 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6782 && TYPE_ANONYMOUS_P (declared_type))
6784 /* 7/3 In a simple-declaration, the optional init-declarator-list
6785 can be omitted only when declaring a class (clause 9) or
6786 enumeration (7.2), that is, when the decl-specifier-seq contains
6787 either a class-specifier, an elaborated-type-specifier with
6788 a class-key (9.1), or an enum-specifier. In these cases and
6789 whenever a class-specifier or enum-specifier is present in the
6790 decl-specifier-seq, the identifiers in these specifiers are among
6791 the names being declared by the declaration (as class-name,
6792 enum-names, or enumerators, depending on the syntax). In such
6793 cases, and except for the declaration of an unnamed bit-field (9.6),
6794 the decl-specifier-seq shall introduce one or more names into the
6795 program, or shall redeclare a name introduced by a previous
6796 declaration. [Example:
6797 enum { }; // ill-formed
6798 typedef class { }; // ill-formed
6799 --end example] */
6800 if (saw_typedef)
6802 error ("missing type-name in typedef-declaration");
6803 return NULL_TREE;
6805 /* Anonymous unions are objects, so they can have specifiers. */;
6806 SET_ANON_AGGR_TYPE_P (declared_type);
6808 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
6809 && !in_system_header)
6810 pedwarn ("ISO C++ prohibits anonymous structs");
6813 else if (ob_modifier)
6815 if (ob_modifier == ridpointers[(int) RID_INLINE]
6816 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6817 error ("`%D' can only be specified for functions", ob_modifier);
6818 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6819 error ("`%D' can only be specified inside a class", ob_modifier);
6820 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6821 error ("`%D' can only be specified for constructors",
6822 ob_modifier);
6823 else
6824 error ("`%D' can only be specified for objects and functions",
6825 ob_modifier);
6828 return declared_type;
6831 /* Called when a declaration is seen that contains no names to declare.
6832 If its type is a reference to a structure, union or enum inherited
6833 from a containing scope, shadow that tag name for the current scope
6834 with a forward reference.
6835 If its type defines a new named structure or union
6836 or defines an enum, it is valid but we need not do anything here.
6837 Otherwise, it is an error.
6839 C++: may have to grok the declspecs to learn about static,
6840 complain for anonymous unions.
6842 Returns the TYPE declared -- or NULL_TREE if none. */
6844 tree
6845 shadow_tag (tree declspecs)
6847 tree t = check_tag_decl (declspecs);
6849 if (!t)
6850 return NULL_TREE;
6852 maybe_process_partial_specialization (t);
6854 /* This is where the variables in an anonymous union are
6855 declared. An anonymous union declaration looks like:
6856 union { ... } ;
6857 because there is no declarator after the union, the parser
6858 sends that declaration here. */
6859 if (ANON_AGGR_TYPE_P (t))
6861 fixup_anonymous_aggr (t);
6863 if (TYPE_FIELDS (t))
6865 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6866 NULL);
6867 finish_anon_union (decl);
6871 return t;
6874 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6876 tree
6877 groktypename (tree typename)
6879 tree specs, attrs;
6880 tree type;
6881 if (TREE_CODE (typename) != TREE_LIST)
6882 return typename;
6883 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6884 type = grokdeclarator (TREE_VALUE (typename), specs,
6885 TYPENAME, 0, &attrs);
6886 if (attrs)
6887 cplus_decl_attributes (&type, attrs, 0);
6888 return type;
6891 /* Decode a declarator in an ordinary declaration or data definition.
6892 This is called as soon as the type information and variable name
6893 have been parsed, before parsing the initializer if any.
6894 Here we create the ..._DECL node, fill in its type,
6895 and put it on the list of decls for the current context.
6896 The ..._DECL node is returned as the value.
6898 Exception: for arrays where the length is not specified,
6899 the type is left null, to be filled in by `cp_finish_decl'.
6901 Function definitions do not come here; they go to start_function
6902 instead. However, external and forward declarations of functions
6903 do go through here. Structure field declarations are done by
6904 grokfield and not through here. */
6906 tree
6907 start_decl (tree declarator,
6908 tree declspecs,
6909 int initialized,
6910 tree attributes,
6911 tree prefix_attributes)
6913 tree decl;
6914 register tree type, tem;
6915 tree context;
6917 /* This should only be done once on the top most decl. */
6918 if (have_extern_spec)
6920 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
6921 declspecs);
6922 have_extern_spec = false;
6925 /* An object declared as __attribute__((deprecated)) suppresses
6926 warnings of uses of other deprecated items. */
6927 if (lookup_attribute ("deprecated", attributes))
6928 deprecated_state = DEPRECATED_SUPPRESS;
6930 attributes = chainon (attributes, prefix_attributes);
6932 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6933 &attributes);
6935 deprecated_state = DEPRECATED_NORMAL;
6937 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
6938 return NULL_TREE;
6940 type = TREE_TYPE (decl);
6942 if (type == error_mark_node)
6943 return NULL_TREE;
6945 context = DECL_CONTEXT (decl);
6947 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6948 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6950 /* When parsing the initializer, lookup should use the object's
6951 namespace. */
6952 push_decl_namespace (context);
6955 /* We are only interested in class contexts, later. */
6956 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6957 context = NULL_TREE;
6959 if (initialized)
6960 /* Is it valid for this decl to have an initializer at all?
6961 If not, set INITIALIZED to zero, which will indirectly
6962 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
6963 switch (TREE_CODE (decl))
6965 case TYPE_DECL:
6966 error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
6967 initialized = 0;
6968 break;
6970 case FUNCTION_DECL:
6971 error ("function `%#D' is initialized like a variable", decl);
6972 initialized = 0;
6973 break;
6975 default:
6976 break;
6979 if (initialized)
6981 if (! toplevel_bindings_p ()
6982 && DECL_EXTERNAL (decl))
6983 warning ("declaration of `%#D' has `extern' and is initialized",
6984 decl);
6985 DECL_EXTERNAL (decl) = 0;
6986 if (toplevel_bindings_p ())
6987 TREE_STATIC (decl) = 1;
6989 /* Tell `pushdecl' this is an initialized decl
6990 even though we don't yet have the initializer expression.
6991 Also tell `cp_finish_decl' it may store the real initializer. */
6992 DECL_INITIAL (decl) = error_mark_node;
6995 /* Set attributes here so if duplicate decl, will have proper attributes. */
6996 cplus_decl_attributes (&decl, attributes, 0);
6998 /* If #pragma weak was used, mark the decl weak now. */
6999 if (current_binding_level == global_binding_level)
7000 maybe_apply_pragma_weak (decl);
7002 if (TREE_CODE (decl) == FUNCTION_DECL
7003 && DECL_DECLARED_INLINE_P (decl)
7004 && DECL_UNINLINABLE (decl)
7005 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
7006 warning ("%Hinline function '%D' given attribute noinline",
7007 &DECL_SOURCE_LOCATION (decl), decl);
7009 if (context && COMPLETE_TYPE_P (complete_type (context)))
7011 push_nested_class (context);
7013 if (TREE_CODE (decl) == VAR_DECL)
7015 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
7016 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7017 error ("`%#D' is not a static member of `%#T'", decl, context);
7018 else
7020 if (DECL_CONTEXT (field) != context)
7022 if (!same_type_p (DECL_CONTEXT (field), context))
7023 pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7024 DECL_CONTEXT (field), DECL_NAME (decl),
7025 context, DECL_NAME (decl));
7026 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7028 /* Static data member are tricky; an in-class initialization
7029 still doesn't provide a definition, so the in-class
7030 declaration will have DECL_EXTERNAL set, but will have an
7031 initialization. Thus, duplicate_decls won't warn
7032 about this situation, and so we check here. */
7033 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7034 error ("duplicate initialization of %D", decl);
7035 if (duplicate_decls (decl, field))
7036 decl = field;
7039 else
7041 tree field = check_classfn (context, decl);
7042 if (field && duplicate_decls (decl, field))
7043 decl = field;
7046 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
7047 DECL_IN_AGGR_P (decl) = 0;
7048 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7049 || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7051 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7052 /* [temp.expl.spec] An explicit specialization of a static data
7053 member of a template is a definition if the declaration
7054 includes an initializer; otherwise, it is a declaration.
7056 We check for processing_specialization so this only applies
7057 to the new specialization syntax. */
7058 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7059 DECL_EXTERNAL (decl) = 1;
7062 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7063 pedwarn ("declaration of `%#D' outside of class is not definition",
7064 decl);
7067 /* Enter this declaration into the symbol table. */
7068 tem = maybe_push_decl (decl);
7070 if (processing_template_decl)
7071 tem = push_template_decl (tem);
7073 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7074 /* Tell the back-end to use or not use .common as appropriate. If we say
7075 -fconserve-space, we want this to save .data space, at the expense of
7076 wrong semantics. If we say -fno-conserve-space, we want this to
7077 produce errors about redefs; to do this we force variables into the
7078 data segment. */
7079 DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7080 || !DECL_THREAD_LOCAL (tem))
7081 && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7082 #endif
7084 if (! processing_template_decl)
7085 start_decl_1 (tem);
7087 return tem;
7090 void
7091 start_decl_1 (tree decl)
7093 tree type = TREE_TYPE (decl);
7094 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7096 if (type == error_mark_node)
7097 return;
7099 maybe_push_cleanup_level (type);
7101 if (initialized)
7102 /* Is it valid for this decl to have an initializer at all?
7103 If not, set INITIALIZED to zero, which will indirectly
7104 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
7106 /* Don't allow initializations for incomplete types except for
7107 arrays which might be completed by the initialization. */
7108 if (COMPLETE_TYPE_P (complete_type (type)))
7109 ; /* A complete type is ok. */
7110 else if (TREE_CODE (type) != ARRAY_TYPE)
7112 error ("variable `%#D' has initializer but incomplete type",
7113 decl);
7114 initialized = 0;
7115 type = TREE_TYPE (decl) = error_mark_node;
7117 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7119 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7120 error ("elements of array `%#D' have incomplete type", decl);
7121 /* else we already gave an error in start_decl. */
7122 initialized = 0;
7126 if (!initialized
7127 && TREE_CODE (decl) != TYPE_DECL
7128 && TREE_CODE (decl) != TEMPLATE_DECL
7129 && type != error_mark_node
7130 && IS_AGGR_TYPE (type)
7131 && ! DECL_EXTERNAL (decl))
7133 if ((! processing_template_decl || ! uses_template_parms (type))
7134 && !COMPLETE_TYPE_P (complete_type (type)))
7136 error ("aggregate `%#D' has incomplete type and cannot be defined",
7137 decl);
7138 /* Change the type so that assemble_variable will give
7139 DECL an rtl we can live with: (mem (const_int 0)). */
7140 type = TREE_TYPE (decl) = error_mark_node;
7142 else
7144 /* If any base type in the hierarchy of TYPE needs a constructor,
7145 then we set initialized to 1. This way any nodes which are
7146 created for the purposes of initializing this aggregate
7147 will live as long as it does. This is necessary for global
7148 aggregates which do not have their initializers processed until
7149 the end of the file. */
7150 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7154 if (! initialized)
7155 DECL_INITIAL (decl) = NULL_TREE;
7158 /* Handle initialization of references.
7159 These three arguments are from `cp_finish_decl', and have the
7160 same meaning here that they do there.
7162 Quotes on semantics can be found in ARM 8.4.3. */
7164 static tree
7165 grok_reference_init (tree decl, tree type, tree init)
7167 tree tmp;
7169 if (init == NULL_TREE)
7171 if ((DECL_LANG_SPECIFIC (decl) == 0
7172 || DECL_IN_AGGR_P (decl) == 0)
7173 && ! DECL_THIS_EXTERN (decl))
7174 error ("`%D' declared as reference but not initialized", decl);
7175 return NULL_TREE;
7178 if (TREE_CODE (init) == CONSTRUCTOR)
7180 error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7181 return NULL_TREE;
7184 if (TREE_CODE (init) == TREE_LIST)
7185 init = build_compound_expr (init);
7187 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7188 init = convert_from_reference (init);
7190 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7191 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7193 /* Note: default conversion is only called in very special cases. */
7194 init = default_conversion (init);
7197 /* Convert INIT to the reference type TYPE. This may involve the
7198 creation of a temporary, whose lifetime must be the same as that
7199 of the reference. If so, a DECL_STMT for the temporary will be
7200 added just after the DECL_STMT for DECL. That's why we don't set
7201 DECL_INITIAL for local references (instead assigning to them
7202 explicitly); we need to allow the temporary to be initialized
7203 first. */
7204 tmp = initialize_reference (type, init, decl);
7206 if (tmp == error_mark_node)
7207 return NULL_TREE;
7208 else if (tmp == NULL_TREE)
7210 error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7211 return NULL_TREE;
7214 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7215 return tmp;
7217 DECL_INITIAL (decl) = tmp;
7219 return NULL_TREE;
7222 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7223 array until we finish parsing the initializer. If that's the
7224 situation we're in, update DECL accordingly. */
7226 static void
7227 maybe_deduce_size_from_array_init (tree decl, tree init)
7229 tree type = TREE_TYPE (decl);
7231 if (TREE_CODE (type) == ARRAY_TYPE
7232 && TYPE_DOMAIN (type) == NULL_TREE
7233 && TREE_CODE (decl) != TYPE_DECL)
7235 /* do_default is really a C-ism to deal with tentative definitions.
7236 But let's leave it here to ease the eventual merge. */
7237 int do_default = !DECL_EXTERNAL (decl);
7238 tree initializer = init ? init : DECL_INITIAL (decl);
7239 int failure = complete_array_type (type, initializer, do_default);
7241 if (failure == 1)
7242 error ("initializer fails to determine size of `%D'", decl);
7244 if (failure == 2)
7246 if (do_default)
7247 error ("array size missing in `%D'", decl);
7248 /* If a `static' var's size isn't known, make it extern as
7249 well as static, so it does not get allocated. If it's not
7250 `static', then don't mark it extern; finish_incomplete_decl
7251 will give it a default size and it will get allocated. */
7252 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7253 DECL_EXTERNAL (decl) = 1;
7256 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7257 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7258 integer_zero_node))
7259 error ("zero-size array `%D'", decl);
7261 layout_decl (decl, 0);
7265 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7266 any appropriate error messages regarding the layout. */
7268 static void
7269 layout_var_decl (tree decl)
7271 tree type = TREE_TYPE (decl);
7272 #if 0
7273 tree ttype = target_type (type);
7274 #endif
7276 /* If we haven't already layed out this declaration, do so now.
7277 Note that we must not call complete type for an external object
7278 because it's type might involve templates that we are not
7279 supposed to isntantiate yet. (And it's perfectly valid to say
7280 `extern X x' for some incomplete type `X'.) */
7281 if (!DECL_EXTERNAL (decl))
7282 complete_type (type);
7283 if (!DECL_SIZE (decl)
7284 && TREE_TYPE (decl) != error_mark_node
7285 && (COMPLETE_TYPE_P (type)
7286 || (TREE_CODE (type) == ARRAY_TYPE
7287 && !TYPE_DOMAIN (type)
7288 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7289 layout_decl (decl, 0);
7291 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7293 /* An automatic variable with an incomplete type: that is an error.
7294 Don't talk about array types here, since we took care of that
7295 message in grokdeclarator. */
7296 error ("storage size of `%D' isn't known", decl);
7297 TREE_TYPE (decl) = error_mark_node;
7299 #if 0
7300 /* Keep this code around in case we later want to control debug info
7301 based on whether a type is "used". (jason 1999-11-11) */
7303 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7304 /* Let debugger know it should output info for this type. */
7305 note_debug_info_needed (ttype);
7307 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7308 note_debug_info_needed (DECL_CONTEXT (decl));
7309 #endif
7311 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7312 && DECL_SIZE (decl) != NULL_TREE
7313 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7315 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7316 constant_expression_warning (DECL_SIZE (decl));
7317 else
7318 error ("storage size of `%D' isn't constant", decl);
7321 if (TREE_STATIC (decl)
7322 && !DECL_ARTIFICIAL (decl)
7323 && current_function_decl
7324 && DECL_CONTEXT (decl) == current_function_decl)
7325 push_local_name (decl);
7328 /* If a local static variable is declared in an inline function, or if
7329 we have a weak definition, we must endeavor to create only one
7330 instance of the variable at link-time. */
7332 static void
7333 maybe_commonize_var (tree decl)
7335 /* Static data in a function with comdat linkage also has comdat
7336 linkage. */
7337 if (TREE_STATIC (decl)
7338 /* Don't mess with __FUNCTION__. */
7339 && ! DECL_ARTIFICIAL (decl)
7340 && DECL_FUNCTION_SCOPE_P (decl)
7341 /* Unfortunately, import_export_decl has not always been called
7342 before the function is processed, so we cannot simply check
7343 DECL_COMDAT. */
7344 && (DECL_COMDAT (DECL_CONTEXT (decl))
7345 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
7346 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
7347 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
7349 /* If flag_weak, we don't need to mess with this, as we can just
7350 make the function weak, and let it refer to its unique local
7351 copy. This works because we don't allow the function to be
7352 inlined. */
7353 if (! flag_weak)
7355 if (DECL_INTERFACE_KNOWN (current_function_decl))
7357 TREE_PUBLIC (decl) = 1;
7358 DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7360 else if (DECL_INITIAL (decl) == NULL_TREE
7361 || DECL_INITIAL (decl) == error_mark_node)
7363 TREE_PUBLIC (decl) = 1;
7364 DECL_COMMON (decl) = 1;
7366 /* else we lose. We can only do this if we can use common,
7367 which we can't if it has been initialized. */
7369 if (!TREE_PUBLIC (decl))
7371 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7372 cp_warning_at (" you can work around this by removing the initializer", decl);
7375 else
7376 comdat_linkage (decl);
7378 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7379 /* Set it up again; we might have set DECL_INITIAL since the last
7380 time. */
7381 comdat_linkage (decl);
7384 /* Issue an error message if DECL is an uninitialized const variable. */
7386 static void
7387 check_for_uninitialized_const_var (tree decl)
7389 tree type = TREE_TYPE (decl);
7391 /* ``Unless explicitly declared extern, a const object does not have
7392 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7393 7.1.6 */
7394 if (TREE_CODE (decl) == VAR_DECL
7395 && TREE_CODE (type) != REFERENCE_TYPE
7396 && CP_TYPE_CONST_P (type)
7397 && !TYPE_NEEDS_CONSTRUCTING (type)
7398 && !DECL_INITIAL (decl))
7399 error ("uninitialized const `%D'", decl);
7402 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
7403 returned is the next FIELD_DECL (possibly FIELD itself) that can be
7404 initialized. If there are no more such fields, the return value
7405 will be NULL. */
7407 static tree
7408 next_initializable_field (tree field)
7410 while (field
7411 && (TREE_CODE (field) != FIELD_DECL
7412 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7413 || DECL_ARTIFICIAL (field)))
7414 field = TREE_CHAIN (field);
7416 return field;
7419 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7420 brace-enclosed aggregate initializer.
7422 *INITP is one of a list of initializers describing a brace-enclosed
7423 initializer for an entity of the indicated aggregate TYPE. It may
7424 not presently match the shape of the TYPE; for example:
7426 struct S { int a; int b; };
7427 struct S a[] = { 1, 2, 3, 4 };
7429 Here *INITP will point to TREE_LIST of four elements, rather than a
7430 list of two elements, each itself a list of two elements. This
7431 routine transforms INIT from the former form into the latter. The
7432 revised initializer is returned. */
7434 static tree
7435 reshape_init (tree type, tree *initp)
7437 tree inits;
7438 tree old_init;
7439 tree old_init_value;
7440 tree new_init;
7441 bool brace_enclosed_p;
7443 old_init = *initp;
7444 old_init_value = (TREE_CODE (*initp) == TREE_LIST
7445 ? TREE_VALUE (*initp) : old_init);
7447 /* For some parse errors, OLD_INIT_VALUE may be NULL. */
7448 if (!old_init_value)
7450 my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
7451 TREE_VALUE (old_init) = error_mark_node;
7452 return old_init;
7455 /* If the initializer is brace-enclosed, pull initializers from the
7456 enclosed elements. Advance past the brace-enclosed initializer
7457 now. */
7458 if (TREE_CODE (old_init_value) == CONSTRUCTOR
7459 && TREE_HAS_CONSTRUCTOR (old_init_value))
7461 *initp = TREE_CHAIN (old_init);
7462 TREE_CHAIN (old_init) = NULL_TREE;
7463 inits = CONSTRUCTOR_ELTS (old_init_value);
7464 initp = &inits;
7465 brace_enclosed_p = true;
7467 else
7469 inits = NULL_TREE;
7470 brace_enclosed_p = false;
7473 /* A non-aggregate type is always initialized with a single
7474 initializer. */
7475 if (!CP_AGGREGATE_TYPE_P (type))
7477 *initp = TREE_CHAIN (old_init);
7478 TREE_CHAIN (old_init) = NULL_TREE;
7479 /* It is invalid to initialize a non-aggregate type with a
7480 brace-enclosed initializer. */
7481 if (brace_enclosed_p)
7483 error ("brace-enclosed initializer used to initialize `%T'",
7484 type);
7485 if (TREE_CODE (old_init) == TREE_LIST)
7486 TREE_VALUE (old_init) = error_mark_node;
7487 else
7488 old_init = error_mark_node;
7491 return old_init;
7494 /* [dcl.init.aggr]
7496 All implicit type conversions (clause _conv_) are considered when
7497 initializing the aggregate member with an initializer from an
7498 initializer-list. If the initializer can initialize a member,
7499 the member is initialized. Otherwise, if the member is itself a
7500 non-empty subaggregate, brace elision is assumed and the
7501 initializer is considered for the initialization of the first
7502 member of the subaggregate. */
7503 if (CLASS_TYPE_P (type)
7504 && !brace_enclosed_p
7505 && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7507 *initp = TREE_CHAIN (old_init);
7508 TREE_CHAIN (old_init) = NULL_TREE;
7509 return old_init;
7512 if (TREE_CODE (old_init_value) == STRING_CST
7513 && TREE_CODE (type) == ARRAY_TYPE
7514 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7516 /* [dcl.init.string]
7518 A char array (whether plain char, signed char, or unsigned char)
7519 can be initialized by a string-literal (optionally enclosed in
7520 braces); a wchar_t array can be initialized by a wide
7521 string-literal (optionally enclosed in braces). */
7522 new_init = old_init;
7523 /* Move past the initializer. */
7524 *initp = TREE_CHAIN (old_init);
7525 TREE_CHAIN (old_init) = NULL_TREE;
7527 else
7529 /* Build a CONSTRUCTOR to hold the contents of the aggregate. */
7530 new_init = build_constructor (type, NULL_TREE);
7531 TREE_HAS_CONSTRUCTOR (new_init) = 1;
7533 if (CLASS_TYPE_P (type))
7535 tree field;
7537 field = next_initializable_field (TYPE_FIELDS (type));
7539 if (!field)
7541 /* [dcl.init.aggr]
7543 An initializer for an aggregate member that is an
7544 empty class shall have the form of an empty
7545 initializer-list {}. */
7546 if (!brace_enclosed_p)
7547 error ("initializer for `%T' must be brace-enclosed",
7548 type);
7550 else
7552 /* Loop through the initializable fields, gathering
7553 initializers. */
7554 /* FIXME support non-trivial labeled initializers. */
7555 while (*initp && field)
7557 tree field_init;
7559 field_init = reshape_init (TREE_TYPE (field), initp);
7560 TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7561 CONSTRUCTOR_ELTS (new_init) = field_init;
7562 /* [dcl.init.aggr]
7564 When a union is initialized with a brace-enclosed
7565 initializer, the braces shall only contain an
7566 initializer for the first member of the union. */
7567 if (TREE_CODE (type) == UNION_TYPE)
7568 break;
7569 field = next_initializable_field (TREE_CHAIN (field));
7573 else if (TREE_CODE (type) == ARRAY_TYPE)
7575 tree index;
7576 tree max_index;
7578 /* If the bound of the array is known, take no more initializers
7579 than are allowed. */
7580 max_index = (TYPE_DOMAIN (type)
7581 ? array_type_nelts (type) : NULL_TREE);
7582 /* Loop through the array elements, gathering initializers. */
7583 for (index = size_zero_node;
7584 *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7585 index = size_binop (PLUS_EXPR, index, size_one_node))
7587 tree element_init;
7589 element_init = reshape_init (TREE_TYPE (type), initp);
7590 TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7591 CONSTRUCTOR_ELTS (new_init) = element_init;
7592 if (TREE_PURPOSE (element_init))
7593 index = TREE_PURPOSE (element_init);
7596 else
7597 abort ();
7599 /* The initializers were placed in reverse order in the
7600 CONSTRUCTOR. */
7601 CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7603 if (TREE_CODE (old_init) == TREE_LIST)
7604 new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7607 /* If this was a brace-enclosed initializer and all of the
7608 initializers were not used up, there is a problem. */
7609 if (brace_enclosed_p && *initp)
7610 error ("too many initializers for `%T'", type);
7612 return new_init;
7615 /* Verify INIT (the initializer for DECL), and record the
7616 initialization in DECL_INITIAL, if appropriate.
7618 If the return value is non-NULL, it is an expression that must be
7619 evaluated dynamically to initialize DECL. */
7621 static tree
7622 check_initializer (tree decl, tree init, int flags)
7624 tree type = TREE_TYPE (decl);
7626 /* If `start_decl' didn't like having an initialization, ignore it now. */
7627 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7628 init = NULL_TREE;
7630 /* If an initializer is present, DECL_INITIAL has been
7631 error_mark_node, to indicate that an as-of-yet unevaluated
7632 initialization will occur. From now on, DECL_INITIAL reflects
7633 the static initialization -- if any -- of DECL. */
7634 DECL_INITIAL (decl) = NULL_TREE;
7636 /* Things that are going to be initialized need to have complete
7637 type. */
7638 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7640 if (type == error_mark_node)
7641 /* We will have already complained. */
7642 init = NULL_TREE;
7643 else if (init && COMPLETE_TYPE_P (type)
7644 && !TREE_CONSTANT (TYPE_SIZE (type)))
7646 error ("variable-sized object `%D' may not be initialized", decl);
7647 init = NULL_TREE;
7649 else if (TREE_CODE (type) == ARRAY_TYPE
7650 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7652 error ("elements of array `%#D' have incomplete type", decl);
7653 init = NULL_TREE;
7655 else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7657 error ("`%D' has incomplete type", decl);
7658 TREE_TYPE (decl) = error_mark_node;
7659 init = NULL_TREE;
7662 if (TREE_CODE (decl) == CONST_DECL)
7664 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7666 DECL_INITIAL (decl) = init;
7668 my_friendly_assert (init != NULL_TREE, 149);
7669 init = NULL_TREE;
7671 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7672 init = grok_reference_init (decl, type, init);
7673 else if (init)
7675 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7677 /* [dcl.init] paragraph 13,
7678 If T is a scalar type, then a declaration of the form
7679 T x = { a };
7680 is equivalent to
7681 T x = a;
7683 reshape_init will complain about the extra braces,
7684 and doesn't do anything useful in the case where TYPE is
7685 scalar, so just don't call it. */
7686 if (CP_AGGREGATE_TYPE_P (type))
7687 init = reshape_init (type, &init);
7689 if ((*targetm.vector_opaque_p) (type))
7691 error ("opaque vector types cannot be initialized");
7692 init = error_mark_node;
7696 /* If DECL has an array type without a specific bound, deduce the
7697 array size from the initializer. */
7698 maybe_deduce_size_from_array_init (decl, init);
7699 type = TREE_TYPE (decl);
7700 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7701 TREE_TYPE (init) = type;
7703 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7705 if (TREE_CODE (type) == ARRAY_TYPE)
7706 goto initialize_aggr;
7707 else if (TREE_CODE (init) == CONSTRUCTOR
7708 && TREE_HAS_CONSTRUCTOR (init))
7710 if (TYPE_NON_AGGREGATE_CLASS (type))
7712 error ("`%D' must be initialized by constructor, not by `{...}'",
7713 decl);
7714 init = error_mark_node;
7716 else
7717 goto dont_use_constructor;
7719 else
7721 int saved_stmts_are_full_exprs_p;
7723 initialize_aggr:
7724 saved_stmts_are_full_exprs_p = 0;
7725 if (building_stmt_tree ())
7727 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7728 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7730 init = build_aggr_init (decl, init, flags);
7731 if (building_stmt_tree ())
7732 current_stmt_tree ()->stmts_are_full_exprs_p =
7733 saved_stmts_are_full_exprs_p;
7734 return init;
7737 else
7739 dont_use_constructor:
7740 if (TREE_CODE (init) != TREE_VEC)
7741 init = store_init_value (decl, init);
7744 else if (DECL_EXTERNAL (decl))
7746 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7747 goto initialize_aggr;
7748 else if (IS_AGGR_TYPE (type))
7750 tree core_type = strip_array_types (type);
7752 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7753 error ("structure `%D' with uninitialized const members", decl);
7754 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7755 error ("structure `%D' with uninitialized reference members",
7756 decl);
7758 check_for_uninitialized_const_var (decl);
7760 else
7761 check_for_uninitialized_const_var (decl);
7763 if (init && init != error_mark_node)
7764 init = build (INIT_EXPR, type, decl, init);
7766 return init;
7769 /* If DECL is not a local variable, give it RTL. */
7771 static void
7772 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7774 int toplev = toplevel_bindings_p ();
7775 int defer_p;
7777 /* Handle non-variables up front. */
7778 if (TREE_CODE (decl) != VAR_DECL)
7780 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7781 return;
7784 /* If we see a class member here, it should be a static data
7785 member. */
7786 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7788 my_friendly_assert (TREE_STATIC (decl), 19990828);
7789 /* An in-class declaration of a static data member should be
7790 external; it is only a declaration, and not a definition. */
7791 if (init == NULL_TREE)
7792 my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7795 /* Set the DECL_ASSEMBLER_NAME for the variable. */
7796 if (asmspec)
7798 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7799 /* The `register' keyword, when used together with an
7800 asm-specification, indicates that the variable should be
7801 placed in a particular register. */
7802 if (DECL_REGISTER (decl))
7803 DECL_C_HARD_REGISTER (decl) = 1;
7806 /* We don't create any RTL for local variables. */
7807 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7808 return;
7810 /* We defer emission of local statics until the corresponding
7811 DECL_STMT is expanded. */
7812 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7814 /* We try to defer namespace-scope static constants so that they are
7815 not emitted into the object file unnecessarily. */
7816 if (!DECL_VIRTUAL_P (decl)
7817 && TREE_READONLY (decl)
7818 && DECL_INITIAL (decl) != NULL_TREE
7819 && DECL_INITIAL (decl) != error_mark_node
7820 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7821 && toplev
7822 && !TREE_PUBLIC (decl))
7824 /* Fool with the linkage of static consts according to #pragma
7825 interface. */
7826 if (!interface_unknown && !TREE_PUBLIC (decl))
7828 TREE_PUBLIC (decl) = 1;
7829 DECL_EXTERNAL (decl) = interface_only;
7832 defer_p = 1;
7834 /* Likewise for template instantiations. */
7835 else if (DECL_COMDAT (decl))
7836 defer_p = 1;
7838 /* If we're deferring the variable, we only need to make RTL if
7839 there's an ASMSPEC. Otherwise, we'll lazily create it later when
7840 we need it. (There's no way to lazily create RTL for things that
7841 have assembly specs because the information about the specifier
7842 isn't stored in the tree, yet) */
7843 if (defer_p && asmspec)
7844 make_decl_rtl (decl, asmspec);
7845 /* If we're not deferring, go ahead and assemble the variable. */
7846 else if (!defer_p)
7847 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7850 /* The old ARM scoping rules injected variables declared in the
7851 initialization statement of a for-statement into the surrounding
7852 scope. We support this usage, in order to be backward-compatible.
7853 DECL is a just-declared VAR_DECL; if necessary inject its
7854 declaration into the surrounding scope. */
7856 void
7857 maybe_inject_for_scope_var (tree decl)
7859 timevar_push (TV_NAME_LOOKUP);
7860 if (!DECL_NAME (decl))
7862 timevar_pop (TV_NAME_LOOKUP);
7863 return;
7866 /* Declarations of __FUNCTION__ and its ilk appear magically when
7867 the variable is first used. If that happens to be inside a
7868 for-loop, we don't want to do anything special. */
7869 if (DECL_PRETTY_FUNCTION_P (decl))
7871 timevar_pop (TV_NAME_LOOKUP);
7872 return;
7875 if (current_binding_level->is_for_scope)
7877 struct cp_binding_level *outer
7878 = current_binding_level->level_chain;
7880 /* Check to see if the same name is already bound at the outer
7881 level, either because it was directly declared, or because a
7882 dead for-decl got preserved. In either case, the code would
7883 not have been valid under the ARM scope rules, so clear
7884 is_for_scope for the current_binding_level.
7886 Otherwise, we need to preserve the temp slot for decl to last
7887 into the outer binding level. */
7889 cxx_binding *outer_binding
7890 = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
7892 if (outer_binding && BINDING_SCOPE (outer_binding) == outer
7893 && (TREE_CODE (BINDING_VALUE (outer_binding)) == VAR_DECL)
7894 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7896 BINDING_VALUE (outer_binding)
7897 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7898 current_binding_level->is_for_scope = 0;
7901 timevar_pop (TV_NAME_LOOKUP);
7904 /* Generate code to initialize DECL (a local variable). */
7906 static void
7907 initialize_local_var (tree decl, tree init)
7909 tree type = TREE_TYPE (decl);
7911 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
7912 || TREE_CODE (decl) == RESULT_DECL,
7913 20021010);
7914 my_friendly_assert (!TREE_STATIC (decl), 20021010);
7916 if (DECL_SIZE (decl) == NULL_TREE)
7918 /* If we used it already as memory, it must stay in memory. */
7919 DECL_INITIAL (decl) = NULL_TREE;
7920 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7923 if (DECL_SIZE (decl) && type != error_mark_node)
7925 int already_used;
7927 /* Compute and store the initial value. */
7928 already_used = TREE_USED (decl) || TREE_USED (type);
7930 /* Perform the initialization. */
7931 if (init)
7933 int saved_stmts_are_full_exprs_p;
7935 my_friendly_assert (building_stmt_tree (), 20000906);
7936 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7937 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7938 finish_expr_stmt (init);
7939 current_stmt_tree ()->stmts_are_full_exprs_p =
7940 saved_stmts_are_full_exprs_p;
7943 /* Set this to 0 so we can tell whether an aggregate which was
7944 initialized was ever used. Don't do this if it has a
7945 destructor, so we don't complain about the 'resource
7946 allocation is initialization' idiom. Now set
7947 attribute((unused)) on types so decls of that type will be
7948 marked used. (see TREE_USED, above.) */
7949 if (TYPE_NEEDS_CONSTRUCTING (type)
7950 && ! already_used
7951 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7952 && DECL_NAME (decl))
7953 TREE_USED (decl) = 0;
7954 else if (already_used)
7955 TREE_USED (decl) = 1;
7958 /* Generate a cleanup, if necessary. */
7959 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7961 tree cleanup;
7963 /* Compute the cleanup. */
7964 cleanup = cxx_maybe_build_cleanup (decl);
7966 /* Record the cleanup required for this declaration. */
7967 if (DECL_SIZE (decl) && cleanup)
7968 finish_decl_cleanup (decl, cleanup);
7972 /* Finish processing of a declaration;
7973 install its line number and initial value.
7974 If the length of an array type is not known before,
7975 it must be determined now, from the initial value, or it is an error.
7977 INIT holds the value of an initializer that should be allowed to escape
7978 the normal rules.
7980 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7981 if the (init) syntax was used. */
7983 void
7984 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
7986 register tree type;
7987 tree ttype = NULL_TREE;
7988 const char *asmspec = NULL;
7989 int was_readonly = 0;
7991 if (! decl)
7993 if (init)
7994 error ("assignment (not initialization) in declaration");
7995 return;
7998 /* If a name was specified, get the string. */
7999 if (current_binding_level == global_binding_level)
8000 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8001 if (asmspec_tree)
8002 asmspec = TREE_STRING_POINTER (asmspec_tree);
8004 if (init && TREE_CODE (init) == NAMESPACE_DECL)
8006 error ("cannot initialize `%D' to namespace `%D'",
8007 decl, init);
8008 init = NULL_TREE;
8011 if (current_class_type
8012 && CP_DECL_CONTEXT (decl) == current_class_type
8013 && TYPE_BEING_DEFINED (current_class_type)
8014 && (DECL_INITIAL (decl) || init))
8015 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8017 if (TREE_CODE (decl) == VAR_DECL
8018 && DECL_CONTEXT (decl)
8019 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8020 && DECL_CONTEXT (decl) != current_namespace
8021 && init)
8023 /* Leave the namespace of the object. */
8024 pop_decl_namespace ();
8027 type = TREE_TYPE (decl);
8029 if (type == error_mark_node)
8030 return;
8032 if (TYPE_HAS_MUTABLE_P (type))
8033 TREE_READONLY (decl) = 0;
8035 if (processing_template_decl)
8037 /* Add this declaration to the statement-tree. */
8038 if (at_function_scope_p ()
8039 && TREE_CODE (decl) != RESULT_DECL)
8040 add_decl_stmt (decl);
8042 if (init && DECL_INITIAL (decl))
8043 DECL_INITIAL (decl) = init;
8044 goto finish_end0;
8047 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8048 my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8050 /* Take care of TYPE_DECLs up front. */
8051 if (TREE_CODE (decl) == TYPE_DECL)
8053 if (type != error_mark_node
8054 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8056 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8057 warning ("shadowing previous type declaration of `%#D'", decl);
8058 set_identifier_type_value (DECL_NAME (decl), type);
8059 CLASSTYPE_GOT_SEMICOLON (type) = 1;
8062 /* If we have installed this as the canonical typedef for this
8063 type, and that type has not been defined yet, delay emitting
8064 the debug information for it, as we will emit it later. */
8065 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8066 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8067 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8069 rest_of_decl_compilation (decl, NULL,
8070 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8071 goto finish_end;
8074 if (TREE_CODE (decl) != FUNCTION_DECL)
8075 ttype = target_type (type);
8077 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8078 && TYPE_NEEDS_CONSTRUCTING (type))
8080 /* Currently, GNU C++ puts constants in text space, making them
8081 impossible to initialize. In the future, one would hope for
8082 an operating system which understood the difference between
8083 initialization and the running of a program. */
8084 was_readonly = 1;
8085 TREE_READONLY (decl) = 0;
8088 if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8090 /* This must override the asm specifier which was placed by
8091 grokclassfn. Lay this out fresh. */
8092 SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8093 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8094 make_decl_rtl (decl, asmspec);
8096 else if (TREE_CODE (decl) == RESULT_DECL)
8097 init = check_initializer (decl, init, flags);
8098 else if (TREE_CODE (decl) == VAR_DECL)
8100 /* Only PODs can have thread-local storage. Other types may require
8101 various kinds of non-trivial initialization. */
8102 if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8103 error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8104 decl, TREE_TYPE (decl));
8105 /* Convert the initializer to the type of DECL, if we have not
8106 already initialized DECL. */
8107 if (!DECL_INITIALIZED_P (decl)
8108 /* If !DECL_EXTERNAL then DECL is being defined. In the
8109 case of a static data member initialized inside the
8110 class-specifier, there can be an initializer even if DECL
8111 is *not* defined. */
8112 && (!DECL_EXTERNAL (decl) || init))
8114 init = check_initializer (decl, init, flags);
8115 /* Thread-local storage cannot be dynamically initialized. */
8116 if (DECL_THREAD_LOCAL (decl) && init)
8118 error ("`%D' is thread-local and so cannot be dynamically "
8119 "initialized", decl);
8120 init = NULL_TREE;
8122 /* Handle:
8124 [dcl.init]
8126 The memory occupied by any object of static storage
8127 duration is zero-initialized at program startup before
8128 any other initialization takes place.
8130 We cannot create an appropriate initializer until after
8131 the type of DECL is finalized. If DECL_INITIAL is set,
8132 then the DECL is statically initialized, and any
8133 necessary zero-initialization has already been performed. */
8134 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8135 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8136 /*nelts=*/NULL_TREE,
8137 /*static_storage_p=*/true);
8138 /* Remember that the initialization for this variable has
8139 taken place. */
8140 DECL_INITIALIZED_P (decl) = 1;
8142 /* If the variable has an array type, lay out the type, even if
8143 there is no initializer. It is valid to index through the
8144 array, and we must get TYPE_ALIGN set correctly on the array
8145 type. */
8146 else if (TREE_CODE (type) == ARRAY_TYPE)
8147 layout_type (type);
8150 /* Add this declaration to the statement-tree. This needs to happen
8151 after the call to check_initializer so that the DECL_STMT for a
8152 reference temp is added before the DECL_STMT for the reference itself. */
8153 if (building_stmt_tree ()
8154 && at_function_scope_p ()
8155 && TREE_CODE (decl) != RESULT_DECL)
8156 add_decl_stmt (decl);
8158 if (TREE_CODE (decl) == VAR_DECL)
8159 layout_var_decl (decl);
8161 /* Output the assembler code and/or RTL code for variables and functions,
8162 unless the type is an undefined structure or union.
8163 If not, it will get done when the type is completed. */
8164 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8165 || TREE_CODE (decl) == RESULT_DECL)
8167 if (TREE_CODE (decl) == VAR_DECL)
8168 maybe_commonize_var (decl);
8170 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8172 if (TREE_CODE (type) == FUNCTION_TYPE
8173 || TREE_CODE (type) == METHOD_TYPE)
8174 abstract_virtuals_error (decl,
8175 strip_array_types (TREE_TYPE (type)));
8176 else
8177 abstract_virtuals_error (decl, strip_array_types (type));
8179 if (TREE_CODE (decl) == FUNCTION_DECL
8180 || TREE_TYPE (decl) == error_mark_node)
8181 /* No initialization required. */
8183 else if (DECL_EXTERNAL (decl)
8184 && ! (DECL_LANG_SPECIFIC (decl)
8185 && DECL_NOT_REALLY_EXTERN (decl)))
8187 if (init)
8188 DECL_INITIAL (decl) = init;
8190 else
8192 /* A variable definition. */
8193 if (DECL_FUNCTION_SCOPE_P (decl))
8195 /* This is a local declaration. */
8196 if (doing_semantic_analysis_p ())
8197 maybe_inject_for_scope_var (decl);
8198 /* Initialize the local variable. */
8199 if (processing_template_decl)
8201 if (init || DECL_INITIAL (decl) == error_mark_node)
8202 DECL_INITIAL (decl) = init;
8204 else if (!TREE_STATIC (decl))
8205 initialize_local_var (decl, init);
8208 if (TREE_STATIC (decl))
8209 expand_static_init (decl, init);
8211 finish_end0:
8213 /* Undo call to `pushclass' that was done in `start_decl'
8214 due to initialization of qualified member variable.
8215 I.e., Foo::x = 10; */
8217 tree context = CP_DECL_CONTEXT (decl);
8218 if (context
8219 && TYPE_P (context)
8220 && (TREE_CODE (decl) == VAR_DECL
8221 /* We also have a pushclass done that we need to undo here
8222 if we're at top level and declare a method. */
8223 || TREE_CODE (decl) == FUNCTION_DECL)
8224 /* If size hasn't been set, we're still defining it,
8225 and therefore inside the class body; don't pop
8226 the binding level.. */
8227 && COMPLETE_TYPE_P (context)
8228 && context == current_class_type)
8229 pop_nested_class ();
8233 finish_end:
8235 if (was_readonly)
8236 TREE_READONLY (decl) = 1;
8239 /* This is here for a midend callback from c-common.c */
8241 void
8242 finish_decl (tree decl, tree init, tree asmspec_tree)
8244 cp_finish_decl (decl, init, asmspec_tree, 0);
8247 /* Returns a declaration for a VAR_DECL as if:
8249 extern "C" TYPE NAME;
8251 had been seen. Used to create compiler-generated global
8252 variables. */
8254 tree
8255 declare_global_var (tree name, tree type)
8257 tree decl;
8259 push_to_top_level ();
8260 decl = build_decl (VAR_DECL, name, type);
8261 TREE_PUBLIC (decl) = 1;
8262 DECL_EXTERNAL (decl) = 1;
8263 DECL_ARTIFICIAL (decl) = 1;
8264 pushdecl (decl);
8265 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8266 pop_from_top_level ();
8268 return decl;
8271 /* Returns a pointer to the `atexit' function. Note that if
8272 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8273 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8275 static tree
8276 get_atexit_node (void)
8278 tree atexit_fndecl;
8279 tree arg_types;
8280 tree fn_type;
8281 tree fn_ptr_type;
8282 const char *name;
8284 if (atexit_node)
8285 return atexit_node;
8287 if (flag_use_cxa_atexit)
8289 /* The declaration for `__cxa_atexit' is:
8291 int __cxa_atexit (void (*)(void *), void *, void *)
8293 We build up the argument types and then then function type
8294 itself. */
8296 /* First, build the pointer-to-function type for the first
8297 argument. */
8298 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8299 fn_type = build_function_type (void_type_node, arg_types);
8300 fn_ptr_type = build_pointer_type (fn_type);
8301 /* Then, build the rest of the argument types. */
8302 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8303 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8304 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8305 /* And the final __cxa_atexit type. */
8306 fn_type = build_function_type (integer_type_node, arg_types);
8307 fn_ptr_type = build_pointer_type (fn_type);
8308 name = "__cxa_atexit";
8310 else
8312 /* The declaration for `atexit' is:
8314 int atexit (void (*)());
8316 We build up the argument types and then then function type
8317 itself. */
8318 fn_type = build_function_type (void_type_node, void_list_node);
8319 fn_ptr_type = build_pointer_type (fn_type);
8320 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8321 /* Build the final atexit type. */
8322 fn_type = build_function_type (integer_type_node, arg_types);
8323 name = "atexit";
8326 /* Now, build the function declaration. */
8327 push_lang_context (lang_name_c);
8328 atexit_fndecl = build_library_fn_ptr (name, fn_type);
8329 mark_used (atexit_fndecl);
8330 pop_lang_context ();
8331 atexit_node = default_conversion (atexit_fndecl);
8333 return atexit_node;
8336 /* Returns the __dso_handle VAR_DECL. */
8338 static tree
8339 get_dso_handle_node (void)
8341 if (dso_handle_node)
8342 return dso_handle_node;
8344 /* Declare the variable. */
8345 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8346 ptr_type_node);
8348 return dso_handle_node;
8351 /* Begin a new function with internal linkage whose job will be simply
8352 to destroy some particular variable. */
8354 static tree
8355 start_cleanup_fn (void)
8357 static int counter = 0;
8358 int old_interface_only = interface_only;
8359 int old_interface_unknown = interface_unknown;
8360 char name[32];
8361 tree parmtypes;
8362 tree fntype;
8363 tree fndecl;
8365 push_to_top_level ();
8367 /* No need to mangle this. */
8368 push_lang_context (lang_name_c);
8370 interface_only = 0;
8371 interface_unknown = 1;
8373 /* Build the parameter-types. */
8374 parmtypes = void_list_node;
8375 /* Functions passed to __cxa_atexit take an additional parameter.
8376 We'll just ignore it. After we implement the new calling
8377 convention for destructors, we can eliminate the use of
8378 additional cleanup functions entirely in the -fnew-abi case. */
8379 if (flag_use_cxa_atexit)
8380 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8381 /* Build the function type itself. */
8382 fntype = build_function_type (void_type_node, parmtypes);
8383 /* Build the name of the function. */
8384 sprintf (name, "__tcf_%d", counter++);
8385 /* Build the function declaration. */
8386 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8387 /* It's a function with internal linkage, generated by the
8388 compiler. */
8389 TREE_PUBLIC (fndecl) = 0;
8390 DECL_ARTIFICIAL (fndecl) = 1;
8391 /* Make the function `inline' so that it is only emitted if it is
8392 actually needed. It is unlikely that it will be inlined, since
8393 it is only called via a function pointer, but we avoid unnecessary
8394 emissions this way. */
8395 DECL_INLINE (fndecl) = 1;
8396 /* Build the parameter. */
8397 if (flag_use_cxa_atexit)
8399 tree parmdecl;
8401 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8402 DECL_CONTEXT (parmdecl) = fndecl;
8403 TREE_USED (parmdecl) = 1;
8404 DECL_ARGUMENTS (fndecl) = parmdecl;
8407 pushdecl (fndecl);
8408 start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8410 interface_unknown = old_interface_unknown;
8411 interface_only = old_interface_only;
8413 pop_lang_context ();
8415 return current_function_decl;
8418 /* Finish the cleanup function begun by start_cleanup_fn. */
8420 static void
8421 end_cleanup_fn (void)
8423 expand_body (finish_function (0));
8425 pop_from_top_level ();
8428 /* Generate code to handle the destruction of DECL, an object with
8429 static storage duration. */
8431 void
8432 register_dtor_fn (tree decl)
8434 tree cleanup;
8435 tree compound_stmt;
8436 tree args;
8437 tree fcall;
8438 int saved_flag_access_control;
8440 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8441 return;
8443 /* Call build_cleanup before we enter the anonymous function so that
8444 any access checks will be done relative to the current scope,
8445 rather than the scope of the anonymous function. */
8446 build_cleanup (decl);
8448 /* Now start the function. */
8449 cleanup = start_cleanup_fn ();
8451 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8452 to the original function, rather than the anonymous one. That
8453 will make the back-end think that nested functions are in use,
8454 which causes confusion. */
8455 saved_flag_access_control = flag_access_control;
8456 scope_chain->check_access = flag_access_control = 0;
8457 fcall = build_cleanup (decl);
8458 scope_chain->check_access = flag_access_control = saved_flag_access_control;
8460 /* Create the body of the anonymous function. */
8461 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
8462 finish_expr_stmt (fcall);
8463 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
8464 end_cleanup_fn ();
8466 /* Call atexit with the cleanup function. */
8467 cxx_mark_addressable (cleanup);
8468 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8469 if (flag_use_cxa_atexit)
8471 args = tree_cons (NULL_TREE,
8472 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8473 NULL_TREE);
8474 args = tree_cons (NULL_TREE, null_pointer_node, args);
8475 args = tree_cons (NULL_TREE, cleanup, args);
8477 else
8478 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8479 finish_expr_stmt (build_function_call (get_atexit_node (), args));
8482 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8483 is its initializer. Generate code to handle the construction
8484 and destruction of DECL. */
8486 static void
8487 expand_static_init (tree decl, tree init)
8489 tree oldstatic;
8491 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8492 my_friendly_assert (TREE_STATIC (decl), 20021010);
8494 /* Some variables require no initialization. */
8495 if (!init
8496 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8497 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8498 return;
8500 oldstatic = value_member (decl, static_aggregates);
8502 if (oldstatic)
8504 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8505 error ("multiple initializations given for `%D'", decl);
8507 else if (! toplevel_bindings_p ())
8509 /* Emit code to perform this initialization but once. */
8510 tree if_stmt;
8511 tree then_clause;
8512 tree assignment;
8513 tree guard;
8514 tree guard_init;
8516 /* Emit code to perform this initialization but once. This code
8517 looks like:
8519 static int guard = 0;
8520 if (!guard) {
8521 // Do initialization.
8522 guard = 1;
8523 // Register variable for destruction at end of program.
8526 Note that the `temp' variable is only set to 1 *after* the
8527 initialization is complete. This ensures that an exception,
8528 thrown during the construction, will cause the variable to
8529 reinitialized when we pass through this code again, as per:
8531 [stmt.dcl]
8533 If the initialization exits by throwing an exception, the
8534 initialization is not complete, so it will be tried again
8535 the next time control enters the declaration.
8537 In theory, this process should be thread-safe, too; multiple
8538 threads should not be able to initialize the variable more
8539 than once. We don't yet attempt to ensure thread-safety. */
8541 /* Create the guard variable. */
8542 guard = get_guard (decl);
8544 /* Begin the conditional initialization. */
8545 if_stmt = begin_if_stmt ();
8546 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8547 then_clause = begin_compound_stmt (/*has_no_scope=*/0);
8549 /* Do the initialization itself. */
8550 assignment = init ? init : NULL_TREE;
8552 /* Once the assignment is complete, set TEMP to 1. Since the
8553 construction of the static object is complete at this point,
8554 we want to make sure TEMP is set to 1 even if a temporary
8555 constructed during the initialization throws an exception
8556 when it is destroyed. So, we combine the initialization and
8557 the assignment to TEMP into a single expression, ensuring
8558 that when we call finish_expr_stmt the cleanups will not be
8559 run until after TEMP is set to 1. */
8560 guard_init = set_guard (guard);
8561 if (assignment)
8563 assignment = tree_cons (NULL_TREE, assignment,
8564 build_tree_list (NULL_TREE,
8565 guard_init));
8566 assignment = build_compound_expr (assignment);
8568 else
8569 assignment = guard_init;
8570 finish_expr_stmt (assignment);
8572 /* Use atexit to register a function for destroying this static
8573 variable. */
8574 register_dtor_fn (decl);
8576 finish_compound_stmt (/*has_no_scope=*/0, then_clause);
8577 finish_then_clause (if_stmt);
8578 finish_if_stmt ();
8580 else
8581 static_aggregates = tree_cons (init, decl, static_aggregates);
8584 /* Finish the declaration of a catch-parameter. */
8586 tree
8587 start_handler_parms (tree declspecs, tree declarator)
8589 tree decl;
8590 if (declspecs)
8592 decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8593 1, NULL);
8594 if (decl == NULL_TREE)
8595 error ("invalid catch parameter");
8597 else
8598 decl = NULL_TREE;
8600 return decl;
8604 /* Make TYPE a complete type based on INITIAL_VALUE.
8605 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8606 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8609 complete_array_type (tree type, tree initial_value, int do_default)
8611 register tree maxindex = NULL_TREE;
8612 int value = 0;
8614 if (initial_value)
8616 /* An array of character type can be initialized from a
8617 brace-enclosed string constant. */
8618 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8619 && TREE_CODE (initial_value) == CONSTRUCTOR
8620 && CONSTRUCTOR_ELTS (initial_value)
8621 && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8622 == STRING_CST)
8623 && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8624 initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8626 /* Note MAXINDEX is really the maximum index, one less than the
8627 size. */
8628 if (TREE_CODE (initial_value) == STRING_CST)
8630 int eltsize
8631 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8632 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8633 / eltsize) - 1, 0);
8635 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8637 tree elts = CONSTRUCTOR_ELTS (initial_value);
8639 maxindex = ssize_int (-1);
8640 for (; elts; elts = TREE_CHAIN (elts))
8642 if (TREE_PURPOSE (elts))
8643 maxindex = TREE_PURPOSE (elts);
8644 else
8645 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8647 maxindex = copy_node (maxindex);
8649 else
8651 /* Make an error message unless that happened already. */
8652 if (initial_value != error_mark_node)
8653 value = 1;
8654 else
8655 initial_value = NULL_TREE;
8657 /* Prevent further error messages. */
8658 maxindex = build_int_2 (0, 0);
8662 if (!maxindex)
8664 if (do_default)
8665 maxindex = build_int_2 (0, 0);
8666 value = 2;
8669 if (maxindex)
8671 tree itype;
8672 tree domain;
8674 domain = build_index_type (maxindex);
8675 TYPE_DOMAIN (type) = domain;
8677 if (! TREE_TYPE (maxindex))
8678 TREE_TYPE (maxindex) = domain;
8679 if (initial_value)
8680 itype = TREE_TYPE (initial_value);
8681 else
8682 itype = NULL;
8683 if (itype && !TYPE_DOMAIN (itype))
8684 TYPE_DOMAIN (itype) = domain;
8685 /* The type of the main variant should never be used for arrays
8686 of different sizes. It should only ever be completed with the
8687 size of the array. */
8688 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8689 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8692 /* Lay out the type now that we can get the real answer. */
8694 layout_type (type);
8696 return value;
8699 /* Return zero if something is declared to be a member of type
8700 CTYPE when in the context of CUR_TYPE. STRING is the error
8701 message to print in that case. Otherwise, quietly return 1. */
8703 static int
8704 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8706 if (ctype && ctype != cur_type)
8708 if (flags == DTOR_FLAG)
8709 error ("destructor for alien class `%T' cannot be a member",
8710 ctype);
8711 else
8712 error ("constructor for alien class `%T' cannot be a member",
8713 ctype);
8714 return 0;
8716 return 1;
8719 /* Subroutine of `grokdeclarator'. */
8721 /* Generate errors possibly applicable for a given set of specifiers.
8722 This is for ARM $7.1.2. */
8724 static void
8725 bad_specifiers (tree object,
8726 const char* type,
8727 int virtualp,
8728 int quals,
8729 int inlinep,
8730 int friendp,
8731 int raises)
8733 if (virtualp)
8734 error ("`%D' declared as a `virtual' %s", object, type);
8735 if (inlinep)
8736 error ("`%D' declared as an `inline' %s", object, type);
8737 if (quals)
8738 error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8739 object, type);
8740 if (friendp)
8741 cp_error_at ("`%D' declared as a friend", object);
8742 if (raises
8743 && (TREE_CODE (object) == TYPE_DECL
8744 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8745 && !TYPE_REFFN_P (TREE_TYPE (object))
8746 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8747 cp_error_at ("`%D' declared with an exception specification", object);
8750 /* CTYPE is class type, or null if non-class.
8751 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8752 or METHOD_TYPE.
8753 DECLARATOR is the function's name.
8754 VIRTUALP is truthvalue of whether the function is virtual or not.
8755 FLAGS are to be passed through to `grokclassfn'.
8756 QUALS are qualifiers indicating whether the function is `const'
8757 or `volatile'.
8758 RAISES is a list of exceptions that this function can raise.
8759 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8760 not look, and -1 if we should not call `grokclassfn' at all.
8762 Returns `NULL_TREE' if something goes wrong, after issuing
8763 applicable error messages. */
8765 static tree
8766 grokfndecl (tree ctype,
8767 tree type,
8768 tree declarator,
8769 tree orig_declarator,
8770 int virtualp,
8771 enum overload_flags flags,
8772 tree quals,
8773 tree raises,
8774 int check,
8775 int friendp,
8776 int publicp,
8777 int inlinep,
8778 int funcdef_flag,
8779 int template_count,
8780 tree in_namespace)
8782 tree decl;
8783 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8784 int has_default_arg = 0;
8785 tree t;
8787 if (raises)
8788 type = build_exception_variant (type, raises);
8790 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8791 /* Propagate volatile out from type to decl. */
8792 if (TYPE_VOLATILE (type))
8793 TREE_THIS_VOLATILE (decl) = 1;
8795 /* If this decl has namespace scope, set that up. */
8796 if (in_namespace)
8797 set_decl_namespace (decl, in_namespace, friendp);
8798 else if (!ctype)
8799 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8801 /* `main' and builtins have implicit 'C' linkage. */
8802 if ((MAIN_NAME_P (declarator)
8803 || (IDENTIFIER_LENGTH (declarator) > 10
8804 && IDENTIFIER_POINTER (declarator)[0] == '_'
8805 && IDENTIFIER_POINTER (declarator)[1] == '_'
8806 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8807 && current_lang_name == lang_name_cplusplus
8808 && ctype == NULL_TREE
8809 /* NULL_TREE means global namespace. */
8810 && DECL_CONTEXT (decl) == NULL_TREE)
8811 SET_DECL_LANGUAGE (decl, lang_c);
8813 /* Should probably propagate const out from type to decl I bet (mrs). */
8814 if (staticp)
8816 DECL_STATIC_FUNCTION_P (decl) = 1;
8817 DECL_CONTEXT (decl) = ctype;
8820 if (ctype)
8821 DECL_CONTEXT (decl) = ctype;
8823 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8825 if (processing_template_decl)
8826 error ("cannot declare `::main' to be a template");
8827 if (inlinep)
8828 error ("cannot declare `::main' to be inline");
8829 if (!publicp)
8830 error ("cannot declare `::main' to be static");
8831 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8832 integer_type_node))
8833 error ("`main' must return `int'");
8834 inlinep = 0;
8835 publicp = 1;
8838 /* Members of anonymous types and local classes have no linkage; make
8839 them internal. */
8840 /* FIXME what if it gets a name from typedef? */
8841 if (ctype && (TYPE_ANONYMOUS_P (ctype)
8842 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8843 publicp = 0;
8845 if (publicp)
8847 /* [basic.link]: A name with no linkage (notably, the name of a class
8848 or enumeration declared in a local scope) shall not be used to
8849 declare an entity with linkage.
8851 Only check this for public decls for now. */
8852 t = no_linkage_check (TREE_TYPE (decl));
8853 if (t)
8855 if (TYPE_ANONYMOUS_P (t))
8857 if (DECL_EXTERN_C_P (decl))
8858 /* Allow this; it's pretty common in C. */;
8859 else
8861 pedwarn ("non-local function `%#D' uses anonymous type",
8862 decl);
8863 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8864 cp_pedwarn_at ("\
8865 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8866 TYPE_NAME (t));
8869 else
8870 pedwarn ("non-local function `%#D' uses local type `%T'",
8871 decl, t);
8875 TREE_PUBLIC (decl) = publicp;
8876 if (! publicp)
8878 DECL_INTERFACE_KNOWN (decl) = 1;
8879 DECL_NOT_REALLY_EXTERN (decl) = 1;
8882 DID_INLINE_FUNC (decl) = 0;
8883 /* If the declaration was declared inline, mark it as such. */
8884 if (inlinep)
8885 DECL_DECLARED_INLINE_P (decl) = 1;
8886 /* We inline functions that are explicitly declared inline, or, when
8887 the user explicitly asks us to, all functions. */
8888 if (DECL_DECLARED_INLINE_P (decl))
8889 DECL_INLINE (decl) = 1;
8890 if (flag_inline_trees == 2 && !DECL_INLINE (decl))
8892 DID_INLINE_FUNC (decl) = 1;
8893 DECL_INLINE (decl) = 1;
8896 DECL_EXTERNAL (decl) = 1;
8897 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8899 error ("%smember function `%D' cannot have `%T' method qualifier",
8900 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8901 quals = NULL_TREE;
8904 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8905 grok_op_properties (decl, friendp);
8907 if (ctype && decl_function_context (decl))
8908 DECL_NO_STATIC_CHAIN (decl) = 1;
8910 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8911 if (TREE_PURPOSE (t)
8912 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8914 has_default_arg = 1;
8915 break;
8918 if (friendp
8919 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8921 if (funcdef_flag)
8922 error
8923 ("defining explicit specialization `%D' in friend declaration",
8924 orig_declarator);
8925 else
8927 tree fns = TREE_OPERAND (orig_declarator, 0);
8928 tree args = TREE_OPERAND (orig_declarator, 1);
8930 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8932 /* Something like `template <class T> friend void f<T>()'. */
8933 error ("invalid use of template-id `%D' in declaration of primary template",
8934 orig_declarator);
8935 return NULL_TREE;
8939 /* A friend declaration of the form friend void f<>(). Record
8940 the information in the TEMPLATE_ID_EXPR. */
8941 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8943 if (TREE_CODE (fns) == COMPONENT_REF)
8945 /* Due to bison parser ickiness, we will have already looked
8946 up an operator_name or PFUNCNAME within the current class
8947 (see template_id in parse.y). If the current class contains
8948 such a name, we'll get a COMPONENT_REF here. Undo that. */
8950 my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
8951 == current_class_type, 20001120);
8952 fns = TREE_OPERAND (fns, 1);
8954 my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
8955 || TREE_CODE (fns) == LOOKUP_EXPR
8956 || TREE_CODE (fns) == OVERLOAD, 20001120);
8957 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
8959 if (has_default_arg)
8961 error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8962 decl);
8963 return NULL_TREE;
8966 if (inlinep)
8968 error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8969 decl);
8970 return NULL_TREE;
8975 if (funcdef_flag)
8976 /* Make the init_value nonzero so pushdecl knows this is not
8977 tentative. error_mark_node is replaced later with the BLOCK. */
8978 DECL_INITIAL (decl) = error_mark_node;
8980 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8981 TREE_NOTHROW (decl) = 1;
8983 /* Caller will do the rest of this. */
8984 if (check < 0)
8985 return decl;
8987 if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
8988 DECL_CONSTRUCTOR_P (decl) = 1;
8990 /* Function gets the ugly name, field gets the nice one. This call
8991 may change the type of the function (because of default
8992 parameters)! */
8993 if (ctype != NULL_TREE)
8994 grokclassfn (ctype, decl, flags, quals);
8996 decl = check_explicit_specialization (orig_declarator, decl,
8997 template_count,
8998 2 * (funcdef_flag != 0) +
8999 4 * (friendp != 0));
9000 if (decl == error_mark_node)
9001 return NULL_TREE;
9003 if (ctype != NULL_TREE
9004 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9005 && check)
9007 tree old_decl;
9009 old_decl = check_classfn (ctype, decl);
9011 if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9012 /* Because grokfndecl is always supposed to return a
9013 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9014 here. We depend on our callers to figure out that its
9015 really a template that's being returned. */
9016 old_decl = DECL_TEMPLATE_RESULT (old_decl);
9018 if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9019 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9021 /* Remove the `this' parm added by grokclassfn.
9022 XXX Isn't this done in start_function, too? */
9023 revert_static_member_fn (decl);
9024 last_function_parms = TREE_CHAIN (last_function_parms);
9026 if (old_decl && DECL_ARTIFICIAL (old_decl))
9027 error ("definition of implicitly-declared `%D'", old_decl);
9029 if (old_decl)
9031 bool ok;
9033 /* Since we've smashed OLD_DECL to its
9034 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9035 if (TREE_CODE (decl) == TEMPLATE_DECL)
9036 decl = DECL_TEMPLATE_RESULT (decl);
9038 /* Attempt to merge the declarations. This can fail, in
9039 the case of some invalid specialization declarations. */
9040 push_scope (ctype);
9041 ok = duplicate_decls (decl, old_decl);
9042 pop_scope (ctype);
9043 if (!ok)
9045 error ("no `%#D' member function declared in class `%T'",
9046 decl, ctype);
9047 return NULL_TREE;
9049 return old_decl;
9053 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9054 return NULL_TREE;
9056 if (ctype == NULL_TREE || check)
9057 return decl;
9059 if (virtualp)
9060 DECL_VIRTUAL_P (decl) = 1;
9062 return decl;
9065 /* Create a VAR_DECL named NAME with the indicated TYPE.
9067 If SCOPE is non-NULL, it is the class type or namespace containing
9068 the variable. If SCOPE is NULL, the variable should is created in
9069 the innermost enclosings scope. */
9071 static tree
9072 grokvardecl (tree type,
9073 tree name,
9074 RID_BIT_TYPE * specbits_in,
9075 int initialized,
9076 int constp,
9077 tree scope)
9079 tree decl;
9080 RID_BIT_TYPE specbits;
9082 my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE,
9083 20020808);
9085 specbits = *specbits_in;
9087 /* Compute the scope in which to place the variable. */
9088 if (!scope)
9090 /* An explicit "extern" specifier indicates a namespace-scope
9091 variable. */
9092 if (RIDBIT_SETP (RID_EXTERN, specbits))
9093 scope = current_namespace;
9094 else if (!at_function_scope_p ())
9096 scope = current_scope ();
9097 if (!scope)
9098 scope = current_namespace;
9102 if (scope
9103 && (/* If the variable is a namespace-scope variable declared in a
9104 template, we need DECL_LANG_SPECIFIC. */
9105 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9106 /* Similarly for namespace-scope variables with language linkage
9107 other than C++. */
9108 || (TREE_CODE (scope) == NAMESPACE_DECL
9109 && current_lang_name != lang_name_cplusplus)
9110 /* Similarly for static data members. */
9111 || TYPE_P (scope)))
9112 decl = build_lang_decl (VAR_DECL, name, type);
9113 else
9114 decl = build_decl (VAR_DECL, name, type);
9116 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9117 set_decl_namespace (decl, scope, 0);
9118 else
9119 DECL_CONTEXT (decl) = scope;
9121 if (name && scope && current_lang_name != lang_name_c)
9122 /* We can't mangle lazily here because we don't have any
9123 way to recover whether or not a variable was `extern
9124 "C"' later. */
9125 mangle_decl (decl);
9127 if (RIDBIT_SETP (RID_EXTERN, specbits))
9129 DECL_THIS_EXTERN (decl) = 1;
9130 DECL_EXTERNAL (decl) = !initialized;
9133 /* In class context, static means one per class,
9134 public access, and static storage. */
9135 if (DECL_CLASS_SCOPE_P (decl))
9137 TREE_PUBLIC (decl) = 1;
9138 TREE_STATIC (decl) = 1;
9139 DECL_EXTERNAL (decl) = 0;
9141 /* At top level, either `static' or no s.c. makes a definition
9142 (perhaps tentative), and absence of `static' makes it public. */
9143 else if (toplevel_bindings_p ())
9145 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9146 && (DECL_THIS_EXTERN (decl) || ! constp));
9147 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9149 /* Not at top level, only `static' makes a static definition. */
9150 else
9152 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9153 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9156 if (RIDBIT_SETP (RID_THREAD, specbits))
9158 if (targetm.have_tls)
9159 DECL_THREAD_LOCAL (decl) = 1;
9160 else
9161 /* A mere warning is sure to result in improper semantics
9162 at runtime. Don't bother to allow this to compile. */
9163 error ("thread-local storage not supported for this target");
9166 if (TREE_PUBLIC (decl))
9168 /* [basic.link]: A name with no linkage (notably, the name of a class
9169 or enumeration declared in a local scope) shall not be used to
9170 declare an entity with linkage.
9172 Only check this for public decls for now. */
9173 tree t = no_linkage_check (TREE_TYPE (decl));
9174 if (t)
9176 if (TYPE_ANONYMOUS_P (t))
9177 /* Ignore for now; `enum { foo } e' is pretty common. */;
9178 else
9179 pedwarn ("non-local variable `%#D' uses local type `%T'",
9180 decl, t);
9184 return decl;
9187 /* Create and return a canonical pointer to member function type, for
9188 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9190 tree
9191 build_ptrmemfunc_type (tree type)
9193 tree field, fields;
9194 tree t;
9195 tree unqualified_variant = NULL_TREE;
9197 if (type == error_mark_node)
9198 return type;
9200 /* If a canonical type already exists for this type, use it. We use
9201 this method instead of type_hash_canon, because it only does a
9202 simple equality check on the list of field members. */
9204 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9205 return t;
9207 /* Make sure that we always have the unqualified pointer-to-member
9208 type first. */
9209 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9210 unqualified_variant
9211 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9213 t = make_aggr_type (RECORD_TYPE);
9214 /* Let the front-end know this is a pointer to member function... */
9215 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9216 /* ... and not really an aggregate. */
9217 SET_IS_AGGR_TYPE (t, 0);
9219 field = build_decl (FIELD_DECL, pfn_identifier, type);
9220 fields = field;
9222 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9223 TREE_CHAIN (field) = fields;
9224 fields = field;
9226 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9228 /* Zap out the name so that the back-end will give us the debugging
9229 information for this anonymous RECORD_TYPE. */
9230 TYPE_NAME (t) = NULL_TREE;
9232 /* If this is not the unqualified form of this pointer-to-member
9233 type, set the TYPE_MAIN_VARIANT for this type to be the
9234 unqualified type. Since they are actually RECORD_TYPEs that are
9235 not variants of each other, we must do this manually. */
9236 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9238 t = build_qualified_type (t, cp_type_quals (type));
9239 TYPE_MAIN_VARIANT (t) = unqualified_variant;
9240 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9241 TYPE_NEXT_VARIANT (unqualified_variant) = t;
9244 /* Cache this pointer-to-member type so that we can find it again
9245 later. */
9246 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9248 /* Seems to be wanted. */
9249 CLASSTYPE_GOT_SEMICOLON (t) = 1;
9251 return t;
9254 /* Create and return a pointer to data member type. */
9256 tree
9257 build_ptrmem_type (tree class_type, tree member_type)
9259 return build_pointer_type (build_offset_type (class_type, member_type));
9262 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9263 Check to see that the definition is valid. Issue appropriate error
9264 messages. Return 1 if the definition is particularly bad, or 0
9265 otherwise. */
9268 check_static_variable_definition (tree decl, tree type)
9270 /* Motion 10 at San Diego: If a static const integral data member is
9271 initialized with an integral constant expression, the initializer
9272 may appear either in the declaration (within the class), or in
9273 the definition, but not both. If it appears in the class, the
9274 member is a member constant. The file-scope definition is always
9275 required. */
9276 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9278 error ("invalid in-class initialization of static data member of non-integral type `%T'",
9279 type);
9280 /* If we just return the declaration, crashes will sometimes
9281 occur. We therefore return void_type_node, as if this was a
9282 friend declaration, to cause callers to completely ignore
9283 this declaration. */
9284 return 1;
9286 else if (!CP_TYPE_CONST_P (type))
9287 error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9288 decl);
9289 else if (pedantic && !INTEGRAL_TYPE_P (type))
9290 pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9292 return 0;
9295 /* Given the SIZE (i.e., number of elements) in an array, compute an
9296 appropriate index type for the array. If non-NULL, NAME is the
9297 name of the thing being declared. */
9299 tree
9300 compute_array_index_type (tree name, tree size)
9302 tree itype;
9304 /* If this involves a template parameter, it will be a constant at
9305 instantiation time, but we don't know what the value is yet.
9306 Even if no template parameters are involved, we may an expression
9307 that is not a constant; we don't even simplify `1 + 2' when
9308 processing a template. */
9309 if (processing_template_decl)
9311 /* Resolve a qualified reference to an enumerator or static
9312 const data member of ours. */
9313 if (TREE_CODE (size) == SCOPE_REF
9314 && TREE_OPERAND (size, 0) == current_class_type)
9316 tree t = lookup_field (current_class_type,
9317 TREE_OPERAND (size, 1), 0, false);
9318 if (t)
9319 size = t;
9322 return build_index_type (build_min (MINUS_EXPR, sizetype,
9323 size, integer_one_node));
9326 /* The size might be the result of a cast. */
9327 STRIP_TYPE_NOPS (size);
9329 /* It might be a const variable or enumeration constant. */
9330 size = decl_constant_value (size);
9332 /* The array bound must be an integer type. */
9333 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9334 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9335 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9337 if (name)
9338 error ("size of array `%D' has non-integer type", name);
9339 else
9340 error ("size of array has non-integer type");
9341 size = integer_one_node;
9344 /* Normally, the array-bound will be a constant. */
9345 if (TREE_CODE (size) == INTEGER_CST)
9347 /* Check to see if the array bound overflowed. Make that an
9348 error, no matter how generous we're being. */
9349 int old_flag_pedantic_errors = flag_pedantic_errors;
9350 int old_pedantic = pedantic;
9351 pedantic = flag_pedantic_errors = 1;
9352 constant_expression_warning (size);
9353 pedantic = old_pedantic;
9354 flag_pedantic_errors = old_flag_pedantic_errors;
9356 /* An array must have a positive number of elements. */
9357 if (INT_CST_LT (size, integer_zero_node))
9359 if (name)
9360 error ("size of array `%D' is negative", name);
9361 else
9362 error ("size of array is negative");
9363 size = integer_one_node;
9365 /* Except that an extension we allow zero-sized arrays. We
9366 always allow them in system headers because glibc uses
9367 them. */
9368 else if (integer_zerop (size) && pedantic && !in_system_header)
9370 if (name)
9371 pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9372 else
9373 pedwarn ("ISO C++ forbids zero-size array");
9376 else if (TREE_CONSTANT (size))
9378 /* `(int) &fn' is not a valid array bound. */
9379 if (name)
9380 error ("size of array `%D' is not an integral constant-expression",
9381 name);
9382 else
9383 error ("size of array is not an integral constant-expression");
9386 /* Compute the index of the largest element in the array. It is
9387 one less than the number of elements in the array. */
9388 itype
9389 = fold (cp_build_binary_op (MINUS_EXPR,
9390 cp_convert (ssizetype, size),
9391 cp_convert (ssizetype,
9392 integer_one_node)));
9394 /* Check for variable-sized arrays. We allow such things as an
9395 extension, even though they are not allowed in ANSI/ISO C++. */
9396 if (!TREE_CONSTANT (itype))
9398 if (pedantic)
9400 if (name)
9401 pedwarn ("ISO C++ forbids variable-size array `%D'",
9402 name);
9403 else
9404 pedwarn ("ISO C++ forbids variable-size array");
9407 /* Create a variable-sized array index type. */
9408 itype = variable_size (itype);
9410 /* Make sure that there was no overflow when creating to a signed
9411 index type. (For example, on a 32-bit machine, an array with
9412 size 2^32 - 1 is too big.) */
9413 else if (TREE_OVERFLOW (itype))
9415 error ("overflow in array dimension");
9416 TREE_OVERFLOW (itype) = 0;
9419 /* Create and return the appropriate index type. */
9420 return build_index_type (itype);
9423 /* Returns the scope (if any) in which the entity declared by
9424 DECLARATOR will be located. If the entity was declared with an
9425 unqualified name, NULL_TREE is returned. */
9427 tree
9428 get_scope_of_declarator (tree declarator)
9430 if (!declarator)
9431 return NULL_TREE;
9433 switch (TREE_CODE (declarator))
9435 case CALL_EXPR:
9436 case ARRAY_REF:
9437 case INDIRECT_REF:
9438 case ADDR_EXPR:
9439 /* For any of these, the main declarator is the first operand. */
9440 return get_scope_of_declarator (TREE_OPERAND
9441 (declarator, 0));
9443 case SCOPE_REF:
9444 /* For a pointer-to-member, continue descending. */
9445 if (TREE_CODE (TREE_OPERAND (declarator, 1))
9446 == INDIRECT_REF)
9447 return get_scope_of_declarator (TREE_OPERAND
9448 (declarator, 1));
9449 /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9450 which the declaration occurs is the first operand. */
9451 return TREE_OPERAND (declarator, 0);
9453 case TREE_LIST:
9454 /* Attributes to be applied. The declarator is TREE_VALUE. */
9455 return get_scope_of_declarator (TREE_VALUE (declarator));
9457 default:
9458 /* Otherwise, we have a declarator-id which is not a qualified
9459 name; the entity will be declared in the current scope. */
9460 return NULL_TREE;
9464 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9465 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9466 with this type. */
9468 static tree
9469 create_array_type_for_decl (tree name, tree type, tree size)
9471 tree itype = NULL_TREE;
9472 const char* error_msg;
9474 /* If things have already gone awry, bail now. */
9475 if (type == error_mark_node || size == error_mark_node)
9476 return error_mark_node;
9478 /* Assume that everything will go OK. */
9479 error_msg = NULL;
9481 /* There are some types which cannot be array elements. */
9482 switch (TREE_CODE (type))
9484 case VOID_TYPE:
9485 error_msg = "array of void";
9486 break;
9488 case FUNCTION_TYPE:
9489 error_msg = "array of functions";
9490 break;
9492 case REFERENCE_TYPE:
9493 error_msg = "array of references";
9494 break;
9496 case OFFSET_TYPE:
9497 error_msg = "array of data members";
9498 break;
9500 case METHOD_TYPE:
9501 error_msg = "array of function members";
9502 break;
9504 default:
9505 break;
9508 /* If something went wrong, issue an error-message and return. */
9509 if (error_msg)
9511 if (name)
9512 error ("declaration of `%D' as %s", name, error_msg);
9513 else
9514 error ("creating %s", error_msg);
9516 return error_mark_node;
9519 /* [dcl.array]
9521 The constant expressions that specify the bounds of the arrays
9522 can be omitted only for the first member of the sequence. */
9523 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9525 if (name)
9526 error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9527 name);
9528 else
9529 error ("multidimensional array must have bounds for all dimensions except the first");
9531 return error_mark_node;
9534 /* Figure out the index type for the array. */
9535 if (size)
9536 itype = compute_array_index_type (name, size);
9538 return build_cplus_array_type (type, itype);
9541 /* Check that it's OK to declare a function with the indicated TYPE.
9542 SFK indicates the kind of special function (if any) that this
9543 function is. OPTYPE is the type given in a conversion operator
9544 declaration. Returns the actual return type of the function; that
9545 may be different than TYPE if an error occurs, or for certain
9546 special functions. */
9548 static tree
9549 check_special_function_return_type (special_function_kind sfk,
9550 tree type,
9551 tree optype)
9553 switch (sfk)
9555 case sfk_constructor:
9556 if (type)
9557 error ("return type specification for constructor invalid");
9559 type = void_type_node;
9560 break;
9562 case sfk_destructor:
9563 if (type)
9564 error ("return type specification for destructor invalid");
9565 type = void_type_node;
9566 break;
9568 case sfk_conversion:
9569 if (type && !same_type_p (type, optype))
9570 error ("operator `%T' declared to return `%T'", optype, type);
9571 else if (type)
9572 pedwarn ("return type specified for `operator %T'", optype);
9573 type = optype;
9574 break;
9576 default:
9577 abort ();
9578 break;
9581 return type;
9584 /* Given declspecs and a declarator (abstract or otherwise), determine
9585 the name and type of the object declared and construct a DECL node
9586 for it.
9588 DECLSPECS is a chain of tree_list nodes whose value fields
9589 are the storage classes and type specifiers.
9591 DECL_CONTEXT says which syntactic context this declaration is in:
9592 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9593 FUNCDEF for a function definition. Like NORMAL but a few different
9594 error messages in each case. Return value may be zero meaning
9595 this definition is too screwy to try to parse.
9596 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9597 handle member functions (which have FIELD context).
9598 Return value may be zero meaning this definition is too screwy to
9599 try to parse.
9600 PARM for a parameter declaration (either within a function prototype
9601 or before a function body). Make a PARM_DECL, or return void_type_node.
9602 CATCHPARM for a parameter declaration before a catch clause.
9603 TYPENAME if for a typename (in a cast or sizeof).
9604 Don't make a DECL node; just return the ..._TYPE node.
9605 FIELD for a struct or union field; make a FIELD_DECL.
9606 BITFIELD for a field with specified width.
9607 INITIALIZED is 1 if the decl has an initializer.
9609 ATTRLIST is a pointer to the list of attributes, which may be NULL
9610 if there are none; *ATTRLIST may be modified if attributes from inside
9611 the declarator should be applied to the declaration.
9613 When this function is called, scoping variables (such as
9614 CURRENT_CLASS_TYPE) should reflect the scope in which the
9615 declaration occurs, not the scope in which the new declaration will
9616 be placed. For example, on:
9618 void S::f() { ... }
9620 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9621 should not be `S'. */
9623 tree
9624 grokdeclarator (tree declarator,
9625 tree declspecs,
9626 enum decl_context decl_context,
9627 int initialized,
9628 tree* attrlist)
9630 RID_BIT_TYPE specbits;
9631 int nclasses = 0;
9632 tree spec;
9633 tree type = NULL_TREE;
9634 int longlong = 0;
9635 int type_quals;
9636 int virtualp, explicitp, friendp, inlinep, staticp;
9637 int explicit_int = 0;
9638 int explicit_char = 0;
9639 int defaulted_int = 0;
9640 int extern_langp = 0;
9641 tree dependant_name = NULL_TREE;
9643 tree typedef_decl = NULL_TREE;
9644 const char *name;
9645 tree typedef_type = NULL_TREE;
9646 int funcdef_flag = 0;
9647 enum tree_code innermost_code = ERROR_MARK;
9648 int bitfield = 0;
9649 #if 0
9650 /* See the code below that used this. */
9651 tree decl_attr = NULL_TREE;
9652 #endif
9654 /* Keep track of what sort of function is being processed
9655 so that we can warn about default return values, or explicit
9656 return values which do not match prescribed defaults. */
9657 special_function_kind sfk = sfk_none;
9659 tree dname = NULL_TREE;
9660 tree ctype = current_class_type;
9661 tree ctor_return_type = NULL_TREE;
9662 enum overload_flags flags = NO_SPECIAL;
9663 tree quals = NULL_TREE;
9664 tree raises = NULL_TREE;
9665 int template_count = 0;
9666 tree in_namespace = NULL_TREE;
9667 tree returned_attrs = NULL_TREE;
9668 tree scope = NULL_TREE;
9670 RIDBIT_RESET_ALL (specbits);
9671 if (decl_context == FUNCDEF)
9672 funcdef_flag = 1, decl_context = NORMAL;
9673 else if (decl_context == MEMFUNCDEF)
9674 funcdef_flag = -1, decl_context = FIELD;
9675 else if (decl_context == BITFIELD)
9676 bitfield = 1, decl_context = FIELD;
9678 /* Look inside a declarator for the name being declared
9679 and get it as a string, for an error message. */
9681 tree *next = &declarator;
9682 register tree decl;
9683 name = NULL;
9685 while (next && *next)
9687 decl = *next;
9688 switch (TREE_CODE (decl))
9690 case TREE_LIST:
9691 /* For attributes. */
9692 next = &TREE_VALUE (decl);
9693 break;
9695 case COND_EXPR:
9696 ctype = NULL_TREE;
9697 next = &TREE_OPERAND (decl, 0);
9698 break;
9700 case BIT_NOT_EXPR: /* For C++ destructors! */
9702 tree name = TREE_OPERAND (decl, 0);
9703 tree rename = NULL_TREE;
9705 my_friendly_assert (flags == NO_SPECIAL, 152);
9706 flags = DTOR_FLAG;
9707 sfk = sfk_destructor;
9708 if (TYPE_P (name))
9709 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9710 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9711 if (ctype == NULL_TREE)
9713 if (current_class_type == NULL_TREE)
9715 error ("destructors must be member functions");
9716 flags = NO_SPECIAL;
9718 else
9720 tree t = constructor_name (current_class_type);
9721 if (t != name)
9722 rename = t;
9725 else
9727 tree t = constructor_name (ctype);
9728 if (t != name)
9729 rename = t;
9732 if (rename)
9734 error ("destructor `%T' must match class name `%T'",
9735 name, rename);
9736 TREE_OPERAND (decl, 0) = rename;
9738 next = &name;
9740 break;
9742 case ADDR_EXPR: /* C++ reference declaration */
9743 /* Fall through. */
9744 case ARRAY_REF:
9745 case INDIRECT_REF:
9746 ctype = NULL_TREE;
9747 innermost_code = TREE_CODE (decl);
9748 next = &TREE_OPERAND (decl, 0);
9749 break;
9751 case CALL_EXPR:
9752 innermost_code = TREE_CODE (decl);
9753 if (decl_context == FIELD && ctype == NULL_TREE)
9754 ctype = current_class_type;
9755 if (ctype
9756 && TREE_OPERAND (decl, 0)
9757 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9758 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9759 ctype)))
9760 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9761 next = &TREE_OPERAND (decl, 0);
9762 decl = *next;
9763 if (ctype != NULL_TREE
9764 && decl != NULL_TREE && flags != DTOR_FLAG
9765 && constructor_name_p (decl, ctype))
9767 sfk = sfk_constructor;
9768 ctor_return_type = ctype;
9770 ctype = NULL_TREE;
9771 break;
9773 case TEMPLATE_ID_EXPR:
9775 tree fns = TREE_OPERAND (decl, 0);
9777 if (TREE_CODE (fns) == LOOKUP_EXPR)
9778 fns = TREE_OPERAND (fns, 0);
9780 dname = fns;
9781 if (TREE_CODE (dname) == COMPONENT_REF)
9782 dname = TREE_OPERAND (dname, 1);
9783 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9785 my_friendly_assert (is_overloaded_fn (dname),
9786 19990331);
9787 dname = DECL_NAME (get_first_fn (dname));
9790 /* Fall through. */
9792 case IDENTIFIER_NODE:
9793 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9794 dname = decl;
9796 next = 0;
9798 if (C_IS_RESERVED_WORD (dname))
9800 error ("declarator-id missing; using reserved word `%D'",
9801 dname);
9802 name = IDENTIFIER_POINTER (dname);
9804 else if (!IDENTIFIER_TYPENAME_P (dname))
9805 name = IDENTIFIER_POINTER (dname);
9806 else
9808 my_friendly_assert (flags == NO_SPECIAL, 154);
9809 flags = TYPENAME_FLAG;
9810 ctor_return_type = TREE_TYPE (dname);
9811 sfk = sfk_conversion;
9812 if (IDENTIFIER_GLOBAL_VALUE (dname)
9813 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9814 == TYPE_DECL))
9815 name = IDENTIFIER_POINTER (dname);
9816 else
9817 name = "<invalid operator>";
9819 break;
9821 /* C++ extension */
9822 case SCOPE_REF:
9824 /* Perform error checking, and decide on a ctype. */
9825 tree cname = TREE_OPERAND (decl, 0);
9826 if (cname == NULL_TREE)
9827 ctype = NULL_TREE;
9828 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9830 ctype = NULL_TREE;
9831 in_namespace = TREE_OPERAND (decl, 0);
9833 else if (! is_aggr_type (cname, 1))
9834 ctype = NULL_TREE;
9835 /* Must test TREE_OPERAND (decl, 1), in case user gives
9836 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9837 else if (TREE_OPERAND (decl, 1)
9838 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9839 ctype = cname;
9840 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9841 || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9843 /* This might be declaring a member of a template
9844 parm to be a friend. */
9845 ctype = cname;
9846 dependant_name = TREE_OPERAND (decl, 1);
9848 else if (ctype == NULL_TREE)
9849 ctype = cname;
9850 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9852 else
9854 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9856 error ("type `%T' is not derived from type `%T'",
9857 cname, ctype);
9858 ctype = NULL_TREE;
9860 else
9861 ctype = cname;
9864 /* It is valid to write:
9866 class C { void f(); };
9867 typedef C D;
9868 void D::f();
9870 The standard is not clear about whether `typedef const C D' is
9871 legal; as of 2002-09-15 the committee is considering
9872 that question. EDG 3.0 allows that syntax.
9873 Therefore, we do as well. */
9874 if (ctype)
9875 ctype = TYPE_MAIN_VARIANT (ctype);
9876 /* Update the declarator so that when we process it
9877 again the correct type is present. */
9878 TREE_OPERAND (decl, 0) = ctype;
9880 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9881 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9882 ctype))
9883 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9884 next = &TREE_OPERAND (decl, 1);
9885 decl = *next;
9886 if (ctype)
9888 if (TREE_CODE (decl) == IDENTIFIER_NODE
9889 && constructor_name_p (decl, ctype))
9891 sfk = sfk_constructor;
9892 ctor_return_type = ctype;
9894 else if (TREE_CODE (decl) == BIT_NOT_EXPR
9895 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9896 && constructor_name_p (TREE_OPERAND (decl, 0),
9897 ctype))
9899 sfk = sfk_destructor;
9900 ctor_return_type = ctype;
9901 flags = DTOR_FLAG;
9902 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9903 next = &TREE_OPERAND (decl, 0);
9907 break;
9909 case ERROR_MARK:
9910 next = 0;
9911 break;
9913 case TYPE_DECL:
9914 /* Parse error puts this typespec where
9915 a declarator should go. */
9916 error ("`%T' specified as declarator-id", DECL_NAME (decl));
9917 if (TREE_TYPE (decl) == current_class_type)
9918 error (" perhaps you want `%T' for a constructor",
9919 current_class_name);
9920 dname = DECL_NAME (decl);
9921 name = IDENTIFIER_POINTER (dname);
9923 /* Avoid giving two errors for this. */
9924 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9926 declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
9927 *next = dname;
9928 next = 0;
9929 break;
9931 case BASELINK:
9932 next = &BASELINK_FUNCTIONS (decl);
9933 break;
9935 case TEMPLATE_DECL:
9936 /* Sometimes, we see a template-name used as part of a
9937 decl-specifier like in
9938 std::allocator alloc;
9939 Handle that gracefully. */
9940 error ("invalid use of template-name '%E' in a declarator", decl);
9941 return error_mark_node;
9942 break;
9944 default:
9945 my_friendly_assert (0, 20020917);
9950 /* A function definition's declarator must have the form of
9951 a function declarator. */
9953 if (funcdef_flag && innermost_code != CALL_EXPR)
9954 return 0;
9956 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9957 && innermost_code != CALL_EXPR
9958 && ! (ctype && declspecs == NULL_TREE))
9960 error ("declaration of `%D' as non-function", dname);
9961 return void_type_node;
9964 /* Anything declared one level down from the top level
9965 must be one of the parameters of a function
9966 (because the body is at least two levels down). */
9968 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9969 by not allowing C++ class definitions to specify their parameters
9970 with xdecls (must be spec.d in the parmlist).
9972 Since we now wait to push a class scope until we are sure that
9973 we are in a legitimate method context, we must set oldcname
9974 explicitly (since current_class_name is not yet alive).
9976 We also want to avoid calling this a PARM if it is in a namespace. */
9978 if (decl_context == NORMAL && !toplevel_bindings_p ())
9980 struct cp_binding_level *b = current_binding_level;
9981 current_binding_level = b->level_chain;
9982 if (current_binding_level != 0 && toplevel_bindings_p ())
9983 decl_context = PARM;
9984 current_binding_level = b;
9987 if (name == NULL)
9988 name = decl_context == PARM ? "parameter" : "type name";
9990 /* Look through the decl specs and record which ones appear.
9991 Some typespecs are defined as built-in typenames.
9992 Others, the ones that are modifiers of other types,
9993 are represented by bits in SPECBITS: set the bits for
9994 the modifiers that appear. Storage class keywords are also in SPECBITS.
9996 If there is a typedef name or a type, store the type in TYPE.
9997 This includes builtin typedefs such as `int'.
9999 Set EXPLICIT_INT if the type is `int' or `char' and did not
10000 come from a user typedef.
10002 Set LONGLONG if `long' is mentioned twice.
10004 For C++, constructors and destructors have their own fast treatment. */
10006 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10008 register int i;
10009 register tree id;
10011 /* Certain parse errors slip through. For example,
10012 `int class;' is not caught by the parser. Try
10013 weakly to recover here. */
10014 if (TREE_CODE (spec) != TREE_LIST)
10015 return 0;
10017 id = TREE_VALUE (spec);
10019 /* If the entire declaration is itself tagged as deprecated then
10020 suppress reports of deprecated items. */
10021 if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10023 if (deprecated_state != DEPRECATED_SUPPRESS)
10024 warn_deprecated_use (id);
10027 if (TREE_CODE (id) == IDENTIFIER_NODE)
10029 if (id == ridpointers[(int) RID_INT]
10030 || id == ridpointers[(int) RID_CHAR]
10031 || id == ridpointers[(int) RID_BOOL]
10032 || id == ridpointers[(int) RID_WCHAR])
10034 if (type)
10036 if (id == ridpointers[(int) RID_BOOL])
10037 error ("`bool' is now a keyword");
10038 else
10039 error ("extraneous `%T' ignored", id);
10041 else
10043 if (id == ridpointers[(int) RID_INT])
10044 explicit_int = 1;
10045 else if (id == ridpointers[(int) RID_CHAR])
10046 explicit_char = 1;
10047 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10049 goto found;
10051 /* C++ aggregate types. */
10052 if (IDENTIFIER_HAS_TYPE_VALUE (id))
10054 if (type)
10055 error ("multiple declarations `%T' and `%T'", type, id);
10056 else
10057 type = IDENTIFIER_TYPE_VALUE (id);
10058 goto found;
10061 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10063 if (ridpointers[i] == id)
10065 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10067 if (pedantic && ! in_system_header && warn_long_long)
10068 pedwarn ("ISO C++ does not support `long long'");
10069 if (longlong)
10070 error ("`long long long' is too long for GCC");
10071 else
10072 longlong = 1;
10074 else if (RIDBIT_SETP (i, specbits))
10075 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10077 /* Diagnose "__thread extern" or "__thread static". */
10078 if (RIDBIT_SETP (RID_THREAD, specbits))
10080 if (i == (int)RID_EXTERN)
10081 error ("`__thread' before `extern'");
10082 else if (i == (int)RID_STATIC)
10083 error ("`__thread' before `static'");
10086 if (i == (int)RID_EXTERN
10087 && TREE_PURPOSE (spec) == error_mark_node)
10088 /* This extern was part of a language linkage. */
10089 extern_langp = 1;
10091 RIDBIT_SET (i, specbits);
10092 goto found;
10096 else if (TREE_CODE (id) == TYPE_DECL)
10098 if (type)
10099 error ("multiple declarations `%T' and `%T'", type,
10100 TREE_TYPE (id));
10101 else
10103 type = TREE_TYPE (id);
10104 TREE_VALUE (spec) = type;
10105 typedef_decl = id;
10107 goto found;
10109 if (type)
10110 error ("two or more data types in declaration of `%s'", name);
10111 else if (TREE_CODE (id) == IDENTIFIER_NODE)
10113 register tree t = lookup_name (id, 1);
10114 if (!t || TREE_CODE (t) != TYPE_DECL)
10115 error ("`%s' fails to be a typedef or built in type",
10116 IDENTIFIER_POINTER (id));
10117 else
10119 type = TREE_TYPE (t);
10120 typedef_decl = t;
10123 else if (id != error_mark_node)
10124 /* Can't change CLASS nodes into RECORD nodes here! */
10125 type = id;
10127 found: ;
10130 #if 0
10131 /* See the code below that used this. */
10132 if (typedef_decl)
10133 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10134 #endif
10135 typedef_type = type;
10137 /* No type at all: default to `int', and set DEFAULTED_INT
10138 because it was not a user-defined typedef. */
10140 if (type == NULL_TREE
10141 && (RIDBIT_SETP (RID_SIGNED, specbits)
10142 || RIDBIT_SETP (RID_UNSIGNED, specbits)
10143 || RIDBIT_SETP (RID_LONG, specbits)
10144 || RIDBIT_SETP (RID_SHORT, specbits)))
10146 /* These imply 'int'. */
10147 type = integer_type_node;
10148 defaulted_int = 1;
10151 if (sfk != sfk_none)
10152 type = check_special_function_return_type (sfk, type,
10153 ctor_return_type);
10154 else if (type == NULL_TREE)
10156 int is_main;
10158 explicit_int = -1;
10160 /* We handle `main' specially here, because 'main () { }' is so
10161 common. With no options, it is allowed. With -Wreturn-type,
10162 it is a warning. It is only an error with -pedantic-errors. */
10163 is_main = (funcdef_flag
10164 && dname && MAIN_NAME_P (dname)
10165 && ctype == NULL_TREE
10166 && in_namespace == NULL_TREE
10167 && current_namespace == global_namespace);
10169 if (in_system_header || flag_ms_extensions)
10170 /* Allow it, sigh. */;
10171 else if (pedantic || ! is_main)
10172 pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10173 name);
10174 else if (warn_return_type)
10175 warning ("ISO C++ forbids declaration of `%s' with no type",
10176 name);
10178 type = integer_type_node;
10181 ctype = NULL_TREE;
10183 /* Now process the modifiers that were specified
10184 and check for invalid combinations. */
10186 /* Long double is a special combination. */
10188 if (RIDBIT_SETP (RID_LONG, specbits)
10189 && TYPE_MAIN_VARIANT (type) == double_type_node)
10191 RIDBIT_RESET (RID_LONG, specbits);
10192 type = build_qualified_type (long_double_type_node,
10193 cp_type_quals (type));
10196 /* Check all other uses of type modifiers. */
10198 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10199 || RIDBIT_SETP (RID_SIGNED, specbits)
10200 || RIDBIT_SETP (RID_LONG, specbits)
10201 || RIDBIT_SETP (RID_SHORT, specbits))
10203 int ok = 0;
10205 if (TREE_CODE (type) == REAL_TYPE)
10206 error ("short, signed or unsigned invalid for `%s'", name);
10207 else if (TREE_CODE (type) != INTEGER_TYPE)
10208 error ("long, short, signed or unsigned invalid for `%s'", name);
10209 else if (RIDBIT_SETP (RID_LONG, specbits)
10210 && RIDBIT_SETP (RID_SHORT, specbits))
10211 error ("long and short specified together for `%s'", name);
10212 else if ((RIDBIT_SETP (RID_LONG, specbits)
10213 || RIDBIT_SETP (RID_SHORT, specbits))
10214 && explicit_char)
10215 error ("long or short specified with char for `%s'", name);
10216 else if ((RIDBIT_SETP (RID_LONG, specbits)
10217 || RIDBIT_SETP (RID_SHORT, specbits))
10218 && TREE_CODE (type) == REAL_TYPE)
10219 error ("long or short specified with floating type for `%s'", name);
10220 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10221 && RIDBIT_SETP (RID_UNSIGNED, specbits))
10222 error ("signed and unsigned given together for `%s'", name);
10223 else
10225 ok = 1;
10226 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10228 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10229 name);
10230 if (flag_pedantic_errors)
10231 ok = 0;
10235 /* Discard the type modifiers if they are invalid. */
10236 if (! ok)
10238 RIDBIT_RESET (RID_UNSIGNED, specbits);
10239 RIDBIT_RESET (RID_SIGNED, specbits);
10240 RIDBIT_RESET (RID_LONG, specbits);
10241 RIDBIT_RESET (RID_SHORT, specbits);
10242 longlong = 0;
10246 if (RIDBIT_SETP (RID_COMPLEX, specbits)
10247 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10249 error ("complex invalid for `%s'", name);
10250 RIDBIT_RESET (RID_COMPLEX, specbits);
10253 /* Decide whether an integer type is signed or not.
10254 Optionally treat bitfields as signed by default. */
10255 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10256 /* [class.bit]
10258 It is implementation-defined whether a plain (neither
10259 explicitly signed or unsigned) char, short, int, or long
10260 bit-field is signed or unsigned.
10262 Naturally, we extend this to long long as well. Note that
10263 this does not include wchar_t. */
10264 || (bitfield && !flag_signed_bitfields
10265 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10266 /* A typedef for plain `int' without `signed' can be
10267 controlled just like plain `int', but a typedef for
10268 `signed int' cannot be so controlled. */
10269 && !(typedef_decl
10270 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10271 && (TREE_CODE (type) == INTEGER_TYPE
10272 || TREE_CODE (type) == CHAR_TYPE)
10273 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10275 if (longlong)
10276 type = long_long_unsigned_type_node;
10277 else if (RIDBIT_SETP (RID_LONG, specbits))
10278 type = long_unsigned_type_node;
10279 else if (RIDBIT_SETP (RID_SHORT, specbits))
10280 type = short_unsigned_type_node;
10281 else if (type == char_type_node)
10282 type = unsigned_char_type_node;
10283 else if (typedef_decl)
10284 type = c_common_unsigned_type (type);
10285 else
10286 type = unsigned_type_node;
10288 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10289 && type == char_type_node)
10290 type = signed_char_type_node;
10291 else if (longlong)
10292 type = long_long_integer_type_node;
10293 else if (RIDBIT_SETP (RID_LONG, specbits))
10294 type = long_integer_type_node;
10295 else if (RIDBIT_SETP (RID_SHORT, specbits))
10296 type = short_integer_type_node;
10298 if (RIDBIT_SETP (RID_COMPLEX, specbits))
10300 /* If we just have "complex", it is equivalent to
10301 "complex double", but if any modifiers at all are specified it is
10302 the complex form of TYPE. E.g, "complex short" is
10303 "complex short int". */
10305 if (defaulted_int && ! longlong
10306 && ! (RIDBIT_SETP (RID_LONG, specbits)
10307 || RIDBIT_SETP (RID_SHORT, specbits)
10308 || RIDBIT_SETP (RID_SIGNED, specbits)
10309 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10310 type = complex_double_type_node;
10311 else if (type == integer_type_node)
10312 type = complex_integer_type_node;
10313 else if (type == float_type_node)
10314 type = complex_float_type_node;
10315 else if (type == double_type_node)
10316 type = complex_double_type_node;
10317 else if (type == long_double_type_node)
10318 type = complex_long_double_type_node;
10319 else
10320 type = build_complex_type (type);
10323 type_quals = TYPE_UNQUALIFIED;
10324 if (RIDBIT_SETP (RID_CONST, specbits))
10325 type_quals |= TYPE_QUAL_CONST;
10326 if (RIDBIT_SETP (RID_VOLATILE, specbits))
10327 type_quals |= TYPE_QUAL_VOLATILE;
10328 if (RIDBIT_SETP (RID_RESTRICT, specbits))
10329 type_quals |= TYPE_QUAL_RESTRICT;
10330 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10331 error ("qualifiers are not allowed on declaration of `operator %T'",
10332 ctor_return_type);
10334 type_quals |= cp_type_quals (type);
10335 type = cp_build_qualified_type_real
10336 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10337 ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10338 /* We might have ignored or rejected some of the qualifiers. */
10339 type_quals = cp_type_quals (type);
10341 staticp = 0;
10342 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10343 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10344 RIDBIT_RESET (RID_VIRTUAL, specbits);
10345 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10346 RIDBIT_RESET (RID_EXPLICIT, specbits);
10348 if (RIDBIT_SETP (RID_STATIC, specbits))
10349 staticp = 1 + (decl_context == FIELD);
10351 if (virtualp && staticp == 2)
10353 error ("member `%D' cannot be declared both virtual and static",
10354 dname);
10355 staticp = 0;
10357 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10358 RIDBIT_RESET (RID_FRIEND, specbits);
10360 if (dependant_name && !friendp)
10362 error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10363 return void_type_node;
10366 /* Warn if two storage classes are given. Default to `auto'. */
10368 if (RIDBIT_ANY_SET (specbits))
10370 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10371 if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10372 if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10373 if (decl_context == PARM && nclasses > 0)
10374 error ("storage class specifiers invalid in parameter declarations");
10375 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10377 if (decl_context == PARM)
10378 error ("typedef declaration invalid in parameter declaration");
10379 nclasses++;
10381 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10382 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10383 if (!nclasses && !friendp && extern_langp)
10384 nclasses++;
10387 /* Give error if `virtual' is used outside of class declaration. */
10388 if (virtualp
10389 && (current_class_name == NULL_TREE || decl_context != FIELD))
10391 error ("virtual outside class declaration");
10392 virtualp = 0;
10395 /* Static anonymous unions are dealt with here. */
10396 if (staticp && decl_context == TYPENAME
10397 && TREE_CODE (declspecs) == TREE_LIST
10398 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10399 decl_context = FIELD;
10401 /* Warn about storage classes that are invalid for certain
10402 kinds of declarations (parameters, typenames, etc.). */
10404 /* "static __thread" and "extern __thread" are allowed. */
10405 if (nclasses == 2
10406 && RIDBIT_SETP (RID_THREAD, specbits)
10407 && (RIDBIT_SETP (RID_EXTERN, specbits)
10408 || RIDBIT_SETP (RID_STATIC, specbits)))
10409 nclasses = 1;
10411 if (nclasses > 1)
10412 error ("multiple storage classes in declaration of `%s'", name);
10413 else if (decl_context != NORMAL && nclasses > 0)
10415 if ((decl_context == PARM || decl_context == CATCHPARM)
10416 && (RIDBIT_SETP (RID_REGISTER, specbits)
10417 || RIDBIT_SETP (RID_AUTO, specbits)))
10419 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10421 else if (decl_context == FIELD
10422 /* C++ allows static class elements */
10423 && RIDBIT_SETP (RID_STATIC, specbits))
10424 /* C++ also allows inlines and signed and unsigned elements,
10425 but in those cases we don't come in here. */
10427 else
10429 if (decl_context == FIELD)
10431 tree tmp = NULL_TREE;
10432 register int op = 0;
10434 if (declarator)
10436 /* Avoid trying to get an operand off an identifier node. */
10437 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10438 tmp = declarator;
10439 else
10440 tmp = TREE_OPERAND (declarator, 0);
10441 op = IDENTIFIER_OPNAME_P (tmp);
10442 if (IDENTIFIER_TYPENAME_P (tmp))
10444 if (IDENTIFIER_GLOBAL_VALUE (tmp)
10445 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10446 == TYPE_DECL))
10447 name = IDENTIFIER_POINTER (tmp);
10448 else
10449 name = "<invalid operator>";
10452 error ("storage class specified for %s `%s'",
10453 op ? "member operator" : "field",
10454 name);
10456 else
10458 if (decl_context == PARM || decl_context == CATCHPARM)
10459 error ("storage class specified for parameter `%s'", name);
10460 else
10461 error ("storage class specified for typename");
10463 RIDBIT_RESET (RID_REGISTER, specbits);
10464 RIDBIT_RESET (RID_AUTO, specbits);
10465 RIDBIT_RESET (RID_EXTERN, specbits);
10466 RIDBIT_RESET (RID_THREAD, specbits);
10469 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10471 if (toplevel_bindings_p ())
10473 /* It's common practice (and completely valid) to have a const
10474 be initialized and declared extern. */
10475 if (!(type_quals & TYPE_QUAL_CONST))
10476 warning ("`%s' initialized and declared `extern'", name);
10478 else
10479 error ("`%s' has both `extern' and initializer", name);
10481 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10482 && ! toplevel_bindings_p ())
10483 error ("nested function `%s' declared `extern'", name);
10484 else if (toplevel_bindings_p ())
10486 if (RIDBIT_SETP (RID_AUTO, specbits))
10487 error ("top-level declaration of `%s' specifies `auto'", name);
10489 else if (RIDBIT_SETP (RID_THREAD, specbits)
10490 && !RIDBIT_SETP (RID_EXTERN, specbits)
10491 && !RIDBIT_SETP (RID_STATIC, specbits))
10493 error ("function-scope `%s' implicitly auto and declared `__thread'",
10494 name);
10495 RIDBIT_RESET (RID_THREAD, specbits);
10498 if (nclasses > 0 && friendp)
10499 error ("storage class specifiers invalid in friend function declarations");
10501 scope = get_scope_of_declarator (declarator);
10503 /* Now figure out the structure of the declarator proper.
10504 Descend through it, creating more complex types, until we reach
10505 the declared identifier (or NULL_TREE, in an abstract declarator). */
10507 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10508 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10510 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10511 an INDIRECT_REF (for *...),
10512 a CALL_EXPR (for ...(...)),
10513 an identifier (for the name being declared)
10514 or a null pointer (for the place in an absolute declarator
10515 where the name was omitted).
10516 For the last two cases, we have just exited the loop.
10518 For C++ it could also be
10519 a SCOPE_REF (for class :: ...). In this case, we have converted
10520 sensible names to types, and those are the values we use to
10521 qualify the member name.
10522 an ADDR_EXPR (for &...),
10523 a BIT_NOT_EXPR (for destructors)
10525 At this point, TYPE is the type of elements of an array,
10526 or for a function to return, or for a pointer to point to.
10527 After this sequence of ifs, TYPE is the type of the
10528 array or function or pointer, and DECLARATOR has had its
10529 outermost layer removed. */
10531 if (type == error_mark_node)
10533 if (TREE_CODE (declarator) == SCOPE_REF)
10534 declarator = TREE_OPERAND (declarator, 1);
10535 else
10536 declarator = TREE_OPERAND (declarator, 0);
10537 continue;
10539 if (quals != NULL_TREE
10540 && (declarator == NULL_TREE
10541 || TREE_CODE (declarator) != SCOPE_REF))
10543 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10544 ctype = TYPE_METHOD_BASETYPE (type);
10545 if (ctype != NULL_TREE)
10547 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10548 grok_method_quals (ctype, dummy, quals);
10549 type = TREE_TYPE (dummy);
10550 quals = NULL_TREE;
10554 switch (TREE_CODE (declarator))
10556 case TREE_LIST:
10558 /* We encode a declarator with embedded attributes using
10559 a TREE_LIST. */
10560 tree attrs = TREE_PURPOSE (declarator);
10561 tree inner_decl;
10562 int attr_flags;
10564 declarator = TREE_VALUE (declarator);
10565 inner_decl = declarator;
10566 while (inner_decl != NULL_TREE
10567 && TREE_CODE (inner_decl) == TREE_LIST)
10568 inner_decl = TREE_VALUE (inner_decl);
10569 attr_flags = 0;
10570 if (inner_decl == NULL_TREE
10571 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10572 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10573 if (TREE_CODE (inner_decl) == CALL_EXPR)
10574 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10575 if (TREE_CODE (inner_decl) == ARRAY_REF)
10576 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10577 returned_attrs = decl_attributes (&type,
10578 chainon (returned_attrs, attrs),
10579 attr_flags);
10581 break;
10583 case ARRAY_REF:
10585 register tree size;
10587 size = TREE_OPERAND (declarator, 1);
10589 /* VC++ spells a zero-sized array with []. */
10590 if (size == NULL_TREE && decl_context == FIELD && ! staticp
10591 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
10592 size = integer_zero_node;
10594 declarator = TREE_OPERAND (declarator, 0);
10596 type = create_array_type_for_decl (dname, type, size);
10598 ctype = NULL_TREE;
10600 break;
10602 case CALL_EXPR:
10604 tree arg_types;
10605 int funcdecl_p;
10606 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10607 tree inner_decl = TREE_OPERAND (declarator, 0);
10609 /* Declaring a function type.
10610 Make sure we have a valid type for the function to return. */
10612 /* We now know that the TYPE_QUALS don't apply to the
10613 decl, but to its return type. */
10614 type_quals = TYPE_UNQUALIFIED;
10616 /* Warn about some types functions can't return. */
10618 if (TREE_CODE (type) == FUNCTION_TYPE)
10620 error ("`%s' declared as function returning a function", name);
10621 type = integer_type_node;
10623 if (TREE_CODE (type) == ARRAY_TYPE)
10625 error ("`%s' declared as function returning an array", name);
10626 type = integer_type_node;
10629 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10630 inner_decl = TREE_OPERAND (inner_decl, 1);
10632 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10633 inner_decl = dname;
10635 /* Pick up type qualifiers which should be applied to `this'. */
10636 quals = CALL_DECLARATOR_QUALS (declarator);
10638 /* Pick up the exception specifications. */
10639 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10641 /* Say it's a definition only for the CALL_EXPR
10642 closest to the identifier. */
10643 funcdecl_p
10644 = inner_decl
10645 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10646 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10647 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10649 if (ctype == NULL_TREE
10650 && decl_context == FIELD
10651 && funcdecl_p
10652 && (friendp == 0 || dname == current_class_name))
10653 ctype = current_class_type;
10655 if (ctype && sfk == sfk_conversion)
10656 TYPE_HAS_CONVERSION (ctype) = 1;
10657 if (ctype && constructor_name_p (dname, ctype))
10659 /* We are within a class's scope. If our declarator name
10660 is the same as the class name, and we are defining
10661 a function, then it is a constructor/destructor, and
10662 therefore returns a void type. */
10664 if (flags == DTOR_FLAG)
10666 /* ISO C++ 12.4/2. A destructor may not be
10667 declared const or volatile. A destructor may
10668 not be static. */
10669 if (staticp == 2)
10670 error ("destructor cannot be static member function");
10671 if (quals)
10673 error ("destructors may not be `%s'",
10674 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10675 quals = NULL_TREE;
10677 if (decl_context == FIELD)
10679 if (! member_function_or_else (ctype,
10680 current_class_type,
10681 flags))
10682 return void_type_node;
10685 else /* It's a constructor. */
10687 if (explicitp == 1)
10688 explicitp = 2;
10689 /* ISO C++ 12.1. A constructor may not be
10690 declared const or volatile. A constructor may
10691 not be virtual. A constructor may not be
10692 static. */
10693 if (staticp == 2)
10694 error ("constructor cannot be static member function");
10695 if (virtualp)
10697 pedwarn ("constructors cannot be declared virtual");
10698 virtualp = 0;
10700 if (quals)
10702 error ("constructors may not be `%s'",
10703 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10704 quals = NULL_TREE;
10707 RID_BIT_TYPE tmp_bits;
10708 memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10709 RIDBIT_RESET (RID_INLINE, tmp_bits);
10710 RIDBIT_RESET (RID_STATIC, tmp_bits);
10711 if (RIDBIT_ANY_SET (tmp_bits))
10712 error ("return value type specifier for constructor ignored");
10714 if (decl_context == FIELD)
10716 if (! member_function_or_else (ctype,
10717 current_class_type,
10718 flags))
10719 return void_type_node;
10720 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10721 if (sfk != sfk_constructor)
10722 return NULL_TREE;
10725 if (decl_context == FIELD)
10726 staticp = 0;
10728 else if (friendp)
10730 if (initialized)
10731 error ("can't initialize friend function `%s'", name);
10732 if (virtualp)
10734 /* Cannot be both friend and virtual. */
10735 error ("virtual functions cannot be friends");
10736 RIDBIT_RESET (RID_FRIEND, specbits);
10737 friendp = 0;
10739 if (decl_context == NORMAL)
10740 error ("friend declaration not in class definition");
10741 if (current_function_decl && funcdef_flag)
10742 error ("can't define friend function `%s' in a local class definition",
10743 name);
10746 /* Construct the function type and go to the next
10747 inner layer of declarator. */
10749 declarator = TREE_OPERAND (declarator, 0);
10751 /* FIXME: This is where default args should be fully
10752 processed. */
10754 arg_types = grokparms (inner_parms);
10756 if (declarator && flags == DTOR_FLAG)
10758 /* A destructor declared in the body of a class will
10759 be represented as a BIT_NOT_EXPR. But, we just
10760 want the underlying IDENTIFIER. */
10761 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10762 declarator = TREE_OPERAND (declarator, 0);
10764 if (arg_types != void_list_node)
10766 error ("destructors may not have parameters");
10767 arg_types = void_list_node;
10768 last_function_parms = NULL_TREE;
10772 /* ANSI says that `const int foo ();'
10773 does not make the function foo const. */
10774 type = build_function_type (type, arg_types);
10776 break;
10778 case ADDR_EXPR:
10779 case INDIRECT_REF:
10780 /* Filter out pointers-to-references and references-to-references.
10781 We can get these if a TYPE_DECL is used. */
10783 if (TREE_CODE (type) == REFERENCE_TYPE)
10785 error (TREE_CODE (declarator) == ADDR_EXPR
10786 ? "cannot declare reference to `%#T'"
10787 : "cannot declare pointer to `%#T'", type);
10788 type = TREE_TYPE (type);
10790 else if (VOID_TYPE_P (type)
10791 && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10792 error (ctype ? "cannot declare pointer to `%#T' member"
10793 : "cannot declare reference to `%#T'", type);
10795 /* Merge any constancy or volatility into the target type
10796 for the pointer. */
10798 /* We now know that the TYPE_QUALS don't apply to the decl,
10799 but to the target of the pointer. */
10800 type_quals = TYPE_UNQUALIFIED;
10802 if (TREE_CODE (declarator) == ADDR_EXPR)
10804 if (!VOID_TYPE_P (type))
10805 type = build_reference_type (type);
10807 else if (TREE_CODE (type) == METHOD_TYPE)
10808 type = build_ptrmemfunc_type (build_pointer_type (type));
10809 else if (ctype)
10810 type = build_ptrmem_type (ctype, type);
10811 else
10812 type = build_pointer_type (type);
10814 /* Process a list of type modifier keywords (such as
10815 const or volatile) that were given inside the `*' or `&'. */
10817 if (TREE_TYPE (declarator))
10819 register tree typemodlist;
10820 int erred = 0;
10821 int constp = 0;
10822 int volatilep = 0;
10823 int restrictp = 0;
10825 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10826 typemodlist = TREE_CHAIN (typemodlist))
10828 tree qualifier = TREE_VALUE (typemodlist);
10830 if (qualifier == ridpointers[(int) RID_CONST])
10832 constp++;
10833 type_quals |= TYPE_QUAL_CONST;
10835 else if (qualifier == ridpointers[(int) RID_VOLATILE])
10837 volatilep++;
10838 type_quals |= TYPE_QUAL_VOLATILE;
10840 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10842 restrictp++;
10843 type_quals |= TYPE_QUAL_RESTRICT;
10845 else if (!erred)
10847 erred = 1;
10848 error ("invalid type modifier within pointer declarator");
10851 if (constp > 1)
10852 pedwarn ("duplicate `const'");
10853 if (volatilep > 1)
10854 pedwarn ("duplicate `volatile'");
10855 if (restrictp > 1)
10856 pedwarn ("duplicate `restrict'");
10857 type = cp_build_qualified_type (type, type_quals);
10858 type_quals = cp_type_quals (type);
10860 declarator = TREE_OPERAND (declarator, 0);
10861 ctype = NULL_TREE;
10862 break;
10864 case SCOPE_REF:
10866 /* We have converted type names to NULL_TREE if the
10867 name was bogus, or to a _TYPE node, if not.
10869 The variable CTYPE holds the type we will ultimately
10870 resolve to. The code here just needs to build
10871 up appropriate member types. */
10872 tree sname = TREE_OPERAND (declarator, 1);
10873 tree t;
10875 /* Destructors can have their visibilities changed as well. */
10876 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10877 sname = TREE_OPERAND (sname, 0);
10879 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10881 /* We had a reference to a global decl, or
10882 perhaps we were given a non-aggregate typedef,
10883 in which case we cleared this out, and should just
10884 keep going as though it wasn't there. */
10885 declarator = sname;
10886 continue;
10888 ctype = TREE_OPERAND (declarator, 0);
10890 t = ctype;
10891 while (t != NULL_TREE && CLASS_TYPE_P (t))
10893 /* You're supposed to have one `template <...>'
10894 for every template class, but you don't need one
10895 for a full specialization. For example:
10897 template <class T> struct S{};
10898 template <> struct S<int> { void f(); };
10899 void S<int>::f () {}
10901 is correct; there shouldn't be a `template <>' for
10902 the definition of `S<int>::f'. */
10903 if (CLASSTYPE_TEMPLATE_INFO (t)
10904 && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
10905 || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10906 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
10907 template_count += 1;
10909 t = TYPE_MAIN_DECL (t);
10910 t = DECL_CONTEXT (t);
10913 if (sname == NULL_TREE)
10914 goto done_scoping;
10916 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10918 /* This is the `standard' use of the scoping operator:
10919 basetype :: member . */
10921 if (ctype == current_class_type)
10923 /* class A {
10924 void A::f ();
10927 Is this ill-formed? */
10929 if (pedantic)
10930 pedwarn ("extra qualification `%T::' on member `%s' ignored",
10931 ctype, name);
10933 else if (TREE_CODE (type) == FUNCTION_TYPE)
10935 if (current_class_type == NULL_TREE || friendp)
10936 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10937 TYPE_ARG_TYPES (type));
10938 else
10940 error ("cannot declare member function `%T::%s' within `%T'",
10941 ctype, name, current_class_type);
10942 return error_mark_node;
10945 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10946 || COMPLETE_TYPE_P (complete_type (ctype)))
10948 /* Have to move this code elsewhere in this function.
10949 this code is used for i.e., typedef int A::M; M *pm;
10951 It is? How? jason 10/2/94 */
10953 if (current_class_type)
10955 error ("cannot declare member `%T::%s' within `%T'",
10956 ctype, name, current_class_type);
10957 return void_type_node;
10960 else
10962 cxx_incomplete_type_error (NULL_TREE, ctype);
10963 return error_mark_node;
10966 declarator = sname;
10968 else if (TREE_CODE (sname) == SCOPE_REF)
10969 abort ();
10970 else
10972 done_scoping:
10973 declarator = TREE_OPERAND (declarator, 1);
10974 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10975 /* In this case, we will deal with it later. */
10977 else if (TREE_CODE (type) == FUNCTION_TYPE)
10978 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10979 TYPE_ARG_TYPES (type));
10982 break;
10984 case BIT_NOT_EXPR:
10985 declarator = TREE_OPERAND (declarator, 0);
10986 break;
10988 case BASELINK:
10989 declarator = BASELINK_FUNCTIONS (declarator);
10990 break;
10992 case RECORD_TYPE:
10993 case UNION_TYPE:
10994 case ENUMERAL_TYPE:
10995 declarator = NULL_TREE;
10996 break;
10998 case ERROR_MARK:
10999 declarator = NULL_TREE;
11000 break;
11002 default:
11003 abort ();
11007 if (returned_attrs)
11009 if (attrlist)
11010 *attrlist = chainon (returned_attrs, *attrlist);
11011 else
11012 attrlist = &returned_attrs;
11015 /* Now TYPE has the actual type. */
11017 /* Did array size calculations overflow? */
11019 if (TREE_CODE (type) == ARRAY_TYPE
11020 && COMPLETE_TYPE_P (type)
11021 && TREE_OVERFLOW (TYPE_SIZE (type)))
11023 error ("size of array `%s' is too large", name);
11024 /* If we proceed with the array type as it is, we'll eventually
11025 crash in tree_low_cst(). */
11026 type = error_mark_node;
11029 if ((decl_context == FIELD || decl_context == PARM)
11030 && !processing_template_decl
11031 && variably_modified_type_p (type))
11033 if (decl_context == FIELD)
11034 error ("data member may not have variably modified type `%T'", type);
11035 else
11036 error ("parameter may not have variably modified type `%T'", type);
11037 type = error_mark_node;
11040 if (explicitp == 1 || (explicitp && friendp))
11042 /* [dcl.fct.spec] The explicit specifier shall only be used in
11043 declarations of constructors within a class definition. */
11044 error ("only declarations of constructors can be `explicit'");
11045 explicitp = 0;
11048 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11050 if (decl_context != FIELD || friendp)
11052 error ("non-member `%s' cannot be declared `mutable'", name);
11053 RIDBIT_RESET (RID_MUTABLE, specbits);
11055 else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11057 error ("non-object member `%s' cannot be declared `mutable'", name);
11058 RIDBIT_RESET (RID_MUTABLE, specbits);
11060 else if (TREE_CODE (type) == FUNCTION_TYPE
11061 || TREE_CODE (type) == METHOD_TYPE)
11063 error ("function `%s' cannot be declared `mutable'", name);
11064 RIDBIT_RESET (RID_MUTABLE, specbits);
11066 else if (staticp)
11068 error ("static `%s' cannot be declared `mutable'", name);
11069 RIDBIT_RESET (RID_MUTABLE, specbits);
11071 else if (type_quals & TYPE_QUAL_CONST)
11073 error ("const `%s' cannot be declared `mutable'", name);
11074 RIDBIT_RESET (RID_MUTABLE, specbits);
11078 if (declarator == NULL_TREE
11079 || TREE_CODE (declarator) == IDENTIFIER_NODE
11080 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11081 && (TREE_CODE (type) == FUNCTION_TYPE
11082 || TREE_CODE (type) == METHOD_TYPE)))
11083 /* OK */;
11084 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11086 error ("template-id `%D' used as a declarator", declarator);
11087 declarator = dname;
11089 else
11090 /* Unexpected declarator format. */
11091 abort ();
11093 /* If this is declaring a typedef name, return a TYPE_DECL. */
11095 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11097 tree decl;
11099 /* Note that the grammar rejects storage classes
11100 in typenames, fields or parameters. */
11101 if (current_lang_name == lang_name_java)
11102 TYPE_FOR_JAVA (type) = 1;
11104 if (decl_context == FIELD)
11106 if (constructor_name_p (declarator, current_class_type))
11107 pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11108 declarator);
11109 decl = build_lang_decl (TYPE_DECL, declarator, type);
11111 else
11113 decl = build_decl (TYPE_DECL, declarator, type);
11114 if (in_namespace || ctype)
11115 cp_error_at ("typedef name may not be a nested-name-specifier",
11116 decl);
11117 if (!current_function_decl)
11118 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11121 /* If the user declares "typedef struct {...} foo" then the
11122 struct will have an anonymous name. Fill that name in now.
11123 Nothing can refer to it, so nothing needs know about the name
11124 change. */
11125 if (type != error_mark_node
11126 && declarator
11127 && TYPE_NAME (type)
11128 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11129 && TYPE_ANONYMOUS_P (type)
11130 /* Don't do this if there are attributes. */
11131 && (!attrlist || !*attrlist)
11132 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11134 tree oldname = TYPE_NAME (type);
11135 tree t;
11137 /* Replace the anonymous name with the real name everywhere. */
11138 lookup_tag_reverse (type, declarator);
11139 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11140 if (TYPE_NAME (t) == oldname)
11141 TYPE_NAME (t) = decl;
11143 if (TYPE_LANG_SPECIFIC (type))
11144 TYPE_WAS_ANONYMOUS (type) = 1;
11146 /* If this is a typedef within a template class, the nested
11147 type is a (non-primary) template. The name for the
11148 template needs updating as well. */
11149 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11150 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11151 = TYPE_IDENTIFIER (type);
11153 /* FIXME remangle member functions; member functions of a
11154 type with external linkage have external linkage. */
11157 if (quals)
11159 if (ctype == NULL_TREE)
11161 if (TREE_CODE (type) != METHOD_TYPE)
11162 cp_error_at ("invalid type qualifier for non-member function type", decl);
11163 else
11164 ctype = TYPE_METHOD_BASETYPE (type);
11166 if (ctype != NULL_TREE)
11167 grok_method_quals (ctype, decl, quals);
11170 if (RIDBIT_SETP (RID_SIGNED, specbits)
11171 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11172 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11174 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11175 inlinep, friendp, raises != NULL_TREE);
11177 return decl;
11180 /* Detect the case of an array type of unspecified size
11181 which came, as such, direct from a typedef name.
11182 We must copy the type, so that the array's domain can be
11183 individually set by the object's initializer. */
11185 if (type && typedef_type
11186 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11187 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11188 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11190 /* Detect where we're using a typedef of function type to declare a
11191 function. last_function_parms will not be set, so we must create
11192 it now. */
11194 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11196 tree decls = NULL_TREE;
11197 tree args;
11199 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11201 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11203 TREE_CHAIN (decl) = decls;
11204 decls = decl;
11207 last_function_parms = nreverse (decls);
11210 /* If this is a type name (such as, in a cast or sizeof),
11211 compute the type and return it now. */
11213 if (decl_context == TYPENAME)
11215 /* Note that the grammar rejects storage classes
11216 in typenames, fields or parameters. */
11217 if (type_quals != TYPE_UNQUALIFIED)
11218 type_quals = TYPE_UNQUALIFIED;
11220 /* Special case: "friend class foo" looks like a TYPENAME context. */
11221 if (friendp)
11223 if (type_quals != TYPE_UNQUALIFIED)
11225 error ("type qualifiers specified for friend class declaration");
11226 type_quals = TYPE_UNQUALIFIED;
11228 if (inlinep)
11230 error ("`inline' specified for friend class declaration");
11231 inlinep = 0;
11234 if (!current_aggr)
11236 /* Don't allow friend declaration without a class-key. */
11237 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11238 pedwarn ("template parameters cannot be friends");
11239 else if (TREE_CODE (type) == TYPENAME_TYPE)
11240 pedwarn ("friend declaration requires class-key, "
11241 "i.e. `friend class %T::%D'",
11242 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11243 else
11244 pedwarn ("friend declaration requires class-key, "
11245 "i.e. `friend %#T'",
11246 type);
11249 /* Only try to do this stuff if we didn't already give up. */
11250 if (type != integer_type_node)
11252 /* A friendly class? */
11253 if (current_class_type)
11254 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11255 else
11256 error ("trying to make class `%T' a friend of global scope",
11257 type);
11259 type = void_type_node;
11262 else if (quals)
11264 if (ctype == NULL_TREE)
11266 if (TREE_CODE (type) != METHOD_TYPE)
11267 error ("invalid qualifiers on non-member function type");
11268 else
11269 ctype = TYPE_METHOD_BASETYPE (type);
11271 if (ctype)
11273 tree dummy = build_decl (TYPE_DECL, declarator, type);
11274 grok_method_quals (ctype, dummy, quals);
11275 type = TREE_TYPE (dummy);
11279 return type;
11281 else if (declarator == NULL_TREE && decl_context != PARM
11282 && decl_context != CATCHPARM
11283 && TREE_CODE (type) != UNION_TYPE
11284 && ! bitfield)
11286 error ("abstract declarator `%T' used as declaration", type);
11287 declarator = make_anon_name ();
11290 /* `void' at top level (not within pointer)
11291 is allowed only in typedefs or type names.
11292 We don't complain about parms either, but that is because
11293 a better error message can be made later. */
11295 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11297 if (! declarator)
11298 error ("unnamed variable or field declared void");
11299 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11301 if (IDENTIFIER_OPNAME_P (declarator))
11302 abort ();
11303 else
11304 error ("variable or field `%s' declared void", name);
11306 else
11307 error ("variable or field declared void");
11308 type = integer_type_node;
11311 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11312 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11314 if (decl_context == PARM || decl_context == CATCHPARM)
11316 if (ctype || in_namespace)
11317 error ("cannot use `::' in parameter declaration");
11319 /* A parameter declared as an array of T is really a pointer to T.
11320 One declared as a function is really a pointer to a function.
11321 One declared as a member is really a pointer to member. */
11323 if (TREE_CODE (type) == ARRAY_TYPE)
11325 /* Transfer const-ness of array into that of type pointed to. */
11326 type = build_pointer_type (TREE_TYPE (type));
11327 type_quals = TYPE_UNQUALIFIED;
11329 else if (TREE_CODE (type) == FUNCTION_TYPE)
11330 type = build_pointer_type (type);
11331 else if (TREE_CODE (type) == OFFSET_TYPE)
11332 type = build_pointer_type (type);
11336 register tree decl;
11338 if (decl_context == PARM)
11340 decl = cp_build_parm_decl (declarator, type);
11342 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11343 inlinep, friendp, raises != NULL_TREE);
11345 else if (decl_context == FIELD)
11347 if (type == error_mark_node)
11349 /* Happens when declaring arrays of sizes which
11350 are error_mark_node, for example. */
11351 decl = NULL_TREE;
11353 else if (in_namespace && !friendp)
11355 /* Something like struct S { int N::j; }; */
11356 error ("invalid use of `::'");
11357 decl = NULL_TREE;
11359 else if (TREE_CODE (type) == FUNCTION_TYPE)
11361 int publicp = 0;
11362 tree function_context;
11364 /* We catch the others as conflicts with the builtin
11365 typedefs. */
11366 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11368 error ("function `%D' cannot be declared friend",
11369 declarator);
11370 friendp = 0;
11373 if (friendp == 0)
11375 if (ctype == NULL_TREE)
11376 ctype = current_class_type;
11378 if (ctype == NULL_TREE)
11380 error ("can't make `%D' into a method -- not in a class",
11381 declarator);
11382 return void_type_node;
11385 /* ``A union may [ ... ] not [ have ] virtual functions.''
11386 ARM 9.5 */
11387 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11389 error ("function `%D' declared virtual inside a union",
11390 declarator);
11391 return void_type_node;
11394 if (declarator == ansi_opname (NEW_EXPR)
11395 || declarator == ansi_opname (VEC_NEW_EXPR)
11396 || declarator == ansi_opname (DELETE_EXPR)
11397 || declarator == ansi_opname (VEC_DELETE_EXPR))
11399 if (virtualp)
11401 error ("`%D' cannot be declared virtual, since it is always static",
11402 declarator);
11403 virtualp = 0;
11406 else if (staticp < 2)
11407 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11408 TYPE_ARG_TYPES (type));
11411 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11412 function_context = (ctype != NULL_TREE) ?
11413 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11414 publicp = (! friendp || ! staticp)
11415 && function_context == NULL_TREE;
11416 decl = grokfndecl (ctype, type,
11417 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11418 ? declarator : dname,
11419 declarator,
11420 virtualp, flags, quals, raises,
11421 friendp ? -1 : 0, friendp, publicp, inlinep,
11422 funcdef_flag, template_count, in_namespace);
11423 if (decl == NULL_TREE)
11424 return decl;
11425 #if 0
11426 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11427 /* The decl and setting of decl_attr is also turned off. */
11428 decl = build_decl_attribute_variant (decl, decl_attr);
11429 #endif
11431 /* [class.conv.ctor]
11433 A constructor declared without the function-specifier
11434 explicit that can be called with a single parameter
11435 specifies a conversion from the type of its first
11436 parameter to the type of its class. Such a constructor
11437 is called a converting constructor. */
11438 if (explicitp == 2)
11439 DECL_NONCONVERTING_P (decl) = 1;
11440 else if (DECL_CONSTRUCTOR_P (decl))
11442 /* The constructor can be called with exactly one
11443 parameter if there is at least one parameter, and
11444 any subsequent parameters have default arguments.
11445 Ignore any compiler-added parms. */
11446 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11448 if (arg_types == void_list_node
11449 || (arg_types
11450 && TREE_CHAIN (arg_types)
11451 && TREE_CHAIN (arg_types) != void_list_node
11452 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11453 DECL_NONCONVERTING_P (decl) = 1;
11456 else if (TREE_CODE (type) == METHOD_TYPE)
11458 /* We only get here for friend declarations of
11459 members of other classes. */
11460 /* All method decls are public, so tell grokfndecl to set
11461 TREE_PUBLIC, also. */
11462 decl = grokfndecl (ctype, type, declarator, declarator,
11463 virtualp, flags, quals, raises,
11464 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11465 template_count, in_namespace);
11466 if (decl == NULL_TREE)
11467 return NULL_TREE;
11469 else if (!staticp && ! processing_template_decl
11470 && !COMPLETE_TYPE_P (complete_type (type))
11471 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11473 if (declarator)
11474 error ("field `%D' has incomplete type", declarator);
11475 else
11476 error ("name `%T' has incomplete type", type);
11478 /* If we're instantiating a template, tell them which
11479 instantiation made the field's type be incomplete. */
11480 if (current_class_type
11481 && TYPE_NAME (current_class_type)
11482 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11483 && declspecs && TREE_VALUE (declspecs)
11484 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11485 error (" in instantiation of template `%T'",
11486 current_class_type);
11488 type = error_mark_node;
11489 decl = NULL_TREE;
11491 else
11493 if (friendp)
11495 error ("`%s' is neither function nor member function; cannot be declared friend",
11496 IDENTIFIER_POINTER (declarator));
11497 friendp = 0;
11499 decl = NULL_TREE;
11502 if (friendp)
11504 /* Friends are treated specially. */
11505 if (ctype == current_class_type)
11506 warning ("member functions are implicitly friends of their class");
11507 else
11509 tree t = NULL_TREE;
11510 if (decl && DECL_NAME (decl))
11512 if (template_class_depth (current_class_type) == 0)
11514 decl
11515 = check_explicit_specialization
11516 (declarator, decl,
11517 template_count, 2 * (funcdef_flag != 0) + 4);
11518 if (decl == error_mark_node)
11519 return error_mark_node;
11522 t = do_friend (ctype, declarator, decl,
11523 last_function_parms, *attrlist,
11524 flags, quals, funcdef_flag);
11526 if (t && funcdef_flag)
11527 return t;
11529 return void_type_node;
11533 /* Structure field. It may not be a function, except for C++ */
11535 if (decl == NULL_TREE)
11537 if (initialized)
11539 if (!staticp)
11541 /* An attempt is being made to initialize a non-static
11542 member. But, from [class.mem]:
11544 4 A member-declarator can contain a
11545 constant-initializer only if it declares a static
11546 member (_class.static_) of integral or enumeration
11547 type, see _class.static.data_.
11549 This used to be relatively common practice, but
11550 the rest of the compiler does not correctly
11551 handle the initialization unless the member is
11552 static so we make it static below. */
11553 pedwarn ("ISO C++ forbids initialization of member `%D'",
11554 declarator);
11555 pedwarn ("making `%D' static", declarator);
11556 staticp = 1;
11559 if (uses_template_parms (type))
11560 /* We'll check at instantiation time. */
11562 else if (check_static_variable_definition (declarator,
11563 type))
11564 /* If we just return the declaration, crashes
11565 will sometimes occur. We therefore return
11566 void_type_node, as if this was a friend
11567 declaration, to cause callers to completely
11568 ignore this declaration. */
11569 return void_type_node;
11572 if (staticp)
11574 /* [class.mem] forbids static data members with the
11575 same name as the enclosing class. Non-static data
11576 members are checked in check_field_decls. */
11577 if (constructor_name_p (declarator, current_class_type))
11578 pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11579 declarator);
11581 /* C++ allows static class members. All other work
11582 for this is done by grokfield. */
11583 decl = build_lang_decl (VAR_DECL, declarator, type);
11584 TREE_STATIC (decl) = 1;
11585 /* In class context, 'static' means public access. */
11586 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11588 else
11590 decl = build_decl (FIELD_DECL, declarator, type);
11591 DECL_NONADDRESSABLE_P (decl) = bitfield;
11592 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11594 DECL_MUTABLE_P (decl) = 1;
11595 RIDBIT_RESET (RID_MUTABLE, specbits);
11599 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11600 inlinep, friendp, raises != NULL_TREE);
11603 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11605 tree original_name;
11606 int publicp = 0;
11608 if (! declarator)
11609 return NULL_TREE;
11611 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11612 original_name = dname;
11613 else
11614 original_name = declarator;
11616 if (RIDBIT_SETP (RID_AUTO, specbits))
11617 error ("storage class `auto' invalid for function `%s'", name);
11618 else if (RIDBIT_SETP (RID_REGISTER, specbits))
11619 error ("storage class `register' invalid for function `%s'", name);
11620 else if (RIDBIT_SETP (RID_THREAD, specbits))
11621 error ("storage class `__thread' invalid for function `%s'", name);
11623 /* Function declaration not at top level.
11624 Storage classes other than `extern' are not allowed
11625 and `extern' makes no difference. */
11626 if (! toplevel_bindings_p ()
11627 && (RIDBIT_SETP (RID_STATIC, specbits)
11628 || RIDBIT_SETP (RID_INLINE, specbits))
11629 && pedantic)
11631 if (RIDBIT_SETP (RID_STATIC, specbits))
11632 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11633 else
11634 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11637 if (ctype == NULL_TREE)
11639 if (virtualp)
11641 error ("virtual non-class function `%s'", name);
11642 virtualp = 0;
11645 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11646 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11647 TYPE_ARG_TYPES (type));
11649 /* Record presence of `static'. */
11650 publicp = (ctype != NULL_TREE
11651 || RIDBIT_SETP (RID_EXTERN, specbits)
11652 || !RIDBIT_SETP (RID_STATIC, specbits));
11654 decl = grokfndecl (ctype, type, original_name, declarator,
11655 virtualp, flags, quals, raises,
11656 1, friendp,
11657 publicp, inlinep, funcdef_flag,
11658 template_count, in_namespace);
11659 if (decl == NULL_TREE)
11660 return NULL_TREE;
11662 if (staticp == 1)
11664 int invalid_static = 0;
11666 /* Don't allow a static member function in a class, and forbid
11667 declaring main to be static. */
11668 if (TREE_CODE (type) == METHOD_TYPE)
11670 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11671 invalid_static = 1;
11673 else if (current_function_decl)
11675 /* FIXME need arm citation */
11676 error ("cannot declare static function inside another function");
11677 invalid_static = 1;
11680 if (invalid_static)
11682 staticp = 0;
11683 RIDBIT_RESET (RID_STATIC, specbits);
11687 else
11689 /* It's a variable. */
11691 /* An uninitialized decl with `extern' is a reference. */
11692 decl = grokvardecl (type, declarator, &specbits,
11693 initialized,
11694 (type_quals & TYPE_QUAL_CONST) != 0,
11695 ctype ? ctype : in_namespace);
11696 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11697 inlinep, friendp, raises != NULL_TREE);
11699 if (ctype)
11701 DECL_CONTEXT (decl) = ctype;
11702 if (staticp == 1)
11704 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11705 staticp = 0;
11706 RIDBIT_RESET (RID_STATIC, specbits);
11708 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11710 error ("static member `%D' declared `register'", decl);
11711 RIDBIT_RESET (RID_REGISTER, specbits);
11713 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11715 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11716 decl);
11717 RIDBIT_RESET (RID_EXTERN, specbits);
11722 my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11724 /* Record `register' declaration for warnings on &
11725 and in case doing stupid register allocation. */
11727 if (RIDBIT_SETP (RID_REGISTER, specbits))
11728 DECL_REGISTER (decl) = 1;
11730 if (RIDBIT_SETP (RID_EXTERN, specbits))
11731 DECL_THIS_EXTERN (decl) = 1;
11733 if (RIDBIT_SETP (RID_STATIC, specbits))
11734 DECL_THIS_STATIC (decl) = 1;
11736 /* Record constancy and volatility. There's no need to do this
11737 when processing a template; we'll do this for the instantiated
11738 declaration based on the type of DECL. */
11739 if (!processing_template_decl)
11740 c_apply_type_quals_to_decl (type_quals, decl);
11742 return decl;
11746 /* Subroutine of start_function. Ensure that each of the parameter
11747 types (as listed in PARMS) is complete, as is required for a
11748 function definition. */
11750 static void
11751 require_complete_types_for_parms (tree parms)
11753 for (; parms; parms = TREE_CHAIN (parms))
11755 if (VOID_TYPE_P (TREE_TYPE (parms)))
11756 /* grokparms will have already issued an error */
11757 TREE_TYPE (parms) = error_mark_node;
11758 else if (complete_type_or_else (TREE_TYPE (parms), parms))
11760 layout_decl (parms, 0);
11761 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11763 else
11764 TREE_TYPE (parms) = error_mark_node;
11768 /* Returns nonzero if T is a local variable. */
11771 local_variable_p (tree t)
11773 if ((TREE_CODE (t) == VAR_DECL
11774 /* A VAR_DECL with a context that is a _TYPE is a static data
11775 member. */
11776 && !TYPE_P (CP_DECL_CONTEXT (t))
11777 /* Any other non-local variable must be at namespace scope. */
11778 && !DECL_NAMESPACE_SCOPE_P (t))
11779 || (TREE_CODE (t) == PARM_DECL))
11780 return 1;
11782 return 0;
11785 /* Returns nonzero if T is an automatic local variable or a label.
11786 (These are the declarations that need to be remapped when the code
11787 containing them is duplicated.) */
11790 nonstatic_local_decl_p (tree t)
11792 return ((local_variable_p (t) && !TREE_STATIC (t))
11793 || TREE_CODE (t) == LABEL_DECL
11794 || TREE_CODE (t) == RESULT_DECL);
11797 /* Like local_variable_p, but suitable for use as a tree-walking
11798 function. */
11800 static tree
11801 local_variable_p_walkfn (tree* tp,
11802 int* walk_subtrees ATTRIBUTE_UNUSED ,
11803 void* data ATTRIBUTE_UNUSED )
11805 return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11806 ? *tp : NULL_TREE);
11809 /* Check that ARG, which is a default-argument expression for a
11810 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11811 something goes wrong. DECL may also be a _TYPE node, rather than a
11812 DECL, if there is no DECL available. */
11814 tree
11815 check_default_argument (tree decl, tree arg)
11817 tree var;
11818 tree decl_type;
11820 if (TREE_CODE (arg) == DEFAULT_ARG)
11821 /* We get a DEFAULT_ARG when looking at an in-class declaration
11822 with a default argument. Ignore the argument for now; we'll
11823 deal with it after the class is complete. */
11824 return arg;
11826 if (processing_template_decl || uses_template_parms (arg))
11827 /* We don't do anything checking until instantiation-time. Note
11828 that there may be uninstantiated arguments even for an
11829 instantiated function, since default arguments are not
11830 instantiated until they are needed. */
11831 return arg;
11833 if (TYPE_P (decl))
11835 decl_type = decl;
11836 decl = NULL_TREE;
11838 else
11839 decl_type = TREE_TYPE (decl);
11841 if (arg == error_mark_node
11842 || decl == error_mark_node
11843 || TREE_TYPE (arg) == error_mark_node
11844 || decl_type == error_mark_node)
11845 /* Something already went wrong. There's no need to check
11846 further. */
11847 return error_mark_node;
11849 /* [dcl.fct.default]
11851 A default argument expression is implicitly converted to the
11852 parameter type. */
11853 if (!TREE_TYPE (arg)
11854 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11856 if (decl)
11857 error ("default argument for `%#D' has type `%T'",
11858 decl, TREE_TYPE (arg));
11859 else
11860 error ("default argument for parameter of type `%T' has type `%T'",
11861 decl_type, TREE_TYPE (arg));
11863 return error_mark_node;
11866 /* [dcl.fct.default]
11868 Local variables shall not be used in default argument
11869 expressions.
11871 The keyword `this' shall not be used in a default argument of a
11872 member function. */
11873 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11874 NULL);
11875 if (var)
11877 error ("default argument `%E' uses local variable `%D'",
11878 arg, var);
11879 return error_mark_node;
11882 /* All is well. */
11883 return arg;
11886 /* Decode the list of parameter types for a function type.
11887 Given the list of things declared inside the parens,
11888 return a list of types.
11890 We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11891 flag. If unset, we append void_list_node. A parmlist declared
11892 as `(void)' is accepted as the empty parmlist.
11894 Also set last_function_parms to the chain of PARM_DECLs. */
11896 static tree
11897 grokparms (tree first_parm)
11899 tree result = NULL_TREE;
11900 tree decls = NULL_TREE;
11901 int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11902 tree parm, chain;
11903 int any_error = 0;
11905 my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11907 for (parm = first_parm; parm != NULL_TREE; parm = chain)
11909 tree type = NULL_TREE;
11910 tree decl = TREE_VALUE (parm);
11911 tree init = TREE_PURPOSE (parm);
11912 tree specs, attrs;
11914 chain = TREE_CHAIN (parm);
11915 /* @@ weak defense against parse errors. */
11916 if (TREE_CODE (decl) != VOID_TYPE
11917 && TREE_CODE (decl) != TREE_LIST)
11919 /* Give various messages as the need arises. */
11920 if (TREE_CODE (decl) == STRING_CST)
11921 error ("invalid string constant `%E'", decl);
11922 else if (TREE_CODE (decl) == INTEGER_CST)
11923 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11924 continue;
11927 if (parm == void_list_node)
11928 break;
11930 split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
11931 decl = grokdeclarator (TREE_VALUE (decl), specs,
11932 PARM, init != NULL_TREE, &attrs);
11933 if (! decl || TREE_TYPE (decl) == error_mark_node)
11934 continue;
11936 if (attrs)
11937 cplus_decl_attributes (&decl, attrs, 0);
11939 type = TREE_TYPE (decl);
11940 if (VOID_TYPE_P (type))
11942 if (same_type_p (type, void_type_node)
11943 && !DECL_NAME (decl) && !result && !chain && !ellipsis)
11944 /* this is a parmlist of `(void)', which is ok. */
11945 break;
11946 cxx_incomplete_type_error (decl, type);
11947 /* It's not a good idea to actually create parameters of
11948 type `void'; other parts of the compiler assume that a
11949 void type terminates the parameter list. */
11950 type = error_mark_node;
11951 TREE_TYPE (decl) = error_mark_node;
11954 if (type != error_mark_node)
11956 /* Top-level qualifiers on the parameters are
11957 ignored for function types. */
11958 type = TYPE_MAIN_VARIANT (type);
11959 if (TREE_CODE (type) == METHOD_TYPE)
11961 error ("parameter `%D' invalidly declared method type", decl);
11962 type = build_pointer_type (type);
11963 TREE_TYPE (decl) = type;
11965 else if (TREE_CODE (type) == OFFSET_TYPE)
11967 error ("parameter `%D' invalidly declared offset type", decl);
11968 type = build_pointer_type (type);
11969 TREE_TYPE (decl) = type;
11971 else if (abstract_virtuals_error (decl, type))
11972 any_error = 1; /* Seems like a good idea. */
11973 else if (POINTER_TYPE_P (type))
11975 /* [dcl.fct]/6, parameter types cannot contain pointers
11976 (references) to arrays of unknown bound. */
11977 tree t = TREE_TYPE (type);
11978 int ptr = TYPE_PTR_P (type);
11980 while (1)
11982 if (TYPE_PTR_P (t))
11983 ptr = 1;
11984 else if (TREE_CODE (t) != ARRAY_TYPE)
11985 break;
11986 else if (!TYPE_DOMAIN (t))
11987 break;
11988 t = TREE_TYPE (t);
11990 if (TREE_CODE (t) == ARRAY_TYPE)
11991 error ("parameter `%D' includes %s to array of unknown bound `%T'",
11992 decl, ptr ? "pointer" : "reference", t);
11995 if (!any_error && init)
11996 init = check_default_argument (decl, init);
11997 else
11998 init = NULL_TREE;
12001 TREE_CHAIN (decl) = decls;
12002 decls = decl;
12003 result = tree_cons (init, type, result);
12005 decls = nreverse (decls);
12006 result = nreverse (result);
12007 if (!ellipsis)
12008 result = chainon (result, void_list_node);
12009 last_function_parms = decls;
12011 return result;
12015 /* D is a constructor or overloaded `operator='.
12017 Let T be the class in which D is declared. Then, this function
12018 returns:
12020 -1 if D's is an ill-formed constructor or copy assignment operator
12021 whose first parameter is of type `T'.
12022 0 if D is not a copy constructor or copy assignment
12023 operator.
12024 1 if D is a copy constructor or copy assignment operator whose
12025 first parameter is a reference to const qualified T.
12026 2 if D is a copy constructor or copy assignment operator whose
12027 first parameter is a reference to non-const qualified T.
12029 This function can be used as a predicate. Positive values indicate
12030 a copy constructor and nonzero values indicate a copy assignment
12031 operator. */
12034 copy_fn_p (tree d)
12036 tree args;
12037 tree arg_type;
12038 int result = 1;
12040 my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12042 if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12043 /* Instantiations of template member functions are never copy
12044 functions. Note that member functions of templated classes are
12045 represented as template functions internally, and we must
12046 accept those as copy functions. */
12047 return 0;
12049 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12050 if (!args)
12051 return 0;
12053 arg_type = TREE_VALUE (args);
12055 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12057 /* Pass by value copy assignment operator. */
12058 result = -1;
12060 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12061 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12063 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12064 result = 2;
12066 else
12067 return 0;
12069 args = TREE_CHAIN (args);
12071 if (args && args != void_list_node && !TREE_PURPOSE (args))
12072 /* There are more non-optional args. */
12073 return 0;
12075 return result;
12078 /* Remember any special properties of member function DECL. */
12080 void grok_special_member_properties (tree decl)
12082 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12083 ; /* Not special. */
12084 else if (DECL_CONSTRUCTOR_P (decl))
12086 int ctor = copy_fn_p (decl);
12088 if (ctor > 0)
12090 /* [class.copy]
12092 A non-template constructor for class X is a copy
12093 constructor if its first parameter is of type X&, const
12094 X&, volatile X& or const volatile X&, and either there
12095 are no other parameters or else all other parameters have
12096 default arguments. */
12097 TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12098 if (ctor > 1)
12099 TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12101 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12102 TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12104 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12106 /* [class.copy]
12108 A non-template assignment operator for class X is a copy
12109 assignment operator if its parameter is of type X, X&, const
12110 X&, volatile X& or const volatile X&. */
12112 int assop = copy_fn_p (decl);
12114 if (assop)
12116 TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12117 if (assop != 1)
12118 TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12119 if (DECL_PURE_VIRTUAL_P (decl))
12120 TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12125 /* Check a constructor DECL has the correct form. Complains
12126 if the class has a constructor of the form X(X). */
12129 grok_ctor_properties (tree ctype, tree decl)
12131 int ctor_parm = copy_fn_p (decl);
12133 if (ctor_parm < 0)
12135 /* [class.copy]
12137 A declaration of a constructor for a class X is ill-formed if
12138 its first parameter is of type (optionally cv-qualified) X
12139 and either there are no other parameters or else all other
12140 parameters have default arguments.
12142 We *don't* complain about member template instantiations that
12143 have this form, though; they can occur as we try to decide
12144 what constructor to use during overload resolution. Since
12145 overload resolution will never prefer such a constructor to
12146 the non-template copy constructor (which is either explicitly
12147 or implicitly defined), there's no need to worry about their
12148 existence. Theoretically, they should never even be
12149 instantiated, but that's hard to forestall. */
12150 error ("invalid constructor; you probably meant `%T (const %T&)'",
12151 ctype, ctype);
12152 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12153 return 0;
12156 return 1;
12159 /* An operator with this code is unary, but can also be binary. */
12161 static int
12162 ambi_op_p (enum tree_code code)
12164 return (code == INDIRECT_REF
12165 || code == ADDR_EXPR
12166 || code == CONVERT_EXPR
12167 || code == NEGATE_EXPR
12168 || code == PREINCREMENT_EXPR
12169 || code == PREDECREMENT_EXPR);
12172 /* An operator with this name can only be unary. */
12174 static int
12175 unary_op_p (enum tree_code code)
12177 return (code == TRUTH_NOT_EXPR
12178 || code == BIT_NOT_EXPR
12179 || code == COMPONENT_REF
12180 || code == TYPE_EXPR);
12183 /* Do a little sanity-checking on how they declared their operator. */
12185 void
12186 grok_op_properties (tree decl, int friendp)
12188 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12189 tree argtype;
12190 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12191 tree name = DECL_NAME (decl);
12192 enum tree_code operator_code;
12193 int arity;
12195 /* Count the number of arguments. */
12196 for (argtype = argtypes, arity = 0;
12197 argtype && argtype != void_list_node;
12198 argtype = TREE_CHAIN (argtype))
12199 ++arity;
12201 if (current_class_type == NULL_TREE)
12202 friendp = 1;
12204 if (DECL_CONV_FN_P (decl))
12205 operator_code = TYPE_EXPR;
12206 else
12209 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12210 if (ansi_opname (CODE) == name) \
12212 operator_code = (CODE); \
12213 break; \
12215 else if (ansi_assopname (CODE) == name) \
12217 operator_code = (CODE); \
12218 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12219 break; \
12222 #include "operators.def"
12223 #undef DEF_OPERATOR
12225 abort ();
12227 while (0);
12228 my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12229 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12231 if (! friendp)
12233 switch (operator_code)
12235 case CALL_EXPR:
12236 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12237 break;
12239 case ARRAY_REF:
12240 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12241 break;
12243 case COMPONENT_REF:
12244 case MEMBER_REF:
12245 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12246 break;
12248 case NEW_EXPR:
12249 TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12250 break;
12252 case DELETE_EXPR:
12253 TYPE_GETS_DELETE (current_class_type) |= 1;
12254 break;
12256 case VEC_NEW_EXPR:
12257 TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12258 break;
12260 case VEC_DELETE_EXPR:
12261 TYPE_GETS_DELETE (current_class_type) |= 2;
12262 break;
12264 default:
12265 break;
12269 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12271 /* When the compiler encounters the definition of A::operator new, it
12272 doesn't look at the class declaration to find out if it's static. */
12273 if (methodp)
12274 revert_static_member_fn (decl);
12276 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12278 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12280 if (methodp)
12281 revert_static_member_fn (decl);
12283 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12285 else
12287 /* An operator function must either be a non-static member function
12288 or have at least one parameter of a class, a reference to a class,
12289 an enumeration, or a reference to an enumeration. 13.4.0.6 */
12290 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12292 if (operator_code == TYPE_EXPR
12293 || operator_code == CALL_EXPR
12294 || operator_code == COMPONENT_REF
12295 || operator_code == ARRAY_REF
12296 || operator_code == NOP_EXPR)
12297 error ("`%D' must be a nonstatic member function", decl);
12298 else
12300 tree p = argtypes;
12302 if (DECL_STATIC_FUNCTION_P (decl))
12303 error ("`%D' must be either a non-static member function or a non-member function", decl);
12305 if (p)
12306 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
12308 tree arg = TREE_VALUE (p);
12309 if (TREE_CODE (arg) == REFERENCE_TYPE)
12310 arg = TREE_TYPE (arg);
12312 /* This lets bad template code slip through. */
12313 if (IS_AGGR_TYPE (arg)
12314 || TREE_CODE (arg) == ENUMERAL_TYPE
12315 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12316 || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
12317 goto foundaggr;
12319 error
12320 ("`%D' must have an argument of class or enumerated type",
12321 decl);
12322 foundaggr:
12327 if (operator_code == CALL_EXPR)
12328 return; /* No restrictions on args. */
12330 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12332 tree t = TREE_TYPE (name);
12333 if (! friendp)
12335 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12336 const char *what = 0;
12338 if (ref)
12339 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12341 if (TREE_CODE (t) == VOID_TYPE)
12342 what = "void";
12343 else if (t == current_class_type)
12344 what = "the same type";
12345 /* Don't force t to be complete here. */
12346 else if (IS_AGGR_TYPE (t)
12347 && COMPLETE_TYPE_P (t)
12348 && DERIVED_FROM_P (t, current_class_type))
12349 what = "a base class";
12351 if (what)
12352 warning ("conversion to %s%s will never use a type conversion operator",
12353 ref ? "a reference to " : "", what);
12356 if (operator_code == COND_EXPR)
12358 /* 13.4.0.3 */
12359 error ("ISO C++ prohibits overloading operator ?:");
12361 else if (ambi_op_p (operator_code))
12363 if (arity == 1)
12364 /* We pick the one-argument operator codes by default, so
12365 we don't have to change anything. */
12367 else if (arity == 2)
12369 /* If we thought this was a unary operator, we now know
12370 it to be a binary operator. */
12371 switch (operator_code)
12373 case INDIRECT_REF:
12374 operator_code = MULT_EXPR;
12375 break;
12377 case ADDR_EXPR:
12378 operator_code = BIT_AND_EXPR;
12379 break;
12381 case CONVERT_EXPR:
12382 operator_code = PLUS_EXPR;
12383 break;
12385 case NEGATE_EXPR:
12386 operator_code = MINUS_EXPR;
12387 break;
12389 case PREINCREMENT_EXPR:
12390 operator_code = POSTINCREMENT_EXPR;
12391 break;
12393 case PREDECREMENT_EXPR:
12394 operator_code = POSTDECREMENT_EXPR;
12395 break;
12397 default:
12398 abort ();
12401 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12403 if ((operator_code == POSTINCREMENT_EXPR
12404 || operator_code == POSTDECREMENT_EXPR)
12405 && ! processing_template_decl
12406 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12408 if (methodp)
12409 error ("postfix `%D' must take `int' as its argument",
12410 decl);
12411 else
12412 error
12413 ("postfix `%D' must take `int' as its second argument",
12414 decl);
12417 else
12419 if (methodp)
12420 error ("`%D' must take either zero or one argument", decl);
12421 else
12422 error ("`%D' must take either one or two arguments", decl);
12425 /* More Effective C++ rule 6. */
12426 if (warn_ecpp
12427 && (operator_code == POSTINCREMENT_EXPR
12428 || operator_code == POSTDECREMENT_EXPR
12429 || operator_code == PREINCREMENT_EXPR
12430 || operator_code == PREDECREMENT_EXPR))
12432 tree arg = TREE_VALUE (argtypes);
12433 tree ret = TREE_TYPE (TREE_TYPE (decl));
12434 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12435 arg = TREE_TYPE (arg);
12436 arg = TYPE_MAIN_VARIANT (arg);
12437 if (operator_code == PREINCREMENT_EXPR
12438 || operator_code == PREDECREMENT_EXPR)
12440 if (TREE_CODE (ret) != REFERENCE_TYPE
12441 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12442 arg))
12443 warning ("prefix `%D' should return `%T'", decl,
12444 build_reference_type (arg));
12446 else
12448 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12449 warning ("postfix `%D' should return `%T'", decl, arg);
12453 else if (unary_op_p (operator_code))
12455 if (arity != 1)
12457 if (methodp)
12458 error ("`%D' must take `void'", decl);
12459 else
12460 error ("`%D' must take exactly one argument", decl);
12463 else /* if (binary_op_p (operator_code)) */
12465 if (arity != 2)
12467 if (methodp)
12468 error ("`%D' must take exactly one argument", decl);
12469 else
12470 error ("`%D' must take exactly two arguments", decl);
12473 /* More Effective C++ rule 7. */
12474 if (warn_ecpp
12475 && (operator_code == TRUTH_ANDIF_EXPR
12476 || operator_code == TRUTH_ORIF_EXPR
12477 || operator_code == COMPOUND_EXPR))
12478 warning ("user-defined `%D' always evaluates both arguments",
12479 decl);
12482 /* Effective C++ rule 23. */
12483 if (warn_ecpp
12484 && arity == 2
12485 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12486 && (operator_code == PLUS_EXPR
12487 || operator_code == MINUS_EXPR
12488 || operator_code == TRUNC_DIV_EXPR
12489 || operator_code == MULT_EXPR
12490 || operator_code == TRUNC_MOD_EXPR)
12491 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12492 warning ("`%D' should return by value", decl);
12494 /* [over.oper]/8 */
12495 for (; argtypes && argtypes != void_list_node;
12496 argtypes = TREE_CHAIN (argtypes))
12497 if (TREE_PURPOSE (argtypes))
12499 TREE_PURPOSE (argtypes) = NULL_TREE;
12500 if (operator_code == POSTINCREMENT_EXPR
12501 || operator_code == POSTDECREMENT_EXPR)
12503 if (pedantic)
12504 pedwarn ("`%D' cannot have default arguments", decl);
12506 else
12507 error ("`%D' cannot have default arguments", decl);
12513 static const char *
12514 tag_name (enum tag_types code)
12516 switch (code)
12518 case record_type:
12519 return "struct";
12520 case class_type:
12521 return "class";
12522 case union_type:
12523 return "union ";
12524 case enum_type:
12525 return "enum";
12526 default:
12527 abort ();
12531 /* Name lookup in an elaborated-type-specifier (after the keyword
12532 indicated by TAG_CODE) has found TYPE. If the
12533 elaborated-type-specifier is invalid, issue a diagnostic and return
12534 error_mark_node; otherwise, return TYPE itself. */
12536 static tree
12537 check_elaborated_type_specifier (enum tag_types tag_code,
12538 tree type)
12540 tree t;
12542 t = follow_tag_typedef (type);
12544 /* [dcl.type.elab] If the identifier resolves to a typedef-name or a
12545 template type-parameter, the elaborated-type-specifier is
12546 ill-formed. */
12547 if (!t)
12549 error ("using typedef-name `%D' after `%s'",
12550 TYPE_NAME (type), tag_name (tag_code));
12551 t = error_mark_node;
12553 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12555 error ("using template type parameter `%T' after `%s'",
12556 type, tag_name (tag_code));
12557 t = error_mark_node;
12560 return t;
12563 /* Get the struct, enum or union (CODE says which) with tag NAME.
12564 Define the tag as a forward-reference if it is not defined.
12566 C++: If a class derivation is given, process it here, and report
12567 an error if multiple derivation declarations are not identical.
12569 If this is a definition, come in through xref_tag and only look in
12570 the current frame for the name (since C++ allows new names in any
12571 scope.) */
12573 tree
12574 xref_tag (enum tag_types tag_code, tree name, tree attributes,
12575 bool globalize)
12577 enum tree_code code;
12578 register tree ref, t;
12579 struct cp_binding_level *b = current_binding_level;
12580 tree context = NULL_TREE;
12582 timevar_push (TV_NAME_LOOKUP);
12583 switch (tag_code)
12585 case record_type:
12586 case class_type:
12587 code = RECORD_TYPE;
12588 break;
12589 case union_type:
12590 code = UNION_TYPE;
12591 break;
12592 case enum_type:
12593 code = ENUMERAL_TYPE;
12594 break;
12595 default:
12596 abort ();
12599 /* If a cross reference is requested, look up the type
12600 already defined for this tag and return it. */
12601 if (TYPE_P (name))
12603 t = name;
12604 name = TYPE_IDENTIFIER (t);
12606 else
12607 t = IDENTIFIER_TYPE_VALUE (name);
12609 /* Warn about 'friend struct Inherited;' doing the wrong thing. */
12610 if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
12612 static int explained;
12613 tree shadowed;
12615 warning ("`%s %T' declares a new type at namespace scope",
12616 tag_name (tag_code), name);
12617 if (!explained++)
12618 warning (" names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
12619 tag_name (tag_code),
12620 constructor_name (current_class_type),
12621 TYPE_IDENTIFIER (t));
12623 /* We need to remove the class scope binding for the
12624 TYPENAME_TYPE as otherwise poplevel_class gets confused. */
12625 for (shadowed = b->class_shadowed;
12626 shadowed;
12627 shadowed = TREE_CHAIN (shadowed))
12628 if (TREE_TYPE (shadowed) == TYPE_NAME (t))
12630 TREE_PURPOSE (shadowed) = NULL_TREE;
12631 break;
12635 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12636 && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
12637 t = NULL_TREE;
12639 if (! globalize)
12641 /* If we know we are defining this tag, only look it up in
12642 this scope and don't try to find it as a type. */
12643 ref = lookup_tag (code, name, b, 1);
12645 else
12647 if (t)
12649 ref = check_elaborated_type_specifier (tag_code, t);
12650 if (ref == error_mark_node)
12651 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12653 else
12654 ref = lookup_tag (code, name, b, 0);
12656 if (! ref)
12658 /* Try finding it as a type declaration. If that wins,
12659 use it. */
12660 ref = lookup_name (name, 1);
12662 if (ref != NULL_TREE
12663 && processing_template_decl
12664 && DECL_CLASS_TEMPLATE_P (ref)
12665 && template_class_depth (current_class_type) == 0)
12666 /* Since GLOBALIZE is true, we're declaring a global
12667 template, so we want this type. */
12668 ref = DECL_TEMPLATE_RESULT (ref);
12670 if (ref && TREE_CODE (ref) == TYPE_DECL)
12672 ref = check_elaborated_type_specifier (tag_code,
12673 TREE_TYPE (ref));
12674 if (ref == error_mark_node)
12675 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12676 if (ref && TREE_CODE (ref) != code)
12677 ref = NULL_TREE;
12679 else
12680 ref = NULL_TREE;
12683 if (ref && current_class_type
12684 && template_class_depth (current_class_type)
12685 && PROCESSING_REAL_TEMPLATE_DECL_P ())
12687 /* Since GLOBALIZE is nonzero, we are not looking at a
12688 definition of this tag. Since, in addition, we are currently
12689 processing a (member) template declaration of a template
12690 class, we must be very careful; consider:
12692 template <class X>
12693 struct S1
12695 template <class U>
12696 struct S2
12697 { template <class V>
12698 friend struct S1; };
12700 Here, the S2::S1 declaration should not be confused with the
12701 outer declaration. In particular, the inner version should
12702 have a template parameter of level 2, not level 1. This
12703 would be particularly important if the member declaration
12704 were instead:
12706 template <class V = U> friend struct S1;
12708 say, when we should tsubst into `U' when instantiating
12709 S2. On the other hand, when presented with:
12711 template <class T>
12712 struct S1 {
12713 template <class U>
12714 struct S2 {};
12715 template <class U>
12716 friend struct S2;
12719 we must find the inner binding eventually. We
12720 accomplish this by making sure that the new type we
12721 create to represent this declaration has the right
12722 TYPE_CONTEXT. */
12723 context = TYPE_CONTEXT (ref);
12724 ref = NULL_TREE;
12728 if (! ref)
12730 /* If no such tag is yet defined, create a forward-reference node
12731 and record it as the "definition".
12732 When a real declaration of this type is found,
12733 the forward-reference will be altered into a real type. */
12734 if (code == ENUMERAL_TYPE)
12736 error ("use of enum `%#D' without previous declaration", name);
12738 ref = make_node (ENUMERAL_TYPE);
12740 /* Give the type a default layout like unsigned int
12741 to avoid crashing if it does not get defined. */
12742 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12743 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12744 TYPE_USER_ALIGN (ref) = 0;
12745 TREE_UNSIGNED (ref) = 1;
12746 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12747 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12748 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12750 /* Enable us to recognize when a type is created in class context.
12751 To do nested classes correctly, this should probably be cleared
12752 out when we leave this classes scope. Currently this in only
12753 done in `start_enum'. */
12755 pushtag (name, ref, globalize);
12757 else
12759 struct cp_binding_level *old_b = class_binding_level;
12761 ref = make_aggr_type (code);
12762 TYPE_CONTEXT (ref) = context;
12764 #ifdef NONNESTED_CLASSES
12765 /* Class types don't nest the way enums do. */
12766 class_binding_level = (struct cp_binding_level *)0;
12767 #endif
12768 pushtag (name, ref, globalize);
12769 class_binding_level = old_b;
12772 else
12774 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12775 redeclare_class_template (ref, current_template_parms);
12778 TYPE_ATTRIBUTES (ref) = attributes;
12780 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ref);
12783 tree
12784 xref_tag_from_type (tree old, tree id, int globalize)
12786 enum tag_types tag_kind;
12788 if (TREE_CODE (old) == RECORD_TYPE)
12789 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12790 else
12791 tag_kind = union_type;
12793 if (id == NULL_TREE)
12794 id = TYPE_IDENTIFIER (old);
12796 return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
12799 /* REF is a type (named NAME), for which we have just seen some
12800 baseclasses. BASE_LIST is a list of those baseclasses; the
12801 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12802 the base-class. TREE_VIA_VIRTUAL indicates virtual
12803 inheritance. CODE_TYPE_NODE indicates whether REF is a class,
12804 struct, or union. */
12806 void
12807 xref_basetypes (tree ref, tree base_list)
12809 /* In the declaration `A : X, Y, ... Z' we mark all the types
12810 (A, X, Y, ..., Z) so we can check for duplicates. */
12811 tree *basep;
12813 int i;
12814 enum tag_types tag_code;
12816 if (TREE_CODE (ref) == UNION_TYPE)
12818 error ("derived union `%T' invalid", ref);
12819 return;
12822 tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12824 /* First, make sure that any templates in base-classes are
12825 instantiated. This ensures that if we call ourselves recursively
12826 we do not get confused about which classes are marked and which
12827 are not. */
12828 basep = &base_list;
12829 while (*basep)
12831 tree basetype = TREE_VALUE (*basep);
12832 if (!(processing_template_decl && uses_template_parms (basetype))
12833 && !complete_type_or_else (basetype, NULL))
12834 /* An incomplete type. Remove it from the list. */
12835 *basep = TREE_CHAIN (*basep);
12836 else
12837 basep = &TREE_CHAIN (*basep);
12840 SET_CLASSTYPE_MARKED (ref);
12841 i = list_length (base_list);
12842 if (i)
12844 tree binfo = TYPE_BINFO (ref);
12845 tree binfos = make_tree_vec (i);
12846 tree accesses = make_tree_vec (i);
12848 BINFO_BASETYPES (binfo) = binfos;
12849 BINFO_BASEACCESSES (binfo) = accesses;
12851 for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
12853 tree access = TREE_PURPOSE (base_list);
12854 int via_virtual = TREE_VIA_VIRTUAL (base_list);
12855 tree basetype = TREE_VALUE (base_list);
12856 tree base_binfo;
12858 if (access == access_default_node)
12859 /* The base of a derived struct is public by default. */
12860 access = (tag_code == class_type
12861 ? access_private_node : access_public_node);
12863 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12864 basetype = TREE_TYPE (basetype);
12865 if (!basetype
12866 || (TREE_CODE (basetype) != RECORD_TYPE
12867 && TREE_CODE (basetype) != TYPENAME_TYPE
12868 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12869 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12871 error ("base type `%T' fails to be a struct or class type",
12872 basetype);
12873 continue;
12876 if (CLASSTYPE_MARKED (basetype))
12878 if (basetype == ref)
12879 error ("recursive type `%T' undefined", basetype);
12880 else
12881 error ("duplicate base type `%T' invalid", basetype);
12882 continue;
12885 if (TYPE_FOR_JAVA (basetype)
12886 && (current_lang_depth () == 0))
12887 TYPE_FOR_JAVA (ref) = 1;
12889 if (CLASS_TYPE_P (basetype))
12891 base_binfo = TYPE_BINFO (basetype);
12892 /* This flag will be in the binfo of the base type, we must
12893 clear it after copying the base binfos. */
12894 BINFO_DEPENDENT_BASE_P (base_binfo)
12895 = dependent_type_p (basetype);
12897 else
12898 base_binfo = make_binfo (size_zero_node, basetype,
12899 NULL_TREE, NULL_TREE);
12901 TREE_VEC_ELT (binfos, i) = base_binfo;
12902 TREE_VEC_ELT (accesses, i) = access;
12903 /* This flag will be in the binfo of the base type, we must
12904 clear it after copying the base binfos. */
12905 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12907 SET_CLASSTYPE_MARKED (basetype);
12909 /* We are free to modify these bits because they are meaningless
12910 at top level, and BASETYPE is a top-level type. */
12911 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12913 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12914 /* Converting to a virtual base class requires looking
12915 up the offset of the virtual base. */
12916 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12919 if (CLASS_TYPE_P (basetype))
12921 TYPE_HAS_NEW_OPERATOR (ref)
12922 |= TYPE_HAS_NEW_OPERATOR (basetype);
12923 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12924 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12925 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12926 /* If the base-class uses multiple inheritance, so do we. */
12927 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12928 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12929 /* Likewise, if converting to a base of the base may require
12930 code, then we may need to generate code to convert to a
12931 base as well. */
12932 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12933 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12935 i++;
12937 if (i)
12938 TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
12939 else
12940 BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
12942 if (i > 1)
12944 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12945 /* If there is more than one non-empty they cannot be at the same
12946 address. */
12947 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12951 /* Copy the base binfos, collect the virtual bases and set the
12952 inheritance order chain. */
12953 copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
12954 CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
12956 /* Unmark all the types. */
12957 while (i--)
12959 tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
12961 CLEAR_CLASSTYPE_MARKED (basetype);
12962 if (CLASS_TYPE_P (basetype))
12964 TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
12965 BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
12968 CLEAR_CLASSTYPE_MARKED (ref);
12972 /* Begin compiling the definition of an enumeration type.
12973 NAME is its name (or null if anonymous).
12974 Returns the type object, as yet incomplete.
12975 Also records info about it so that build_enumerator
12976 may be used to declare the individual values as they are read. */
12978 tree
12979 start_enum (tree name)
12981 register tree enumtype = NULL_TREE;
12982 struct cp_binding_level *b = current_binding_level;
12984 /* If this is the real definition for a previous forward reference,
12985 fill in the contents in the same object that used to be the
12986 forward reference. */
12988 if (name != NULL_TREE)
12989 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12991 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
12993 error ("multiple definition of `%#T'", enumtype);
12994 cp_error_at ("previous definition here", enumtype);
12995 /* Clear out TYPE_VALUES, and start again. */
12996 TYPE_VALUES (enumtype) = NULL_TREE;
12998 else
13000 enumtype = make_node (ENUMERAL_TYPE);
13001 pushtag (name, enumtype, 0);
13004 return enumtype;
13007 /* After processing and defining all the values of an enumeration type,
13008 install their decls in the enumeration type and finish it off.
13009 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
13011 void
13012 finish_enum (tree enumtype)
13014 tree pair;
13015 tree minnode;
13016 tree maxnode;
13017 tree t;
13018 bool unsignedp;
13019 int lowprec;
13020 int highprec;
13021 int precision;
13023 /* We built up the VALUES in reverse order. */
13024 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13026 /* For an enum defined in a template, just set the type of the values;
13027 all further processing is postponed until the template is
13028 instantiated. We need to set the type so that tsubst of a CONST_DECL
13029 works. */
13030 if (processing_template_decl)
13032 for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13033 TREE_TYPE (TREE_VALUE (pair)) = enumtype;
13034 if (at_function_scope_p ())
13035 add_stmt (build_min (TAG_DEFN, enumtype));
13036 return;
13039 if (TYPE_VALUES (enumtype))
13041 minnode = maxnode = NULL_TREE;
13043 for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13045 tree decl = TREE_VALUE (pair);
13046 tree value = DECL_INITIAL (decl);
13048 /* [dcl.enum]: Following the closing brace of an enum-specifier,
13049 each enumerator has the type of its enumeration. Prior to the
13050 closing brace, the type of each enumerator is the type of its
13051 initializing value. */
13052 TREE_TYPE (decl) = enumtype;
13054 /* Figure out what the minimum and maximum values of the
13055 enumerators are. */
13056 if (!minnode)
13057 minnode = maxnode = value;
13058 else if (tree_int_cst_lt (maxnode, value))
13059 maxnode = value;
13060 else if (tree_int_cst_lt (value, minnode))
13061 minnode = value;
13063 /* Set the TREE_TYPE for the values as well. That's so that when
13064 we call decl_constant_value we get an entity of the right type
13065 (but with the constant value). But first make a copy so we
13066 don't clobber shared INTEGER_CSTs. */
13067 if (TREE_TYPE (value) != enumtype)
13069 value = DECL_INITIAL (decl) = copy_node (value);
13070 TREE_TYPE (value) = enumtype;
13073 /* In addition, transform the TYPE_VALUES list to contain the
13074 values, rather than the CONST_DECLs for them. */
13075 TREE_VALUE (pair) = value;
13078 else
13079 minnode = maxnode = integer_zero_node;
13081 /* Compute the number of bits require to represent all values of the
13082 enumeration. We must do this before the type of MINNODE and
13083 MAXNODE are transformed, since min_precision relies on the
13084 TREE_TYPE of the value it is passed. */
13085 unsignedp = tree_int_cst_sgn (minnode) >= 0;
13086 lowprec = min_precision (minnode, unsignedp);
13087 highprec = min_precision (maxnode, unsignedp);
13088 precision = MAX (lowprec, highprec);
13090 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
13091 TYPE_SIZE (enumtype) = NULL_TREE;
13092 TYPE_PRECISION (enumtype) = precision;
13093 if (unsignedp)
13094 fixup_unsigned_type (enumtype);
13095 else
13096 fixup_signed_type (enumtype);
13098 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13099 /* Use the width of the narrowest normal C type which is wide
13100 enough. */
13101 TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size
13102 (precision, 1));
13103 else
13104 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
13106 TYPE_SIZE (enumtype) = NULL_TREE;
13107 layout_type (enumtype);
13109 /* Fix up all variant types of this enum type. */
13110 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13112 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13113 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13114 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13115 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13116 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13117 TYPE_MODE (t) = TYPE_MODE (enumtype);
13118 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13119 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13120 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13121 TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13124 /* Finish debugging output for this type. */
13125 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13128 /* Build and install a CONST_DECL for an enumeration constant of the
13129 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13130 Assignment of sequential values by default is handled here. */
13132 void
13133 build_enumerator (tree name, tree value, tree enumtype)
13135 tree decl;
13136 tree context;
13137 tree type;
13139 /* Remove no-op casts from the value. */
13140 if (value)
13141 STRIP_TYPE_NOPS (value);
13143 if (! processing_template_decl)
13145 /* Validate and default VALUE. */
13146 if (value != NULL_TREE)
13148 value = decl_constant_value (value);
13150 if (TREE_CODE (value) == INTEGER_CST)
13152 value = default_conversion (value);
13153 constant_expression_warning (value);
13155 else
13157 error ("enumerator value for `%D' not integer constant", name);
13158 value = NULL_TREE;
13162 /* Default based on previous value. */
13163 if (value == NULL_TREE)
13165 tree prev_value;
13167 if (TYPE_VALUES (enumtype))
13169 /* The next value is the previous value ... */
13170 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13171 /* ... plus one. */
13172 value = cp_build_binary_op (PLUS_EXPR,
13173 prev_value,
13174 integer_one_node);
13176 if (tree_int_cst_lt (value, prev_value))
13177 error ("overflow in enumeration values at `%D'", name);
13179 else
13180 value = integer_zero_node;
13183 /* Remove no-op casts from the value. */
13184 STRIP_TYPE_NOPS (value);
13187 /* C++ associates enums with global, function, or class declarations. */
13188 context = current_scope ();
13190 /* Build the actual enumeration constant. Note that the enumeration
13191 constants have the type of their initializers until the
13192 enumeration is complete:
13194 [ dcl.enum ]
13196 Following the closing brace of an enum-specifier, each enumer-
13197 ator has the type of its enumeration. Prior to the closing
13198 brace, the type of each enumerator is the type of its
13199 initializing value.
13201 In finish_enum we will reset the type. Of course, if we're
13202 processing a template, there may be no value. */
13203 type = value ? TREE_TYPE (value) : NULL_TREE;
13205 if (context && context == current_class_type)
13206 /* This enum declaration is local to the class. We need the full
13207 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
13208 decl = build_lang_decl (CONST_DECL, name, type);
13209 else
13210 /* It's a global enum, or it's local to a function. (Note local to
13211 a function could mean local to a class method. */
13212 decl = build_decl (CONST_DECL, name, type);
13214 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13215 TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
13216 DECL_INITIAL (decl) = value;
13218 if (context && context == current_class_type)
13219 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13220 on the TYPE_FIELDS list for `S'. (That's so that you can say
13221 things like `S::i' later.) */
13222 finish_member_declaration (decl);
13223 else
13224 pushdecl (decl);
13226 /* Add this enumeration constant to the list for this type. */
13227 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13231 /* We're defining DECL. Make sure that it's type is OK. */
13233 static void
13234 check_function_type (tree decl, tree current_function_parms)
13236 tree fntype = TREE_TYPE (decl);
13237 tree return_type = complete_type (TREE_TYPE (fntype));
13239 /* In a function definition, arg types must be complete. */
13240 require_complete_types_for_parms (current_function_parms);
13242 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13244 error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13246 /* Make it return void instead, but don't change the
13247 type of the DECL_RESULT, in case we have a named return value. */
13248 if (TREE_CODE (fntype) == METHOD_TYPE)
13250 tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13251 TREE_TYPE (decl)
13252 = build_cplus_method_type (ctype,
13253 void_type_node,
13254 FUNCTION_ARG_CHAIN (decl));
13256 else
13257 TREE_TYPE (decl)
13258 = build_function_type (void_type_node,
13259 TYPE_ARG_TYPES (TREE_TYPE (decl)));
13260 TREE_TYPE (decl)
13261 = build_exception_variant (fntype,
13262 TYPE_RAISES_EXCEPTIONS (fntype));
13264 else
13265 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13268 /* Create the FUNCTION_DECL for a function definition.
13269 DECLSPECS and DECLARATOR are the parts of the declaration;
13270 they describe the function's name and the type it returns,
13271 but twisted together in a fashion that parallels the syntax of C.
13273 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13274 DECLARATOR is really the DECL for the function we are about to
13275 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13276 indicating that the function is an inline defined in-class.
13278 This function creates a binding context for the function body
13279 as well as setting up the FUNCTION_DECL in current_function_decl.
13281 Returns 1 on success. If the DECLARATOR is not suitable for a function
13282 (it defines a datum instead), we return 0, which tells
13283 yyparse to report a parse error.
13285 For C++, we must first check whether that datum makes any sense.
13286 For example, "class A local_a(1,2);" means that variable local_a
13287 is an aggregate of type A, which should have a constructor
13288 applied to it with the argument list [1, 2]. */
13291 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13293 tree decl1;
13294 tree ctype = NULL_TREE;
13295 tree fntype;
13296 tree restype;
13297 int doing_friend = 0;
13298 struct cp_binding_level *bl;
13299 tree current_function_parms;
13301 /* Sanity check. */
13302 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13303 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13305 /* This should only be done once on the top most decl. */
13306 if (have_extern_spec)
13308 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13309 have_extern_spec = false;
13312 if (flags & SF_PRE_PARSED)
13314 decl1 = declarator;
13316 fntype = TREE_TYPE (decl1);
13317 if (TREE_CODE (fntype) == METHOD_TYPE)
13318 ctype = TYPE_METHOD_BASETYPE (fntype);
13320 /* ISO C++ 11.4/5. A friend function defined in a class is in
13321 the (lexical) scope of the class in which it is defined. */
13322 if (!ctype && DECL_FRIEND_P (decl1))
13324 ctype = DECL_FRIEND_CONTEXT (decl1);
13326 /* CTYPE could be null here if we're dealing with a template;
13327 for example, `inline friend float foo()' inside a template
13328 will have no CTYPE set. */
13329 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13330 ctype = NULL_TREE;
13331 else
13332 doing_friend = 1;
13335 last_function_parms = DECL_ARGUMENTS (decl1);
13337 else
13339 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13340 /* If the declarator is not suitable for a function definition,
13341 cause a syntax error. */
13342 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13343 return 0;
13345 cplus_decl_attributes (&decl1, attrs, 0);
13347 /* If #pragma weak was used, mark the decl weak now. */
13348 if (current_binding_level == global_binding_level)
13349 maybe_apply_pragma_weak (decl1);
13351 fntype = TREE_TYPE (decl1);
13353 restype = TREE_TYPE (fntype);
13354 if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
13356 error ("semicolon missing after declaration of `%#T'", restype);
13357 shadow_tag (build_tree_list (NULL_TREE, restype));
13358 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13359 if (TREE_CODE (fntype) == FUNCTION_TYPE)
13360 fntype = build_function_type (integer_type_node,
13361 TYPE_ARG_TYPES (fntype));
13362 else
13363 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13364 integer_type_node,
13365 TYPE_ARG_TYPES (fntype));
13366 TREE_TYPE (decl1) = fntype;
13369 if (TREE_CODE (fntype) == METHOD_TYPE)
13370 ctype = TYPE_METHOD_BASETYPE (fntype);
13371 else if (DECL_MAIN_P (decl1))
13373 /* If this doesn't return integer_type, complain. */
13374 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13376 if (pedantic || warn_return_type)
13377 pedwarn ("return type for `main' changed to `int'");
13378 TREE_TYPE (decl1) = fntype = default_function_type;
13383 if (DECL_DECLARED_INLINE_P (decl1)
13384 && lookup_attribute ("noinline", attrs))
13385 warning ("%Hinline function '%D' given attribute noinline",
13386 &DECL_SOURCE_LOCATION (decl1), decl1);
13388 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13389 /* This is a constructor, we must ensure that any default args
13390 introduced by this definition are propagated to the clones
13391 now. The clones are used directly in overload resolution. */
13392 adjust_clone_args (decl1);
13394 /* Sometimes we don't notice that a function is a static member, and
13395 build a METHOD_TYPE for it. Fix that up now. */
13396 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13397 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13399 revert_static_member_fn (decl1);
13400 last_function_parms = TREE_CHAIN (last_function_parms);
13401 ctype = NULL_TREE;
13404 /* Warn if function was previously implicitly declared
13405 (but not if we warned then). */
13406 if (! warn_implicit
13407 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13408 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13410 /* Set up current_class_type, and enter the scope of the class, if
13411 appropriate. */
13412 if (ctype)
13413 push_nested_class (ctype);
13414 else if (DECL_STATIC_FUNCTION_P (decl1))
13415 push_nested_class (DECL_CONTEXT (decl1));
13417 /* Now that we have entered the scope of the class, we must restore
13418 the bindings for any template parameters surrounding DECL1, if it
13419 is an inline member template. (Order is important; consider the
13420 case where a template parameter has the same name as a field of
13421 the class.) It is not until after this point that
13422 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13423 if (flags & SF_INCLASS_INLINE)
13424 maybe_begin_member_template_processing (decl1);
13426 /* Effective C++ rule 15. */
13427 if (warn_ecpp
13428 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13429 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13430 warning ("`operator=' should return a reference to `*this'");
13432 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13433 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13434 if (!DECL_INITIAL (decl1))
13435 DECL_INITIAL (decl1) = error_mark_node;
13437 /* This function exists in static storage.
13438 (This does not mean `static' in the C sense!) */
13439 TREE_STATIC (decl1) = 1;
13441 /* We must call push_template_decl after current_class_type is set
13442 up. (If we are processing inline definitions after exiting a
13443 class scope, current_class_type will be NULL_TREE until set above
13444 by push_nested_class.) */
13445 if (processing_template_decl)
13446 decl1 = push_template_decl (decl1);
13448 /* We are now in the scope of the function being defined. */
13449 current_function_decl = decl1;
13451 /* Save the parm names or decls from this function's declarator
13452 where store_parm_decls will find them. */
13453 current_function_parms = last_function_parms;
13455 /* Make sure the parameter and return types are reasonable. When
13456 you declare a function, these types can be incomplete, but they
13457 must be complete when you define the function. */
13458 if (! processing_template_decl)
13459 check_function_type (decl1, current_function_parms);
13461 /* Build the return declaration for the function. */
13462 restype = TREE_TYPE (fntype);
13463 /* Promote the value to int before returning it. */
13464 if (c_promoting_integer_type_p (restype))
13465 restype = type_promotes_to (restype);
13466 if (DECL_RESULT (decl1) == NULL_TREE)
13468 DECL_RESULT (decl1)
13469 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13470 c_apply_type_quals_to_decl (cp_type_quals (restype),
13471 DECL_RESULT (decl1));
13474 /* Initialize RTL machinery. We cannot do this until
13475 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13476 even when processing a template; this is how we get
13477 CFUN set up, and our per-function variables initialized.
13478 FIXME factor out the non-RTL stuff. */
13479 bl = current_binding_level;
13480 init_function_start (decl1, input_filename, lineno);
13481 current_binding_level = bl;
13483 /* Even though we're inside a function body, we still don't want to
13484 call expand_expr to calculate the size of a variable-sized array.
13485 We haven't necessarily assigned RTL to all variables yet, so it's
13486 not safe to try to expand expressions involving them. */
13487 immediate_size_expand = 0;
13488 cfun->x_dont_save_pending_sizes_p = 1;
13490 /* Start the statement-tree, start the tree now. */
13491 begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13493 /* Don't double-count statements in templates. */
13494 DECL_NUM_STMTS (decl1) = 0;
13496 /* Let the user know we're compiling this function. */
13497 announce_function (decl1);
13499 /* Record the decl so that the function name is defined.
13500 If we already have a decl for this name, and it is a FUNCTION_DECL,
13501 use the old decl. */
13502 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13504 /* A specialization is not used to guide overload resolution. */
13505 if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13506 && ! DECL_FUNCTION_MEMBER_P (decl1))
13507 decl1 = pushdecl (decl1);
13508 else
13510 /* We need to set the DECL_CONTEXT. */
13511 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13512 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13513 /* And make sure we have enough default args. */
13514 check_default_args (decl1);
13516 fntype = TREE_TYPE (decl1);
13519 /* Reset these in case the call to pushdecl changed them. */
13520 current_function_decl = decl1;
13521 cfun->decl = decl1;
13523 /* If we are (erroneously) defining a function that we have already
13524 defined before, wipe out what we knew before. */
13525 if (!DECL_PENDING_INLINE_P (decl1))
13526 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13528 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13530 /* We know that this was set up by `grokclassfn'. We do not
13531 wait until `store_parm_decls', since evil parse errors may
13532 never get us to that point. Here we keep the consistency
13533 between `current_class_type' and `current_class_ptr'. */
13534 tree t = DECL_ARGUMENTS (decl1);
13536 my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13537 162);
13538 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13539 19990811);
13541 cp_function_chain->x_current_class_ref
13542 = build_indirect_ref (t, NULL);
13543 cp_function_chain->x_current_class_ptr = t;
13545 /* Constructors and destructors need to know whether they're "in
13546 charge" of initializing virtual base classes. */
13547 t = TREE_CHAIN (t);
13548 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13550 current_in_charge_parm = t;
13551 t = TREE_CHAIN (t);
13553 if (DECL_HAS_VTT_PARM_P (decl1))
13555 if (DECL_NAME (t) != vtt_parm_identifier)
13556 abort ();
13557 current_vtt_parm = t;
13561 if (DECL_INTERFACE_KNOWN (decl1))
13563 tree ctx = decl_function_context (decl1);
13565 if (DECL_NOT_REALLY_EXTERN (decl1))
13566 DECL_EXTERNAL (decl1) = 0;
13568 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13569 && TREE_PUBLIC (ctx))
13570 /* This is a function in a local class in an extern inline
13571 function. */
13572 comdat_linkage (decl1);
13574 /* If this function belongs to an interface, it is public.
13575 If it belongs to someone else's interface, it is also external.
13576 This only affects inlines and template instantiations. */
13577 else if (interface_unknown == 0
13578 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13579 || flag_alt_external_templates))
13581 if (DECL_DECLARED_INLINE_P (decl1)
13582 || DECL_TEMPLATE_INSTANTIATION (decl1)
13583 || processing_template_decl)
13585 DECL_EXTERNAL (decl1)
13586 = (interface_only
13587 || (DECL_DECLARED_INLINE_P (decl1)
13588 && ! flag_implement_inlines
13589 && !DECL_VINDEX (decl1)));
13591 /* For WIN32 we also want to put these in linkonce sections. */
13592 maybe_make_one_only (decl1);
13594 else
13595 DECL_EXTERNAL (decl1) = 0;
13596 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13597 DECL_INTERFACE_KNOWN (decl1) = 1;
13599 else if (interface_unknown && interface_only
13600 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13601 || flag_alt_external_templates))
13603 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13604 interface, we will have interface_only set but not
13605 interface_known. In that case, we don't want to use the normal
13606 heuristics because someone will supply a #pragma implementation
13607 elsewhere, and deducing it here would produce a conflict. */
13608 comdat_linkage (decl1);
13609 DECL_EXTERNAL (decl1) = 0;
13610 DECL_INTERFACE_KNOWN (decl1) = 1;
13611 DECL_DEFER_OUTPUT (decl1) = 1;
13613 else
13615 /* This is a definition, not a reference.
13616 So clear DECL_EXTERNAL. */
13617 DECL_EXTERNAL (decl1) = 0;
13619 if ((DECL_DECLARED_INLINE_P (decl1)
13620 || DECL_TEMPLATE_INSTANTIATION (decl1))
13621 && ! DECL_INTERFACE_KNOWN (decl1)
13622 /* Don't try to defer nested functions for now. */
13623 && ! decl_function_context (decl1))
13624 DECL_DEFER_OUTPUT (decl1) = 1;
13625 else
13626 DECL_INTERFACE_KNOWN (decl1) = 1;
13629 pushlevel (0);
13630 current_binding_level->parm_flag = 1;
13632 ++function_depth;
13634 if (DECL_DESTRUCTOR_P (decl1))
13636 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13637 DECL_CONTEXT (dtor_label) = current_function_decl;
13640 start_fname_decls ();
13642 store_parm_decls (current_function_parms);
13644 return 1;
13647 /* Store the parameter declarations into the current function declaration.
13648 This is called after parsing the parameter declarations, before
13649 digesting the body of the function.
13651 Also install to binding contour return value identifier, if any. */
13653 static void
13654 store_parm_decls (tree current_function_parms)
13656 register tree fndecl = current_function_decl;
13657 register tree parm;
13659 /* This is a chain of any other decls that came in among the parm
13660 declarations. If a parm is declared with enum {foo, bar} x;
13661 then CONST_DECLs for foo and bar are put here. */
13662 tree nonparms = NULL_TREE;
13664 if (current_function_parms)
13666 /* This case is when the function was defined with an ANSI prototype.
13667 The parms already have decls, so we need not do anything here
13668 except record them as in effect
13669 and complain if any redundant old-style parm decls were written. */
13671 tree specparms = current_function_parms;
13672 tree next;
13674 /* Must clear this because it might contain TYPE_DECLs declared
13675 at class level. */
13676 storedecls (NULL_TREE);
13678 /* If we're doing semantic analysis, then we'll call pushdecl
13679 for each of these. We must do them in reverse order so that
13680 they end in the correct forward order. */
13681 specparms = nreverse (specparms);
13683 for (parm = specparms; parm; parm = next)
13685 next = TREE_CHAIN (parm);
13686 if (TREE_CODE (parm) == PARM_DECL)
13688 if (DECL_NAME (parm) == NULL_TREE
13689 || TREE_CODE (parm) != VOID_TYPE)
13690 pushdecl (parm);
13691 else
13692 error ("parameter `%D' declared void", parm);
13694 else
13696 /* If we find an enum constant or a type tag,
13697 put it aside for the moment. */
13698 TREE_CHAIN (parm) = NULL_TREE;
13699 nonparms = chainon (nonparms, parm);
13703 /* Get the decls in their original chain order and record in the
13704 function. This is all and only the PARM_DECLs that were
13705 pushed into scope by the loop above. */
13706 DECL_ARGUMENTS (fndecl) = getdecls ();
13707 storetags (gettags ());
13709 else
13710 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13712 /* Now store the final chain of decls for the arguments
13713 as the decl-chain of the current lexical scope.
13714 Put the enumerators in as well, at the front so that
13715 DECL_ARGUMENTS is not modified. */
13716 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13718 /* Do the starting of the exception specifications, if we have any. */
13719 if (flag_exceptions && !processing_template_decl
13720 && flag_enforce_eh_specs
13721 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13722 current_eh_spec_block = begin_eh_spec_block ();
13726 /* We have finished doing semantic analysis on DECL, but have not yet
13727 generated RTL for its body. Save away our current state, so that
13728 when we want to generate RTL later we know what to do. */
13730 static void
13731 save_function_data (tree decl)
13733 struct language_function *f;
13735 /* Save the language-specific per-function data so that we can
13736 get it back when we really expand this function. */
13737 my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13738 19990908);
13740 /* Make a copy. */
13741 f = ((struct language_function *)
13742 ggc_alloc (sizeof (struct language_function)));
13743 memcpy (f, cp_function_chain, sizeof (struct language_function));
13744 DECL_SAVED_FUNCTION_DATA (decl) = f;
13746 /* Clear out the bits we don't need. */
13747 f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13748 f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13749 f->x_named_label_uses = NULL;
13750 f->bindings = NULL;
13751 f->x_local_names = NULL;
13753 /* When we get back here again, we will be expanding. */
13754 f->x_expanding_p = 1;
13756 /* If we've already decided that we cannot inline this function, we
13757 must remember that fact when we actually go to expand the
13758 function. */
13759 if (current_function_cannot_inline)
13761 f->cannot_inline = current_function_cannot_inline;
13762 DECL_INLINE (decl) = 0;
13766 /* Add a note to mark the beginning of the main body of the constructor.
13767 This is used to set up the data structures for the cleanup regions for
13768 fully-constructed bases and members. */
13770 static void
13771 begin_constructor_body (void)
13775 /* Add a note to mark the end of the main body of the constructor. This is
13776 used to end the cleanup regions for fully-constructed bases and
13777 members. */
13779 static void
13780 finish_constructor_body (void)
13784 /* Do all the processing for the beginning of a destructor; set up the
13785 vtable pointers and cleanups for bases and members. */
13787 static void
13788 begin_destructor_body (void)
13790 tree if_stmt;
13791 tree compound_stmt;
13793 /* If the dtor is empty, and we know there is not any possible
13794 way we could use any vtable entries, before they are possibly
13795 set by a base class dtor, we don't have to setup the vtables,
13796 as we know that any base class dtor will set up any vtables
13797 it needs. We avoid MI, because one base class dtor can do a
13798 virtual dispatch to an overridden function that would need to
13799 have a non-related vtable set up, we cannot avoid setting up
13800 vtables in that case. We could change this to see if there
13801 is just one vtable.
13803 ??? In the destructor for a class, the vtables are set
13804 appropriately for that class. There will be no non-related
13805 vtables. jason 2001-12-11. */
13806 if_stmt = begin_if_stmt ();
13808 /* If it is not safe to avoid setting up the vtables, then
13809 someone will change the condition to be boolean_true_node.
13810 (Actually, for now, we do not have code to set the condition
13811 appropriately, so we just assume that we always need to
13812 initialize the vtables.) */
13813 finish_if_stmt_cond (boolean_true_node, if_stmt);
13815 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13817 /* Make all virtual function table pointers in non-virtual base
13818 classes point to CURRENT_CLASS_TYPE's virtual function
13819 tables. */
13820 initialize_vtbl_ptrs (current_class_ptr);
13822 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13823 finish_then_clause (if_stmt);
13824 finish_if_stmt ();
13826 /* And insert cleanups for our bases and members so that they
13827 will be properly destroyed if we throw. */
13828 push_base_cleanups ();
13831 /* At the end of every destructor we generate code to delete the object if
13832 necessary. Do that now. */
13834 static void
13835 finish_destructor_body (void)
13837 tree exprstmt;
13839 /* Any return from a destructor will end up here; that way all base
13840 and member cleanups will be run when the function returns. */
13841 add_stmt (build_stmt (LABEL_STMT, dtor_label));
13843 /* In a virtual destructor, we must call delete. */
13844 if (DECL_VIRTUAL_P (current_function_decl))
13846 tree if_stmt;
13847 tree virtual_size = cxx_sizeof (current_class_type);
13849 /* [class.dtor]
13851 At the point of definition of a virtual destructor (including
13852 an implicit definition), non-placement operator delete shall
13853 be looked up in the scope of the destructor's class and if
13854 found shall be accessible and unambiguous. */
13855 exprstmt = build_op_delete_call
13856 (DELETE_EXPR, current_class_ptr, virtual_size,
13857 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13859 if_stmt = begin_if_stmt ();
13860 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13861 current_in_charge_parm,
13862 integer_one_node),
13863 if_stmt);
13864 finish_expr_stmt (exprstmt);
13865 finish_then_clause (if_stmt);
13866 finish_if_stmt ();
13870 /* Do the necessary processing for the beginning of a function body, which
13871 in this case includes member-initializers, but not the catch clauses of
13872 a function-try-block. Currently, this means opening a binding level
13873 for the member-initializers (in a ctor) and member cleanups (in a dtor).
13874 In other functions, this isn't necessary, but it doesn't hurt. */
13876 tree
13877 begin_function_body (void)
13879 tree stmt;
13881 if (processing_template_decl)
13882 /* Do nothing now. */;
13883 else
13884 /* Always keep the BLOCK node associated with the outermost pair of
13885 curly braces of a function. These are needed for correct
13886 operation of dwarfout.c. */
13887 keep_next_level (1);
13889 stmt = begin_compound_stmt (0);
13890 COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13892 if (processing_template_decl)
13893 /* Do nothing now. */;
13894 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13895 begin_constructor_body ();
13896 else if (DECL_DESTRUCTOR_P (current_function_decl))
13897 begin_destructor_body ();
13899 return stmt;
13902 /* Do the processing for the end of a function body. Currently, this means
13903 closing out the cleanups for fully-constructed bases and members, and in
13904 the case of the destructor, deleting the object if desired. Again, this
13905 is only meaningful for [cd]tors, since they are the only functions where
13906 there is a significant distinction between the main body and any
13907 function catch clauses. Handling, say, main() return semantics here
13908 would be wrong, as flowing off the end of a function catch clause for
13909 main() would also need to return 0. */
13911 void
13912 finish_function_body (tree compstmt)
13914 /* Close the block. */
13915 finish_compound_stmt (0, compstmt);
13917 if (processing_template_decl)
13918 /* Do nothing now. */;
13919 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13920 finish_constructor_body ();
13921 else if (DECL_DESTRUCTOR_P (current_function_decl))
13922 finish_destructor_body ();
13925 /* Finish up a function declaration and compile that function
13926 all the way to assembler language output. The free the storage
13927 for the function definition.
13929 FLAGS is a bitwise or of the following values:
13930 2 - INCLASS_INLINE
13931 We just finished processing the body of an in-class inline
13932 function definition. (This processing will have taken place
13933 after the class definition is complete.) */
13935 tree
13936 finish_function (int flags)
13938 register tree fndecl = current_function_decl;
13939 tree fntype, ctype = NULL_TREE;
13940 int inclass_inline = (flags & 2) != 0;
13941 int nested;
13943 /* When we get some parse errors, we can end up without a
13944 current_function_decl, so cope. */
13945 if (fndecl == NULL_TREE)
13946 return error_mark_node;
13948 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13949 && DECL_VIRTUAL_P (fndecl)
13950 && !processing_template_decl)
13952 tree fnclass = DECL_CONTEXT (fndecl);
13953 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
13954 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
13957 nested = function_depth > 1;
13958 fntype = TREE_TYPE (fndecl);
13960 /* TREE_READONLY (fndecl) = 1;
13961 This caused &foo to be of type ptr-to-const-function
13962 which then got a warning when stored in a ptr-to-function variable. */
13964 my_friendly_assert (building_stmt_tree (), 20000911);
13966 finish_fname_decls ();
13968 /* For a cloned function, we've already got all the code we need;
13969 there's no need to add any extra bits. */
13970 if (!DECL_CLONED_FUNCTION_P (fndecl))
13972 if (DECL_MAIN_P (current_function_decl))
13974 /* Make it so that `main' always returns 0 by default. */
13975 #if VMS_TARGET
13976 finish_return_stmt (integer_one_node);
13977 #else
13978 finish_return_stmt (integer_zero_node);
13979 #endif
13982 /* Finish dealing with exception specifiers. */
13983 if (flag_exceptions && !processing_template_decl
13984 && flag_enforce_eh_specs
13985 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13986 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
13987 (TREE_TYPE (current_function_decl)),
13988 current_eh_spec_block);
13991 /* If we're saving up tree structure, tie off the function now. */
13992 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
13994 /* If this function can't throw any exceptions, remember that. */
13995 if (!processing_template_decl
13996 && !cp_function_chain->can_throw
13997 && !flag_non_call_exceptions)
13998 TREE_NOTHROW (fndecl) = 1;
14000 /* This must come after expand_function_end because cleanups might
14001 have declarations (from inline functions) that need to go into
14002 this function's blocks. */
14004 /* If the current binding level isn't the outermost binding level
14005 for this function, either there is a bug, or we have experienced
14006 syntax errors and the statement tree is malformed. */
14007 if (current_binding_level->parm_flag != 1)
14009 /* Make sure we have already experienced errors. */
14010 if (errorcount == 0)
14011 abort ();
14013 /* Throw away the broken statement tree and extra binding
14014 levels. */
14015 DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14017 while (current_binding_level->parm_flag != 1)
14019 if (current_binding_level->parm_flag == 2)
14020 pop_nested_class ();
14021 else
14022 poplevel (0, 0, 0);
14025 poplevel (1, 0, 1);
14027 /* Set up the named return value optimization, if we can. Here, we
14028 eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14029 for the nrv. genrtl_start_function and declare_return_variable
14030 handle making the nrv and RESULT_DECL share space. */
14031 if (current_function_return_value)
14033 tree r = current_function_return_value;
14034 tree outer;
14036 if (r != error_mark_node
14037 /* This is only worth doing for fns that return in memory--and
14038 simpler, since we don't have to worry about promoted modes. */
14039 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)))
14040 /* Only allow this for variables declared in the outer scope of
14041 the function so we know that their lifetime always ends with a
14042 return; see g++.dg/opt/nrv6.C. We could be more flexible if
14043 we were to do this optimization in tree-ssa. */
14044 /* Skip the artificial function body block. */
14045 && (outer = BLOCK_SUBBLOCKS (BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl))),
14046 chain_member (r, BLOCK_VARS (outer))))
14049 DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14050 walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14051 nullify_returns_r, r);
14053 else
14054 /* Clear it so genrtl_start_function and declare_return_variable
14055 know we're not optimizing. */
14056 current_function_return_value = NULL_TREE;
14059 /* Remember that we were in class scope. */
14060 if (current_class_name)
14061 ctype = current_class_type;
14063 /* Must mark the RESULT_DECL as being in this function. */
14064 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14066 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14067 to the FUNCTION_DECL node itself. */
14068 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14070 /* Save away current state, if appropriate. */
14071 if (!processing_template_decl)
14072 save_function_data (fndecl);
14074 /* If this function calls `setjmp' it cannot be inlined. When
14075 `longjmp' is called it is not guaranteed to restore the value of
14076 local variables that have been modified since the call to
14077 `setjmp'. So, if were to inline this function into some caller
14078 `c', then when we `longjmp', we might not restore all variables
14079 in `c'. (It might seem, at first blush, that there's no way for
14080 this function to modify local variables in `c', but their
14081 addresses may have been stored somewhere accessible to this
14082 function.) */
14083 if (!processing_template_decl && calls_setjmp_p (fndecl))
14084 DECL_UNINLINABLE (fndecl) = 1;
14086 /* Complain if there's just no return statement. */
14087 if (warn_return_type
14088 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14089 && !current_function_returns_value && !current_function_returns_null
14090 /* Don't complain if we abort or throw. */
14091 && !current_function_returns_abnormally
14092 && !DECL_NAME (DECL_RESULT (fndecl))
14093 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
14094 inline function, as we might never be compiled separately. */
14095 && (DECL_INLINE (fndecl) || processing_template_decl))
14096 warning ("no return statement in function returning non-void");
14098 /* Clear out memory we no longer need. */
14099 free_after_parsing (cfun);
14100 /* Since we never call rest_of_compilation, we never clear
14101 CFUN. Do so explicitly. */
14102 free_after_compilation (cfun);
14103 cfun = NULL;
14105 /* If this is an in-class inline definition, we may have to pop the
14106 bindings for the template parameters that we added in
14107 maybe_begin_member_template_processing when start_function was
14108 called. */
14109 if (inclass_inline)
14110 maybe_end_member_template_processing ();
14112 /* Leave the scope of the class. */
14113 if (ctype)
14114 pop_nested_class ();
14116 --function_depth;
14118 /* Clean up. */
14119 if (! nested)
14120 /* Let the error reporting routines know that we're outside a
14121 function. For a nested function, this value is used in
14122 cxx_pop_function_context and then reset via pop_function_context. */
14123 current_function_decl = NULL_TREE;
14125 return fndecl;
14128 /* Create the FUNCTION_DECL for a function definition.
14129 DECLSPECS and DECLARATOR are the parts of the declaration;
14130 they describe the return type and the name of the function,
14131 but twisted together in a fashion that parallels the syntax of C.
14133 This function creates a binding context for the function body
14134 as well as setting up the FUNCTION_DECL in current_function_decl.
14136 Returns a FUNCTION_DECL on success.
14138 If the DECLARATOR is not suitable for a function (it defines a datum
14139 instead), we return 0, which tells yyparse to report a parse error.
14141 May return void_type_node indicating that this method is actually
14142 a friend. See grokfield for more details.
14144 Came here with a `.pushlevel' .
14146 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14147 CHANGES TO CODE IN `grokfield'. */
14149 tree
14150 start_method (tree declspecs, tree declarator, tree attrlist)
14152 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14153 &attrlist);
14155 if (fndecl == error_mark_node)
14156 return error_mark_node;
14158 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14160 error ("invalid member function declaration");
14161 return error_mark_node;
14164 if (attrlist)
14165 cplus_decl_attributes (&fndecl, attrlist, 0);
14167 /* Pass friends other than inline friend functions back. */
14168 if (fndecl == void_type_node)
14169 return fndecl;
14171 if (DECL_IN_AGGR_P (fndecl))
14173 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14175 if (DECL_CONTEXT (fndecl)
14176 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14177 error ("`%D' is already defined in class `%T'", fndecl,
14178 DECL_CONTEXT (fndecl));
14180 return void_type_node;
14183 check_template_shadow (fndecl);
14185 DECL_DECLARED_INLINE_P (fndecl) = 1;
14187 DID_INLINE_FUNC (fndecl) = 0;
14188 if (flag_default_inline)
14189 DECL_INLINE (fndecl) = 1;
14191 /* We process method specializations in finish_struct_1. */
14192 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14193 fndecl = push_template_decl (fndecl);
14195 if (! DECL_FRIEND_P (fndecl))
14197 if (TREE_CHAIN (fndecl))
14199 fndecl = copy_node (fndecl);
14200 TREE_CHAIN (fndecl) = NULL_TREE;
14202 grok_special_member_properties (fndecl);
14205 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14207 /* Make a place for the parms */
14208 pushlevel (0);
14209 current_binding_level->parm_flag = 1;
14211 DECL_IN_AGGR_P (fndecl) = 1;
14212 return fndecl;
14215 /* Go through the motions of finishing a function definition.
14216 We don't compile this method until after the whole class has
14217 been processed.
14219 FINISH_METHOD must return something that looks as though it
14220 came from GROKFIELD (since we are defining a method, after all).
14222 This is called after parsing the body of the function definition.
14223 STMTS is the chain of statements that makes up the function body.
14225 DECL is the ..._DECL that `start_method' provided. */
14227 tree
14228 finish_method (tree decl)
14230 register tree fndecl = decl;
14231 tree old_initial;
14233 register tree link;
14235 if (decl == void_type_node)
14236 return decl;
14238 old_initial = DECL_INITIAL (fndecl);
14240 /* Undo the level for the parms (from start_method).
14241 This is like poplevel, but it causes nothing to be
14242 saved. Saving information here confuses symbol-table
14243 output routines. Besides, this information will
14244 be correctly output when this method is actually
14245 compiled. */
14247 /* Clear out the meanings of the local variables of this level;
14248 also record in each decl which block it belongs to. */
14250 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14252 if (DECL_NAME (link) != NULL_TREE)
14253 pop_binding (DECL_NAME (link), link);
14254 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14255 DECL_CONTEXT (link) = NULL_TREE;
14258 poplevel (0, 0, 0);
14260 DECL_INITIAL (fndecl) = old_initial;
14262 /* We used to check if the context of FNDECL was different from
14263 current_class_type as another way to get inside here. This didn't work
14264 for String.cc in libg++. */
14265 if (DECL_FRIEND_P (fndecl))
14267 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14268 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14269 decl = void_type_node;
14272 return decl;
14276 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14277 we can lay it out later, when and if its type becomes complete. */
14279 void
14280 maybe_register_incomplete_var (tree var)
14282 my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14284 /* Keep track of variables with incomplete types. */
14285 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14286 && DECL_EXTERNAL (var))
14288 tree inner_type = TREE_TYPE (var);
14290 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14291 inner_type = TREE_TYPE (inner_type);
14292 inner_type = TYPE_MAIN_VARIANT (inner_type);
14294 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14295 /* RTTI TD entries are created while defining the type_info. */
14296 || (TYPE_LANG_SPECIFIC (inner_type)
14297 && TYPE_BEING_DEFINED (inner_type)))
14298 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14302 /* Called when a class type (given by TYPE) is defined. If there are
14303 any existing VAR_DECLs whose type hsa been completed by this
14304 declaration, update them now. */
14306 void
14307 complete_vars (tree type)
14309 tree *list = &incomplete_vars;
14311 my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14312 while (*list)
14314 if (same_type_p (type, TREE_PURPOSE (*list)))
14316 tree var = TREE_VALUE (*list);
14317 /* Complete the type of the variable. The VAR_DECL itself
14318 will be laid out in expand_expr. */
14319 complete_type (TREE_TYPE (var));
14320 /* Remove this entry from the list. */
14321 *list = TREE_CHAIN (*list);
14323 else
14324 list = &TREE_CHAIN (*list);
14328 /* If DECL is of a type which needs a cleanup, build that cleanup
14329 here. */
14331 tree
14332 cxx_maybe_build_cleanup (tree decl)
14334 tree type = TREE_TYPE (decl);
14336 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14338 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14339 tree rval;
14341 if (TREE_CODE (type) == ARRAY_TYPE)
14342 rval = decl;
14343 else
14345 cxx_mark_addressable (decl);
14346 rval = build_unary_op (ADDR_EXPR, decl, 0);
14349 /* Optimize for space over speed here. */
14350 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14351 || flag_expensive_optimizations)
14352 flags |= LOOKUP_NONVIRTUAL;
14354 rval = build_delete (TREE_TYPE (rval), rval,
14355 sfk_complete_destructor, flags, 0);
14357 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14358 && ! TYPE_HAS_DESTRUCTOR (type))
14359 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
14360 build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
14362 return rval;
14364 return NULL_TREE;
14367 /* When a stmt has been parsed, this function is called. */
14369 void
14370 finish_stmt (void)
14372 /* Always assume this statement was not an expression statement. If
14373 it actually was an expression statement, its our callers
14374 responsibility to fix this up. */
14375 last_expr_type = NULL_TREE;
14378 /* DECL was originally constructed as a non-static member function,
14379 but turned out to be static. Update it accordingly. */
14381 void
14382 revert_static_member_fn (tree decl)
14384 tree tmp;
14385 tree function = TREE_TYPE (decl);
14386 tree args = TYPE_ARG_TYPES (function);
14388 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14389 != TYPE_UNQUALIFIED)
14390 error ("static member function `%#D' declared with type qualifiers",
14391 decl);
14393 args = TREE_CHAIN (args);
14394 tmp = build_function_type (TREE_TYPE (function), args);
14395 tmp = build_qualified_type (tmp, cp_type_quals (function));
14396 tmp = build_exception_variant (tmp,
14397 TYPE_RAISES_EXCEPTIONS (function));
14398 TREE_TYPE (decl) = tmp;
14399 if (DECL_ARGUMENTS (decl))
14400 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14401 DECL_STATIC_FUNCTION_P (decl) = 1;
14404 /* Initialize the variables used during compilation of a C++
14405 function. */
14407 void
14408 cxx_push_function_context (struct function * f)
14410 struct language_function *p
14411 = ((struct language_function *)
14412 ggc_alloc_cleared (sizeof (struct language_function)));
14413 f->language = p;
14415 /* It takes an explicit call to expand_body to generate RTL for a
14416 function. */
14417 expanding_p = 0;
14419 /* Whenever we start a new function, we destroy temporaries in the
14420 usual way. */
14421 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14424 /* Free the language-specific parts of F, now that we've finished
14425 compiling the function. */
14427 void
14428 cxx_pop_function_context (struct function * f)
14430 f->language = 0;
14433 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14434 one of the language-independent trees. */
14436 enum cp_tree_node_structure_enum
14437 cp_tree_node_structure (union lang_tree_node * t)
14439 switch (TREE_CODE (&t->generic))
14441 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
14442 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
14443 case OVERLOAD: return TS_CP_OVERLOAD;
14444 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14445 case PTRMEM_CST: return TS_CP_PTRMEM;
14446 case BASELINK: return TS_CP_BASELINK;
14447 case WRAPPER: return TS_CP_WRAPPER;
14448 default: return TS_CP_GENERIC;
14452 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14453 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
14455 tree
14456 identifier_global_value (tree t)
14458 return IDENTIFIER_GLOBAL_VALUE (t);
14461 /* Build the void_list_node (void_type_node having been created). */
14462 tree
14463 build_void_list_node (void)
14465 tree t = build_tree_list (NULL_TREE, void_type_node);
14466 TREE_PARMLIST (t) = 1;
14467 return t;
14470 static int
14471 cp_missing_noreturn_ok_p (tree decl)
14473 /* A missing noreturn is ok for the `main' function. */
14474 return DECL_MAIN_P (decl);
14477 #include "gt-cp-decl.h"
14478 #include "gtype-cp.h"