Compile-time improvement: 2/n.
[official-gcc.git] / gcc / cp / decl.c
blobdb116e1f7a87fec42e93e0a7e2f4ceeccb3943fb
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 cxx_binding *find_binding (tree, tree, cxx_binding *);
102 static tree select_decl (cxx_binding *, int);
103 static int lookup_flags (int, int);
104 static tree qualify_lookup (tree, int);
105 static tree record_builtin_java_type (const char *, int);
106 static const char *tag_name (enum tag_types code);
107 static void find_class_binding_level (void);
108 static struct cp_binding_level *innermost_nonclass_level (void);
109 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
110 static int walk_globals_r (tree, void*);
111 static int walk_vtables_r (tree, void*);
112 static void add_decl_to_level (tree, struct cp_binding_level *);
113 static tree make_label_decl (tree, int);
114 static void use_label (tree);
115 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
116 const char *, int);
117 static void check_previous_goto (struct named_label_use_list *);
118 static void check_switch_goto (struct cp_binding_level *);
119 static void check_previous_gotos (tree);
120 static void pop_label (tree, tree);
121 static void pop_labels (tree);
122 static void maybe_deduce_size_from_array_init (tree, tree);
123 static void layout_var_decl (tree);
124 static void maybe_commonize_var (tree);
125 static tree check_initializer (tree, tree, int);
126 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
127 static void save_function_data (tree);
128 static void check_function_type (tree, tree);
129 static void begin_constructor_body (void);
130 static void finish_constructor_body (void);
131 static void begin_destructor_body (void);
132 static void finish_destructor_body (void);
133 static tree create_array_type_for_decl (tree, tree, tree);
134 static tree get_atexit_node (void);
135 static tree get_dso_handle_node (void);
136 static tree start_cleanup_fn (void);
137 static void end_cleanup_fn (void);
138 static tree cp_make_fname_decl (tree, int);
139 static void initialize_predefined_identifiers (void);
140 static tree check_special_function_return_type
141 (special_function_kind, tree, tree);
142 static tree push_cp_library_fn (enum tree_code, tree);
143 static tree build_cp_library_fn (tree, enum tree_code, tree);
144 static void store_parm_decls (tree);
145 static int cp_missing_noreturn_ok_p (tree);
146 static void initialize_local_var (tree, tree);
147 static void expand_static_init (tree, tree);
148 static tree next_initializable_field (tree);
149 static tree reshape_init (tree, tree *);
150 static tree build_typename_type (tree, tree, tree);
152 #if defined (DEBUG_BINDING_LEVELS)
153 static void indent (void);
154 #endif
156 /* Erroneous argument lists can use this *IFF* they do not modify it. */
157 tree error_mark_list;
159 /* The following symbols are subsumed in the cp_global_trees array, and
160 listed here individually for documentation purposes.
162 C++ extensions
163 tree wchar_decl_node;
165 tree vtable_entry_type;
166 tree delta_type_node;
167 tree __t_desc_type_node;
168 tree ti_desc_type_node;
169 tree bltn_desc_type_node, ptr_desc_type_node;
170 tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
171 tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
172 tree ptm_desc_type_node;
173 tree base_desc_type_node;
175 tree class_type_node, record_type_node, union_type_node, enum_type_node;
176 tree unknown_type_node;
178 Array type `vtable_entry_type[]'
180 tree vtbl_type_node;
181 tree vtbl_ptr_type_node;
183 Namespaces,
185 tree std_node;
186 tree abi_node;
188 A FUNCTION_DECL which can call `abort'. Not necessarily the
189 one that the user will declare, but sufficient to be called
190 by routines that want to abort the program.
192 tree abort_fndecl;
194 The FUNCTION_DECL for the default `::operator delete'.
196 tree global_delete_fndecl;
198 Used by RTTI
199 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
200 tree tinfo_var_id;
204 tree cp_global_trees[CPTI_MAX];
206 /* Indicates that there is a type value in some namespace, although
207 that is not necessarily in scope at the moment. */
209 static GTY(()) tree global_type_node;
211 /* Used only for jumps to as-yet undefined labels, since jumps to
212 defined labels can have their validity checked immediately. */
214 struct named_label_use_list GTY(())
216 struct cp_binding_level *binding_level;
217 tree names_in_scope;
218 tree label_decl;
219 const char *filename_o_goto;
220 int lineno_o_goto;
221 struct named_label_use_list *next;
224 #define named_label_uses cp_function_chain->x_named_label_uses
226 #define local_names cp_function_chain->x_local_names
228 /* A list of objects which have constructors or destructors
229 which reside in the global scope. The decl is stored in
230 the TREE_VALUE slot and the initializer is stored
231 in the TREE_PURPOSE slot. */
232 tree static_aggregates;
234 /* -- end of C++ */
236 /* A node for the integer constants 2, and 3. */
238 tree integer_two_node, integer_three_node;
240 /* Similar, for last_function_parm_tags. */
241 tree last_function_parms;
243 /* A list of all LABEL_DECLs in the function that have names. Here so
244 we can clear out their names' definitions at the end of the
245 function, and so we can check the validity of jumps to these labels. */
247 struct named_label_list GTY(())
249 struct cp_binding_level *binding_level;
250 tree names_in_scope;
251 tree old_value;
252 tree label_decl;
253 tree bad_decls;
254 struct named_label_list *next;
255 unsigned int in_try_scope : 1;
256 unsigned int in_catch_scope : 1;
259 #define named_labels cp_function_chain->x_named_labels
261 /* The name of the anonymous namespace, throughout this translation
262 unit. */
263 tree anonymous_namespace_name;
265 /* The number of function bodies which we are currently processing.
266 (Zero if we are at namespace scope, one inside the body of a
267 function, two inside the body of a function in a local class, etc.) */
268 int function_depth;
270 /* States indicating how grokdeclarator() should handle declspecs marked
271 with __attribute__((deprecated)). An object declared as
272 __attribute__((deprecated)) suppresses warnings of uses of other
273 deprecated items. */
275 enum deprecated_states {
276 DEPRECATED_NORMAL,
277 DEPRECATED_SUPPRESS
280 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
282 /* Set by add_implicitly_declared_members() to keep those members from
283 being flagged as deprecated or reported as using deprecated
284 types. */
285 int adding_implicit_members = 0;
287 /* True if a declaration with an `extern' linkage specifier is being
288 processed. */
289 bool have_extern_spec;
292 /* For each binding contour we allocate a binding_level structure
293 which records the names defined in that contour.
294 Contours include:
295 0) the global one
296 1) one for each function definition,
297 where internal declarations of the parameters appear.
298 2) one for each compound statement,
299 to record its declarations.
301 The current meaning of a name can be found by searching the levels
302 from the current one out to the global one.
304 Off to the side, may be the class_binding_level. This exists only
305 to catch class-local declarations. It is otherwise nonexistent.
307 Also there may be binding levels that catch cleanups that must be
308 run when exceptions occur. Thus, to see whether a name is bound in
309 the current scope, it is not enough to look in the
310 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
311 instead. */
313 /* Note that the information in the `names' component of the global contour
314 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
316 struct cp_binding_level GTY(())
318 /* A chain of _DECL nodes for all variables, constants, functions,
319 and typedef types. These are in the reverse of the order
320 supplied. There may be OVERLOADs on this list, too, but they
321 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
322 tree names;
324 /* Count of elements in names chain. */
325 size_t names_size;
327 /* A chain of NAMESPACE_DECL nodes. */
328 tree namespaces;
330 /* An array of static functions and variables (for namespaces only) */
331 varray_type static_decls;
333 /* A chain of VTABLE_DECL nodes. */
334 tree vtables;
336 /* A list of structure, union and enum definitions, for looking up
337 tag names.
338 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
339 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
340 or ENUMERAL_TYPE node.
342 C++: the TREE_VALUE nodes can be simple types for
343 component_bindings. */
344 tree tags;
346 /* A list of USING_DECL nodes. */
347 tree usings;
349 /* A list of used namespaces. PURPOSE is the namespace,
350 VALUE the common ancestor with this binding_level's namespace. */
351 tree using_directives;
353 /* If this binding level is the binding level for a class, then
354 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
355 is the name of an entity bound in the class. The TREE_TYPE is
356 the DECL bound by this name in the class. */
357 tree class_shadowed;
359 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
360 is used for all binding levels. In addition the TREE_VALUE is the
361 IDENTIFIER_TYPE_VALUE before we entered the class. */
362 tree type_shadowed;
364 /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local
365 label in this scope. The TREE_PURPOSE is the previous value of
366 the IDENTIFIER_LABEL VALUE. */
367 tree shadowed_labels;
369 /* For each level (except not the global one),
370 a chain of BLOCK nodes for all the levels
371 that were entered and exited one level down. */
372 tree blocks;
374 /* The _TYPE node for this level, if parm_flag == 2. */
375 tree this_class;
377 /* The binding level which this one is contained in (inherits from). */
378 struct cp_binding_level *level_chain;
380 /* List of VAR_DECLS saved from a previous for statement.
381 These would be dead in ISO-conforming code, but might
382 be referenced in ARM-era code. These are stored in a
383 TREE_LIST; the TREE_VALUE is the actual declaration. */
384 tree dead_vars_from_for;
386 /* 1 for the level that holds the parameters of a function.
387 2 for the level that holds a class declaration. */
388 unsigned parm_flag : 2;
390 /* 1 means make a BLOCK for this level regardless of all else.
391 2 for temporary binding contours created by the compiler. */
392 unsigned keep : 2;
394 /* Nonzero if this level "doesn't exist" for tags. */
395 unsigned tag_transparent : 1;
397 /* Nonzero if this level can safely have additional
398 cleanup-needing variables added to it. */
399 unsigned more_cleanups_ok : 1;
400 unsigned have_cleanups : 1;
402 /* Nonzero if this scope is for storing the decls for template
403 parameters and generic decls; these decls will be discarded and
404 replaced with a TEMPLATE_DECL. */
405 unsigned template_parms_p : 1;
407 /* Nonzero if this scope corresponds to the `<>' in a
408 `template <>' clause. Whenever this flag is set,
409 TEMPLATE_PARMS_P will be set as well. */
410 unsigned template_spec_p : 1;
412 /* This is set for a namespace binding level. */
413 unsigned namespace_p : 1;
415 /* True if this level is that of a for-statement where we need to
416 worry about ambiguous (ARM or ISO) scope rules. */
417 unsigned is_for_scope : 1;
419 /* True if this level corresponds to a TRY block. Currently this
420 information is only available while building the tree structure. */
421 unsigned is_try_scope : 1;
423 /* True if this level corresponds to a CATCH block. Currently this
424 information is only available while building the tree structure. */
425 unsigned is_catch_scope : 1;
427 /* Three bits left for this word. */
429 /* Binding depth at which this level began. */
430 unsigned binding_depth;
433 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
435 /* The binding level currently in effect. */
437 #define current_binding_level \
438 (cfun && cp_function_chain->bindings \
439 ? cp_function_chain->bindings \
440 : scope_chain->bindings)
442 /* The binding level of the current class, if any. */
444 #define class_binding_level scope_chain->class_bindings
446 /* A chain of binding_level structures awaiting reuse. */
448 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
450 /* The outermost binding level, for names of file scope.
451 This is created when the compiler is started and exists
452 through the entire run. */
454 static GTY(()) struct cp_binding_level *global_binding_level;
456 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
458 static int keep_next_level_flag;
460 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
461 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
462 time the VAR_DECL was declared, the type was incomplete. */
464 static GTY(()) tree incomplete_vars;
466 #if defined(DEBUG_BINDING_LEVELS)
467 static int binding_depth = 0;
468 static int is_class_level = 0;
470 static void
471 indent (void)
473 register unsigned i;
475 for (i = 0; i < binding_depth*2; i++)
476 putc (' ', stderr);
478 #endif /* defined(DEBUG_BINDING_LEVELS) */
480 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
482 static void
483 push_binding_level (struct cp_binding_level *newlevel,
484 int tag_transparent,
485 int keep)
487 /* Add this level to the front of the chain (stack) of levels that
488 are active. */
489 memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
490 newlevel->level_chain = current_binding_level;
491 current_binding_level = newlevel;
492 newlevel->tag_transparent = tag_transparent;
493 newlevel->more_cleanups_ok = 1;
495 newlevel->keep = keep;
496 #if defined(DEBUG_BINDING_LEVELS)
497 newlevel->binding_depth = binding_depth;
498 indent ();
499 fprintf (stderr, "push %s level 0x%08x line %d\n",
500 (is_class_level) ? "class" : "block", newlevel, lineno);
501 is_class_level = 0;
502 binding_depth++;
503 #endif /* defined(DEBUG_BINDING_LEVELS) */
506 /* Find the innermost enclosing class scope, and reset
507 CLASS_BINDING_LEVEL appropriately. */
509 static void
510 find_class_binding_level (void)
512 struct cp_binding_level *level = current_binding_level;
514 while (level && level->parm_flag != 2)
515 level = level->level_chain;
516 if (level && level->parm_flag == 2)
517 class_binding_level = level;
518 else
519 class_binding_level = 0;
522 static void
523 pop_binding_level (void)
525 if (global_binding_level)
527 /* Cannot pop a level, if there are none left to pop. */
528 if (current_binding_level == global_binding_level)
529 abort ();
531 /* Pop the current level, and free the structure for reuse. */
532 #if defined(DEBUG_BINDING_LEVELS)
533 binding_depth--;
534 indent ();
535 fprintf (stderr, "pop %s level 0x%08x line %d\n",
536 (is_class_level) ? "class" : "block",
537 current_binding_level, lineno);
538 if (is_class_level != (current_binding_level == class_binding_level))
540 indent ();
541 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
543 is_class_level = 0;
544 #endif /* defined(DEBUG_BINDING_LEVELS) */
546 register struct cp_binding_level *level = current_binding_level;
547 current_binding_level = current_binding_level->level_chain;
548 level->level_chain = free_binding_level;
549 #if 0 /* defined(DEBUG_BINDING_LEVELS) */
550 if (level->binding_depth != binding_depth)
551 abort ();
552 #endif /* defined(DEBUG_BINDING_LEVELS) */
553 free_binding_level = level;
554 find_class_binding_level ();
558 static void
559 suspend_binding_level (void)
561 if (class_binding_level)
562 current_binding_level = class_binding_level;
564 if (global_binding_level)
566 /* Cannot suspend a level, if there are none left to suspend. */
567 if (current_binding_level == global_binding_level)
568 abort ();
570 /* Suspend the current level. */
571 #if defined(DEBUG_BINDING_LEVELS)
572 binding_depth--;
573 indent ();
574 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
575 (is_class_level) ? "class" : "block",
576 current_binding_level, lineno);
577 if (is_class_level != (current_binding_level == class_binding_level))
579 indent ();
580 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
582 is_class_level = 0;
583 #endif /* defined(DEBUG_BINDING_LEVELS) */
584 current_binding_level = current_binding_level->level_chain;
585 find_class_binding_level ();
588 static void
589 resume_binding_level (struct cp_binding_level* b)
591 /* Resuming binding levels is meant only for namespaces,
592 and those cannot nest into classes. */
593 my_friendly_assert(!class_binding_level, 386);
594 /* Also, resuming a non-directly nested namespace is a no-no. */
595 my_friendly_assert(b->level_chain == current_binding_level, 386);
596 current_binding_level = b;
597 #if defined(DEBUG_BINDING_LEVELS)
598 b->binding_depth = binding_depth;
599 indent ();
600 fprintf (stderr, "resume %s level 0x%08x line %d\n",
601 (is_class_level) ? "class" : "block", b, lineno);
602 is_class_level = 0;
603 binding_depth++;
604 #endif /* defined(DEBUG_BINDING_LEVELS) */
607 /* Create a new `struct cp_binding_level'. */
609 static
610 struct cp_binding_level *
611 make_binding_level (void)
613 /* NOSTRICT */
614 return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
617 /* Nonzero if we are currently in the global binding level. */
620 global_bindings_p (void)
622 return current_binding_level == global_binding_level;
625 /* Return the innermost binding level that is not for a class scope. */
627 static struct cp_binding_level *
628 innermost_nonclass_level (void)
630 struct cp_binding_level *b;
632 b = current_binding_level;
633 while (b->parm_flag == 2)
634 b = b->level_chain;
636 return b;
639 /* Nonzero if we are currently in a toplevel binding level. This
640 means either the global binding level or a namespace in a toplevel
641 binding level. Since there are no non-toplevel namespace levels,
642 this really means any namespace or template parameter level. We
643 also include a class whose context is toplevel. */
646 toplevel_bindings_p (void)
648 struct cp_binding_level *b = innermost_nonclass_level ();
650 return b->namespace_p || b->template_parms_p;
653 /* Nonzero if this is a namespace scope, or if we are defining a class
654 which is itself at namespace scope, or whose enclosing class is
655 such a class, etc. */
658 namespace_bindings_p (void)
660 struct cp_binding_level *b = innermost_nonclass_level ();
662 return b->namespace_p;
665 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
666 unconditionally. Otherwise, use the normal logic to decide whether
667 or not to create a BLOCK. */
669 void
670 keep_next_level (int keep)
672 keep_next_level_flag = keep;
675 /* Nonzero if the current level needs to have a BLOCK made. */
678 kept_level_p (void)
680 return (current_binding_level->blocks != NULL_TREE
681 || current_binding_level->keep
682 || current_binding_level->names != NULL_TREE
683 || (current_binding_level->tags != NULL_TREE
684 && !current_binding_level->tag_transparent));
687 static void
688 declare_namespace_level (void)
690 current_binding_level->namespace_p = 1;
693 /* Returns nonzero if this scope was created to store template
694 parameters. */
697 template_parm_scope_p (void)
699 return current_binding_level->template_parms_p;
702 /* Returns the kind of template specialization we are currently
703 processing, given that it's declaration contained N_CLASS_SCOPES
704 explicit scope qualifications. */
706 tmpl_spec_kind
707 current_tmpl_spec_kind (int n_class_scopes)
709 int n_template_parm_scopes = 0;
710 int seen_specialization_p = 0;
711 int innermost_specialization_p = 0;
712 struct cp_binding_level *b;
714 /* Scan through the template parameter scopes. */
715 for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
717 /* If we see a specialization scope inside a parameter scope,
718 then something is wrong. That corresponds to a declaration
719 like:
721 template <class T> template <> ...
723 which is always invalid since [temp.expl.spec] forbids the
724 specialization of a class member template if the enclosing
725 class templates are not explicitly specialized as well. */
726 if (b->template_spec_p)
728 if (n_template_parm_scopes == 0)
729 innermost_specialization_p = 1;
730 else
731 seen_specialization_p = 1;
733 else if (seen_specialization_p == 1)
734 return tsk_invalid_member_spec;
736 ++n_template_parm_scopes;
739 /* Handle explicit instantiations. */
740 if (processing_explicit_instantiation)
742 if (n_template_parm_scopes != 0)
743 /* We've seen a template parameter list during an explicit
744 instantiation. For example:
746 template <class T> template void f(int);
748 This is erroneous. */
749 return tsk_invalid_expl_inst;
750 else
751 return tsk_expl_inst;
754 if (n_template_parm_scopes < n_class_scopes)
755 /* We've not seen enough template headers to match all the
756 specialized classes present. For example:
758 template <class T> void R<T>::S<T>::f(int);
760 This is invalid; there needs to be one set of template
761 parameters for each class. */
762 return tsk_insufficient_parms;
763 else if (n_template_parm_scopes == n_class_scopes)
764 /* We're processing a non-template declaration (even though it may
765 be a member of a template class.) For example:
767 template <class T> void S<T>::f(int);
769 The `class T' maches the `S<T>', leaving no template headers
770 corresponding to the `f'. */
771 return tsk_none;
772 else if (n_template_parm_scopes > n_class_scopes + 1)
773 /* We've got too many template headers. For example:
775 template <> template <class T> void f (T);
777 There need to be more enclosing classes. */
778 return tsk_excessive_parms;
779 else
780 /* This must be a template. It's of the form:
782 template <class T> template <class U> void S<T>::f(U);
784 This is a specialization if the innermost level was a
785 specialization; otherwise it's just a definition of the
786 template. */
787 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
790 void
791 set_class_shadows (tree shadows)
793 class_binding_level->class_shadowed = shadows;
796 /* Enter a new binding level.
797 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
798 not for that of tags. */
800 void
801 pushlevel (int tag_transparent)
803 struct cp_binding_level *newlevel;
805 if (cfun && !doing_semantic_analysis_p ())
806 return;
808 /* Reuse or create a struct for this binding level. */
809 #if defined(DEBUG_BINDING_LEVELS)
810 if (0)
811 #else /* !defined(DEBUG_BINDING_LEVELS) */
812 if (free_binding_level)
813 #endif /* !defined(DEBUG_BINDING_LEVELS) */
815 newlevel = free_binding_level;
816 free_binding_level = free_binding_level->level_chain;
818 else
819 newlevel = make_binding_level ();
821 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
822 keep_next_level_flag = 0;
825 /* We're defining an object of type TYPE. If it needs a cleanup, but
826 we're not allowed to add any more objects with cleanups to the current
827 scope, create a new binding level. */
829 void
830 maybe_push_cleanup_level (tree type)
832 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
833 && current_binding_level->more_cleanups_ok == 0)
835 keep_next_level (2);
836 pushlevel (1);
837 clear_last_expr ();
838 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
842 /* Enter a new scope. The KIND indicates what kind of scope is being
843 created. */
845 void
846 begin_scope (scope_kind sk)
848 pushlevel (0);
850 switch (sk)
852 case sk_block:
853 break;
855 case sk_try:
856 current_binding_level->is_try_scope = 1;
857 break;
859 case sk_catch:
860 current_binding_level->is_catch_scope = 1;
861 break;
863 case sk_for:
864 current_binding_level->is_for_scope = 1;
865 break;
867 case sk_template_spec:
868 current_binding_level->template_spec_p = 1;
869 /* Fall through. */
871 case sk_template_parms:
872 current_binding_level->template_parms_p = 1;
873 break;
875 default:
876 abort ();
880 /* Exit the current scope. */
882 void
883 finish_scope (void)
885 poplevel (0, 0, 0);
888 /* For a binding between a name and an entity at a block scope,
889 this is the `struct cp_binding_level' for the block. */
890 #define BINDING_LEVEL(NODE) ((NODE)->scope.level)
892 /* A free list of cxx_binding nodes, connected by their
893 TREE_CHAINs. */
895 static GTY((deletable (""))) cxx_binding *free_bindings;
897 /* Make DECL the innermost binding for ID. The LEVEL is the binding
898 level at which this declaration is being bound. */
900 static void
901 push_binding (tree id, tree decl, struct cp_binding_level* level)
903 cxx_binding *binding;
905 if (free_bindings)
907 binding = free_bindings;
908 free_bindings = binding->previous;
910 else
911 binding = cxx_binding_make ();
913 /* Now, fill in the binding information. */
914 binding->previous = IDENTIFIER_BINDING (id);
915 BINDING_VALUE (binding) = decl;
916 BINDING_TYPE (binding) = NULL_TREE;
917 BINDING_LEVEL (binding) = level;
918 INHERITED_VALUE_BINDING_P (binding) = 0;
919 LOCAL_BINDING_P (binding) = (level != class_binding_level);
920 BINDING_HAS_LEVEL_P (binding) = 1;
922 /* And put it on the front of the list of bindings for ID. */
923 IDENTIFIER_BINDING (id) = binding;
926 /* ID is already bound in the current scope. But, DECL is an
927 additional binding for ID in the same scope. This is the `struct
928 stat' hack whereby a non-typedef class-name or enum-name can be
929 bound at the same level as some other kind of entity. It's the
930 responsibility of the caller to check that inserting this name is
931 valid here. Returns nonzero if the new binding was successful. */
932 static int
933 add_binding (tree id, tree decl)
935 cxx_binding *binding = IDENTIFIER_BINDING (id);
936 int ok = 1;
938 timevar_push (TV_NAME_LOOKUP);
939 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
940 /* The new name is the type name. */
941 BINDING_TYPE (binding) = decl;
942 else if (!BINDING_VALUE (binding))
943 /* This situation arises when push_class_level_binding moves an
944 inherited type-binding out of the way to make room for a new
945 value binding. */
946 BINDING_VALUE (binding) = decl;
947 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
948 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
950 /* The old binding was a type name. It was placed in
951 BINDING_VALUE because it was thought, at the point it was
952 declared, to be the only entity with such a name. Move the
953 type name into the type slot; it is now hidden by the new
954 binding. */
955 BINDING_TYPE (binding) = BINDING_VALUE (binding);
956 BINDING_VALUE (binding) = decl;
957 INHERITED_VALUE_BINDING_P (binding) = 0;
959 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
960 && TREE_CODE (decl) == TYPE_DECL
961 && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
962 && (same_type_p (TREE_TYPE (decl),
963 TREE_TYPE (BINDING_VALUE (binding)))
964 /* If either type involves template parameters, we must
965 wait until instantiation. */
966 || uses_template_parms (TREE_TYPE (decl))
967 || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
968 /* We have two typedef-names, both naming the same type to have
969 the same name. This is OK because of:
971 [dcl.typedef]
973 In a given scope, a typedef specifier can be used to redefine
974 the name of any type declared in that scope to refer to the
975 type to which it already refers. */
976 ok = 0;
977 /* There can be two block-scope declarations of the same variable,
978 so long as they are `extern' declarations. */
979 else if (TREE_CODE (decl) == VAR_DECL
980 && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
981 && DECL_EXTERNAL (decl)
982 && DECL_EXTERNAL (BINDING_VALUE (binding)))
984 duplicate_decls (decl, BINDING_VALUE (binding));
985 ok = 0;
987 else
989 error ("declaration of `%#D'", decl);
990 cp_error_at ("conflicts with previous declaration `%#D'",
991 BINDING_VALUE (binding));
992 ok = 0;
995 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
998 /* Add DECL to the list of things declared in B. */
1000 static void
1001 add_decl_to_level (tree decl,
1002 struct cp_binding_level* b)
1004 if (TREE_CODE (decl) == NAMESPACE_DECL
1005 && !DECL_NAMESPACE_ALIAS (decl))
1007 TREE_CHAIN (decl) = b->namespaces;
1008 b->namespaces = decl;
1010 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
1012 TREE_CHAIN (decl) = b->vtables;
1013 b->vtables = decl;
1015 else
1017 /* We build up the list in reverse order, and reverse it later if
1018 necessary. */
1019 TREE_CHAIN (decl) = b->names;
1020 b->names = decl;
1021 b->names_size++;
1023 /* If appropriate, add decl to separate list of statics */
1024 if (b->namespace_p)
1025 if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
1026 || (TREE_CODE (decl) == FUNCTION_DECL
1027 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
1028 VARRAY_PUSH_TREE (b->static_decls, decl);
1032 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1033 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1034 doesn't really belong to this binding level, that it got here
1035 through a using-declaration. */
1037 void
1038 push_local_binding (tree id, tree decl, int flags)
1040 struct cp_binding_level *b;
1042 /* Skip over any local classes. This makes sense if we call
1043 push_local_binding with a friend decl of a local class. */
1044 b = current_binding_level;
1045 while (b->parm_flag == 2)
1046 b = b->level_chain;
1048 if (lookup_name_current_level (id))
1050 /* Supplement the existing binding. */
1051 if (!add_binding (id, decl))
1052 /* It didn't work. Something else must be bound at this
1053 level. Do not add DECL to the list of things to pop
1054 later. */
1055 return;
1057 else
1058 /* Create a new binding. */
1059 push_binding (id, decl, b);
1061 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1062 /* We must put the OVERLOAD into a TREE_LIST since the
1063 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1064 decls that got here through a using-declaration. */
1065 decl = build_tree_list (NULL_TREE, decl);
1067 /* And put DECL on the list of things declared by the current
1068 binding level. */
1069 add_decl_to_level (decl, b);
1072 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1073 binding was successful. */
1076 push_class_binding (tree id, tree decl)
1078 int result = 1;
1079 cxx_binding *binding = IDENTIFIER_BINDING (id);
1080 tree context;
1082 timevar_push (TV_NAME_LOOKUP);
1083 /* Note that we declared this value so that we can issue an error if
1084 this is an invalid redeclaration of a name already used for some
1085 other purpose. */
1086 note_name_declared_in_class (id, decl);
1088 if (binding && BINDING_LEVEL (binding) == class_binding_level)
1089 /* Supplement the existing binding. */
1090 result = add_binding (id, decl);
1091 else
1092 /* Create a new binding. */
1093 push_binding (id, decl, class_binding_level);
1095 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1096 class-level declaration. Note that we do not use DECL here
1097 because of the possibility of the `struct stat' hack; if DECL is
1098 a class-name or enum-name we might prefer a field-name, or some
1099 such. */
1100 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1102 /* If this is a binding from a base class, mark it as such. */
1103 binding = IDENTIFIER_BINDING (id);
1104 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1106 if (TREE_CODE (decl) == OVERLOAD)
1107 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1108 else
1110 my_friendly_assert (DECL_P (decl), 0);
1111 context = context_for_name_lookup (decl);
1114 if (is_properly_derived_from (current_class_type, context))
1115 INHERITED_VALUE_BINDING_P (binding) = 1;
1116 else
1117 INHERITED_VALUE_BINDING_P (binding) = 0;
1119 else if (BINDING_VALUE (binding) == decl)
1120 /* We only encounter a TREE_LIST when push_class_decls detects an
1121 ambiguity. Such an ambiguity can be overridden by a definition
1122 in this class. */
1123 INHERITED_VALUE_BINDING_P (binding) = 1;
1125 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1128 /* Remove the binding for DECL which should be the innermost binding
1129 for ID. */
1131 static void
1132 pop_binding (tree id, tree decl)
1134 cxx_binding *binding;
1136 if (id == NULL_TREE)
1137 /* It's easiest to write the loops that call this function without
1138 checking whether or not the entities involved have names. We
1139 get here for such an entity. */
1140 return;
1142 /* Get the innermost binding for ID. */
1143 binding = IDENTIFIER_BINDING (id);
1145 /* The name should be bound. */
1146 my_friendly_assert (binding != NULL, 0);
1148 /* The DECL will be either the ordinary binding or the type
1149 binding for this identifier. Remove that binding. */
1150 if (BINDING_VALUE (binding) == decl)
1151 BINDING_VALUE (binding) = NULL_TREE;
1152 else if (BINDING_TYPE (binding) == decl)
1153 BINDING_TYPE (binding) = NULL_TREE;
1154 else
1155 abort ();
1157 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1159 /* We're completely done with the innermost binding for this
1160 identifier. Unhook it from the list of bindings. */
1161 IDENTIFIER_BINDING (id) = binding->previous;
1163 /* Add it to the free list. */
1164 binding->previous = free_bindings;
1165 free_bindings = binding;
1167 /* Clear the BINDING_LEVEL so the garbage collector doesn't walk
1168 it. */
1169 BINDING_LEVEL (binding) = NULL;
1173 /* When a label goes out of scope, check to see if that label was used
1174 in a valid manner, and issue any appropriate warnings or errors. */
1176 static void
1177 pop_label (tree label, tree old_value)
1179 if (!processing_template_decl && doing_semantic_analysis_p ())
1181 if (DECL_INITIAL (label) == NULL_TREE)
1183 cp_error_at ("label `%D' used but not defined", label);
1184 /* Avoid crashing later. */
1185 define_label (input_filename, 1, DECL_NAME (label));
1187 else if (warn_unused_label && !TREE_USED (label))
1188 cp_warning_at ("label `%D' defined but not used", label);
1191 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1194 /* At the end of a function, all labels declared within the function
1195 go out of scope. BLOCK is the top-level block for the
1196 function. */
1198 static void
1199 pop_labels (tree block)
1201 struct named_label_list *link;
1203 /* Clear out the definitions of all label names, since their scopes
1204 end here. */
1205 for (link = named_labels; link; link = link->next)
1207 pop_label (link->label_decl, link->old_value);
1208 /* Put the labels into the "variables" of the top-level block,
1209 so debugger can see them. */
1210 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1211 BLOCK_VARS (block) = link->label_decl;
1214 named_labels = NULL;
1217 /* Exit a binding level.
1218 Pop the level off, and restore the state of the identifier-decl mappings
1219 that were in effect when this level was entered.
1221 If KEEP == 1, this level had explicit declarations, so
1222 and create a "block" (a BLOCK node) for the level
1223 to record its declarations and subblocks for symbol table output.
1225 If FUNCTIONBODY is nonzero, this level is the body of a function,
1226 so create a block as if KEEP were set and also clear out all
1227 label names.
1229 If REVERSE is nonzero, reverse the order of decls before putting
1230 them into the BLOCK. */
1232 tree
1233 poplevel (int keep, int reverse, int functionbody)
1235 register tree link;
1236 /* The chain of decls was accumulated in reverse order.
1237 Put it into forward order, just for cleanliness. */
1238 tree decls;
1239 int tmp = functionbody;
1240 int real_functionbody;
1241 tree tags;
1242 tree subblocks;
1243 tree block = NULL_TREE;
1244 tree decl;
1245 int leaving_for_scope;
1247 timevar_push (TV_NAME_LOOKUP);
1248 if (cfun && !doing_semantic_analysis_p ())
1249 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1251 my_friendly_assert (current_binding_level->parm_flag != 2,
1252 19990916);
1254 real_functionbody = (current_binding_level->keep == 2
1255 ? ((functionbody = 0), tmp) : functionbody);
1256 tags = functionbody >= 0 ? current_binding_level->tags : 0;
1257 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1259 my_friendly_assert (!current_binding_level->class_shadowed,
1260 19990414);
1262 /* We used to use KEEP == 2 to indicate that the new block should go
1263 at the beginning of the list of blocks at this binding level,
1264 rather than the end. This hack is no longer used. */
1265 my_friendly_assert (keep == 0 || keep == 1, 0);
1267 if (current_binding_level->keep == 1)
1268 keep = 1;
1270 /* Any uses of undefined labels, and any defined labels, now operate
1271 under constraints of next binding contour. */
1272 if (cfun && !functionbody)
1274 struct cp_binding_level *level_chain;
1275 level_chain = current_binding_level->level_chain;
1276 if (level_chain)
1278 struct named_label_use_list *uses;
1279 struct named_label_list *labels;
1280 for (labels = named_labels; labels; labels = labels->next)
1281 if (labels->binding_level == current_binding_level)
1283 tree decl;
1284 if (current_binding_level->is_try_scope)
1285 labels->in_try_scope = 1;
1286 if (current_binding_level->is_catch_scope)
1287 labels->in_catch_scope = 1;
1288 for (decl = labels->names_in_scope; decl;
1289 decl = TREE_CHAIN (decl))
1290 if (decl_jump_unsafe (decl))
1291 labels->bad_decls = tree_cons (NULL_TREE, decl,
1292 labels->bad_decls);
1293 labels->binding_level = level_chain;
1294 labels->names_in_scope = level_chain->names;
1297 for (uses = named_label_uses; uses; uses = uses->next)
1298 if (uses->binding_level == current_binding_level)
1300 uses->binding_level = level_chain;
1301 uses->names_in_scope = level_chain->names;
1306 /* Get the decls in the order they were written.
1307 Usually current_binding_level->names is in reverse order.
1308 But parameter decls were previously put in forward order. */
1310 if (reverse)
1311 current_binding_level->names
1312 = decls = nreverse (current_binding_level->names);
1313 else
1314 decls = current_binding_level->names;
1316 /* Output any nested inline functions within this block
1317 if they weren't already output. */
1318 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1319 if (TREE_CODE (decl) == FUNCTION_DECL
1320 && ! TREE_ASM_WRITTEN (decl)
1321 && DECL_INITIAL (decl) != NULL_TREE
1322 && TREE_ADDRESSABLE (decl)
1323 && decl_function_context (decl) == current_function_decl)
1325 /* If this decl was copied from a file-scope decl
1326 on account of a block-scope extern decl,
1327 propagate TREE_ADDRESSABLE to the file-scope decl. */
1328 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1329 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1330 else
1332 push_function_context ();
1333 output_inline_function (decl);
1334 pop_function_context ();
1338 /* When not in function-at-a-time mode, expand_end_bindings will
1339 warn about unused variables. But, in function-at-a-time mode
1340 expand_end_bindings is not passed the list of variables in the
1341 current scope, and therefore no warning is emitted. So, we
1342 explicitly warn here. */
1343 if (!processing_template_decl)
1344 warn_about_unused_variables (getdecls ());
1346 /* If there were any declarations or structure tags in that level,
1347 or if this level is a function body,
1348 create a BLOCK to record them for the life of this function. */
1349 block = NULL_TREE;
1350 if (keep == 1 || functionbody)
1351 block = make_node (BLOCK);
1352 if (block != NULL_TREE)
1354 BLOCK_VARS (block) = decls;
1355 BLOCK_SUBBLOCKS (block) = subblocks;
1358 /* In each subblock, record that this is its superior. */
1359 if (keep >= 0)
1360 for (link = subblocks; link; link = TREE_CHAIN (link))
1361 BLOCK_SUPERCONTEXT (link) = block;
1363 /* We still support the old for-scope rules, whereby the variables
1364 in a for-init statement were in scope after the for-statement
1365 ended. We only use the new rules if flag_new_for_scope is
1366 nonzero. */
1367 leaving_for_scope
1368 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1370 /* Remove declarations for all the DECLs in this level. */
1371 for (link = decls; link; link = TREE_CHAIN (link))
1373 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1374 && DECL_NAME (link))
1376 cxx_binding *outer_binding
1377 = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
1378 tree ns_binding;
1380 if (!outer_binding)
1381 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1382 else
1383 ns_binding = NULL_TREE;
1385 if (outer_binding
1386 && (BINDING_LEVEL (outer_binding)
1387 == current_binding_level->level_chain))
1388 /* We have something like:
1390 int i;
1391 for (int i; ;);
1393 and we are leaving the `for' scope. There's no reason to
1394 keep the binding of the inner `i' in this case. */
1395 pop_binding (DECL_NAME (link), link);
1396 else if ((outer_binding
1397 && (TREE_CODE (BINDING_VALUE (outer_binding))
1398 == TYPE_DECL))
1399 || (ns_binding
1400 && TREE_CODE (ns_binding) == TYPE_DECL))
1401 /* Here, we have something like:
1403 typedef int I;
1405 void f () {
1406 for (int I; ;);
1409 We must pop the for-scope binding so we know what's a
1410 type and what isn't. */
1411 pop_binding (DECL_NAME (link), link);
1412 else
1414 /* Mark this VAR_DECL as dead so that we can tell we left it
1415 there only for backward compatibility. */
1416 DECL_DEAD_FOR_LOCAL (link) = 1;
1418 /* Keep track of what should of have happenned when we
1419 popped the binding. */
1420 if (outer_binding && BINDING_VALUE (outer_binding))
1421 DECL_SHADOWED_FOR_VAR (link)
1422 = BINDING_VALUE (outer_binding);
1424 /* Add it to the list of dead variables in the next
1425 outermost binding to that we can remove these when we
1426 leave that binding. */
1427 current_binding_level->level_chain->dead_vars_from_for
1428 = tree_cons (NULL_TREE, link,
1429 current_binding_level->level_chain->
1430 dead_vars_from_for);
1432 /* Although we don't pop the cxx_binding, we do clear
1433 its BINDING_LEVEL since the level is going away now. */
1434 BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1435 = 0;
1438 else
1440 /* Remove the binding. */
1441 decl = link;
1442 if (TREE_CODE (decl) == TREE_LIST)
1443 decl = TREE_VALUE (decl);
1444 if (DECL_P (decl))
1445 pop_binding (DECL_NAME (decl), decl);
1446 else if (TREE_CODE (decl) == OVERLOAD)
1447 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1448 else
1449 abort ();
1453 /* Remove declarations for any `for' variables from inner scopes
1454 that we kept around. */
1455 for (link = current_binding_level->dead_vars_from_for;
1456 link; link = TREE_CHAIN (link))
1457 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1459 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1460 for (link = current_binding_level->type_shadowed;
1461 link; link = TREE_CHAIN (link))
1462 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1464 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1465 for (link = current_binding_level->shadowed_labels;
1466 link;
1467 link = TREE_CHAIN (link))
1468 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1470 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1471 list if a `using' declaration put them there. The debugging
1472 back-ends won't understand OVERLOAD, so we remove them here.
1473 Because the BLOCK_VARS are (temporarily) shared with
1474 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1475 popped all the bindings. */
1476 if (block)
1478 tree* d;
1480 for (d = &BLOCK_VARS (block); *d; )
1482 if (TREE_CODE (*d) == TREE_LIST)
1483 *d = TREE_CHAIN (*d);
1484 else
1485 d = &TREE_CHAIN (*d);
1489 /* If the level being exited is the top level of a function,
1490 check over all the labels. */
1491 if (functionbody)
1493 /* Since this is the top level block of a function, the vars are
1494 the function's parameters. Don't leave them in the BLOCK
1495 because they are found in the FUNCTION_DECL instead. */
1496 BLOCK_VARS (block) = 0;
1497 pop_labels (block);
1500 tmp = current_binding_level->keep;
1502 pop_binding_level ();
1503 if (functionbody)
1504 DECL_INITIAL (current_function_decl) = block;
1505 else if (block)
1506 current_binding_level->blocks
1507 = chainon (current_binding_level->blocks, block);
1509 /* If we did not make a block for the level just exited,
1510 any blocks made for inner levels
1511 (since they cannot be recorded as subblocks in that level)
1512 must be carried forward so they will later become subblocks
1513 of something else. */
1514 else if (subblocks)
1515 current_binding_level->blocks
1516 = chainon (current_binding_level->blocks, subblocks);
1518 /* Each and every BLOCK node created here in `poplevel' is important
1519 (e.g. for proper debugging information) so if we created one
1520 earlier, mark it as "used". */
1521 if (block)
1522 TREE_USED (block) = 1;
1524 /* Take care of compiler's internal binding structures. */
1525 if (tmp == 2)
1527 tree scope_stmts;
1529 scope_stmts
1530 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1531 if (block)
1533 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1534 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1537 block = poplevel (keep, reverse, functionbody);
1540 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1543 /* Delete the node BLOCK from the current binding level.
1544 This is used for the block inside a stmt expr ({...})
1545 so that the block can be reinserted where appropriate. */
1547 void
1548 delete_block (tree block)
1550 tree t;
1551 if (current_binding_level->blocks == block)
1552 current_binding_level->blocks = TREE_CHAIN (block);
1553 for (t = current_binding_level->blocks; t;)
1555 if (TREE_CHAIN (t) == block)
1556 TREE_CHAIN (t) = TREE_CHAIN (block);
1557 else
1558 t = TREE_CHAIN (t);
1560 TREE_CHAIN (block) = NULL_TREE;
1561 /* Clear TREE_USED which is always set by poplevel.
1562 The flag is set again if insert_block is called. */
1563 TREE_USED (block) = 0;
1566 /* Insert BLOCK at the end of the list of subblocks of the
1567 current binding level. This is used when a BIND_EXPR is expanded,
1568 to handle the BLOCK node inside the BIND_EXPR. */
1570 void
1571 insert_block (tree block)
1573 TREE_USED (block) = 1;
1574 current_binding_level->blocks
1575 = chainon (current_binding_level->blocks, block);
1578 /* Set the BLOCK node for the innermost scope
1579 (the one we are currently in). */
1581 void
1582 set_block (tree block ATTRIBUTE_UNUSED )
1584 /* The RTL expansion machinery requires us to provide this callback,
1585 but it is not applicable in function-at-a-time mode. */
1586 my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1589 /* Do a pushlevel for class declarations. */
1591 void
1592 pushlevel_class (void)
1594 register struct cp_binding_level *newlevel;
1596 /* Reuse or create a struct for this binding level. */
1597 #if defined(DEBUG_BINDING_LEVELS)
1598 if (0)
1599 #else /* !defined(DEBUG_BINDING_LEVELS) */
1600 if (free_binding_level)
1601 #endif /* !defined(DEBUG_BINDING_LEVELS) */
1603 newlevel = free_binding_level;
1604 free_binding_level = free_binding_level->level_chain;
1606 else
1607 newlevel = make_binding_level ();
1609 #if defined(DEBUG_BINDING_LEVELS)
1610 is_class_level = 1;
1611 #endif /* defined(DEBUG_BINDING_LEVELS) */
1613 push_binding_level (newlevel, 0, 0);
1615 class_binding_level = current_binding_level;
1616 class_binding_level->parm_flag = 2;
1617 class_binding_level->this_class = current_class_type;
1620 /* ...and a poplevel for class declarations. */
1622 void
1623 poplevel_class (void)
1625 register struct cp_binding_level *level = class_binding_level;
1626 tree shadowed;
1628 timevar_push (TV_NAME_LOOKUP);
1629 my_friendly_assert (level != 0, 354);
1631 /* If we're leaving a toplevel class, don't bother to do the setting
1632 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1633 shouldn't even be used when current_class_type isn't set, and second,
1634 if we don't touch it here, we're able to use the cache effect if the
1635 next time we're entering a class scope, it is the same class. */
1636 if (current_class_depth != 1)
1638 struct cp_binding_level* b;
1640 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1641 for (shadowed = level->class_shadowed;
1642 shadowed;
1643 shadowed = TREE_CHAIN (shadowed))
1644 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1646 /* Find the next enclosing class, and recreate
1647 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1648 b = level->level_chain;
1649 while (b && b->parm_flag != 2)
1650 b = b->level_chain;
1652 if (b)
1653 for (shadowed = b->class_shadowed;
1654 shadowed;
1655 shadowed = TREE_CHAIN (shadowed))
1657 cxx_binding *binding;
1659 binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1660 while (binding && BINDING_LEVEL (binding) != b)
1661 binding = binding->previous;
1663 if (binding)
1664 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1665 = BINDING_VALUE (binding);
1668 else
1669 /* Remember to save what IDENTIFIER's were bound in this scope so we
1670 can recover from cache misses. */
1672 previous_class_type = current_class_type;
1673 previous_class_values = class_binding_level->class_shadowed;
1675 for (shadowed = level->type_shadowed;
1676 shadowed;
1677 shadowed = TREE_CHAIN (shadowed))
1678 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1680 /* Remove the bindings for all of the class-level declarations. */
1681 for (shadowed = level->class_shadowed;
1682 shadowed;
1683 shadowed = TREE_CHAIN (shadowed))
1684 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1686 /* Now, pop out of the binding level which we created up in the
1687 `pushlevel_class' routine. */
1688 #if defined(DEBUG_BINDING_LEVELS)
1689 is_class_level = 1;
1690 #endif /* defined(DEBUG_BINDING_LEVELS) */
1692 pop_binding_level ();
1693 timevar_pop (TV_NAME_LOOKUP);
1696 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1697 for any names in enclosing classes. */
1699 void
1700 clear_identifier_class_values (void)
1702 tree t;
1704 if (!class_binding_level)
1705 return;
1707 for (t = class_binding_level->class_shadowed;
1709 t = TREE_CHAIN (t))
1710 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1713 /* Returns nonzero if T is a virtual function table. */
1716 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1718 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1721 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1722 functions. */
1725 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1727 return (TREE_CODE (t) == TYPE_DECL
1728 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1729 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1732 /* Return the declarations that are members of the namespace NS. */
1734 tree
1735 cp_namespace_decls (tree ns)
1737 return NAMESPACE_LEVEL (ns)->names;
1740 struct walk_globals_data {
1741 walk_globals_pred p;
1742 walk_globals_fn f;
1743 void *data;
1746 /* Walk the vtable declarations in NAMESPACE. Whenever one is found
1747 for which P returns nonzero, call F with its address. If any call
1748 to F returns a nonzero value, return a nonzero value. */
1750 static int
1751 walk_vtables_r (tree namespace, void* data)
1753 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1754 walk_globals_fn f = wgd->f;
1755 void *d = wgd->data;
1756 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1757 int result = 0;
1759 for (; decl ; decl = TREE_CHAIN (decl))
1760 result |= (*f) (&decl, d);
1762 return result;
1765 /* Walk the vtable declarations. Whenever one is found for which P
1766 returns nonzero, call F with its address. If any call to F
1767 returns a nonzero value, return a nonzero value. */
1768 bool
1769 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1771 struct walk_globals_data wgd;
1772 wgd.p = p;
1773 wgd.f = f;
1774 wgd.data = data;
1776 return walk_namespaces (walk_vtables_r, &wgd);
1779 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1780 itself, calling F for each. The DATA is passed to F as well. */
1782 static int
1783 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1785 int result = 0;
1786 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
1788 result |= (*f) (namespace, data);
1790 for (; current; current = TREE_CHAIN (current))
1791 result |= walk_namespaces_r (current, f, data);
1793 return result;
1796 /* Walk all the namespaces, calling F for each. The DATA is passed to
1797 F as well. */
1800 walk_namespaces (walk_namespaces_fn f, void* data)
1802 return walk_namespaces_r (global_namespace, f, data);
1805 /* Walk the global declarations in NAMESPACE. Whenever one is found
1806 for which P returns nonzero, call F with its address. If any call
1807 to F returns a nonzero value, return a nonzero value. */
1809 static int
1810 walk_globals_r (tree namespace, void* data)
1812 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1813 walk_globals_pred p = wgd->p;
1814 walk_globals_fn f = wgd->f;
1815 void *d = wgd->data;
1816 tree *t;
1817 int result = 0;
1819 t = &NAMESPACE_LEVEL (namespace)->names;
1821 while (*t)
1823 tree glbl = *t;
1825 if ((*p) (glbl, d))
1826 result |= (*f) (t, d);
1828 /* If F changed *T, then *T still points at the next item to
1829 examine. */
1830 if (*t == glbl)
1831 t = &TREE_CHAIN (*t);
1834 return result;
1837 /* Walk the global declarations. Whenever one is found for which P
1838 returns true, call F with its address. If any call to F
1839 returns true, return true. */
1841 bool
1842 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1844 struct walk_globals_data wgd;
1845 wgd.p = p;
1846 wgd.f = f;
1847 wgd.data = data;
1849 return walk_namespaces (walk_globals_r, &wgd);
1852 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1853 DATA is non-NULL, this is the last time we will call
1854 wrapup_global_declarations for this NAMESPACE. */
1857 wrapup_globals_for_namespace (tree namespace, void* data)
1859 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
1860 varray_type statics = level->static_decls;
1861 tree *vec = &VARRAY_TREE (statics, 0);
1862 int len = VARRAY_ACTIVE_SIZE (statics);
1863 int last_time = (data != 0);
1865 if (last_time)
1867 check_global_declarations (vec, len);
1868 return 0;
1871 /* Write out any globals that need to be output. */
1872 return wrapup_global_declarations (vec, len);
1876 /* For debugging. */
1877 static int no_print_functions = 0;
1878 static int no_print_builtins = 0;
1880 void
1881 print_binding_level (struct cp_binding_level* lvl)
1883 tree t;
1884 int i = 0, len;
1885 fprintf (stderr, " blocks=");
1886 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1887 if (lvl->tag_transparent)
1888 fprintf (stderr, " tag-transparent");
1889 if (lvl->more_cleanups_ok)
1890 fprintf (stderr, " more-cleanups-ok");
1891 if (lvl->have_cleanups)
1892 fprintf (stderr, " have-cleanups");
1893 fprintf (stderr, "\n");
1894 if (lvl->names)
1896 fprintf (stderr, " names:\t");
1897 /* We can probably fit 3 names to a line? */
1898 for (t = lvl->names; t; t = TREE_CHAIN (t))
1900 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1901 continue;
1902 if (no_print_builtins
1903 && (TREE_CODE (t) == TYPE_DECL)
1904 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1905 continue;
1907 /* Function decls tend to have longer names. */
1908 if (TREE_CODE (t) == FUNCTION_DECL)
1909 len = 3;
1910 else
1911 len = 2;
1912 i += len;
1913 if (i > 6)
1915 fprintf (stderr, "\n\t");
1916 i = len;
1918 print_node_brief (stderr, "", t, 0);
1919 if (t == error_mark_node)
1920 break;
1922 if (i)
1923 fprintf (stderr, "\n");
1925 if (lvl->tags)
1927 fprintf (stderr, " tags:\t");
1928 i = 0;
1929 for (t = lvl->tags; t; t = TREE_CHAIN (t))
1931 if (TREE_PURPOSE (t) == NULL_TREE)
1932 len = 3;
1933 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1934 len = 2;
1935 else
1936 len = 4;
1937 i += len;
1938 if (i > 5)
1940 fprintf (stderr, "\n\t");
1941 i = len;
1943 if (TREE_PURPOSE (t) == NULL_TREE)
1945 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1946 fprintf (stderr, ">");
1948 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1949 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1950 else
1952 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1953 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1954 fprintf (stderr, ">");
1957 if (i)
1958 fprintf (stderr, "\n");
1960 if (lvl->class_shadowed)
1962 fprintf (stderr, " class-shadowed:");
1963 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1965 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1967 fprintf (stderr, "\n");
1969 if (lvl->type_shadowed)
1971 fprintf (stderr, " type-shadowed:");
1972 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1974 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1976 fprintf (stderr, "\n");
1980 void
1981 print_other_binding_stack (struct cp_binding_level *stack)
1983 struct cp_binding_level *level;
1984 for (level = stack; level != global_binding_level; level = level->level_chain)
1986 fprintf (stderr, "binding level ");
1987 fprintf (stderr, HOST_PTR_PRINTF, level);
1988 fprintf (stderr, "\n");
1989 print_binding_level (level);
1993 void
1994 print_binding_stack (void)
1996 struct cp_binding_level *b;
1997 fprintf (stderr, "current_binding_level=");
1998 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
1999 fprintf (stderr, "\nclass_binding_level=");
2000 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2001 fprintf (stderr, "\nglobal_binding_level=");
2002 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
2003 fprintf (stderr, "\n");
2004 if (class_binding_level)
2006 for (b = class_binding_level; b; b = b->level_chain)
2007 if (b == current_binding_level)
2008 break;
2009 if (b)
2010 b = class_binding_level;
2011 else
2012 b = current_binding_level;
2014 else
2015 b = current_binding_level;
2016 print_other_binding_stack (b);
2017 fprintf (stderr, "global:\n");
2018 print_binding_level (global_binding_level);
2021 /* Namespace binding access routines: The namespace_bindings field of
2022 the identifier is polymorphic, with three possible values:
2023 NULL_TREE, a list of "cxx_binding"s. */
2025 /* Check whether the a binding for the name to scope is known.
2026 Assumes that the bindings of the name are already a list
2027 of bindings. Returns the binding found, or NULL_TREE. */
2029 static inline cxx_binding *
2030 find_binding (tree name, tree scope, cxx_binding *front)
2032 cxx_binding *iter;
2033 cxx_binding *prev = NULL;
2035 timevar_push (TV_NAME_LOOKUP);
2036 scope = ORIGINAL_NAMESPACE (scope);
2038 for (iter = front; iter != NULL; iter = iter->previous)
2040 if (BINDING_SCOPE (iter) == scope)
2042 /* Move binding found to the front of the list, so
2043 subsequent lookups will find it faster. */
2044 if (prev)
2046 prev->previous = iter->previous;
2047 iter->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2048 IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2050 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, iter);
2052 prev = iter;
2054 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL);
2057 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2058 cxx_binding *
2059 cxx_scope_find_binding_for_name (tree scope, tree name)
2061 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2062 if (b)
2064 scope = ORIGINAL_NAMESPACE (scope);
2065 /* Fold-in case where NAME is used only once. */
2066 if (scope == BINDING_SCOPE (b) && b->previous == NULL)
2067 return b;
2068 return find_binding (name, scope, b);
2070 return b;
2073 /* Always returns a binding for name in scope. If the
2074 namespace_bindings is not a list, convert it to one first.
2075 If no binding is found, make a new one. */
2077 cxx_binding *
2078 binding_for_name (tree name, tree scope)
2080 cxx_binding *result;
2082 scope = ORIGINAL_NAMESPACE (scope);
2083 result = cxx_scope_find_binding_for_name (scope, name);
2084 if (result)
2085 return result;
2086 /* Not found, make a new one. */
2087 result = cxx_binding_make ();
2088 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2089 BINDING_TYPE (result) = NULL_TREE;
2090 BINDING_VALUE (result) = NULL_TREE;
2091 BINDING_SCOPE (result) = scope;
2092 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2093 return result;
2096 /* Return the binding value for name in scope. */
2098 tree
2099 namespace_binding (tree name, tree scope)
2101 cxx_binding *b =
2102 cxx_scope_find_binding_for_name (scope ? scope : global_namespace, name);
2104 return b ? b->value : NULL_TREE;
2107 /* Set the binding value for name in scope. If modifying the binding
2108 of global_namespace is attempted, try to optimize it. */
2110 void
2111 set_namespace_binding (tree name, tree scope, tree val)
2113 cxx_binding *b;
2115 timevar_push (TV_NAME_LOOKUP);
2116 if (scope == NULL_TREE)
2117 scope = global_namespace;
2118 b = binding_for_name (name, scope);
2119 BINDING_VALUE (b) = val;
2120 timevar_pop (TV_NAME_LOOKUP);
2123 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2124 select a name that is unique to this compilation unit. */
2126 void
2127 push_namespace (tree name)
2129 tree d = NULL_TREE;
2130 int need_new = 1;
2131 int implicit_use = 0;
2132 int global = 0;
2134 timevar_push (TV_NAME_LOOKUP);
2136 if (!global_namespace)
2138 /* This must be ::. */
2139 my_friendly_assert (name == get_identifier ("::"), 377);
2140 global = 1;
2142 else if (!name)
2144 /* The name of anonymous namespace is unique for the translation
2145 unit. */
2146 if (!anonymous_namespace_name)
2147 anonymous_namespace_name = get_file_function_name ('N');
2148 name = anonymous_namespace_name;
2149 d = IDENTIFIER_NAMESPACE_VALUE (name);
2150 if (d)
2151 /* Reopening anonymous namespace. */
2152 need_new = 0;
2153 implicit_use = 1;
2155 else
2157 /* Check whether this is an extended namespace definition. */
2158 d = IDENTIFIER_NAMESPACE_VALUE (name);
2159 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2161 need_new = 0;
2162 if (DECL_NAMESPACE_ALIAS (d))
2164 error ("namespace alias `%D' not allowed here, assuming `%D'",
2165 d, DECL_NAMESPACE_ALIAS (d));
2166 d = DECL_NAMESPACE_ALIAS (d);
2171 if (need_new)
2173 /* Make a new namespace, binding the name to it. */
2174 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2175 /* The global namespace is not pushed, and the global binding
2176 level is set elsewhere. */
2177 if (!global)
2179 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2180 d = pushdecl (d);
2181 pushlevel (0);
2182 declare_namespace_level ();
2183 NAMESPACE_LEVEL (d) = current_binding_level;
2184 VARRAY_TREE_INIT (current_binding_level->static_decls,
2185 name != std_identifier ? 10 : 200,
2186 "Static declarations");
2189 else
2190 resume_binding_level (NAMESPACE_LEVEL (d));
2192 if (implicit_use)
2193 do_using_directive (d);
2194 /* Enter the name space. */
2195 current_namespace = d;
2197 timevar_pop (TV_NAME_LOOKUP);
2200 /* Pop from the scope of the current namespace. */
2202 void
2203 pop_namespace (void)
2205 my_friendly_assert (current_namespace != global_namespace, 20010801);
2206 current_namespace = CP_DECL_CONTEXT (current_namespace);
2207 /* The binding level is not popped, as it might be re-opened later. */
2208 suspend_binding_level ();
2211 /* Push into the scope of the namespace NS, even if it is deeply
2212 nested within another namespace. */
2214 void
2215 push_nested_namespace (tree ns)
2217 if (ns == global_namespace)
2218 push_to_top_level ();
2219 else
2221 push_nested_namespace (CP_DECL_CONTEXT (ns));
2222 push_namespace (DECL_NAME (ns));
2226 /* Pop back from the scope of the namespace NS, which was previously
2227 entered with push_nested_namespace. */
2229 void
2230 pop_nested_namespace (tree ns)
2232 timevar_push (TV_NAME_LOOKUP);
2233 while (ns != global_namespace)
2235 pop_namespace ();
2236 ns = CP_DECL_CONTEXT (ns);
2239 pop_from_top_level ();
2240 timevar_pop (TV_NAME_LOOKUP);
2244 /* Allocate storage for saving a C++ binding. */
2245 #define cxx_saved_binding_make() \
2246 (ggc_alloc (sizeof (cxx_saved_binding)))
2248 struct cxx_saved_binding GTY(())
2250 /* Link that chains saved C++ bindings for a given name into a stack. */
2251 cxx_saved_binding *previous;
2252 /* The name of the current binding. */
2253 tree identifier;
2254 /* The binding we're saving. */
2255 cxx_binding *binding;
2256 tree class_value;
2257 tree real_type_value;
2260 /* Subroutines for reverting temporarily to top-level for instantiation
2261 of templates and such. We actually need to clear out the class- and
2262 local-value slots of all identifiers, so that only the global values
2263 are at all visible. Simply setting current_binding_level to the global
2264 scope isn't enough, because more binding levels may be pushed. */
2265 struct saved_scope *scope_chain;
2267 static cxx_saved_binding *
2268 store_bindings (tree names, cxx_saved_binding *old_bindings)
2270 tree t;
2271 cxx_saved_binding *search_bindings = old_bindings;
2273 timevar_push (TV_NAME_LOOKUP);
2274 for (t = names; t; t = TREE_CHAIN (t))
2276 tree id;
2277 cxx_saved_binding *saved;
2278 cxx_saved_binding *t1;
2280 if (TREE_CODE (t) == TREE_LIST)
2281 id = TREE_PURPOSE (t);
2282 else
2283 id = DECL_NAME (t);
2285 if (!id
2286 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2287 we have no IDENTIFIER_BINDING if we have left the class
2288 scope, but cached the class-level declarations. */
2289 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2290 continue;
2292 for (t1 = search_bindings; t1; t1 = t1->previous)
2293 if (t1->identifier == id)
2294 goto skip_it;
2296 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2297 saved = cxx_saved_binding_make ();
2298 saved->previous = old_bindings;
2299 saved->identifier = id;
2300 saved->binding = IDENTIFIER_BINDING (id);
2301 saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2302 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2303 IDENTIFIER_BINDING (id) = NULL;
2304 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2305 old_bindings = saved;
2306 skip_it:
2309 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2312 void
2313 maybe_push_to_top_level (int pseudo)
2315 struct saved_scope *s;
2316 struct cp_binding_level *b;
2317 cxx_saved_binding *old_bindings;
2318 int need_pop;
2320 timevar_push (TV_NAME_LOOKUP);
2321 s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2323 b = scope_chain ? current_binding_level : 0;
2325 /* If we're in the middle of some function, save our state. */
2326 if (cfun)
2328 need_pop = 1;
2329 push_function_context_to (NULL_TREE);
2331 else
2332 need_pop = 0;
2334 old_bindings = NULL;
2335 if (scope_chain && previous_class_type)
2336 old_bindings = store_bindings (previous_class_values, old_bindings);
2338 /* Have to include global_binding_level, because class-level decls
2339 aren't listed anywhere useful. */
2340 for (; b; b = b->level_chain)
2342 tree t;
2344 /* Template IDs are inserted into the global level. If they were
2345 inserted into namespace level, finish_file wouldn't find them
2346 when doing pending instantiations. Therefore, don't stop at
2347 namespace level, but continue until :: . */
2348 if (b == global_binding_level || (pseudo && b->template_parms_p))
2349 break;
2351 old_bindings = store_bindings (b->names, old_bindings);
2352 /* We also need to check class_shadowed to save class-level type
2353 bindings, since pushclass doesn't fill in b->names. */
2354 if (b->parm_flag == 2)
2355 old_bindings = store_bindings (b->class_shadowed, old_bindings);
2357 /* Unwind type-value slots back to top level. */
2358 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2359 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2361 s->prev = scope_chain;
2362 s->old_bindings = old_bindings;
2363 s->bindings = b;
2364 s->need_pop_function_context = need_pop;
2365 s->function_decl = current_function_decl;
2366 s->last_parms = last_function_parms;
2367 s->check_access = flag_access_control;
2369 scope_chain = s;
2370 current_function_decl = NULL_TREE;
2371 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2372 current_lang_name = lang_name_cplusplus;
2373 current_namespace = global_namespace;
2374 timevar_pop (TV_NAME_LOOKUP);
2377 void
2378 push_to_top_level (void)
2380 maybe_push_to_top_level (0);
2383 void
2384 pop_from_top_level (void)
2386 struct saved_scope *s = scope_chain;
2387 cxx_saved_binding *saved;
2389 timevar_push (TV_NAME_LOOKUP);
2390 /* Clear out class-level bindings cache. */
2391 if (previous_class_type)
2392 invalidate_class_lookup_cache ();
2394 current_lang_base = 0;
2396 scope_chain = s->prev;
2397 for (saved = s->old_bindings; saved; saved = saved->previous)
2399 tree id = saved->identifier;
2401 IDENTIFIER_BINDING (id) = saved->binding;
2402 IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2403 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2406 /* If we were in the middle of compiling a function, restore our
2407 state. */
2408 if (s->need_pop_function_context)
2409 pop_function_context_from (NULL_TREE);
2410 current_function_decl = s->function_decl;
2411 last_function_parms = s->last_parms;
2412 timevar_pop (TV_NAME_LOOKUP);
2415 /* Push a definition of struct, union or enum tag "name".
2416 into binding_level "b". "type" should be the type node,
2417 We assume that the tag "name" is not already defined.
2419 Note that the definition may really be just a forward reference.
2420 In that case, the TYPE_SIZE will be a NULL_TREE.
2422 C++ gratuitously puts all these tags in the name space. */
2424 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2425 record the shadowed value for this binding contour. TYPE is
2426 the type that ID maps to. */
2428 static void
2429 set_identifier_type_value_with_scope (tree id,
2430 tree type,
2431 struct cp_binding_level* b)
2433 if (!b->namespace_p)
2435 /* Shadow the marker, not the real thing, so that the marker
2436 gets restored later. */
2437 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2438 b->type_shadowed
2439 = tree_cons (id, old_type_value, b->type_shadowed);
2441 else
2443 cxx_binding *binding = binding_for_name (id, current_namespace);
2444 BINDING_TYPE (binding) = type;
2445 /* Store marker instead of real type. */
2446 type = global_type_node;
2448 SET_IDENTIFIER_TYPE_VALUE (id, type);
2451 /* As set_identifier_type_value_with_scope, but using current_binding_level. */
2453 void
2454 set_identifier_type_value (tree id, tree type)
2456 set_identifier_type_value_with_scope (id, type, current_binding_level);
2459 /* Return the type associated with id. */
2461 tree
2462 identifier_type_value (tree id)
2464 timevar_push (TV_NAME_LOOKUP);
2465 /* There is no type with that name, anywhere. */
2466 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2467 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2468 /* This is not the type marker, but the real thing. */
2469 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2470 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2471 /* Have to search for it. It must be on the global level, now.
2472 Ask lookup_name not to return non-types. */
2473 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2474 if (id)
2475 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2476 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2479 /* Pop off extraneous binding levels left over due to syntax errors.
2481 We don't pop past namespaces, as they might be valid. */
2483 void
2484 pop_everything (void)
2486 #ifdef DEBUG_BINDING_LEVELS
2487 fprintf (stderr, "XXX entering pop_everything ()\n");
2488 #endif
2489 while (!toplevel_bindings_p ())
2491 if (current_binding_level->parm_flag == 2)
2492 pop_nested_class ();
2493 else
2494 poplevel (0, 0, 0);
2496 #ifdef DEBUG_BINDING_LEVELS
2497 fprintf (stderr, "XXX leaving pop_everything ()\n");
2498 #endif
2501 /* The type TYPE is being declared. If it is a class template, or a
2502 specialization of a class template, do any processing required and
2503 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
2504 being declared a friend. B is the binding level at which this TYPE
2505 should be bound.
2507 Returns the TYPE_DECL for TYPE, which may have been altered by this
2508 processing. */
2510 static tree
2511 maybe_process_template_type_declaration (tree type,
2512 int globalize,
2513 struct cp_binding_level* b)
2515 tree decl = TYPE_NAME (type);
2517 if (processing_template_parmlist)
2518 /* You can't declare a new template type in a template parameter
2519 list. But, you can declare a non-template type:
2521 template <class A*> struct S;
2523 is a forward-declaration of `A'. */
2525 else
2527 maybe_check_template_type (type);
2529 my_friendly_assert (IS_AGGR_TYPE (type)
2530 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2533 if (processing_template_decl)
2535 /* This may change after the call to
2536 push_template_decl_real, but we want the original value. */
2537 tree name = DECL_NAME (decl);
2539 decl = push_template_decl_real (decl, globalize);
2540 /* If the current binding level is the binding level for the
2541 template parameters (see the comment in
2542 begin_template_parm_list) and the enclosing level is a class
2543 scope, and we're not looking at a friend, push the
2544 declaration of the member class into the class scope. In the
2545 friend case, push_template_decl will already have put the
2546 friend into global scope, if appropriate. */
2547 if (TREE_CODE (type) != ENUMERAL_TYPE
2548 && !globalize && b->template_parms_p
2549 && b->level_chain->parm_flag == 2)
2551 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2552 /* Put this tag on the list of tags for the class, since
2553 that won't happen below because B is not the class
2554 binding level, but is instead the pseudo-global level. */
2555 b->level_chain->tags =
2556 tree_cons (name, type, b->level_chain->tags);
2557 if (!COMPLETE_TYPE_P (current_class_type))
2559 maybe_add_class_template_decl_list (current_class_type,
2560 type, /*friend_p=*/0);
2561 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2567 return decl;
2570 /* In C++, you don't have to write `struct S' to refer to `S'; you
2571 can just use `S'. We accomplish this by creating a TYPE_DECL as
2572 if the user had written `typedef struct S S'. Create and return
2573 the TYPE_DECL for TYPE. */
2575 tree
2576 create_implicit_typedef (tree name, tree type)
2578 tree decl;
2580 decl = build_decl (TYPE_DECL, name, type);
2581 DECL_ARTIFICIAL (decl) = 1;
2582 /* There are other implicit type declarations, like the one *within*
2583 a class that allows you to write `S::S'. We must distinguish
2584 amongst these. */
2585 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2586 TYPE_NAME (type) = decl;
2588 return decl;
2591 /* Remember a local name for name-mangling purposes. */
2593 static void
2594 push_local_name (tree decl)
2596 size_t i, nelts;
2597 tree t, name;
2599 timevar_push (TV_NAME_LOOKUP);
2600 if (!local_names)
2601 VARRAY_TREE_INIT (local_names, 8, "local_names");
2603 name = DECL_NAME (decl);
2605 nelts = VARRAY_ACTIVE_SIZE (local_names);
2606 for (i = 0; i < nelts; i++)
2608 t = VARRAY_TREE (local_names, i);
2609 if (DECL_NAME (t) == name)
2611 if (!DECL_LANG_SPECIFIC (decl))
2612 retrofit_lang_decl (decl);
2613 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2614 if (DECL_LANG_SPECIFIC (t))
2615 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2616 else
2617 DECL_DISCRIMINATOR (decl) = 1;
2619 VARRAY_TREE (local_names, i) = decl;
2620 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
2624 VARRAY_PUSH_TREE (local_names, decl);
2625 timevar_pop (TV_NAME_LOOKUP);
2628 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2629 Normally put it into the inner-most non-tag-transparent scope,
2630 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2631 The latter is needed for implicit declarations. */
2633 void
2634 pushtag (tree name, tree type, int globalize)
2636 register struct cp_binding_level *b;
2638 timevar_push (TV_NAME_LOOKUP);
2639 b = current_binding_level;
2640 while (b->tag_transparent
2641 || (b->parm_flag == 2
2642 && (globalize
2643 /* We may be defining a new type in the initializer
2644 of a static member variable. We allow this when
2645 not pedantic, and it is particularly useful for
2646 type punning via an anonymous union. */
2647 || COMPLETE_TYPE_P (b->this_class))))
2648 b = b->level_chain;
2650 b->tags = tree_cons (name, type, b->tags);
2652 if (name)
2654 /* Do C++ gratuitous typedefing. */
2655 if (IDENTIFIER_TYPE_VALUE (name) != type)
2657 register tree d = NULL_TREE;
2658 int in_class = 0;
2659 tree context = TYPE_CONTEXT (type);
2661 if (! context)
2663 tree cs = current_scope ();
2665 if (! globalize)
2666 context = cs;
2667 else if (cs != NULL_TREE && TYPE_P (cs))
2668 /* When declaring a friend class of a local class, we want
2669 to inject the newly named class into the scope
2670 containing the local class, not the namespace scope. */
2671 context = decl_function_context (get_type_decl (cs));
2673 if (!context)
2674 context = current_namespace;
2676 if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2677 || b->parm_flag == 2)
2678 in_class = 1;
2680 if (current_lang_name == lang_name_java)
2681 TYPE_FOR_JAVA (type) = 1;
2683 d = create_implicit_typedef (name, type);
2684 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2685 if (! in_class)
2686 set_identifier_type_value_with_scope (name, type, b);
2688 d = maybe_process_template_type_declaration (type,
2689 globalize, b);
2691 if (b->parm_flag == 2)
2693 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2694 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2695 class. But if it's a member template class, we
2696 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2697 is done later. */
2698 finish_member_declaration (d);
2699 else
2700 pushdecl_class_level (d);
2702 else
2703 d = pushdecl_with_scope (d, b);
2705 /* FIXME what if it gets a name from typedef? */
2706 if (ANON_AGGRNAME_P (name))
2707 DECL_IGNORED_P (d) = 1;
2709 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2711 /* If this is a local class, keep track of it. We need this
2712 information for name-mangling, and so that it is possible to find
2713 all function definitions in a translation unit in a convenient
2714 way. (It's otherwise tricky to find a member function definition
2715 it's only pointed to from within a local class.) */
2716 if (TYPE_CONTEXT (type)
2717 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2718 && !processing_template_decl)
2719 VARRAY_PUSH_TREE (local_classes, type);
2721 if (b->parm_flag == 2)
2723 if (!COMPLETE_TYPE_P (current_class_type))
2725 maybe_add_class_template_decl_list (current_class_type,
2726 type, /*friend_p=*/0);
2727 CLASSTYPE_TAGS (current_class_type) = b->tags;
2732 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2733 /* Use the canonical TYPE_DECL for this node. */
2734 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2735 else
2737 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2738 will be the tagged type we just added to the current
2739 binding level. This fake NULL-named TYPE_DECL node helps
2740 dwarfout.c to know when it needs to output a
2741 representation of a tagged type, and it also gives us a
2742 convenient place to record the "scope start" address for
2743 the tagged type. */
2745 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2746 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2748 timevar_pop (TV_NAME_LOOKUP);
2751 /* Counter used to create anonymous type names. */
2753 static GTY(()) int anon_cnt;
2755 /* Return an IDENTIFIER which can be used as a name for
2756 anonymous structs and unions. */
2758 tree
2759 make_anon_name (void)
2761 char buf[32];
2763 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2764 return get_identifier (buf);
2767 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2768 This keeps dbxout from getting confused. */
2770 void
2771 clear_anon_tags (void)
2773 register struct cp_binding_level *b;
2774 register tree tags;
2775 static int last_cnt = 0;
2777 /* Fast out if no new anon names were declared. */
2778 if (last_cnt == anon_cnt)
2779 return;
2781 b = current_binding_level;
2782 while (b->tag_transparent)
2783 b = b->level_chain;
2784 tags = b->tags;
2785 while (tags)
2787 /* A NULL purpose means we have already processed all tags
2788 from here to the end of the list. */
2789 if (TREE_PURPOSE (tags) == NULL_TREE)
2790 break;
2791 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2792 TREE_PURPOSE (tags) = NULL_TREE;
2793 tags = TREE_CHAIN (tags);
2795 last_cnt = anon_cnt;
2798 /* Subroutine of duplicate_decls: return truthvalue of whether
2799 or not types of these decls match.
2801 For C++, we must compare the parameter list so that `int' can match
2802 `int&' in a parameter position, but `int&' is not confused with
2803 `const int&'. */
2806 decls_match (tree newdecl, tree olddecl)
2808 int types_match;
2810 if (newdecl == olddecl)
2811 return 1;
2813 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2814 /* If the two DECLs are not even the same kind of thing, we're not
2815 interested in their types. */
2816 return 0;
2818 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2820 tree f1 = TREE_TYPE (newdecl);
2821 tree f2 = TREE_TYPE (olddecl);
2822 tree p1 = TYPE_ARG_TYPES (f1);
2823 tree p2 = TYPE_ARG_TYPES (f2);
2825 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2826 && ! (DECL_EXTERN_C_P (newdecl)
2827 && DECL_EXTERN_C_P (olddecl)))
2828 return 0;
2830 if (TREE_CODE (f1) != TREE_CODE (f2))
2831 return 0;
2833 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2835 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2836 && (DECL_BUILT_IN (olddecl)
2837 #ifndef NO_IMPLICIT_EXTERN_C
2838 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2839 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2840 #endif
2843 types_match = self_promoting_args_p (p1);
2844 if (p1 == void_list_node)
2845 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2847 #ifndef NO_IMPLICIT_EXTERN_C
2848 else if (p1 == NULL_TREE
2849 && (DECL_EXTERN_C_P (olddecl)
2850 && DECL_IN_SYSTEM_HEADER (olddecl)
2851 && !DECL_CLASS_SCOPE_P (olddecl))
2852 && (DECL_EXTERN_C_P (newdecl)
2853 && DECL_IN_SYSTEM_HEADER (newdecl)
2854 && !DECL_CLASS_SCOPE_P (newdecl)))
2856 types_match = self_promoting_args_p (p2);
2857 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2859 #endif
2860 else
2861 types_match = compparms (p1, p2);
2863 else
2864 types_match = 0;
2866 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2868 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2869 DECL_TEMPLATE_PARMS (olddecl)))
2870 return 0;
2872 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2873 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2874 return 0;
2876 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2877 types_match = 1;
2878 else
2879 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2880 DECL_TEMPLATE_RESULT (newdecl));
2882 else
2884 if (TREE_TYPE (newdecl) == error_mark_node)
2885 types_match = TREE_TYPE (olddecl) == error_mark_node;
2886 else if (TREE_TYPE (olddecl) == NULL_TREE)
2887 types_match = TREE_TYPE (newdecl) == NULL_TREE;
2888 else if (TREE_TYPE (newdecl) == NULL_TREE)
2889 types_match = 0;
2890 else
2891 types_match = comptypes (TREE_TYPE (newdecl),
2892 TREE_TYPE (olddecl),
2893 COMPARE_REDECLARATION);
2896 return types_match;
2899 /* If NEWDECL is `static' and an `extern' was seen previously,
2900 warn about it. OLDDECL is the previous declaration.
2902 Note that this does not apply to the C++ case of declaring
2903 a variable `extern const' and then later `const'.
2905 Don't complain about built-in functions, since they are beyond
2906 the user's control. */
2908 static void
2909 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2911 static const char *const explicit_extern_static_warning
2912 = "`%D' was declared `extern' and later `static'";
2913 static const char *const implicit_extern_static_warning
2914 = "`%D' was declared implicitly `extern' and later `static'";
2916 tree name;
2918 if (TREE_CODE (newdecl) == TYPE_DECL
2919 || TREE_CODE (newdecl) == TEMPLATE_DECL
2920 || TREE_CODE (newdecl) == CONST_DECL)
2921 return;
2923 /* Don't get confused by static member functions; that's a different
2924 use of `static'. */
2925 if (TREE_CODE (newdecl) == FUNCTION_DECL
2926 && DECL_STATIC_FUNCTION_P (newdecl))
2927 return;
2929 /* If the old declaration was `static', or the new one isn't, then
2930 then everything is OK. */
2931 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2932 return;
2934 /* It's OK to declare a builtin function as `static'. */
2935 if (TREE_CODE (olddecl) == FUNCTION_DECL
2936 && DECL_ARTIFICIAL (olddecl))
2937 return;
2939 name = DECL_ASSEMBLER_NAME (newdecl);
2940 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2941 ? implicit_extern_static_warning
2942 : explicit_extern_static_warning, newdecl);
2943 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2946 /* Handle when a new declaration NEWDECL has the same name as an old
2947 one OLDDECL in the same binding contour. Prints an error message
2948 if appropriate.
2950 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2951 Otherwise, return 0. */
2954 duplicate_decls (tree newdecl, tree olddecl)
2956 unsigned olddecl_uid = DECL_UID (olddecl);
2957 int olddecl_friend = 0, types_match = 0;
2958 int new_defines_function = 0;
2960 if (newdecl == olddecl)
2961 return 1;
2963 types_match = decls_match (newdecl, olddecl);
2965 /* If either the type of the new decl or the type of the old decl is an
2966 error_mark_node, then that implies that we have already issued an
2967 error (earlier) for some bogus type specification, and in that case,
2968 it is rather pointless to harass the user with yet more error message
2969 about the same declaration, so just pretend the types match here. */
2970 if (TREE_TYPE (newdecl) == error_mark_node
2971 || TREE_TYPE (olddecl) == error_mark_node)
2972 types_match = 1;
2974 if (DECL_P (olddecl)
2975 && TREE_CODE (newdecl) == FUNCTION_DECL
2976 && TREE_CODE (olddecl) == FUNCTION_DECL
2977 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2979 if (DECL_DECLARED_INLINE_P (newdecl)
2980 && DECL_UNINLINABLE (newdecl)
2981 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2982 /* Already warned elsewhere. */;
2983 else if (DECL_DECLARED_INLINE_P (olddecl)
2984 && DECL_UNINLINABLE (olddecl)
2985 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2986 /* Already warned. */;
2987 else if (DECL_DECLARED_INLINE_P (newdecl)
2988 && DECL_UNINLINABLE (olddecl)
2989 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2991 warning ("%Hfunction '%D' redeclared as inline",
2992 &DECL_SOURCE_LOCATION (newdecl), newdecl);
2993 warning ("%Hprevious declaration of '%D' with attribute noinline",
2994 &DECL_SOURCE_LOCATION (olddecl), olddecl);
2996 else if (DECL_DECLARED_INLINE_P (olddecl)
2997 && DECL_UNINLINABLE (newdecl)
2998 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
3000 warning ("%Hfunction '%D' redeclared with attribute noinline",
3001 &DECL_SOURCE_LOCATION (newdecl), newdecl);
3002 warning ("%Hprevious declaration of '%D' was inline",
3003 &DECL_SOURCE_LOCATION (olddecl), olddecl);
3007 /* Check for redeclaration and other discrepancies. */
3008 if (TREE_CODE (olddecl) == FUNCTION_DECL
3009 && DECL_ARTIFICIAL (olddecl))
3011 if (TREE_CODE (newdecl) != FUNCTION_DECL)
3013 /* Avoid warnings redeclaring anticipated built-ins. */
3014 if (DECL_ANTICIPATED (olddecl))
3015 return 0;
3017 /* If you declare a built-in or predefined function name as static,
3018 the old definition is overridden, but optionally warn this was a
3019 bad choice of name. */
3020 if (! TREE_PUBLIC (newdecl))
3022 if (warn_shadow)
3023 warning ("shadowing %s function `%#D'",
3024 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3025 olddecl);
3026 /* Discard the old built-in function. */
3027 return 0;
3029 /* If the built-in is not ansi, then programs can override
3030 it even globally without an error. */
3031 else if (! DECL_BUILT_IN (olddecl))
3032 warning ("library function `%#D' redeclared as non-function `%#D'",
3033 olddecl, newdecl);
3034 else
3036 error ("declaration of `%#D'", newdecl);
3037 error ("conflicts with built-in declaration `%#D'",
3038 olddecl);
3040 return 0;
3042 else if (!types_match)
3044 /* Avoid warnings redeclaring anticipated built-ins. */
3045 if (DECL_ANTICIPATED (olddecl))
3046 ; /* Do nothing yet. */
3047 else if ((DECL_EXTERN_C_P (newdecl)
3048 && DECL_EXTERN_C_P (olddecl))
3049 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3050 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3052 /* A near match; override the builtin. */
3054 if (TREE_PUBLIC (newdecl))
3056 warning ("new declaration `%#D'", newdecl);
3057 warning ("ambiguates built-in declaration `%#D'",
3058 olddecl);
3060 else if (warn_shadow)
3061 warning ("shadowing %s function `%#D'",
3062 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3063 olddecl);
3065 else
3066 /* Discard the old built-in function. */
3067 return 0;
3069 /* Replace the old RTL to avoid problems with inlining. */
3070 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3073 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
3075 /* If a builtin function is redeclared as `static', merge
3076 the declarations, but make the original one static. */
3077 DECL_THIS_STATIC (olddecl) = 1;
3078 TREE_PUBLIC (olddecl) = 0;
3080 /* Make the old declaration consistent with the new one so
3081 that all remnants of the builtin-ness of this function
3082 will be banished. */
3083 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3084 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3087 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3089 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3090 && TREE_CODE (newdecl) != TYPE_DECL
3091 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3092 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3093 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3094 && TREE_CODE (olddecl) != TYPE_DECL
3095 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3096 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3097 == TYPE_DECL))))
3099 /* We do nothing special here, because C++ does such nasty
3100 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3101 get shadowed, and know that if we need to find a TYPE_DECL
3102 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3103 slot of the identifier. */
3104 return 0;
3107 if ((TREE_CODE (newdecl) == FUNCTION_DECL
3108 && DECL_FUNCTION_TEMPLATE_P (olddecl))
3109 || (TREE_CODE (olddecl) == FUNCTION_DECL
3110 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3111 return 0;
3113 error ("`%#D' redeclared as different kind of symbol", newdecl);
3114 if (TREE_CODE (olddecl) == TREE_LIST)
3115 olddecl = TREE_VALUE (olddecl);
3116 cp_error_at ("previous declaration of `%#D'", olddecl);
3118 /* New decl is completely inconsistent with the old one =>
3119 tell caller to replace the old one. */
3121 return 0;
3123 else if (!types_match)
3125 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3126 /* These are certainly not duplicate declarations; they're
3127 from different scopes. */
3128 return 0;
3130 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3132 /* The name of a class template may not be declared to refer to
3133 any other template, class, function, object, namespace, value,
3134 or type in the same scope. */
3135 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3136 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3138 error ("declaration of template `%#D'", newdecl);
3139 cp_error_at ("conflicts with previous declaration `%#D'",
3140 olddecl);
3142 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3143 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3144 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3145 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3146 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3147 DECL_TEMPLATE_PARMS (olddecl))
3148 /* Template functions can be disambiguated by
3149 return type. */
3150 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3151 TREE_TYPE (TREE_TYPE (olddecl))))
3153 error ("new declaration `%#D'", newdecl);
3154 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3156 return 0;
3158 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3160 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3162 error ("declaration of C function `%#D' conflicts with",
3163 newdecl);
3164 cp_error_at ("previous declaration `%#D' here", olddecl);
3166 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3167 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3169 error ("new declaration `%#D'", newdecl);
3170 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3172 else
3173 return 0;
3176 /* Already complained about this, so don't do so again. */
3177 else if (current_class_type == NULL_TREE
3178 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3180 error ("conflicting types for `%#D'", newdecl);
3181 cp_error_at ("previous declaration as `%#D'", olddecl);
3184 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3185 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3186 && (!DECL_TEMPLATE_INFO (newdecl)
3187 || (DECL_TI_TEMPLATE (newdecl)
3188 != DECL_TI_TEMPLATE (olddecl))))
3189 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3190 && (!DECL_TEMPLATE_INFO (olddecl)
3191 || (DECL_TI_TEMPLATE (olddecl)
3192 != DECL_TI_TEMPLATE (newdecl))))))
3193 /* It's OK to have a template specialization and a non-template
3194 with the same type, or to have specializations of two
3195 different templates with the same type. Note that if one is a
3196 specialization, and the other is an instantiation of the same
3197 template, that we do not exit at this point. That situation
3198 can occur if we instantiate a template class, and then
3199 specialize one of its methods. This situation is valid, but
3200 the declarations must be merged in the usual way. */
3201 return 0;
3202 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3203 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3204 && !DECL_USE_TEMPLATE (newdecl))
3205 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3206 && !DECL_USE_TEMPLATE (olddecl))))
3207 /* One of the declarations is a template instantiation, and the
3208 other is not a template at all. That's OK. */
3209 return 0;
3210 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3211 && DECL_NAMESPACE_ALIAS (newdecl)
3212 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3213 /* Redeclaration of namespace alias, ignore it. */
3214 return 1;
3215 else
3217 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3218 if (errmsg)
3220 error (errmsg, newdecl);
3221 if (DECL_NAME (olddecl) != NULL_TREE)
3222 cp_error_at ((DECL_INITIAL (olddecl)
3223 && namespace_bindings_p ())
3224 ? "`%#D' previously defined here"
3225 : "`%#D' previously declared here", olddecl);
3226 return 0;
3228 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3229 && DECL_INITIAL (olddecl) != NULL_TREE
3230 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3231 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3233 /* Prototype decl follows defn w/o prototype. */
3234 cp_warning_at ("prototype for `%#D'", newdecl);
3235 cp_warning_at ("follows non-prototype definition here", olddecl);
3237 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3238 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3240 /* extern "C" int foo ();
3241 int foo () { bar (); }
3242 is OK. */
3243 if (current_lang_depth () == 0)
3244 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3245 else
3247 cp_error_at ("previous declaration of `%#D' with %L linkage",
3248 olddecl, DECL_LANGUAGE (olddecl));
3249 error ("conflicts with new declaration with %L linkage",
3250 DECL_LANGUAGE (newdecl));
3254 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3256 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3258 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3259 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3260 int i = 1;
3262 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3263 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3265 for (; t1 && t1 != void_list_node;
3266 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3267 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3269 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3270 TREE_PURPOSE (t2)))
3272 pedwarn ("default argument given for parameter %d of `%#D'",
3273 i, newdecl);
3274 cp_pedwarn_at ("after previous specification in `%#D'",
3275 olddecl);
3277 else
3279 error ("default argument given for parameter %d of `%#D'",
3280 i, newdecl);
3281 cp_error_at ("after previous specification in `%#D'",
3282 olddecl);
3286 if (DECL_DECLARED_INLINE_P (newdecl)
3287 && ! DECL_DECLARED_INLINE_P (olddecl)
3288 && TREE_ADDRESSABLE (olddecl) && warn_inline)
3290 warning ("`%#D' was used before it was declared inline",
3291 newdecl);
3292 cp_warning_at ("previous non-inline declaration here",
3293 olddecl);
3298 /* Do not merge an implicit typedef with an explicit one. In:
3300 class A;
3302 typedef class A A __attribute__ ((foo));
3304 the attribute should apply only to the typedef. */
3305 if (TREE_CODE (olddecl) == TYPE_DECL
3306 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3307 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3308 return 0;
3310 /* If new decl is `static' and an `extern' was seen previously,
3311 warn about it. */
3312 warn_extern_redeclared_static (newdecl, olddecl);
3314 /* We have committed to returning 1 at this point. */
3315 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3317 /* Now that functions must hold information normally held
3318 by field decls, there is extra work to do so that
3319 declaration information does not get destroyed during
3320 definition. */
3321 if (DECL_VINDEX (olddecl))
3322 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3323 if (DECL_CONTEXT (olddecl))
3324 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3325 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3326 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3327 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3328 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3329 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3330 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3331 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3332 SET_OVERLOADED_OPERATOR_CODE
3333 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3334 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3336 /* Optionally warn about more than one declaration for the same
3337 name, but don't warn about a function declaration followed by a
3338 definition. */
3339 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3340 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3341 /* Don't warn about extern decl followed by definition. */
3342 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3343 /* Don't warn about friends, let add_friend take care of it. */
3344 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3346 warning ("redundant redeclaration of `%D' in same scope", newdecl);
3347 cp_warning_at ("previous declaration of `%D'", olddecl);
3351 /* Deal with C++: must preserve virtual function table size. */
3352 if (TREE_CODE (olddecl) == TYPE_DECL)
3354 register tree newtype = TREE_TYPE (newdecl);
3355 register tree oldtype = TREE_TYPE (olddecl);
3357 if (newtype != error_mark_node && oldtype != error_mark_node
3358 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3359 CLASSTYPE_FRIEND_CLASSES (newtype)
3360 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3362 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3365 /* Copy all the DECL_... slots specified in the new decl
3366 except for any that we copy here from the old type. */
3367 DECL_ATTRIBUTES (newdecl)
3368 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3370 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3372 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3373 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3374 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3375 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3377 /* If the new declaration is a definition, update the file and
3378 line information on the declaration. */
3379 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3380 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3382 DECL_SOURCE_LOCATION (olddecl)
3383 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3384 = DECL_SOURCE_LOCATION (newdecl);
3387 return 1;
3390 if (types_match)
3392 /* Automatically handles default parameters. */
3393 tree oldtype = TREE_TYPE (olddecl);
3394 tree newtype;
3396 /* Merge the data types specified in the two decls. */
3397 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3399 /* If merge_types produces a non-typedef type, just use the old type. */
3400 if (TREE_CODE (newdecl) == TYPE_DECL
3401 && newtype == DECL_ORIGINAL_TYPE (newdecl))
3402 newtype = oldtype;
3404 if (TREE_CODE (newdecl) == VAR_DECL)
3406 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3407 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3410 /* Do this after calling `merge_types' so that default
3411 parameters don't confuse us. */
3412 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3413 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3414 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3416 TREE_TYPE (newdecl) = build_exception_variant (newtype,
3417 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3418 TREE_TYPE (olddecl) = build_exception_variant (newtype,
3419 TYPE_RAISES_EXCEPTIONS (oldtype));
3421 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3422 && DECL_SOURCE_LINE (olddecl) != 0
3423 && flag_exceptions
3424 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3425 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3427 error ("declaration of `%F' throws different exceptions",
3428 newdecl);
3429 cp_error_at ("than previous declaration `%F'", olddecl);
3432 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3434 /* Lay the type out, unless already done. */
3435 if (! same_type_p (newtype, oldtype)
3436 && TREE_TYPE (newdecl) != error_mark_node
3437 && !(processing_template_decl && uses_template_parms (newdecl)))
3438 layout_type (TREE_TYPE (newdecl));
3440 if ((TREE_CODE (newdecl) == VAR_DECL
3441 || TREE_CODE (newdecl) == PARM_DECL
3442 || TREE_CODE (newdecl) == RESULT_DECL
3443 || TREE_CODE (newdecl) == FIELD_DECL
3444 || TREE_CODE (newdecl) == TYPE_DECL)
3445 && !(processing_template_decl && uses_template_parms (newdecl)))
3446 layout_decl (newdecl, 0);
3448 /* Merge the type qualifiers. */
3449 if (TREE_READONLY (newdecl))
3450 TREE_READONLY (olddecl) = 1;
3451 if (TREE_THIS_VOLATILE (newdecl))
3452 TREE_THIS_VOLATILE (olddecl) = 1;
3454 /* Merge the initialization information. */
3455 if (DECL_INITIAL (newdecl) == NULL_TREE
3456 && DECL_INITIAL (olddecl) != NULL_TREE)
3458 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3459 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3460 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3461 && DECL_LANG_SPECIFIC (newdecl)
3462 && DECL_LANG_SPECIFIC (olddecl))
3463 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3466 /* Merge the section attribute.
3467 We want to issue an error if the sections conflict but that must be
3468 done later in decl_attributes since we are called before attributes
3469 are assigned. */
3470 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3471 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3473 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3475 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3476 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3477 DECL_NO_LIMIT_STACK (newdecl)
3478 |= DECL_NO_LIMIT_STACK (olddecl);
3479 /* Keep the old RTL. */
3480 COPY_DECL_RTL (olddecl, newdecl);
3482 else if (TREE_CODE (newdecl) == VAR_DECL
3483 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3485 /* Keep the old RTL. We cannot keep the old RTL if the old
3486 declaration was for an incomplete object and the new
3487 declaration is not since many attributes of the RTL will
3488 change. */
3489 COPY_DECL_RTL (olddecl, newdecl);
3492 /* If cannot merge, then use the new type and qualifiers,
3493 and don't preserve the old rtl. */
3494 else
3496 /* Clean out any memory we had of the old declaration. */
3497 tree oldstatic = value_member (olddecl, static_aggregates);
3498 if (oldstatic)
3499 TREE_VALUE (oldstatic) = error_mark_node;
3501 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3502 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3503 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3504 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3507 /* Merge the storage class information. */
3508 merge_weak (newdecl, olddecl);
3510 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3511 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3512 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3513 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3514 if (! DECL_EXTERNAL (olddecl))
3515 DECL_EXTERNAL (newdecl) = 0;
3517 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3519 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3520 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3521 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3522 DECL_TEMPLATE_INSTANTIATED (newdecl)
3523 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3524 /* Don't really know how much of the language-specific
3525 values we should copy from old to new. */
3526 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3527 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
3528 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3529 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3530 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3531 DECL_INITIALIZED_IN_CLASS_P (newdecl)
3532 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3533 olddecl_friend = DECL_FRIEND_P (olddecl);
3535 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3536 if (TREE_CODE (newdecl) == FUNCTION_DECL
3537 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3539 DECL_BEFRIENDING_CLASSES (newdecl)
3540 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3541 DECL_BEFRIENDING_CLASSES (olddecl));
3542 /* DECL_THUNKS is only valid for virtual functions,
3543 otherwise it is a DECL_FRIEND_CONTEXT. */
3544 if (DECL_VIRTUAL_P (newdecl))
3545 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3549 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3551 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3552 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3554 /* If newdecl is not a specialization, then it is not a
3555 template-related function at all. And that means that we
3556 should have exited above, returning 0. */
3557 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3560 if (TREE_USED (olddecl))
3561 /* From [temp.expl.spec]:
3563 If a template, a member template or the member of a class
3564 template is explicitly specialized then that
3565 specialization shall be declared before the first use of
3566 that specialization that would cause an implicit
3567 instantiation to take place, in every translation unit in
3568 which such a use occurs. */
3569 error ("explicit specialization of %D after first use",
3570 olddecl);
3572 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3574 /* [temp.expl.spec/14] We don't inline explicit specialization
3575 just because the primary template says so. */
3577 else
3579 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3580 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3582 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3584 /* If either decl says `inline', this fn is inline, unless
3585 its definition was passed already. */
3586 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3587 DECL_INLINE (olddecl) = 1;
3588 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3590 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3591 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3594 /* Preserve abstractness on cloned [cd]tors. */
3595 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3597 if (! types_match)
3599 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3600 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3601 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3603 if (! types_match || new_defines_function)
3605 /* These need to be copied so that the names are available.
3606 Note that if the types do match, we'll preserve inline
3607 info and other bits, but if not, we won't. */
3608 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3609 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3611 if (new_defines_function)
3612 /* If defining a function declared with other language
3613 linkage, use the previously declared language linkage. */
3614 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3615 else if (types_match)
3617 /* If redeclaring a builtin function, and not a definition,
3618 it stays built in. */
3619 if (DECL_BUILT_IN (olddecl))
3621 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3622 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3623 /* If we're keeping the built-in definition, keep the rtl,
3624 regardless of declaration matches. */
3625 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3627 else
3628 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3630 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3631 /* Don't clear out the arguments if we're redefining a function. */
3632 if (DECL_ARGUMENTS (olddecl))
3633 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3636 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3637 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3639 /* Now preserve various other info from the definition. */
3640 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3641 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3642 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3643 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3645 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3647 int function_size;
3649 function_size = sizeof (struct tree_decl);
3651 memcpy ((char *) olddecl + sizeof (struct tree_common),
3652 (char *) newdecl + sizeof (struct tree_common),
3653 function_size - sizeof (struct tree_common));
3655 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3657 /* If newdecl is a template instantiation, it is possible that
3658 the following sequence of events has occurred:
3660 o A friend function was declared in a class template. The
3661 class template was instantiated.
3663 o The instantiation of the friend declaration was
3664 recorded on the instantiation list, and is newdecl.
3666 o Later, however, instantiate_class_template called pushdecl
3667 on the newdecl to perform name injection. But, pushdecl in
3668 turn called duplicate_decls when it discovered that another
3669 declaration of a global function with the same name already
3670 existed.
3672 o Here, in duplicate_decls, we decided to clobber newdecl.
3674 If we're going to do that, we'd better make sure that
3675 olddecl, and not newdecl, is on the list of
3676 instantiations so that if we try to do the instantiation
3677 again we won't get the clobbered declaration. */
3679 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3680 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3682 for (; decls; decls = TREE_CHAIN (decls))
3683 if (TREE_VALUE (decls) == newdecl)
3684 TREE_VALUE (decls) = olddecl;
3687 else
3689 memcpy ((char *) olddecl + sizeof (struct tree_common),
3690 (char *) newdecl + sizeof (struct tree_common),
3691 sizeof (struct tree_decl) - sizeof (struct tree_common)
3692 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3695 DECL_UID (olddecl) = olddecl_uid;
3696 if (olddecl_friend)
3697 DECL_FRIEND_P (olddecl) = 1;
3699 /* NEWDECL contains the merged attribute lists.
3700 Update OLDDECL to be the same. */
3701 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3703 return 1;
3706 /* Record a decl-node X as belonging to the current lexical scope.
3707 Check for errors (such as an incompatible declaration for the same
3708 name already seen in the same scope).
3710 Returns either X or an old decl for the same name.
3711 If an old decl is returned, it may have been smashed
3712 to agree with what X says. */
3714 tree
3715 pushdecl (tree x)
3717 register tree t;
3718 register tree name;
3719 int need_new_binding;
3721 timevar_push (TV_NAME_LOOKUP);
3722 /* We shouldn't be calling pushdecl when we're generating RTL for a
3723 function that we already did semantic analysis on previously. */
3724 my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3725 19990913);
3727 need_new_binding = 1;
3729 if (DECL_TEMPLATE_PARM_P (x))
3730 /* Template parameters have no context; they are not X::T even
3731 when declared within a class or namespace. */
3733 else
3735 if (current_function_decl && x != current_function_decl
3736 /* A local declaration for a function doesn't constitute
3737 nesting. */
3738 && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3739 /* A local declaration for an `extern' variable is in the
3740 scope of the current namespace, not the current
3741 function. */
3742 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3743 && !DECL_CONTEXT (x))
3744 DECL_CONTEXT (x) = current_function_decl;
3746 /* If this is the declaration for a namespace-scope function,
3747 but the declaration itself is in a local scope, mark the
3748 declaration. */
3749 if (TREE_CODE (x) == FUNCTION_DECL
3750 && DECL_NAMESPACE_SCOPE_P (x)
3751 && current_function_decl
3752 && x != current_function_decl)
3753 DECL_LOCAL_FUNCTION_P (x) = 1;
3756 name = DECL_NAME (x);
3757 if (name)
3759 int different_binding_level = 0;
3761 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3762 name = TREE_OPERAND (name, 0);
3764 /* In case this decl was explicitly namespace-qualified, look it
3765 up in its namespace context. */
3766 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3767 && namespace_bindings_p ())
3768 t = namespace_binding (name, DECL_CONTEXT (x));
3769 else
3770 t = lookup_name_current_level (name);
3772 /* [basic.link] If there is a visible declaration of an entity
3773 with linkage having the same name and type, ignoring entities
3774 declared outside the innermost enclosing namespace scope, the
3775 block scope declaration declares that same entity and
3776 receives the linkage of the previous declaration. */
3777 if (! t && current_function_decl && x != current_function_decl
3778 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3779 && DECL_EXTERNAL (x))
3781 /* Look in block scope. */
3782 t = IDENTIFIER_VALUE (name);
3783 /* Or in the innermost namespace. */
3784 if (! t)
3785 t = namespace_binding (name, DECL_CONTEXT (x));
3786 /* Does it have linkage? Note that if this isn't a DECL, it's an
3787 OVERLOAD, which is OK. */
3788 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3789 t = NULL_TREE;
3790 if (t)
3791 different_binding_level = 1;
3794 /* If we are declaring a function, and the result of name-lookup
3795 was an OVERLOAD, look for an overloaded instance that is
3796 actually the same as the function we are declaring. (If
3797 there is one, we have to merge our declaration with the
3798 previous declaration.) */
3799 if (t && TREE_CODE (t) == OVERLOAD)
3801 tree match;
3803 if (TREE_CODE (x) == FUNCTION_DECL)
3804 for (match = t; match; match = OVL_NEXT (match))
3806 if (decls_match (OVL_CURRENT (match), x))
3807 break;
3809 else
3810 /* Just choose one. */
3811 match = t;
3813 if (match)
3814 t = OVL_CURRENT (match);
3815 else
3816 t = NULL_TREE;
3819 if (t == error_mark_node)
3821 /* error_mark_node is 0 for a while during initialization! */
3822 t = NULL_TREE;
3823 cp_error_at ("`%#D' used prior to declaration", x);
3825 else if (t != NULL_TREE)
3827 if (different_binding_level)
3829 if (decls_match (x, t))
3830 /* The standard only says that the local extern
3831 inherits linkage from the previous decl; in
3832 particular, default args are not shared. It would
3833 be nice to propagate inlining info, though. FIXME. */
3834 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3836 else if (TREE_CODE (t) == PARM_DECL)
3838 if (DECL_CONTEXT (t) == NULL_TREE)
3839 /* This is probaby caused by too many errors, but calling
3840 abort will say that if errors have occurred. */
3841 abort ();
3843 /* Check for duplicate params. */
3844 if (duplicate_decls (x, t))
3845 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3847 else if ((DECL_EXTERN_C_FUNCTION_P (x)
3848 || DECL_FUNCTION_TEMPLATE_P (x))
3849 && is_overloaded_fn (t))
3850 /* Don't do anything just yet. */;
3851 else if (t == wchar_decl_node)
3853 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3854 pedwarn ("redeclaration of `wchar_t' as `%T'",
3855 TREE_TYPE (x));
3857 /* Throw away the redeclaration. */
3858 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3860 else if (TREE_CODE (t) != TREE_CODE (x))
3862 if (duplicate_decls (x, t))
3863 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3865 else if (duplicate_decls (x, t))
3867 if (TREE_CODE (t) == TYPE_DECL)
3868 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3869 else if (TREE_CODE (t) == FUNCTION_DECL)
3870 check_default_args (t);
3872 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3874 else if (DECL_MAIN_P (x))
3876 /* A redeclaration of main, but not a duplicate of the
3877 previous one.
3879 [basic.start.main]
3881 This function shall not be overloaded. */
3882 cp_error_at ("invalid redeclaration of `%D'", t);
3883 error ("as `%D'", x);
3884 /* We don't try to push this declaration since that
3885 causes a crash. */
3886 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3890 check_template_shadow (x);
3892 /* If this is a function conjured up by the backend, massage it
3893 so it looks friendly. */
3894 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3896 retrofit_lang_decl (x);
3897 SET_DECL_LANGUAGE (x, lang_c);
3900 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3902 t = push_overloaded_decl (x, PUSH_LOCAL);
3903 if (t != x)
3904 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3905 if (!namespace_bindings_p ())
3906 /* We do not need to create a binding for this name;
3907 push_overloaded_decl will have already done so if
3908 necessary. */
3909 need_new_binding = 0;
3911 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3913 t = push_overloaded_decl (x, PUSH_GLOBAL);
3914 if (t == x)
3915 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3916 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3919 /* If declaring a type as a typedef, copy the type (unless we're
3920 at line 0), and install this TYPE_DECL as the new type's typedef
3921 name. See the extensive comment in ../c-decl.c (pushdecl). */
3922 if (TREE_CODE (x) == TYPE_DECL)
3924 tree type = TREE_TYPE (x);
3925 if (DECL_SOURCE_LINE (x) == 0)
3927 if (TYPE_NAME (type) == 0)
3928 TYPE_NAME (type) = x;
3930 else if (type != error_mark_node && TYPE_NAME (type) != x
3931 /* We don't want to copy the type when all we're
3932 doing is making a TYPE_DECL for the purposes of
3933 inlining. */
3934 && (!TYPE_NAME (type)
3935 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3937 DECL_ORIGINAL_TYPE (x) = type;
3938 type = build_type_copy (type);
3939 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3940 TYPE_NAME (type) = x;
3941 TREE_TYPE (x) = type;
3944 if (type != error_mark_node
3945 && TYPE_NAME (type)
3946 && TYPE_IDENTIFIER (type))
3947 set_identifier_type_value_with_scope (DECL_NAME (x), type,
3948 current_binding_level);
3952 /* Multiple external decls of the same identifier ought to match.
3954 We get warnings about inline functions where they are defined.
3955 We get warnings about other functions from push_overloaded_decl.
3957 Avoid duplicate warnings where they are used. */
3958 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3960 tree decl;
3962 decl = IDENTIFIER_NAMESPACE_VALUE (name);
3963 if (decl && TREE_CODE (decl) == OVERLOAD)
3964 decl = OVL_FUNCTION (decl);
3966 if (decl && decl != error_mark_node
3967 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3968 /* If different sort of thing, we already gave an error. */
3969 && TREE_CODE (decl) == TREE_CODE (x)
3970 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3972 pedwarn ("type mismatch with previous external decl", x);
3973 cp_pedwarn_at ("previous external decl of `%#D'", decl);
3977 /* This name is new in its binding level.
3978 Install the new declaration and return it. */
3979 if (namespace_bindings_p ())
3981 /* Install a global value. */
3983 /* If the first global decl has external linkage,
3984 warn if we later see static one. */
3985 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3986 TREE_PUBLIC (name) = 1;
3988 /* Bind the name for the entity. */
3989 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3990 && t != NULL_TREE)
3991 && (TREE_CODE (x) == TYPE_DECL
3992 || TREE_CODE (x) == VAR_DECL
3993 || TREE_CODE (x) == ALIAS_DECL
3994 || TREE_CODE (x) == NAMESPACE_DECL
3995 || TREE_CODE (x) == CONST_DECL
3996 || TREE_CODE (x) == TEMPLATE_DECL))
3997 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3999 /* Don't forget if the function was used via an implicit decl. */
4000 if (IDENTIFIER_IMPLICIT_DECL (name)
4001 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
4002 TREE_USED (x) = 1;
4004 /* Don't forget if its address was taken in that way. */
4005 if (IDENTIFIER_IMPLICIT_DECL (name)
4006 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
4007 TREE_ADDRESSABLE (x) = 1;
4009 /* Warn about mismatches against previous implicit decl. */
4010 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
4011 /* If this real decl matches the implicit, don't complain. */
4012 && ! (TREE_CODE (x) == FUNCTION_DECL
4013 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
4014 warning
4015 ("`%D' was previously implicitly declared to return `int'", x);
4017 /* If new decl is `static' and an `extern' was seen previously,
4018 warn about it. */
4019 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
4020 warn_extern_redeclared_static (x, t);
4022 else
4024 /* Here to install a non-global value. */
4025 tree oldlocal = IDENTIFIER_VALUE (name);
4026 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
4028 if (need_new_binding)
4030 push_local_binding (name, x, 0);
4031 /* Because push_local_binding will hook X on to the
4032 current_binding_level's name list, we don't want to
4033 do that again below. */
4034 need_new_binding = 0;
4037 /* If this is a TYPE_DECL, push it into the type value slot. */
4038 if (TREE_CODE (x) == TYPE_DECL)
4039 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
4040 current_binding_level);
4042 /* Clear out any TYPE_DECL shadowed by a namespace so that
4043 we won't think this is a type. The C struct hack doesn't
4044 go through namespaces. */
4045 if (TREE_CODE (x) == NAMESPACE_DECL)
4046 set_identifier_type_value_with_scope (name, NULL_TREE,
4047 current_binding_level);
4049 if (oldlocal)
4051 tree d = oldlocal;
4053 while (oldlocal
4054 && TREE_CODE (oldlocal) == VAR_DECL
4055 && DECL_DEAD_FOR_LOCAL (oldlocal))
4056 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
4058 if (oldlocal == NULL_TREE)
4059 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
4062 /* If this is an extern function declaration, see if we
4063 have a global definition or declaration for the function. */
4064 if (oldlocal == NULL_TREE
4065 && DECL_EXTERNAL (x)
4066 && oldglobal != NULL_TREE
4067 && TREE_CODE (x) == FUNCTION_DECL
4068 && TREE_CODE (oldglobal) == FUNCTION_DECL)
4070 /* We have one. Their types must agree. */
4071 if (decls_match (x, oldglobal))
4072 /* OK */;
4073 else
4075 warning ("extern declaration of `%#D' doesn't match", x);
4076 cp_warning_at ("global declaration `%#D'", oldglobal);
4079 /* If we have a local external declaration,
4080 and no file-scope declaration has yet been seen,
4081 then if we later have a file-scope decl it must not be static. */
4082 if (oldlocal == NULL_TREE
4083 && oldglobal == NULL_TREE
4084 && DECL_EXTERNAL (x)
4085 && TREE_PUBLIC (x))
4086 TREE_PUBLIC (name) = 1;
4088 /* Warn if shadowing an argument at the top level of the body. */
4089 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
4090 /* Inline decls shadow nothing. */
4091 && !DECL_FROM_INLINE (x)
4092 && TREE_CODE (oldlocal) == PARM_DECL
4093 /* Don't check the `this' parameter. */
4094 && !DECL_ARTIFICIAL (oldlocal))
4096 bool err = false;
4098 /* Don't complain if it's from an enclosing function. */
4099 if (DECL_CONTEXT (oldlocal) == current_function_decl
4100 && TREE_CODE (x) != PARM_DECL)
4102 /* Go to where the parms should be and see if we find
4103 them there. */
4104 struct cp_binding_level *b = current_binding_level->level_chain;
4106 /* Skip the ctor/dtor cleanup level. */
4107 b = b->level_chain;
4109 /* ARM $8.3 */
4110 if (b->parm_flag == 1)
4112 error ("declaration of `%#D' shadows a parameter",
4113 name);
4114 err = true;
4118 if (warn_shadow && !err)
4119 shadow_warning ("a parameter", name, oldlocal);
4122 /* Maybe warn if shadowing something else. */
4123 else if (warn_shadow && !DECL_EXTERNAL (x)
4124 /* No shadow warnings for internally generated vars. */
4125 && ! DECL_ARTIFICIAL (x)
4126 /* No shadow warnings for vars made for inlining. */
4127 && ! DECL_FROM_INLINE (x))
4129 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4130 && current_class_ptr
4131 && !TREE_STATIC (name))
4132 warning ("declaration of `%s' shadows a member of `this'",
4133 IDENTIFIER_POINTER (name));
4134 else if (oldlocal != NULL_TREE
4135 && TREE_CODE (oldlocal) == VAR_DECL)
4136 shadow_warning ("a previous local", name, oldlocal);
4137 else if (oldglobal != NULL_TREE
4138 && TREE_CODE (oldglobal) == VAR_DECL)
4139 /* XXX shadow warnings in outer-more namespaces */
4140 shadow_warning ("a global declaration", name, oldglobal);
4144 if (TREE_CODE (x) == FUNCTION_DECL)
4145 check_default_args (x);
4147 if (TREE_CODE (x) == VAR_DECL)
4148 maybe_register_incomplete_var (x);
4151 if (need_new_binding)
4152 add_decl_to_level (x,
4153 DECL_NAMESPACE_SCOPE_P (x)
4154 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4155 : current_binding_level);
4157 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4160 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4161 caller to set DECL_CONTEXT properly. */
4163 static tree
4164 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4166 register struct cp_binding_level *b;
4167 tree function_decl = current_function_decl;
4169 timevar_push (TV_NAME_LOOKUP);
4170 current_function_decl = NULL_TREE;
4171 if (level->parm_flag == 2)
4173 b = class_binding_level;
4174 class_binding_level = level;
4175 pushdecl_class_level (x);
4176 class_binding_level = b;
4178 else
4180 b = current_binding_level;
4181 current_binding_level = level;
4182 x = pushdecl (x);
4183 current_binding_level = b;
4185 current_function_decl = function_decl;
4186 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4189 /* Like pushdecl, only it places X in the current namespace,
4190 if appropriate. */
4192 tree
4193 pushdecl_namespace_level (tree x)
4195 register struct cp_binding_level *b = current_binding_level;
4196 register tree t;
4198 timevar_push (TV_NAME_LOOKUP);
4199 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4201 /* Now, the type_shadowed stack may screw us. Munge it so it does
4202 what we want. */
4203 if (TREE_CODE (x) == TYPE_DECL)
4205 tree name = DECL_NAME (x);
4206 tree newval;
4207 tree *ptr = (tree *)0;
4208 for (; b != global_binding_level; b = b->level_chain)
4210 tree shadowed = b->type_shadowed;
4211 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4212 if (TREE_PURPOSE (shadowed) == name)
4214 ptr = &TREE_VALUE (shadowed);
4215 /* Can't break out of the loop here because sometimes
4216 a binding level will have duplicate bindings for
4217 PT names. It's gross, but I haven't time to fix it. */
4220 newval = TREE_TYPE (x);
4221 if (ptr == (tree *)0)
4223 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4224 up here if this is changed to an assertion. --KR */
4225 SET_IDENTIFIER_TYPE_VALUE (name, newval);
4227 else
4229 *ptr = newval;
4232 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4235 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4236 if appropriate. */
4238 tree
4239 pushdecl_top_level (tree x)
4241 timevar_push (TV_NAME_LOOKUP);
4242 push_to_top_level ();
4243 x = pushdecl_namespace_level (x);
4244 pop_from_top_level ();
4245 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4248 /* Make the declaration of X appear in CLASS scope. */
4250 void
4251 pushdecl_class_level (tree x)
4253 tree name;
4255 timevar_push (TV_NAME_LOOKUP);
4256 /* Get the name of X. */
4257 if (TREE_CODE (x) == OVERLOAD)
4258 name = DECL_NAME (get_first_fn (x));
4259 else
4260 name = DECL_NAME (x);
4262 if (name)
4264 push_class_level_binding (name, x);
4265 if (TREE_CODE (x) == TYPE_DECL)
4266 set_identifier_type_value (name, TREE_TYPE (x));
4268 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4270 /* If X is an anonymous aggregate, all of its members are
4271 treated as if they were members of the class containing the
4272 aggregate, for naming purposes. */
4273 tree f;
4275 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4276 pushdecl_class_level (f);
4278 timevar_pop (TV_NAME_LOOKUP);
4281 /* Enter DECL into the symbol table, if that's appropriate. Returns
4282 DECL, or a modified version thereof. */
4284 tree
4285 maybe_push_decl (tree decl)
4287 tree type = TREE_TYPE (decl);
4289 /* Add this decl to the current binding level, but not if it comes
4290 from another scope, e.g. a static member variable. TEM may equal
4291 DECL or it may be a previous decl of the same name. */
4292 if (decl == error_mark_node
4293 || (TREE_CODE (decl) != PARM_DECL
4294 && DECL_CONTEXT (decl) != NULL_TREE
4295 /* Definitions of namespace members outside their namespace are
4296 possible. */
4297 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4298 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4299 || TREE_CODE (type) == UNKNOWN_TYPE
4300 /* The declaration of a template specialization does not affect
4301 the functions available for overload resolution, so we do not
4302 call pushdecl. */
4303 || (TREE_CODE (decl) == FUNCTION_DECL
4304 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4305 return decl;
4306 else
4307 return pushdecl (decl);
4310 /* Make the declaration(s) of X appear in CLASS scope
4311 under the name NAME. */
4313 void
4314 push_class_level_binding (tree name, tree x)
4316 cxx_binding *binding;
4317 timevar_push (TV_NAME_LOOKUP);
4318 /* The class_binding_level will be NULL if x is a template
4319 parameter name in a member template. */
4320 if (!class_binding_level)
4321 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
4323 /* Make sure that this new member does not have the same name
4324 as a template parameter. */
4325 if (TYPE_BEING_DEFINED (current_class_type))
4326 check_template_shadow (x);
4328 /* If this declaration shadows a declaration from an enclosing
4329 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4330 we leave this class. Record the shadowed declaration here. */
4331 binding = IDENTIFIER_BINDING (name);
4332 if (binding
4333 && ((TREE_CODE (x) == OVERLOAD
4334 && BINDING_VALUE (binding)
4335 && is_overloaded_fn (BINDING_VALUE (binding)))
4336 || INHERITED_VALUE_BINDING_P (binding)))
4338 tree shadow;
4339 tree old_decl;
4341 /* If the old binding was from a base class, and was for a tag
4342 name, slide it over to make room for the new binding. The
4343 old binding is still visible if explicitly qualified with a
4344 class-key. */
4345 if (INHERITED_VALUE_BINDING_P (binding)
4346 && BINDING_VALUE (binding)
4347 && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4348 && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4349 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4351 old_decl = BINDING_TYPE (binding);
4352 BINDING_TYPE (binding) = BINDING_VALUE (binding);
4353 BINDING_VALUE (binding) = NULL_TREE;
4354 INHERITED_VALUE_BINDING_P (binding) = 0;
4356 else
4357 old_decl = BINDING_VALUE (binding);
4359 /* Find the previous binding of name on the class-shadowed
4360 list, and update it. */
4361 for (shadow = class_binding_level->class_shadowed;
4362 shadow;
4363 shadow = TREE_CHAIN (shadow))
4364 if (TREE_PURPOSE (shadow) == name
4365 && TREE_TYPE (shadow) == old_decl)
4367 BINDING_VALUE (binding) = x;
4368 INHERITED_VALUE_BINDING_P (binding) = 0;
4369 TREE_TYPE (shadow) = x;
4370 IDENTIFIER_CLASS_VALUE (name) = x;
4371 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
4375 /* If we didn't replace an existing binding, put the binding on the
4376 stack of bindings for the identifier, and update the shadowed list. */
4377 if (push_class_binding (name, x))
4379 class_binding_level->class_shadowed
4380 = tree_cons (name, NULL,
4381 class_binding_level->class_shadowed);
4382 /* Record the value we are binding NAME to so that we can know
4383 what to pop later. */
4384 TREE_TYPE (class_binding_level->class_shadowed) = x;
4386 timevar_pop (TV_NAME_LOOKUP);
4389 /* Insert another USING_DECL into the current binding level, returning
4390 this declaration. If this is a redeclaration, do nothing, and
4391 return NULL_TREE if this not in namespace scope (in namespace
4392 scope, a using decl might extend any previous bindings). */
4394 tree
4395 push_using_decl (tree scope, tree name)
4397 tree decl;
4399 timevar_push (TV_NAME_LOOKUP);
4400 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4401 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4402 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4403 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4404 break;
4405 if (decl)
4406 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4407 namespace_bindings_p () ? decl : NULL_TREE);
4408 decl = build_lang_decl (USING_DECL, name, void_type_node);
4409 DECL_INITIAL (decl) = scope;
4410 TREE_CHAIN (decl) = current_binding_level->usings;
4411 current_binding_level->usings = decl;
4412 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4415 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4416 changed (i.e. there was already a directive), or the fresh
4417 TREE_LIST otherwise. */
4419 tree
4420 push_using_directive (tree used)
4422 tree ud = current_binding_level->using_directives;
4423 tree iter, ancestor;
4425 timevar_push (TV_NAME_LOOKUP);
4426 /* Check if we already have this. */
4427 if (purpose_member (used, ud) != NULL_TREE)
4428 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4430 ancestor = namespace_ancestor (current_decl_namespace (), used);
4431 ud = current_binding_level->using_directives;
4432 ud = tree_cons (used, ancestor, ud);
4433 current_binding_level->using_directives = ud;
4435 /* Recursively add all namespaces used. */
4436 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4437 push_using_directive (TREE_PURPOSE (iter));
4439 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4442 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4443 other definitions already in place. We get around this by making
4444 the value of the identifier point to a list of all the things that
4445 want to be referenced by that name. It is then up to the users of
4446 that name to decide what to do with that list.
4448 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4449 DECL_TEMPLATE_RESULT. It is dealt with the same way.
4451 FLAGS is a bitwise-or of the following values:
4452 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4453 namespace scope.
4454 PUSH_USING: DECL is being pushed as the result of a using
4455 declaration.
4457 The value returned may be a previous declaration if we guessed wrong
4458 about what language DECL should belong to (C or C++). Otherwise,
4459 it's always DECL (and never something that's not a _DECL). */
4461 tree
4462 push_overloaded_decl (tree decl, int flags)
4464 tree name = DECL_NAME (decl);
4465 tree old;
4466 tree new_binding;
4467 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4469 timevar_push (TV_NAME_LOOKUP);
4470 if (doing_global)
4471 old = namespace_binding (name, DECL_CONTEXT (decl));
4472 else
4473 old = lookup_name_current_level (name);
4475 if (old)
4477 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4479 tree t = TREE_TYPE (old);
4480 if (IS_AGGR_TYPE (t) && warn_shadow
4481 && (! DECL_IN_SYSTEM_HEADER (decl)
4482 || ! DECL_IN_SYSTEM_HEADER (old)))
4483 warning ("`%#D' hides constructor for `%#T'", decl, t);
4484 old = NULL_TREE;
4486 else if (is_overloaded_fn (old))
4488 tree tmp;
4490 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4492 tree fn = OVL_CURRENT (tmp);
4494 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4495 && !(flags & PUSH_USING)
4496 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4497 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4498 error ("`%#D' conflicts with previous using declaration `%#D'",
4499 decl, fn);
4501 if (duplicate_decls (decl, fn))
4502 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4505 else if (old == error_mark_node)
4506 /* Ignore the undefined symbol marker. */
4507 old = NULL_TREE;
4508 else
4510 cp_error_at ("previous non-function declaration `%#D'", old);
4511 error ("conflicts with function declaration `%#D'", decl);
4512 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4516 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4518 if (old && TREE_CODE (old) != OVERLOAD)
4519 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4520 else
4521 new_binding = ovl_cons (decl, old);
4522 if (flags & PUSH_USING)
4523 OVL_USED (new_binding) = 1;
4525 else
4526 /* NAME is not ambiguous. */
4527 new_binding = decl;
4529 if (doing_global)
4530 set_namespace_binding (name, current_namespace, new_binding);
4531 else
4533 /* We only create an OVERLOAD if there was a previous binding at
4534 this level, or if decl is a template. In the former case, we
4535 need to remove the old binding and replace it with the new
4536 binding. We must also run through the NAMES on the binding
4537 level where the name was bound to update the chain. */
4539 if (TREE_CODE (new_binding) == OVERLOAD && old)
4541 tree *d;
4543 for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4545 d = &TREE_CHAIN (*d))
4546 if (*d == old
4547 || (TREE_CODE (*d) == TREE_LIST
4548 && TREE_VALUE (*d) == old))
4550 if (TREE_CODE (*d) == TREE_LIST)
4551 /* Just replace the old binding with the new. */
4552 TREE_VALUE (*d) = new_binding;
4553 else
4554 /* Build a TREE_LIST to wrap the OVERLOAD. */
4555 *d = tree_cons (NULL_TREE, new_binding,
4556 TREE_CHAIN (*d));
4558 /* And update the cxx_binding node. */
4559 BINDING_VALUE (IDENTIFIER_BINDING (name))
4560 = new_binding;
4561 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4564 /* We should always find a previous binding in this case. */
4565 abort ();
4568 /* Install the new binding. */
4569 push_local_binding (name, new_binding, flags);
4572 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4575 /* Generate an implicit declaration for identifier FUNCTIONID
4576 as a function of type int (). Print a warning if appropriate. */
4578 tree
4579 implicitly_declare (tree functionid)
4581 register tree decl;
4583 /* We used to reuse an old implicit decl here,
4584 but this loses with inline functions because it can clobber
4585 the saved decl chains. */
4586 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4588 DECL_EXTERNAL (decl) = 1;
4589 TREE_PUBLIC (decl) = 1;
4591 /* ISO standard says implicit declarations are in the innermost block.
4592 So we record the decl in the standard fashion. */
4593 pushdecl (decl);
4594 rest_of_decl_compilation (decl, NULL, 0, 0);
4596 if (warn_implicit
4597 /* Only one warning per identifier. */
4598 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4600 pedwarn ("implicit declaration of function `%#D'", decl);
4603 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4605 return decl;
4608 /* Return zero if the declaration NEWDECL is valid
4609 when the declaration OLDDECL (assumed to be for the same name)
4610 has already been seen.
4611 Otherwise return an error message format string with a %s
4612 where the identifier should go. */
4614 static const char *
4615 redeclaration_error_message (tree newdecl, tree olddecl)
4617 if (TREE_CODE (newdecl) == TYPE_DECL)
4619 /* Because C++ can put things into name space for free,
4620 constructs like "typedef struct foo { ... } foo"
4621 would look like an erroneous redeclaration. */
4622 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4623 return 0;
4624 else
4625 return "redefinition of `%#D'";
4627 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4629 /* If this is a pure function, its olddecl will actually be
4630 the original initialization to `0' (which we force to call
4631 abort()). Don't complain about redefinition in this case. */
4632 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4633 return 0;
4635 /* If both functions come from different namespaces, this is not
4636 a redeclaration - this is a conflict with a used function. */
4637 if (DECL_NAMESPACE_SCOPE_P (olddecl)
4638 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4639 return "`%D' conflicts with used function";
4641 /* We'll complain about linkage mismatches in
4642 warn_extern_redeclared_static. */
4644 /* Defining the same name twice is no good. */
4645 if (DECL_INITIAL (olddecl) != NULL_TREE
4646 && DECL_INITIAL (newdecl) != NULL_TREE)
4648 if (DECL_NAME (olddecl) == NULL_TREE)
4649 return "`%#D' not declared in class";
4650 else
4651 return "redefinition of `%#D'";
4653 return 0;
4655 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4657 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4658 && (DECL_TEMPLATE_RESULT (newdecl)
4659 != DECL_TEMPLATE_RESULT (olddecl))
4660 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4661 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4662 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4663 && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4664 && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4665 return "redefinition of `%#D'";
4666 return 0;
4668 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4670 /* Objects declared at top level: */
4671 /* If at least one is a reference, it's ok. */
4672 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4673 return 0;
4674 /* Reject two definitions. */
4675 return "redefinition of `%#D'";
4677 else
4679 /* Objects declared with block scope: */
4680 /* Reject two definitions, and reject a definition
4681 together with an external reference. */
4682 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4683 return "redeclaration of `%#D'";
4684 return 0;
4688 /* Create a new label, named ID. */
4690 static tree
4691 make_label_decl (tree id, int local_p)
4693 tree decl;
4695 decl = build_decl (LABEL_DECL, id, void_type_node);
4696 if (expanding_p)
4697 /* Make sure every label has an rtx. */
4698 label_rtx (decl);
4700 DECL_CONTEXT (decl) = current_function_decl;
4701 DECL_MODE (decl) = VOIDmode;
4702 C_DECLARED_LABEL_FLAG (decl) = local_p;
4704 /* Say where one reference is to the label, for the sake of the
4705 error if it is not defined. */
4706 DECL_SOURCE_LINE (decl) = lineno;
4707 DECL_SOURCE_FILE (decl) = input_filename;
4709 /* Record the fact that this identifier is bound to this label. */
4710 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4712 return decl;
4715 /* Record this label on the list of used labels so that we can check
4716 at the end of the function to see whether or not the label was
4717 actually defined, and so we can check when the label is defined whether
4718 this use is valid. */
4720 static void
4721 use_label (tree decl)
4723 if (named_label_uses == NULL
4724 || named_label_uses->names_in_scope != current_binding_level->names
4725 || named_label_uses->label_decl != decl)
4727 struct named_label_use_list *new_ent;
4728 new_ent = ((struct named_label_use_list *)
4729 ggc_alloc (sizeof (struct named_label_use_list)));
4730 new_ent->label_decl = decl;
4731 new_ent->names_in_scope = current_binding_level->names;
4732 new_ent->binding_level = current_binding_level;
4733 new_ent->lineno_o_goto = lineno;
4734 new_ent->filename_o_goto = input_filename;
4735 new_ent->next = named_label_uses;
4736 named_label_uses = new_ent;
4740 /* Look for a label named ID in the current function. If one cannot
4741 be found, create one. (We keep track of used, but undefined,
4742 labels, and complain about them at the end of a function.) */
4744 tree
4745 lookup_label (tree id)
4747 tree decl;
4748 struct named_label_list *ent;
4750 timevar_push (TV_NAME_LOOKUP);
4751 /* You can't use labels at global scope. */
4752 if (current_function_decl == NULL_TREE)
4754 error ("label `%s' referenced outside of any function",
4755 IDENTIFIER_POINTER (id));
4756 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4759 /* See if we've already got this label. */
4760 decl = IDENTIFIER_LABEL_VALUE (id);
4761 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4762 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4764 /* Record this label on the list of labels used in this function.
4765 We do this before calling make_label_decl so that we get the
4766 IDENTIFIER_LABEL_VALUE before the new label is declared. */
4767 ent = ((struct named_label_list *)
4768 ggc_alloc_cleared (sizeof (struct named_label_list)));
4769 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4770 ent->next = named_labels;
4771 named_labels = ent;
4773 /* We need a new label. */
4774 decl = make_label_decl (id, /*local_p=*/0);
4776 /* Now fill in the information we didn't have before. */
4777 ent->label_decl = decl;
4779 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4782 /* Declare a local label named ID. */
4784 tree
4785 declare_local_label (tree id)
4787 tree decl;
4789 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4790 this scope we can restore the old value of
4791 IDENTIFIER_TYPE_VALUE. */
4792 current_binding_level->shadowed_labels
4793 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4794 current_binding_level->shadowed_labels);
4795 /* Look for the label. */
4796 decl = make_label_decl (id, /*local_p=*/1);
4797 /* Now fill in the information we didn't have before. */
4798 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4800 return decl;
4803 /* Returns nonzero if it is ill-formed to jump past the declaration of
4804 DECL. Returns 2 if it's also a real problem. */
4806 static int
4807 decl_jump_unsafe (tree decl)
4809 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4810 return 0;
4812 if (DECL_INITIAL (decl) == NULL_TREE
4813 && pod_type_p (TREE_TYPE (decl)))
4814 return 0;
4816 /* This is really only important if we're crossing an initialization.
4817 The POD stuff is just pedantry; why should it matter if the class
4818 contains a field of pointer to member type? */
4819 if (DECL_INITIAL (decl)
4820 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4821 return 2;
4822 return 1;
4825 /* Check that a single previously seen jump to a newly defined label
4826 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4827 the jump context; NAMES are the names in scope in LEVEL at the jump
4828 context; FILE and LINE are the source position of the jump or 0. */
4830 static void
4831 check_previous_goto_1 (tree decl,
4832 struct cp_binding_level* level,
4833 tree names,
4834 const char* file,
4835 int line)
4837 int identified = 0;
4838 int saw_eh = 0;
4839 struct cp_binding_level *b = current_binding_level;
4840 for (; b; b = b->level_chain)
4842 tree new_decls = b->names;
4843 tree old_decls = (b == level ? names : NULL_TREE);
4844 for (; new_decls != old_decls;
4845 new_decls = TREE_CHAIN (new_decls))
4847 int problem = decl_jump_unsafe (new_decls);
4848 if (! problem)
4849 continue;
4851 if (! identified)
4853 if (decl)
4854 pedwarn ("jump to label `%D'", decl);
4855 else
4856 pedwarn ("jump to case label");
4858 if (file)
4859 pedwarn_with_file_and_line (file, line, " from here");
4860 identified = 1;
4863 if (problem > 1)
4864 cp_error_at (" crosses initialization of `%#D'",
4865 new_decls);
4866 else
4867 cp_pedwarn_at (" enters scope of non-POD `%#D'",
4868 new_decls);
4871 if (b == level)
4872 break;
4873 if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
4875 if (! identified)
4877 if (decl)
4878 pedwarn ("jump to label `%D'", decl);
4879 else
4880 pedwarn ("jump to case label");
4882 if (file)
4883 pedwarn_with_file_and_line (file, line, " from here");
4884 identified = 1;
4886 if (b->is_try_scope)
4887 error (" enters try block");
4888 else
4889 error (" enters catch block");
4890 saw_eh = 1;
4895 static void
4896 check_previous_goto (struct named_label_use_list* use)
4898 check_previous_goto_1 (use->label_decl, use->binding_level,
4899 use->names_in_scope, use->filename_o_goto,
4900 use->lineno_o_goto);
4903 static void
4904 check_switch_goto (struct cp_binding_level* level)
4906 check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0);
4909 /* Check that any previously seen jumps to a newly defined label DECL
4910 are OK. Called by define_label. */
4912 static void
4913 check_previous_gotos (tree decl)
4915 struct named_label_use_list **usep;
4917 if (! TREE_USED (decl))
4918 return;
4920 for (usep = &named_label_uses; *usep; )
4922 struct named_label_use_list *use = *usep;
4923 if (use->label_decl == decl)
4925 check_previous_goto (use);
4926 *usep = use->next;
4928 else
4929 usep = &(use->next);
4933 /* Check that a new jump to a label DECL is OK. Called by
4934 finish_goto_stmt. */
4936 void
4937 check_goto (tree decl)
4939 int identified = 0;
4940 tree bad;
4941 struct named_label_list *lab;
4943 /* We can't know where a computed goto is jumping. So we assume
4944 that it's OK. */
4945 if (! DECL_P (decl))
4946 return;
4948 /* If the label hasn't been defined yet, defer checking. */
4949 if (! DECL_INITIAL (decl))
4951 use_label (decl);
4952 return;
4955 for (lab = named_labels; lab; lab = lab->next)
4956 if (decl == lab->label_decl)
4957 break;
4959 /* If the label is not on named_labels it's a gcc local label, so
4960 it must be in an outer scope, so jumping to it is always OK. */
4961 if (lab == 0)
4962 return;
4964 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4965 && !identified)
4967 cp_pedwarn_at ("jump to label `%D'", decl);
4968 pedwarn (" from here");
4969 identified = 1;
4972 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4974 tree b = TREE_VALUE (bad);
4975 int u = decl_jump_unsafe (b);
4977 if (u > 1 && DECL_ARTIFICIAL (b))
4978 /* Can't skip init of __exception_info. */
4979 cp_error_at (" enters catch block", b);
4980 else if (u > 1)
4981 cp_error_at (" skips initialization of `%#D'", b);
4982 else
4983 cp_pedwarn_at (" enters scope of non-POD `%#D'", b);
4986 if (lab->in_try_scope)
4987 error (" enters try block");
4988 else if (lab->in_catch_scope)
4989 error (" enters catch block");
4992 /* Define a label, specifying the location in the source file.
4993 Return the LABEL_DECL node for the label, if the definition is valid.
4994 Otherwise return 0. */
4996 tree
4997 define_label (const char* filename, int line, tree name)
4999 tree decl = lookup_label (name);
5000 struct named_label_list *ent;
5001 register struct cp_binding_level *p;
5003 timevar_push (TV_NAME_LOOKUP);
5004 for (ent = named_labels; ent; ent = ent->next)
5005 if (ent->label_decl == decl)
5006 break;
5008 /* After labels, make any new cleanups in the function go into their
5009 own new (temporary) binding contour. */
5010 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5011 p->more_cleanups_ok = 0;
5013 if (name == get_identifier ("wchar_t"))
5014 pedwarn ("label named wchar_t");
5016 if (DECL_INITIAL (decl) != NULL_TREE)
5018 error ("duplicate label `%D'", decl);
5019 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5021 else
5023 /* Mark label as having been defined. */
5024 DECL_INITIAL (decl) = error_mark_node;
5025 /* Say where in the source. */
5026 DECL_SOURCE_FILE (decl) = filename;
5027 DECL_SOURCE_LINE (decl) = line;
5028 if (ent)
5030 ent->names_in_scope = current_binding_level->names;
5031 ent->binding_level = current_binding_level;
5033 check_previous_gotos (decl);
5034 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5036 timevar_pop (TV_NAME_LOOKUP);
5039 struct cp_switch
5041 struct cp_binding_level *level;
5042 struct cp_switch *next;
5043 /* The SWITCH_STMT being built. */
5044 tree switch_stmt;
5045 /* A splay-tree mapping the low element of a case range to the high
5046 element, or NULL_TREE if there is no high element. Used to
5047 determine whether or not a new case label duplicates an old case
5048 label. We need a tree, rather than simply a hash table, because
5049 of the GNU case range extension. */
5050 splay_tree cases;
5053 /* A stack of the currently active switch statements. The innermost
5054 switch statement is on the top of the stack. There is no need to
5055 mark the stack for garbage collection because it is only active
5056 during the processing of the body of a function, and we never
5057 collect at that point. */
5059 static struct cp_switch *switch_stack;
5061 /* Called right after a switch-statement condition is parsed.
5062 SWITCH_STMT is the switch statement being parsed. */
5064 void
5065 push_switch (tree switch_stmt)
5067 struct cp_switch *p
5068 = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
5069 p->level = current_binding_level;
5070 p->next = switch_stack;
5071 p->switch_stmt = switch_stmt;
5072 p->cases = splay_tree_new (case_compare, NULL, NULL);
5073 switch_stack = p;
5076 void
5077 pop_switch (void)
5079 struct cp_switch *cs;
5081 cs = switch_stack;
5082 splay_tree_delete (cs->cases);
5083 switch_stack = switch_stack->next;
5084 free (cs);
5087 /* Note that we've seen a definition of a case label, and complain if this
5088 is a bad place for one. */
5090 tree
5091 finish_case_label (tree low_value, tree high_value)
5093 tree cond, r;
5094 register struct cp_binding_level *p;
5096 if (! switch_stack)
5098 if (high_value)
5099 error ("case label not within a switch statement");
5100 else if (low_value)
5101 error ("case label `%E' not within a switch statement",
5102 low_value);
5103 else
5104 error ("`default' label not within a switch statement");
5105 return NULL_TREE;
5108 if (processing_template_decl)
5110 tree label;
5112 /* For templates, just add the case label; we'll do semantic
5113 analysis at instantiation-time. */
5114 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5115 return add_stmt (build_case_label (low_value, high_value, label));
5118 /* Find the condition on which this switch statement depends. */
5119 cond = SWITCH_COND (switch_stack->switch_stmt);
5120 if (cond && TREE_CODE (cond) == TREE_LIST)
5121 cond = TREE_VALUE (cond);
5123 r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5125 check_switch_goto (switch_stack->level);
5127 /* After labels, make any new cleanups in the function go into their
5128 own new (temporary) binding contour. */
5129 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5130 p->more_cleanups_ok = 0;
5132 return r;
5135 /* Return the list of declarations of the current level.
5136 Note that this list is in reverse order unless/until
5137 you nreverse it; and when you do nreverse it, you must
5138 store the result back using `storedecls' or you will lose. */
5140 tree
5141 getdecls (void)
5143 return current_binding_level->names;
5146 /* Return the list of type-tags (for structs, etc) of the current level. */
5148 tree
5149 gettags (void)
5151 return current_binding_level->tags;
5154 /* Store the list of declarations of the current level.
5155 This is done for the parameter declarations of a function being defined,
5156 after they are modified in the light of any missing parameters. */
5158 static void
5159 storedecls (tree decls)
5161 current_binding_level->names = decls;
5164 /* Similarly, store the list of tags of the current level. */
5166 void
5167 storetags (tree tags)
5169 current_binding_level->tags = tags;
5172 /* Return the type that should be used when TYPE's name is preceded
5173 by a tag such as 'struct' or 'union', or null if the name cannot
5174 be used in this way.
5176 For example, when processing the third line of:
5178 struct A;
5179 typedef struct A A;
5180 struct A;
5182 lookup of A will find the typedef. Given A's typedef, this function
5183 will return the type associated with "struct A". For the tag to be
5184 anything other than TYPE, TYPE must be a typedef whose original type
5185 has the same name and context as TYPE itself.
5187 It is not valid for a typedef of an anonymous type to be used with
5188 an explicit tag:
5190 typedef struct { ... } B;
5191 struct B;
5193 Return null for this case. */
5195 static tree
5196 follow_tag_typedef (tree type)
5198 tree original;
5200 original = original_type (type);
5201 if (! TYPE_NAME (original))
5202 return NULL_TREE;
5203 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5204 && (CP_DECL_CONTEXT (TYPE_NAME (original))
5205 == CP_DECL_CONTEXT (TYPE_NAME (type)))
5206 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5207 return original;
5208 else
5209 return NULL_TREE;
5212 /* Given NAME, an IDENTIFIER_NODE,
5213 return the structure (or union or enum) definition for that name.
5214 Searches binding levels from BINDING_LEVEL up to the global level.
5215 If THISLEVEL_ONLY is nonzero, searches only the specified context
5216 (but skips any tag-transparent contexts to find one that is
5217 meaningful for tags).
5218 FORM says which kind of type the caller wants;
5219 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5220 If the wrong kind of type is found, and it's not a template, an error is
5221 reported. */
5223 static tree
5224 lookup_tag (enum tree_code form, tree name,
5225 struct cp_binding_level* binding_level, int thislevel_only)
5227 register struct cp_binding_level *level;
5228 /* Nonzero if, we should look past a template parameter level, even
5229 if THISLEVEL_ONLY. */
5230 int allow_template_parms_p = 1;
5232 timevar_push (TV_NAME_LOOKUP);
5233 for (level = binding_level; level; level = level->level_chain)
5235 register tree tail;
5236 if (ANON_AGGRNAME_P (name))
5237 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5239 /* There's no need for error checking here, because
5240 anon names are unique throughout the compilation. */
5241 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5242 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5244 else if (level->namespace_p)
5245 /* Do namespace lookup. */
5246 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5248 cxx_binding *binding =
5249 cxx_scope_find_binding_for_name (tail, name);
5250 tree old;
5252 /* If we just skipped past a template parameter level,
5253 even though THISLEVEL_ONLY, and we find a template
5254 class declaration, then we use the _TYPE node for the
5255 template. See the example below. */
5256 if (thislevel_only && !allow_template_parms_p
5257 && binding && BINDING_VALUE (binding)
5258 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (binding)))
5259 old = TREE_TYPE (BINDING_VALUE (binding));
5260 else if (binding)
5261 old = BINDING_TYPE (binding);
5262 else
5263 old = NULL_TREE;
5265 if (old)
5267 /* We've found something at this binding level. If it is
5268 a typedef, extract the tag it refers to. Lookup fails
5269 if the typedef doesn't refer to a taggable type. */
5270 old = follow_tag_typedef (old);
5271 if (!old)
5272 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5273 if (TREE_CODE (old) != form
5274 && (form == ENUMERAL_TYPE
5275 || TREE_CODE (old) == ENUMERAL_TYPE))
5277 error ("`%#D' redeclared as %C", old, form);
5278 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5280 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
5282 if (thislevel_only || tail == global_namespace)
5283 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5285 else
5286 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5288 if (TREE_PURPOSE (tail) == name)
5290 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5292 if (code != form
5293 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5295 /* Definition isn't the kind we were looking for. */
5296 error ("`%#D' redeclared as %C", TREE_VALUE (tail), form);
5297 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5299 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5302 if (thislevel_only && ! level->tag_transparent)
5304 if (level->template_parms_p && allow_template_parms_p)
5306 /* We must deal with cases like this:
5308 template <class T> struct S;
5309 template <class T> struct S {};
5311 When looking up `S', for the second declaration, we
5312 would like to find the first declaration. But, we
5313 are in the pseudo-global level created for the
5314 template parameters, rather than the (surrounding)
5315 namespace level. Thus, we keep going one more level,
5316 even though THISLEVEL_ONLY is nonzero. */
5317 allow_template_parms_p = 0;
5318 continue;
5320 else
5321 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5324 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5327 /* Given a type, find the tag that was defined for it and return the tag name.
5328 Otherwise return 0. However, the value can never be 0
5329 in the cases in which this is used.
5331 C++: If NAME is nonzero, this is the new name to install. This is
5332 done when replacing anonymous tags with real tag names. */
5334 static tree
5335 lookup_tag_reverse (tree type, tree name)
5337 register struct cp_binding_level *level;
5339 timevar_push (TV_NAME_LOOKUP);
5340 for (level = current_binding_level; level; level = level->level_chain)
5342 register tree tail;
5343 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5345 if (TREE_VALUE (tail) == type)
5347 if (name)
5348 TREE_PURPOSE (tail) = name;
5349 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_PURPOSE (tail));
5353 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5356 /* Look up NAME in the NAMESPACE. */
5358 tree
5359 lookup_namespace_name (tree namespace, tree name)
5361 tree val;
5362 tree template_id = NULL_TREE;
5363 cxx_binding binding;
5365 timevar_push (TV_NAME_LOOKUP);
5366 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5368 if (TREE_CODE (name) == NAMESPACE_DECL)
5369 /* This happens for A::B<int> when B is a namespace. */
5370 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5371 else if (TREE_CODE (name) == TEMPLATE_DECL)
5373 /* This happens for A::B where B is a template, and there are no
5374 template arguments. */
5375 error ("invalid use of `%D'", name);
5376 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5379 namespace = ORIGINAL_NAMESPACE (namespace);
5381 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5383 template_id = name;
5384 name = TREE_OPERAND (name, 0);
5385 if (TREE_CODE (name) == OVERLOAD)
5386 name = DECL_NAME (OVL_CURRENT (name));
5387 else if (DECL_P (name))
5388 name = DECL_NAME (name);
5391 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5393 cxx_binding_clear (&binding);
5394 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
5395 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5397 if (binding.value)
5399 val = binding.value;
5401 if (template_id)
5403 if (DECL_CLASS_TEMPLATE_P (val))
5404 val = lookup_template_class (val,
5405 TREE_OPERAND (template_id, 1),
5406 /*in_decl=*/NULL_TREE,
5407 /*context=*/NULL_TREE,
5408 /*entering_scope=*/0,
5409 tf_error | tf_warning);
5410 else if (DECL_FUNCTION_TEMPLATE_P (val)
5411 || TREE_CODE (val) == OVERLOAD)
5412 val = lookup_template_function (val,
5413 TREE_OPERAND (template_id, 1));
5414 else
5416 error ("`%D::%D' is not a template",
5417 namespace, name);
5418 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5422 /* If we have a single function from a using decl, pull it out. */
5423 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5424 val = OVL_FUNCTION (val);
5426 /* Ignore built-in functions that haven't been prototyped yet. */
5427 if (!val || !DECL_P(val)
5428 || !DECL_LANG_SPECIFIC(val)
5429 || !DECL_ANTICIPATED (val))
5430 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5433 error ("`%D' undeclared in namespace `%D'", name, namespace);
5434 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5437 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5439 static hashval_t
5440 typename_hash (const void* k)
5442 hashval_t hash;
5443 tree t = (tree) k;
5445 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5446 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5448 return hash;
5451 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5453 static int
5454 typename_compare (const void * k1, const void * k2)
5456 tree t1;
5457 tree t2;
5458 tree d1;
5459 tree d2;
5461 t1 = (tree) k1;
5462 t2 = (tree) k2;
5463 d1 = TYPE_NAME (t1);
5464 d2 = TYPE_NAME (t2);
5466 return (DECL_NAME (d1) == DECL_NAME (d2)
5467 && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5468 && ((TREE_TYPE (t1) != NULL_TREE)
5469 == (TREE_TYPE (t2) != NULL_TREE))
5470 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5471 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5474 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5475 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5476 is non-NULL, this type is being created by the implicit typename
5477 extension, and BASE_TYPE is a type named `t' in some base class of
5478 `T' which depends on template parameters.
5480 Returns the new TYPENAME_TYPE. */
5482 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5484 tree
5485 build_typename_type (tree context, tree name, tree fullname)
5487 tree t;
5488 tree d;
5489 PTR *e;
5491 if (typename_htab == NULL)
5493 typename_htab = htab_create_ggc (61, &typename_hash,
5494 &typename_compare, NULL);
5497 /* Build the TYPENAME_TYPE. */
5498 t = make_aggr_type (TYPENAME_TYPE);
5499 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5500 TYPENAME_TYPE_FULLNAME (t) = fullname;
5502 /* Build the corresponding TYPE_DECL. */
5503 d = build_decl (TYPE_DECL, name, t);
5504 TYPE_NAME (TREE_TYPE (d)) = d;
5505 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5506 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5507 DECL_ARTIFICIAL (d) = 1;
5509 /* See if we already have this type. */
5510 e = htab_find_slot (typename_htab, t, INSERT);
5511 if (*e)
5512 t = (tree) *e;
5513 else
5514 *e = t;
5516 return t;
5519 /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type,
5520 unless an error occurs, in which case error_mark_node is returned.
5521 If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5522 set, we return that, rather than the _TYPE it corresponds to, in
5523 other cases we look through the type decl. If TF_ERROR is set,
5524 complain about errors, otherwise be quiet. */
5526 tree
5527 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5529 tree fullname;
5531 if (TYPE_P (name))
5533 if (!(TYPE_LANG_SPECIFIC (name)
5534 && (CLASSTYPE_IS_TEMPLATE (name)
5535 || CLASSTYPE_USE_TEMPLATE (name))))
5536 name = TYPE_IDENTIFIER (name);
5537 else
5538 /* Create a TEMPLATE_ID_EXPR for the type. */
5539 name = build_nt (TEMPLATE_ID_EXPR,
5540 CLASSTYPE_TI_TEMPLATE (name),
5541 CLASSTYPE_TI_ARGS (name));
5543 else if (TREE_CODE (name) == TYPE_DECL)
5544 name = DECL_NAME (name);
5546 fullname = name;
5548 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5550 name = TREE_OPERAND (name, 0);
5551 if (TREE_CODE (name) == TEMPLATE_DECL)
5552 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5554 if (TREE_CODE (name) == TEMPLATE_DECL)
5556 error ("`%D' used without template parameters", name);
5557 return error_mark_node;
5559 if (TREE_CODE (name) != IDENTIFIER_NODE)
5560 abort ();
5562 if (TREE_CODE (context) == NAMESPACE_DECL)
5564 /* We can get here from typename_sub0 in the explicit_template_type
5565 expansion. Just fail. */
5566 if (complain & tf_error)
5567 error ("no class template named `%#T' in `%#T'",
5568 name, context);
5569 return error_mark_node;
5572 if (! uses_template_parms (context)
5573 || currently_open_class (context))
5575 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5577 tree tmpl = NULL_TREE;
5578 if (IS_AGGR_TYPE (context))
5579 tmpl = lookup_field (context, name, 0, false);
5580 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5582 if (complain & tf_error)
5583 error ("no class template named `%#T' in `%#T'",
5584 name, context);
5585 return error_mark_node;
5588 if (complain & tf_error)
5590 if (complain & tf_parsing)
5591 perform_or_defer_access_check (context, tmpl);
5592 else
5593 enforce_access (context, tmpl);
5596 return lookup_template_class (tmpl,
5597 TREE_OPERAND (fullname, 1),
5598 NULL_TREE, context,
5599 /*entering_scope=*/0,
5600 tf_error | tf_warning);
5602 else
5604 tree t;
5606 if (!IS_AGGR_TYPE (context))
5608 if (complain & tf_error)
5609 error ("no type named `%#T' in `%#T'", name, context);
5610 return error_mark_node;
5613 t = lookup_field (context, name, 0, true);
5614 if (t)
5616 if (TREE_CODE (t) != TYPE_DECL)
5618 if (complain & tf_error)
5619 error ("no type named `%#T' in `%#T'", name, context);
5620 return error_mark_node;
5623 if (complain & tf_error)
5625 if (complain & tf_parsing)
5626 perform_or_defer_access_check (context, t);
5627 else
5628 enforce_access (context, t);
5631 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5632 t = TREE_TYPE (t);
5634 return t;
5639 /* If the CONTEXT is not a template type, then either the field is
5640 there now or its never going to be. */
5641 if (!uses_template_parms (context))
5643 if (complain & tf_error)
5644 error ("no type named `%#T' in `%#T'", name, context);
5645 return error_mark_node;
5648 return build_typename_type (context, name, fullname);
5651 /* Resolve `CONTEXT::template NAME'. Returns an appropriate type,
5652 unless an error occurs, in which case error_mark_node is returned.
5653 If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5654 corresponds to. If COMPLAIN zero, don't complain about any errors
5655 that occur. */
5657 tree
5658 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5660 tree t;
5661 tree d;
5663 if (TYPE_P (name))
5664 name = TYPE_IDENTIFIER (name);
5665 else if (DECL_P (name))
5666 name = DECL_NAME (name);
5667 if (TREE_CODE (name) != IDENTIFIER_NODE)
5668 abort ();
5670 if (!uses_template_parms (context)
5671 || currently_open_class (context))
5673 tree tmpl = NULL_TREE;
5675 if (IS_AGGR_TYPE (context))
5676 tmpl = lookup_field (context, name, 0, false);
5678 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5680 if (complain & tf_error)
5681 error ("no class template named `%#T' in `%#T'", name, context);
5682 return error_mark_node;
5685 if (complain & tf_error)
5687 if (complain & tf_parsing)
5688 perform_or_defer_access_check (context, tmpl);
5689 else
5690 enforce_access (context, tmpl);
5693 return tmpl;
5696 /* Build the UNBOUND_CLASS_TEMPLATE. */
5697 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5698 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5699 TREE_TYPE (t) = NULL_TREE;
5701 /* Build the corresponding TEMPLATE_DECL. */
5702 d = build_decl (TEMPLATE_DECL, name, t);
5703 TYPE_NAME (TREE_TYPE (d)) = d;
5704 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5705 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5706 DECL_ARTIFICIAL (d) = 1;
5708 return t;
5711 /* Select the right _DECL from multiple choices. */
5713 static tree
5714 select_decl (cxx_binding *binding, int flags)
5716 tree val;
5717 val = BINDING_VALUE (binding);
5719 timevar_push (TV_NAME_LOOKUP);
5720 if (LOOKUP_NAMESPACES_ONLY (flags))
5722 /* We are not interested in types. */
5723 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5724 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5725 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5728 /* If we could have a type and
5729 we have nothing or we need a type and have none. */
5730 if (BINDING_TYPE (binding)
5731 && (!val || ((flags & LOOKUP_PREFER_TYPES)
5732 && TREE_CODE (val) != TYPE_DECL)))
5733 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5734 /* Don't return non-types if we really prefer types. */
5735 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
5736 && (TREE_CODE (val) != TEMPLATE_DECL
5737 || !DECL_CLASS_TEMPLATE_P (val)))
5738 val = NULL_TREE;
5740 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5743 /* Unscoped lookup of a global: iterate over current namespaces,
5744 considering using-directives. If SPACESP is non-NULL, store a list
5745 of the namespaces we've considered in it. */
5747 tree
5748 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5750 tree initial = current_decl_namespace ();
5751 tree scope = initial;
5752 tree siter;
5753 struct cp_binding_level *level;
5754 tree val = NULL_TREE;
5755 cxx_binding binding;
5757 timevar_push (TV_NAME_LOOKUP);
5758 cxx_binding_clear (&binding);
5759 if (spacesp)
5760 *spacesp = NULL_TREE;
5762 for (; !val; scope = CP_DECL_CONTEXT (scope))
5764 cxx_binding *b = cxx_scope_find_binding_for_name (scope, name);
5765 if (spacesp)
5766 *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5768 /* Ignore anticipated built-in functions. */
5769 if (b && BINDING_VALUE (b)
5770 && DECL_P (BINDING_VALUE (b))
5771 && DECL_LANG_SPECIFIC (BINDING_VALUE (b))
5772 && DECL_ANTICIPATED (BINDING_VALUE (b)))
5773 /* Keep binding cleared. */;
5774 else if (b)
5776 /* Initialize binding for this context. */
5777 binding.value = BINDING_VALUE (b);
5778 binding.type = BINDING_TYPE (b);
5781 /* Add all _DECLs seen through local using-directives. */
5782 for (level = current_binding_level;
5783 !level->namespace_p;
5784 level = level->level_chain)
5785 if (!lookup_using_namespace (name, &binding, level->using_directives,
5786 scope, flags, spacesp))
5787 /* Give up because of error. */
5788 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5790 /* Add all _DECLs seen through global using-directives. */
5791 /* XXX local and global using lists should work equally. */
5792 siter = initial;
5793 while (1)
5795 if (!lookup_using_namespace (name, &binding,
5796 DECL_NAMESPACE_USING (siter),
5797 scope, flags, spacesp))
5798 /* Give up because of error. */
5799 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5800 if (siter == scope) break;
5801 siter = CP_DECL_CONTEXT (siter);
5804 val = select_decl (&binding, flags);
5805 if (scope == global_namespace)
5806 break;
5808 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5811 /* Combine prefer_type and namespaces_only into flags. */
5813 static int
5814 lookup_flags (int prefer_type, int namespaces_only)
5816 if (namespaces_only)
5817 return LOOKUP_PREFER_NAMESPACES;
5818 if (prefer_type > 1)
5819 return LOOKUP_PREFER_TYPES;
5820 if (prefer_type > 0)
5821 return LOOKUP_PREFER_BOTH;
5822 return 0;
5825 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5826 ignore it or not. Subroutine of lookup_name_real. */
5828 static tree
5829 qualify_lookup (tree val, int flags)
5831 if (val == NULL_TREE)
5832 return val;
5833 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5834 return val;
5835 if ((flags & LOOKUP_PREFER_TYPES) && TREE_CODE (val) == TYPE_DECL)
5836 return val;
5837 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5838 return NULL_TREE;
5839 return val;
5842 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5843 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
5844 bindings.
5846 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5847 declaration found. */
5849 tree
5850 lookup_qualified_name (tree scope, tree name, bool is_type_p, int flags)
5852 if (TREE_CODE (scope) == NAMESPACE_DECL)
5854 cxx_binding binding;
5856 cxx_binding_clear (&binding);
5857 flags |= LOOKUP_COMPLAIN;
5858 if (is_type_p)
5859 flags |= LOOKUP_PREFER_TYPES;
5860 if (!qualified_lookup_using_namespace (name, scope, &binding, flags))
5861 return NULL_TREE;
5862 return select_decl (&binding, flags);
5864 else
5865 return lookup_member (scope, name, 0, is_type_p);
5868 /* Check to see whether or not DECL is a variable that would have been
5869 in scope under the ARM, but is not in scope under the ANSI/ISO
5870 standard. If so, issue an error message. If name lookup would
5871 work in both cases, but return a different result, this function
5872 returns the result of ANSI/ISO lookup. Otherwise, it returns
5873 DECL. */
5875 tree
5876 check_for_out_of_scope_variable (tree decl)
5878 tree shadowed;
5880 /* We only care about out of scope variables. */
5881 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5882 return decl;
5884 shadowed = DECL_SHADOWED_FOR_VAR (decl);
5885 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5886 && DECL_DEAD_FOR_LOCAL (shadowed))
5887 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5888 if (!shadowed)
5889 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5890 if (shadowed)
5892 if (!DECL_ERROR_REPORTED (decl))
5894 warning ("name lookup of `%D' changed",
5895 DECL_NAME (decl));
5896 cp_warning_at (" matches this `%D' under ISO standard rules",
5897 shadowed);
5898 cp_warning_at (" matches this `%D' under old rules", decl);
5899 DECL_ERROR_REPORTED (decl) = 1;
5901 return shadowed;
5904 /* If we have already complained about this declaration, there's no
5905 need to do it again. */
5906 if (DECL_ERROR_REPORTED (decl))
5907 return decl;
5909 DECL_ERROR_REPORTED (decl) = 1;
5910 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5912 error ("name lookup of `%D' changed for new ISO `for' scoping",
5913 DECL_NAME (decl));
5914 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl);
5915 return error_mark_node;
5917 else
5919 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5920 DECL_NAME (decl));
5921 cp_pedwarn_at (" using obsolete binding at `%D'", decl);
5924 return decl;
5927 /* Look up NAME in the current binding level and its superiors in the
5928 namespace of variables, functions and typedefs. Return a ..._DECL
5929 node of some kind representing its definition if there is only one
5930 such declaration, or return a TREE_LIST with all the overloaded
5931 definitions if there are many, or return 0 if it is undefined.
5933 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5934 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5935 Otherwise we prefer non-TYPE_DECLs.
5937 If NONCLASS is nonzero, we don't look for the NAME in class scope,
5938 using IDENTIFIER_CLASS_VALUE. */
5940 tree
5941 lookup_name_real (tree name, int prefer_type, int nonclass,
5942 int namespaces_only, int flags)
5944 cxx_binding *iter;
5945 tree val = NULL_TREE;
5947 timevar_push (TV_NAME_LOOKUP);
5948 /* Conversion operators are handled specially because ordinary
5949 unqualified name lookup will not find template conversion
5950 operators. */
5951 if (IDENTIFIER_TYPENAME_P (name))
5953 struct cp_binding_level *level;
5955 for (level = current_binding_level;
5956 level && !level->namespace_p;
5957 level = level->level_chain)
5959 tree class_type;
5960 tree operators;
5962 /* A conversion operator can only be declared in a class
5963 scope. */
5964 if (level->parm_flag != 2)
5965 continue;
5967 /* Lookup the conversion operator in the class. */
5968 class_type = level->this_class;
5969 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5970 if (operators)
5971 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
5974 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5977 flags |= lookup_flags (prefer_type, namespaces_only);
5979 /* First, look in non-namespace scopes. */
5981 if (current_class_type == NULL_TREE)
5982 nonclass = 1;
5984 for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
5986 tree binding;
5988 if (!LOCAL_BINDING_P (iter) && nonclass)
5989 /* We're not looking for class-scoped bindings, so keep going. */
5990 continue;
5992 /* If this is the kind of thing we're looking for, we're done. */
5993 if (qualify_lookup (BINDING_VALUE (iter), flags))
5994 binding = BINDING_VALUE (iter);
5995 else if ((flags & LOOKUP_PREFER_TYPES)
5996 && qualify_lookup (BINDING_TYPE (iter), flags))
5997 binding = BINDING_TYPE (iter);
5998 else
5999 binding = NULL_TREE;
6001 if (binding)
6003 val = binding;
6004 break;
6008 /* Now lookup in namespace scopes. */
6009 if (!val)
6011 tree t = unqualified_namespace_lookup (name, flags, 0);
6012 if (t)
6013 val = t;
6016 if (val)
6018 /* If we have a single function from a using decl, pull it out. */
6019 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
6020 val = OVL_FUNCTION (val);
6023 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
6026 tree
6027 lookup_name_nonclass (tree name)
6029 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
6032 tree
6033 lookup_function_nonclass (tree name, tree args)
6035 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
6038 tree
6039 lookup_name (tree name, int prefer_type)
6041 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
6044 /* Similar to `lookup_name' but look only in the innermost non-class
6045 binding level. */
6047 tree
6048 lookup_name_current_level (tree name)
6050 struct cp_binding_level *b;
6051 tree t = NULL_TREE;
6053 timevar_push (TV_NAME_LOOKUP);
6054 b = current_binding_level;
6055 while (b->parm_flag == 2)
6056 b = b->level_chain;
6058 if (b->namespace_p)
6060 t = IDENTIFIER_NAMESPACE_VALUE (name);
6062 /* extern "C" function() */
6063 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
6064 t = TREE_VALUE (t);
6066 else if (IDENTIFIER_BINDING (name)
6067 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
6069 while (1)
6071 if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
6072 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
6074 if (b->keep == 2)
6075 b = b->level_chain;
6076 else
6077 break;
6081 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6084 /* Like lookup_name_current_level, but for types. */
6086 tree
6087 lookup_type_current_level (tree name)
6089 register tree t = NULL_TREE;
6091 timevar_push (TV_NAME_LOOKUP);
6092 my_friendly_assert (! current_binding_level->namespace_p, 980716);
6094 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
6095 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
6097 struct cp_binding_level *b = current_binding_level;
6098 while (1)
6100 if (purpose_member (name, b->type_shadowed))
6101 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
6102 REAL_IDENTIFIER_TYPE_VALUE (name));
6103 if (b->keep == 2)
6104 b = b->level_chain;
6105 else
6106 break;
6110 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6114 /* Push the declarations of builtin types into the namespace.
6115 RID_INDEX is the index of the builtin type
6116 in the array RID_POINTERS. NAME is the name used when looking
6117 up the builtin type. TYPE is the _TYPE node for the builtin type. */
6119 void
6120 record_builtin_type (enum rid rid_index,
6121 const char* name,
6122 tree type)
6124 tree rname = NULL_TREE, tname = NULL_TREE;
6125 tree tdecl = NULL_TREE;
6127 if ((int) rid_index < (int) RID_MAX)
6128 rname = ridpointers[(int) rid_index];
6129 if (name)
6130 tname = get_identifier (name);
6132 if (tname)
6134 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6135 set_identifier_type_value (tname, NULL_TREE);
6136 if ((int) rid_index < (int) RID_MAX)
6137 /* Built-in types live in the global namespace. */
6138 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6140 if (rname != NULL_TREE)
6142 if (tname != NULL_TREE)
6144 set_identifier_type_value (rname, NULL_TREE);
6145 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6147 else
6149 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6150 set_identifier_type_value (rname, NULL_TREE);
6155 /* Record one of the standard Java types.
6156 * Declare it as having the given NAME.
6157 * If SIZE > 0, it is the size of one of the integral types;
6158 * otherwise it is the negative of the size of one of the other types. */
6160 static tree
6161 record_builtin_java_type (const char* name, int size)
6163 tree type, decl;
6164 if (size > 0)
6165 type = make_signed_type (size);
6166 else if (size > -32)
6167 { /* "__java_char" or ""__java_boolean". */
6168 type = make_unsigned_type (-size);
6169 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6171 else
6172 { /* "__java_float" or ""__java_double". */
6173 type = make_node (REAL_TYPE);
6174 TYPE_PRECISION (type) = - size;
6175 layout_type (type);
6177 record_builtin_type (RID_MAX, name, type);
6178 decl = TYPE_NAME (type);
6180 /* Suppress generate debug symbol entries for these types,
6181 since for normal C++ they are just clutter.
6182 However, push_lang_context undoes this if extern "Java" is seen. */
6183 DECL_IGNORED_P (decl) = 1;
6185 TYPE_FOR_JAVA (type) = 1;
6186 return type;
6189 /* Push a type into the namespace so that the back-ends ignore it. */
6191 static void
6192 record_unknown_type (tree type, const char* name)
6194 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6195 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
6196 DECL_IGNORED_P (decl) = 1;
6197 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6198 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6199 TYPE_ALIGN (type) = 1;
6200 TYPE_USER_ALIGN (type) = 0;
6201 TYPE_MODE (type) = TYPE_MODE (void_type_node);
6204 /* An string for which we should create an IDENTIFIER_NODE at
6205 startup. */
6207 typedef struct predefined_identifier
6209 /* The name of the identifier. */
6210 const char *const name;
6211 /* The place where the IDENTIFIER_NODE should be stored. */
6212 tree *const node;
6213 /* Nonzero if this is the name of a constructor or destructor. */
6214 const int ctor_or_dtor_p;
6215 } predefined_identifier;
6217 /* Create all the predefined identifiers. */
6219 static void
6220 initialize_predefined_identifiers (void)
6222 const predefined_identifier *pid;
6224 /* A table of identifiers to create at startup. */
6225 static const predefined_identifier predefined_identifiers[] = {
6226 { "C++", &lang_name_cplusplus, 0 },
6227 { "C", &lang_name_c, 0 },
6228 { "Java", &lang_name_java, 0 },
6229 { CTOR_NAME, &ctor_identifier, 1 },
6230 { "__base_ctor", &base_ctor_identifier, 1 },
6231 { "__comp_ctor", &complete_ctor_identifier, 1 },
6232 { DTOR_NAME, &dtor_identifier, 1 },
6233 { "__comp_dtor", &complete_dtor_identifier, 1 },
6234 { "__base_dtor", &base_dtor_identifier, 1 },
6235 { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6236 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6237 { "nelts", &nelts_identifier, 0 },
6238 { THIS_NAME, &this_identifier, 0 },
6239 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6240 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6241 { "_vptr", &vptr_identifier, 0 },
6242 { "__vtt_parm", &vtt_parm_identifier, 0 },
6243 { "std", &std_identifier, 0 },
6244 { NULL, NULL, 0 }
6247 for (pid = predefined_identifiers; pid->name; ++pid)
6249 *pid->node = get_identifier (pid->name);
6250 if (pid->ctor_or_dtor_p)
6251 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6255 /* Create the predefined scalar types of C,
6256 and some nodes representing standard constants (0, 1, (void *)0).
6257 Initialize the global binding level.
6258 Make definitions for built-in primitive functions. */
6260 void
6261 cxx_init_decl_processing (void)
6263 tree void_ftype;
6264 tree void_ftype_ptr;
6266 /* Create all the identifiers we need. */
6267 initialize_predefined_identifiers ();
6269 /* Fill in back-end hooks. */
6270 lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6272 /* Create the global variables. */
6273 push_to_top_level ();
6275 /* Enter the global namespace. */
6276 my_friendly_assert (global_namespace == NULL_TREE, 375);
6277 push_namespace (get_identifier ("::"));
6278 global_namespace = current_namespace;
6279 current_lang_name = NULL_TREE;
6281 /* Adjust various flags based on command-line settings. */
6282 if (! flag_permissive && ! pedantic)
6283 flag_pedantic_errors = 1;
6284 if (!flag_no_inline)
6286 flag_inline_trees = 1;
6287 flag_no_inline = 1;
6289 if (flag_inline_functions)
6291 flag_inline_trees = 2;
6292 flag_inline_functions = 0;
6295 /* Force minimum function alignment if using the least significant
6296 bit of function pointers to store the virtual bit. */
6297 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6298 && force_align_functions_log < 1)
6299 force_align_functions_log = 1;
6301 /* Initially, C. */
6302 current_lang_name = lang_name_c;
6304 current_function_decl = NULL_TREE;
6305 current_binding_level = NULL_BINDING_LEVEL;
6306 free_binding_level = NULL_BINDING_LEVEL;
6308 build_common_tree_nodes (flag_signed_char);
6310 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6311 TREE_TYPE (error_mark_list) = error_mark_node;
6313 /* Make the binding_level structure for global names. */
6314 pushlevel (0);
6315 global_binding_level = current_binding_level;
6316 /* The global level is the namespace level of ::. */
6317 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6318 declare_namespace_level ();
6320 VARRAY_TREE_INIT (global_binding_level->static_decls,
6321 200,
6322 "Static declarations");
6324 /* Create the `std' namespace. */
6325 push_namespace (std_identifier);
6326 std_node = current_namespace;
6327 pop_namespace ();
6329 c_common_nodes_and_builtins ();
6331 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6332 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6333 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6334 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6335 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6336 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6337 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6338 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6340 integer_two_node = build_int_2 (2, 0);
6341 TREE_TYPE (integer_two_node) = integer_type_node;
6342 integer_three_node = build_int_2 (3, 0);
6343 TREE_TYPE (integer_three_node) = integer_type_node;
6345 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6346 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6347 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6348 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6349 TYPE_PRECISION (boolean_type_node) = 1;
6350 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6351 boolean_false_node = build_int_2 (0, 0);
6352 TREE_TYPE (boolean_false_node) = boolean_type_node;
6353 boolean_true_node = build_int_2 (1, 0);
6354 TREE_TYPE (boolean_true_node) = boolean_type_node;
6356 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6358 #if 0
6359 record_builtin_type (RID_MAX, NULL, string_type_node);
6360 #endif
6362 delta_type_node = ptrdiff_type_node;
6363 vtable_index_type = ptrdiff_type_node;
6365 vtt_parm_type = build_pointer_type (const_ptr_type_node);
6366 void_ftype = build_function_type (void_type_node, void_list_node);
6367 void_ftype_ptr = build_function_type (void_type_node,
6368 tree_cons (NULL_TREE,
6369 ptr_type_node,
6370 void_list_node));
6371 void_ftype_ptr
6372 = build_exception_variant (void_ftype_ptr, empty_except_spec);
6374 /* C++ extensions */
6376 unknown_type_node = make_node (UNKNOWN_TYPE);
6377 record_unknown_type (unknown_type_node, "unknown type");
6379 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6380 TREE_TYPE (unknown_type_node) = unknown_type_node;
6382 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6383 result. */
6384 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6385 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6388 /* Make sure we get a unique function type, so we can give
6389 its pointer type a name. (This wins for gdb.) */
6390 tree vfunc_type = make_node (FUNCTION_TYPE);
6391 TREE_TYPE (vfunc_type) = integer_type_node;
6392 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6393 layout_type (vfunc_type);
6395 vtable_entry_type = build_pointer_type (vfunc_type);
6397 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6399 vtbl_type_node
6400 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6401 layout_type (vtbl_type_node);
6402 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6403 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6404 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6405 layout_type (vtbl_ptr_type_node);
6406 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6408 push_namespace (get_identifier ("__cxxabiv1"));
6409 abi_node = current_namespace;
6410 pop_namespace ();
6412 global_type_node = make_node (LANG_TYPE);
6413 record_unknown_type (global_type_node, "global type");
6415 /* Now, C++. */
6416 current_lang_name = lang_name_cplusplus;
6419 tree bad_alloc_type_node, newtype, deltype;
6420 tree ptr_ftype_sizetype;
6422 push_namespace (std_identifier);
6423 bad_alloc_type_node
6424 = xref_tag (class_type, get_identifier ("bad_alloc"),
6425 /*attributes=*/NULL_TREE, 1);
6426 pop_namespace ();
6427 ptr_ftype_sizetype
6428 = build_function_type (ptr_type_node,
6429 tree_cons (NULL_TREE,
6430 size_type_node,
6431 void_list_node));
6432 newtype = build_exception_variant
6433 (ptr_ftype_sizetype, add_exception_specifier
6434 (NULL_TREE, bad_alloc_type_node, -1));
6435 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6436 push_cp_library_fn (NEW_EXPR, newtype);
6437 push_cp_library_fn (VEC_NEW_EXPR, newtype);
6438 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6439 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6442 abort_fndecl
6443 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6445 /* Perform other language dependent initializations. */
6446 init_class_processing ();
6447 init_search_processing ();
6448 init_rtti_processing ();
6450 if (flag_exceptions)
6451 init_exception_processing ();
6453 if (! supports_one_only ())
6454 flag_weak = 0;
6456 make_fname_decl = cp_make_fname_decl;
6457 start_fname_decls ();
6459 /* Show we use EH for cleanups. */
6460 using_eh_for_cleanups ();
6462 /* Maintain consistency. Perhaps we should just complain if they
6463 say -fwritable-strings? */
6464 if (flag_writable_strings)
6465 flag_const_strings = 0;
6468 /* Generate an initializer for a function naming variable from
6469 NAME. NAME may be NULL, in which case we generate a special
6470 ERROR_MARK node which should be replaced later. */
6472 tree
6473 cp_fname_init (const char* name)
6475 tree domain = NULL_TREE;
6476 tree type;
6477 tree init = NULL_TREE;
6478 size_t length = 0;
6480 if (name)
6482 length = strlen (name);
6483 domain = build_index_type (size_int (length));
6484 init = build_string (length + 1, name);
6487 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6488 type = build_cplus_array_type (type, domain);
6490 if (init)
6491 TREE_TYPE (init) = type;
6492 else
6493 /* We don't know the value until instantiation time. Make
6494 something which will be digested now, but replaced later. */
6495 init = build (ERROR_MARK, type);
6497 return init;
6500 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6501 decl, NAME is the initialization string and TYPE_DEP indicates whether
6502 NAME depended on the type of the function. We make use of that to detect
6503 __PRETTY_FUNCTION__ inside a template fn. This is being done
6504 lazily at the point of first use, so we musn't push the decl now. */
6506 static tree
6507 cp_make_fname_decl (tree id, int type_dep)
6509 const char *const name = (type_dep && processing_template_decl
6510 ? NULL : fname_as_string (type_dep));
6511 tree init = cp_fname_init (name);
6512 tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6514 /* As we're using pushdecl_with_scope, we must set the context. */
6515 DECL_CONTEXT (decl) = current_function_decl;
6516 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6518 TREE_STATIC (decl) = 1;
6519 TREE_READONLY (decl) = 1;
6520 DECL_ARTIFICIAL (decl) = 1;
6521 DECL_INITIAL (decl) = init;
6523 TREE_USED (decl) = 1;
6525 if (current_function_decl)
6527 struct cp_binding_level *b = current_binding_level;
6528 while (b->level_chain->parm_flag == 0)
6529 b = b->level_chain;
6530 pushdecl_with_scope (decl, b);
6533 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6535 return decl;
6538 /* Make a definition for a builtin function named NAME in the current
6539 namespace, whose data type is TYPE and whose context is CONTEXT.
6540 TYPE should be a function type with argument types.
6542 CLASS and CODE tell later passes how to compile calls to this function.
6543 See tree.h for possible values.
6545 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6546 the name to be called if we can't opencode the function.
6547 If ATTRS is nonzero, use that for the function's attribute
6548 list. */
6550 static tree
6551 builtin_function_1 (const char* name,
6552 tree type,
6553 tree context,
6554 int code,
6555 enum built_in_class class,
6556 const char* libname,
6557 tree attrs)
6559 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6560 DECL_BUILT_IN_CLASS (decl) = class;
6561 DECL_FUNCTION_CODE (decl) = code;
6562 DECL_CONTEXT (decl) = context;
6564 pushdecl (decl);
6566 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6567 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6568 function in the namespace. */
6569 if (libname)
6570 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6571 make_decl_rtl (decl, NULL);
6573 /* Warn if a function in the namespace for users
6574 is used without an occasion to consider it declared. */
6575 if (name[0] != '_' || name[1] != '_')
6576 DECL_ANTICIPATED (decl) = 1;
6578 /* Possibly apply some default attributes to this built-in function. */
6579 if (attrs)
6580 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6581 else
6582 decl_attributes (&decl, NULL_TREE, 0);
6584 return decl;
6587 /* Entry point for the benefit of c_common_nodes_and_builtins.
6589 Make a defintion for a builtin function named NAME and whose data type
6590 is TYPE. TYPE should be a function type with argument types. This
6591 function places the anticipated declaration in the global namespace
6592 and additionally in the std namespace if appropriate.
6594 CLASS and CODE tell later passes how to compile calls to this function.
6595 See tree.h for possible values.
6597 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6598 the name to be called if we can't opencode the function.
6600 If ATTRS is nonzero, use that for the function's attribute
6601 list. */
6603 tree
6604 builtin_function (const char* name,
6605 tree type,
6606 int code,
6607 enum built_in_class class,
6608 const char* libname,
6609 tree attrs)
6611 /* All builtins that don't begin with an '_' should additionally
6612 go in the 'std' namespace. */
6613 if (name[0] != '_')
6615 push_namespace (std_identifier);
6616 builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6617 pop_namespace ();
6620 return builtin_function_1 (name, type, NULL_TREE, code,
6621 class, libname, attrs);
6624 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6625 function. Not called directly. */
6627 static tree
6628 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6630 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6631 DECL_EXTERNAL (fn) = 1;
6632 TREE_PUBLIC (fn) = 1;
6633 DECL_ARTIFICIAL (fn) = 1;
6634 TREE_NOTHROW (fn) = 1;
6635 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6636 SET_DECL_LANGUAGE (fn, lang_c);
6637 return fn;
6640 /* Returns the _DECL for a library function with C linkage.
6641 We assume that such functions never throw; if this is incorrect,
6642 callers should unset TREE_NOTHROW. */
6644 tree
6645 build_library_fn (tree name, tree type)
6647 return build_library_fn_1 (name, ERROR_MARK, type);
6650 /* Returns the _DECL for a library function with C++ linkage. */
6652 static tree
6653 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6655 tree fn = build_library_fn_1 (name, operator_code, type);
6656 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6657 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6658 SET_DECL_LANGUAGE (fn, lang_cplusplus);
6659 set_mangled_name_for_decl (fn);
6660 return fn;
6663 /* Like build_library_fn, but takes a C string instead of an
6664 IDENTIFIER_NODE. */
6666 tree
6667 build_library_fn_ptr (const char* name, tree type)
6669 return build_library_fn (get_identifier (name), type);
6672 /* Like build_cp_library_fn, but takes a C string instead of an
6673 IDENTIFIER_NODE. */
6675 tree
6676 build_cp_library_fn_ptr (const char* name, tree type)
6678 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6681 /* Like build_library_fn, but also pushes the function so that we will
6682 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
6684 tree
6685 push_library_fn (tree name, tree type)
6687 tree fn = build_library_fn (name, type);
6688 pushdecl_top_level (fn);
6689 return fn;
6692 /* Like build_cp_library_fn, but also pushes the function so that it
6693 will be found by normal lookup. */
6695 static tree
6696 push_cp_library_fn (enum tree_code operator_code, tree type)
6698 tree fn = build_cp_library_fn (ansi_opname (operator_code),
6699 operator_code,
6700 type);
6701 pushdecl (fn);
6702 return fn;
6705 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6706 a FUNCTION_TYPE. */
6708 tree
6709 push_void_library_fn (tree name, tree parmtypes)
6711 tree type = build_function_type (void_type_node, parmtypes);
6712 return push_library_fn (name, type);
6715 /* Like push_library_fn, but also note that this function throws
6716 and does not return. Used for __throw_foo and the like. */
6718 tree
6719 push_throw_library_fn (tree name, tree type)
6721 tree fn = push_library_fn (name, type);
6722 TREE_THIS_VOLATILE (fn) = 1;
6723 TREE_NOTHROW (fn) = 0;
6724 return fn;
6727 /* Apply default attributes to a function, if a system function with default
6728 attributes. */
6730 void
6731 cxx_insert_default_attributes (tree decl)
6733 if (!DECL_EXTERN_C_FUNCTION_P (decl))
6734 return;
6735 if (!TREE_PUBLIC (decl))
6736 return;
6737 c_common_insert_default_attributes (decl);
6740 /* When we call finish_struct for an anonymous union, we create
6741 default copy constructors and such. But, an anonymous union
6742 shouldn't have such things; this function undoes the damage to the
6743 anonymous union type T.
6745 (The reason that we create the synthesized methods is that we don't
6746 distinguish `union { int i; }' from `typedef union { int i; } U'.
6747 The first is an anonymous union; the second is just an ordinary
6748 union type.) */
6750 void
6751 fixup_anonymous_aggr (tree t)
6753 tree *q;
6755 /* Wipe out memory of synthesized methods */
6756 TYPE_HAS_CONSTRUCTOR (t) = 0;
6757 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6758 TYPE_HAS_INIT_REF (t) = 0;
6759 TYPE_HAS_CONST_INIT_REF (t) = 0;
6760 TYPE_HAS_ASSIGN_REF (t) = 0;
6761 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6763 /* Splice the implicitly generated functions out of the TYPE_METHODS
6764 list. */
6765 q = &TYPE_METHODS (t);
6766 while (*q)
6768 if (DECL_ARTIFICIAL (*q))
6769 *q = TREE_CHAIN (*q);
6770 else
6771 q = &TREE_CHAIN (*q);
6774 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
6775 if (TYPE_METHODS (t))
6776 cp_error_at ("an anonymous union cannot have function members", t);
6778 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6779 assignment operators (because they cannot have these methods themselves).
6780 For anonymous unions this is already checked because they are not allowed
6781 in any union, otherwise we have to check it. */
6782 if (TREE_CODE (t) != UNION_TYPE)
6784 tree field, type;
6786 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6787 if (TREE_CODE (field) == FIELD_DECL)
6789 type = TREE_TYPE (field);
6790 if (CLASS_TYPE_P (type))
6792 if (TYPE_NEEDS_CONSTRUCTING (type))
6793 cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6794 field);
6795 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6796 cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6797 field);
6798 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6799 cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6800 field);
6806 /* Make sure that a declaration with no declarator is well-formed, i.e.
6807 just declares a tagged type or anonymous union.
6809 Returns the type declared; or NULL_TREE if none. */
6811 tree
6812 check_tag_decl (tree declspecs)
6814 int found_type = 0;
6815 int saw_friend = 0;
6816 int saw_typedef = 0;
6817 tree ob_modifier = NULL_TREE;
6818 register tree link;
6819 /* If a class, struct, or enum type is declared by the DECLSPECS
6820 (i.e, if a class-specifier, enum-specifier, or non-typename
6821 elaborated-type-specifier appears in the DECLSPECS),
6822 DECLARED_TYPE is set to the corresponding type. */
6823 tree declared_type = NULL_TREE;
6824 bool error_p = false;
6826 for (link = declspecs; link; link = TREE_CHAIN (link))
6828 tree value = TREE_VALUE (link);
6830 if (TYPE_P (value)
6831 || TREE_CODE (value) == TYPE_DECL
6832 || (TREE_CODE (value) == IDENTIFIER_NODE
6833 && IDENTIFIER_GLOBAL_VALUE (value)
6834 && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6836 ++found_type;
6838 if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6840 if (! in_system_header)
6841 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6842 return NULL_TREE;
6845 if (TYPE_P (value)
6846 && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6847 || TREE_CODE (value) == ENUMERAL_TYPE))
6849 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6850 declared_type = value;
6853 else if (value == ridpointers[(int) RID_TYPEDEF])
6854 saw_typedef = 1;
6855 else if (value == ridpointers[(int) RID_FRIEND])
6857 if (current_class_type == NULL_TREE
6858 || current_scope () != current_class_type)
6859 ob_modifier = value;
6860 else
6861 saw_friend = 1;
6863 else if (value == ridpointers[(int) RID_STATIC]
6864 || value == ridpointers[(int) RID_EXTERN]
6865 || value == ridpointers[(int) RID_AUTO]
6866 || value == ridpointers[(int) RID_REGISTER]
6867 || value == ridpointers[(int) RID_INLINE]
6868 || value == ridpointers[(int) RID_VIRTUAL]
6869 || value == ridpointers[(int) RID_CONST]
6870 || value == ridpointers[(int) RID_VOLATILE]
6871 || value == ridpointers[(int) RID_EXPLICIT]
6872 || value == ridpointers[(int) RID_THREAD])
6873 ob_modifier = value;
6874 else if (value == error_mark_node)
6875 error_p = true;
6878 if (found_type > 1)
6879 error ("multiple types in one declaration");
6881 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6882 pedwarn ("declaration does not declare anything");
6883 /* Check for an anonymous union. */
6884 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6885 && TYPE_ANONYMOUS_P (declared_type))
6887 /* 7/3 In a simple-declaration, the optional init-declarator-list
6888 can be omitted only when declaring a class (clause 9) or
6889 enumeration (7.2), that is, when the decl-specifier-seq contains
6890 either a class-specifier, an elaborated-type-specifier with
6891 a class-key (9.1), or an enum-specifier. In these cases and
6892 whenever a class-specifier or enum-specifier is present in the
6893 decl-specifier-seq, the identifiers in these specifiers are among
6894 the names being declared by the declaration (as class-name,
6895 enum-names, or enumerators, depending on the syntax). In such
6896 cases, and except for the declaration of an unnamed bit-field (9.6),
6897 the decl-specifier-seq shall introduce one or more names into the
6898 program, or shall redeclare a name introduced by a previous
6899 declaration. [Example:
6900 enum { }; // ill-formed
6901 typedef class { }; // ill-formed
6902 --end example] */
6903 if (saw_typedef)
6905 error ("missing type-name in typedef-declaration");
6906 return NULL_TREE;
6908 /* Anonymous unions are objects, so they can have specifiers. */;
6909 SET_ANON_AGGR_TYPE_P (declared_type);
6911 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
6912 && !in_system_header)
6913 pedwarn ("ISO C++ prohibits anonymous structs");
6916 else if (ob_modifier)
6918 if (ob_modifier == ridpointers[(int) RID_INLINE]
6919 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6920 error ("`%D' can only be specified for functions", ob_modifier);
6921 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6922 error ("`%D' can only be specified inside a class", ob_modifier);
6923 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6924 error ("`%D' can only be specified for constructors",
6925 ob_modifier);
6926 else
6927 error ("`%D' can only be specified for objects and functions",
6928 ob_modifier);
6931 return declared_type;
6934 /* Called when a declaration is seen that contains no names to declare.
6935 If its type is a reference to a structure, union or enum inherited
6936 from a containing scope, shadow that tag name for the current scope
6937 with a forward reference.
6938 If its type defines a new named structure or union
6939 or defines an enum, it is valid but we need not do anything here.
6940 Otherwise, it is an error.
6942 C++: may have to grok the declspecs to learn about static,
6943 complain for anonymous unions.
6945 Returns the TYPE declared -- or NULL_TREE if none. */
6947 tree
6948 shadow_tag (tree declspecs)
6950 tree t = check_tag_decl (declspecs);
6952 if (!t)
6953 return NULL_TREE;
6955 maybe_process_partial_specialization (t);
6957 /* This is where the variables in an anonymous union are
6958 declared. An anonymous union declaration looks like:
6959 union { ... } ;
6960 because there is no declarator after the union, the parser
6961 sends that declaration here. */
6962 if (ANON_AGGR_TYPE_P (t))
6964 fixup_anonymous_aggr (t);
6966 if (TYPE_FIELDS (t))
6968 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6969 NULL);
6970 finish_anon_union (decl);
6974 return t;
6977 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6979 tree
6980 groktypename (tree typename)
6982 tree specs, attrs;
6983 tree type;
6984 if (TREE_CODE (typename) != TREE_LIST)
6985 return typename;
6986 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6987 type = grokdeclarator (TREE_VALUE (typename), specs,
6988 TYPENAME, 0, &attrs);
6989 if (attrs)
6990 cplus_decl_attributes (&type, attrs, 0);
6991 return type;
6994 /* Decode a declarator in an ordinary declaration or data definition.
6995 This is called as soon as the type information and variable name
6996 have been parsed, before parsing the initializer if any.
6997 Here we create the ..._DECL node, fill in its type,
6998 and put it on the list of decls for the current context.
6999 The ..._DECL node is returned as the value.
7001 Exception: for arrays where the length is not specified,
7002 the type is left null, to be filled in by `cp_finish_decl'.
7004 Function definitions do not come here; they go to start_function
7005 instead. However, external and forward declarations of functions
7006 do go through here. Structure field declarations are done by
7007 grokfield and not through here. */
7009 tree
7010 start_decl (tree declarator,
7011 tree declspecs,
7012 int initialized,
7013 tree attributes,
7014 tree prefix_attributes)
7016 tree decl;
7017 register tree type, tem;
7018 tree context;
7020 /* This should only be done once on the top most decl. */
7021 if (have_extern_spec)
7023 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
7024 declspecs);
7025 have_extern_spec = false;
7028 /* An object declared as __attribute__((deprecated)) suppresses
7029 warnings of uses of other deprecated items. */
7030 if (lookup_attribute ("deprecated", attributes))
7031 deprecated_state = DEPRECATED_SUPPRESS;
7033 attributes = chainon (attributes, prefix_attributes);
7035 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
7036 &attributes);
7038 deprecated_state = DEPRECATED_NORMAL;
7040 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
7041 return NULL_TREE;
7043 type = TREE_TYPE (decl);
7045 if (type == error_mark_node)
7046 return NULL_TREE;
7048 context = DECL_CONTEXT (decl);
7050 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
7051 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
7053 /* When parsing the initializer, lookup should use the object's
7054 namespace. */
7055 push_decl_namespace (context);
7058 /* We are only interested in class contexts, later. */
7059 if (context && TREE_CODE (context) == NAMESPACE_DECL)
7060 context = NULL_TREE;
7062 if (initialized)
7063 /* Is it valid for this decl to have an initializer at all?
7064 If not, set INITIALIZED to zero, which will indirectly
7065 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
7066 switch (TREE_CODE (decl))
7068 case TYPE_DECL:
7069 error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
7070 initialized = 0;
7071 break;
7073 case FUNCTION_DECL:
7074 error ("function `%#D' is initialized like a variable", decl);
7075 initialized = 0;
7076 break;
7078 default:
7079 break;
7082 if (initialized)
7084 if (! toplevel_bindings_p ()
7085 && DECL_EXTERNAL (decl))
7086 warning ("declaration of `%#D' has `extern' and is initialized",
7087 decl);
7088 DECL_EXTERNAL (decl) = 0;
7089 if (toplevel_bindings_p ())
7090 TREE_STATIC (decl) = 1;
7092 /* Tell `pushdecl' this is an initialized decl
7093 even though we don't yet have the initializer expression.
7094 Also tell `cp_finish_decl' it may store the real initializer. */
7095 DECL_INITIAL (decl) = error_mark_node;
7098 /* Set attributes here so if duplicate decl, will have proper attributes. */
7099 cplus_decl_attributes (&decl, attributes, 0);
7101 /* If #pragma weak was used, mark the decl weak now. */
7102 if (current_binding_level == global_binding_level)
7103 maybe_apply_pragma_weak (decl);
7105 if (TREE_CODE (decl) == FUNCTION_DECL
7106 && DECL_DECLARED_INLINE_P (decl)
7107 && DECL_UNINLINABLE (decl)
7108 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
7109 warning ("%Hinline function '%D' given attribute noinline",
7110 &DECL_SOURCE_LOCATION (decl), decl);
7112 if (context && COMPLETE_TYPE_P (complete_type (context)))
7114 push_nested_class (context);
7116 if (TREE_CODE (decl) == VAR_DECL)
7118 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
7119 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7120 error ("`%#D' is not a static member of `%#T'", decl, context);
7121 else
7123 if (DECL_CONTEXT (field) != context)
7125 if (!same_type_p (DECL_CONTEXT (field), context))
7126 pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7127 DECL_CONTEXT (field), DECL_NAME (decl),
7128 context, DECL_NAME (decl));
7129 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7131 /* Static data member are tricky; an in-class initialization
7132 still doesn't provide a definition, so the in-class
7133 declaration will have DECL_EXTERNAL set, but will have an
7134 initialization. Thus, duplicate_decls won't warn
7135 about this situation, and so we check here. */
7136 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7137 error ("duplicate initialization of %D", decl);
7138 if (duplicate_decls (decl, field))
7139 decl = field;
7142 else
7144 tree field = check_classfn (context, decl);
7145 if (field && duplicate_decls (decl, field))
7146 decl = field;
7149 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
7150 DECL_IN_AGGR_P (decl) = 0;
7151 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7152 || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7154 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7155 /* [temp.expl.spec] An explicit specialization of a static data
7156 member of a template is a definition if the declaration
7157 includes an initializer; otherwise, it is a declaration.
7159 We check for processing_specialization so this only applies
7160 to the new specialization syntax. */
7161 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7162 DECL_EXTERNAL (decl) = 1;
7165 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7166 pedwarn ("declaration of `%#D' outside of class is not definition",
7167 decl);
7170 /* Enter this declaration into the symbol table. */
7171 tem = maybe_push_decl (decl);
7173 if (processing_template_decl)
7174 tem = push_template_decl (tem);
7176 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7177 /* Tell the back-end to use or not use .common as appropriate. If we say
7178 -fconserve-space, we want this to save .data space, at the expense of
7179 wrong semantics. If we say -fno-conserve-space, we want this to
7180 produce errors about redefs; to do this we force variables into the
7181 data segment. */
7182 DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7183 || !DECL_THREAD_LOCAL (tem))
7184 && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7185 #endif
7187 if (! processing_template_decl)
7188 start_decl_1 (tem);
7190 return tem;
7193 void
7194 start_decl_1 (tree decl)
7196 tree type = TREE_TYPE (decl);
7197 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7199 if (type == error_mark_node)
7200 return;
7202 maybe_push_cleanup_level (type);
7204 if (initialized)
7205 /* Is it valid for this decl to have an initializer at all?
7206 If not, set INITIALIZED to zero, which will indirectly
7207 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
7209 /* Don't allow initializations for incomplete types except for
7210 arrays which might be completed by the initialization. */
7211 if (COMPLETE_TYPE_P (complete_type (type)))
7212 ; /* A complete type is ok. */
7213 else if (TREE_CODE (type) != ARRAY_TYPE)
7215 error ("variable `%#D' has initializer but incomplete type",
7216 decl);
7217 initialized = 0;
7218 type = TREE_TYPE (decl) = error_mark_node;
7220 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7222 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7223 error ("elements of array `%#D' have incomplete type", decl);
7224 /* else we already gave an error in start_decl. */
7225 initialized = 0;
7229 if (!initialized
7230 && TREE_CODE (decl) != TYPE_DECL
7231 && TREE_CODE (decl) != TEMPLATE_DECL
7232 && type != error_mark_node
7233 && IS_AGGR_TYPE (type)
7234 && ! DECL_EXTERNAL (decl))
7236 if ((! processing_template_decl || ! uses_template_parms (type))
7237 && !COMPLETE_TYPE_P (complete_type (type)))
7239 error ("aggregate `%#D' has incomplete type and cannot be defined",
7240 decl);
7241 /* Change the type so that assemble_variable will give
7242 DECL an rtl we can live with: (mem (const_int 0)). */
7243 type = TREE_TYPE (decl) = error_mark_node;
7245 else
7247 /* If any base type in the hierarchy of TYPE needs a constructor,
7248 then we set initialized to 1. This way any nodes which are
7249 created for the purposes of initializing this aggregate
7250 will live as long as it does. This is necessary for global
7251 aggregates which do not have their initializers processed until
7252 the end of the file. */
7253 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7257 if (! initialized)
7258 DECL_INITIAL (decl) = NULL_TREE;
7261 /* Handle initialization of references.
7262 These three arguments are from `cp_finish_decl', and have the
7263 same meaning here that they do there.
7265 Quotes on semantics can be found in ARM 8.4.3. */
7267 static tree
7268 grok_reference_init (tree decl, tree type, tree init)
7270 tree tmp;
7272 if (init == NULL_TREE)
7274 if ((DECL_LANG_SPECIFIC (decl) == 0
7275 || DECL_IN_AGGR_P (decl) == 0)
7276 && ! DECL_THIS_EXTERN (decl))
7277 error ("`%D' declared as reference but not initialized", decl);
7278 return NULL_TREE;
7281 if (TREE_CODE (init) == CONSTRUCTOR)
7283 error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7284 return NULL_TREE;
7287 if (TREE_CODE (init) == TREE_LIST)
7288 init = build_compound_expr (init);
7290 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7291 init = convert_from_reference (init);
7293 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7294 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7296 /* Note: default conversion is only called in very special cases. */
7297 init = default_conversion (init);
7300 /* Convert INIT to the reference type TYPE. This may involve the
7301 creation of a temporary, whose lifetime must be the same as that
7302 of the reference. If so, a DECL_STMT for the temporary will be
7303 added just after the DECL_STMT for DECL. That's why we don't set
7304 DECL_INITIAL for local references (instead assigning to them
7305 explicitly); we need to allow the temporary to be initialized
7306 first. */
7307 tmp = initialize_reference (type, init, decl);
7309 if (tmp == error_mark_node)
7310 return NULL_TREE;
7311 else if (tmp == NULL_TREE)
7313 error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7314 return NULL_TREE;
7317 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7318 return tmp;
7320 DECL_INITIAL (decl) = tmp;
7322 return NULL_TREE;
7325 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7326 array until we finish parsing the initializer. If that's the
7327 situation we're in, update DECL accordingly. */
7329 static void
7330 maybe_deduce_size_from_array_init (tree decl, tree init)
7332 tree type = TREE_TYPE (decl);
7334 if (TREE_CODE (type) == ARRAY_TYPE
7335 && TYPE_DOMAIN (type) == NULL_TREE
7336 && TREE_CODE (decl) != TYPE_DECL)
7338 /* do_default is really a C-ism to deal with tentative definitions.
7339 But let's leave it here to ease the eventual merge. */
7340 int do_default = !DECL_EXTERNAL (decl);
7341 tree initializer = init ? init : DECL_INITIAL (decl);
7342 int failure = complete_array_type (type, initializer, do_default);
7344 if (failure == 1)
7345 error ("initializer fails to determine size of `%D'", decl);
7347 if (failure == 2)
7349 if (do_default)
7350 error ("array size missing in `%D'", decl);
7351 /* If a `static' var's size isn't known, make it extern as
7352 well as static, so it does not get allocated. If it's not
7353 `static', then don't mark it extern; finish_incomplete_decl
7354 will give it a default size and it will get allocated. */
7355 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7356 DECL_EXTERNAL (decl) = 1;
7359 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7360 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7361 integer_zero_node))
7362 error ("zero-size array `%D'", decl);
7364 layout_decl (decl, 0);
7368 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7369 any appropriate error messages regarding the layout. */
7371 static void
7372 layout_var_decl (tree decl)
7374 tree type = TREE_TYPE (decl);
7375 #if 0
7376 tree ttype = target_type (type);
7377 #endif
7379 /* If we haven't already layed out this declaration, do so now.
7380 Note that we must not call complete type for an external object
7381 because it's type might involve templates that we are not
7382 supposed to isntantiate yet. (And it's perfectly valid to say
7383 `extern X x' for some incomplete type `X'.) */
7384 if (!DECL_EXTERNAL (decl))
7385 complete_type (type);
7386 if (!DECL_SIZE (decl)
7387 && TREE_TYPE (decl) != error_mark_node
7388 && (COMPLETE_TYPE_P (type)
7389 || (TREE_CODE (type) == ARRAY_TYPE
7390 && !TYPE_DOMAIN (type)
7391 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7392 layout_decl (decl, 0);
7394 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7396 /* An automatic variable with an incomplete type: that is an error.
7397 Don't talk about array types here, since we took care of that
7398 message in grokdeclarator. */
7399 error ("storage size of `%D' isn't known", decl);
7400 TREE_TYPE (decl) = error_mark_node;
7402 #if 0
7403 /* Keep this code around in case we later want to control debug info
7404 based on whether a type is "used". (jason 1999-11-11) */
7406 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7407 /* Let debugger know it should output info for this type. */
7408 note_debug_info_needed (ttype);
7410 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7411 note_debug_info_needed (DECL_CONTEXT (decl));
7412 #endif
7414 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7415 && DECL_SIZE (decl) != NULL_TREE
7416 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7418 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7419 constant_expression_warning (DECL_SIZE (decl));
7420 else
7421 error ("storage size of `%D' isn't constant", decl);
7424 if (TREE_STATIC (decl)
7425 && !DECL_ARTIFICIAL (decl)
7426 && current_function_decl
7427 && DECL_CONTEXT (decl) == current_function_decl)
7428 push_local_name (decl);
7431 /* If a local static variable is declared in an inline function, or if
7432 we have a weak definition, we must endeavor to create only one
7433 instance of the variable at link-time. */
7435 static void
7436 maybe_commonize_var (tree decl)
7438 /* Static data in a function with comdat linkage also has comdat
7439 linkage. */
7440 if (TREE_STATIC (decl)
7441 /* Don't mess with __FUNCTION__. */
7442 && ! DECL_ARTIFICIAL (decl)
7443 && current_function_decl
7444 && DECL_CONTEXT (decl) == current_function_decl
7445 && (DECL_DECLARED_INLINE_P (current_function_decl)
7446 || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
7447 && TREE_PUBLIC (current_function_decl))
7449 /* If flag_weak, we don't need to mess with this, as we can just
7450 make the function weak, and let it refer to its unique local
7451 copy. This works because we don't allow the function to be
7452 inlined. */
7453 if (! flag_weak)
7455 if (DECL_INTERFACE_KNOWN (current_function_decl))
7457 TREE_PUBLIC (decl) = 1;
7458 DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7460 else if (DECL_INITIAL (decl) == NULL_TREE
7461 || DECL_INITIAL (decl) == error_mark_node)
7463 TREE_PUBLIC (decl) = 1;
7464 DECL_COMMON (decl) = 1;
7466 /* else we lose. We can only do this if we can use common,
7467 which we can't if it has been initialized. */
7469 if (!TREE_PUBLIC (decl))
7471 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7472 cp_warning_at (" you can work around this by removing the initializer", decl);
7475 else
7476 comdat_linkage (decl);
7478 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7479 /* Set it up again; we might have set DECL_INITIAL since the last
7480 time. */
7481 comdat_linkage (decl);
7484 /* Issue an error message if DECL is an uninitialized const variable. */
7486 static void
7487 check_for_uninitialized_const_var (tree decl)
7489 tree type = TREE_TYPE (decl);
7491 /* ``Unless explicitly declared extern, a const object does not have
7492 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7493 7.1.6 */
7494 if (TREE_CODE (decl) == VAR_DECL
7495 && TREE_CODE (type) != REFERENCE_TYPE
7496 && CP_TYPE_CONST_P (type)
7497 && !TYPE_NEEDS_CONSTRUCTING (type)
7498 && !DECL_INITIAL (decl))
7499 error ("uninitialized const `%D'", decl);
7502 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
7503 returned is the next FIELD_DECL (possibly FIELD itself) that can be
7504 initialized. If there are no more such fields, the return value
7505 will be NULL. */
7507 static tree
7508 next_initializable_field (tree field)
7510 while (field
7511 && (TREE_CODE (field) != FIELD_DECL
7512 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7513 || DECL_ARTIFICIAL (field)))
7514 field = TREE_CHAIN (field);
7516 return field;
7519 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7520 brace-enclosed aggregate initializer.
7522 *INITP is one of a list of initializers describing a brace-enclosed
7523 initializer for an entity of the indicated aggregate TYPE. It may
7524 not presently match the shape of the TYPE; for example:
7526 struct S { int a; int b; };
7527 struct S a[] = { 1, 2, 3, 4 };
7529 Here *INITP will point to TREE_LIST of four elements, rather than a
7530 list of two elements, each itself a list of two elements. This
7531 routine transforms INIT from the former form into the latter. The
7532 revised initializer is returned. */
7534 static tree
7535 reshape_init (tree type, tree *initp)
7537 tree inits;
7538 tree old_init;
7539 tree old_init_value;
7540 tree new_init;
7541 bool brace_enclosed_p;
7543 old_init = *initp;
7544 old_init_value = (TREE_CODE (*initp) == TREE_LIST
7545 ? TREE_VALUE (*initp) : old_init);
7547 /* For some parse errors, OLD_INIT_VALUE may be NULL. */
7548 if (!old_init_value)
7550 my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
7551 TREE_VALUE (old_init) = error_mark_node;
7552 return old_init;
7555 /* If the initializer is brace-enclosed, pull initializers from the
7556 enclosed elements. Advance past the brace-enclosed initializer
7557 now. */
7558 if (TREE_CODE (old_init_value) == CONSTRUCTOR
7559 && TREE_HAS_CONSTRUCTOR (old_init_value))
7561 *initp = TREE_CHAIN (old_init);
7562 TREE_CHAIN (old_init) = NULL_TREE;
7563 inits = CONSTRUCTOR_ELTS (old_init_value);
7564 initp = &inits;
7565 brace_enclosed_p = true;
7567 else
7569 inits = NULL_TREE;
7570 brace_enclosed_p = false;
7573 /* A non-aggregate type is always initialized with a single
7574 initializer. */
7575 if (!CP_AGGREGATE_TYPE_P (type))
7577 *initp = TREE_CHAIN (old_init);
7578 TREE_CHAIN (old_init) = NULL_TREE;
7579 /* It is invalid to initialize a non-aggregate type with a
7580 brace-enclosed initializer. */
7581 if (brace_enclosed_p)
7583 error ("brace-enclosed initializer used to initialize `%T'",
7584 type);
7585 if (TREE_CODE (old_init) == TREE_LIST)
7586 TREE_VALUE (old_init) = error_mark_node;
7587 else
7588 old_init = error_mark_node;
7591 return old_init;
7594 /* [dcl.init.aggr]
7596 All implicit type conversions (clause _conv_) are considered when
7597 initializing the aggregate member with an initializer from an
7598 initializer-list. If the initializer can initialize a member,
7599 the member is initialized. Otherwise, if the member is itself a
7600 non-empty subaggregate, brace elision is assumed and the
7601 initializer is considered for the initialization of the first
7602 member of the subaggregate. */
7603 if (CLASS_TYPE_P (type)
7604 && !brace_enclosed_p
7605 && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7607 *initp = TREE_CHAIN (old_init);
7608 TREE_CHAIN (old_init) = NULL_TREE;
7609 return old_init;
7612 if (TREE_CODE (old_init_value) == STRING_CST
7613 && TREE_CODE (type) == ARRAY_TYPE
7614 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7616 /* [dcl.init.string]
7618 A char array (whether plain char, signed char, or unsigned char)
7619 can be initialized by a string-literal (optionally enclosed in
7620 braces); a wchar_t array can be initialized by a wide
7621 string-literal (optionally enclosed in braces). */
7622 new_init = old_init;
7623 /* Move past the initializer. */
7624 *initp = TREE_CHAIN (old_init);
7625 TREE_CHAIN (old_init) = NULL_TREE;
7627 else
7629 /* Build a CONSTRUCTOR to hold the contents of the aggregate. */
7630 new_init = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
7631 TREE_HAS_CONSTRUCTOR (new_init) = 1;
7633 if (CLASS_TYPE_P (type))
7635 tree field;
7637 field = next_initializable_field (TYPE_FIELDS (type));
7639 if (!field)
7641 /* [dcl.init.aggr]
7643 An initializer for an aggregate member that is an
7644 empty class shall have the form of an empty
7645 initializer-list {}. */
7646 if (!brace_enclosed_p)
7647 error ("initializer for `%T' must be brace-enclosed",
7648 type);
7650 else
7652 /* Loop through the initializable fields, gathering
7653 initializers. */
7654 /* FIXME support non-trivial labeled initializers. */
7655 while (*initp && field)
7657 tree field_init;
7659 field_init = reshape_init (TREE_TYPE (field), initp);
7660 TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7661 CONSTRUCTOR_ELTS (new_init) = field_init;
7662 /* [dcl.init.aggr]
7664 When a union is initialized with a brace-enclosed
7665 initializer, the braces shall only contain an
7666 initializer for the first member of the union. */
7667 if (TREE_CODE (type) == UNION_TYPE)
7668 break;
7669 field = next_initializable_field (TREE_CHAIN (field));
7673 else if (TREE_CODE (type) == ARRAY_TYPE)
7675 tree index;
7676 tree max_index;
7678 /* If the bound of the array is known, take no more initializers
7679 than are allowed. */
7680 max_index = (TYPE_DOMAIN (type)
7681 ? array_type_nelts (type) : NULL_TREE);
7682 /* Loop through the array elements, gathering initializers. */
7683 for (index = size_zero_node;
7684 *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7685 index = size_binop (PLUS_EXPR, index, size_one_node))
7687 tree element_init;
7689 element_init = reshape_init (TREE_TYPE (type), initp);
7690 TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7691 CONSTRUCTOR_ELTS (new_init) = element_init;
7692 if (TREE_PURPOSE (element_init))
7693 index = TREE_PURPOSE (element_init);
7696 else
7697 abort ();
7699 /* The initializers were placed in reverse order in the
7700 CONSTRUCTOR. */
7701 CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7703 if (TREE_CODE (old_init) == TREE_LIST)
7704 new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7707 /* If this was a brace-enclosed initializer and all of the
7708 initializers were not used up, there is a problem. */
7709 if (brace_enclosed_p && *initp)
7710 error ("too many initializers for `%T'", type);
7712 return new_init;
7715 /* Verify INIT (the initializer for DECL), and record the
7716 initialization in DECL_INITIAL, if appropriate.
7718 If the return value is non-NULL, it is an expression that must be
7719 evaluated dynamically to initialize DECL. */
7721 static tree
7722 check_initializer (tree decl, tree init, int flags)
7724 tree type = TREE_TYPE (decl);
7726 /* If `start_decl' didn't like having an initialization, ignore it now. */
7727 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7728 init = NULL_TREE;
7730 /* If an initializer is present, DECL_INITIAL has been
7731 error_mark_node, to indicate that an as-of-yet unevaluated
7732 initialization will occur. From now on, DECL_INITIAL reflects
7733 the static initialization -- if any -- of DECL. */
7734 DECL_INITIAL (decl) = NULL_TREE;
7736 /* Things that are going to be initialized need to have complete
7737 type. */
7738 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7740 if (type == error_mark_node)
7741 /* We will have already complained. */
7742 init = NULL_TREE;
7743 else if (init && COMPLETE_TYPE_P (type)
7744 && !TREE_CONSTANT (TYPE_SIZE (type)))
7746 error ("variable-sized object `%D' may not be initialized", decl);
7747 init = NULL_TREE;
7749 else if (TREE_CODE (type) == ARRAY_TYPE
7750 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7752 error ("elements of array `%#D' have incomplete type", decl);
7753 init = NULL_TREE;
7755 else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7757 error ("`%D' has incomplete type", decl);
7758 TREE_TYPE (decl) = error_mark_node;
7759 init = NULL_TREE;
7762 if (TREE_CODE (decl) == CONST_DECL)
7764 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7766 DECL_INITIAL (decl) = init;
7768 my_friendly_assert (init != NULL_TREE, 149);
7769 init = NULL_TREE;
7771 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7772 init = grok_reference_init (decl, type, init);
7773 else if (init)
7775 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7777 /* [dcl.init] paragraph 13,
7778 If T is a scalar type, then a declaration of the form
7779 T x = { a };
7780 is equivalent to
7781 T x = a;
7783 reshape_init will complain about the extra braces,
7784 and doesn't do anything useful in the case where TYPE is
7785 scalar, so just don't call it. */
7786 if (CP_AGGREGATE_TYPE_P (type))
7787 init = reshape_init (type, &init);
7789 if ((*targetm.vector_opaque_p) (type))
7791 error ("opaque vector types cannot be initialized");
7792 init = error_mark_node;
7796 /* If DECL has an array type without a specific bound, deduce the
7797 array size from the initializer. */
7798 maybe_deduce_size_from_array_init (decl, init);
7799 type = TREE_TYPE (decl);
7800 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7801 TREE_TYPE (init) = type;
7803 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7805 if (TREE_CODE (type) == ARRAY_TYPE)
7806 goto initialize_aggr;
7807 else if (TREE_CODE (init) == CONSTRUCTOR
7808 && TREE_HAS_CONSTRUCTOR (init))
7810 if (TYPE_NON_AGGREGATE_CLASS (type))
7812 error ("`%D' must be initialized by constructor, not by `{...}'",
7813 decl);
7814 init = error_mark_node;
7816 else
7817 goto dont_use_constructor;
7819 else
7821 int saved_stmts_are_full_exprs_p;
7823 initialize_aggr:
7824 saved_stmts_are_full_exprs_p = 0;
7825 if (building_stmt_tree ())
7827 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7828 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7830 init = build_aggr_init (decl, init, flags);
7831 if (building_stmt_tree ())
7832 current_stmt_tree ()->stmts_are_full_exprs_p =
7833 saved_stmts_are_full_exprs_p;
7834 return init;
7837 else
7839 dont_use_constructor:
7840 if (TREE_CODE (init) != TREE_VEC)
7841 init = store_init_value (decl, init);
7844 else if (DECL_EXTERNAL (decl))
7846 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7847 goto initialize_aggr;
7848 else if (IS_AGGR_TYPE (type))
7850 tree core_type = strip_array_types (type);
7852 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7853 error ("structure `%D' with uninitialized const members", decl);
7854 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7855 error ("structure `%D' with uninitialized reference members",
7856 decl);
7858 check_for_uninitialized_const_var (decl);
7860 else
7861 check_for_uninitialized_const_var (decl);
7863 if (init && init != error_mark_node)
7864 init = build (INIT_EXPR, type, decl, init);
7866 return init;
7869 /* If DECL is not a local variable, give it RTL. */
7871 static void
7872 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7874 int toplev = toplevel_bindings_p ();
7875 int defer_p;
7877 /* Handle non-variables up front. */
7878 if (TREE_CODE (decl) != VAR_DECL)
7880 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7881 return;
7884 /* If we see a class member here, it should be a static data
7885 member. */
7886 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7888 my_friendly_assert (TREE_STATIC (decl), 19990828);
7889 /* An in-class declaration of a static data member should be
7890 external; it is only a declaration, and not a definition. */
7891 if (init == NULL_TREE)
7892 my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7895 /* Set the DECL_ASSEMBLER_NAME for the variable. */
7896 if (asmspec)
7898 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7899 /* The `register' keyword, when used together with an
7900 asm-specification, indicates that the variable should be
7901 placed in a particular register. */
7902 if (DECL_REGISTER (decl))
7903 DECL_C_HARD_REGISTER (decl) = 1;
7906 /* We don't create any RTL for local variables. */
7907 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7908 return;
7910 /* We defer emission of local statics until the corresponding
7911 DECL_STMT is expanded. */
7912 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7914 /* We try to defer namespace-scope static constants so that they are
7915 not emitted into the object file unnecessarily. */
7916 if (!DECL_VIRTUAL_P (decl)
7917 && TREE_READONLY (decl)
7918 && DECL_INITIAL (decl) != NULL_TREE
7919 && DECL_INITIAL (decl) != error_mark_node
7920 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7921 && toplev
7922 && !TREE_PUBLIC (decl))
7924 /* Fool with the linkage of static consts according to #pragma
7925 interface. */
7926 if (!interface_unknown && !TREE_PUBLIC (decl))
7928 TREE_PUBLIC (decl) = 1;
7929 DECL_EXTERNAL (decl) = interface_only;
7932 defer_p = 1;
7934 /* Likewise for template instantiations. */
7935 else if (DECL_COMDAT (decl))
7936 defer_p = 1;
7938 /* If we're deferring the variable, we only need to make RTL if
7939 there's an ASMSPEC. Otherwise, we'll lazily create it later when
7940 we need it. (There's no way to lazily create RTL for things that
7941 have assembly specs because the information about the specifier
7942 isn't stored in the tree, yet) */
7943 if (defer_p && asmspec)
7944 make_decl_rtl (decl, asmspec);
7945 /* If we're not deferring, go ahead and assemble the variable. */
7946 else if (!defer_p)
7947 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7950 /* The old ARM scoping rules injected variables declared in the
7951 initialization statement of a for-statement into the surrounding
7952 scope. We support this usage, in order to be backward-compatible.
7953 DECL is a just-declared VAR_DECL; if necessary inject its
7954 declaration into the surrounding scope. */
7956 void
7957 maybe_inject_for_scope_var (tree decl)
7959 timevar_push (TV_NAME_LOOKUP);
7960 if (!DECL_NAME (decl))
7961 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
7963 /* Declarations of __FUNCTION__ and its ilk appear magically when
7964 the variable is first used. If that happens to be inside a
7965 for-loop, we don't want to do anything special. */
7966 if (DECL_PRETTY_FUNCTION_P (decl))
7967 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
7969 if (current_binding_level->is_for_scope)
7971 struct cp_binding_level *outer
7972 = current_binding_level->level_chain;
7974 /* Check to see if the same name is already bound at the outer
7975 level, either because it was directly declared, or because a
7976 dead for-decl got preserved. In either case, the code would
7977 not have been valid under the ARM scope rules, so clear
7978 is_for_scope for the current_binding_level.
7980 Otherwise, we need to preserve the temp slot for decl to last
7981 into the outer binding level. */
7983 cxx_binding *outer_binding
7984 = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
7986 if (outer_binding && BINDING_LEVEL (outer_binding) == outer
7987 && (TREE_CODE (BINDING_VALUE (outer_binding)) == VAR_DECL)
7988 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7990 BINDING_VALUE (outer_binding)
7991 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7992 current_binding_level->is_for_scope = 0;
7995 timevar_pop (TV_NAME_LOOKUP);
7998 /* Generate code to initialize DECL (a local variable). */
8000 static void
8001 initialize_local_var (tree decl, tree init)
8003 tree type = TREE_TYPE (decl);
8005 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
8006 || TREE_CODE (decl) == RESULT_DECL,
8007 20021010);
8008 my_friendly_assert (!TREE_STATIC (decl), 20021010);
8010 if (DECL_SIZE (decl) == NULL_TREE)
8012 /* If we used it already as memory, it must stay in memory. */
8013 DECL_INITIAL (decl) = NULL_TREE;
8014 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8017 if (DECL_SIZE (decl) && type != error_mark_node)
8019 int already_used;
8021 /* Compute and store the initial value. */
8022 already_used = TREE_USED (decl) || TREE_USED (type);
8024 /* Perform the initialization. */
8025 if (init)
8027 int saved_stmts_are_full_exprs_p;
8029 my_friendly_assert (building_stmt_tree (), 20000906);
8030 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8031 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8032 finish_expr_stmt (init);
8033 current_stmt_tree ()->stmts_are_full_exprs_p =
8034 saved_stmts_are_full_exprs_p;
8037 /* Set this to 0 so we can tell whether an aggregate which was
8038 initialized was ever used. Don't do this if it has a
8039 destructor, so we don't complain about the 'resource
8040 allocation is initialization' idiom. Now set
8041 attribute((unused)) on types so decls of that type will be
8042 marked used. (see TREE_USED, above.) */
8043 if (TYPE_NEEDS_CONSTRUCTING (type)
8044 && ! already_used
8045 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8046 && DECL_NAME (decl))
8047 TREE_USED (decl) = 0;
8048 else if (already_used)
8049 TREE_USED (decl) = 1;
8052 /* Generate a cleanup, if necessary. */
8053 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8055 tree cleanup;
8057 /* Compute the cleanup. */
8058 cleanup = cxx_maybe_build_cleanup (decl);
8060 /* Record the cleanup required for this declaration. */
8061 if (DECL_SIZE (decl) && cleanup)
8062 finish_decl_cleanup (decl, cleanup);
8066 /* Finish processing of a declaration;
8067 install its line number and initial value.
8068 If the length of an array type is not known before,
8069 it must be determined now, from the initial value, or it is an error.
8071 INIT holds the value of an initializer that should be allowed to escape
8072 the normal rules.
8074 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8075 if the (init) syntax was used. */
8077 void
8078 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
8080 register tree type;
8081 tree ttype = NULL_TREE;
8082 const char *asmspec = NULL;
8083 int was_readonly = 0;
8085 if (! decl)
8087 if (init)
8088 error ("assignment (not initialization) in declaration");
8089 return;
8092 /* If a name was specified, get the string. */
8093 if (current_binding_level == global_binding_level)
8094 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8095 if (asmspec_tree)
8096 asmspec = TREE_STRING_POINTER (asmspec_tree);
8098 if (init && TREE_CODE (init) == NAMESPACE_DECL)
8100 error ("cannot initialize `%D' to namespace `%D'",
8101 decl, init);
8102 init = NULL_TREE;
8105 if (current_class_type
8106 && CP_DECL_CONTEXT (decl) == current_class_type
8107 && TYPE_BEING_DEFINED (current_class_type)
8108 && (DECL_INITIAL (decl) || init))
8109 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8111 if (TREE_CODE (decl) == VAR_DECL
8112 && DECL_CONTEXT (decl)
8113 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8114 && DECL_CONTEXT (decl) != current_namespace
8115 && init)
8117 /* Leave the namespace of the object. */
8118 pop_decl_namespace ();
8121 type = TREE_TYPE (decl);
8123 if (type == error_mark_node)
8124 return;
8126 if (TYPE_HAS_MUTABLE_P (type))
8127 TREE_READONLY (decl) = 0;
8129 if (processing_template_decl)
8131 /* Add this declaration to the statement-tree. */
8132 if (at_function_scope_p ()
8133 && TREE_CODE (decl) != RESULT_DECL)
8134 add_decl_stmt (decl);
8136 if (init && DECL_INITIAL (decl))
8137 DECL_INITIAL (decl) = init;
8138 goto finish_end0;
8141 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8142 my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8144 /* Take care of TYPE_DECLs up front. */
8145 if (TREE_CODE (decl) == TYPE_DECL)
8147 if (type != error_mark_node
8148 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8150 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8151 warning ("shadowing previous type declaration of `%#D'", decl);
8152 set_identifier_type_value (DECL_NAME (decl), type);
8153 CLASSTYPE_GOT_SEMICOLON (type) = 1;
8156 /* If we have installed this as the canonical typedef for this
8157 type, and that type has not been defined yet, delay emitting
8158 the debug information for it, as we will emit it later. */
8159 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8160 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8161 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8163 rest_of_decl_compilation (decl, NULL,
8164 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8165 goto finish_end;
8168 if (TREE_CODE (decl) != FUNCTION_DECL)
8169 ttype = target_type (type);
8171 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8172 && TYPE_NEEDS_CONSTRUCTING (type))
8174 /* Currently, GNU C++ puts constants in text space, making them
8175 impossible to initialize. In the future, one would hope for
8176 an operating system which understood the difference between
8177 initialization and the running of a program. */
8178 was_readonly = 1;
8179 TREE_READONLY (decl) = 0;
8182 if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8184 /* This must override the asm specifier which was placed by
8185 grokclassfn. Lay this out fresh. */
8186 SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8187 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8188 make_decl_rtl (decl, asmspec);
8190 else if (TREE_CODE (decl) == RESULT_DECL)
8191 init = check_initializer (decl, init, flags);
8192 else if (TREE_CODE (decl) == VAR_DECL)
8194 /* Only PODs can have thread-local storage. Other types may require
8195 various kinds of non-trivial initialization. */
8196 if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8197 error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8198 decl, TREE_TYPE (decl));
8199 /* Convert the initializer to the type of DECL, if we have not
8200 already initialized DECL. */
8201 if (!DECL_INITIALIZED_P (decl)
8202 /* If !DECL_EXTERNAL then DECL is being defined. In the
8203 case of a static data member initialized inside the
8204 class-specifier, there can be an initializer even if DECL
8205 is *not* defined. */
8206 && (!DECL_EXTERNAL (decl) || init))
8208 init = check_initializer (decl, init, flags);
8209 /* Thread-local storage cannot be dynamically initialized. */
8210 if (DECL_THREAD_LOCAL (decl) && init)
8212 error ("`%D' is thread-local and so cannot be dynamically "
8213 "initialized", decl);
8214 init = NULL_TREE;
8216 /* Handle:
8218 [dcl.init]
8220 The memory occupied by any object of static storage
8221 duration is zero-initialized at program startup before
8222 any other initialization takes place.
8224 We cannot create an appropriate initializer until after
8225 the type of DECL is finalized. If DECL_INITIAL is set,
8226 then the DECL is statically initialized, and any
8227 necessary zero-initialization has already been performed. */
8228 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8229 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8230 /*nelts=*/NULL_TREE,
8231 /*static_storage_p=*/true);
8232 /* Remember that the initialization for this variable has
8233 taken place. */
8234 DECL_INITIALIZED_P (decl) = 1;
8236 /* If the variable has an array type, lay out the type, even if
8237 there is no initializer. It is valid to index through the
8238 array, and we must get TYPE_ALIGN set correctly on the array
8239 type. */
8240 else if (TREE_CODE (type) == ARRAY_TYPE)
8241 layout_type (type);
8244 /* Add this declaration to the statement-tree. This needs to happen
8245 after the call to check_initializer so that the DECL_STMT for a
8246 reference temp is added before the DECL_STMT for the reference itself. */
8247 if (building_stmt_tree ()
8248 && at_function_scope_p ()
8249 && TREE_CODE (decl) != RESULT_DECL)
8250 add_decl_stmt (decl);
8252 if (TREE_CODE (decl) == VAR_DECL)
8253 layout_var_decl (decl);
8255 /* Output the assembler code and/or RTL code for variables and functions,
8256 unless the type is an undefined structure or union.
8257 If not, it will get done when the type is completed. */
8258 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8259 || TREE_CODE (decl) == RESULT_DECL)
8261 if (TREE_CODE (decl) == VAR_DECL)
8262 maybe_commonize_var (decl);
8264 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8266 if (TREE_CODE (type) == FUNCTION_TYPE
8267 || TREE_CODE (type) == METHOD_TYPE)
8268 abstract_virtuals_error (decl,
8269 strip_array_types (TREE_TYPE (type)));
8270 else
8271 abstract_virtuals_error (decl, strip_array_types (type));
8273 if (TREE_CODE (decl) == FUNCTION_DECL
8274 || TREE_TYPE (decl) == error_mark_node)
8275 /* No initialization required. */
8277 else if (DECL_EXTERNAL (decl)
8278 && ! (DECL_LANG_SPECIFIC (decl)
8279 && DECL_NOT_REALLY_EXTERN (decl)))
8281 if (init)
8282 DECL_INITIAL (decl) = init;
8284 else
8286 /* A variable definition. */
8287 if (DECL_FUNCTION_SCOPE_P (decl))
8289 /* This is a local declaration. */
8290 if (doing_semantic_analysis_p ())
8291 maybe_inject_for_scope_var (decl);
8292 /* Initialize the local variable. */
8293 if (processing_template_decl)
8295 if (init || DECL_INITIAL (decl) == error_mark_node)
8296 DECL_INITIAL (decl) = init;
8298 else if (!TREE_STATIC (decl))
8299 initialize_local_var (decl, init);
8302 if (TREE_STATIC (decl))
8303 expand_static_init (decl, init);
8305 finish_end0:
8307 /* Undo call to `pushclass' that was done in `start_decl'
8308 due to initialization of qualified member variable.
8309 I.e., Foo::x = 10; */
8311 tree context = CP_DECL_CONTEXT (decl);
8312 if (context
8313 && TYPE_P (context)
8314 && (TREE_CODE (decl) == VAR_DECL
8315 /* We also have a pushclass done that we need to undo here
8316 if we're at top level and declare a method. */
8317 || TREE_CODE (decl) == FUNCTION_DECL)
8318 /* If size hasn't been set, we're still defining it,
8319 and therefore inside the class body; don't pop
8320 the binding level.. */
8321 && COMPLETE_TYPE_P (context)
8322 && context == current_class_type)
8323 pop_nested_class ();
8327 finish_end:
8329 if (was_readonly)
8330 TREE_READONLY (decl) = 1;
8333 /* This is here for a midend callback from c-common.c */
8335 void
8336 finish_decl (tree decl, tree init, tree asmspec_tree)
8338 cp_finish_decl (decl, init, asmspec_tree, 0);
8341 /* Returns a declaration for a VAR_DECL as if:
8343 extern "C" TYPE NAME;
8345 had been seen. Used to create compiler-generated global
8346 variables. */
8348 tree
8349 declare_global_var (tree name, tree type)
8351 tree decl;
8353 push_to_top_level ();
8354 decl = build_decl (VAR_DECL, name, type);
8355 TREE_PUBLIC (decl) = 1;
8356 DECL_EXTERNAL (decl) = 1;
8357 DECL_ARTIFICIAL (decl) = 1;
8358 pushdecl (decl);
8359 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8360 pop_from_top_level ();
8362 return decl;
8365 /* Returns a pointer to the `atexit' function. Note that if
8366 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8367 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8369 static tree
8370 get_atexit_node (void)
8372 tree atexit_fndecl;
8373 tree arg_types;
8374 tree fn_type;
8375 tree fn_ptr_type;
8376 const char *name;
8378 if (atexit_node)
8379 return atexit_node;
8381 if (flag_use_cxa_atexit)
8383 /* The declaration for `__cxa_atexit' is:
8385 int __cxa_atexit (void (*)(void *), void *, void *)
8387 We build up the argument types and then then function type
8388 itself. */
8390 /* First, build the pointer-to-function type for the first
8391 argument. */
8392 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8393 fn_type = build_function_type (void_type_node, arg_types);
8394 fn_ptr_type = build_pointer_type (fn_type);
8395 /* Then, build the rest of the argument types. */
8396 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8397 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8398 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8399 /* And the final __cxa_atexit type. */
8400 fn_type = build_function_type (integer_type_node, arg_types);
8401 fn_ptr_type = build_pointer_type (fn_type);
8402 name = "__cxa_atexit";
8404 else
8406 /* The declaration for `atexit' is:
8408 int atexit (void (*)());
8410 We build up the argument types and then then function type
8411 itself. */
8412 fn_type = build_function_type (void_type_node, void_list_node);
8413 fn_ptr_type = build_pointer_type (fn_type);
8414 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8415 /* Build the final atexit type. */
8416 fn_type = build_function_type (integer_type_node, arg_types);
8417 name = "atexit";
8420 /* Now, build the function declaration. */
8421 push_lang_context (lang_name_c);
8422 atexit_fndecl = build_library_fn_ptr (name, fn_type);
8423 mark_used (atexit_fndecl);
8424 pop_lang_context ();
8425 atexit_node = default_conversion (atexit_fndecl);
8427 return atexit_node;
8430 /* Returns the __dso_handle VAR_DECL. */
8432 static tree
8433 get_dso_handle_node (void)
8435 if (dso_handle_node)
8436 return dso_handle_node;
8438 /* Declare the variable. */
8439 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8440 ptr_type_node);
8442 return dso_handle_node;
8445 /* Begin a new function with internal linkage whose job will be simply
8446 to destroy some particular variable. */
8448 static tree
8449 start_cleanup_fn (void)
8451 static int counter = 0;
8452 int old_interface_only = interface_only;
8453 int old_interface_unknown = interface_unknown;
8454 char name[32];
8455 tree parmtypes;
8456 tree fntype;
8457 tree fndecl;
8459 push_to_top_level ();
8461 /* No need to mangle this. */
8462 push_lang_context (lang_name_c);
8464 interface_only = 0;
8465 interface_unknown = 1;
8467 /* Build the parameter-types. */
8468 parmtypes = void_list_node;
8469 /* Functions passed to __cxa_atexit take an additional parameter.
8470 We'll just ignore it. After we implement the new calling
8471 convention for destructors, we can eliminate the use of
8472 additional cleanup functions entirely in the -fnew-abi case. */
8473 if (flag_use_cxa_atexit)
8474 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8475 /* Build the function type itself. */
8476 fntype = build_function_type (void_type_node, parmtypes);
8477 /* Build the name of the function. */
8478 sprintf (name, "__tcf_%d", counter++);
8479 /* Build the function declaration. */
8480 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8481 /* It's a function with internal linkage, generated by the
8482 compiler. */
8483 TREE_PUBLIC (fndecl) = 0;
8484 DECL_ARTIFICIAL (fndecl) = 1;
8485 /* Make the function `inline' so that it is only emitted if it is
8486 actually needed. It is unlikely that it will be inlined, since
8487 it is only called via a function pointer, but we avoid unnecessary
8488 emissions this way. */
8489 DECL_INLINE (fndecl) = 1;
8490 /* Build the parameter. */
8491 if (flag_use_cxa_atexit)
8493 tree parmdecl;
8495 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8496 DECL_CONTEXT (parmdecl) = fndecl;
8497 TREE_USED (parmdecl) = 1;
8498 DECL_ARGUMENTS (fndecl) = parmdecl;
8501 pushdecl (fndecl);
8502 start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8504 interface_unknown = old_interface_unknown;
8505 interface_only = old_interface_only;
8507 pop_lang_context ();
8509 return current_function_decl;
8512 /* Finish the cleanup function begun by start_cleanup_fn. */
8514 static void
8515 end_cleanup_fn (void)
8517 expand_body (finish_function (0));
8519 pop_from_top_level ();
8522 /* Generate code to handle the destruction of DECL, an object with
8523 static storage duration. */
8525 void
8526 register_dtor_fn (tree decl)
8528 tree cleanup;
8529 tree compound_stmt;
8530 tree args;
8531 tree fcall;
8532 int saved_flag_access_control;
8534 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8535 return;
8537 /* Call build_cleanup before we enter the anonymous function so that
8538 any access checks will be done relative to the current scope,
8539 rather than the scope of the anonymous function. */
8540 build_cleanup (decl);
8542 /* Now start the function. */
8543 cleanup = start_cleanup_fn ();
8545 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8546 to the original function, rather than the anonymous one. That
8547 will make the back-end think that nested functions are in use,
8548 which causes confusion. */
8549 saved_flag_access_control = flag_access_control;
8550 scope_chain->check_access = flag_access_control = 0;
8551 fcall = build_cleanup (decl);
8552 scope_chain->check_access = flag_access_control = saved_flag_access_control;
8554 /* Create the body of the anonymous function. */
8555 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
8556 finish_expr_stmt (fcall);
8557 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
8558 end_cleanup_fn ();
8560 /* Call atexit with the cleanup function. */
8561 cxx_mark_addressable (cleanup);
8562 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8563 if (flag_use_cxa_atexit)
8565 args = tree_cons (NULL_TREE,
8566 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8567 NULL_TREE);
8568 args = tree_cons (NULL_TREE, null_pointer_node, args);
8569 args = tree_cons (NULL_TREE, cleanup, args);
8571 else
8572 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8573 finish_expr_stmt (build_function_call (get_atexit_node (), args));
8576 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8577 is its initializer. Generate code to handle the construction
8578 and destruction of DECL. */
8580 static void
8581 expand_static_init (tree decl, tree init)
8583 tree oldstatic;
8585 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8586 my_friendly_assert (TREE_STATIC (decl), 20021010);
8588 /* Some variables require no initialization. */
8589 if (!init
8590 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8591 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8592 return;
8594 oldstatic = value_member (decl, static_aggregates);
8596 if (oldstatic)
8598 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8599 error ("multiple initializations given for `%D'", decl);
8601 else if (! toplevel_bindings_p ())
8603 /* Emit code to perform this initialization but once. */
8604 tree if_stmt;
8605 tree then_clause;
8606 tree assignment;
8607 tree guard;
8608 tree guard_init;
8610 /* Emit code to perform this initialization but once. This code
8611 looks like:
8613 static int guard = 0;
8614 if (!guard) {
8615 // Do initialization.
8616 guard = 1;
8617 // Register variable for destruction at end of program.
8620 Note that the `temp' variable is only set to 1 *after* the
8621 initialization is complete. This ensures that an exception,
8622 thrown during the construction, will cause the variable to
8623 reinitialized when we pass through this code again, as per:
8625 [stmt.dcl]
8627 If the initialization exits by throwing an exception, the
8628 initialization is not complete, so it will be tried again
8629 the next time control enters the declaration.
8631 In theory, this process should be thread-safe, too; multiple
8632 threads should not be able to initialize the variable more
8633 than once. We don't yet attempt to ensure thread-safety. */
8635 /* Create the guard variable. */
8636 guard = get_guard (decl);
8638 /* Begin the conditional initialization. */
8639 if_stmt = begin_if_stmt ();
8640 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8641 then_clause = begin_compound_stmt (/*has_no_scope=*/0);
8643 /* Do the initialization itself. */
8644 assignment = init ? init : NULL_TREE;
8646 /* Once the assignment is complete, set TEMP to 1. Since the
8647 construction of the static object is complete at this point,
8648 we want to make sure TEMP is set to 1 even if a temporary
8649 constructed during the initialization throws an exception
8650 when it is destroyed. So, we combine the initialization and
8651 the assignment to TEMP into a single expression, ensuring
8652 that when we call finish_expr_stmt the cleanups will not be
8653 run until after TEMP is set to 1. */
8654 guard_init = set_guard (guard);
8655 if (assignment)
8657 assignment = tree_cons (NULL_TREE, assignment,
8658 build_tree_list (NULL_TREE,
8659 guard_init));
8660 assignment = build_compound_expr (assignment);
8662 else
8663 assignment = guard_init;
8664 finish_expr_stmt (assignment);
8666 /* Use atexit to register a function for destroying this static
8667 variable. */
8668 register_dtor_fn (decl);
8670 finish_compound_stmt (/*has_no_scope=*/0, then_clause);
8671 finish_then_clause (if_stmt);
8672 finish_if_stmt ();
8674 else
8675 static_aggregates = tree_cons (init, decl, static_aggregates);
8678 /* Finish the declaration of a catch-parameter. */
8680 tree
8681 start_handler_parms (tree declspecs, tree declarator)
8683 tree decl;
8684 if (declspecs)
8686 decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8687 1, NULL);
8688 if (decl == NULL_TREE)
8689 error ("invalid catch parameter");
8691 else
8692 decl = NULL_TREE;
8694 return decl;
8698 /* Make TYPE a complete type based on INITIAL_VALUE.
8699 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8700 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8703 complete_array_type (tree type, tree initial_value, int do_default)
8705 register tree maxindex = NULL_TREE;
8706 int value = 0;
8708 if (initial_value)
8710 /* An array of character type can be initialized from a
8711 brace-enclosed string constant. */
8712 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8713 && TREE_CODE (initial_value) == CONSTRUCTOR
8714 && CONSTRUCTOR_ELTS (initial_value)
8715 && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8716 == STRING_CST)
8717 && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8718 initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8720 /* Note MAXINDEX is really the maximum index, one less than the
8721 size. */
8722 if (TREE_CODE (initial_value) == STRING_CST)
8724 int eltsize
8725 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8726 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8727 / eltsize) - 1, 0);
8729 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8731 tree elts = CONSTRUCTOR_ELTS (initial_value);
8733 maxindex = ssize_int (-1);
8734 for (; elts; elts = TREE_CHAIN (elts))
8736 if (TREE_PURPOSE (elts))
8737 maxindex = TREE_PURPOSE (elts);
8738 else
8739 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8741 maxindex = copy_node (maxindex);
8743 else
8745 /* Make an error message unless that happened already. */
8746 if (initial_value != error_mark_node)
8747 value = 1;
8748 else
8749 initial_value = NULL_TREE;
8751 /* Prevent further error messages. */
8752 maxindex = build_int_2 (0, 0);
8756 if (!maxindex)
8758 if (do_default)
8759 maxindex = build_int_2 (0, 0);
8760 value = 2;
8763 if (maxindex)
8765 tree itype;
8766 tree domain;
8768 domain = build_index_type (maxindex);
8769 TYPE_DOMAIN (type) = domain;
8771 if (! TREE_TYPE (maxindex))
8772 TREE_TYPE (maxindex) = domain;
8773 if (initial_value)
8774 itype = TREE_TYPE (initial_value);
8775 else
8776 itype = NULL;
8777 if (itype && !TYPE_DOMAIN (itype))
8778 TYPE_DOMAIN (itype) = domain;
8779 /* The type of the main variant should never be used for arrays
8780 of different sizes. It should only ever be completed with the
8781 size of the array. */
8782 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8783 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8786 /* Lay out the type now that we can get the real answer. */
8788 layout_type (type);
8790 return value;
8793 /* Return zero if something is declared to be a member of type
8794 CTYPE when in the context of CUR_TYPE. STRING is the error
8795 message to print in that case. Otherwise, quietly return 1. */
8797 static int
8798 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8800 if (ctype && ctype != cur_type)
8802 if (flags == DTOR_FLAG)
8803 error ("destructor for alien class `%T' cannot be a member",
8804 ctype);
8805 else
8806 error ("constructor for alien class `%T' cannot be a member",
8807 ctype);
8808 return 0;
8810 return 1;
8813 /* Subroutine of `grokdeclarator'. */
8815 /* Generate errors possibly applicable for a given set of specifiers.
8816 This is for ARM $7.1.2. */
8818 static void
8819 bad_specifiers (tree object,
8820 const char* type,
8821 int virtualp,
8822 int quals,
8823 int inlinep,
8824 int friendp,
8825 int raises)
8827 if (virtualp)
8828 error ("`%D' declared as a `virtual' %s", object, type);
8829 if (inlinep)
8830 error ("`%D' declared as an `inline' %s", object, type);
8831 if (quals)
8832 error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8833 object, type);
8834 if (friendp)
8835 cp_error_at ("`%D' declared as a friend", object);
8836 if (raises
8837 && (TREE_CODE (object) == TYPE_DECL
8838 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8839 && !TYPE_REFFN_P (TREE_TYPE (object))
8840 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8841 cp_error_at ("`%D' declared with an exception specification", object);
8844 /* CTYPE is class type, or null if non-class.
8845 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8846 or METHOD_TYPE.
8847 DECLARATOR is the function's name.
8848 VIRTUALP is truthvalue of whether the function is virtual or not.
8849 FLAGS are to be passed through to `grokclassfn'.
8850 QUALS are qualifiers indicating whether the function is `const'
8851 or `volatile'.
8852 RAISES is a list of exceptions that this function can raise.
8853 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8854 not look, and -1 if we should not call `grokclassfn' at all.
8856 Returns `NULL_TREE' if something goes wrong, after issuing
8857 applicable error messages. */
8859 static tree
8860 grokfndecl (tree ctype,
8861 tree type,
8862 tree declarator,
8863 tree orig_declarator,
8864 int virtualp,
8865 enum overload_flags flags,
8866 tree quals,
8867 tree raises,
8868 int check,
8869 int friendp,
8870 int publicp,
8871 int inlinep,
8872 int funcdef_flag,
8873 int template_count,
8874 tree in_namespace)
8876 tree decl;
8877 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8878 int has_default_arg = 0;
8879 tree t;
8881 if (raises)
8882 type = build_exception_variant (type, raises);
8884 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8885 /* Propagate volatile out from type to decl. */
8886 if (TYPE_VOLATILE (type))
8887 TREE_THIS_VOLATILE (decl) = 1;
8889 /* If this decl has namespace scope, set that up. */
8890 if (in_namespace)
8891 set_decl_namespace (decl, in_namespace, friendp);
8892 else if (!ctype)
8893 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8895 /* `main' and builtins have implicit 'C' linkage. */
8896 if ((MAIN_NAME_P (declarator)
8897 || (IDENTIFIER_LENGTH (declarator) > 10
8898 && IDENTIFIER_POINTER (declarator)[0] == '_'
8899 && IDENTIFIER_POINTER (declarator)[1] == '_'
8900 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8901 && current_lang_name == lang_name_cplusplus
8902 && ctype == NULL_TREE
8903 /* NULL_TREE means global namespace. */
8904 && DECL_CONTEXT (decl) == NULL_TREE)
8905 SET_DECL_LANGUAGE (decl, lang_c);
8907 /* Should probably propagate const out from type to decl I bet (mrs). */
8908 if (staticp)
8910 DECL_STATIC_FUNCTION_P (decl) = 1;
8911 DECL_CONTEXT (decl) = ctype;
8914 if (ctype)
8915 DECL_CONTEXT (decl) = ctype;
8917 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8919 if (processing_template_decl)
8920 error ("cannot declare `::main' to be a template");
8921 if (inlinep)
8922 error ("cannot declare `::main' to be inline");
8923 if (!publicp)
8924 error ("cannot declare `::main' to be static");
8925 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8926 integer_type_node))
8927 error ("`main' must return `int'");
8928 inlinep = 0;
8929 publicp = 1;
8932 /* Members of anonymous types and local classes have no linkage; make
8933 them internal. */
8934 /* FIXME what if it gets a name from typedef? */
8935 if (ctype && (TYPE_ANONYMOUS_P (ctype)
8936 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8937 publicp = 0;
8939 if (publicp)
8941 /* [basic.link]: A name with no linkage (notably, the name of a class
8942 or enumeration declared in a local scope) shall not be used to
8943 declare an entity with linkage.
8945 Only check this for public decls for now. */
8946 t = no_linkage_check (TREE_TYPE (decl));
8947 if (t)
8949 if (TYPE_ANONYMOUS_P (t))
8951 if (DECL_EXTERN_C_P (decl))
8952 /* Allow this; it's pretty common in C. */;
8953 else
8955 pedwarn ("non-local function `%#D' uses anonymous type",
8956 decl);
8957 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8958 cp_pedwarn_at ("\
8959 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8960 TYPE_NAME (t));
8963 else
8964 pedwarn ("non-local function `%#D' uses local type `%T'",
8965 decl, t);
8969 TREE_PUBLIC (decl) = publicp;
8970 if (! publicp)
8972 DECL_INTERFACE_KNOWN (decl) = 1;
8973 DECL_NOT_REALLY_EXTERN (decl) = 1;
8976 DID_INLINE_FUNC (decl) = 0;
8977 /* If the declaration was declared inline, mark it as such. */
8978 if (inlinep)
8979 DECL_DECLARED_INLINE_P (decl) = 1;
8980 /* We inline functions that are explicitly declared inline, or, when
8981 the user explicitly asks us to, all functions. */
8982 if (DECL_DECLARED_INLINE_P (decl))
8983 DECL_INLINE (decl) = 1;
8984 if (flag_inline_trees == 2 && !DECL_INLINE (decl))
8986 DID_INLINE_FUNC (decl) = 1;
8987 DECL_INLINE (decl) = 1;
8990 DECL_EXTERNAL (decl) = 1;
8991 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8993 error ("%smember function `%D' cannot have `%T' method qualifier",
8994 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8995 quals = NULL_TREE;
8998 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8999 grok_op_properties (decl, friendp);
9001 if (ctype && decl_function_context (decl))
9002 DECL_NO_STATIC_CHAIN (decl) = 1;
9004 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
9005 if (TREE_PURPOSE (t)
9006 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
9008 has_default_arg = 1;
9009 break;
9012 if (friendp
9013 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9015 if (funcdef_flag)
9016 error
9017 ("defining explicit specialization `%D' in friend declaration",
9018 orig_declarator);
9019 else
9021 tree fns = TREE_OPERAND (orig_declarator, 0);
9022 tree args = TREE_OPERAND (orig_declarator, 1);
9024 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9026 /* Something like `template <class T> friend void f<T>()'. */
9027 error ("invalid use of template-id `%D' in declaration of primary template",
9028 orig_declarator);
9029 return NULL_TREE;
9033 /* A friend declaration of the form friend void f<>(). Record
9034 the information in the TEMPLATE_ID_EXPR. */
9035 SET_DECL_IMPLICIT_INSTANTIATION (decl);
9037 if (TREE_CODE (fns) == COMPONENT_REF)
9039 /* Due to bison parser ickiness, we will have already looked
9040 up an operator_name or PFUNCNAME within the current class
9041 (see template_id in parse.y). If the current class contains
9042 such a name, we'll get a COMPONENT_REF here. Undo that. */
9044 my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
9045 == current_class_type, 20001120);
9046 fns = TREE_OPERAND (fns, 1);
9048 my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
9049 || TREE_CODE (fns) == LOOKUP_EXPR
9050 || TREE_CODE (fns) == OVERLOAD, 20001120);
9051 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
9053 if (has_default_arg)
9055 error ("default arguments are not allowed in declaration of friend template specialization `%D'",
9056 decl);
9057 return NULL_TREE;
9060 if (inlinep)
9062 error ("`inline' is not allowed in declaration of friend template specialization `%D'",
9063 decl);
9064 return NULL_TREE;
9069 if (funcdef_flag)
9070 /* Make the init_value nonzero so pushdecl knows this is not
9071 tentative. error_mark_node is replaced later with the BLOCK. */
9072 DECL_INITIAL (decl) = error_mark_node;
9074 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9075 TREE_NOTHROW (decl) = 1;
9077 /* Caller will do the rest of this. */
9078 if (check < 0)
9079 return decl;
9081 if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
9082 DECL_CONSTRUCTOR_P (decl) = 1;
9084 /* Function gets the ugly name, field gets the nice one. This call
9085 may change the type of the function (because of default
9086 parameters)! */
9087 if (ctype != NULL_TREE)
9088 grokclassfn (ctype, decl, flags, quals);
9090 decl = check_explicit_specialization (orig_declarator, decl,
9091 template_count,
9092 2 * (funcdef_flag != 0) +
9093 4 * (friendp != 0));
9094 if (decl == error_mark_node)
9095 return NULL_TREE;
9097 if (ctype != NULL_TREE
9098 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9099 && check)
9101 tree old_decl;
9103 old_decl = check_classfn (ctype, decl);
9105 if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9106 /* Because grokfndecl is always supposed to return a
9107 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9108 here. We depend on our callers to figure out that its
9109 really a template that's being returned. */
9110 old_decl = DECL_TEMPLATE_RESULT (old_decl);
9112 if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9113 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9115 /* Remove the `this' parm added by grokclassfn.
9116 XXX Isn't this done in start_function, too? */
9117 revert_static_member_fn (decl);
9118 last_function_parms = TREE_CHAIN (last_function_parms);
9120 if (old_decl && DECL_ARTIFICIAL (old_decl))
9121 error ("definition of implicitly-declared `%D'", old_decl);
9123 if (old_decl)
9125 /* Since we've smashed OLD_DECL to its
9126 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9127 if (TREE_CODE (decl) == TEMPLATE_DECL)
9128 decl = DECL_TEMPLATE_RESULT (decl);
9130 /* Attempt to merge the declarations. This can fail, in
9131 the case of some invalid specialization declarations. */
9132 push_scope (ctype);
9133 if (!duplicate_decls (decl, old_decl))
9134 error ("no `%#D' member function declared in class `%T'",
9135 decl, ctype);
9136 pop_scope (ctype);
9137 return old_decl;
9141 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9142 return NULL_TREE;
9144 if (ctype == NULL_TREE || check)
9145 return decl;
9147 if (virtualp)
9148 DECL_VIRTUAL_P (decl) = 1;
9150 return decl;
9153 /* Create a VAR_DECL named NAME with the indicated TYPE.
9155 If SCOPE is non-NULL, it is the class type or namespace containing
9156 the variable. If SCOPE is NULL, the variable should is created in
9157 the innermost enclosings scope. */
9159 static tree
9160 grokvardecl (tree type,
9161 tree name,
9162 RID_BIT_TYPE * specbits_in,
9163 int initialized,
9164 int constp,
9165 tree scope)
9167 tree decl;
9168 RID_BIT_TYPE specbits;
9170 my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE,
9171 20020808);
9173 specbits = *specbits_in;
9175 /* Compute the scope in which to place the variable. */
9176 if (!scope)
9178 /* An explicit "extern" specifier indicates a namespace-scope
9179 variable. */
9180 if (RIDBIT_SETP (RID_EXTERN, specbits))
9181 scope = current_namespace;
9182 else if (!at_function_scope_p ())
9184 scope = current_scope ();
9185 if (!scope)
9186 scope = current_namespace;
9190 if (scope
9191 && (/* If the variable is a namespace-scope variable declared in a
9192 template, we need DECL_LANG_SPECIFIC. */
9193 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9194 /* Similarly for namespace-scope variables with language linkage
9195 other than C++. */
9196 || (TREE_CODE (scope) == NAMESPACE_DECL
9197 && current_lang_name != lang_name_cplusplus)
9198 /* Similarly for static data members. */
9199 || TYPE_P (scope)))
9200 decl = build_lang_decl (VAR_DECL, name, type);
9201 else
9202 decl = build_decl (VAR_DECL, name, type);
9204 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9205 set_decl_namespace (decl, scope, 0);
9206 else
9207 DECL_CONTEXT (decl) = scope;
9209 if (name && scope && current_lang_name != lang_name_c)
9210 /* We can't mangle lazily here because we don't have any
9211 way to recover whether or not a variable was `extern
9212 "C"' later. */
9213 mangle_decl (decl);
9215 if (RIDBIT_SETP (RID_EXTERN, specbits))
9217 DECL_THIS_EXTERN (decl) = 1;
9218 DECL_EXTERNAL (decl) = !initialized;
9221 /* In class context, static means one per class,
9222 public access, and static storage. */
9223 if (DECL_CLASS_SCOPE_P (decl))
9225 TREE_PUBLIC (decl) = 1;
9226 TREE_STATIC (decl) = 1;
9227 DECL_EXTERNAL (decl) = 0;
9229 /* At top level, either `static' or no s.c. makes a definition
9230 (perhaps tentative), and absence of `static' makes it public. */
9231 else if (toplevel_bindings_p ())
9233 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9234 && (DECL_THIS_EXTERN (decl) || ! constp));
9235 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9237 /* Not at top level, only `static' makes a static definition. */
9238 else
9240 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9241 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9244 if (RIDBIT_SETP (RID_THREAD, specbits))
9246 if (targetm.have_tls)
9247 DECL_THREAD_LOCAL (decl) = 1;
9248 else
9249 /* A mere warning is sure to result in improper semantics
9250 at runtime. Don't bother to allow this to compile. */
9251 error ("thread-local storage not supported for this target");
9254 if (TREE_PUBLIC (decl))
9256 /* [basic.link]: A name with no linkage (notably, the name of a class
9257 or enumeration declared in a local scope) shall not be used to
9258 declare an entity with linkage.
9260 Only check this for public decls for now. */
9261 tree t = no_linkage_check (TREE_TYPE (decl));
9262 if (t)
9264 if (TYPE_ANONYMOUS_P (t))
9265 /* Ignore for now; `enum { foo } e' is pretty common. */;
9266 else
9267 pedwarn ("non-local variable `%#D' uses local type `%T'",
9268 decl, t);
9272 return decl;
9275 /* Create and return a canonical pointer to member function type, for
9276 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9278 tree
9279 build_ptrmemfunc_type (tree type)
9281 tree field, fields;
9282 tree t;
9283 tree unqualified_variant = NULL_TREE;
9285 if (type == error_mark_node)
9286 return type;
9288 /* If a canonical type already exists for this type, use it. We use
9289 this method instead of type_hash_canon, because it only does a
9290 simple equality check on the list of field members. */
9292 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9293 return t;
9295 /* Make sure that we always have the unqualified pointer-to-member
9296 type first. */
9297 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9298 unqualified_variant
9299 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9301 t = make_aggr_type (RECORD_TYPE);
9302 /* Let the front-end know this is a pointer to member function... */
9303 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9304 /* ... and not really an aggregate. */
9305 SET_IS_AGGR_TYPE (t, 0);
9307 field = build_decl (FIELD_DECL, pfn_identifier, type);
9308 fields = field;
9310 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9311 TREE_CHAIN (field) = fields;
9312 fields = field;
9314 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9316 /* Zap out the name so that the back-end will give us the debugging
9317 information for this anonymous RECORD_TYPE. */
9318 TYPE_NAME (t) = NULL_TREE;
9320 /* If this is not the unqualified form of this pointer-to-member
9321 type, set the TYPE_MAIN_VARIANT for this type to be the
9322 unqualified type. Since they are actually RECORD_TYPEs that are
9323 not variants of each other, we must do this manually. */
9324 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9326 t = build_qualified_type (t, cp_type_quals (type));
9327 TYPE_MAIN_VARIANT (t) = unqualified_variant;
9328 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9329 TYPE_NEXT_VARIANT (unqualified_variant) = t;
9332 /* Cache this pointer-to-member type so that we can find it again
9333 later. */
9334 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9336 /* Seems to be wanted. */
9337 CLASSTYPE_GOT_SEMICOLON (t) = 1;
9339 return t;
9342 /* Create and return a pointer to data member type. */
9344 tree
9345 build_ptrmem_type (tree class_type, tree member_type)
9347 return build_pointer_type (build_offset_type (class_type, member_type));
9350 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9351 Check to see that the definition is valid. Issue appropriate error
9352 messages. Return 1 if the definition is particularly bad, or 0
9353 otherwise. */
9356 check_static_variable_definition (tree decl, tree type)
9358 /* Motion 10 at San Diego: If a static const integral data member is
9359 initialized with an integral constant expression, the initializer
9360 may appear either in the declaration (within the class), or in
9361 the definition, but not both. If it appears in the class, the
9362 member is a member constant. The file-scope definition is always
9363 required. */
9364 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9366 error ("invalid in-class initialization of static data member of non-integral type `%T'",
9367 type);
9368 /* If we just return the declaration, crashes will sometimes
9369 occur. We therefore return void_type_node, as if this was a
9370 friend declaration, to cause callers to completely ignore
9371 this declaration. */
9372 return 1;
9374 else if (!CP_TYPE_CONST_P (type))
9375 error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9376 decl);
9377 else if (pedantic && !INTEGRAL_TYPE_P (type))
9378 pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9380 return 0;
9383 /* Given the SIZE (i.e., number of elements) in an array, compute an
9384 appropriate index type for the array. If non-NULL, NAME is the
9385 name of the thing being declared. */
9387 tree
9388 compute_array_index_type (tree name, tree size)
9390 tree itype;
9392 /* If this involves a template parameter, it will be a constant at
9393 instantiation time, but we don't know what the value is yet.
9394 Even if no template parameters are involved, we may an expression
9395 that is not a constant; we don't even simplify `1 + 2' when
9396 processing a template. */
9397 if (processing_template_decl)
9399 /* Resolve a qualified reference to an enumerator or static
9400 const data member of ours. */
9401 if (TREE_CODE (size) == SCOPE_REF
9402 && TREE_OPERAND (size, 0) == current_class_type)
9404 tree t = lookup_field (current_class_type,
9405 TREE_OPERAND (size, 1), 0, false);
9406 if (t)
9407 size = t;
9410 return build_index_type (build_min (MINUS_EXPR, sizetype,
9411 size, integer_one_node));
9414 /* The size might be the result of a cast. */
9415 STRIP_TYPE_NOPS (size);
9417 /* It might be a const variable or enumeration constant. */
9418 size = decl_constant_value (size);
9420 /* The array bound must be an integer type. */
9421 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9422 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9423 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9425 if (name)
9426 error ("size of array `%D' has non-integer type", name);
9427 else
9428 error ("size of array has non-integer type");
9429 size = integer_one_node;
9432 /* Normally, the array-bound will be a constant. */
9433 if (TREE_CODE (size) == INTEGER_CST)
9435 /* Check to see if the array bound overflowed. Make that an
9436 error, no matter how generous we're being. */
9437 int old_flag_pedantic_errors = flag_pedantic_errors;
9438 int old_pedantic = pedantic;
9439 pedantic = flag_pedantic_errors = 1;
9440 constant_expression_warning (size);
9441 pedantic = old_pedantic;
9442 flag_pedantic_errors = old_flag_pedantic_errors;
9444 /* An array must have a positive number of elements. */
9445 if (INT_CST_LT (size, integer_zero_node))
9447 if (name)
9448 error ("size of array `%D' is negative", name);
9449 else
9450 error ("size of array is negative");
9451 size = integer_one_node;
9453 /* Except that an extension we allow zero-sized arrays. We
9454 always allow them in system headers because glibc uses
9455 them. */
9456 else if (integer_zerop (size) && pedantic && !in_system_header)
9458 if (name)
9459 pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9460 else
9461 pedwarn ("ISO C++ forbids zero-size array");
9464 else if (TREE_CONSTANT (size))
9466 /* `(int) &fn' is not a valid array bound. */
9467 if (name)
9468 error ("size of array `%D' is not an integral constant-expression",
9469 name);
9470 else
9471 error ("size of array is not an integral constant-expression");
9474 /* Compute the index of the largest element in the array. It is
9475 one less than the number of elements in the array. */
9476 itype
9477 = fold (cp_build_binary_op (MINUS_EXPR,
9478 cp_convert (ssizetype, size),
9479 cp_convert (ssizetype,
9480 integer_one_node)));
9482 /* Check for variable-sized arrays. We allow such things as an
9483 extension, even though they are not allowed in ANSI/ISO C++. */
9484 if (!TREE_CONSTANT (itype))
9486 if (pedantic)
9488 if (name)
9489 pedwarn ("ISO C++ forbids variable-size array `%D'",
9490 name);
9491 else
9492 pedwarn ("ISO C++ forbids variable-size array");
9495 /* Create a variable-sized array index type. */
9496 itype = variable_size (itype);
9498 /* Make sure that there was no overflow when creating to a signed
9499 index type. (For example, on a 32-bit machine, an array with
9500 size 2^32 - 1 is too big.) */
9501 else if (TREE_OVERFLOW (itype))
9503 error ("overflow in array dimension");
9504 TREE_OVERFLOW (itype) = 0;
9507 /* Create and return the appropriate index type. */
9508 return build_index_type (itype);
9511 /* Returns the scope (if any) in which the entity declared by
9512 DECLARATOR will be located. If the entity was declared with an
9513 unqualified name, NULL_TREE is returned. */
9515 tree
9516 get_scope_of_declarator (tree declarator)
9518 if (!declarator)
9519 return NULL_TREE;
9521 switch (TREE_CODE (declarator))
9523 case CALL_EXPR:
9524 case ARRAY_REF:
9525 case INDIRECT_REF:
9526 case ADDR_EXPR:
9527 /* For any of these, the main declarator is the first operand. */
9528 return get_scope_of_declarator (TREE_OPERAND
9529 (declarator, 0));
9531 case SCOPE_REF:
9532 /* For a pointer-to-member, continue descending. */
9533 if (TREE_CODE (TREE_OPERAND (declarator, 1))
9534 == INDIRECT_REF)
9535 return get_scope_of_declarator (TREE_OPERAND
9536 (declarator, 1));
9537 /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9538 which the declaration occurs is the first operand. */
9539 return TREE_OPERAND (declarator, 0);
9541 case TREE_LIST:
9542 /* Attributes to be applied. The declarator is TREE_VALUE. */
9543 return get_scope_of_declarator (TREE_VALUE (declarator));
9545 default:
9546 /* Otherwise, we have a declarator-id which is not a qualified
9547 name; the entity will be declared in the current scope. */
9548 return NULL_TREE;
9552 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9553 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9554 with this type. */
9556 static tree
9557 create_array_type_for_decl (tree name, tree type, tree size)
9559 tree itype = NULL_TREE;
9560 const char* error_msg;
9562 /* If things have already gone awry, bail now. */
9563 if (type == error_mark_node || size == error_mark_node)
9564 return error_mark_node;
9566 /* Assume that everything will go OK. */
9567 error_msg = NULL;
9569 /* There are some types which cannot be array elements. */
9570 switch (TREE_CODE (type))
9572 case VOID_TYPE:
9573 error_msg = "array of void";
9574 break;
9576 case FUNCTION_TYPE:
9577 error_msg = "array of functions";
9578 break;
9580 case REFERENCE_TYPE:
9581 error_msg = "array of references";
9582 break;
9584 case OFFSET_TYPE:
9585 error_msg = "array of data members";
9586 break;
9588 case METHOD_TYPE:
9589 error_msg = "array of function members";
9590 break;
9592 default:
9593 break;
9596 /* If something went wrong, issue an error-message and return. */
9597 if (error_msg)
9599 if (name)
9600 error ("declaration of `%D' as %s", name, error_msg);
9601 else
9602 error ("creating %s", error_msg);
9604 return error_mark_node;
9607 /* [dcl.array]
9609 The constant expressions that specify the bounds of the arrays
9610 can be omitted only for the first member of the sequence. */
9611 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9613 if (name)
9614 error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9615 name);
9616 else
9617 error ("multidimensional array must have bounds for all dimensions except the first");
9619 return error_mark_node;
9622 /* Figure out the index type for the array. */
9623 if (size)
9624 itype = compute_array_index_type (name, size);
9626 return build_cplus_array_type (type, itype);
9629 /* Check that it's OK to declare a function with the indicated TYPE.
9630 SFK indicates the kind of special function (if any) that this
9631 function is. OPTYPE is the type given in a conversion operator
9632 declaration. Returns the actual return type of the function; that
9633 may be different than TYPE if an error occurs, or for certain
9634 special functions. */
9636 static tree
9637 check_special_function_return_type (special_function_kind sfk,
9638 tree type,
9639 tree optype)
9641 switch (sfk)
9643 case sfk_constructor:
9644 if (type)
9645 error ("return type specification for constructor invalid");
9647 type = void_type_node;
9648 break;
9650 case sfk_destructor:
9651 if (type)
9652 error ("return type specification for destructor invalid");
9653 type = void_type_node;
9654 break;
9656 case sfk_conversion:
9657 if (type && !same_type_p (type, optype))
9658 error ("operator `%T' declared to return `%T'", optype, type);
9659 else if (type)
9660 pedwarn ("return type specified for `operator %T'", optype);
9661 type = optype;
9662 break;
9664 default:
9665 abort ();
9666 break;
9669 return type;
9672 /* Given declspecs and a declarator (abstract or otherwise), determine
9673 the name and type of the object declared and construct a DECL node
9674 for it.
9676 DECLSPECS is a chain of tree_list nodes whose value fields
9677 are the storage classes and type specifiers.
9679 DECL_CONTEXT says which syntactic context this declaration is in:
9680 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9681 FUNCDEF for a function definition. Like NORMAL but a few different
9682 error messages in each case. Return value may be zero meaning
9683 this definition is too screwy to try to parse.
9684 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9685 handle member functions (which have FIELD context).
9686 Return value may be zero meaning this definition is too screwy to
9687 try to parse.
9688 PARM for a parameter declaration (either within a function prototype
9689 or before a function body). Make a PARM_DECL, or return void_type_node.
9690 CATCHPARM for a parameter declaration before a catch clause.
9691 TYPENAME if for a typename (in a cast or sizeof).
9692 Don't make a DECL node; just return the ..._TYPE node.
9693 FIELD for a struct or union field; make a FIELD_DECL.
9694 BITFIELD for a field with specified width.
9695 INITIALIZED is 1 if the decl has an initializer.
9697 ATTRLIST is a pointer to the list of attributes, which may be NULL
9698 if there are none; *ATTRLIST may be modified if attributes from inside
9699 the declarator should be applied to the declaration.
9701 When this function is called, scoping variables (such as
9702 CURRENT_CLASS_TYPE) should reflect the scope in which the
9703 declaration occurs, not the scope in which the new declaration will
9704 be placed. For example, on:
9706 void S::f() { ... }
9708 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9709 should not be `S'. */
9711 tree
9712 grokdeclarator (tree declarator,
9713 tree declspecs,
9714 enum decl_context decl_context,
9715 int initialized,
9716 tree* attrlist)
9718 RID_BIT_TYPE specbits;
9719 int nclasses = 0;
9720 tree spec;
9721 tree type = NULL_TREE;
9722 int longlong = 0;
9723 int type_quals;
9724 int virtualp, explicitp, friendp, inlinep, staticp;
9725 int explicit_int = 0;
9726 int explicit_char = 0;
9727 int defaulted_int = 0;
9728 int extern_langp = 0;
9729 tree dependant_name = NULL_TREE;
9731 tree typedef_decl = NULL_TREE;
9732 const char *name;
9733 tree typedef_type = NULL_TREE;
9734 int funcdef_flag = 0;
9735 enum tree_code innermost_code = ERROR_MARK;
9736 int bitfield = 0;
9737 #if 0
9738 /* See the code below that used this. */
9739 tree decl_attr = NULL_TREE;
9740 #endif
9742 /* Keep track of what sort of function is being processed
9743 so that we can warn about default return values, or explicit
9744 return values which do not match prescribed defaults. */
9745 special_function_kind sfk = sfk_none;
9747 tree dname = NULL_TREE;
9748 tree ctype = current_class_type;
9749 tree ctor_return_type = NULL_TREE;
9750 enum overload_flags flags = NO_SPECIAL;
9751 tree quals = NULL_TREE;
9752 tree raises = NULL_TREE;
9753 int template_count = 0;
9754 tree in_namespace = NULL_TREE;
9755 tree returned_attrs = NULL_TREE;
9756 tree scope = NULL_TREE;
9758 RIDBIT_RESET_ALL (specbits);
9759 if (decl_context == FUNCDEF)
9760 funcdef_flag = 1, decl_context = NORMAL;
9761 else if (decl_context == MEMFUNCDEF)
9762 funcdef_flag = -1, decl_context = FIELD;
9763 else if (decl_context == BITFIELD)
9764 bitfield = 1, decl_context = FIELD;
9766 /* Look inside a declarator for the name being declared
9767 and get it as a string, for an error message. */
9769 tree *next = &declarator;
9770 register tree decl;
9771 name = NULL;
9773 while (next && *next)
9775 decl = *next;
9776 switch (TREE_CODE (decl))
9778 case TREE_LIST:
9779 /* For attributes. */
9780 next = &TREE_VALUE (decl);
9781 break;
9783 case COND_EXPR:
9784 ctype = NULL_TREE;
9785 next = &TREE_OPERAND (decl, 0);
9786 break;
9788 case BIT_NOT_EXPR: /* For C++ destructors! */
9790 tree name = TREE_OPERAND (decl, 0);
9791 tree rename = NULL_TREE;
9793 my_friendly_assert (flags == NO_SPECIAL, 152);
9794 flags = DTOR_FLAG;
9795 sfk = sfk_destructor;
9796 if (TYPE_P (name))
9797 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9798 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9799 if (ctype == NULL_TREE)
9801 if (current_class_type == NULL_TREE)
9803 error ("destructors must be member functions");
9804 flags = NO_SPECIAL;
9806 else
9808 tree t = constructor_name (current_class_type);
9809 if (t != name)
9810 rename = t;
9813 else
9815 tree t = constructor_name (ctype);
9816 if (t != name)
9817 rename = t;
9820 if (rename)
9822 error ("destructor `%T' must match class name `%T'",
9823 name, rename);
9824 TREE_OPERAND (decl, 0) = rename;
9826 next = &name;
9828 break;
9830 case ADDR_EXPR: /* C++ reference declaration */
9831 /* Fall through. */
9832 case ARRAY_REF:
9833 case INDIRECT_REF:
9834 ctype = NULL_TREE;
9835 innermost_code = TREE_CODE (decl);
9836 next = &TREE_OPERAND (decl, 0);
9837 break;
9839 case CALL_EXPR:
9840 innermost_code = TREE_CODE (decl);
9841 if (decl_context == FIELD && ctype == NULL_TREE)
9842 ctype = current_class_type;
9843 if (ctype
9844 && TREE_OPERAND (decl, 0)
9845 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9846 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9847 ctype)))
9848 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9849 next = &TREE_OPERAND (decl, 0);
9850 decl = *next;
9851 if (ctype != NULL_TREE
9852 && decl != NULL_TREE && flags != DTOR_FLAG
9853 && constructor_name_p (decl, ctype))
9855 sfk = sfk_constructor;
9856 ctor_return_type = ctype;
9858 ctype = NULL_TREE;
9859 break;
9861 case TEMPLATE_ID_EXPR:
9863 tree fns = TREE_OPERAND (decl, 0);
9865 if (TREE_CODE (fns) == LOOKUP_EXPR)
9866 fns = TREE_OPERAND (fns, 0);
9868 dname = fns;
9869 if (TREE_CODE (dname) == COMPONENT_REF)
9870 dname = TREE_OPERAND (dname, 1);
9871 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9873 my_friendly_assert (is_overloaded_fn (dname),
9874 19990331);
9875 dname = DECL_NAME (get_first_fn (dname));
9878 /* Fall through. */
9880 case IDENTIFIER_NODE:
9881 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9882 dname = decl;
9884 next = 0;
9886 if (C_IS_RESERVED_WORD (dname))
9888 error ("declarator-id missing; using reserved word `%D'",
9889 dname);
9890 name = IDENTIFIER_POINTER (dname);
9892 else if (!IDENTIFIER_TYPENAME_P (dname))
9893 name = IDENTIFIER_POINTER (dname);
9894 else
9896 my_friendly_assert (flags == NO_SPECIAL, 154);
9897 flags = TYPENAME_FLAG;
9898 ctor_return_type = TREE_TYPE (dname);
9899 sfk = sfk_conversion;
9900 if (IDENTIFIER_GLOBAL_VALUE (dname)
9901 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9902 == TYPE_DECL))
9903 name = IDENTIFIER_POINTER (dname);
9904 else
9905 name = "<invalid operator>";
9907 break;
9909 /* C++ extension */
9910 case SCOPE_REF:
9912 /* Perform error checking, and decide on a ctype. */
9913 tree cname = TREE_OPERAND (decl, 0);
9914 if (cname == NULL_TREE)
9915 ctype = NULL_TREE;
9916 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9918 ctype = NULL_TREE;
9919 in_namespace = TREE_OPERAND (decl, 0);
9921 else if (! is_aggr_type (cname, 1))
9922 ctype = NULL_TREE;
9923 /* Must test TREE_OPERAND (decl, 1), in case user gives
9924 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9925 else if (TREE_OPERAND (decl, 1)
9926 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9927 ctype = cname;
9928 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9929 || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9931 /* This might be declaring a member of a template
9932 parm to be a friend. */
9933 ctype = cname;
9934 dependant_name = TREE_OPERAND (decl, 1);
9936 else if (ctype == NULL_TREE)
9937 ctype = cname;
9938 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9940 else
9942 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9944 error ("type `%T' is not derived from type `%T'",
9945 cname, ctype);
9946 ctype = NULL_TREE;
9948 else
9949 ctype = cname;
9952 /* It is valid to write:
9954 class C { void f(); };
9955 typedef C D;
9956 void D::f();
9958 The standard is not clear about whether `typedef const C D' is
9959 legal; as of 2002-09-15 the committee is considering
9960 that question. EDG 3.0 allows that syntax.
9961 Therefore, we do as well. */
9962 if (ctype)
9963 ctype = TYPE_MAIN_VARIANT (ctype);
9964 /* Update the declarator so that when we process it
9965 again the correct type is present. */
9966 TREE_OPERAND (decl, 0) = ctype;
9968 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9969 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9970 ctype))
9971 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9972 next = &TREE_OPERAND (decl, 1);
9973 decl = *next;
9974 if (ctype)
9976 if (TREE_CODE (decl) == IDENTIFIER_NODE
9977 && constructor_name_p (decl, ctype))
9979 sfk = sfk_constructor;
9980 ctor_return_type = ctype;
9982 else if (TREE_CODE (decl) == BIT_NOT_EXPR
9983 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9984 && constructor_name_p (TREE_OPERAND (decl, 0),
9985 ctype))
9987 sfk = sfk_destructor;
9988 ctor_return_type = ctype;
9989 flags = DTOR_FLAG;
9990 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9991 next = &TREE_OPERAND (decl, 0);
9995 break;
9997 case ERROR_MARK:
9998 next = 0;
9999 break;
10001 case TYPE_DECL:
10002 /* Parse error puts this typespec where
10003 a declarator should go. */
10004 error ("`%T' specified as declarator-id", DECL_NAME (decl));
10005 if (TREE_TYPE (decl) == current_class_type)
10006 error (" perhaps you want `%T' for a constructor",
10007 current_class_name);
10008 dname = DECL_NAME (decl);
10009 name = IDENTIFIER_POINTER (dname);
10011 /* Avoid giving two errors for this. */
10012 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
10014 declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
10015 *next = dname;
10016 next = 0;
10017 break;
10019 case BASELINK:
10020 next = &BASELINK_FUNCTIONS (decl);
10021 break;
10023 case TEMPLATE_DECL:
10024 /* Sometimes, we see a template-name used as part of a
10025 decl-specifier like in
10026 std::allocator alloc;
10027 Handle that gracefully. */
10028 error ("invalid use of template-name '%E' in a declarator", decl);
10029 return error_mark_node;
10030 break;
10032 default:
10033 my_friendly_assert (0, 20020917);
10038 /* A function definition's declarator must have the form of
10039 a function declarator. */
10041 if (funcdef_flag && innermost_code != CALL_EXPR)
10042 return 0;
10044 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
10045 && innermost_code != CALL_EXPR
10046 && ! (ctype && declspecs == NULL_TREE))
10048 error ("declaration of `%D' as non-function", dname);
10049 return void_type_node;
10052 /* Anything declared one level down from the top level
10053 must be one of the parameters of a function
10054 (because the body is at least two levels down). */
10056 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10057 by not allowing C++ class definitions to specify their parameters
10058 with xdecls (must be spec.d in the parmlist).
10060 Since we now wait to push a class scope until we are sure that
10061 we are in a legitimate method context, we must set oldcname
10062 explicitly (since current_class_name is not yet alive).
10064 We also want to avoid calling this a PARM if it is in a namespace. */
10066 if (decl_context == NORMAL && !toplevel_bindings_p ())
10068 struct cp_binding_level *b = current_binding_level;
10069 current_binding_level = b->level_chain;
10070 if (current_binding_level != 0 && toplevel_bindings_p ())
10071 decl_context = PARM;
10072 current_binding_level = b;
10075 if (name == NULL)
10076 name = decl_context == PARM ? "parameter" : "type name";
10078 /* Look through the decl specs and record which ones appear.
10079 Some typespecs are defined as built-in typenames.
10080 Others, the ones that are modifiers of other types,
10081 are represented by bits in SPECBITS: set the bits for
10082 the modifiers that appear. Storage class keywords are also in SPECBITS.
10084 If there is a typedef name or a type, store the type in TYPE.
10085 This includes builtin typedefs such as `int'.
10087 Set EXPLICIT_INT if the type is `int' or `char' and did not
10088 come from a user typedef.
10090 Set LONGLONG if `long' is mentioned twice.
10092 For C++, constructors and destructors have their own fast treatment. */
10094 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10096 register int i;
10097 register tree id;
10099 /* Certain parse errors slip through. For example,
10100 `int class;' is not caught by the parser. Try
10101 weakly to recover here. */
10102 if (TREE_CODE (spec) != TREE_LIST)
10103 return 0;
10105 id = TREE_VALUE (spec);
10107 /* If the entire declaration is itself tagged as deprecated then
10108 suppress reports of deprecated items. */
10109 if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10111 if (deprecated_state != DEPRECATED_SUPPRESS)
10112 warn_deprecated_use (id);
10115 if (TREE_CODE (id) == IDENTIFIER_NODE)
10117 if (id == ridpointers[(int) RID_INT]
10118 || id == ridpointers[(int) RID_CHAR]
10119 || id == ridpointers[(int) RID_BOOL]
10120 || id == ridpointers[(int) RID_WCHAR])
10122 if (type)
10124 if (id == ridpointers[(int) RID_BOOL])
10125 error ("`bool' is now a keyword");
10126 else
10127 error ("extraneous `%T' ignored", id);
10129 else
10131 if (id == ridpointers[(int) RID_INT])
10132 explicit_int = 1;
10133 else if (id == ridpointers[(int) RID_CHAR])
10134 explicit_char = 1;
10135 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10137 goto found;
10139 /* C++ aggregate types. */
10140 if (IDENTIFIER_HAS_TYPE_VALUE (id))
10142 if (type)
10143 error ("multiple declarations `%T' and `%T'", type, id);
10144 else
10145 type = IDENTIFIER_TYPE_VALUE (id);
10146 goto found;
10149 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10151 if (ridpointers[i] == id)
10153 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10155 if (pedantic && ! in_system_header && warn_long_long)
10156 pedwarn ("ISO C++ does not support `long long'");
10157 if (longlong)
10158 error ("`long long long' is too long for GCC");
10159 else
10160 longlong = 1;
10162 else if (RIDBIT_SETP (i, specbits))
10163 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10165 /* Diagnose "__thread extern" or "__thread static". */
10166 if (RIDBIT_SETP (RID_THREAD, specbits))
10168 if (i == (int)RID_EXTERN)
10169 error ("`__thread' before `extern'");
10170 else if (i == (int)RID_STATIC)
10171 error ("`__thread' before `static'");
10174 if (i == (int)RID_EXTERN
10175 && TREE_PURPOSE (spec) == error_mark_node)
10176 /* This extern was part of a language linkage. */
10177 extern_langp = 1;
10179 RIDBIT_SET (i, specbits);
10180 goto found;
10184 else if (TREE_CODE (id) == TYPE_DECL)
10186 if (type)
10187 error ("multiple declarations `%T' and `%T'", type,
10188 TREE_TYPE (id));
10189 else
10191 type = TREE_TYPE (id);
10192 TREE_VALUE (spec) = type;
10193 typedef_decl = id;
10195 goto found;
10197 if (type)
10198 error ("two or more data types in declaration of `%s'", name);
10199 else if (TREE_CODE (id) == IDENTIFIER_NODE)
10201 register tree t = lookup_name (id, 1);
10202 if (!t || TREE_CODE (t) != TYPE_DECL)
10203 error ("`%s' fails to be a typedef or built in type",
10204 IDENTIFIER_POINTER (id));
10205 else
10207 type = TREE_TYPE (t);
10208 typedef_decl = t;
10211 else if (id != error_mark_node)
10212 /* Can't change CLASS nodes into RECORD nodes here! */
10213 type = id;
10215 found: ;
10218 #if 0
10219 /* See the code below that used this. */
10220 if (typedef_decl)
10221 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10222 #endif
10223 typedef_type = type;
10225 /* No type at all: default to `int', and set DEFAULTED_INT
10226 because it was not a user-defined typedef. */
10228 if (type == NULL_TREE
10229 && (RIDBIT_SETP (RID_SIGNED, specbits)
10230 || RIDBIT_SETP (RID_UNSIGNED, specbits)
10231 || RIDBIT_SETP (RID_LONG, specbits)
10232 || RIDBIT_SETP (RID_SHORT, specbits)))
10234 /* These imply 'int'. */
10235 type = integer_type_node;
10236 defaulted_int = 1;
10239 if (sfk != sfk_none)
10240 type = check_special_function_return_type (sfk, type,
10241 ctor_return_type);
10242 else if (type == NULL_TREE)
10244 int is_main;
10246 explicit_int = -1;
10248 /* We handle `main' specially here, because 'main () { }' is so
10249 common. With no options, it is allowed. With -Wreturn-type,
10250 it is a warning. It is only an error with -pedantic-errors. */
10251 is_main = (funcdef_flag
10252 && dname && MAIN_NAME_P (dname)
10253 && ctype == NULL_TREE
10254 && in_namespace == NULL_TREE
10255 && current_namespace == global_namespace);
10257 if (in_system_header || flag_ms_extensions)
10258 /* Allow it, sigh. */;
10259 else if (pedantic || ! is_main)
10260 pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10261 name);
10262 else if (warn_return_type)
10263 warning ("ISO C++ forbids declaration of `%s' with no type",
10264 name);
10266 type = integer_type_node;
10269 ctype = NULL_TREE;
10271 /* Now process the modifiers that were specified
10272 and check for invalid combinations. */
10274 /* Long double is a special combination. */
10276 if (RIDBIT_SETP (RID_LONG, specbits)
10277 && TYPE_MAIN_VARIANT (type) == double_type_node)
10279 RIDBIT_RESET (RID_LONG, specbits);
10280 type = build_qualified_type (long_double_type_node,
10281 cp_type_quals (type));
10284 /* Check all other uses of type modifiers. */
10286 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10287 || RIDBIT_SETP (RID_SIGNED, specbits)
10288 || RIDBIT_SETP (RID_LONG, specbits)
10289 || RIDBIT_SETP (RID_SHORT, specbits))
10291 int ok = 0;
10293 if (TREE_CODE (type) == REAL_TYPE)
10294 error ("short, signed or unsigned invalid for `%s'", name);
10295 else if (TREE_CODE (type) != INTEGER_TYPE)
10296 error ("long, short, signed or unsigned invalid for `%s'", name);
10297 else if (RIDBIT_SETP (RID_LONG, specbits)
10298 && RIDBIT_SETP (RID_SHORT, specbits))
10299 error ("long and short specified together for `%s'", name);
10300 else if ((RIDBIT_SETP (RID_LONG, specbits)
10301 || RIDBIT_SETP (RID_SHORT, specbits))
10302 && explicit_char)
10303 error ("long or short specified with char for `%s'", name);
10304 else if ((RIDBIT_SETP (RID_LONG, specbits)
10305 || RIDBIT_SETP (RID_SHORT, specbits))
10306 && TREE_CODE (type) == REAL_TYPE)
10307 error ("long or short specified with floating type for `%s'", name);
10308 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10309 && RIDBIT_SETP (RID_UNSIGNED, specbits))
10310 error ("signed and unsigned given together for `%s'", name);
10311 else
10313 ok = 1;
10314 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10316 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10317 name);
10318 if (flag_pedantic_errors)
10319 ok = 0;
10323 /* Discard the type modifiers if they are invalid. */
10324 if (! ok)
10326 RIDBIT_RESET (RID_UNSIGNED, specbits);
10327 RIDBIT_RESET (RID_SIGNED, specbits);
10328 RIDBIT_RESET (RID_LONG, specbits);
10329 RIDBIT_RESET (RID_SHORT, specbits);
10330 longlong = 0;
10334 if (RIDBIT_SETP (RID_COMPLEX, specbits)
10335 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10337 error ("complex invalid for `%s'", name);
10338 RIDBIT_RESET (RID_COMPLEX, specbits);
10341 /* Decide whether an integer type is signed or not.
10342 Optionally treat bitfields as signed by default. */
10343 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10344 /* [class.bit]
10346 It is implementation-defined whether a plain (neither
10347 explicitly signed or unsigned) char, short, int, or long
10348 bit-field is signed or unsigned.
10350 Naturally, we extend this to long long as well. Note that
10351 this does not include wchar_t. */
10352 || (bitfield && !flag_signed_bitfields
10353 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10354 /* A typedef for plain `int' without `signed' can be
10355 controlled just like plain `int', but a typedef for
10356 `signed int' cannot be so controlled. */
10357 && !(typedef_decl
10358 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10359 && (TREE_CODE (type) == INTEGER_TYPE
10360 || TREE_CODE (type) == CHAR_TYPE)
10361 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10363 if (longlong)
10364 type = long_long_unsigned_type_node;
10365 else if (RIDBIT_SETP (RID_LONG, specbits))
10366 type = long_unsigned_type_node;
10367 else if (RIDBIT_SETP (RID_SHORT, specbits))
10368 type = short_unsigned_type_node;
10369 else if (type == char_type_node)
10370 type = unsigned_char_type_node;
10371 else if (typedef_decl)
10372 type = c_common_unsigned_type (type);
10373 else
10374 type = unsigned_type_node;
10376 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10377 && type == char_type_node)
10378 type = signed_char_type_node;
10379 else if (longlong)
10380 type = long_long_integer_type_node;
10381 else if (RIDBIT_SETP (RID_LONG, specbits))
10382 type = long_integer_type_node;
10383 else if (RIDBIT_SETP (RID_SHORT, specbits))
10384 type = short_integer_type_node;
10386 if (RIDBIT_SETP (RID_COMPLEX, specbits))
10388 /* If we just have "complex", it is equivalent to
10389 "complex double", but if any modifiers at all are specified it is
10390 the complex form of TYPE. E.g, "complex short" is
10391 "complex short int". */
10393 if (defaulted_int && ! longlong
10394 && ! (RIDBIT_SETP (RID_LONG, specbits)
10395 || RIDBIT_SETP (RID_SHORT, specbits)
10396 || RIDBIT_SETP (RID_SIGNED, specbits)
10397 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10398 type = complex_double_type_node;
10399 else if (type == integer_type_node)
10400 type = complex_integer_type_node;
10401 else if (type == float_type_node)
10402 type = complex_float_type_node;
10403 else if (type == double_type_node)
10404 type = complex_double_type_node;
10405 else if (type == long_double_type_node)
10406 type = complex_long_double_type_node;
10407 else
10408 type = build_complex_type (type);
10411 type_quals = TYPE_UNQUALIFIED;
10412 if (RIDBIT_SETP (RID_CONST, specbits))
10413 type_quals |= TYPE_QUAL_CONST;
10414 if (RIDBIT_SETP (RID_VOLATILE, specbits))
10415 type_quals |= TYPE_QUAL_VOLATILE;
10416 if (RIDBIT_SETP (RID_RESTRICT, specbits))
10417 type_quals |= TYPE_QUAL_RESTRICT;
10418 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10419 error ("qualifiers are not allowed on declaration of `operator %T'",
10420 ctor_return_type);
10422 type_quals |= cp_type_quals (type);
10423 type = cp_build_qualified_type_real
10424 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10425 ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10426 /* We might have ignored or rejected some of the qualifiers. */
10427 type_quals = cp_type_quals (type);
10429 staticp = 0;
10430 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10431 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10432 RIDBIT_RESET (RID_VIRTUAL, specbits);
10433 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10434 RIDBIT_RESET (RID_EXPLICIT, specbits);
10436 if (RIDBIT_SETP (RID_STATIC, specbits))
10437 staticp = 1 + (decl_context == FIELD);
10439 if (virtualp && staticp == 2)
10441 error ("member `%D' cannot be declared both virtual and static",
10442 dname);
10443 staticp = 0;
10445 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10446 RIDBIT_RESET (RID_FRIEND, specbits);
10448 if (dependant_name && !friendp)
10450 error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10451 return void_type_node;
10454 /* Warn if two storage classes are given. Default to `auto'. */
10456 if (RIDBIT_ANY_SET (specbits))
10458 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10459 if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10460 if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10461 if (decl_context == PARM && nclasses > 0)
10462 error ("storage class specifiers invalid in parameter declarations");
10463 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10465 if (decl_context == PARM)
10466 error ("typedef declaration invalid in parameter declaration");
10467 nclasses++;
10469 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10470 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10471 if (!nclasses && !friendp && extern_langp)
10472 nclasses++;
10475 /* Give error if `virtual' is used outside of class declaration. */
10476 if (virtualp
10477 && (current_class_name == NULL_TREE || decl_context != FIELD))
10479 error ("virtual outside class declaration");
10480 virtualp = 0;
10483 /* Static anonymous unions are dealt with here. */
10484 if (staticp && decl_context == TYPENAME
10485 && TREE_CODE (declspecs) == TREE_LIST
10486 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10487 decl_context = FIELD;
10489 /* Warn about storage classes that are invalid for certain
10490 kinds of declarations (parameters, typenames, etc.). */
10492 /* "static __thread" and "extern __thread" are allowed. */
10493 if (nclasses == 2
10494 && RIDBIT_SETP (RID_THREAD, specbits)
10495 && (RIDBIT_SETP (RID_EXTERN, specbits)
10496 || RIDBIT_SETP (RID_STATIC, specbits)))
10497 nclasses = 1;
10499 if (nclasses > 1)
10500 error ("multiple storage classes in declaration of `%s'", name);
10501 else if (decl_context != NORMAL && nclasses > 0)
10503 if ((decl_context == PARM || decl_context == CATCHPARM)
10504 && (RIDBIT_SETP (RID_REGISTER, specbits)
10505 || RIDBIT_SETP (RID_AUTO, specbits)))
10507 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10509 else if (decl_context == FIELD
10510 /* C++ allows static class elements */
10511 && RIDBIT_SETP (RID_STATIC, specbits))
10512 /* C++ also allows inlines and signed and unsigned elements,
10513 but in those cases we don't come in here. */
10515 else
10517 if (decl_context == FIELD)
10519 tree tmp = NULL_TREE;
10520 register int op = 0;
10522 if (declarator)
10524 /* Avoid trying to get an operand off an identifier node. */
10525 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10526 tmp = declarator;
10527 else
10528 tmp = TREE_OPERAND (declarator, 0);
10529 op = IDENTIFIER_OPNAME_P (tmp);
10530 if (IDENTIFIER_TYPENAME_P (tmp))
10532 if (IDENTIFIER_GLOBAL_VALUE (tmp)
10533 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10534 == TYPE_DECL))
10535 name = IDENTIFIER_POINTER (tmp);
10536 else
10537 name = "<invalid operator>";
10540 error ("storage class specified for %s `%s'",
10541 op ? "member operator" : "field",
10542 name);
10544 else
10546 if (decl_context == PARM || decl_context == CATCHPARM)
10547 error ("storage class specified for parameter `%s'", name);
10548 else
10549 error ("storage class specified for typename");
10551 RIDBIT_RESET (RID_REGISTER, specbits);
10552 RIDBIT_RESET (RID_AUTO, specbits);
10553 RIDBIT_RESET (RID_EXTERN, specbits);
10554 RIDBIT_RESET (RID_THREAD, specbits);
10557 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10559 if (toplevel_bindings_p ())
10561 /* It's common practice (and completely valid) to have a const
10562 be initialized and declared extern. */
10563 if (!(type_quals & TYPE_QUAL_CONST))
10564 warning ("`%s' initialized and declared `extern'", name);
10566 else
10567 error ("`%s' has both `extern' and initializer", name);
10569 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10570 && ! toplevel_bindings_p ())
10571 error ("nested function `%s' declared `extern'", name);
10572 else if (toplevel_bindings_p ())
10574 if (RIDBIT_SETP (RID_AUTO, specbits))
10575 error ("top-level declaration of `%s' specifies `auto'", name);
10577 else if (RIDBIT_SETP (RID_THREAD, specbits)
10578 && !RIDBIT_SETP (RID_EXTERN, specbits)
10579 && !RIDBIT_SETP (RID_STATIC, specbits))
10581 error ("function-scope `%s' implicitly auto and declared `__thread'",
10582 name);
10583 RIDBIT_RESET (RID_THREAD, specbits);
10586 if (nclasses > 0 && friendp)
10587 error ("storage class specifiers invalid in friend function declarations");
10589 scope = get_scope_of_declarator (declarator);
10591 /* Now figure out the structure of the declarator proper.
10592 Descend through it, creating more complex types, until we reach
10593 the declared identifier (or NULL_TREE, in an abstract declarator). */
10595 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10596 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10598 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10599 an INDIRECT_REF (for *...),
10600 a CALL_EXPR (for ...(...)),
10601 an identifier (for the name being declared)
10602 or a null pointer (for the place in an absolute declarator
10603 where the name was omitted).
10604 For the last two cases, we have just exited the loop.
10606 For C++ it could also be
10607 a SCOPE_REF (for class :: ...). In this case, we have converted
10608 sensible names to types, and those are the values we use to
10609 qualify the member name.
10610 an ADDR_EXPR (for &...),
10611 a BIT_NOT_EXPR (for destructors)
10613 At this point, TYPE is the type of elements of an array,
10614 or for a function to return, or for a pointer to point to.
10615 After this sequence of ifs, TYPE is the type of the
10616 array or function or pointer, and DECLARATOR has had its
10617 outermost layer removed. */
10619 if (type == error_mark_node)
10621 if (TREE_CODE (declarator) == SCOPE_REF)
10622 declarator = TREE_OPERAND (declarator, 1);
10623 else
10624 declarator = TREE_OPERAND (declarator, 0);
10625 continue;
10627 if (quals != NULL_TREE
10628 && (declarator == NULL_TREE
10629 || TREE_CODE (declarator) != SCOPE_REF))
10631 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10632 ctype = TYPE_METHOD_BASETYPE (type);
10633 if (ctype != NULL_TREE)
10635 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10636 grok_method_quals (ctype, dummy, quals);
10637 type = TREE_TYPE (dummy);
10638 quals = NULL_TREE;
10642 switch (TREE_CODE (declarator))
10644 case TREE_LIST:
10646 /* We encode a declarator with embedded attributes using
10647 a TREE_LIST. */
10648 tree attrs = TREE_PURPOSE (declarator);
10649 tree inner_decl;
10650 int attr_flags;
10652 declarator = TREE_VALUE (declarator);
10653 inner_decl = declarator;
10654 while (inner_decl != NULL_TREE
10655 && TREE_CODE (inner_decl) == TREE_LIST)
10656 inner_decl = TREE_VALUE (inner_decl);
10657 attr_flags = 0;
10658 if (inner_decl == NULL_TREE
10659 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10660 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10661 if (TREE_CODE (inner_decl) == CALL_EXPR)
10662 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10663 if (TREE_CODE (inner_decl) == ARRAY_REF)
10664 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10665 returned_attrs = decl_attributes (&type,
10666 chainon (returned_attrs, attrs),
10667 attr_flags);
10669 break;
10671 case ARRAY_REF:
10673 register tree size;
10675 size = TREE_OPERAND (declarator, 1);
10677 /* VC++ spells a zero-sized array with []. */
10678 if (size == NULL_TREE && decl_context == FIELD && ! staticp
10679 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
10680 size = integer_zero_node;
10682 declarator = TREE_OPERAND (declarator, 0);
10684 type = create_array_type_for_decl (dname, type, size);
10686 ctype = NULL_TREE;
10688 break;
10690 case CALL_EXPR:
10692 tree arg_types;
10693 int funcdecl_p;
10694 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10695 tree inner_decl = TREE_OPERAND (declarator, 0);
10697 /* Declaring a function type.
10698 Make sure we have a valid type for the function to return. */
10700 /* We now know that the TYPE_QUALS don't apply to the
10701 decl, but to its return type. */
10702 type_quals = TYPE_UNQUALIFIED;
10704 /* Warn about some types functions can't return. */
10706 if (TREE_CODE (type) == FUNCTION_TYPE)
10708 error ("`%s' declared as function returning a function", name);
10709 type = integer_type_node;
10711 if (TREE_CODE (type) == ARRAY_TYPE)
10713 error ("`%s' declared as function returning an array", name);
10714 type = integer_type_node;
10717 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10718 inner_decl = TREE_OPERAND (inner_decl, 1);
10720 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10721 inner_decl = dname;
10723 /* Pick up type qualifiers which should be applied to `this'. */
10724 quals = CALL_DECLARATOR_QUALS (declarator);
10726 /* Pick up the exception specifications. */
10727 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10729 /* Say it's a definition only for the CALL_EXPR
10730 closest to the identifier. */
10731 funcdecl_p
10732 = inner_decl
10733 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10734 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10735 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10737 if (ctype == NULL_TREE
10738 && decl_context == FIELD
10739 && funcdecl_p
10740 && (friendp == 0 || dname == current_class_name))
10741 ctype = current_class_type;
10743 if (ctype && sfk == sfk_conversion)
10744 TYPE_HAS_CONVERSION (ctype) = 1;
10745 if (ctype && constructor_name_p (dname, ctype))
10747 /* We are within a class's scope. If our declarator name
10748 is the same as the class name, and we are defining
10749 a function, then it is a constructor/destructor, and
10750 therefore returns a void type. */
10752 if (flags == DTOR_FLAG)
10754 /* ISO C++ 12.4/2. A destructor may not be
10755 declared const or volatile. A destructor may
10756 not be static. */
10757 if (staticp == 2)
10758 error ("destructor cannot be static member function");
10759 if (quals)
10761 error ("destructors may not be `%s'",
10762 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10763 quals = NULL_TREE;
10765 if (decl_context == FIELD)
10767 if (! member_function_or_else (ctype,
10768 current_class_type,
10769 flags))
10770 return void_type_node;
10773 else /* It's a constructor. */
10775 if (explicitp == 1)
10776 explicitp = 2;
10777 /* ISO C++ 12.1. A constructor may not be
10778 declared const or volatile. A constructor may
10779 not be virtual. A constructor may not be
10780 static. */
10781 if (staticp == 2)
10782 error ("constructor cannot be static member function");
10783 if (virtualp)
10785 pedwarn ("constructors cannot be declared virtual");
10786 virtualp = 0;
10788 if (quals)
10790 error ("constructors may not be `%s'",
10791 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10792 quals = NULL_TREE;
10795 RID_BIT_TYPE tmp_bits;
10796 memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10797 RIDBIT_RESET (RID_INLINE, tmp_bits);
10798 RIDBIT_RESET (RID_STATIC, tmp_bits);
10799 if (RIDBIT_ANY_SET (tmp_bits))
10800 error ("return value type specifier for constructor ignored");
10802 if (decl_context == FIELD)
10804 if (! member_function_or_else (ctype,
10805 current_class_type,
10806 flags))
10807 return void_type_node;
10808 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10809 if (sfk != sfk_constructor)
10810 return NULL_TREE;
10813 if (decl_context == FIELD)
10814 staticp = 0;
10816 else if (friendp)
10818 if (initialized)
10819 error ("can't initialize friend function `%s'", name);
10820 if (virtualp)
10822 /* Cannot be both friend and virtual. */
10823 error ("virtual functions cannot be friends");
10824 RIDBIT_RESET (RID_FRIEND, specbits);
10825 friendp = 0;
10827 if (decl_context == NORMAL)
10828 error ("friend declaration not in class definition");
10829 if (current_function_decl && funcdef_flag)
10830 error ("can't define friend function `%s' in a local class definition",
10831 name);
10834 /* Construct the function type and go to the next
10835 inner layer of declarator. */
10837 declarator = TREE_OPERAND (declarator, 0);
10839 /* FIXME: This is where default args should be fully
10840 processed. */
10842 arg_types = grokparms (inner_parms);
10844 if (declarator && flags == DTOR_FLAG)
10846 /* A destructor declared in the body of a class will
10847 be represented as a BIT_NOT_EXPR. But, we just
10848 want the underlying IDENTIFIER. */
10849 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10850 declarator = TREE_OPERAND (declarator, 0);
10852 if (arg_types != void_list_node)
10854 error ("destructors may not have parameters");
10855 arg_types = void_list_node;
10856 last_function_parms = NULL_TREE;
10860 /* ANSI says that `const int foo ();'
10861 does not make the function foo const. */
10862 type = build_function_type (type, arg_types);
10864 break;
10866 case ADDR_EXPR:
10867 case INDIRECT_REF:
10868 /* Filter out pointers-to-references and references-to-references.
10869 We can get these if a TYPE_DECL is used. */
10871 if (TREE_CODE (type) == REFERENCE_TYPE)
10873 error (TREE_CODE (declarator) == ADDR_EXPR
10874 ? "cannot declare reference to `%#T'"
10875 : "cannot declare pointer to `%#T'", type);
10876 type = TREE_TYPE (type);
10878 else if (VOID_TYPE_P (type)
10879 && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10880 error (ctype ? "cannot declare pointer to `%#T' member"
10881 : "cannot declare reference to `%#T'", type);
10883 /* Merge any constancy or volatility into the target type
10884 for the pointer. */
10886 /* We now know that the TYPE_QUALS don't apply to the decl,
10887 but to the target of the pointer. */
10888 type_quals = TYPE_UNQUALIFIED;
10890 if (TREE_CODE (declarator) == ADDR_EXPR)
10892 if (!VOID_TYPE_P (type))
10893 type = build_reference_type (type);
10895 else if (TREE_CODE (type) == METHOD_TYPE)
10896 type = build_ptrmemfunc_type (build_pointer_type (type));
10897 else if (ctype)
10898 type = build_ptrmem_type (ctype, type);
10899 else
10900 type = build_pointer_type (type);
10902 /* Process a list of type modifier keywords (such as
10903 const or volatile) that were given inside the `*' or `&'. */
10905 if (TREE_TYPE (declarator))
10907 register tree typemodlist;
10908 int erred = 0;
10909 int constp = 0;
10910 int volatilep = 0;
10911 int restrictp = 0;
10913 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10914 typemodlist = TREE_CHAIN (typemodlist))
10916 tree qualifier = TREE_VALUE (typemodlist);
10918 if (qualifier == ridpointers[(int) RID_CONST])
10920 constp++;
10921 type_quals |= TYPE_QUAL_CONST;
10923 else if (qualifier == ridpointers[(int) RID_VOLATILE])
10925 volatilep++;
10926 type_quals |= TYPE_QUAL_VOLATILE;
10928 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10930 restrictp++;
10931 type_quals |= TYPE_QUAL_RESTRICT;
10933 else if (!erred)
10935 erred = 1;
10936 error ("invalid type modifier within pointer declarator");
10939 if (constp > 1)
10940 pedwarn ("duplicate `const'");
10941 if (volatilep > 1)
10942 pedwarn ("duplicate `volatile'");
10943 if (restrictp > 1)
10944 pedwarn ("duplicate `restrict'");
10945 type = cp_build_qualified_type (type, type_quals);
10946 type_quals = cp_type_quals (type);
10948 declarator = TREE_OPERAND (declarator, 0);
10949 ctype = NULL_TREE;
10950 break;
10952 case SCOPE_REF:
10954 /* We have converted type names to NULL_TREE if the
10955 name was bogus, or to a _TYPE node, if not.
10957 The variable CTYPE holds the type we will ultimately
10958 resolve to. The code here just needs to build
10959 up appropriate member types. */
10960 tree sname = TREE_OPERAND (declarator, 1);
10961 tree t;
10963 /* Destructors can have their visibilities changed as well. */
10964 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10965 sname = TREE_OPERAND (sname, 0);
10967 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10969 /* We had a reference to a global decl, or
10970 perhaps we were given a non-aggregate typedef,
10971 in which case we cleared this out, and should just
10972 keep going as though it wasn't there. */
10973 declarator = sname;
10974 continue;
10976 ctype = TREE_OPERAND (declarator, 0);
10978 t = ctype;
10979 while (t != NULL_TREE && CLASS_TYPE_P (t))
10981 /* You're supposed to have one `template <...>'
10982 for every template class, but you don't need one
10983 for a full specialization. For example:
10985 template <class T> struct S{};
10986 template <> struct S<int> { void f(); };
10987 void S<int>::f () {}
10989 is correct; there shouldn't be a `template <>' for
10990 the definition of `S<int>::f'. */
10991 if (CLASSTYPE_TEMPLATE_INFO (t)
10992 && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
10993 || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10994 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
10995 template_count += 1;
10997 t = TYPE_MAIN_DECL (t);
10998 t = DECL_CONTEXT (t);
11001 if (sname == NULL_TREE)
11002 goto done_scoping;
11004 if (TREE_CODE (sname) == IDENTIFIER_NODE)
11006 /* This is the `standard' use of the scoping operator:
11007 basetype :: member . */
11009 if (ctype == current_class_type)
11011 /* class A {
11012 void A::f ();
11015 Is this ill-formed? */
11017 if (pedantic)
11018 pedwarn ("extra qualification `%T::' on member `%s' ignored",
11019 ctype, name);
11021 else if (TREE_CODE (type) == FUNCTION_TYPE)
11023 if (current_class_type == NULL_TREE || friendp)
11024 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11025 TYPE_ARG_TYPES (type));
11026 else
11028 error ("cannot declare member function `%T::%s' within `%T'",
11029 ctype, name, current_class_type);
11030 return error_mark_node;
11033 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
11034 || COMPLETE_TYPE_P (complete_type (ctype)))
11036 /* Have to move this code elsewhere in this function.
11037 this code is used for i.e., typedef int A::M; M *pm;
11039 It is? How? jason 10/2/94 */
11041 if (current_class_type)
11043 error ("cannot declare member `%T::%s' within `%T'",
11044 ctype, name, current_class_type);
11045 return void_type_node;
11048 else
11050 cxx_incomplete_type_error (NULL_TREE, ctype);
11051 return error_mark_node;
11054 declarator = sname;
11056 else if (TREE_CODE (sname) == SCOPE_REF)
11057 abort ();
11058 else
11060 done_scoping:
11061 declarator = TREE_OPERAND (declarator, 1);
11062 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
11063 /* In this case, we will deal with it later. */
11065 else if (TREE_CODE (type) == FUNCTION_TYPE)
11066 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11067 TYPE_ARG_TYPES (type));
11070 break;
11072 case BIT_NOT_EXPR:
11073 declarator = TREE_OPERAND (declarator, 0);
11074 break;
11076 case BASELINK:
11077 declarator = BASELINK_FUNCTIONS (declarator);
11078 break;
11080 case RECORD_TYPE:
11081 case UNION_TYPE:
11082 case ENUMERAL_TYPE:
11083 declarator = NULL_TREE;
11084 break;
11086 case ERROR_MARK:
11087 declarator = NULL_TREE;
11088 break;
11090 default:
11091 abort ();
11095 if (returned_attrs)
11097 if (attrlist)
11098 *attrlist = chainon (returned_attrs, *attrlist);
11099 else
11100 attrlist = &returned_attrs;
11103 /* Now TYPE has the actual type. */
11105 /* Did array size calculations overflow? */
11107 if (TREE_CODE (type) == ARRAY_TYPE
11108 && COMPLETE_TYPE_P (type)
11109 && TREE_OVERFLOW (TYPE_SIZE (type)))
11111 error ("size of array `%s' is too large", name);
11112 /* If we proceed with the array type as it is, we'll eventually
11113 crash in tree_low_cst(). */
11114 type = error_mark_node;
11117 if ((decl_context == FIELD || decl_context == PARM)
11118 && !processing_template_decl
11119 && variably_modified_type_p (type))
11121 if (decl_context == FIELD)
11122 error ("data member may not have variably modified type `%T'", type);
11123 else
11124 error ("parameter may not have variably modified type `%T'", type);
11125 type = error_mark_node;
11128 if (explicitp == 1 || (explicitp && friendp))
11130 /* [dcl.fct.spec] The explicit specifier shall only be used in
11131 declarations of constructors within a class definition. */
11132 error ("only declarations of constructors can be `explicit'");
11133 explicitp = 0;
11136 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11138 if (current_class_name == NULL_TREE || decl_context == PARM || friendp)
11140 error ("non-member `%s' cannot be declared `mutable'", name);
11141 RIDBIT_RESET (RID_MUTABLE, specbits);
11143 else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11145 error ("non-object member `%s' cannot be declared `mutable'", name);
11146 RIDBIT_RESET (RID_MUTABLE, specbits);
11148 else if (TREE_CODE (type) == FUNCTION_TYPE
11149 || TREE_CODE (type) == METHOD_TYPE)
11151 error ("function `%s' cannot be declared `mutable'", name);
11152 RIDBIT_RESET (RID_MUTABLE, specbits);
11154 else if (staticp)
11156 error ("static `%s' cannot be declared `mutable'", name);
11157 RIDBIT_RESET (RID_MUTABLE, specbits);
11159 else if (type_quals & TYPE_QUAL_CONST)
11161 error ("const `%s' cannot be declared `mutable'", name);
11162 RIDBIT_RESET (RID_MUTABLE, specbits);
11166 if (declarator == NULL_TREE
11167 || TREE_CODE (declarator) == IDENTIFIER_NODE
11168 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11169 && (TREE_CODE (type) == FUNCTION_TYPE
11170 || TREE_CODE (type) == METHOD_TYPE)))
11171 /* OK */;
11172 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11174 error ("template-id `%D' used as a declarator", declarator);
11175 declarator = dname;
11177 else
11178 /* Unexpected declarator format. */
11179 abort ();
11181 /* If this is declaring a typedef name, return a TYPE_DECL. */
11183 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11185 tree decl;
11187 /* Note that the grammar rejects storage classes
11188 in typenames, fields or parameters. */
11189 if (current_lang_name == lang_name_java)
11190 TYPE_FOR_JAVA (type) = 1;
11192 if (decl_context == FIELD)
11194 if (constructor_name_p (declarator, current_class_type))
11195 pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11196 declarator);
11197 decl = build_lang_decl (TYPE_DECL, declarator, type);
11199 else
11201 decl = build_decl (TYPE_DECL, declarator, type);
11202 if (in_namespace || ctype)
11203 cp_error_at ("typedef name may not be a nested-name-specifier",
11204 decl);
11205 if (!current_function_decl)
11206 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11209 /* If the user declares "typedef struct {...} foo" then the
11210 struct will have an anonymous name. Fill that name in now.
11211 Nothing can refer to it, so nothing needs know about the name
11212 change. */
11213 if (type != error_mark_node
11214 && declarator
11215 && TYPE_NAME (type)
11216 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11217 && TYPE_ANONYMOUS_P (type)
11218 /* Don't do this if there are attributes. */
11219 && (!attrlist || !*attrlist)
11220 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11222 tree oldname = TYPE_NAME (type);
11223 tree t;
11225 /* Replace the anonymous name with the real name everywhere. */
11226 lookup_tag_reverse (type, declarator);
11227 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11228 if (TYPE_NAME (t) == oldname)
11229 TYPE_NAME (t) = decl;
11231 if (TYPE_LANG_SPECIFIC (type))
11232 TYPE_WAS_ANONYMOUS (type) = 1;
11234 /* If this is a typedef within a template class, the nested
11235 type is a (non-primary) template. The name for the
11236 template needs updating as well. */
11237 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11238 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11239 = TYPE_IDENTIFIER (type);
11241 /* FIXME remangle member functions; member functions of a
11242 type with external linkage have external linkage. */
11245 if (quals)
11247 if (ctype == NULL_TREE)
11249 if (TREE_CODE (type) != METHOD_TYPE)
11250 cp_error_at ("invalid type qualifier for non-member function type", decl);
11251 else
11252 ctype = TYPE_METHOD_BASETYPE (type);
11254 if (ctype != NULL_TREE)
11255 grok_method_quals (ctype, decl, quals);
11258 if (RIDBIT_SETP (RID_SIGNED, specbits)
11259 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11260 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11262 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11263 inlinep, friendp, raises != NULL_TREE);
11265 return decl;
11268 /* Detect the case of an array type of unspecified size
11269 which came, as such, direct from a typedef name.
11270 We must copy the type, so that the array's domain can be
11271 individually set by the object's initializer. */
11273 if (type && typedef_type
11274 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11275 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11276 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11278 /* Detect where we're using a typedef of function type to declare a
11279 function. last_function_parms will not be set, so we must create
11280 it now. */
11282 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11284 tree decls = NULL_TREE;
11285 tree args;
11287 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11289 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11291 TREE_CHAIN (decl) = decls;
11292 decls = decl;
11295 last_function_parms = nreverse (decls);
11298 /* If this is a type name (such as, in a cast or sizeof),
11299 compute the type and return it now. */
11301 if (decl_context == TYPENAME)
11303 /* Note that the grammar rejects storage classes
11304 in typenames, fields or parameters. */
11305 if (type_quals != TYPE_UNQUALIFIED)
11306 type_quals = TYPE_UNQUALIFIED;
11308 /* Special case: "friend class foo" looks like a TYPENAME context. */
11309 if (friendp)
11311 if (type_quals != TYPE_UNQUALIFIED)
11313 error ("type qualifiers specified for friend class declaration");
11314 type_quals = TYPE_UNQUALIFIED;
11316 if (inlinep)
11318 error ("`inline' specified for friend class declaration");
11319 inlinep = 0;
11322 if (!current_aggr)
11324 /* Don't allow friend declaration without a class-key. */
11325 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11326 pedwarn ("template parameters cannot be friends");
11327 else if (TREE_CODE (type) == TYPENAME_TYPE)
11328 pedwarn ("friend declaration requires class-key, "
11329 "i.e. `friend class %T::%D'",
11330 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11331 else
11332 pedwarn ("friend declaration requires class-key, "
11333 "i.e. `friend %#T'",
11334 type);
11337 /* Only try to do this stuff if we didn't already give up. */
11338 if (type != integer_type_node)
11340 /* A friendly class? */
11341 if (current_class_type)
11342 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11343 else
11344 error ("trying to make class `%T' a friend of global scope",
11345 type);
11347 type = void_type_node;
11350 else if (quals)
11352 if (ctype == NULL_TREE)
11354 if (TREE_CODE (type) != METHOD_TYPE)
11355 error ("invalid qualifiers on non-member function type");
11356 else
11357 ctype = TYPE_METHOD_BASETYPE (type);
11359 if (ctype)
11361 tree dummy = build_decl (TYPE_DECL, declarator, type);
11362 grok_method_quals (ctype, dummy, quals);
11363 type = TREE_TYPE (dummy);
11367 return type;
11369 else if (declarator == NULL_TREE && decl_context != PARM
11370 && decl_context != CATCHPARM
11371 && TREE_CODE (type) != UNION_TYPE
11372 && ! bitfield)
11374 error ("abstract declarator `%T' used as declaration", type);
11375 declarator = make_anon_name ();
11378 /* `void' at top level (not within pointer)
11379 is allowed only in typedefs or type names.
11380 We don't complain about parms either, but that is because
11381 a better error message can be made later. */
11383 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11385 if (! declarator)
11386 error ("unnamed variable or field declared void");
11387 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11389 if (IDENTIFIER_OPNAME_P (declarator))
11390 abort ();
11391 else
11392 error ("variable or field `%s' declared void", name);
11394 else
11395 error ("variable or field declared void");
11396 type = integer_type_node;
11399 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11400 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11402 if (decl_context == PARM || decl_context == CATCHPARM)
11404 if (ctype || in_namespace)
11405 error ("cannot use `::' in parameter declaration");
11407 /* A parameter declared as an array of T is really a pointer to T.
11408 One declared as a function is really a pointer to a function.
11409 One declared as a member is really a pointer to member. */
11411 if (TREE_CODE (type) == ARRAY_TYPE)
11413 /* Transfer const-ness of array into that of type pointed to. */
11414 type = build_pointer_type (TREE_TYPE (type));
11415 type_quals = TYPE_UNQUALIFIED;
11417 else if (TREE_CODE (type) == FUNCTION_TYPE)
11418 type = build_pointer_type (type);
11419 else if (TREE_CODE (type) == OFFSET_TYPE)
11420 type = build_pointer_type (type);
11424 register tree decl;
11426 if (decl_context == PARM)
11428 decl = cp_build_parm_decl (declarator, type);
11430 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11431 inlinep, friendp, raises != NULL_TREE);
11433 else if (decl_context == FIELD)
11435 if (type == error_mark_node)
11437 /* Happens when declaring arrays of sizes which
11438 are error_mark_node, for example. */
11439 decl = NULL_TREE;
11441 else if (in_namespace && !friendp)
11443 /* Something like struct S { int N::j; }; */
11444 error ("invalid use of `::'");
11445 decl = NULL_TREE;
11447 else if (TREE_CODE (type) == FUNCTION_TYPE)
11449 int publicp = 0;
11450 tree function_context;
11452 /* We catch the others as conflicts with the builtin
11453 typedefs. */
11454 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11456 error ("function `%D' cannot be declared friend",
11457 declarator);
11458 friendp = 0;
11461 if (friendp == 0)
11463 if (ctype == NULL_TREE)
11464 ctype = current_class_type;
11466 if (ctype == NULL_TREE)
11468 error ("can't make `%D' into a method -- not in a class",
11469 declarator);
11470 return void_type_node;
11473 /* ``A union may [ ... ] not [ have ] virtual functions.''
11474 ARM 9.5 */
11475 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11477 error ("function `%D' declared virtual inside a union",
11478 declarator);
11479 return void_type_node;
11482 if (declarator == ansi_opname (NEW_EXPR)
11483 || declarator == ansi_opname (VEC_NEW_EXPR)
11484 || declarator == ansi_opname (DELETE_EXPR)
11485 || declarator == ansi_opname (VEC_DELETE_EXPR))
11487 if (virtualp)
11489 error ("`%D' cannot be declared virtual, since it is always static",
11490 declarator);
11491 virtualp = 0;
11494 else if (staticp < 2)
11495 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11496 TYPE_ARG_TYPES (type));
11499 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11500 function_context = (ctype != NULL_TREE) ?
11501 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11502 publicp = (! friendp || ! staticp)
11503 && function_context == NULL_TREE;
11504 decl = grokfndecl (ctype, type,
11505 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11506 ? declarator : dname,
11507 declarator,
11508 virtualp, flags, quals, raises,
11509 friendp ? -1 : 0, friendp, publicp, inlinep,
11510 funcdef_flag, template_count, in_namespace);
11511 if (decl == NULL_TREE)
11512 return decl;
11513 #if 0
11514 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11515 /* The decl and setting of decl_attr is also turned off. */
11516 decl = build_decl_attribute_variant (decl, decl_attr);
11517 #endif
11519 /* [class.conv.ctor]
11521 A constructor declared without the function-specifier
11522 explicit that can be called with a single parameter
11523 specifies a conversion from the type of its first
11524 parameter to the type of its class. Such a constructor
11525 is called a converting constructor. */
11526 if (explicitp == 2)
11527 DECL_NONCONVERTING_P (decl) = 1;
11528 else if (DECL_CONSTRUCTOR_P (decl))
11530 /* The constructor can be called with exactly one
11531 parameter if there is at least one parameter, and
11532 any subsequent parameters have default arguments.
11533 Ignore any compiler-added parms. */
11534 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11536 if (arg_types == void_list_node
11537 || (arg_types
11538 && TREE_CHAIN (arg_types)
11539 && TREE_CHAIN (arg_types) != void_list_node
11540 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11541 DECL_NONCONVERTING_P (decl) = 1;
11544 else if (TREE_CODE (type) == METHOD_TYPE)
11546 /* We only get here for friend declarations of
11547 members of other classes. */
11548 /* All method decls are public, so tell grokfndecl to set
11549 TREE_PUBLIC, also. */
11550 decl = grokfndecl (ctype, type, declarator, declarator,
11551 virtualp, flags, quals, raises,
11552 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11553 template_count, in_namespace);
11554 if (decl == NULL_TREE)
11555 return NULL_TREE;
11557 else if (!staticp && ! processing_template_decl
11558 && !COMPLETE_TYPE_P (complete_type (type))
11559 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11561 if (declarator)
11562 error ("field `%D' has incomplete type", declarator);
11563 else
11564 error ("name `%T' has incomplete type", type);
11566 /* If we're instantiating a template, tell them which
11567 instantiation made the field's type be incomplete. */
11568 if (current_class_type
11569 && TYPE_NAME (current_class_type)
11570 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11571 && declspecs && TREE_VALUE (declspecs)
11572 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11573 error (" in instantiation of template `%T'",
11574 current_class_type);
11576 type = error_mark_node;
11577 decl = NULL_TREE;
11579 else
11581 if (friendp)
11583 error ("`%s' is neither function nor member function; cannot be declared friend",
11584 IDENTIFIER_POINTER (declarator));
11585 friendp = 0;
11587 decl = NULL_TREE;
11590 if (friendp)
11592 /* Friends are treated specially. */
11593 if (ctype == current_class_type)
11594 warning ("member functions are implicitly friends of their class");
11595 else
11597 tree t = NULL_TREE;
11598 if (decl && DECL_NAME (decl))
11600 if (template_class_depth (current_class_type) == 0)
11602 decl
11603 = check_explicit_specialization
11604 (declarator, decl,
11605 template_count, 2 * (funcdef_flag != 0) + 4);
11606 if (decl == error_mark_node)
11607 return error_mark_node;
11610 t = do_friend (ctype, declarator, decl,
11611 last_function_parms, *attrlist,
11612 flags, quals, funcdef_flag);
11614 if (t && funcdef_flag)
11615 return t;
11617 return void_type_node;
11621 /* Structure field. It may not be a function, except for C++ */
11623 if (decl == NULL_TREE)
11625 if (initialized)
11627 if (!staticp)
11629 /* An attempt is being made to initialize a non-static
11630 member. But, from [class.mem]:
11632 4 A member-declarator can contain a
11633 constant-initializer only if it declares a static
11634 member (_class.static_) of integral or enumeration
11635 type, see _class.static.data_.
11637 This used to be relatively common practice, but
11638 the rest of the compiler does not correctly
11639 handle the initialization unless the member is
11640 static so we make it static below. */
11641 pedwarn ("ISO C++ forbids initialization of member `%D'",
11642 declarator);
11643 pedwarn ("making `%D' static", declarator);
11644 staticp = 1;
11647 if (uses_template_parms (type))
11648 /* We'll check at instantiation time. */
11650 else if (check_static_variable_definition (declarator,
11651 type))
11652 /* If we just return the declaration, crashes
11653 will sometimes occur. We therefore return
11654 void_type_node, as if this was a friend
11655 declaration, to cause callers to completely
11656 ignore this declaration. */
11657 return void_type_node;
11660 /* 9.2p13 [class.mem] */
11661 if (constructor_name_p (declarator, current_class_type)
11662 /* The standard does not allow non-static data members
11663 here either, but we agreed at the 10/99 meeting
11664 to change that in TC 1 so that they are allowed in
11665 classes with no user-defined constructors. */
11666 && staticp)
11667 pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11668 declarator);
11670 if (staticp)
11672 /* C++ allows static class members. All other work
11673 for this is done by grokfield. */
11674 decl = build_lang_decl (VAR_DECL, declarator, type);
11675 TREE_STATIC (decl) = 1;
11676 /* In class context, 'static' means public access. */
11677 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11679 else
11681 decl = build_decl (FIELD_DECL, declarator, type);
11682 DECL_NONADDRESSABLE_P (decl) = bitfield;
11683 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11685 DECL_MUTABLE_P (decl) = 1;
11686 RIDBIT_RESET (RID_MUTABLE, specbits);
11690 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11691 inlinep, friendp, raises != NULL_TREE);
11694 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11696 tree original_name;
11697 int publicp = 0;
11699 if (! declarator)
11700 return NULL_TREE;
11702 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11703 original_name = dname;
11704 else
11705 original_name = declarator;
11707 if (RIDBIT_SETP (RID_AUTO, specbits))
11708 error ("storage class `auto' invalid for function `%s'", name);
11709 else if (RIDBIT_SETP (RID_REGISTER, specbits))
11710 error ("storage class `register' invalid for function `%s'", name);
11711 else if (RIDBIT_SETP (RID_THREAD, specbits))
11712 error ("storage class `__thread' invalid for function `%s'", name);
11714 /* Function declaration not at top level.
11715 Storage classes other than `extern' are not allowed
11716 and `extern' makes no difference. */
11717 if (! toplevel_bindings_p ()
11718 && (RIDBIT_SETP (RID_STATIC, specbits)
11719 || RIDBIT_SETP (RID_INLINE, specbits))
11720 && pedantic)
11722 if (RIDBIT_SETP (RID_STATIC, specbits))
11723 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11724 else
11725 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11728 if (ctype == NULL_TREE)
11730 if (virtualp)
11732 error ("virtual non-class function `%s'", name);
11733 virtualp = 0;
11736 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11737 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11738 TYPE_ARG_TYPES (type));
11740 /* Record presence of `static'. */
11741 publicp = (ctype != NULL_TREE
11742 || RIDBIT_SETP (RID_EXTERN, specbits)
11743 || !RIDBIT_SETP (RID_STATIC, specbits));
11745 decl = grokfndecl (ctype, type, original_name, declarator,
11746 virtualp, flags, quals, raises,
11747 1, friendp,
11748 publicp, inlinep, funcdef_flag,
11749 template_count, in_namespace);
11750 if (decl == NULL_TREE)
11751 return NULL_TREE;
11753 if (staticp == 1)
11755 int invalid_static = 0;
11757 /* Don't allow a static member function in a class, and forbid
11758 declaring main to be static. */
11759 if (TREE_CODE (type) == METHOD_TYPE)
11761 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11762 invalid_static = 1;
11764 else if (current_function_decl)
11766 /* FIXME need arm citation */
11767 error ("cannot declare static function inside another function");
11768 invalid_static = 1;
11771 if (invalid_static)
11773 staticp = 0;
11774 RIDBIT_RESET (RID_STATIC, specbits);
11778 else
11780 /* It's a variable. */
11782 /* An uninitialized decl with `extern' is a reference. */
11783 decl = grokvardecl (type, declarator, &specbits,
11784 initialized,
11785 (type_quals & TYPE_QUAL_CONST) != 0,
11786 ctype ? ctype : in_namespace);
11787 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11788 inlinep, friendp, raises != NULL_TREE);
11790 if (ctype)
11792 DECL_CONTEXT (decl) = ctype;
11793 if (staticp == 1)
11795 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11796 staticp = 0;
11797 RIDBIT_RESET (RID_STATIC, specbits);
11799 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11801 error ("static member `%D' declared `register'", decl);
11802 RIDBIT_RESET (RID_REGISTER, specbits);
11804 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11806 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11807 decl);
11808 RIDBIT_RESET (RID_EXTERN, specbits);
11813 my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11815 /* Record `register' declaration for warnings on &
11816 and in case doing stupid register allocation. */
11818 if (RIDBIT_SETP (RID_REGISTER, specbits))
11819 DECL_REGISTER (decl) = 1;
11821 if (RIDBIT_SETP (RID_EXTERN, specbits))
11822 DECL_THIS_EXTERN (decl) = 1;
11824 if (RIDBIT_SETP (RID_STATIC, specbits))
11825 DECL_THIS_STATIC (decl) = 1;
11827 /* Record constancy and volatility. There's no need to do this
11828 when processing a template; we'll do this for the instantiated
11829 declaration based on the type of DECL. */
11830 if (!processing_template_decl)
11831 c_apply_type_quals_to_decl (type_quals, decl);
11833 return decl;
11837 /* Subroutine of start_function. Ensure that each of the parameter
11838 types (as listed in PARMS) is complete, as is required for a
11839 function definition. */
11841 static void
11842 require_complete_types_for_parms (tree parms)
11844 for (; parms; parms = TREE_CHAIN (parms))
11846 if (VOID_TYPE_P (TREE_TYPE (parms)))
11847 /* grokparms will have already issued an error */
11848 TREE_TYPE (parms) = error_mark_node;
11849 else if (complete_type_or_else (TREE_TYPE (parms), parms))
11851 layout_decl (parms, 0);
11852 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11854 else
11855 TREE_TYPE (parms) = error_mark_node;
11859 /* Returns nonzero if T is a local variable. */
11862 local_variable_p (tree t)
11864 if ((TREE_CODE (t) == VAR_DECL
11865 /* A VAR_DECL with a context that is a _TYPE is a static data
11866 member. */
11867 && !TYPE_P (CP_DECL_CONTEXT (t))
11868 /* Any other non-local variable must be at namespace scope. */
11869 && !DECL_NAMESPACE_SCOPE_P (t))
11870 || (TREE_CODE (t) == PARM_DECL))
11871 return 1;
11873 return 0;
11876 /* Returns nonzero if T is an automatic local variable or a label.
11877 (These are the declarations that need to be remapped when the code
11878 containing them is duplicated.) */
11881 nonstatic_local_decl_p (tree t)
11883 return ((local_variable_p (t) && !TREE_STATIC (t))
11884 || TREE_CODE (t) == LABEL_DECL
11885 || TREE_CODE (t) == RESULT_DECL);
11888 /* Like local_variable_p, but suitable for use as a tree-walking
11889 function. */
11891 static tree
11892 local_variable_p_walkfn (tree* tp,
11893 int* walk_subtrees ATTRIBUTE_UNUSED ,
11894 void* data ATTRIBUTE_UNUSED )
11896 return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11897 ? *tp : NULL_TREE);
11900 /* Check that ARG, which is a default-argument expression for a
11901 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11902 something goes wrong. DECL may also be a _TYPE node, rather than a
11903 DECL, if there is no DECL available. */
11905 tree
11906 check_default_argument (tree decl, tree arg)
11908 tree var;
11909 tree decl_type;
11911 if (TREE_CODE (arg) == DEFAULT_ARG)
11912 /* We get a DEFAULT_ARG when looking at an in-class declaration
11913 with a default argument. Ignore the argument for now; we'll
11914 deal with it after the class is complete. */
11915 return arg;
11917 if (processing_template_decl || uses_template_parms (arg))
11918 /* We don't do anything checking until instantiation-time. Note
11919 that there may be uninstantiated arguments even for an
11920 instantiated function, since default arguments are not
11921 instantiated until they are needed. */
11922 return arg;
11924 if (TYPE_P (decl))
11926 decl_type = decl;
11927 decl = NULL_TREE;
11929 else
11930 decl_type = TREE_TYPE (decl);
11932 if (arg == error_mark_node
11933 || decl == error_mark_node
11934 || TREE_TYPE (arg) == error_mark_node
11935 || decl_type == error_mark_node)
11936 /* Something already went wrong. There's no need to check
11937 further. */
11938 return error_mark_node;
11940 /* [dcl.fct.default]
11942 A default argument expression is implicitly converted to the
11943 parameter type. */
11944 if (!TREE_TYPE (arg)
11945 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11947 if (decl)
11948 error ("default argument for `%#D' has type `%T'",
11949 decl, TREE_TYPE (arg));
11950 else
11951 error ("default argument for parameter of type `%T' has type `%T'",
11952 decl_type, TREE_TYPE (arg));
11954 return error_mark_node;
11957 /* [dcl.fct.default]
11959 Local variables shall not be used in default argument
11960 expressions.
11962 The keyword `this' shall not be used in a default argument of a
11963 member function. */
11964 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11965 NULL);
11966 if (var)
11968 error ("default argument `%E' uses local variable `%D'",
11969 arg, var);
11970 return error_mark_node;
11973 /* All is well. */
11974 return arg;
11977 /* Decode the list of parameter types for a function type.
11978 Given the list of things declared inside the parens,
11979 return a list of types.
11981 We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11982 flag. If unset, we append void_list_node. A parmlist declared
11983 as `(void)' is accepted as the empty parmlist.
11985 Also set last_function_parms to the chain of PARM_DECLs. */
11987 static tree
11988 grokparms (tree first_parm)
11990 tree result = NULL_TREE;
11991 tree decls = NULL_TREE;
11992 int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11993 tree parm, chain;
11994 int any_error = 0;
11996 my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11998 for (parm = first_parm; parm != NULL_TREE; parm = chain)
12000 tree type = NULL_TREE;
12001 tree decl = TREE_VALUE (parm);
12002 tree init = TREE_PURPOSE (parm);
12003 tree specs, attrs;
12005 chain = TREE_CHAIN (parm);
12006 /* @@ weak defense against parse errors. */
12007 if (TREE_CODE (decl) != VOID_TYPE
12008 && TREE_CODE (decl) != TREE_LIST)
12010 /* Give various messages as the need arises. */
12011 if (TREE_CODE (decl) == STRING_CST)
12012 error ("invalid string constant `%E'", decl);
12013 else if (TREE_CODE (decl) == INTEGER_CST)
12014 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
12015 continue;
12018 if (parm == void_list_node)
12019 break;
12021 split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
12022 decl = grokdeclarator (TREE_VALUE (decl), specs,
12023 PARM, init != NULL_TREE, &attrs);
12024 if (! decl || TREE_TYPE (decl) == error_mark_node)
12025 continue;
12027 if (attrs)
12028 cplus_decl_attributes (&decl, attrs, 0);
12030 type = TREE_TYPE (decl);
12031 if (VOID_TYPE_P (type))
12033 if (same_type_p (type, void_type_node)
12034 && !DECL_NAME (decl) && !result && !chain && !ellipsis)
12035 /* this is a parmlist of `(void)', which is ok. */
12036 break;
12037 cxx_incomplete_type_error (decl, type);
12038 /* It's not a good idea to actually create parameters of
12039 type `void'; other parts of the compiler assume that a
12040 void type terminates the parameter list. */
12041 type = error_mark_node;
12042 TREE_TYPE (decl) = error_mark_node;
12045 if (type != error_mark_node)
12047 /* Top-level qualifiers on the parameters are
12048 ignored for function types. */
12049 type = TYPE_MAIN_VARIANT (type);
12050 if (TREE_CODE (type) == METHOD_TYPE)
12052 error ("parameter `%D' invalidly declared method type", decl);
12053 type = build_pointer_type (type);
12054 TREE_TYPE (decl) = type;
12056 else if (TREE_CODE (type) == OFFSET_TYPE)
12058 error ("parameter `%D' invalidly declared offset type", decl);
12059 type = build_pointer_type (type);
12060 TREE_TYPE (decl) = type;
12062 else if (abstract_virtuals_error (decl, type))
12063 any_error = 1; /* Seems like a good idea. */
12064 else if (POINTER_TYPE_P (type))
12066 /* [dcl.fct]/6, parameter types cannot contain pointers
12067 (references) to arrays of unknown bound. */
12068 tree t = TREE_TYPE (type);
12069 int ptr = TYPE_PTR_P (type);
12071 while (1)
12073 if (TYPE_PTR_P (t))
12074 ptr = 1;
12075 else if (TREE_CODE (t) != ARRAY_TYPE)
12076 break;
12077 else if (!TYPE_DOMAIN (t))
12078 break;
12079 t = TREE_TYPE (t);
12081 if (TREE_CODE (t) == ARRAY_TYPE)
12082 error ("parameter `%D' includes %s to array of unknown bound `%T'",
12083 decl, ptr ? "pointer" : "reference", t);
12086 if (!any_error && init)
12087 init = check_default_argument (decl, init);
12088 else
12089 init = NULL_TREE;
12092 TREE_CHAIN (decl) = decls;
12093 decls = decl;
12094 result = tree_cons (init, type, result);
12096 decls = nreverse (decls);
12097 result = nreverse (result);
12098 if (!ellipsis)
12099 result = chainon (result, void_list_node);
12100 last_function_parms = decls;
12102 return result;
12106 /* D is a constructor or overloaded `operator='.
12108 Let T be the class in which D is declared. Then, this function
12109 returns:
12111 -1 if D's is an ill-formed constructor or copy assignment operator
12112 whose first parameter is of type `T'.
12113 0 if D is not a copy constructor or copy assignment
12114 operator.
12115 1 if D is a copy constructor or copy assignment operator whose
12116 first parameter is a reference to const qualified T.
12117 2 if D is a copy constructor or copy assignment operator whose
12118 first parameter is a reference to non-const qualified T.
12120 This function can be used as a predicate. Positive values indicate
12121 a copy constructor and nonzero values indicate a copy assignment
12122 operator. */
12125 copy_fn_p (tree d)
12127 tree args;
12128 tree arg_type;
12129 int result = 1;
12131 my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12133 if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12134 /* Instantiations of template member functions are never copy
12135 functions. Note that member functions of templated classes are
12136 represented as template functions internally, and we must
12137 accept those as copy functions. */
12138 return 0;
12140 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12141 if (!args)
12142 return 0;
12144 arg_type = TREE_VALUE (args);
12146 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12148 /* Pass by value copy assignment operator. */
12149 result = -1;
12151 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12152 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12154 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12155 result = 2;
12157 else
12158 return 0;
12160 args = TREE_CHAIN (args);
12162 if (args && args != void_list_node && !TREE_PURPOSE (args))
12163 /* There are more non-optional args. */
12164 return 0;
12166 return result;
12169 /* Remember any special properties of member function DECL. */
12171 void grok_special_member_properties (tree decl)
12173 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12174 ; /* Not special. */
12175 else if (DECL_CONSTRUCTOR_P (decl))
12177 int ctor = copy_fn_p (decl);
12179 if (ctor > 0)
12181 /* [class.copy]
12183 A non-template constructor for class X is a copy
12184 constructor if its first parameter is of type X&, const
12185 X&, volatile X& or const volatile X&, and either there
12186 are no other parameters or else all other parameters have
12187 default arguments. */
12188 TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12189 if (ctor > 1)
12190 TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12192 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12193 TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12195 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12197 /* [class.copy]
12199 A non-template assignment operator for class X is a copy
12200 assignment operator if its parameter is of type X, X&, const
12201 X&, volatile X& or const volatile X&. */
12203 int assop = copy_fn_p (decl);
12205 if (assop)
12207 TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12208 if (assop != 1)
12209 TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12210 if (DECL_PURE_VIRTUAL_P (decl))
12211 TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12216 /* Check a constructor DECL has the correct form. Complains
12217 if the class has a constructor of the form X(X). */
12220 grok_ctor_properties (tree ctype, tree decl)
12222 int ctor_parm = copy_fn_p (decl);
12224 if (ctor_parm < 0)
12226 /* [class.copy]
12228 A declaration of a constructor for a class X is ill-formed if
12229 its first parameter is of type (optionally cv-qualified) X
12230 and either there are no other parameters or else all other
12231 parameters have default arguments.
12233 We *don't* complain about member template instantiations that
12234 have this form, though; they can occur as we try to decide
12235 what constructor to use during overload resolution. Since
12236 overload resolution will never prefer such a constructor to
12237 the non-template copy constructor (which is either explicitly
12238 or implicitly defined), there's no need to worry about their
12239 existence. Theoretically, they should never even be
12240 instantiated, but that's hard to forestall. */
12241 error ("invalid constructor; you probably meant `%T (const %T&)'",
12242 ctype, ctype);
12243 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12244 return 0;
12247 return 1;
12250 /* An operator with this code is unary, but can also be binary. */
12252 static int
12253 ambi_op_p (enum tree_code code)
12255 return (code == INDIRECT_REF
12256 || code == ADDR_EXPR
12257 || code == CONVERT_EXPR
12258 || code == NEGATE_EXPR
12259 || code == PREINCREMENT_EXPR
12260 || code == PREDECREMENT_EXPR);
12263 /* An operator with this name can only be unary. */
12265 static int
12266 unary_op_p (enum tree_code code)
12268 return (code == TRUTH_NOT_EXPR
12269 || code == BIT_NOT_EXPR
12270 || code == COMPONENT_REF
12271 || code == TYPE_EXPR);
12274 /* Do a little sanity-checking on how they declared their operator. */
12276 void
12277 grok_op_properties (tree decl, int friendp)
12279 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12280 tree argtype;
12281 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12282 tree name = DECL_NAME (decl);
12283 enum tree_code operator_code;
12284 int arity;
12286 /* Count the number of arguments. */
12287 for (argtype = argtypes, arity = 0;
12288 argtype && argtype != void_list_node;
12289 argtype = TREE_CHAIN (argtype))
12290 ++arity;
12292 if (current_class_type == NULL_TREE)
12293 friendp = 1;
12295 if (DECL_CONV_FN_P (decl))
12296 operator_code = TYPE_EXPR;
12297 else
12300 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12301 if (ansi_opname (CODE) == name) \
12303 operator_code = (CODE); \
12304 break; \
12306 else if (ansi_assopname (CODE) == name) \
12308 operator_code = (CODE); \
12309 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12310 break; \
12313 #include "operators.def"
12314 #undef DEF_OPERATOR
12316 abort ();
12318 while (0);
12319 my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12320 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12322 if (! friendp)
12324 switch (operator_code)
12326 case CALL_EXPR:
12327 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12328 break;
12330 case ARRAY_REF:
12331 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12332 break;
12334 case COMPONENT_REF:
12335 case MEMBER_REF:
12336 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12337 break;
12339 case NEW_EXPR:
12340 TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12341 break;
12343 case DELETE_EXPR:
12344 TYPE_GETS_DELETE (current_class_type) |= 1;
12345 break;
12347 case VEC_NEW_EXPR:
12348 TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12349 break;
12351 case VEC_DELETE_EXPR:
12352 TYPE_GETS_DELETE (current_class_type) |= 2;
12353 break;
12355 default:
12356 break;
12360 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12362 /* When the compiler encounters the definition of A::operator new, it
12363 doesn't look at the class declaration to find out if it's static. */
12364 if (methodp)
12365 revert_static_member_fn (decl);
12367 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12369 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12371 if (methodp)
12372 revert_static_member_fn (decl);
12374 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12376 else
12378 /* An operator function must either be a non-static member function
12379 or have at least one parameter of a class, a reference to a class,
12380 an enumeration, or a reference to an enumeration. 13.4.0.6 */
12381 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12383 if (operator_code == TYPE_EXPR
12384 || operator_code == CALL_EXPR
12385 || operator_code == COMPONENT_REF
12386 || operator_code == ARRAY_REF
12387 || operator_code == NOP_EXPR)
12388 error ("`%D' must be a nonstatic member function", decl);
12389 else
12391 tree p = argtypes;
12393 if (DECL_STATIC_FUNCTION_P (decl))
12394 error ("`%D' must be either a non-static member function or a non-member function", decl);
12396 if (p)
12397 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
12399 tree arg = TREE_VALUE (p);
12400 if (TREE_CODE (arg) == REFERENCE_TYPE)
12401 arg = TREE_TYPE (arg);
12403 /* This lets bad template code slip through. */
12404 if (IS_AGGR_TYPE (arg)
12405 || TREE_CODE (arg) == ENUMERAL_TYPE
12406 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12407 || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
12408 goto foundaggr;
12410 error
12411 ("`%D' must have an argument of class or enumerated type",
12412 decl);
12413 foundaggr:
12418 if (operator_code == CALL_EXPR)
12419 return; /* No restrictions on args. */
12421 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12423 tree t = TREE_TYPE (name);
12424 if (! friendp)
12426 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12427 const char *what = 0;
12429 if (ref)
12430 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12432 if (TREE_CODE (t) == VOID_TYPE)
12433 what = "void";
12434 else if (t == current_class_type)
12435 what = "the same type";
12436 /* Don't force t to be complete here. */
12437 else if (IS_AGGR_TYPE (t)
12438 && COMPLETE_TYPE_P (t)
12439 && DERIVED_FROM_P (t, current_class_type))
12440 what = "a base class";
12442 if (what)
12443 warning ("conversion to %s%s will never use a type conversion operator",
12444 ref ? "a reference to " : "", what);
12447 if (operator_code == COND_EXPR)
12449 /* 13.4.0.3 */
12450 error ("ISO C++ prohibits overloading operator ?:");
12452 else if (ambi_op_p (operator_code))
12454 if (arity == 1)
12455 /* We pick the one-argument operator codes by default, so
12456 we don't have to change anything. */
12458 else if (arity == 2)
12460 /* If we thought this was a unary operator, we now know
12461 it to be a binary operator. */
12462 switch (operator_code)
12464 case INDIRECT_REF:
12465 operator_code = MULT_EXPR;
12466 break;
12468 case ADDR_EXPR:
12469 operator_code = BIT_AND_EXPR;
12470 break;
12472 case CONVERT_EXPR:
12473 operator_code = PLUS_EXPR;
12474 break;
12476 case NEGATE_EXPR:
12477 operator_code = MINUS_EXPR;
12478 break;
12480 case PREINCREMENT_EXPR:
12481 operator_code = POSTINCREMENT_EXPR;
12482 break;
12484 case PREDECREMENT_EXPR:
12485 operator_code = POSTDECREMENT_EXPR;
12486 break;
12488 default:
12489 abort ();
12492 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12494 if ((operator_code == POSTINCREMENT_EXPR
12495 || operator_code == POSTDECREMENT_EXPR)
12496 && ! processing_template_decl
12497 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12499 if (methodp)
12500 error ("postfix `%D' must take `int' as its argument",
12501 decl);
12502 else
12503 error
12504 ("postfix `%D' must take `int' as its second argument",
12505 decl);
12508 else
12510 if (methodp)
12511 error ("`%D' must take either zero or one argument", decl);
12512 else
12513 error ("`%D' must take either one or two arguments", decl);
12516 /* More Effective C++ rule 6. */
12517 if (warn_ecpp
12518 && (operator_code == POSTINCREMENT_EXPR
12519 || operator_code == POSTDECREMENT_EXPR
12520 || operator_code == PREINCREMENT_EXPR
12521 || operator_code == PREDECREMENT_EXPR))
12523 tree arg = TREE_VALUE (argtypes);
12524 tree ret = TREE_TYPE (TREE_TYPE (decl));
12525 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12526 arg = TREE_TYPE (arg);
12527 arg = TYPE_MAIN_VARIANT (arg);
12528 if (operator_code == PREINCREMENT_EXPR
12529 || operator_code == PREDECREMENT_EXPR)
12531 if (TREE_CODE (ret) != REFERENCE_TYPE
12532 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12533 arg))
12534 warning ("prefix `%D' should return `%T'", decl,
12535 build_reference_type (arg));
12537 else
12539 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12540 warning ("postfix `%D' should return `%T'", decl, arg);
12544 else if (unary_op_p (operator_code))
12546 if (arity != 1)
12548 if (methodp)
12549 error ("`%D' must take `void'", decl);
12550 else
12551 error ("`%D' must take exactly one argument", decl);
12554 else /* if (binary_op_p (operator_code)) */
12556 if (arity != 2)
12558 if (methodp)
12559 error ("`%D' must take exactly one argument", decl);
12560 else
12561 error ("`%D' must take exactly two arguments", decl);
12564 /* More Effective C++ rule 7. */
12565 if (warn_ecpp
12566 && (operator_code == TRUTH_ANDIF_EXPR
12567 || operator_code == TRUTH_ORIF_EXPR
12568 || operator_code == COMPOUND_EXPR))
12569 warning ("user-defined `%D' always evaluates both arguments",
12570 decl);
12573 /* Effective C++ rule 23. */
12574 if (warn_ecpp
12575 && arity == 2
12576 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12577 && (operator_code == PLUS_EXPR
12578 || operator_code == MINUS_EXPR
12579 || operator_code == TRUNC_DIV_EXPR
12580 || operator_code == MULT_EXPR
12581 || operator_code == TRUNC_MOD_EXPR)
12582 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12583 warning ("`%D' should return by value", decl);
12585 /* [over.oper]/8 */
12586 for (; argtypes && argtypes != void_list_node;
12587 argtypes = TREE_CHAIN (argtypes))
12588 if (TREE_PURPOSE (argtypes))
12590 TREE_PURPOSE (argtypes) = NULL_TREE;
12591 if (operator_code == POSTINCREMENT_EXPR
12592 || operator_code == POSTDECREMENT_EXPR)
12594 if (pedantic)
12595 pedwarn ("`%D' cannot have default arguments", decl);
12597 else
12598 error ("`%D' cannot have default arguments", decl);
12604 static const char *
12605 tag_name (enum tag_types code)
12607 switch (code)
12609 case record_type:
12610 return "struct";
12611 case class_type:
12612 return "class";
12613 case union_type:
12614 return "union ";
12615 case enum_type:
12616 return "enum";
12617 default:
12618 abort ();
12622 /* Get the struct, enum or union (CODE says which) with tag NAME.
12623 Define the tag as a forward-reference if it is not defined.
12625 C++: If a class derivation is given, process it here, and report
12626 an error if multiple derivation declarations are not identical.
12628 If this is a definition, come in through xref_tag and only look in
12629 the current frame for the name (since C++ allows new names in any
12630 scope.) */
12632 tree
12633 xref_tag (enum tag_types tag_code, tree name, tree attributes,
12634 bool globalize)
12636 enum tree_code code;
12637 register tree ref, t;
12638 struct cp_binding_level *b = current_binding_level;
12639 tree context = NULL_TREE;
12641 timevar_push (TV_NAME_LOOKUP);
12642 switch (tag_code)
12644 case record_type:
12645 case class_type:
12646 code = RECORD_TYPE;
12647 break;
12648 case union_type:
12649 code = UNION_TYPE;
12650 break;
12651 case enum_type:
12652 code = ENUMERAL_TYPE;
12653 break;
12654 default:
12655 abort ();
12658 /* If a cross reference is requested, look up the type
12659 already defined for this tag and return it. */
12660 if (TYPE_P (name))
12662 t = name;
12663 name = TYPE_IDENTIFIER (t);
12665 else
12666 t = IDENTIFIER_TYPE_VALUE (name);
12668 /* Warn about 'friend struct Inherited;' doing the wrong thing. */
12669 if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
12671 static int explained;
12672 tree shadowed;
12674 warning ("`%s %T' declares a new type at namespace scope",
12675 tag_name (tag_code), name);
12676 if (!explained++)
12677 warning (" names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
12678 tag_name (tag_code),
12679 constructor_name (current_class_type),
12680 TYPE_IDENTIFIER (t));
12682 /* We need to remove the class scope binding for the
12683 TYPENAME_TYPE as otherwise poplevel_class gets confused. */
12684 for (shadowed = b->class_shadowed;
12685 shadowed;
12686 shadowed = TREE_CHAIN (shadowed))
12687 if (TREE_TYPE (shadowed) == TYPE_NAME (t))
12689 TREE_PURPOSE (shadowed) = NULL_TREE;
12690 break;
12694 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12695 && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
12696 t = NULL_TREE;
12698 if (! globalize)
12700 /* If we know we are defining this tag, only look it up in
12701 this scope and don't try to find it as a type. */
12702 ref = lookup_tag (code, name, b, 1);
12704 else
12706 if (t)
12708 ref = follow_tag_typedef (t);
12710 /* [dcl.type.elab] If the identifier resolves to a
12711 typedef-name or a template type-parameter, the
12712 elaborated-type-specifier is ill-formed. */
12713 if (!ref)
12715 pedwarn ("using typedef-name `%D' after `%s'",
12716 TYPE_NAME (t), tag_name (tag_code));
12717 ref = t;
12719 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12720 error ("using template type parameter `%T' after `%s'",
12721 t, tag_name (tag_code));
12723 else
12724 ref = lookup_tag (code, name, b, 0);
12726 if (! ref)
12728 /* Try finding it as a type declaration. If that wins,
12729 use it. */
12730 ref = lookup_name (name, 1);
12732 if (ref != NULL_TREE
12733 && processing_template_decl
12734 && DECL_CLASS_TEMPLATE_P (ref)
12735 && template_class_depth (current_class_type) == 0)
12736 /* Since GLOBALIZE is true, we're declaring a global
12737 template, so we want this type. */
12738 ref = DECL_TEMPLATE_RESULT (ref);
12740 if (ref && TREE_CODE (ref) == TYPE_DECL
12741 && TREE_CODE (TREE_TYPE (ref)) == code)
12742 ref = TREE_TYPE (ref);
12743 else
12744 ref = NULL_TREE;
12747 if (ref && current_class_type
12748 && template_class_depth (current_class_type)
12749 && PROCESSING_REAL_TEMPLATE_DECL_P ())
12751 /* Since GLOBALIZE is nonzero, we are not looking at a
12752 definition of this tag. Since, in addition, we are currently
12753 processing a (member) template declaration of a template
12754 class, we must be very careful; consider:
12756 template <class X>
12757 struct S1
12759 template <class U>
12760 struct S2
12761 { template <class V>
12762 friend struct S1; };
12764 Here, the S2::S1 declaration should not be confused with the
12765 outer declaration. In particular, the inner version should
12766 have a template parameter of level 2, not level 1. This
12767 would be particularly important if the member declaration
12768 were instead:
12770 template <class V = U> friend struct S1;
12772 say, when we should tsubst into `U' when instantiating
12773 S2. On the other hand, when presented with:
12775 template <class T>
12776 struct S1 {
12777 template <class U>
12778 struct S2 {};
12779 template <class U>
12780 friend struct S2;
12783 we must find the inner binding eventually. We
12784 accomplish this by making sure that the new type we
12785 create to represent this declaration has the right
12786 TYPE_CONTEXT. */
12787 context = TYPE_CONTEXT (ref);
12788 ref = NULL_TREE;
12792 if (! ref)
12794 /* If no such tag is yet defined, create a forward-reference node
12795 and record it as the "definition".
12796 When a real declaration of this type is found,
12797 the forward-reference will be altered into a real type. */
12798 if (code == ENUMERAL_TYPE)
12800 error ("use of enum `%#D' without previous declaration", name);
12802 ref = make_node (ENUMERAL_TYPE);
12804 /* Give the type a default layout like unsigned int
12805 to avoid crashing if it does not get defined. */
12806 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12807 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12808 TYPE_USER_ALIGN (ref) = 0;
12809 TREE_UNSIGNED (ref) = 1;
12810 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12811 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12812 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12814 /* Enable us to recognize when a type is created in class context.
12815 To do nested classes correctly, this should probably be cleared
12816 out when we leave this classes scope. Currently this in only
12817 done in `start_enum'. */
12819 pushtag (name, ref, globalize);
12821 else
12823 struct cp_binding_level *old_b = class_binding_level;
12825 ref = make_aggr_type (code);
12826 TYPE_CONTEXT (ref) = context;
12828 #ifdef NONNESTED_CLASSES
12829 /* Class types don't nest the way enums do. */
12830 class_binding_level = (struct cp_binding_level *)0;
12831 #endif
12832 pushtag (name, ref, globalize);
12833 class_binding_level = old_b;
12836 else
12838 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12839 redeclare_class_template (ref, current_template_parms);
12842 TYPE_ATTRIBUTES (ref) = attributes;
12844 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ref);
12847 tree
12848 xref_tag_from_type (tree old, tree id, int globalize)
12850 enum tag_types tag_kind;
12852 if (TREE_CODE (old) == RECORD_TYPE)
12853 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12854 else
12855 tag_kind = union_type;
12857 if (id == NULL_TREE)
12858 id = TYPE_IDENTIFIER (old);
12860 return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
12863 /* REF is a type (named NAME), for which we have just seen some
12864 baseclasses. BASE_LIST is a list of those baseclasses; the
12865 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12866 the base-class. TREE_VIA_VIRTUAL indicates virtual
12867 inheritance. CODE_TYPE_NODE indicates whether REF is a class,
12868 struct, or union. */
12870 void
12871 xref_basetypes (tree ref, tree base_list)
12873 /* In the declaration `A : X, Y, ... Z' we mark all the types
12874 (A, X, Y, ..., Z) so we can check for duplicates. */
12875 tree *basep;
12877 int i;
12878 enum tag_types tag_code;
12880 if (TREE_CODE (ref) == UNION_TYPE)
12882 error ("derived union `%T' invalid", ref);
12883 return;
12886 tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12888 /* First, make sure that any templates in base-classes are
12889 instantiated. This ensures that if we call ourselves recursively
12890 we do not get confused about which classes are marked and which
12891 are not. */
12892 basep = &base_list;
12893 while (*basep)
12895 tree basetype = TREE_VALUE (*basep);
12896 if (!(processing_template_decl && uses_template_parms (basetype))
12897 && !complete_type_or_else (basetype, NULL))
12898 /* An incomplete type. Remove it from the list. */
12899 *basep = TREE_CHAIN (*basep);
12900 else
12901 basep = &TREE_CHAIN (*basep);
12904 SET_CLASSTYPE_MARKED (ref);
12905 i = list_length (base_list);
12906 if (i)
12908 tree binfo = TYPE_BINFO (ref);
12909 tree binfos = make_tree_vec (i);
12910 tree accesses = make_tree_vec (i);
12912 BINFO_BASETYPES (binfo) = binfos;
12913 BINFO_BASEACCESSES (binfo) = accesses;
12915 for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
12917 tree access = TREE_PURPOSE (base_list);
12918 int via_virtual = TREE_VIA_VIRTUAL (base_list);
12919 tree basetype = TREE_VALUE (base_list);
12920 tree base_binfo;
12922 if (access == access_default_node)
12923 /* The base of a derived struct is public by default. */
12924 access = (tag_code == class_type
12925 ? access_private_node : access_public_node);
12927 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12928 basetype = TREE_TYPE (basetype);
12929 if (!basetype
12930 || (TREE_CODE (basetype) != RECORD_TYPE
12931 && TREE_CODE (basetype) != TYPENAME_TYPE
12932 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12933 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12935 error ("base type `%T' fails to be a struct or class type",
12936 basetype);
12937 continue;
12940 if (CLASSTYPE_MARKED (basetype))
12942 if (basetype == ref)
12943 error ("recursive type `%T' undefined", basetype);
12944 else
12945 error ("duplicate base type `%T' invalid", basetype);
12946 continue;
12949 if (TYPE_FOR_JAVA (basetype)
12950 && (current_lang_depth () == 0))
12951 TYPE_FOR_JAVA (ref) = 1;
12953 if (CLASS_TYPE_P (basetype))
12955 base_binfo = TYPE_BINFO (basetype);
12956 /* This flag will be in the binfo of the base type, we must
12957 clear it after copying the base binfos. */
12958 BINFO_DEPENDENT_BASE_P (base_binfo)
12959 = dependent_type_p (basetype);
12961 else
12962 base_binfo = make_binfo (size_zero_node, basetype,
12963 NULL_TREE, NULL_TREE);
12965 TREE_VEC_ELT (binfos, i) = base_binfo;
12966 TREE_VEC_ELT (accesses, i) = access;
12967 /* This flag will be in the binfo of the base type, we must
12968 clear it after copying the base binfos. */
12969 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12971 SET_CLASSTYPE_MARKED (basetype);
12973 /* We are free to modify these bits because they are meaningless
12974 at top level, and BASETYPE is a top-level type. */
12975 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12977 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12978 /* Converting to a virtual base class requires looking
12979 up the offset of the virtual base. */
12980 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12983 if (CLASS_TYPE_P (basetype))
12985 TYPE_HAS_NEW_OPERATOR (ref)
12986 |= TYPE_HAS_NEW_OPERATOR (basetype);
12987 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12988 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12989 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12990 /* If the base-class uses multiple inheritance, so do we. */
12991 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12992 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12993 /* Likewise, if converting to a base of the base may require
12994 code, then we may need to generate code to convert to a
12995 base as well. */
12996 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12997 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12999 i++;
13001 if (i)
13002 TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
13003 else
13004 BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
13006 if (i > 1)
13008 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
13009 /* If there is more than one non-empty they cannot be at the same
13010 address. */
13011 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
13015 /* Copy the base binfos, collect the virtual bases and set the
13016 inheritance order chain. */
13017 copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
13018 CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
13020 /* Unmark all the types. */
13021 while (i--)
13023 tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
13025 CLEAR_CLASSTYPE_MARKED (basetype);
13026 if (CLASS_TYPE_P (basetype))
13028 TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
13029 BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
13032 CLEAR_CLASSTYPE_MARKED (ref);
13036 /* Begin compiling the definition of an enumeration type.
13037 NAME is its name (or null if anonymous).
13038 Returns the type object, as yet incomplete.
13039 Also records info about it so that build_enumerator
13040 may be used to declare the individual values as they are read. */
13042 tree
13043 start_enum (tree name)
13045 register tree enumtype = NULL_TREE;
13046 struct cp_binding_level *b = current_binding_level;
13048 /* If this is the real definition for a previous forward reference,
13049 fill in the contents in the same object that used to be the
13050 forward reference. */
13052 if (name != NULL_TREE)
13053 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
13055 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13057 error ("multiple definition of `%#T'", enumtype);
13058 cp_error_at ("previous definition here", enumtype);
13059 /* Clear out TYPE_VALUES, and start again. */
13060 TYPE_VALUES (enumtype) = NULL_TREE;
13062 else
13064 enumtype = make_node (ENUMERAL_TYPE);
13065 pushtag (name, enumtype, 0);
13068 return enumtype;
13071 /* After processing and defining all the values of an enumeration type,
13072 install their decls in the enumeration type and finish it off.
13073 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
13075 void
13076 finish_enum (tree enumtype)
13078 tree pair;
13079 tree minnode;
13080 tree maxnode;
13081 tree t;
13082 bool unsignedp;
13083 int lowprec;
13084 int highprec;
13085 int precision;
13087 /* We built up the VALUES in reverse order. */
13088 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13090 /* For an enum defined in a template, just set the type of the values;
13091 all further processing is postponed until the template is
13092 instantiated. We need to set the type so that tsubst of a CONST_DECL
13093 works. */
13094 if (processing_template_decl)
13096 for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13097 TREE_TYPE (TREE_VALUE (pair)) = enumtype;
13098 if (at_function_scope_p ())
13099 add_stmt (build_min (TAG_DEFN, enumtype));
13100 return;
13103 if (TYPE_VALUES (enumtype))
13105 minnode = maxnode = NULL_TREE;
13107 for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13109 tree decl = TREE_VALUE (pair);
13110 tree value = DECL_INITIAL (decl);
13112 /* [dcl.enum]: Following the closing brace of an enum-specifier,
13113 each enumerator has the type of its enumeration. Prior to the
13114 closing brace, the type of each enumerator is the type of its
13115 initializing value. */
13116 TREE_TYPE (decl) = enumtype;
13118 /* Figure out what the minimum and maximum values of the
13119 enumerators are. */
13120 if (!minnode)
13121 minnode = maxnode = value;
13122 else if (tree_int_cst_lt (maxnode, value))
13123 maxnode = value;
13124 else if (tree_int_cst_lt (value, minnode))
13125 minnode = value;
13127 /* Set the TREE_TYPE for the values as well. That's so that when
13128 we call decl_constant_value we get an entity of the right type
13129 (but with the constant value). But first make a copy so we
13130 don't clobber shared INTEGER_CSTs. */
13131 if (TREE_TYPE (value) != enumtype)
13133 value = DECL_INITIAL (decl) = copy_node (value);
13134 TREE_TYPE (value) = enumtype;
13137 /* In addition, transform the TYPE_VALUES list to contain the
13138 values, rather than the CONST_DECLs for them. */
13139 TREE_VALUE (pair) = value;
13142 else
13143 minnode = maxnode = integer_zero_node;
13145 /* Compute the number of bits require to represent all values of the
13146 enumeration. We must do this before the type of MINNODE and
13147 MAXNODE are transformed, since min_precision relies on the
13148 TREE_TYPE of the value it is passed. */
13149 unsignedp = tree_int_cst_sgn (minnode) >= 0;
13150 lowprec = min_precision (minnode, unsignedp);
13151 highprec = min_precision (maxnode, unsignedp);
13152 precision = MAX (lowprec, highprec);
13154 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
13155 TYPE_SIZE (enumtype) = NULL_TREE;
13156 TYPE_PRECISION (enumtype) = precision;
13157 if (unsignedp)
13158 fixup_unsigned_type (enumtype);
13159 else
13160 fixup_signed_type (enumtype);
13162 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13163 /* Use the width of the narrowest normal C type which is wide
13164 enough. */
13165 TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size
13166 (precision, 1));
13167 else
13168 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
13170 TYPE_SIZE (enumtype) = NULL_TREE;
13171 layout_type (enumtype);
13173 /* Fix up all variant types of this enum type. */
13174 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13176 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13177 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13178 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13179 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13180 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13181 TYPE_MODE (t) = TYPE_MODE (enumtype);
13182 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13183 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13184 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13185 TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13188 /* Finish debugging output for this type. */
13189 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13192 /* Build and install a CONST_DECL for an enumeration constant of the
13193 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13194 Assignment of sequential values by default is handled here. */
13196 void
13197 build_enumerator (tree name, tree value, tree enumtype)
13199 tree decl;
13200 tree context;
13201 tree type;
13203 /* Remove no-op casts from the value. */
13204 if (value)
13205 STRIP_TYPE_NOPS (value);
13207 if (! processing_template_decl)
13209 /* Validate and default VALUE. */
13210 if (value != NULL_TREE)
13212 value = decl_constant_value (value);
13214 if (TREE_CODE (value) == INTEGER_CST)
13216 value = default_conversion (value);
13217 constant_expression_warning (value);
13219 else
13221 error ("enumerator value for `%D' not integer constant", name);
13222 value = NULL_TREE;
13226 /* Default based on previous value. */
13227 if (value == NULL_TREE)
13229 tree prev_value;
13231 if (TYPE_VALUES (enumtype))
13233 /* The next value is the previous value ... */
13234 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13235 /* ... plus one. */
13236 value = cp_build_binary_op (PLUS_EXPR,
13237 prev_value,
13238 integer_one_node);
13240 if (tree_int_cst_lt (value, prev_value))
13241 error ("overflow in enumeration values at `%D'", name);
13243 else
13244 value = integer_zero_node;
13247 /* Remove no-op casts from the value. */
13248 STRIP_TYPE_NOPS (value);
13251 /* C++ associates enums with global, function, or class declarations. */
13252 context = current_scope ();
13254 /* Build the actual enumeration constant. Note that the enumeration
13255 constants have the type of their initializers until the
13256 enumeration is complete:
13258 [ dcl.enum ]
13260 Following the closing brace of an enum-specifier, each enumer-
13261 ator has the type of its enumeration. Prior to the closing
13262 brace, the type of each enumerator is the type of its
13263 initializing value.
13265 In finish_enum we will reset the type. Of course, if we're
13266 processing a template, there may be no value. */
13267 type = value ? TREE_TYPE (value) : NULL_TREE;
13269 if (context && context == current_class_type)
13270 /* This enum declaration is local to the class. We need the full
13271 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
13272 decl = build_lang_decl (CONST_DECL, name, type);
13273 else
13274 /* It's a global enum, or it's local to a function. (Note local to
13275 a function could mean local to a class method. */
13276 decl = build_decl (CONST_DECL, name, type);
13278 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13279 DECL_INITIAL (decl) = value;
13280 TREE_READONLY (decl) = 1;
13282 if (context && context == current_class_type)
13283 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13284 on the TYPE_FIELDS list for `S'. (That's so that you can say
13285 things like `S::i' later.) */
13286 finish_member_declaration (decl);
13287 else
13288 pushdecl (decl);
13290 /* Add this enumeration constant to the list for this type. */
13291 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13295 /* We're defining DECL. Make sure that it's type is OK. */
13297 static void
13298 check_function_type (tree decl, tree current_function_parms)
13300 tree fntype = TREE_TYPE (decl);
13301 tree return_type = complete_type (TREE_TYPE (fntype));
13303 /* In a function definition, arg types must be complete. */
13304 require_complete_types_for_parms (current_function_parms);
13306 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13308 error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13310 /* Make it return void instead, but don't change the
13311 type of the DECL_RESULT, in case we have a named return value. */
13312 if (TREE_CODE (fntype) == METHOD_TYPE)
13314 tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13315 TREE_TYPE (decl)
13316 = build_cplus_method_type (ctype,
13317 void_type_node,
13318 FUNCTION_ARG_CHAIN (decl));
13320 else
13321 TREE_TYPE (decl)
13322 = build_function_type (void_type_node,
13323 TYPE_ARG_TYPES (TREE_TYPE (decl)));
13324 TREE_TYPE (decl)
13325 = build_exception_variant (fntype,
13326 TYPE_RAISES_EXCEPTIONS (fntype));
13328 else
13329 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13332 /* Create the FUNCTION_DECL for a function definition.
13333 DECLSPECS and DECLARATOR are the parts of the declaration;
13334 they describe the function's name and the type it returns,
13335 but twisted together in a fashion that parallels the syntax of C.
13337 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13338 DECLARATOR is really the DECL for the function we are about to
13339 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13340 indicating that the function is an inline defined in-class.
13342 This function creates a binding context for the function body
13343 as well as setting up the FUNCTION_DECL in current_function_decl.
13345 Returns 1 on success. If the DECLARATOR is not suitable for a function
13346 (it defines a datum instead), we return 0, which tells
13347 yyparse to report a parse error.
13349 For C++, we must first check whether that datum makes any sense.
13350 For example, "class A local_a(1,2);" means that variable local_a
13351 is an aggregate of type A, which should have a constructor
13352 applied to it with the argument list [1, 2]. */
13355 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13357 tree decl1;
13358 tree ctype = NULL_TREE;
13359 tree fntype;
13360 tree restype;
13361 int doing_friend = 0;
13362 struct cp_binding_level *bl;
13363 tree current_function_parms;
13365 /* Sanity check. */
13366 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13367 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13369 /* This should only be done once on the top most decl. */
13370 if (have_extern_spec)
13372 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13373 have_extern_spec = false;
13376 if (flags & SF_PRE_PARSED)
13378 decl1 = declarator;
13380 fntype = TREE_TYPE (decl1);
13381 if (TREE_CODE (fntype) == METHOD_TYPE)
13382 ctype = TYPE_METHOD_BASETYPE (fntype);
13384 /* ISO C++ 11.4/5. A friend function defined in a class is in
13385 the (lexical) scope of the class in which it is defined. */
13386 if (!ctype && DECL_FRIEND_P (decl1))
13388 ctype = DECL_FRIEND_CONTEXT (decl1);
13390 /* CTYPE could be null here if we're dealing with a template;
13391 for example, `inline friend float foo()' inside a template
13392 will have no CTYPE set. */
13393 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13394 ctype = NULL_TREE;
13395 else
13396 doing_friend = 1;
13399 last_function_parms = DECL_ARGUMENTS (decl1);
13401 else
13403 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13404 /* If the declarator is not suitable for a function definition,
13405 cause a syntax error. */
13406 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13407 return 0;
13409 cplus_decl_attributes (&decl1, attrs, 0);
13411 /* If #pragma weak was used, mark the decl weak now. */
13412 if (current_binding_level == global_binding_level)
13413 maybe_apply_pragma_weak (decl1);
13415 fntype = TREE_TYPE (decl1);
13417 restype = TREE_TYPE (fntype);
13418 if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
13420 error ("semicolon missing after declaration of `%#T'", restype);
13421 shadow_tag (build_tree_list (NULL_TREE, restype));
13422 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13423 if (TREE_CODE (fntype) == FUNCTION_TYPE)
13424 fntype = build_function_type (integer_type_node,
13425 TYPE_ARG_TYPES (fntype));
13426 else
13427 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13428 integer_type_node,
13429 TYPE_ARG_TYPES (fntype));
13430 TREE_TYPE (decl1) = fntype;
13433 if (TREE_CODE (fntype) == METHOD_TYPE)
13434 ctype = TYPE_METHOD_BASETYPE (fntype);
13435 else if (DECL_MAIN_P (decl1))
13437 /* If this doesn't return integer_type, complain. */
13438 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13440 if (pedantic || warn_return_type)
13441 pedwarn ("return type for `main' changed to `int'");
13442 TREE_TYPE (decl1) = fntype = default_function_type;
13447 if (DECL_DECLARED_INLINE_P (decl1)
13448 && lookup_attribute ("noinline", attrs))
13449 warning ("%Hinline function '%D' given attribute noinline",
13450 &DECL_SOURCE_LOCATION (decl1), decl1);
13452 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13453 /* This is a constructor, we must ensure that any default args
13454 introduced by this definition are propagated to the clones
13455 now. The clones are used directly in overload resolution. */
13456 adjust_clone_args (decl1);
13458 /* Sometimes we don't notice that a function is a static member, and
13459 build a METHOD_TYPE for it. Fix that up now. */
13460 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13461 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13463 revert_static_member_fn (decl1);
13464 last_function_parms = TREE_CHAIN (last_function_parms);
13465 ctype = NULL_TREE;
13468 /* Warn if function was previously implicitly declared
13469 (but not if we warned then). */
13470 if (! warn_implicit
13471 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13472 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13474 /* Set up current_class_type, and enter the scope of the class, if
13475 appropriate. */
13476 if (ctype)
13477 push_nested_class (ctype);
13478 else if (DECL_STATIC_FUNCTION_P (decl1))
13479 push_nested_class (DECL_CONTEXT (decl1));
13481 /* Now that we have entered the scope of the class, we must restore
13482 the bindings for any template parameters surrounding DECL1, if it
13483 is an inline member template. (Order is important; consider the
13484 case where a template parameter has the same name as a field of
13485 the class.) It is not until after this point that
13486 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13487 if (flags & SF_INCLASS_INLINE)
13488 maybe_begin_member_template_processing (decl1);
13490 /* Effective C++ rule 15. */
13491 if (warn_ecpp
13492 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13493 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13494 warning ("`operator=' should return a reference to `*this'");
13496 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13497 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13498 if (!DECL_INITIAL (decl1))
13499 DECL_INITIAL (decl1) = error_mark_node;
13501 /* This function exists in static storage.
13502 (This does not mean `static' in the C sense!) */
13503 TREE_STATIC (decl1) = 1;
13505 /* We must call push_template_decl after current_class_type is set
13506 up. (If we are processing inline definitions after exiting a
13507 class scope, current_class_type will be NULL_TREE until set above
13508 by push_nested_class.) */
13509 if (processing_template_decl)
13510 decl1 = push_template_decl (decl1);
13512 /* We are now in the scope of the function being defined. */
13513 current_function_decl = decl1;
13515 /* Save the parm names or decls from this function's declarator
13516 where store_parm_decls will find them. */
13517 current_function_parms = last_function_parms;
13519 /* Make sure the parameter and return types are reasonable. When
13520 you declare a function, these types can be incomplete, but they
13521 must be complete when you define the function. */
13522 if (! processing_template_decl)
13523 check_function_type (decl1, current_function_parms);
13525 /* Build the return declaration for the function. */
13526 restype = TREE_TYPE (fntype);
13527 /* Promote the value to int before returning it. */
13528 if (c_promoting_integer_type_p (restype))
13529 restype = type_promotes_to (restype);
13530 if (DECL_RESULT (decl1) == NULL_TREE)
13532 DECL_RESULT (decl1)
13533 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13534 c_apply_type_quals_to_decl (cp_type_quals (restype),
13535 DECL_RESULT (decl1));
13538 /* Initialize RTL machinery. We cannot do this until
13539 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13540 even when processing a template; this is how we get
13541 CFUN set up, and our per-function variables initialized.
13542 FIXME factor out the non-RTL stuff. */
13543 bl = current_binding_level;
13544 init_function_start (decl1, input_filename, lineno);
13545 current_binding_level = bl;
13547 /* Even though we're inside a function body, we still don't want to
13548 call expand_expr to calculate the size of a variable-sized array.
13549 We haven't necessarily assigned RTL to all variables yet, so it's
13550 not safe to try to expand expressions involving them. */
13551 immediate_size_expand = 0;
13552 cfun->x_dont_save_pending_sizes_p = 1;
13554 /* Start the statement-tree, start the tree now. */
13555 begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13557 /* Don't double-count statements in templates. */
13558 DECL_NUM_STMTS (decl1) = 0;
13560 /* Let the user know we're compiling this function. */
13561 announce_function (decl1);
13563 /* Record the decl so that the function name is defined.
13564 If we already have a decl for this name, and it is a FUNCTION_DECL,
13565 use the old decl. */
13566 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13568 /* A specialization is not used to guide overload resolution. */
13569 if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13570 && ! DECL_FUNCTION_MEMBER_P (decl1))
13571 decl1 = pushdecl (decl1);
13572 else
13574 /* We need to set the DECL_CONTEXT. */
13575 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13576 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13577 /* And make sure we have enough default args. */
13578 check_default_args (decl1);
13580 fntype = TREE_TYPE (decl1);
13583 /* Reset these in case the call to pushdecl changed them. */
13584 current_function_decl = decl1;
13585 cfun->decl = decl1;
13587 /* If we are (erroneously) defining a function that we have already
13588 defined before, wipe out what we knew before. */
13589 if (!DECL_PENDING_INLINE_P (decl1))
13590 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13592 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13594 /* We know that this was set up by `grokclassfn'. We do not
13595 wait until `store_parm_decls', since evil parse errors may
13596 never get us to that point. Here we keep the consistency
13597 between `current_class_type' and `current_class_ptr'. */
13598 tree t = DECL_ARGUMENTS (decl1);
13600 my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13601 162);
13602 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13603 19990811);
13605 cp_function_chain->x_current_class_ref
13606 = build_indirect_ref (t, NULL);
13607 cp_function_chain->x_current_class_ptr = t;
13609 /* Constructors and destructors need to know whether they're "in
13610 charge" of initializing virtual base classes. */
13611 t = TREE_CHAIN (t);
13612 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13614 current_in_charge_parm = t;
13615 t = TREE_CHAIN (t);
13617 if (DECL_HAS_VTT_PARM_P (decl1))
13619 if (DECL_NAME (t) != vtt_parm_identifier)
13620 abort ();
13621 current_vtt_parm = t;
13625 if (DECL_INTERFACE_KNOWN (decl1))
13627 tree ctx = decl_function_context (decl1);
13629 if (DECL_NOT_REALLY_EXTERN (decl1))
13630 DECL_EXTERNAL (decl1) = 0;
13632 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13633 && TREE_PUBLIC (ctx))
13634 /* This is a function in a local class in an extern inline
13635 function. */
13636 comdat_linkage (decl1);
13638 /* If this function belongs to an interface, it is public.
13639 If it belongs to someone else's interface, it is also external.
13640 This only affects inlines and template instantiations. */
13641 else if (interface_unknown == 0
13642 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13643 || flag_alt_external_templates))
13645 if (DECL_DECLARED_INLINE_P (decl1)
13646 || DECL_TEMPLATE_INSTANTIATION (decl1)
13647 || processing_template_decl)
13649 DECL_EXTERNAL (decl1)
13650 = (interface_only
13651 || (DECL_DECLARED_INLINE_P (decl1)
13652 && ! flag_implement_inlines
13653 && !DECL_VINDEX (decl1)));
13655 /* For WIN32 we also want to put these in linkonce sections. */
13656 maybe_make_one_only (decl1);
13658 else
13659 DECL_EXTERNAL (decl1) = 0;
13660 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13661 DECL_INTERFACE_KNOWN (decl1) = 1;
13663 else if (interface_unknown && interface_only
13664 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13665 || flag_alt_external_templates))
13667 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13668 interface, we will have interface_only set but not
13669 interface_known. In that case, we don't want to use the normal
13670 heuristics because someone will supply a #pragma implementation
13671 elsewhere, and deducing it here would produce a conflict. */
13672 comdat_linkage (decl1);
13673 DECL_EXTERNAL (decl1) = 0;
13674 DECL_INTERFACE_KNOWN (decl1) = 1;
13675 DECL_DEFER_OUTPUT (decl1) = 1;
13677 else
13679 /* This is a definition, not a reference.
13680 So clear DECL_EXTERNAL. */
13681 DECL_EXTERNAL (decl1) = 0;
13683 if ((DECL_DECLARED_INLINE_P (decl1)
13684 || DECL_TEMPLATE_INSTANTIATION (decl1))
13685 && ! DECL_INTERFACE_KNOWN (decl1)
13686 /* Don't try to defer nested functions for now. */
13687 && ! decl_function_context (decl1))
13688 DECL_DEFER_OUTPUT (decl1) = 1;
13689 else
13690 DECL_INTERFACE_KNOWN (decl1) = 1;
13693 pushlevel (0);
13694 current_binding_level->parm_flag = 1;
13696 ++function_depth;
13698 if (DECL_DESTRUCTOR_P (decl1))
13700 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13701 DECL_CONTEXT (dtor_label) = current_function_decl;
13704 start_fname_decls ();
13706 store_parm_decls (current_function_parms);
13708 return 1;
13711 /* Store the parameter declarations into the current function declaration.
13712 This is called after parsing the parameter declarations, before
13713 digesting the body of the function.
13715 Also install to binding contour return value identifier, if any. */
13717 static void
13718 store_parm_decls (tree current_function_parms)
13720 register tree fndecl = current_function_decl;
13721 register tree parm;
13723 /* This is a chain of any other decls that came in among the parm
13724 declarations. If a parm is declared with enum {foo, bar} x;
13725 then CONST_DECLs for foo and bar are put here. */
13726 tree nonparms = NULL_TREE;
13728 if (current_function_parms)
13730 /* This case is when the function was defined with an ANSI prototype.
13731 The parms already have decls, so we need not do anything here
13732 except record them as in effect
13733 and complain if any redundant old-style parm decls were written. */
13735 tree specparms = current_function_parms;
13736 tree next;
13738 /* Must clear this because it might contain TYPE_DECLs declared
13739 at class level. */
13740 storedecls (NULL_TREE);
13742 /* If we're doing semantic analysis, then we'll call pushdecl
13743 for each of these. We must do them in reverse order so that
13744 they end in the correct forward order. */
13745 specparms = nreverse (specparms);
13747 for (parm = specparms; parm; parm = next)
13749 next = TREE_CHAIN (parm);
13750 if (TREE_CODE (parm) == PARM_DECL)
13752 if (DECL_NAME (parm) == NULL_TREE
13753 || TREE_CODE (parm) != VOID_TYPE)
13754 pushdecl (parm);
13755 else
13756 error ("parameter `%D' declared void", parm);
13758 else
13760 /* If we find an enum constant or a type tag,
13761 put it aside for the moment. */
13762 TREE_CHAIN (parm) = NULL_TREE;
13763 nonparms = chainon (nonparms, parm);
13767 /* Get the decls in their original chain order and record in the
13768 function. This is all and only the PARM_DECLs that were
13769 pushed into scope by the loop above. */
13770 DECL_ARGUMENTS (fndecl) = getdecls ();
13771 storetags (gettags ());
13773 else
13774 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13776 /* Now store the final chain of decls for the arguments
13777 as the decl-chain of the current lexical scope.
13778 Put the enumerators in as well, at the front so that
13779 DECL_ARGUMENTS is not modified. */
13780 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13782 /* Do the starting of the exception specifications, if we have any. */
13783 if (flag_exceptions && !processing_template_decl
13784 && flag_enforce_eh_specs
13785 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13786 current_eh_spec_block = begin_eh_spec_block ();
13790 /* We have finished doing semantic analysis on DECL, but have not yet
13791 generated RTL for its body. Save away our current state, so that
13792 when we want to generate RTL later we know what to do. */
13794 static void
13795 save_function_data (tree decl)
13797 struct language_function *f;
13799 /* Save the language-specific per-function data so that we can
13800 get it back when we really expand this function. */
13801 my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13802 19990908);
13804 /* Make a copy. */
13805 f = ((struct language_function *)
13806 ggc_alloc (sizeof (struct language_function)));
13807 memcpy (f, cp_function_chain, sizeof (struct language_function));
13808 DECL_SAVED_FUNCTION_DATA (decl) = f;
13810 /* Clear out the bits we don't need. */
13811 f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13812 f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13813 f->x_named_label_uses = NULL;
13814 f->bindings = NULL;
13815 f->x_local_names = NULL;
13817 /* When we get back here again, we will be expanding. */
13818 f->x_expanding_p = 1;
13820 /* If we've already decided that we cannot inline this function, we
13821 must remember that fact when we actually go to expand the
13822 function. */
13823 if (current_function_cannot_inline)
13825 f->cannot_inline = current_function_cannot_inline;
13826 DECL_INLINE (decl) = 0;
13830 /* Add a note to mark the beginning of the main body of the constructor.
13831 This is used to set up the data structures for the cleanup regions for
13832 fully-constructed bases and members. */
13834 static void
13835 begin_constructor_body (void)
13839 /* Add a note to mark the end of the main body of the constructor. This is
13840 used to end the cleanup regions for fully-constructed bases and
13841 members. */
13843 static void
13844 finish_constructor_body (void)
13848 /* Do all the processing for the beginning of a destructor; set up the
13849 vtable pointers and cleanups for bases and members. */
13851 static void
13852 begin_destructor_body (void)
13854 tree if_stmt;
13855 tree compound_stmt;
13857 /* If the dtor is empty, and we know there is not any possible
13858 way we could use any vtable entries, before they are possibly
13859 set by a base class dtor, we don't have to setup the vtables,
13860 as we know that any base class dtor will set up any vtables
13861 it needs. We avoid MI, because one base class dtor can do a
13862 virtual dispatch to an overridden function that would need to
13863 have a non-related vtable set up, we cannot avoid setting up
13864 vtables in that case. We could change this to see if there
13865 is just one vtable.
13867 ??? In the destructor for a class, the vtables are set
13868 appropriately for that class. There will be no non-related
13869 vtables. jason 2001-12-11. */
13870 if_stmt = begin_if_stmt ();
13872 /* If it is not safe to avoid setting up the vtables, then
13873 someone will change the condition to be boolean_true_node.
13874 (Actually, for now, we do not have code to set the condition
13875 appropriately, so we just assume that we always need to
13876 initialize the vtables.) */
13877 finish_if_stmt_cond (boolean_true_node, if_stmt);
13879 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13881 /* Make all virtual function table pointers in non-virtual base
13882 classes point to CURRENT_CLASS_TYPE's virtual function
13883 tables. */
13884 initialize_vtbl_ptrs (current_class_ptr);
13886 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13887 finish_then_clause (if_stmt);
13888 finish_if_stmt ();
13890 /* And insert cleanups for our bases and members so that they
13891 will be properly destroyed if we throw. */
13892 push_base_cleanups ();
13895 /* At the end of every destructor we generate code to delete the object if
13896 necessary. Do that now. */
13898 static void
13899 finish_destructor_body (void)
13901 tree exprstmt;
13903 /* Any return from a destructor will end up here; that way all base
13904 and member cleanups will be run when the function returns. */
13905 add_stmt (build_stmt (LABEL_STMT, dtor_label));
13907 /* In a virtual destructor, we must call delete. */
13908 if (DECL_VIRTUAL_P (current_function_decl))
13910 tree if_stmt;
13911 tree virtual_size = cxx_sizeof (current_class_type);
13913 /* [class.dtor]
13915 At the point of definition of a virtual destructor (including
13916 an implicit definition), non-placement operator delete shall
13917 be looked up in the scope of the destructor's class and if
13918 found shall be accessible and unambiguous. */
13919 exprstmt = build_op_delete_call
13920 (DELETE_EXPR, current_class_ptr, virtual_size,
13921 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13923 if_stmt = begin_if_stmt ();
13924 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13925 current_in_charge_parm,
13926 integer_one_node),
13927 if_stmt);
13928 finish_expr_stmt (exprstmt);
13929 finish_then_clause (if_stmt);
13930 finish_if_stmt ();
13934 /* Do the necessary processing for the beginning of a function body, which
13935 in this case includes member-initializers, but not the catch clauses of
13936 a function-try-block. Currently, this means opening a binding level
13937 for the member-initializers (in a ctor) and member cleanups (in a dtor).
13938 In other functions, this isn't necessary, but it doesn't hurt. */
13940 tree
13941 begin_function_body (void)
13943 tree stmt;
13945 if (processing_template_decl)
13946 /* Do nothing now. */;
13947 else
13948 /* Always keep the BLOCK node associated with the outermost pair of
13949 curly braces of a function. These are needed for correct
13950 operation of dwarfout.c. */
13951 keep_next_level (1);
13953 stmt = begin_compound_stmt (0);
13954 COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13956 if (processing_template_decl)
13957 /* Do nothing now. */;
13958 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13959 begin_constructor_body ();
13960 else if (DECL_DESTRUCTOR_P (current_function_decl))
13961 begin_destructor_body ();
13963 return stmt;
13966 /* Do the processing for the end of a function body. Currently, this means
13967 closing out the cleanups for fully-constructed bases and members, and in
13968 the case of the destructor, deleting the object if desired. Again, this
13969 is only meaningful for [cd]tors, since they are the only functions where
13970 there is a significant distinction between the main body and any
13971 function catch clauses. Handling, say, main() return semantics here
13972 would be wrong, as flowing off the end of a function catch clause for
13973 main() would also need to return 0. */
13975 void
13976 finish_function_body (tree compstmt)
13978 /* Close the block. */
13979 finish_compound_stmt (0, compstmt);
13981 if (processing_template_decl)
13982 /* Do nothing now. */;
13983 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13984 finish_constructor_body ();
13985 else if (DECL_DESTRUCTOR_P (current_function_decl))
13986 finish_destructor_body ();
13989 /* Finish up a function declaration and compile that function
13990 all the way to assembler language output. The free the storage
13991 for the function definition.
13993 FLAGS is a bitwise or of the following values:
13994 2 - INCLASS_INLINE
13995 We just finished processing the body of an in-class inline
13996 function definition. (This processing will have taken place
13997 after the class definition is complete.) */
13999 tree
14000 finish_function (int flags)
14002 register tree fndecl = current_function_decl;
14003 tree fntype, ctype = NULL_TREE;
14004 int inclass_inline = (flags & 2) != 0;
14005 int nested;
14007 /* When we get some parse errors, we can end up without a
14008 current_function_decl, so cope. */
14009 if (fndecl == NULL_TREE)
14010 return error_mark_node;
14012 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14013 && DECL_VIRTUAL_P (fndecl)
14014 && !processing_template_decl)
14016 tree fnclass = DECL_CONTEXT (fndecl);
14017 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14018 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14021 nested = function_depth > 1;
14022 fntype = TREE_TYPE (fndecl);
14024 /* TREE_READONLY (fndecl) = 1;
14025 This caused &foo to be of type ptr-to-const-function
14026 which then got a warning when stored in a ptr-to-function variable. */
14028 my_friendly_assert (building_stmt_tree (), 20000911);
14030 finish_fname_decls ();
14032 /* For a cloned function, we've already got all the code we need;
14033 there's no need to add any extra bits. */
14034 if (!DECL_CLONED_FUNCTION_P (fndecl))
14036 if (DECL_MAIN_P (current_function_decl))
14038 /* Make it so that `main' always returns 0 by default. */
14039 #if VMS_TARGET
14040 finish_return_stmt (integer_one_node);
14041 #else
14042 finish_return_stmt (integer_zero_node);
14043 #endif
14046 /* Finish dealing with exception specifiers. */
14047 if (flag_exceptions && !processing_template_decl
14048 && flag_enforce_eh_specs
14049 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
14050 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14051 (TREE_TYPE (current_function_decl)),
14052 current_eh_spec_block);
14055 /* If we're saving up tree structure, tie off the function now. */
14056 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
14058 /* This must come after expand_function_end because cleanups might
14059 have declarations (from inline functions) that need to go into
14060 this function's blocks. */
14062 /* If the current binding level isn't the outermost binding level
14063 for this function, either there is a bug, or we have experienced
14064 syntax errors and the statement tree is malformed. */
14065 if (current_binding_level->parm_flag != 1)
14067 /* Make sure we have already experienced errors. */
14068 if (errorcount == 0)
14069 abort ();
14071 /* Throw away the broken statement tree and extra binding
14072 levels. */
14073 DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14075 while (current_binding_level->parm_flag != 1)
14077 if (current_binding_level->parm_flag == 2)
14078 pop_nested_class ();
14079 else
14080 poplevel (0, 0, 0);
14083 poplevel (1, 0, 1);
14085 /* Set up the named return value optimization, if we can. Here, we
14086 eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14087 for the nrv. genrtl_start_function and declare_return_variable
14088 handle making the nrv and RESULT_DECL share space. */
14089 if (current_function_return_value)
14091 tree r = current_function_return_value;
14092 /* This is only worth doing for fns that return in memory--and
14093 simpler, since we don't have to worry about promoted modes. */
14094 if (r != error_mark_node
14095 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl))))
14097 DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14098 walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14099 nullify_returns_r, r);
14101 else
14102 /* Clear it so genrtl_start_function and declare_return_variable
14103 know we're not optimizing. */
14104 current_function_return_value = NULL_TREE;
14107 /* Remember that we were in class scope. */
14108 if (current_class_name)
14109 ctype = current_class_type;
14111 /* Must mark the RESULT_DECL as being in this function. */
14112 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14114 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14115 to the FUNCTION_DECL node itself. */
14116 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14118 /* Save away current state, if appropriate. */
14119 if (!processing_template_decl)
14120 save_function_data (fndecl);
14122 /* If this function calls `setjmp' it cannot be inlined. When
14123 `longjmp' is called it is not guaranteed to restore the value of
14124 local variables that have been modified since the call to
14125 `setjmp'. So, if were to inline this function into some caller
14126 `c', then when we `longjmp', we might not restore all variables
14127 in `c'. (It might seem, at first blush, that there's no way for
14128 this function to modify local variables in `c', but their
14129 addresses may have been stored somewhere accessible to this
14130 function.) */
14131 if (!processing_template_decl && calls_setjmp_p (fndecl))
14132 DECL_UNINLINABLE (fndecl) = 1;
14134 /* Complain if there's just no return statement. */
14135 if (warn_return_type
14136 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14137 && !current_function_returns_value && !current_function_returns_null
14138 /* Don't complain if we abort or throw. */
14139 && !current_function_returns_abnormally
14140 && !DECL_NAME (DECL_RESULT (fndecl))
14141 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
14142 inline function, as we might never be compiled separately. */
14143 && (DECL_INLINE (fndecl) || processing_template_decl))
14144 warning ("no return statement in function returning non-void");
14146 /* Clear out memory we no longer need. */
14147 free_after_parsing (cfun);
14148 /* Since we never call rest_of_compilation, we never clear
14149 CFUN. Do so explicitly. */
14150 free_after_compilation (cfun);
14151 cfun = NULL;
14153 /* If this is an in-class inline definition, we may have to pop the
14154 bindings for the template parameters that we added in
14155 maybe_begin_member_template_processing when start_function was
14156 called. */
14157 if (inclass_inline)
14158 maybe_end_member_template_processing ();
14160 /* Leave the scope of the class. */
14161 if (ctype)
14162 pop_nested_class ();
14164 --function_depth;
14166 /* Clean up. */
14167 if (! nested)
14168 /* Let the error reporting routines know that we're outside a
14169 function. For a nested function, this value is used in
14170 cxx_pop_function_context and then reset via pop_function_context. */
14171 current_function_decl = NULL_TREE;
14173 return fndecl;
14176 /* Create the FUNCTION_DECL for a function definition.
14177 DECLSPECS and DECLARATOR are the parts of the declaration;
14178 they describe the return type and the name of the function,
14179 but twisted together in a fashion that parallels the syntax of C.
14181 This function creates a binding context for the function body
14182 as well as setting up the FUNCTION_DECL in current_function_decl.
14184 Returns a FUNCTION_DECL on success.
14186 If the DECLARATOR is not suitable for a function (it defines a datum
14187 instead), we return 0, which tells yyparse to report a parse error.
14189 May return void_type_node indicating that this method is actually
14190 a friend. See grokfield for more details.
14192 Came here with a `.pushlevel' .
14194 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14195 CHANGES TO CODE IN `grokfield'. */
14197 tree
14198 start_method (tree declspecs, tree declarator, tree attrlist)
14200 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14201 &attrlist);
14203 if (fndecl == error_mark_node)
14204 return error_mark_node;
14206 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14208 error ("invalid member function declaration");
14209 return error_mark_node;
14212 if (attrlist)
14213 cplus_decl_attributes (&fndecl, attrlist, 0);
14215 /* Pass friends other than inline friend functions back. */
14216 if (fndecl == void_type_node)
14217 return fndecl;
14219 if (DECL_IN_AGGR_P (fndecl))
14221 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14223 if (DECL_CONTEXT (fndecl)
14224 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14225 error ("`%D' is already defined in class `%T'", fndecl,
14226 DECL_CONTEXT (fndecl));
14228 return void_type_node;
14231 check_template_shadow (fndecl);
14233 DECL_DECLARED_INLINE_P (fndecl) = 1;
14235 DID_INLINE_FUNC (fndecl) = 0;
14236 if (flag_default_inline)
14237 DECL_INLINE (fndecl) = 1;
14239 /* We process method specializations in finish_struct_1. */
14240 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14241 fndecl = push_template_decl (fndecl);
14243 if (! DECL_FRIEND_P (fndecl))
14245 if (TREE_CHAIN (fndecl))
14247 fndecl = copy_node (fndecl);
14248 TREE_CHAIN (fndecl) = NULL_TREE;
14250 grok_special_member_properties (fndecl);
14253 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14255 /* Make a place for the parms */
14256 pushlevel (0);
14257 current_binding_level->parm_flag = 1;
14259 DECL_IN_AGGR_P (fndecl) = 1;
14260 return fndecl;
14263 /* Go through the motions of finishing a function definition.
14264 We don't compile this method until after the whole class has
14265 been processed.
14267 FINISH_METHOD must return something that looks as though it
14268 came from GROKFIELD (since we are defining a method, after all).
14270 This is called after parsing the body of the function definition.
14271 STMTS is the chain of statements that makes up the function body.
14273 DECL is the ..._DECL that `start_method' provided. */
14275 tree
14276 finish_method (tree decl)
14278 register tree fndecl = decl;
14279 tree old_initial;
14281 register tree link;
14283 if (decl == void_type_node)
14284 return decl;
14286 old_initial = DECL_INITIAL (fndecl);
14288 /* Undo the level for the parms (from start_method).
14289 This is like poplevel, but it causes nothing to be
14290 saved. Saving information here confuses symbol-table
14291 output routines. Besides, this information will
14292 be correctly output when this method is actually
14293 compiled. */
14295 /* Clear out the meanings of the local variables of this level;
14296 also record in each decl which block it belongs to. */
14298 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14300 if (DECL_NAME (link) != NULL_TREE)
14301 pop_binding (DECL_NAME (link), link);
14302 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14303 DECL_CONTEXT (link) = NULL_TREE;
14306 poplevel (0, 0, 0);
14308 DECL_INITIAL (fndecl) = old_initial;
14310 /* We used to check if the context of FNDECL was different from
14311 current_class_type as another way to get inside here. This didn't work
14312 for String.cc in libg++. */
14313 if (DECL_FRIEND_P (fndecl))
14315 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14316 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14317 decl = void_type_node;
14320 return decl;
14324 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14325 we can lay it out later, when and if its type becomes complete. */
14327 void
14328 maybe_register_incomplete_var (tree var)
14330 my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14332 /* Keep track of variables with incomplete types. */
14333 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14334 && DECL_EXTERNAL (var))
14336 tree inner_type = TREE_TYPE (var);
14338 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14339 inner_type = TREE_TYPE (inner_type);
14340 inner_type = TYPE_MAIN_VARIANT (inner_type);
14342 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14343 /* RTTI TD entries are created while defining the type_info. */
14344 || (TYPE_LANG_SPECIFIC (inner_type)
14345 && TYPE_BEING_DEFINED (inner_type)))
14346 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14350 /* Called when a class type (given by TYPE) is defined. If there are
14351 any existing VAR_DECLs whose type hsa been completed by this
14352 declaration, update them now. */
14354 void
14355 complete_vars (tree type)
14357 tree *list = &incomplete_vars;
14359 my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14360 while (*list)
14362 if (same_type_p (type, TREE_PURPOSE (*list)))
14364 tree var = TREE_VALUE (*list);
14365 /* Complete the type of the variable. The VAR_DECL itself
14366 will be laid out in expand_expr. */
14367 complete_type (TREE_TYPE (var));
14368 /* Remove this entry from the list. */
14369 *list = TREE_CHAIN (*list);
14371 else
14372 list = &TREE_CHAIN (*list);
14376 /* If DECL is of a type which needs a cleanup, build that cleanup
14377 here. */
14379 tree
14380 cxx_maybe_build_cleanup (tree decl)
14382 tree type = TREE_TYPE (decl);
14384 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14386 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14387 tree rval;
14389 if (TREE_CODE (type) == ARRAY_TYPE)
14390 rval = decl;
14391 else
14393 cxx_mark_addressable (decl);
14394 rval = build_unary_op (ADDR_EXPR, decl, 0);
14397 /* Optimize for space over speed here. */
14398 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14399 || flag_expensive_optimizations)
14400 flags |= LOOKUP_NONVIRTUAL;
14402 rval = build_delete (TREE_TYPE (rval), rval,
14403 sfk_complete_destructor, flags, 0);
14405 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14406 && ! TYPE_HAS_DESTRUCTOR (type))
14407 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
14408 build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
14410 return rval;
14412 return NULL_TREE;
14415 /* When a stmt has been parsed, this function is called. */
14417 void
14418 finish_stmt (void)
14420 /* Always assume this statement was not an expression statement. If
14421 it actually was an expression statement, its our callers
14422 responsibility to fix this up. */
14423 last_expr_type = NULL_TREE;
14426 /* DECL was originally constructed as a non-static member function,
14427 but turned out to be static. Update it accordingly. */
14429 void
14430 revert_static_member_fn (tree decl)
14432 tree tmp;
14433 tree function = TREE_TYPE (decl);
14434 tree args = TYPE_ARG_TYPES (function);
14436 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14437 != TYPE_UNQUALIFIED)
14438 error ("static member function `%#D' declared with type qualifiers",
14439 decl);
14441 args = TREE_CHAIN (args);
14442 tmp = build_function_type (TREE_TYPE (function), args);
14443 tmp = build_qualified_type (tmp, cp_type_quals (function));
14444 tmp = build_exception_variant (tmp,
14445 TYPE_RAISES_EXCEPTIONS (function));
14446 TREE_TYPE (decl) = tmp;
14447 if (DECL_ARGUMENTS (decl))
14448 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14449 DECL_STATIC_FUNCTION_P (decl) = 1;
14452 /* Initialize the variables used during compilation of a C++
14453 function. */
14455 void
14456 cxx_push_function_context (struct function * f)
14458 struct language_function *p
14459 = ((struct language_function *)
14460 ggc_alloc_cleared (sizeof (struct language_function)));
14461 f->language = p;
14463 /* It takes an explicit call to expand_body to generate RTL for a
14464 function. */
14465 expanding_p = 0;
14467 /* Whenever we start a new function, we destroy temporaries in the
14468 usual way. */
14469 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14472 /* Free the language-specific parts of F, now that we've finished
14473 compiling the function. */
14475 void
14476 cxx_pop_function_context (struct function * f)
14478 f->language = 0;
14481 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14482 one of the language-independent trees. */
14484 enum cp_tree_node_structure_enum
14485 cp_tree_node_structure (union lang_tree_node * t)
14487 switch (TREE_CODE (&t->generic))
14489 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
14490 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
14491 case OVERLOAD: return TS_CP_OVERLOAD;
14492 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14493 case PTRMEM_CST: return TS_CP_PTRMEM;
14494 case BASELINK: return TS_CP_BASELINK;
14495 case WRAPPER: return TS_CP_WRAPPER;
14496 case SRCLOC: return TS_CP_SRCLOC;
14497 default: return TS_CP_GENERIC;
14501 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14502 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
14504 tree
14505 identifier_global_value (tree t)
14507 return IDENTIFIER_GLOBAL_VALUE (t);
14510 /* Build the void_list_node (void_type_node having been created). */
14511 tree
14512 build_void_list_node (void)
14514 tree t = build_tree_list (NULL_TREE, void_type_node);
14515 TREE_PARMLIST (t) = 1;
14516 return t;
14519 static int
14520 cp_missing_noreturn_ok_p (tree decl)
14522 /* A missing noreturn is ok for the `main' function. */
14523 return DECL_MAIN_P (decl);
14526 #include "gt-cp-decl.h"
14527 #include "gtype-cp.h"