decl.c (build_enumerator): Remove unneeded test.
[official-gcc.git] / gcc / cp / decl.c
blobd9f29e165901b7ad3774d39dc9524dbddd580402
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"
54 static tree grokparms (tree);
55 static const char *redeclaration_error_message (tree, tree);
57 static void push_binding_level (struct cp_binding_level *, int,
58 int);
59 static void pop_binding_level (void);
60 static void suspend_binding_level (void);
61 static void resume_binding_level (struct cp_binding_level *);
62 static struct cp_binding_level *make_binding_level (void);
63 static void declare_namespace_level (void);
64 static int decl_jump_unsafe (tree);
65 static void storedecls (tree);
66 static void require_complete_types_for_parms (tree);
67 static int ambi_op_p (enum tree_code);
68 static int unary_op_p (enum tree_code);
69 static tree store_bindings (tree, tree);
70 static tree lookup_tag_reverse (tree, tree);
71 static void push_local_name (tree);
72 static void warn_extern_redeclared_static (tree, tree);
73 static tree grok_reference_init (tree, tree, tree);
74 static tree grokfndecl (tree, tree, tree, tree, int,
75 enum overload_flags, tree,
76 tree, int, int, int, int, int, int, tree);
77 static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
78 static tree follow_tag_typedef (tree);
79 static tree lookup_tag (enum tree_code, tree,
80 struct cp_binding_level *, int);
81 static void set_identifier_type_value_with_scope
82 (tree, tree, struct cp_binding_level *);
83 static void record_unknown_type (tree, const char *);
84 static tree builtin_function_1 (const char *, tree, tree, int,
85 enum built_in_class, const char *,
86 tree);
87 static tree build_library_fn_1 (tree, enum tree_code, tree);
88 static int member_function_or_else (tree, tree, enum overload_flags);
89 static void bad_specifiers (tree, const char *, int, int, int, int,
90 int);
91 static tree maybe_process_template_type_declaration
92 (tree, int, struct cp_binding_level*);
93 static void check_for_uninitialized_const_var (tree);
94 static hashval_t typename_hash (const void *);
95 static int typename_compare (const void *, const void *);
96 static void push_binding (tree, tree, struct cp_binding_level*);
97 static int add_binding (tree, tree);
98 static void pop_binding (tree, tree);
99 static tree local_variable_p_walkfn (tree *, int *, void *);
100 static tree find_binding (tree, tree);
101 static tree select_decl (tree, int);
102 static int lookup_flags (int, int);
103 static tree qualify_lookup (tree, int);
104 static tree record_builtin_java_type (const char *, int);
105 static const char *tag_name (enum tag_types code);
106 static void find_class_binding_level (void);
107 static struct cp_binding_level *innermost_nonclass_level (void);
108 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
109 static int walk_globals_r (tree, void*);
110 static int walk_vtables_r (tree, void*);
111 static void add_decl_to_level (tree, struct cp_binding_level *);
112 static tree make_label_decl (tree, int);
113 static void use_label (tree);
114 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
115 const char *, int);
116 static void check_previous_goto (struct named_label_use_list *);
117 static void check_switch_goto (struct cp_binding_level *);
118 static void check_previous_gotos (tree);
119 static void pop_label (tree, tree);
120 static void pop_labels (tree);
121 static void maybe_deduce_size_from_array_init (tree, tree);
122 static void layout_var_decl (tree);
123 static void maybe_commonize_var (tree);
124 static tree check_initializer (tree, tree, int);
125 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
126 static void save_function_data (tree);
127 static void check_function_type (tree, tree);
128 static void begin_constructor_body (void);
129 static void finish_constructor_body (void);
130 static void begin_destructor_body (void);
131 static void finish_destructor_body (void);
132 static tree create_array_type_for_decl (tree, tree, tree);
133 static tree get_atexit_node (void);
134 static tree get_dso_handle_node (void);
135 static tree start_cleanup_fn (void);
136 static void end_cleanup_fn (void);
137 static tree cp_make_fname_decl (tree, int);
138 static void initialize_predefined_identifiers (void);
139 static tree check_special_function_return_type
140 (special_function_kind, tree, tree);
141 static tree push_cp_library_fn (enum tree_code, tree);
142 static tree build_cp_library_fn (tree, enum tree_code, tree);
143 static void store_parm_decls (tree);
144 static int cp_missing_noreturn_ok_p (tree);
145 static void initialize_local_var (tree, tree);
146 static void expand_static_init (tree, tree);
147 static tree next_initializable_field (tree);
148 static tree reshape_init (tree, tree *);
149 static tree build_typename_type (tree, tree, tree);
151 #if defined (DEBUG_BINDING_LEVELS)
152 static void indent (void);
153 #endif
155 /* Erroneous argument lists can use this *IFF* they do not modify it. */
156 tree error_mark_list;
158 /* The following symbols are subsumed in the cp_global_trees array, and
159 listed here individually for documentation purposes.
161 C++ extensions
162 tree wchar_decl_node;
164 tree vtable_entry_type;
165 tree delta_type_node;
166 tree __t_desc_type_node;
167 tree ti_desc_type_node;
168 tree bltn_desc_type_node, ptr_desc_type_node;
169 tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
170 tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
171 tree ptm_desc_type_node;
172 tree base_desc_type_node;
174 tree class_type_node, record_type_node, union_type_node, enum_type_node;
175 tree unknown_type_node;
177 Array type `vtable_entry_type[]'
179 tree vtbl_type_node;
180 tree vtbl_ptr_type_node;
182 Namespaces,
184 tree std_node;
185 tree abi_node;
187 A FUNCTION_DECL which can call `abort'. Not necessarily the
188 one that the user will declare, but sufficient to be called
189 by routines that want to abort the program.
191 tree abort_fndecl;
193 The FUNCTION_DECL for the default `::operator delete'.
195 tree global_delete_fndecl;
197 Used by RTTI
198 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
199 tree tinfo_var_id;
203 tree cp_global_trees[CPTI_MAX];
205 /* Indicates that there is a type value in some namespace, although
206 that is not necessarily in scope at the moment. */
208 static GTY(()) tree global_type_node;
210 /* Used only for jumps to as-yet undefined labels, since jumps to
211 defined labels can have their validity checked immediately. */
213 struct named_label_use_list GTY(())
215 struct cp_binding_level *binding_level;
216 tree names_in_scope;
217 tree label_decl;
218 const char *filename_o_goto;
219 int lineno_o_goto;
220 struct named_label_use_list *next;
223 #define named_label_uses cp_function_chain->x_named_label_uses
225 #define local_names cp_function_chain->x_local_names
227 /* A list of objects which have constructors or destructors
228 which reside in the global scope. The decl is stored in
229 the TREE_VALUE slot and the initializer is stored
230 in the TREE_PURPOSE slot. */
231 tree static_aggregates;
233 /* -- end of C++ */
235 /* A node for the integer constants 2, and 3. */
237 tree integer_two_node, integer_three_node;
239 /* Similar, for last_function_parm_tags. */
240 tree last_function_parms;
242 /* A list of all LABEL_DECLs in the function that have names. Here so
243 we can clear out their names' definitions at the end of the
244 function, and so we can check the validity of jumps to these labels. */
246 struct named_label_list GTY(())
248 struct cp_binding_level *binding_level;
249 tree names_in_scope;
250 tree old_value;
251 tree label_decl;
252 tree bad_decls;
253 struct named_label_list *next;
254 unsigned int in_try_scope : 1;
255 unsigned int in_catch_scope : 1;
258 #define named_labels cp_function_chain->x_named_labels
260 /* The name of the anonymous namespace, throughout this translation
261 unit. */
262 tree anonymous_namespace_name;
264 /* The number of function bodies which we are currently processing.
265 (Zero if we are at namespace scope, one inside the body of a
266 function, two inside the body of a function in a local class, etc.) */
267 int function_depth;
269 /* States indicating how grokdeclarator() should handle declspecs marked
270 with __attribute__((deprecated)). An object declared as
271 __attribute__((deprecated)) suppresses warnings of uses of other
272 deprecated items. */
274 enum deprecated_states {
275 DEPRECATED_NORMAL,
276 DEPRECATED_SUPPRESS
279 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
281 /* Set by add_implicitly_declared_members() to keep those members from
282 being flagged as deprecated or reported as using deprecated
283 types. */
284 int adding_implicit_members = 0;
286 /* True if a declaration with an `extern' linkage specifier is being
287 processed. */
288 bool have_extern_spec;
291 /* For each binding contour we allocate a binding_level structure
292 which records the names defined in that contour.
293 Contours include:
294 0) the global one
295 1) one for each function definition,
296 where internal declarations of the parameters appear.
297 2) one for each compound statement,
298 to record its declarations.
300 The current meaning of a name can be found by searching the levels
301 from the current one out to the global one.
303 Off to the side, may be the class_binding_level. This exists only
304 to catch class-local declarations. It is otherwise nonexistent.
306 Also there may be binding levels that catch cleanups that must be
307 run when exceptions occur. Thus, to see whether a name is bound in
308 the current scope, it is not enough to look in the
309 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
310 instead. */
312 /* Note that the information in the `names' component of the global contour
313 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
315 struct cp_binding_level GTY(())
317 /* A chain of _DECL nodes for all variables, constants, functions,
318 and typedef types. These are in the reverse of the order
319 supplied. There may be OVERLOADs on this list, too, but they
320 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
321 tree names;
323 /* Count of elements in names chain. */
324 size_t names_size;
326 /* A chain of NAMESPACE_DECL nodes. */
327 tree namespaces;
329 /* A chain of VTABLE_DECL nodes. */
330 tree vtables;
332 /* A list of structure, union and enum definitions, for looking up
333 tag names.
334 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
335 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
336 or ENUMERAL_TYPE node.
338 C++: the TREE_VALUE nodes can be simple types for
339 component_bindings. */
340 tree tags;
342 /* A list of USING_DECL nodes. */
343 tree usings;
345 /* A list of used namespaces. PURPOSE is the namespace,
346 VALUE the common ancestor with this binding_level's namespace. */
347 tree using_directives;
349 /* If this binding level is the binding level for a class, then
350 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
351 is the name of an entity bound in the class. The TREE_TYPE is
352 the DECL bound by this name in the class. */
353 tree class_shadowed;
355 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
356 is used for all binding levels. In addition the TREE_VALUE is the
357 IDENTIFIER_TYPE_VALUE before we entered the class. */
358 tree type_shadowed;
360 /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local
361 label in this scope. The TREE_PURPOSE is the previous value of
362 the IDENTIFIER_LABEL VALUE. */
363 tree shadowed_labels;
365 /* For each level (except not the global one),
366 a chain of BLOCK nodes for all the levels
367 that were entered and exited one level down. */
368 tree blocks;
370 /* The _TYPE node for this level, if parm_flag == 2. */
371 tree this_class;
373 /* The binding level which this one is contained in (inherits from). */
374 struct cp_binding_level *level_chain;
376 /* List of VAR_DECLS saved from a previous for statement.
377 These would be dead in ISO-conforming code, but might
378 be referenced in ARM-era code. These are stored in a
379 TREE_LIST; the TREE_VALUE is the actual declaration. */
380 tree dead_vars_from_for;
382 /* 1 for the level that holds the parameters of a function.
383 2 for the level that holds a class declaration. */
384 unsigned parm_flag : 2;
386 /* 1 means make a BLOCK for this level regardless of all else.
387 2 for temporary binding contours created by the compiler. */
388 unsigned keep : 2;
390 /* Nonzero if this level "doesn't exist" for tags. */
391 unsigned tag_transparent : 1;
393 /* Nonzero if this level can safely have additional
394 cleanup-needing variables added to it. */
395 unsigned more_cleanups_ok : 1;
396 unsigned have_cleanups : 1;
398 /* Nonzero if this scope is for storing the decls for template
399 parameters and generic decls; these decls will be discarded and
400 replaced with a TEMPLATE_DECL. */
401 unsigned template_parms_p : 1;
403 /* Nonzero if this scope corresponds to the `<>' in a
404 `template <>' clause. Whenever this flag is set,
405 TEMPLATE_PARMS_P will be set as well. */
406 unsigned template_spec_p : 1;
408 /* This is set for a namespace binding level. */
409 unsigned namespace_p : 1;
411 /* True if this level is that of a for-statement where we need to
412 worry about ambiguous (ARM or ISO) scope rules. */
413 unsigned is_for_scope : 1;
415 /* True if this level corresponds to a TRY block. Currently this
416 information is only available while building the tree structure. */
417 unsigned is_try_scope : 1;
419 /* True if this level corresponds to a CATCH block. Currently this
420 information is only available while building the tree structure. */
421 unsigned is_catch_scope : 1;
423 /* Three bits left for this word. */
425 /* Binding depth at which this level began. */
426 unsigned binding_depth;
429 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
431 /* The binding level currently in effect. */
433 #define current_binding_level \
434 (cfun && cp_function_chain->bindings \
435 ? cp_function_chain->bindings \
436 : scope_chain->bindings)
438 /* The binding level of the current class, if any. */
440 #define class_binding_level scope_chain->class_bindings
442 /* A chain of binding_level structures awaiting reuse. */
444 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
446 /* The outermost binding level, for names of file scope.
447 This is created when the compiler is started and exists
448 through the entire run. */
450 static GTY(()) struct cp_binding_level *global_binding_level;
452 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
454 static int keep_next_level_flag;
456 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
457 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
458 time the VAR_DECL was declared, the type was incomplete. */
460 static GTY(()) tree incomplete_vars;
462 #if defined(DEBUG_BINDING_LEVELS)
463 static int binding_depth = 0;
464 static int is_class_level = 0;
466 static void
467 indent (void)
469 register unsigned i;
471 for (i = 0; i < binding_depth*2; i++)
472 putc (' ', stderr);
474 #endif /* defined(DEBUG_BINDING_LEVELS) */
476 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
478 static void
479 push_binding_level (struct cp_binding_level *newlevel,
480 int tag_transparent,
481 int keep)
483 /* Add this level to the front of the chain (stack) of levels that
484 are active. */
485 memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
486 newlevel->level_chain = current_binding_level;
487 current_binding_level = newlevel;
488 newlevel->tag_transparent = tag_transparent;
489 newlevel->more_cleanups_ok = 1;
491 newlevel->keep = keep;
492 #if defined(DEBUG_BINDING_LEVELS)
493 newlevel->binding_depth = binding_depth;
494 indent ();
495 fprintf (stderr, "push %s level 0x%08x line %d\n",
496 (is_class_level) ? "class" : "block", newlevel, lineno);
497 is_class_level = 0;
498 binding_depth++;
499 #endif /* defined(DEBUG_BINDING_LEVELS) */
502 /* Find the innermost enclosing class scope, and reset
503 CLASS_BINDING_LEVEL appropriately. */
505 static void
506 find_class_binding_level (void)
508 struct cp_binding_level *level = current_binding_level;
510 while (level && level->parm_flag != 2)
511 level = level->level_chain;
512 if (level && level->parm_flag == 2)
513 class_binding_level = level;
514 else
515 class_binding_level = 0;
518 static void
519 pop_binding_level (void)
521 if (global_binding_level)
523 /* Cannot pop a level, if there are none left to pop. */
524 if (current_binding_level == global_binding_level)
525 abort ();
527 /* Pop the current level, and free the structure for reuse. */
528 #if defined(DEBUG_BINDING_LEVELS)
529 binding_depth--;
530 indent ();
531 fprintf (stderr, "pop %s level 0x%08x line %d\n",
532 (is_class_level) ? "class" : "block",
533 current_binding_level, lineno);
534 if (is_class_level != (current_binding_level == class_binding_level))
536 indent ();
537 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
539 is_class_level = 0;
540 #endif /* defined(DEBUG_BINDING_LEVELS) */
542 register struct cp_binding_level *level = current_binding_level;
543 current_binding_level = current_binding_level->level_chain;
544 level->level_chain = free_binding_level;
545 #if 0 /* defined(DEBUG_BINDING_LEVELS) */
546 if (level->binding_depth != binding_depth)
547 abort ();
548 #endif /* defined(DEBUG_BINDING_LEVELS) */
549 free_binding_level = level;
550 find_class_binding_level ();
554 static void
555 suspend_binding_level (void)
557 if (class_binding_level)
558 current_binding_level = class_binding_level;
560 if (global_binding_level)
562 /* Cannot suspend a level, if there are none left to suspend. */
563 if (current_binding_level == global_binding_level)
564 abort ();
566 /* Suspend the current level. */
567 #if defined(DEBUG_BINDING_LEVELS)
568 binding_depth--;
569 indent ();
570 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
571 (is_class_level) ? "class" : "block",
572 current_binding_level, lineno);
573 if (is_class_level != (current_binding_level == class_binding_level))
575 indent ();
576 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
578 is_class_level = 0;
579 #endif /* defined(DEBUG_BINDING_LEVELS) */
580 current_binding_level = current_binding_level->level_chain;
581 find_class_binding_level ();
584 static void
585 resume_binding_level (struct cp_binding_level* b)
587 /* Resuming binding levels is meant only for namespaces,
588 and those cannot nest into classes. */
589 my_friendly_assert(!class_binding_level, 386);
590 /* Also, resuming a non-directly nested namespace is a no-no. */
591 my_friendly_assert(b->level_chain == current_binding_level, 386);
592 current_binding_level = b;
593 #if defined(DEBUG_BINDING_LEVELS)
594 b->binding_depth = binding_depth;
595 indent ();
596 fprintf (stderr, "resume %s level 0x%08x line %d\n",
597 (is_class_level) ? "class" : "block", b, lineno);
598 is_class_level = 0;
599 binding_depth++;
600 #endif /* defined(DEBUG_BINDING_LEVELS) */
603 /* Create a new `struct cp_binding_level'. */
605 static
606 struct cp_binding_level *
607 make_binding_level (void)
609 /* NOSTRICT */
610 return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
613 /* Nonzero if we are currently in the global binding level. */
616 global_bindings_p (void)
618 return current_binding_level == global_binding_level;
621 /* Return the innermost binding level that is not for a class scope. */
623 static struct cp_binding_level *
624 innermost_nonclass_level (void)
626 struct cp_binding_level *b;
628 b = current_binding_level;
629 while (b->parm_flag == 2)
630 b = b->level_chain;
632 return b;
635 /* Nonzero if we are currently in a toplevel binding level. This
636 means either the global binding level or a namespace in a toplevel
637 binding level. Since there are no non-toplevel namespace levels,
638 this really means any namespace or template parameter level. We
639 also include a class whose context is toplevel. */
642 toplevel_bindings_p (void)
644 struct cp_binding_level *b = innermost_nonclass_level ();
646 return b->namespace_p || b->template_parms_p;
649 /* Nonzero if this is a namespace scope, or if we are defining a class
650 which is itself at namespace scope, or whose enclosing class is
651 such a class, etc. */
654 namespace_bindings_p (void)
656 struct cp_binding_level *b = innermost_nonclass_level ();
658 return b->namespace_p;
661 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
662 unconditionally. Otherwise, use the normal logic to decide whether
663 or not to create a BLOCK. */
665 void
666 keep_next_level (int keep)
668 keep_next_level_flag = keep;
671 /* Nonzero if the current level needs to have a BLOCK made. */
674 kept_level_p (void)
676 return (current_binding_level->blocks != NULL_TREE
677 || current_binding_level->keep
678 || current_binding_level->names != NULL_TREE
679 || (current_binding_level->tags != NULL_TREE
680 && !current_binding_level->tag_transparent));
683 static void
684 declare_namespace_level (void)
686 current_binding_level->namespace_p = 1;
689 /* Returns nonzero if this scope was created to store template
690 parameters. */
693 template_parm_scope_p (void)
695 return current_binding_level->template_parms_p;
698 /* Returns the kind of template specialization we are currently
699 processing, given that it's declaration contained N_CLASS_SCOPES
700 explicit scope qualifications. */
702 tmpl_spec_kind
703 current_tmpl_spec_kind (int n_class_scopes)
705 int n_template_parm_scopes = 0;
706 int seen_specialization_p = 0;
707 int innermost_specialization_p = 0;
708 struct cp_binding_level *b;
710 /* Scan through the template parameter scopes. */
711 for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
713 /* If we see a specialization scope inside a parameter scope,
714 then something is wrong. That corresponds to a declaration
715 like:
717 template <class T> template <> ...
719 which is always invalid since [temp.expl.spec] forbids the
720 specialization of a class member template if the enclosing
721 class templates are not explicitly specialized as well. */
722 if (b->template_spec_p)
724 if (n_template_parm_scopes == 0)
725 innermost_specialization_p = 1;
726 else
727 seen_specialization_p = 1;
729 else if (seen_specialization_p == 1)
730 return tsk_invalid_member_spec;
732 ++n_template_parm_scopes;
735 /* Handle explicit instantiations. */
736 if (processing_explicit_instantiation)
738 if (n_template_parm_scopes != 0)
739 /* We've seen a template parameter list during an explicit
740 instantiation. For example:
742 template <class T> template void f(int);
744 This is erroneous. */
745 return tsk_invalid_expl_inst;
746 else
747 return tsk_expl_inst;
750 if (n_template_parm_scopes < n_class_scopes)
751 /* We've not seen enough template headers to match all the
752 specialized classes present. For example:
754 template <class T> void R<T>::S<T>::f(int);
756 This is invalid; there needs to be one set of template
757 parameters for each class. */
758 return tsk_insufficient_parms;
759 else if (n_template_parm_scopes == n_class_scopes)
760 /* We're processing a non-template declaration (even though it may
761 be a member of a template class.) For example:
763 template <class T> void S<T>::f(int);
765 The `class T' maches the `S<T>', leaving no template headers
766 corresponding to the `f'. */
767 return tsk_none;
768 else if (n_template_parm_scopes > n_class_scopes + 1)
769 /* We've got too many template headers. For example:
771 template <> template <class T> void f (T);
773 There need to be more enclosing classes. */
774 return tsk_excessive_parms;
775 else
776 /* This must be a template. It's of the form:
778 template <class T> template <class U> void S<T>::f(U);
780 This is a specialization if the innermost level was a
781 specialization; otherwise it's just a definition of the
782 template. */
783 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
786 void
787 set_class_shadows (tree shadows)
789 class_binding_level->class_shadowed = shadows;
792 /* Enter a new binding level.
793 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
794 not for that of tags. */
796 void
797 pushlevel (int tag_transparent)
799 struct cp_binding_level *newlevel;
801 if (cfun && !doing_semantic_analysis_p ())
802 return;
804 /* Reuse or create a struct for this binding level. */
805 #if defined(DEBUG_BINDING_LEVELS)
806 if (0)
807 #else /* !defined(DEBUG_BINDING_LEVELS) */
808 if (free_binding_level)
809 #endif /* !defined(DEBUG_BINDING_LEVELS) */
811 newlevel = free_binding_level;
812 free_binding_level = free_binding_level->level_chain;
814 else
815 newlevel = make_binding_level ();
817 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
818 keep_next_level_flag = 0;
821 /* We're defining an object of type TYPE. If it needs a cleanup, but
822 we're not allowed to add any more objects with cleanups to the current
823 scope, create a new binding level. */
825 void
826 maybe_push_cleanup_level (tree type)
828 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
829 && current_binding_level->more_cleanups_ok == 0)
831 keep_next_level (2);
832 pushlevel (1);
833 clear_last_expr ();
834 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
838 /* Enter a new scope. The KIND indicates what kind of scope is being
839 created. */
841 void
842 begin_scope (scope_kind sk)
844 pushlevel (0);
846 switch (sk)
848 case sk_block:
849 break;
851 case sk_try:
852 current_binding_level->is_try_scope = 1;
853 break;
855 case sk_catch:
856 current_binding_level->is_catch_scope = 1;
857 break;
859 case sk_for:
860 current_binding_level->is_for_scope = 1;
861 break;
863 case sk_template_spec:
864 current_binding_level->template_spec_p = 1;
865 /* Fall through. */
867 case sk_template_parms:
868 current_binding_level->template_parms_p = 1;
869 break;
871 default:
872 abort ();
876 /* Exit the current scope. */
878 void
879 finish_scope (void)
881 poplevel (0, 0, 0);
884 /* For a binding between a name and an entity at a block scope,
885 this is the `struct cp_binding_level' for the block. */
886 #define BINDING_LEVEL(NODE) \
887 (((struct tree_binding*)(NODE))->scope.level)
889 /* A free list of CPLUS_BINDING nodes, connected by their
890 TREE_CHAINs. */
892 static GTY((deletable (""))) tree free_bindings;
894 /* Make DECL the innermost binding for ID. The LEVEL is the binding
895 level at which this declaration is being bound. */
897 static void
898 push_binding (tree id,
899 tree decl,
900 struct cp_binding_level* level)
902 tree binding;
904 if (free_bindings)
906 binding = free_bindings;
907 free_bindings = TREE_CHAIN (binding);
909 else
910 binding = make_node (CPLUS_BINDING);
912 /* Now, fill in the binding information. */
913 BINDING_VALUE (binding) = decl;
914 BINDING_TYPE (binding) = NULL_TREE;
915 BINDING_LEVEL (binding) = level;
916 INHERITED_VALUE_BINDING_P (binding) = 0;
917 LOCAL_BINDING_P (binding) = (level != class_binding_level);
918 BINDING_HAS_LEVEL_P (binding) = 1;
920 /* And put it on the front of the list of bindings for ID. */
921 TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
922 IDENTIFIER_BINDING (id) = binding;
925 /* ID is already bound in the current scope. But, DECL is an
926 additional binding for ID in the same scope. This is the `struct
927 stat' hack whereby a non-typedef class-name or enum-name can be
928 bound at the same level as some other kind of entity. It's the
929 responsibility of the caller to check that inserting this name is
930 valid here. Returns nonzero if the new binding was successful. */
931 static int
932 add_binding (tree id, tree decl)
934 tree binding = IDENTIFIER_BINDING (id);
935 int ok = 1;
937 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
938 /* The new name is the type name. */
939 BINDING_TYPE (binding) = decl;
940 else if (!BINDING_VALUE (binding))
941 /* This situation arises when push_class_level_binding moves an
942 inherited type-binding out of the way to make room for a new
943 value binding. */
944 BINDING_VALUE (binding) = decl;
945 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
946 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
948 /* The old binding was a type name. It was placed in
949 BINDING_VALUE because it was thought, at the point it was
950 declared, to be the only entity with such a name. Move the
951 type name into the type slot; it is now hidden by the new
952 binding. */
953 BINDING_TYPE (binding) = BINDING_VALUE (binding);
954 BINDING_VALUE (binding) = decl;
955 INHERITED_VALUE_BINDING_P (binding) = 0;
957 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
958 && TREE_CODE (decl) == TYPE_DECL
959 && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
960 && (same_type_p (TREE_TYPE (decl),
961 TREE_TYPE (BINDING_VALUE (binding)))
962 /* If either type involves template parameters, we must
963 wait until instantiation. */
964 || uses_template_parms (TREE_TYPE (decl))
965 || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
966 /* We have two typedef-names, both naming the same type to have
967 the same name. This is OK because of:
969 [dcl.typedef]
971 In a given scope, a typedef specifier can be used to redefine
972 the name of any type declared in that scope to refer to the
973 type to which it already refers. */
974 ok = 0;
975 /* There can be two block-scope declarations of the same variable,
976 so long as they are `extern' declarations. */
977 else if (TREE_CODE (decl) == VAR_DECL
978 && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
979 && DECL_EXTERNAL (decl)
980 && DECL_EXTERNAL (BINDING_VALUE (binding)))
982 duplicate_decls (decl, BINDING_VALUE (binding));
983 ok = 0;
985 else
987 error ("declaration of `%#D'", decl);
988 cp_error_at ("conflicts with previous declaration `%#D'",
989 BINDING_VALUE (binding));
990 ok = 0;
993 return ok;
996 /* Add DECL to the list of things declared in B. */
998 static void
999 add_decl_to_level (tree decl,
1000 struct cp_binding_level* b)
1002 if (TREE_CODE (decl) == NAMESPACE_DECL
1003 && !DECL_NAMESPACE_ALIAS (decl))
1005 TREE_CHAIN (decl) = b->namespaces;
1006 b->namespaces = decl;
1008 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
1010 TREE_CHAIN (decl) = b->vtables;
1011 b->vtables = decl;
1013 else
1015 /* We build up the list in reverse order, and reverse it later if
1016 necessary. */
1017 TREE_CHAIN (decl) = b->names;
1018 b->names = decl;
1019 b->names_size++;
1023 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1024 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1025 doesn't really belong to this binding level, that it got here
1026 through a using-declaration. */
1028 void
1029 push_local_binding (tree id, tree decl, int flags)
1031 struct cp_binding_level *b;
1033 /* Skip over any local classes. This makes sense if we call
1034 push_local_binding with a friend decl of a local class. */
1035 b = current_binding_level;
1036 while (b->parm_flag == 2)
1037 b = b->level_chain;
1039 if (lookup_name_current_level (id))
1041 /* Supplement the existing binding. */
1042 if (!add_binding (id, decl))
1043 /* It didn't work. Something else must be bound at this
1044 level. Do not add DECL to the list of things to pop
1045 later. */
1046 return;
1048 else
1049 /* Create a new binding. */
1050 push_binding (id, decl, b);
1052 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1053 /* We must put the OVERLOAD into a TREE_LIST since the
1054 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1055 decls that got here through a using-declaration. */
1056 decl = build_tree_list (NULL_TREE, decl);
1058 /* And put DECL on the list of things declared by the current
1059 binding level. */
1060 add_decl_to_level (decl, b);
1063 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1064 binding was successful. */
1067 push_class_binding (tree id, tree decl)
1069 int result = 1;
1070 tree binding = IDENTIFIER_BINDING (id);
1071 tree context;
1073 /* Note that we declared this value so that we can issue an error if
1074 this is an invalid redeclaration of a name already used for some
1075 other purpose. */
1076 note_name_declared_in_class (id, decl);
1078 if (binding && BINDING_LEVEL (binding) == class_binding_level)
1079 /* Supplement the existing binding. */
1080 result = add_binding (id, decl);
1081 else
1082 /* Create a new binding. */
1083 push_binding (id, decl, class_binding_level);
1085 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1086 class-level declaration. Note that we do not use DECL here
1087 because of the possibility of the `struct stat' hack; if DECL is
1088 a class-name or enum-name we might prefer a field-name, or some
1089 such. */
1090 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1092 /* If this is a binding from a base class, mark it as such. */
1093 binding = IDENTIFIER_BINDING (id);
1094 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1096 if (TREE_CODE (decl) == OVERLOAD)
1097 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1098 else
1100 my_friendly_assert (DECL_P (decl), 0);
1101 context = context_for_name_lookup (decl);
1104 if (is_properly_derived_from (current_class_type, context))
1105 INHERITED_VALUE_BINDING_P (binding) = 1;
1106 else
1107 INHERITED_VALUE_BINDING_P (binding) = 0;
1109 else if (BINDING_VALUE (binding) == decl)
1110 /* We only encounter a TREE_LIST when push_class_decls detects an
1111 ambiguity. Such an ambiguity can be overridden by a definition
1112 in this class. */
1113 INHERITED_VALUE_BINDING_P (binding) = 1;
1115 return result;
1118 /* Remove the binding for DECL which should be the innermost binding
1119 for ID. */
1121 static void
1122 pop_binding (tree id, tree decl)
1124 tree binding;
1126 if (id == NULL_TREE)
1127 /* It's easiest to write the loops that call this function without
1128 checking whether or not the entities involved have names. We
1129 get here for such an entity. */
1130 return;
1132 /* Get the innermost binding for ID. */
1133 binding = IDENTIFIER_BINDING (id);
1135 /* The name should be bound. */
1136 my_friendly_assert (binding != NULL_TREE, 0);
1138 /* The DECL will be either the ordinary binding or the type
1139 binding for this identifier. Remove that binding. */
1140 if (BINDING_VALUE (binding) == decl)
1141 BINDING_VALUE (binding) = NULL_TREE;
1142 else if (BINDING_TYPE (binding) == decl)
1143 BINDING_TYPE (binding) = NULL_TREE;
1144 else
1145 abort ();
1147 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1149 /* We're completely done with the innermost binding for this
1150 identifier. Unhook it from the list of bindings. */
1151 IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1153 /* Add it to the free list. */
1154 TREE_CHAIN (binding) = free_bindings;
1155 free_bindings = binding;
1157 /* Clear the BINDING_LEVEL so the garbage collector doesn't walk
1158 it. */
1159 BINDING_LEVEL (binding) = NULL;
1163 /* When a label goes out of scope, check to see if that label was used
1164 in a valid manner, and issue any appropriate warnings or errors. */
1166 static void
1167 pop_label (tree label, tree old_value)
1169 if (!processing_template_decl && doing_semantic_analysis_p ())
1171 if (DECL_INITIAL (label) == NULL_TREE)
1173 cp_error_at ("label `%D' used but not defined", label);
1174 /* Avoid crashing later. */
1175 define_label (input_filename, 1, DECL_NAME (label));
1177 else if (warn_unused_label && !TREE_USED (label))
1178 cp_warning_at ("label `%D' defined but not used", label);
1181 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1184 /* At the end of a function, all labels declared within the function
1185 go out of scope. BLOCK is the top-level block for the
1186 function. */
1188 static void
1189 pop_labels (tree block)
1191 struct named_label_list *link;
1193 /* Clear out the definitions of all label names, since their scopes
1194 end here. */
1195 for (link = named_labels; link; link = link->next)
1197 pop_label (link->label_decl, link->old_value);
1198 /* Put the labels into the "variables" of the top-level block,
1199 so debugger can see them. */
1200 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1201 BLOCK_VARS (block) = link->label_decl;
1204 named_labels = NULL;
1207 /* Exit a binding level.
1208 Pop the level off, and restore the state of the identifier-decl mappings
1209 that were in effect when this level was entered.
1211 If KEEP == 1, this level had explicit declarations, so
1212 and create a "block" (a BLOCK node) for the level
1213 to record its declarations and subblocks for symbol table output.
1215 If FUNCTIONBODY is nonzero, this level is the body of a function,
1216 so create a block as if KEEP were set and also clear out all
1217 label names.
1219 If REVERSE is nonzero, reverse the order of decls before putting
1220 them into the BLOCK. */
1222 tree
1223 poplevel (int keep, int reverse, int functionbody)
1225 register tree link;
1226 /* The chain of decls was accumulated in reverse order.
1227 Put it into forward order, just for cleanliness. */
1228 tree decls;
1229 int tmp = functionbody;
1230 int real_functionbody;
1231 tree tags;
1232 tree subblocks;
1233 tree block = NULL_TREE;
1234 tree decl;
1235 int leaving_for_scope;
1237 if (cfun && !doing_semantic_analysis_p ())
1238 return NULL_TREE;
1240 my_friendly_assert (current_binding_level->parm_flag != 2,
1241 19990916);
1243 real_functionbody = (current_binding_level->keep == 2
1244 ? ((functionbody = 0), tmp) : functionbody);
1245 tags = functionbody >= 0 ? current_binding_level->tags : 0;
1246 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1248 my_friendly_assert (!current_binding_level->class_shadowed,
1249 19990414);
1251 /* We used to use KEEP == 2 to indicate that the new block should go
1252 at the beginning of the list of blocks at this binding level,
1253 rather than the end. This hack is no longer used. */
1254 my_friendly_assert (keep == 0 || keep == 1, 0);
1256 if (current_binding_level->keep == 1)
1257 keep = 1;
1259 /* Any uses of undefined labels, and any defined labels, now operate
1260 under constraints of next binding contour. */
1261 if (cfun && !functionbody)
1263 struct cp_binding_level *level_chain;
1264 level_chain = current_binding_level->level_chain;
1265 if (level_chain)
1267 struct named_label_use_list *uses;
1268 struct named_label_list *labels;
1269 for (labels = named_labels; labels; labels = labels->next)
1270 if (labels->binding_level == current_binding_level)
1272 tree decl;
1273 if (current_binding_level->is_try_scope)
1274 labels->in_try_scope = 1;
1275 if (current_binding_level->is_catch_scope)
1276 labels->in_catch_scope = 1;
1277 for (decl = labels->names_in_scope; decl;
1278 decl = TREE_CHAIN (decl))
1279 if (decl_jump_unsafe (decl))
1280 labels->bad_decls = tree_cons (NULL_TREE, decl,
1281 labels->bad_decls);
1282 labels->binding_level = level_chain;
1283 labels->names_in_scope = level_chain->names;
1286 for (uses = named_label_uses; uses; uses = uses->next)
1287 if (uses->binding_level == current_binding_level)
1289 uses->binding_level = level_chain;
1290 uses->names_in_scope = level_chain->names;
1295 /* Get the decls in the order they were written.
1296 Usually current_binding_level->names is in reverse order.
1297 But parameter decls were previously put in forward order. */
1299 if (reverse)
1300 current_binding_level->names
1301 = decls = nreverse (current_binding_level->names);
1302 else
1303 decls = current_binding_level->names;
1305 /* Output any nested inline functions within this block
1306 if they weren't already output. */
1307 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1308 if (TREE_CODE (decl) == FUNCTION_DECL
1309 && ! TREE_ASM_WRITTEN (decl)
1310 && DECL_INITIAL (decl) != NULL_TREE
1311 && TREE_ADDRESSABLE (decl)
1312 && decl_function_context (decl) == current_function_decl)
1314 /* If this decl was copied from a file-scope decl
1315 on account of a block-scope extern decl,
1316 propagate TREE_ADDRESSABLE to the file-scope decl. */
1317 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1318 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1319 else
1321 push_function_context ();
1322 output_inline_function (decl);
1323 pop_function_context ();
1327 /* When not in function-at-a-time mode, expand_end_bindings will
1328 warn about unused variables. But, in function-at-a-time mode
1329 expand_end_bindings is not passed the list of variables in the
1330 current scope, and therefore no warning is emitted. So, we
1331 explicitly warn here. */
1332 if (!processing_template_decl)
1333 warn_about_unused_variables (getdecls ());
1335 /* If there were any declarations or structure tags in that level,
1336 or if this level is a function body,
1337 create a BLOCK to record them for the life of this function. */
1338 block = NULL_TREE;
1339 if (keep == 1 || functionbody)
1340 block = make_node (BLOCK);
1341 if (block != NULL_TREE)
1343 BLOCK_VARS (block) = decls;
1344 BLOCK_SUBBLOCKS (block) = subblocks;
1347 /* In each subblock, record that this is its superior. */
1348 if (keep >= 0)
1349 for (link = subblocks; link; link = TREE_CHAIN (link))
1350 BLOCK_SUPERCONTEXT (link) = block;
1352 /* We still support the old for-scope rules, whereby the variables
1353 in a for-init statement were in scope after the for-statement
1354 ended. We only use the new rules if flag_new_for_scope is
1355 nonzero. */
1356 leaving_for_scope
1357 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1359 /* Remove declarations for all the DECLs in this level. */
1360 for (link = decls; link; link = TREE_CHAIN (link))
1362 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1363 && DECL_NAME (link))
1365 tree outer_binding
1366 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1367 tree ns_binding;
1369 if (!outer_binding)
1370 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1371 else
1372 ns_binding = NULL_TREE;
1374 if (outer_binding
1375 && (BINDING_LEVEL (outer_binding)
1376 == current_binding_level->level_chain))
1377 /* We have something like:
1379 int i;
1380 for (int i; ;);
1382 and we are leaving the `for' scope. There's no reason to
1383 keep the binding of the inner `i' in this case. */
1384 pop_binding (DECL_NAME (link), link);
1385 else if ((outer_binding
1386 && (TREE_CODE (BINDING_VALUE (outer_binding))
1387 == TYPE_DECL))
1388 || (ns_binding
1389 && TREE_CODE (ns_binding) == TYPE_DECL))
1390 /* Here, we have something like:
1392 typedef int I;
1394 void f () {
1395 for (int I; ;);
1398 We must pop the for-scope binding so we know what's a
1399 type and what isn't. */
1400 pop_binding (DECL_NAME (link), link);
1401 else
1403 /* Mark this VAR_DECL as dead so that we can tell we left it
1404 there only for backward compatibility. */
1405 DECL_DEAD_FOR_LOCAL (link) = 1;
1407 /* Keep track of what should of have happenned when we
1408 popped the binding. */
1409 if (outer_binding && BINDING_VALUE (outer_binding))
1410 DECL_SHADOWED_FOR_VAR (link)
1411 = BINDING_VALUE (outer_binding);
1413 /* Add it to the list of dead variables in the next
1414 outermost binding to that we can remove these when we
1415 leave that binding. */
1416 current_binding_level->level_chain->dead_vars_from_for
1417 = tree_cons (NULL_TREE, link,
1418 current_binding_level->level_chain->
1419 dead_vars_from_for);
1421 /* Although we don't pop the CPLUS_BINDING, we do clear
1422 its BINDING_LEVEL since the level is going away now. */
1423 BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1424 = 0;
1427 else
1429 /* Remove the binding. */
1430 decl = link;
1431 if (TREE_CODE (decl) == TREE_LIST)
1432 decl = TREE_VALUE (decl);
1433 if (DECL_P (decl))
1434 pop_binding (DECL_NAME (decl), decl);
1435 else if (TREE_CODE (decl) == OVERLOAD)
1436 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1437 else
1438 abort ();
1442 /* Remove declarations for any `for' variables from inner scopes
1443 that we kept around. */
1444 for (link = current_binding_level->dead_vars_from_for;
1445 link; link = TREE_CHAIN (link))
1446 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1448 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1449 for (link = current_binding_level->type_shadowed;
1450 link; link = TREE_CHAIN (link))
1451 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1453 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1454 for (link = current_binding_level->shadowed_labels;
1455 link;
1456 link = TREE_CHAIN (link))
1457 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1459 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1460 list if a `using' declaration put them there. The debugging
1461 back-ends won't understand OVERLOAD, so we remove them here.
1462 Because the BLOCK_VARS are (temporarily) shared with
1463 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1464 popped all the bindings. */
1465 if (block)
1467 tree* d;
1469 for (d = &BLOCK_VARS (block); *d; )
1471 if (TREE_CODE (*d) == TREE_LIST)
1472 *d = TREE_CHAIN (*d);
1473 else
1474 d = &TREE_CHAIN (*d);
1478 /* If the level being exited is the top level of a function,
1479 check over all the labels. */
1480 if (functionbody)
1482 /* Since this is the top level block of a function, the vars are
1483 the function's parameters. Don't leave them in the BLOCK
1484 because they are found in the FUNCTION_DECL instead. */
1485 BLOCK_VARS (block) = 0;
1486 pop_labels (block);
1489 tmp = current_binding_level->keep;
1491 pop_binding_level ();
1492 if (functionbody)
1493 DECL_INITIAL (current_function_decl) = block;
1494 else if (block)
1495 current_binding_level->blocks
1496 = chainon (current_binding_level->blocks, block);
1498 /* If we did not make a block for the level just exited,
1499 any blocks made for inner levels
1500 (since they cannot be recorded as subblocks in that level)
1501 must be carried forward so they will later become subblocks
1502 of something else. */
1503 else if (subblocks)
1504 current_binding_level->blocks
1505 = chainon (current_binding_level->blocks, subblocks);
1507 /* Each and every BLOCK node created here in `poplevel' is important
1508 (e.g. for proper debugging information) so if we created one
1509 earlier, mark it as "used". */
1510 if (block)
1511 TREE_USED (block) = 1;
1513 /* Take care of compiler's internal binding structures. */
1514 if (tmp == 2)
1516 tree scope_stmts;
1518 scope_stmts
1519 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1520 if (block)
1522 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1523 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1526 block = poplevel (keep, reverse, functionbody);
1529 return block;
1532 /* Delete the node BLOCK from the current binding level.
1533 This is used for the block inside a stmt expr ({...})
1534 so that the block can be reinserted where appropriate. */
1536 void
1537 delete_block (tree block)
1539 tree t;
1540 if (current_binding_level->blocks == block)
1541 current_binding_level->blocks = TREE_CHAIN (block);
1542 for (t = current_binding_level->blocks; t;)
1544 if (TREE_CHAIN (t) == block)
1545 TREE_CHAIN (t) = TREE_CHAIN (block);
1546 else
1547 t = TREE_CHAIN (t);
1549 TREE_CHAIN (block) = NULL_TREE;
1550 /* Clear TREE_USED which is always set by poplevel.
1551 The flag is set again if insert_block is called. */
1552 TREE_USED (block) = 0;
1555 /* Insert BLOCK at the end of the list of subblocks of the
1556 current binding level. This is used when a BIND_EXPR is expanded,
1557 to handle the BLOCK node inside the BIND_EXPR. */
1559 void
1560 insert_block (tree block)
1562 TREE_USED (block) = 1;
1563 current_binding_level->blocks
1564 = chainon (current_binding_level->blocks, block);
1567 /* Set the BLOCK node for the innermost scope
1568 (the one we are currently in). */
1570 void
1571 set_block (tree block ATTRIBUTE_UNUSED )
1573 /* The RTL expansion machinery requires us to provide this callback,
1574 but it is not applicable in function-at-a-time mode. */
1575 my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1578 /* Do a pushlevel for class declarations. */
1580 void
1581 pushlevel_class (void)
1583 register struct cp_binding_level *newlevel;
1585 /* Reuse or create a struct for this binding level. */
1586 #if defined(DEBUG_BINDING_LEVELS)
1587 if (0)
1588 #else /* !defined(DEBUG_BINDING_LEVELS) */
1589 if (free_binding_level)
1590 #endif /* !defined(DEBUG_BINDING_LEVELS) */
1592 newlevel = free_binding_level;
1593 free_binding_level = free_binding_level->level_chain;
1595 else
1596 newlevel = make_binding_level ();
1598 #if defined(DEBUG_BINDING_LEVELS)
1599 is_class_level = 1;
1600 #endif /* defined(DEBUG_BINDING_LEVELS) */
1602 push_binding_level (newlevel, 0, 0);
1604 class_binding_level = current_binding_level;
1605 class_binding_level->parm_flag = 2;
1606 class_binding_level->this_class = current_class_type;
1609 /* ...and a poplevel for class declarations. */
1611 void
1612 poplevel_class (void)
1614 register struct cp_binding_level *level = class_binding_level;
1615 tree shadowed;
1617 my_friendly_assert (level != 0, 354);
1619 /* If we're leaving a toplevel class, don't bother to do the setting
1620 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1621 shouldn't even be used when current_class_type isn't set, and second,
1622 if we don't touch it here, we're able to use the cache effect if the
1623 next time we're entering a class scope, it is the same class. */
1624 if (current_class_depth != 1)
1626 struct cp_binding_level* b;
1628 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1629 for (shadowed = level->class_shadowed;
1630 shadowed;
1631 shadowed = TREE_CHAIN (shadowed))
1632 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1634 /* Find the next enclosing class, and recreate
1635 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1636 b = level->level_chain;
1637 while (b && b->parm_flag != 2)
1638 b = b->level_chain;
1640 if (b)
1641 for (shadowed = b->class_shadowed;
1642 shadowed;
1643 shadowed = TREE_CHAIN (shadowed))
1645 tree t;
1647 t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1648 while (t && BINDING_LEVEL (t) != b)
1649 t = TREE_CHAIN (t);
1651 if (t)
1652 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1653 = BINDING_VALUE (t);
1656 else
1657 /* Remember to save what IDENTIFIER's were bound in this scope so we
1658 can recover from cache misses. */
1660 previous_class_type = current_class_type;
1661 previous_class_values = class_binding_level->class_shadowed;
1663 for (shadowed = level->type_shadowed;
1664 shadowed;
1665 shadowed = TREE_CHAIN (shadowed))
1666 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1668 /* Remove the bindings for all of the class-level declarations. */
1669 for (shadowed = level->class_shadowed;
1670 shadowed;
1671 shadowed = TREE_CHAIN (shadowed))
1672 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1674 /* Now, pop out of the binding level which we created up in the
1675 `pushlevel_class' routine. */
1676 #if defined(DEBUG_BINDING_LEVELS)
1677 is_class_level = 1;
1678 #endif /* defined(DEBUG_BINDING_LEVELS) */
1680 pop_binding_level ();
1683 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1684 for any names in enclosing classes. */
1686 void
1687 clear_identifier_class_values (void)
1689 tree t;
1691 if (!class_binding_level)
1692 return;
1694 for (t = class_binding_level->class_shadowed;
1696 t = TREE_CHAIN (t))
1697 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1700 /* Returns nonzero if T is a virtual function table. */
1703 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1705 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1708 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1709 functions. */
1712 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1714 return (TREE_CODE (t) == TYPE_DECL
1715 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1716 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1719 /* Return the declarations that are members of the namespace NS. */
1721 tree
1722 cp_namespace_decls (tree ns)
1724 return NAMESPACE_LEVEL (ns)->names;
1727 struct walk_globals_data {
1728 walk_globals_pred p;
1729 walk_globals_fn f;
1730 void *data;
1733 /* Walk the vtable declarations in NAMESPACE. Whenever one is found
1734 for which P returns nonzero, call F with its address. If any call
1735 to F returns a nonzero value, return a nonzero value. */
1737 static int
1738 walk_vtables_r (tree namespace, void* data)
1740 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1741 walk_globals_fn f = wgd->f;
1742 void *d = wgd->data;
1743 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1744 int result = 0;
1746 for (; decl ; decl = TREE_CHAIN (decl))
1747 result |= (*f) (&decl, d);
1749 return result;
1752 /* Walk the vtable declarations. Whenever one is found for which P
1753 returns nonzero, call F with its address. If any call to F
1754 returns a nonzero value, return a nonzero value. */
1755 bool
1756 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1758 struct walk_globals_data wgd;
1759 wgd.p = p;
1760 wgd.f = f;
1761 wgd.data = data;
1763 return walk_namespaces (walk_vtables_r, &wgd);
1766 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1767 itself, calling F for each. The DATA is passed to F as well. */
1769 static int
1770 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1772 int result = 0;
1773 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
1775 result |= (*f) (namespace, data);
1777 for (; current; current = TREE_CHAIN (current))
1778 result |= walk_namespaces_r (current, f, data);
1780 return result;
1783 /* Walk all the namespaces, calling F for each. The DATA is passed to
1784 F as well. */
1787 walk_namespaces (walk_namespaces_fn f, void* data)
1789 return walk_namespaces_r (global_namespace, f, data);
1792 /* Walk the global declarations in NAMESPACE. Whenever one is found
1793 for which P returns nonzero, call F with its address. If any call
1794 to F returns a nonzero value, return a nonzero value. */
1796 static int
1797 walk_globals_r (tree namespace, void* data)
1799 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1800 walk_globals_pred p = wgd->p;
1801 walk_globals_fn f = wgd->f;
1802 void *d = wgd->data;
1803 tree *t;
1804 int result = 0;
1806 t = &NAMESPACE_LEVEL (namespace)->names;
1808 while (*t)
1810 tree glbl = *t;
1812 if ((*p) (glbl, d))
1813 result |= (*f) (t, d);
1815 /* If F changed *T, then *T still points at the next item to
1816 examine. */
1817 if (*t == glbl)
1818 t = &TREE_CHAIN (*t);
1821 return result;
1824 /* Walk the global declarations. Whenever one is found for which P
1825 returns true, call F with its address. If any call to F
1826 returns true, return true. */
1828 bool
1829 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1831 struct walk_globals_data wgd;
1832 wgd.p = p;
1833 wgd.f = f;
1834 wgd.data = data;
1836 return walk_namespaces (walk_globals_r, &wgd);
1839 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1840 DATA is non-NULL, this is the last time we will call
1841 wrapup_global_declarations for this NAMESPACE. */
1844 wrapup_globals_for_namespace (tree namespace, void* data)
1846 tree globals = cp_namespace_decls (namespace);
1847 int len = NAMESPACE_LEVEL (namespace)->names_size;
1848 tree *vec = (tree *) alloca (sizeof (tree) * len);
1849 int i;
1850 int result;
1851 tree decl;
1852 int last_time = (data != 0);
1854 if (last_time && namespace == global_namespace)
1855 /* Let compile_file handle the global namespace. */
1856 return 0;
1858 /* Process the decls in reverse order--earliest first.
1859 Put them into VEC from back to front, then take out from front. */
1860 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
1861 vec[len - i - 1] = decl;
1863 if (last_time)
1865 check_global_declarations (vec, len);
1866 return 0;
1869 /* Write out any globals that need to be output. */
1870 result = wrapup_global_declarations (vec, len);
1872 return result;
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 CPLUS_BINDINGS, or any other tree_node
2024 indicating the BINDING_VALUE of global_namespace. */
2026 /* Check whether the a binding for the name to scope is known.
2027 Assumes that the bindings of the name are already a list
2028 of bindings. Returns the binding found, or NULL_TREE. */
2030 static tree
2031 find_binding (tree name, tree scope)
2033 tree iter, prev = NULL_TREE;
2035 scope = ORIGINAL_NAMESPACE (scope);
2037 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2038 iter = TREE_CHAIN (iter))
2040 my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2041 if (BINDING_SCOPE (iter) == scope)
2043 /* Move binding found to the front of the list, so
2044 subsequent lookups will find it faster. */
2045 if (prev)
2047 TREE_CHAIN (prev) = TREE_CHAIN (iter);
2048 TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2049 IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2051 return iter;
2053 prev = iter;
2055 return NULL_TREE;
2058 /* Always returns a binding for name in scope. If the
2059 namespace_bindings is not a list, convert it to one first.
2060 If no binding is found, make a new one. */
2062 tree
2063 binding_for_name (tree name, tree scope)
2065 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2066 tree result;
2068 scope = ORIGINAL_NAMESPACE (scope);
2070 if (b && TREE_CODE (b) != CPLUS_BINDING)
2072 /* Get rid of optimization for global scope. */
2073 IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2074 BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2075 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2077 if (b && (result = find_binding (name, scope)))
2078 return result;
2079 /* Not found, make a new one. */
2080 result = make_node (CPLUS_BINDING);
2081 TREE_CHAIN (result) = b;
2082 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2083 BINDING_SCOPE (result) = scope;
2084 BINDING_TYPE (result) = NULL_TREE;
2085 BINDING_VALUE (result) = NULL_TREE;
2086 return result;
2089 /* Return the binding value for name in scope, considering that
2090 namespace_binding may or may not be a list of CPLUS_BINDINGS. */
2092 tree
2093 namespace_binding (tree name, tree scope)
2095 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2096 if (b == NULL_TREE)
2097 return NULL_TREE;
2098 if (scope == NULL_TREE)
2099 scope = global_namespace;
2100 if (TREE_CODE (b) != CPLUS_BINDING)
2101 return (scope == global_namespace) ? b : NULL_TREE;
2102 name = find_binding (name,scope);
2103 if (name == NULL_TREE)
2104 return name;
2105 return BINDING_VALUE (name);
2108 /* Set the binding value for name in scope. If modifying the binding
2109 of global_namespace is attempted, try to optimize it. */
2111 void
2112 set_namespace_binding (tree name, tree scope, tree val)
2114 tree b;
2116 if (scope == NULL_TREE)
2117 scope = global_namespace;
2119 if (scope == global_namespace)
2121 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2122 if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2124 IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2125 return;
2128 b = binding_for_name (name, scope);
2129 BINDING_VALUE (b) = val;
2132 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2133 select a name that is unique to this compilation unit. */
2135 void
2136 push_namespace (tree name)
2138 tree d = NULL_TREE;
2139 int need_new = 1;
2140 int implicit_use = 0;
2141 int global = 0;
2142 if (!global_namespace)
2144 /* This must be ::. */
2145 my_friendly_assert (name == get_identifier ("::"), 377);
2146 global = 1;
2148 else if (!name)
2150 /* The name of anonymous namespace is unique for the translation
2151 unit. */
2152 if (!anonymous_namespace_name)
2153 anonymous_namespace_name = get_file_function_name ('N');
2154 name = anonymous_namespace_name;
2155 d = IDENTIFIER_NAMESPACE_VALUE (name);
2156 if (d)
2157 /* Reopening anonymous namespace. */
2158 need_new = 0;
2159 implicit_use = 1;
2161 else
2163 /* Check whether this is an extended namespace definition. */
2164 d = IDENTIFIER_NAMESPACE_VALUE (name);
2165 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2167 need_new = 0;
2168 if (DECL_NAMESPACE_ALIAS (d))
2170 error ("namespace alias `%D' not allowed here, assuming `%D'",
2171 d, DECL_NAMESPACE_ALIAS (d));
2172 d = DECL_NAMESPACE_ALIAS (d);
2177 if (need_new)
2179 /* Make a new namespace, binding the name to it. */
2180 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2181 /* The global namespace is not pushed, and the global binding
2182 level is set elsewhere. */
2183 if (!global)
2185 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2186 d = pushdecl (d);
2187 pushlevel (0);
2188 declare_namespace_level ();
2189 NAMESPACE_LEVEL (d) = current_binding_level;
2192 else
2193 resume_binding_level (NAMESPACE_LEVEL (d));
2195 if (implicit_use)
2196 do_using_directive (d);
2197 /* Enter the name space. */
2198 current_namespace = d;
2201 /* Pop from the scope of the current namespace. */
2203 void
2204 pop_namespace (void)
2206 my_friendly_assert (current_namespace != global_namespace, 20010801);
2207 current_namespace = CP_DECL_CONTEXT (current_namespace);
2208 /* The binding level is not popped, as it might be re-opened later. */
2209 suspend_binding_level ();
2212 /* Push into the scope of the namespace NS, even if it is deeply
2213 nested within another namespace. */
2215 void
2216 push_nested_namespace (tree ns)
2218 if (ns == global_namespace)
2219 push_to_top_level ();
2220 else
2222 push_nested_namespace (CP_DECL_CONTEXT (ns));
2223 push_namespace (DECL_NAME (ns));
2227 /* Pop back from the scope of the namespace NS, which was previously
2228 entered with push_nested_namespace. */
2230 void
2231 pop_nested_namespace (tree ns)
2233 while (ns != global_namespace)
2235 pop_namespace ();
2236 ns = CP_DECL_CONTEXT (ns);
2239 pop_from_top_level ();
2243 /* Subroutines for reverting temporarily to top-level for instantiation
2244 of templates and such. We actually need to clear out the class- and
2245 local-value slots of all identifiers, so that only the global values
2246 are at all visible. Simply setting current_binding_level to the global
2247 scope isn't enough, because more binding levels may be pushed. */
2248 struct saved_scope *scope_chain;
2250 static tree
2251 store_bindings (tree names, tree old_bindings)
2253 tree t;
2254 tree search_bindings = old_bindings;
2256 for (t = names; t; t = TREE_CHAIN (t))
2258 tree binding, t1, id;
2260 if (TREE_CODE (t) == TREE_LIST)
2261 id = TREE_PURPOSE (t);
2262 else
2263 id = DECL_NAME (t);
2265 if (!id
2266 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2267 we have no IDENTIFIER_BINDING if we have left the class
2268 scope, but cached the class-level declarations. */
2269 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2270 continue;
2272 for (t1 = search_bindings; t1; t1 = TREE_CHAIN (t1))
2273 if (TREE_VEC_ELT (t1, 0) == id)
2274 goto skip_it;
2276 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2277 binding = make_tree_vec (4);
2278 TREE_VEC_ELT (binding, 0) = id;
2279 TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
2280 TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
2281 TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
2282 IDENTIFIER_BINDING (id) = NULL_TREE;
2283 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2284 TREE_CHAIN (binding) = old_bindings;
2285 old_bindings = binding;
2286 skip_it:
2289 return old_bindings;
2292 void
2293 maybe_push_to_top_level (int pseudo)
2295 struct saved_scope *s;
2296 struct cp_binding_level *b;
2297 tree old_bindings;
2298 int need_pop;
2300 s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2302 b = scope_chain ? current_binding_level : 0;
2304 /* If we're in the middle of some function, save our state. */
2305 if (cfun)
2307 need_pop = 1;
2308 push_function_context_to (NULL_TREE);
2310 else
2311 need_pop = 0;
2313 old_bindings = NULL_TREE;
2314 if (scope_chain && previous_class_type)
2315 old_bindings = store_bindings (previous_class_values, old_bindings);
2317 /* Have to include global_binding_level, because class-level decls
2318 aren't listed anywhere useful. */
2319 for (; b; b = b->level_chain)
2321 tree t;
2323 /* Template IDs are inserted into the global level. If they were
2324 inserted into namespace level, finish_file wouldn't find them
2325 when doing pending instantiations. Therefore, don't stop at
2326 namespace level, but continue until :: . */
2327 if (b == global_binding_level || (pseudo && b->template_parms_p))
2328 break;
2330 old_bindings = store_bindings (b->names, old_bindings);
2331 /* We also need to check class_shadowed to save class-level type
2332 bindings, since pushclass doesn't fill in b->names. */
2333 if (b->parm_flag == 2)
2334 old_bindings = store_bindings (b->class_shadowed, old_bindings);
2336 /* Unwind type-value slots back to top level. */
2337 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2338 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2340 s->prev = scope_chain;
2341 s->old_bindings = old_bindings;
2342 s->bindings = b;
2343 s->need_pop_function_context = need_pop;
2344 s->function_decl = current_function_decl;
2345 s->last_parms = last_function_parms;
2346 s->check_access = flag_access_control;
2348 scope_chain = s;
2349 current_function_decl = NULL_TREE;
2350 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2351 current_lang_name = lang_name_cplusplus;
2352 current_namespace = global_namespace;
2355 void
2356 push_to_top_level (void)
2358 maybe_push_to_top_level (0);
2361 void
2362 pop_from_top_level (void)
2364 struct saved_scope *s = scope_chain;
2365 tree t;
2367 /* Clear out class-level bindings cache. */
2368 if (previous_class_type)
2369 invalidate_class_lookup_cache ();
2371 current_lang_base = 0;
2373 scope_chain = s->prev;
2374 for (t = s->old_bindings; t; t = TREE_CHAIN (t))
2376 tree id = TREE_VEC_ELT (t, 0);
2378 SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
2379 IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
2380 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2383 /* If we were in the middle of compiling a function, restore our
2384 state. */
2385 if (s->need_pop_function_context)
2386 pop_function_context_from (NULL_TREE);
2387 current_function_decl = s->function_decl;
2388 last_function_parms = s->last_parms;
2391 /* Push a definition of struct, union or enum tag "name".
2392 into binding_level "b". "type" should be the type node,
2393 We assume that the tag "name" is not already defined.
2395 Note that the definition may really be just a forward reference.
2396 In that case, the TYPE_SIZE will be a NULL_TREE.
2398 C++ gratuitously puts all these tags in the name space. */
2400 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2401 record the shadowed value for this binding contour. TYPE is
2402 the type that ID maps to. */
2404 static void
2405 set_identifier_type_value_with_scope (tree id,
2406 tree type,
2407 struct cp_binding_level* b)
2409 if (!b->namespace_p)
2411 /* Shadow the marker, not the real thing, so that the marker
2412 gets restored later. */
2413 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2414 b->type_shadowed
2415 = tree_cons (id, old_type_value, b->type_shadowed);
2417 else
2419 tree binding = binding_for_name (id, current_namespace);
2420 BINDING_TYPE (binding) = type;
2421 /* Store marker instead of real type. */
2422 type = global_type_node;
2424 SET_IDENTIFIER_TYPE_VALUE (id, type);
2427 /* As set_identifier_type_value_with_scope, but using current_binding_level. */
2429 void
2430 set_identifier_type_value (tree id, tree type)
2432 set_identifier_type_value_with_scope (id, type, current_binding_level);
2435 /* Return the type associated with id. */
2437 tree
2438 identifier_type_value (tree id)
2440 /* There is no type with that name, anywhere. */
2441 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2442 return NULL_TREE;
2443 /* This is not the type marker, but the real thing. */
2444 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2445 return REAL_IDENTIFIER_TYPE_VALUE (id);
2446 /* Have to search for it. It must be on the global level, now.
2447 Ask lookup_name not to return non-types. */
2448 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2449 if (id)
2450 return TREE_TYPE (id);
2451 return NULL_TREE;
2454 /* Pop off extraneous binding levels left over due to syntax errors.
2456 We don't pop past namespaces, as they might be valid. */
2458 void
2459 pop_everything (void)
2461 #ifdef DEBUG_BINDING_LEVELS
2462 fprintf (stderr, "XXX entering pop_everything ()\n");
2463 #endif
2464 while (!toplevel_bindings_p ())
2466 if (current_binding_level->parm_flag == 2)
2467 pop_nested_class ();
2468 else
2469 poplevel (0, 0, 0);
2471 #ifdef DEBUG_BINDING_LEVELS
2472 fprintf (stderr, "XXX leaving pop_everything ()\n");
2473 #endif
2476 /* The type TYPE is being declared. If it is a class template, or a
2477 specialization of a class template, do any processing required and
2478 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
2479 being declared a friend. B is the binding level at which this TYPE
2480 should be bound.
2482 Returns the TYPE_DECL for TYPE, which may have been altered by this
2483 processing. */
2485 static tree
2486 maybe_process_template_type_declaration (tree type,
2487 int globalize,
2488 struct cp_binding_level* b)
2490 tree decl = TYPE_NAME (type);
2492 if (processing_template_parmlist)
2493 /* You can't declare a new template type in a template parameter
2494 list. But, you can declare a non-template type:
2496 template <class A*> struct S;
2498 is a forward-declaration of `A'. */
2500 else
2502 maybe_check_template_type (type);
2504 my_friendly_assert (IS_AGGR_TYPE (type)
2505 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2508 if (processing_template_decl)
2510 /* This may change after the call to
2511 push_template_decl_real, but we want the original value. */
2512 tree name = DECL_NAME (decl);
2514 decl = push_template_decl_real (decl, globalize);
2515 /* If the current binding level is the binding level for the
2516 template parameters (see the comment in
2517 begin_template_parm_list) and the enclosing level is a class
2518 scope, and we're not looking at a friend, push the
2519 declaration of the member class into the class scope. In the
2520 friend case, push_template_decl will already have put the
2521 friend into global scope, if appropriate. */
2522 if (TREE_CODE (type) != ENUMERAL_TYPE
2523 && !globalize && b->template_parms_p
2524 && b->level_chain->parm_flag == 2)
2526 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2527 /* Put this tag on the list of tags for the class, since
2528 that won't happen below because B is not the class
2529 binding level, but is instead the pseudo-global level. */
2530 b->level_chain->tags =
2531 tree_cons (name, type, b->level_chain->tags);
2532 if (!COMPLETE_TYPE_P (current_class_type))
2534 maybe_add_class_template_decl_list (current_class_type,
2535 type, /*friend_p=*/0);
2536 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2542 return decl;
2545 /* In C++, you don't have to write `struct S' to refer to `S'; you
2546 can just use `S'. We accomplish this by creating a TYPE_DECL as
2547 if the user had written `typedef struct S S'. Create and return
2548 the TYPE_DECL for TYPE. */
2550 tree
2551 create_implicit_typedef (tree name, tree type)
2553 tree decl;
2555 decl = build_decl (TYPE_DECL, name, type);
2556 DECL_ARTIFICIAL (decl) = 1;
2557 /* There are other implicit type declarations, like the one *within*
2558 a class that allows you to write `S::S'. We must distinguish
2559 amongst these. */
2560 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2561 TYPE_NAME (type) = decl;
2563 return decl;
2566 /* Remember a local name for name-mangling purposes. */
2568 static void
2569 push_local_name (tree decl)
2571 size_t i, nelts;
2572 tree t, name;
2574 if (!local_names)
2575 VARRAY_TREE_INIT (local_names, 8, "local_names");
2577 name = DECL_NAME (decl);
2579 nelts = VARRAY_ACTIVE_SIZE (local_names);
2580 for (i = 0; i < nelts; i++)
2582 t = VARRAY_TREE (local_names, i);
2583 if (DECL_NAME (t) == name)
2585 if (!DECL_LANG_SPECIFIC (decl))
2586 retrofit_lang_decl (decl);
2587 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2588 if (DECL_LANG_SPECIFIC (t))
2589 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2590 else
2591 DECL_DISCRIMINATOR (decl) = 1;
2593 VARRAY_TREE (local_names, i) = decl;
2594 return;
2598 VARRAY_PUSH_TREE (local_names, decl);
2601 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2602 Normally put it into the inner-most non-tag-transparent scope,
2603 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2604 The latter is needed for implicit declarations. */
2606 void
2607 pushtag (tree name, tree type, int globalize)
2609 register struct cp_binding_level *b;
2611 b = current_binding_level;
2612 while (b->tag_transparent
2613 || (b->parm_flag == 2
2614 && (globalize
2615 /* We may be defining a new type in the initializer
2616 of a static member variable. We allow this when
2617 not pedantic, and it is particularly useful for
2618 type punning via an anonymous union. */
2619 || COMPLETE_TYPE_P (b->this_class))))
2620 b = b->level_chain;
2622 b->tags = tree_cons (name, type, b->tags);
2624 if (name)
2626 /* Do C++ gratuitous typedefing. */
2627 if (IDENTIFIER_TYPE_VALUE (name) != type)
2629 register tree d = NULL_TREE;
2630 int in_class = 0;
2631 tree context = TYPE_CONTEXT (type);
2633 if (! context)
2635 tree cs = current_scope ();
2637 if (! globalize)
2638 context = cs;
2639 else if (cs != NULL_TREE && TYPE_P (cs))
2640 /* When declaring a friend class of a local class, we want
2641 to inject the newly named class into the scope
2642 containing the local class, not the namespace scope. */
2643 context = decl_function_context (get_type_decl (cs));
2645 if (!context)
2646 context = current_namespace;
2648 if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2649 || b->parm_flag == 2)
2650 in_class = 1;
2652 if (current_lang_name == lang_name_java)
2653 TYPE_FOR_JAVA (type) = 1;
2655 d = create_implicit_typedef (name, type);
2656 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2657 if (! in_class)
2658 set_identifier_type_value_with_scope (name, type, b);
2660 d = maybe_process_template_type_declaration (type,
2661 globalize, b);
2663 if (b->parm_flag == 2)
2665 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2666 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2667 class. But if it's a member template class, we
2668 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2669 is done later. */
2670 finish_member_declaration (d);
2671 else
2672 pushdecl_class_level (d);
2674 else
2675 d = pushdecl_with_scope (d, b);
2677 /* FIXME what if it gets a name from typedef? */
2678 if (ANON_AGGRNAME_P (name))
2679 DECL_IGNORED_P (d) = 1;
2681 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2683 /* If this is a local class, keep track of it. We need this
2684 information for name-mangling, and so that it is possible to find
2685 all function definitions in a translation unit in a convenient
2686 way. (It's otherwise tricky to find a member function definition
2687 it's only pointed to from within a local class.) */
2688 if (TYPE_CONTEXT (type)
2689 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2690 && !processing_template_decl)
2691 VARRAY_PUSH_TREE (local_classes, type);
2693 if (b->parm_flag == 2)
2695 if (!COMPLETE_TYPE_P (current_class_type))
2697 maybe_add_class_template_decl_list (current_class_type,
2698 type, /*friend_p=*/0);
2699 CLASSTYPE_TAGS (current_class_type) = b->tags;
2704 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2705 /* Use the canonical TYPE_DECL for this node. */
2706 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2707 else
2709 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2710 will be the tagged type we just added to the current
2711 binding level. This fake NULL-named TYPE_DECL node helps
2712 dwarfout.c to know when it needs to output a
2713 representation of a tagged type, and it also gives us a
2714 convenient place to record the "scope start" address for
2715 the tagged type. */
2717 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2718 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2722 /* Counter used to create anonymous type names. */
2724 static GTY(()) int anon_cnt;
2726 /* Return an IDENTIFIER which can be used as a name for
2727 anonymous structs and unions. */
2729 tree
2730 make_anon_name (void)
2732 char buf[32];
2734 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2735 return get_identifier (buf);
2738 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2739 This keeps dbxout from getting confused. */
2741 void
2742 clear_anon_tags (void)
2744 register struct cp_binding_level *b;
2745 register tree tags;
2746 static int last_cnt = 0;
2748 /* Fast out if no new anon names were declared. */
2749 if (last_cnt == anon_cnt)
2750 return;
2752 b = current_binding_level;
2753 while (b->tag_transparent)
2754 b = b->level_chain;
2755 tags = b->tags;
2756 while (tags)
2758 /* A NULL purpose means we have already processed all tags
2759 from here to the end of the list. */
2760 if (TREE_PURPOSE (tags) == NULL_TREE)
2761 break;
2762 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2763 TREE_PURPOSE (tags) = NULL_TREE;
2764 tags = TREE_CHAIN (tags);
2766 last_cnt = anon_cnt;
2769 /* Subroutine of duplicate_decls: return truthvalue of whether
2770 or not types of these decls match.
2772 For C++, we must compare the parameter list so that `int' can match
2773 `int&' in a parameter position, but `int&' is not confused with
2774 `const int&'. */
2777 decls_match (tree newdecl, tree olddecl)
2779 int types_match;
2781 if (newdecl == olddecl)
2782 return 1;
2784 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2785 /* If the two DECLs are not even the same kind of thing, we're not
2786 interested in their types. */
2787 return 0;
2789 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2791 tree f1 = TREE_TYPE (newdecl);
2792 tree f2 = TREE_TYPE (olddecl);
2793 tree p1 = TYPE_ARG_TYPES (f1);
2794 tree p2 = TYPE_ARG_TYPES (f2);
2796 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2797 && ! (DECL_EXTERN_C_P (newdecl)
2798 && DECL_EXTERN_C_P (olddecl)))
2799 return 0;
2801 if (TREE_CODE (f1) != TREE_CODE (f2))
2802 return 0;
2804 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2806 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2807 && (DECL_BUILT_IN (olddecl)
2808 #ifndef NO_IMPLICIT_EXTERN_C
2809 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2810 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2811 #endif
2814 types_match = self_promoting_args_p (p1);
2815 if (p1 == void_list_node)
2816 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2818 #ifndef NO_IMPLICIT_EXTERN_C
2819 else if (p1 == NULL_TREE
2820 && (DECL_EXTERN_C_P (olddecl)
2821 && DECL_IN_SYSTEM_HEADER (olddecl)
2822 && !DECL_CLASS_SCOPE_P (olddecl))
2823 && (DECL_EXTERN_C_P (newdecl)
2824 && DECL_IN_SYSTEM_HEADER (newdecl)
2825 && !DECL_CLASS_SCOPE_P (newdecl)))
2827 types_match = self_promoting_args_p (p2);
2828 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2830 #endif
2831 else
2832 types_match = compparms (p1, p2);
2834 else
2835 types_match = 0;
2837 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2839 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2840 DECL_TEMPLATE_PARMS (olddecl)))
2841 return 0;
2843 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2844 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2845 return 0;
2847 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2848 types_match = 1;
2849 else
2850 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2851 DECL_TEMPLATE_RESULT (newdecl));
2853 else
2855 if (TREE_TYPE (newdecl) == error_mark_node)
2856 types_match = TREE_TYPE (olddecl) == error_mark_node;
2857 else if (TREE_TYPE (olddecl) == NULL_TREE)
2858 types_match = TREE_TYPE (newdecl) == NULL_TREE;
2859 else if (TREE_TYPE (newdecl) == NULL_TREE)
2860 types_match = 0;
2861 else
2862 types_match = comptypes (TREE_TYPE (newdecl),
2863 TREE_TYPE (olddecl),
2864 COMPARE_REDECLARATION);
2867 return types_match;
2870 /* If NEWDECL is `static' and an `extern' was seen previously,
2871 warn about it. OLDDECL is the previous declaration.
2873 Note that this does not apply to the C++ case of declaring
2874 a variable `extern const' and then later `const'.
2876 Don't complain about built-in functions, since they are beyond
2877 the user's control. */
2879 static void
2880 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2882 static const char *const explicit_extern_static_warning
2883 = "`%D' was declared `extern' and later `static'";
2884 static const char *const implicit_extern_static_warning
2885 = "`%D' was declared implicitly `extern' and later `static'";
2887 tree name;
2889 if (TREE_CODE (newdecl) == TYPE_DECL
2890 || TREE_CODE (newdecl) == TEMPLATE_DECL
2891 || TREE_CODE (newdecl) == CONST_DECL)
2892 return;
2894 /* Don't get confused by static member functions; that's a different
2895 use of `static'. */
2896 if (TREE_CODE (newdecl) == FUNCTION_DECL
2897 && DECL_STATIC_FUNCTION_P (newdecl))
2898 return;
2900 /* If the old declaration was `static', or the new one isn't, then
2901 then everything is OK. */
2902 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2903 return;
2905 /* It's OK to declare a builtin function as `static'. */
2906 if (TREE_CODE (olddecl) == FUNCTION_DECL
2907 && DECL_ARTIFICIAL (olddecl))
2908 return;
2910 name = DECL_ASSEMBLER_NAME (newdecl);
2911 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2912 ? implicit_extern_static_warning
2913 : explicit_extern_static_warning, newdecl);
2914 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2917 /* Handle when a new declaration NEWDECL has the same name as an old
2918 one OLDDECL in the same binding contour. Prints an error message
2919 if appropriate.
2921 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2922 Otherwise, return 0. */
2925 duplicate_decls (tree newdecl, tree olddecl)
2927 unsigned olddecl_uid = DECL_UID (olddecl);
2928 int olddecl_friend = 0, types_match = 0;
2929 int new_defines_function = 0;
2931 if (newdecl == olddecl)
2932 return 1;
2934 types_match = decls_match (newdecl, olddecl);
2936 /* If either the type of the new decl or the type of the old decl is an
2937 error_mark_node, then that implies that we have already issued an
2938 error (earlier) for some bogus type specification, and in that case,
2939 it is rather pointless to harass the user with yet more error message
2940 about the same declaration, so just pretend the types match here. */
2941 if (TREE_TYPE (newdecl) == error_mark_node
2942 || TREE_TYPE (olddecl) == error_mark_node)
2943 types_match = 1;
2945 if (DECL_P (olddecl)
2946 && TREE_CODE (newdecl) == FUNCTION_DECL
2947 && TREE_CODE (olddecl) == FUNCTION_DECL
2948 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2950 if (DECL_DECLARED_INLINE_P (newdecl)
2951 && DECL_UNINLINABLE (newdecl)
2952 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2953 /* Already warned elsewhere. */;
2954 else if (DECL_DECLARED_INLINE_P (olddecl)
2955 && DECL_UNINLINABLE (olddecl)
2956 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2957 /* Already warned. */;
2958 else if (DECL_DECLARED_INLINE_P (newdecl)
2959 && DECL_UNINLINABLE (olddecl)
2960 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2962 warning_with_decl (newdecl,
2963 "function `%s' redeclared as inline");
2964 warning_with_decl (olddecl,
2965 "previous declaration of function `%s' with attribute noinline");
2967 else if (DECL_DECLARED_INLINE_P (olddecl)
2968 && DECL_UNINLINABLE (newdecl)
2969 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2971 warning_with_decl (newdecl,
2972 "function `%s' redeclared with attribute noinline");
2973 warning_with_decl (olddecl,
2974 "previous declaration of function `%s' was inline");
2978 /* Check for redeclaration and other discrepancies. */
2979 if (TREE_CODE (olddecl) == FUNCTION_DECL
2980 && DECL_ARTIFICIAL (olddecl))
2982 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2984 /* Avoid warnings redeclaring anticipated built-ins. */
2985 if (DECL_ANTICIPATED (olddecl))
2986 return 0;
2988 /* If you declare a built-in or predefined function name as static,
2989 the old definition is overridden, but optionally warn this was a
2990 bad choice of name. */
2991 if (! TREE_PUBLIC (newdecl))
2993 if (warn_shadow)
2994 warning ("shadowing %s function `%#D'",
2995 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2996 olddecl);
2997 /* Discard the old built-in function. */
2998 return 0;
3000 /* If the built-in is not ansi, then programs can override
3001 it even globally without an error. */
3002 else if (! DECL_BUILT_IN (olddecl))
3003 warning ("library function `%#D' redeclared as non-function `%#D'",
3004 olddecl, newdecl);
3005 else
3007 error ("declaration of `%#D'", newdecl);
3008 error ("conflicts with built-in declaration `%#D'",
3009 olddecl);
3011 return 0;
3013 else if (!types_match)
3015 /* Avoid warnings redeclaring anticipated built-ins. */
3016 if (DECL_ANTICIPATED (olddecl))
3017 ; /* Do nothing yet. */
3018 else if ((DECL_EXTERN_C_P (newdecl)
3019 && DECL_EXTERN_C_P (olddecl))
3020 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3021 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3023 /* A near match; override the builtin. */
3025 if (TREE_PUBLIC (newdecl))
3027 warning ("new declaration `%#D'", newdecl);
3028 warning ("ambiguates built-in declaration `%#D'",
3029 olddecl);
3031 else if (warn_shadow)
3032 warning ("shadowing %s function `%#D'",
3033 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3034 olddecl);
3036 else
3037 /* Discard the old built-in function. */
3038 return 0;
3040 /* Replace the old RTL to avoid problems with inlining. */
3041 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3044 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
3046 /* If a builtin function is redeclared as `static', merge
3047 the declarations, but make the original one static. */
3048 DECL_THIS_STATIC (olddecl) = 1;
3049 TREE_PUBLIC (olddecl) = 0;
3051 /* Make the old declaration consistent with the new one so
3052 that all remnants of the builtin-ness of this function
3053 will be banished. */
3054 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3055 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3058 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3060 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3061 && TREE_CODE (newdecl) != TYPE_DECL
3062 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3063 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3064 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3065 && TREE_CODE (olddecl) != TYPE_DECL
3066 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3067 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3068 == TYPE_DECL))))
3070 /* We do nothing special here, because C++ does such nasty
3071 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3072 get shadowed, and know that if we need to find a TYPE_DECL
3073 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3074 slot of the identifier. */
3075 return 0;
3078 if ((TREE_CODE (newdecl) == FUNCTION_DECL
3079 && DECL_FUNCTION_TEMPLATE_P (olddecl))
3080 || (TREE_CODE (olddecl) == FUNCTION_DECL
3081 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3082 return 0;
3084 error ("`%#D' redeclared as different kind of symbol", newdecl);
3085 if (TREE_CODE (olddecl) == TREE_LIST)
3086 olddecl = TREE_VALUE (olddecl);
3087 cp_error_at ("previous declaration of `%#D'", olddecl);
3089 /* New decl is completely inconsistent with the old one =>
3090 tell caller to replace the old one. */
3092 return 0;
3094 else if (!types_match)
3096 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3097 /* These are certainly not duplicate declarations; they're
3098 from different scopes. */
3099 return 0;
3101 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3103 /* The name of a class template may not be declared to refer to
3104 any other template, class, function, object, namespace, value,
3105 or type in the same scope. */
3106 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3107 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3109 error ("declaration of template `%#D'", newdecl);
3110 cp_error_at ("conflicts with previous declaration `%#D'",
3111 olddecl);
3113 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3114 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3115 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3116 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3117 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3118 DECL_TEMPLATE_PARMS (olddecl))
3119 /* Template functions can be disambiguated by
3120 return type. */
3121 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3122 TREE_TYPE (TREE_TYPE (olddecl))))
3124 error ("new declaration `%#D'", newdecl);
3125 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3127 return 0;
3129 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3131 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3133 error ("declaration of C function `%#D' conflicts with",
3134 newdecl);
3135 cp_error_at ("previous declaration `%#D' here", olddecl);
3137 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3138 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3140 error ("new declaration `%#D'", newdecl);
3141 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3143 else
3144 return 0;
3147 /* Already complained about this, so don't do so again. */
3148 else if (current_class_type == NULL_TREE
3149 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3151 error ("conflicting types for `%#D'", newdecl);
3152 cp_error_at ("previous declaration as `%#D'", olddecl);
3155 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3156 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3157 && (!DECL_TEMPLATE_INFO (newdecl)
3158 || (DECL_TI_TEMPLATE (newdecl)
3159 != DECL_TI_TEMPLATE (olddecl))))
3160 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3161 && (!DECL_TEMPLATE_INFO (olddecl)
3162 || (DECL_TI_TEMPLATE (olddecl)
3163 != DECL_TI_TEMPLATE (newdecl))))))
3164 /* It's OK to have a template specialization and a non-template
3165 with the same type, or to have specializations of two
3166 different templates with the same type. Note that if one is a
3167 specialization, and the other is an instantiation of the same
3168 template, that we do not exit at this point. That situation
3169 can occur if we instantiate a template class, and then
3170 specialize one of its methods. This situation is valid, but
3171 the declarations must be merged in the usual way. */
3172 return 0;
3173 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3174 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3175 && !DECL_USE_TEMPLATE (newdecl))
3176 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3177 && !DECL_USE_TEMPLATE (olddecl))))
3178 /* One of the declarations is a template instantiation, and the
3179 other is not a template at all. That's OK. */
3180 return 0;
3181 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3182 && DECL_NAMESPACE_ALIAS (newdecl)
3183 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3184 /* Redeclaration of namespace alias, ignore it. */
3185 return 1;
3186 else
3188 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3189 if (errmsg)
3191 error (errmsg, newdecl);
3192 if (DECL_NAME (olddecl) != NULL_TREE)
3193 cp_error_at ((DECL_INITIAL (olddecl)
3194 && namespace_bindings_p ())
3195 ? "`%#D' previously defined here"
3196 : "`%#D' previously declared here", olddecl);
3197 return 0;
3199 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3200 && DECL_INITIAL (olddecl) != NULL_TREE
3201 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3202 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3204 /* Prototype decl follows defn w/o prototype. */
3205 cp_warning_at ("prototype for `%#D'", newdecl);
3206 cp_warning_at ("follows non-prototype definition here", olddecl);
3208 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3209 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3211 /* extern "C" int foo ();
3212 int foo () { bar (); }
3213 is OK. */
3214 if (current_lang_depth () == 0)
3215 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3216 else
3218 cp_error_at ("previous declaration of `%#D' with %L linkage",
3219 olddecl, DECL_LANGUAGE (olddecl));
3220 error ("conflicts with new declaration with %L linkage",
3221 DECL_LANGUAGE (newdecl));
3225 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3227 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3229 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3230 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3231 int i = 1;
3233 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3234 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3236 for (; t1 && t1 != void_list_node;
3237 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3238 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3240 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3241 TREE_PURPOSE (t2)))
3243 pedwarn ("default argument given for parameter %d of `%#D'",
3244 i, newdecl);
3245 cp_pedwarn_at ("after previous specification in `%#D'",
3246 olddecl);
3248 else
3250 error ("default argument given for parameter %d of `%#D'",
3251 i, newdecl);
3252 cp_error_at ("after previous specification in `%#D'",
3253 olddecl);
3257 if (DECL_DECLARED_INLINE_P (newdecl)
3258 && ! DECL_DECLARED_INLINE_P (olddecl)
3259 && TREE_ADDRESSABLE (olddecl) && warn_inline)
3261 warning ("`%#D' was used before it was declared inline",
3262 newdecl);
3263 cp_warning_at ("previous non-inline declaration here",
3264 olddecl);
3269 /* Do not merge an implicit typedef with an explicit one. In:
3271 class A;
3273 typedef class A A __attribute__ ((foo));
3275 the attribute should apply only to the typedef. */
3276 if (TREE_CODE (olddecl) == TYPE_DECL
3277 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3278 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3279 return 0;
3281 /* If new decl is `static' and an `extern' was seen previously,
3282 warn about it. */
3283 warn_extern_redeclared_static (newdecl, olddecl);
3285 /* We have committed to returning 1 at this point. */
3286 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3288 /* Now that functions must hold information normally held
3289 by field decls, there is extra work to do so that
3290 declaration information does not get destroyed during
3291 definition. */
3292 if (DECL_VINDEX (olddecl))
3293 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3294 if (DECL_CONTEXT (olddecl))
3295 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3296 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3297 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3298 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3299 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3300 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3301 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3302 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3303 SET_OVERLOADED_OPERATOR_CODE
3304 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3305 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3307 /* Optionally warn about more than one declaration for the same
3308 name, but don't warn about a function declaration followed by a
3309 definition. */
3310 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3311 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3312 /* Don't warn about extern decl followed by definition. */
3313 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3314 /* Don't warn about friends, let add_friend take care of it. */
3315 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3317 warning ("redundant redeclaration of `%D' in same scope", newdecl);
3318 cp_warning_at ("previous declaration of `%D'", olddecl);
3322 /* Deal with C++: must preserve virtual function table size. */
3323 if (TREE_CODE (olddecl) == TYPE_DECL)
3325 register tree newtype = TREE_TYPE (newdecl);
3326 register tree oldtype = TREE_TYPE (olddecl);
3328 if (newtype != error_mark_node && oldtype != error_mark_node
3329 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3330 CLASSTYPE_FRIEND_CLASSES (newtype)
3331 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3333 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3336 /* Copy all the DECL_... slots specified in the new decl
3337 except for any that we copy here from the old type. */
3338 DECL_ATTRIBUTES (newdecl)
3339 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3341 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3343 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3344 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3345 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3346 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3348 /* If the new declaration is a definition, update the file and
3349 line information on the declaration. */
3350 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3351 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3353 DECL_SOURCE_LOCATION (olddecl)
3354 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3355 = DECL_SOURCE_LOCATION (newdecl);
3358 return 1;
3361 if (types_match)
3363 /* Automatically handles default parameters. */
3364 tree oldtype = TREE_TYPE (olddecl);
3365 tree newtype;
3367 /* Merge the data types specified in the two decls. */
3368 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3370 /* If merge_types produces a non-typedef type, just use the old type. */
3371 if (TREE_CODE (newdecl) == TYPE_DECL
3372 && newtype == DECL_ORIGINAL_TYPE (newdecl))
3373 newtype = oldtype;
3375 if (TREE_CODE (newdecl) == VAR_DECL)
3377 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3378 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3381 /* Do this after calling `merge_types' so that default
3382 parameters don't confuse us. */
3383 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3384 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3385 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3387 TREE_TYPE (newdecl) = build_exception_variant (newtype,
3388 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3389 TREE_TYPE (olddecl) = build_exception_variant (newtype,
3390 TYPE_RAISES_EXCEPTIONS (oldtype));
3392 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3393 && DECL_SOURCE_LINE (olddecl) != 0
3394 && flag_exceptions
3395 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3396 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3398 error ("declaration of `%F' throws different exceptions",
3399 newdecl);
3400 cp_error_at ("than previous declaration `%F'", olddecl);
3403 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3405 /* Lay the type out, unless already done. */
3406 if (! same_type_p (newtype, oldtype)
3407 && TREE_TYPE (newdecl) != error_mark_node
3408 && !(processing_template_decl && uses_template_parms (newdecl)))
3409 layout_type (TREE_TYPE (newdecl));
3411 if ((TREE_CODE (newdecl) == VAR_DECL
3412 || TREE_CODE (newdecl) == PARM_DECL
3413 || TREE_CODE (newdecl) == RESULT_DECL
3414 || TREE_CODE (newdecl) == FIELD_DECL
3415 || TREE_CODE (newdecl) == TYPE_DECL)
3416 && !(processing_template_decl && uses_template_parms (newdecl)))
3417 layout_decl (newdecl, 0);
3419 /* Merge the type qualifiers. */
3420 if (TREE_READONLY (newdecl))
3421 TREE_READONLY (olddecl) = 1;
3422 if (TREE_THIS_VOLATILE (newdecl))
3423 TREE_THIS_VOLATILE (olddecl) = 1;
3425 /* Merge the initialization information. */
3426 if (DECL_INITIAL (newdecl) == NULL_TREE
3427 && DECL_INITIAL (olddecl) != NULL_TREE)
3429 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3430 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3431 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3432 && DECL_LANG_SPECIFIC (newdecl)
3433 && DECL_LANG_SPECIFIC (olddecl))
3434 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3437 /* Merge the section attribute.
3438 We want to issue an error if the sections conflict but that must be
3439 done later in decl_attributes since we are called before attributes
3440 are assigned. */
3441 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3442 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3444 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3446 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3447 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3448 DECL_NO_LIMIT_STACK (newdecl)
3449 |= DECL_NO_LIMIT_STACK (olddecl);
3450 /* Keep the old RTL. */
3451 COPY_DECL_RTL (olddecl, newdecl);
3453 else if (TREE_CODE (newdecl) == VAR_DECL
3454 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3456 /* Keep the old RTL. We cannot keep the old RTL if the old
3457 declaration was for an incomplete object and the new
3458 declaration is not since many attributes of the RTL will
3459 change. */
3460 COPY_DECL_RTL (olddecl, newdecl);
3463 /* If cannot merge, then use the new type and qualifiers,
3464 and don't preserve the old rtl. */
3465 else
3467 /* Clean out any memory we had of the old declaration. */
3468 tree oldstatic = value_member (olddecl, static_aggregates);
3469 if (oldstatic)
3470 TREE_VALUE (oldstatic) = error_mark_node;
3472 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3473 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3474 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3475 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3478 /* Merge the storage class information. */
3479 merge_weak (newdecl, olddecl);
3481 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3482 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3483 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3484 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3485 if (! DECL_EXTERNAL (olddecl))
3486 DECL_EXTERNAL (newdecl) = 0;
3488 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3490 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3491 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3492 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3493 DECL_TEMPLATE_INSTANTIATED (newdecl)
3494 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3495 /* Don't really know how much of the language-specific
3496 values we should copy from old to new. */
3497 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3498 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
3499 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3500 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3501 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3502 DECL_INITIALIZED_IN_CLASS_P (newdecl)
3503 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3504 olddecl_friend = DECL_FRIEND_P (olddecl);
3506 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3507 if (TREE_CODE (newdecl) == FUNCTION_DECL
3508 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3510 DECL_BEFRIENDING_CLASSES (newdecl)
3511 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3512 DECL_BEFRIENDING_CLASSES (olddecl));
3513 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3517 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3519 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3520 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3522 /* If newdecl is not a specialization, then it is not a
3523 template-related function at all. And that means that we
3524 shoud have exited above, returning 0. */
3525 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3528 if (TREE_USED (olddecl))
3529 /* From [temp.expl.spec]:
3531 If a template, a member template or the member of a class
3532 template is explicitly specialized then that
3533 specialization shall be declared before the first use of
3534 that specialization that would cause an implicit
3535 instantiation to take place, in every translation unit in
3536 which such a use occurs. */
3537 error ("explicit specialization of %D after first use",
3538 olddecl);
3540 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3542 /* [temp.expl.spec/14] We don't inline explicit specialization
3543 just because the primary template says so. */
3545 else
3547 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3548 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3550 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3552 /* If either decl says `inline', this fn is inline, unless
3553 its definition was passed already. */
3554 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3555 DECL_INLINE (olddecl) = 1;
3556 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3558 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3559 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3562 /* Preserve abstractness on cloned [cd]tors. */
3563 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3565 if (! types_match)
3567 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3568 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3569 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3571 if (! types_match || new_defines_function)
3573 /* These need to be copied so that the names are available.
3574 Note that if the types do match, we'll preserve inline
3575 info and other bits, but if not, we won't. */
3576 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3577 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3579 if (new_defines_function)
3580 /* If defining a function declared with other language
3581 linkage, use the previously declared language linkage. */
3582 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3583 else if (types_match)
3585 /* If redeclaring a builtin function, and not a definition,
3586 it stays built in. */
3587 if (DECL_BUILT_IN (olddecl))
3589 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3590 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3591 /* If we're keeping the built-in definition, keep the rtl,
3592 regardless of declaration matches. */
3593 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3595 else
3596 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3598 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3599 /* Don't clear out the arguments if we're redefining a function. */
3600 if (DECL_ARGUMENTS (olddecl))
3601 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3604 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3605 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3607 /* Now preserve various other info from the definition. */
3608 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3609 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3610 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3611 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3613 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3615 int function_size;
3617 function_size = sizeof (struct tree_decl);
3619 memcpy ((char *) olddecl + sizeof (struct tree_common),
3620 (char *) newdecl + sizeof (struct tree_common),
3621 function_size - sizeof (struct tree_common));
3623 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3625 /* If newdecl is a template instantiation, it is possible that
3626 the following sequence of events has occurred:
3628 o A friend function was declared in a class template. The
3629 class template was instantiated.
3631 o The instantiation of the friend declaration was
3632 recorded on the instantiation list, and is newdecl.
3634 o Later, however, instantiate_class_template called pushdecl
3635 on the newdecl to perform name injection. But, pushdecl in
3636 turn called duplicate_decls when it discovered that another
3637 declaration of a global function with the same name already
3638 existed.
3640 o Here, in duplicate_decls, we decided to clobber newdecl.
3642 If we're going to do that, we'd better make sure that
3643 olddecl, and not newdecl, is on the list of
3644 instantiations so that if we try to do the instantiation
3645 again we won't get the clobbered declaration. */
3647 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3648 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3650 for (; decls; decls = TREE_CHAIN (decls))
3651 if (TREE_VALUE (decls) == newdecl)
3652 TREE_VALUE (decls) = olddecl;
3655 else
3657 memcpy ((char *) olddecl + sizeof (struct tree_common),
3658 (char *) newdecl + sizeof (struct tree_common),
3659 sizeof (struct tree_decl) - sizeof (struct tree_common)
3660 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3663 DECL_UID (olddecl) = olddecl_uid;
3664 if (olddecl_friend)
3665 DECL_FRIEND_P (olddecl) = 1;
3667 /* NEWDECL contains the merged attribute lists.
3668 Update OLDDECL to be the same. */
3669 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3671 return 1;
3674 /* Record a decl-node X as belonging to the current lexical scope.
3675 Check for errors (such as an incompatible declaration for the same
3676 name already seen in the same scope).
3678 Returns either X or an old decl for the same name.
3679 If an old decl is returned, it may have been smashed
3680 to agree with what X says. */
3682 tree
3683 pushdecl (tree x)
3685 register tree t;
3686 register tree name;
3687 int need_new_binding;
3689 /* We shouldn't be calling pushdecl when we're generating RTL for a
3690 function that we already did semantic analysis on previously. */
3691 my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3692 19990913);
3694 need_new_binding = 1;
3696 if (DECL_TEMPLATE_PARM_P (x))
3697 /* Template parameters have no context; they are not X::T even
3698 when declared within a class or namespace. */
3700 else
3702 if (current_function_decl && x != current_function_decl
3703 /* A local declaration for a function doesn't constitute
3704 nesting. */
3705 && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3706 /* A local declaration for an `extern' variable is in the
3707 scope of the current namespace, not the current
3708 function. */
3709 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3710 && !DECL_CONTEXT (x))
3711 DECL_CONTEXT (x) = current_function_decl;
3713 /* If this is the declaration for a namespace-scope function,
3714 but the declaration itself is in a local scope, mark the
3715 declaration. */
3716 if (TREE_CODE (x) == FUNCTION_DECL
3717 && DECL_NAMESPACE_SCOPE_P (x)
3718 && current_function_decl
3719 && x != current_function_decl)
3720 DECL_LOCAL_FUNCTION_P (x) = 1;
3723 name = DECL_NAME (x);
3724 if (name)
3726 int different_binding_level = 0;
3728 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3729 name = TREE_OPERAND (name, 0);
3731 /* In case this decl was explicitly namespace-qualified, look it
3732 up in its namespace context. */
3733 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3734 && namespace_bindings_p ())
3735 t = namespace_binding (name, DECL_CONTEXT (x));
3736 else
3737 t = lookup_name_current_level (name);
3739 /* [basic.link] If there is a visible declaration of an entity
3740 with linkage having the same name and type, ignoring entities
3741 declared outside the innermost enclosing namespace scope, the
3742 block scope declaration declares that same entity and
3743 receives the linkage of the previous declaration. */
3744 if (! t && current_function_decl && x != current_function_decl
3745 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3746 && DECL_EXTERNAL (x))
3748 /* Look in block scope. */
3749 t = IDENTIFIER_VALUE (name);
3750 /* Or in the innermost namespace. */
3751 if (! t)
3752 t = namespace_binding (name, DECL_CONTEXT (x));
3753 /* Does it have linkage? Note that if this isn't a DECL, it's an
3754 OVERLOAD, which is OK. */
3755 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3756 t = NULL_TREE;
3757 if (t)
3758 different_binding_level = 1;
3761 /* If we are declaring a function, and the result of name-lookup
3762 was an OVERLOAD, look for an overloaded instance that is
3763 actually the same as the function we are declaring. (If
3764 there is one, we have to merge our declaration with the
3765 previous declaration.) */
3766 if (t && TREE_CODE (t) == OVERLOAD)
3768 tree match;
3770 if (TREE_CODE (x) == FUNCTION_DECL)
3771 for (match = t; match; match = OVL_NEXT (match))
3773 if (decls_match (OVL_CURRENT (match), x))
3774 break;
3776 else
3777 /* Just choose one. */
3778 match = t;
3780 if (match)
3781 t = OVL_CURRENT (match);
3782 else
3783 t = NULL_TREE;
3786 if (t == error_mark_node)
3788 /* error_mark_node is 0 for a while during initialization! */
3789 t = NULL_TREE;
3790 cp_error_at ("`%#D' used prior to declaration", x);
3792 else if (t != NULL_TREE)
3794 if (different_binding_level)
3796 if (decls_match (x, t))
3797 /* The standard only says that the local extern
3798 inherits linkage from the previous decl; in
3799 particular, default args are not shared. It would
3800 be nice to propagate inlining info, though. FIXME. */
3801 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3803 else if (TREE_CODE (t) == PARM_DECL)
3805 if (DECL_CONTEXT (t) == NULL_TREE)
3806 /* This is probaby caused by too many errors, but calling
3807 abort will say that if errors have occurred. */
3808 abort ();
3810 /* Check for duplicate params. */
3811 if (duplicate_decls (x, t))
3812 return t;
3814 else if ((DECL_EXTERN_C_FUNCTION_P (x)
3815 || DECL_FUNCTION_TEMPLATE_P (x))
3816 && is_overloaded_fn (t))
3817 /* Don't do anything just yet. */;
3818 else if (t == wchar_decl_node)
3820 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3821 pedwarn ("redeclaration of `wchar_t' as `%T'",
3822 TREE_TYPE (x));
3824 /* Throw away the redeclaration. */
3825 return t;
3827 else if (TREE_CODE (t) != TREE_CODE (x))
3829 if (duplicate_decls (x, t))
3830 return t;
3832 else if (duplicate_decls (x, t))
3834 if (TREE_CODE (t) == TYPE_DECL)
3835 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3836 else if (TREE_CODE (t) == FUNCTION_DECL)
3837 check_default_args (t);
3839 return t;
3841 else if (DECL_MAIN_P (x))
3843 /* A redeclaration of main, but not a duplicate of the
3844 previous one.
3846 [basic.start.main]
3848 This function shall not be overloaded. */
3849 cp_error_at ("invalid redeclaration of `%D'", t);
3850 error ("as `%D'", x);
3851 /* We don't try to push this declaration since that
3852 causes a crash. */
3853 return x;
3857 check_template_shadow (x);
3859 /* If this is a function conjured up by the backend, massage it
3860 so it looks friendly. */
3861 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3863 retrofit_lang_decl (x);
3864 SET_DECL_LANGUAGE (x, lang_c);
3867 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3869 t = push_overloaded_decl (x, PUSH_LOCAL);
3870 if (t != x)
3871 return t;
3872 if (!namespace_bindings_p ())
3873 /* We do not need to create a binding for this name;
3874 push_overloaded_decl will have already done so if
3875 necessary. */
3876 need_new_binding = 0;
3878 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3880 t = push_overloaded_decl (x, PUSH_GLOBAL);
3881 if (t == x)
3882 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3883 return t;
3886 /* If declaring a type as a typedef, copy the type (unless we're
3887 at line 0), and install this TYPE_DECL as the new type's typedef
3888 name. See the extensive comment in ../c-decl.c (pushdecl). */
3889 if (TREE_CODE (x) == TYPE_DECL)
3891 tree type = TREE_TYPE (x);
3892 if (DECL_SOURCE_LINE (x) == 0)
3894 if (TYPE_NAME (type) == 0)
3895 TYPE_NAME (type) = x;
3897 else if (type != error_mark_node && TYPE_NAME (type) != x
3898 /* We don't want to copy the type when all we're
3899 doing is making a TYPE_DECL for the purposes of
3900 inlining. */
3901 && (!TYPE_NAME (type)
3902 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3904 DECL_ORIGINAL_TYPE (x) = type;
3905 type = build_type_copy (type);
3906 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3907 TYPE_NAME (type) = x;
3908 TREE_TYPE (x) = type;
3911 if (type != error_mark_node
3912 && TYPE_NAME (type)
3913 && TYPE_IDENTIFIER (type))
3914 set_identifier_type_value_with_scope (DECL_NAME (x), type,
3915 current_binding_level);
3919 /* Multiple external decls of the same identifier ought to match.
3921 We get warnings about inline functions where they are defined.
3922 We get warnings about other functions from push_overloaded_decl.
3924 Avoid duplicate warnings where they are used. */
3925 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3927 tree decl;
3929 decl = IDENTIFIER_NAMESPACE_VALUE (name);
3930 if (decl && TREE_CODE (decl) == OVERLOAD)
3931 decl = OVL_FUNCTION (decl);
3933 if (decl && decl != error_mark_node
3934 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3935 /* If different sort of thing, we already gave an error. */
3936 && TREE_CODE (decl) == TREE_CODE (x)
3937 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3939 pedwarn ("type mismatch with previous external decl", x);
3940 cp_pedwarn_at ("previous external decl of `%#D'", decl);
3944 /* This name is new in its binding level.
3945 Install the new declaration and return it. */
3946 if (namespace_bindings_p ())
3948 /* Install a global value. */
3950 /* If the first global decl has external linkage,
3951 warn if we later see static one. */
3952 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3953 TREE_PUBLIC (name) = 1;
3955 /* Bind the name for the entity. */
3956 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3957 && t != NULL_TREE)
3958 && (TREE_CODE (x) == TYPE_DECL
3959 || TREE_CODE (x) == VAR_DECL
3960 || TREE_CODE (x) == ALIAS_DECL
3961 || TREE_CODE (x) == NAMESPACE_DECL
3962 || TREE_CODE (x) == CONST_DECL
3963 || TREE_CODE (x) == TEMPLATE_DECL))
3964 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3966 /* Don't forget if the function was used via an implicit decl. */
3967 if (IDENTIFIER_IMPLICIT_DECL (name)
3968 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3969 TREE_USED (x) = 1;
3971 /* Don't forget if its address was taken in that way. */
3972 if (IDENTIFIER_IMPLICIT_DECL (name)
3973 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3974 TREE_ADDRESSABLE (x) = 1;
3976 /* Warn about mismatches against previous implicit decl. */
3977 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3978 /* If this real decl matches the implicit, don't complain. */
3979 && ! (TREE_CODE (x) == FUNCTION_DECL
3980 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3981 warning
3982 ("`%D' was previously implicitly declared to return `int'", x);
3984 /* If new decl is `static' and an `extern' was seen previously,
3985 warn about it. */
3986 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3987 warn_extern_redeclared_static (x, t);
3989 else
3991 /* Here to install a non-global value. */
3992 tree oldlocal = IDENTIFIER_VALUE (name);
3993 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3995 if (need_new_binding)
3997 push_local_binding (name, x, 0);
3998 /* Because push_local_binding will hook X on to the
3999 current_binding_level's name list, we don't want to
4000 do that again below. */
4001 need_new_binding = 0;
4004 /* If this is a TYPE_DECL, push it into the type value slot. */
4005 if (TREE_CODE (x) == TYPE_DECL)
4006 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
4007 current_binding_level);
4009 /* Clear out any TYPE_DECL shadowed by a namespace so that
4010 we won't think this is a type. The C struct hack doesn't
4011 go through namespaces. */
4012 if (TREE_CODE (x) == NAMESPACE_DECL)
4013 set_identifier_type_value_with_scope (name, NULL_TREE,
4014 current_binding_level);
4016 if (oldlocal)
4018 tree d = oldlocal;
4020 while (oldlocal
4021 && TREE_CODE (oldlocal) == VAR_DECL
4022 && DECL_DEAD_FOR_LOCAL (oldlocal))
4023 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
4025 if (oldlocal == NULL_TREE)
4026 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
4029 /* If this is an extern function declaration, see if we
4030 have a global definition or declaration for the function. */
4031 if (oldlocal == NULL_TREE
4032 && DECL_EXTERNAL (x)
4033 && oldglobal != NULL_TREE
4034 && TREE_CODE (x) == FUNCTION_DECL
4035 && TREE_CODE (oldglobal) == FUNCTION_DECL)
4037 /* We have one. Their types must agree. */
4038 if (decls_match (x, oldglobal))
4039 /* OK */;
4040 else
4042 warning ("extern declaration of `%#D' doesn't match", x);
4043 cp_warning_at ("global declaration `%#D'", oldglobal);
4046 /* If we have a local external declaration,
4047 and no file-scope declaration has yet been seen,
4048 then if we later have a file-scope decl it must not be static. */
4049 if (oldlocal == NULL_TREE
4050 && oldglobal == NULL_TREE
4051 && DECL_EXTERNAL (x)
4052 && TREE_PUBLIC (x))
4053 TREE_PUBLIC (name) = 1;
4055 /* Warn if shadowing an argument at the top level of the body. */
4056 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
4057 /* Inline decls shadow nothing. */
4058 && !DECL_FROM_INLINE (x)
4059 && TREE_CODE (oldlocal) == PARM_DECL)
4061 bool err = false;
4063 /* Don't complain if it's from an enclosing function. */
4064 if (DECL_CONTEXT (oldlocal) == current_function_decl
4065 && TREE_CODE (x) != PARM_DECL)
4067 /* Go to where the parms should be and see if we find
4068 them there. */
4069 struct cp_binding_level *b = current_binding_level->level_chain;
4071 /* Skip the ctor/dtor cleanup level. */
4072 b = b->level_chain;
4074 /* ARM $8.3 */
4075 if (b->parm_flag == 1)
4077 error ("declaration of `%#D' shadows a parameter",
4078 name);
4079 err = true;
4083 if (warn_shadow && !err)
4084 shadow_warning ("a parameter", name, oldlocal);
4087 /* Maybe warn if shadowing something else. */
4088 else if (warn_shadow && !DECL_EXTERNAL (x)
4089 /* No shadow warnings for internally generated vars. */
4090 && ! DECL_ARTIFICIAL (x)
4091 /* No shadow warnings for vars made for inlining. */
4092 && ! DECL_FROM_INLINE (x))
4094 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4095 && current_class_ptr
4096 && !TREE_STATIC (name))
4097 warning ("declaration of `%s' shadows a member of `this'",
4098 IDENTIFIER_POINTER (name));
4099 else if (oldlocal != NULL_TREE
4100 && TREE_CODE (oldlocal) == VAR_DECL)
4101 shadow_warning ("a previous local", name, oldlocal);
4102 else if (oldglobal != NULL_TREE
4103 && TREE_CODE (oldglobal) == VAR_DECL)
4104 /* XXX shadow warnings in outer-more namespaces */
4105 shadow_warning ("a global declaration", name, oldglobal);
4109 if (TREE_CODE (x) == FUNCTION_DECL)
4110 check_default_args (x);
4112 if (TREE_CODE (x) == VAR_DECL)
4113 maybe_register_incomplete_var (x);
4116 if (need_new_binding)
4117 add_decl_to_level (x,
4118 DECL_NAMESPACE_SCOPE_P (x)
4119 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4120 : current_binding_level);
4122 return x;
4125 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4126 caller to set DECL_CONTEXT properly. */
4128 static tree
4129 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4131 register struct cp_binding_level *b;
4132 tree function_decl = current_function_decl;
4134 current_function_decl = NULL_TREE;
4135 if (level->parm_flag == 2)
4137 b = class_binding_level;
4138 class_binding_level = level;
4139 pushdecl_class_level (x);
4140 class_binding_level = b;
4142 else
4144 b = current_binding_level;
4145 current_binding_level = level;
4146 x = pushdecl (x);
4147 current_binding_level = b;
4149 current_function_decl = function_decl;
4150 return x;
4153 /* Like pushdecl, only it places X in the current namespace,
4154 if appropriate. */
4156 tree
4157 pushdecl_namespace_level (tree x)
4159 register struct cp_binding_level *b = current_binding_level;
4160 register tree t;
4162 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4164 /* Now, the type_shadowed stack may screw us. Munge it so it does
4165 what we want. */
4166 if (TREE_CODE (x) == TYPE_DECL)
4168 tree name = DECL_NAME (x);
4169 tree newval;
4170 tree *ptr = (tree *)0;
4171 for (; b != global_binding_level; b = b->level_chain)
4173 tree shadowed = b->type_shadowed;
4174 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4175 if (TREE_PURPOSE (shadowed) == name)
4177 ptr = &TREE_VALUE (shadowed);
4178 /* Can't break out of the loop here because sometimes
4179 a binding level will have duplicate bindings for
4180 PT names. It's gross, but I haven't time to fix it. */
4183 newval = TREE_TYPE (x);
4184 if (ptr == (tree *)0)
4186 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4187 up here if this is changed to an assertion. --KR */
4188 SET_IDENTIFIER_TYPE_VALUE (name, newval);
4190 else
4192 *ptr = newval;
4195 return t;
4198 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4199 if appropriate. */
4201 tree
4202 pushdecl_top_level (tree x)
4204 push_to_top_level ();
4205 x = pushdecl_namespace_level (x);
4206 pop_from_top_level ();
4207 return x;
4210 /* Make the declaration of X appear in CLASS scope. */
4212 void
4213 pushdecl_class_level (tree x)
4215 tree name;
4217 /* Get the name of X. */
4218 if (TREE_CODE (x) == OVERLOAD)
4219 name = DECL_NAME (get_first_fn (x));
4220 else
4221 name = DECL_NAME (x);
4223 if (name)
4225 push_class_level_binding (name, x);
4226 if (TREE_CODE (x) == TYPE_DECL)
4227 set_identifier_type_value (name, TREE_TYPE (x));
4229 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4231 /* If X is an anonymous aggregate, all of its members are
4232 treated as if they were members of the class containing the
4233 aggregate, for naming purposes. */
4234 tree f;
4236 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4237 pushdecl_class_level (f);
4241 /* Enter DECL into the symbol table, if that's appropriate. Returns
4242 DECL, or a modified version thereof. */
4244 tree
4245 maybe_push_decl (tree decl)
4247 tree type = TREE_TYPE (decl);
4249 /* Add this decl to the current binding level, but not if it comes
4250 from another scope, e.g. a static member variable. TEM may equal
4251 DECL or it may be a previous decl of the same name. */
4252 if (decl == error_mark_node
4253 || (TREE_CODE (decl) != PARM_DECL
4254 && DECL_CONTEXT (decl) != NULL_TREE
4255 /* Definitions of namespace members outside their namespace are
4256 possible. */
4257 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4258 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4259 || TREE_CODE (type) == UNKNOWN_TYPE
4260 /* The declaration of a template specialization does not affect
4261 the functions available for overload resolution, so we do not
4262 call pushdecl. */
4263 || (TREE_CODE (decl) == FUNCTION_DECL
4264 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4265 return decl;
4266 else
4267 return pushdecl (decl);
4270 /* Make the declaration(s) of X appear in CLASS scope
4271 under the name NAME. */
4273 void
4274 push_class_level_binding (tree name, tree x)
4276 tree binding;
4277 /* The class_binding_level will be NULL if x is a template
4278 parameter name in a member template. */
4279 if (!class_binding_level)
4280 return;
4282 /* Make sure that this new member does not have the same name
4283 as a template parameter. */
4284 if (TYPE_BEING_DEFINED (current_class_type))
4285 check_template_shadow (x);
4287 /* If this declaration shadows a declaration from an enclosing
4288 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4289 we leave this class. Record the shadowed declaration here. */
4290 binding = IDENTIFIER_BINDING (name);
4291 if (binding
4292 && ((TREE_CODE (x) == OVERLOAD
4293 && BINDING_VALUE (binding)
4294 && is_overloaded_fn (BINDING_VALUE (binding)))
4295 || INHERITED_VALUE_BINDING_P (binding)))
4297 tree shadow;
4298 tree old_decl;
4300 /* If the old binding was from a base class, and was for a tag
4301 name, slide it over to make room for the new binding. The
4302 old binding is still visible if explicitly qualified with a
4303 class-key. */
4304 if (INHERITED_VALUE_BINDING_P (binding)
4305 && BINDING_VALUE (binding)
4306 && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4307 && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4308 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4310 old_decl = BINDING_TYPE (binding);
4311 BINDING_TYPE (binding) = BINDING_VALUE (binding);
4312 BINDING_VALUE (binding) = NULL_TREE;
4313 INHERITED_VALUE_BINDING_P (binding) = 0;
4315 else
4316 old_decl = BINDING_VALUE (binding);
4318 /* Find the previous binding of name on the class-shadowed
4319 list, and update it. */
4320 for (shadow = class_binding_level->class_shadowed;
4321 shadow;
4322 shadow = TREE_CHAIN (shadow))
4323 if (TREE_PURPOSE (shadow) == name
4324 && TREE_TYPE (shadow) == old_decl)
4326 BINDING_VALUE (binding) = x;
4327 INHERITED_VALUE_BINDING_P (binding) = 0;
4328 TREE_TYPE (shadow) = x;
4329 IDENTIFIER_CLASS_VALUE (name) = x;
4330 return;
4334 /* If we didn't replace an existing binding, put the binding on the
4335 stack of bindings for the identifier, and update the shadowed list. */
4336 if (push_class_binding (name, x))
4338 class_binding_level->class_shadowed
4339 = tree_cons (name, NULL,
4340 class_binding_level->class_shadowed);
4341 /* Record the value we are binding NAME to so that we can know
4342 what to pop later. */
4343 TREE_TYPE (class_binding_level->class_shadowed) = x;
4347 /* Insert another USING_DECL into the current binding level, returning
4348 this declaration. If this is a redeclaration, do nothing, and
4349 return NULL_TREE if this not in namespace scope (in namespace
4350 scope, a using decl might extend any previous bindings). */
4352 tree
4353 push_using_decl (tree scope, tree name)
4355 tree decl;
4357 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4358 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4359 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4360 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4361 break;
4362 if (decl)
4363 return namespace_bindings_p () ? decl : NULL_TREE;
4364 decl = build_lang_decl (USING_DECL, name, void_type_node);
4365 DECL_INITIAL (decl) = scope;
4366 TREE_CHAIN (decl) = current_binding_level->usings;
4367 current_binding_level->usings = decl;
4368 return decl;
4371 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4372 changed (i.e. there was already a directive), or the fresh
4373 TREE_LIST otherwise. */
4375 tree
4376 push_using_directive (tree used)
4378 tree ud = current_binding_level->using_directives;
4379 tree iter, ancestor;
4381 /* Check if we already have this. */
4382 if (purpose_member (used, ud) != NULL_TREE)
4383 return NULL_TREE;
4385 /* Recursively add all namespaces used. */
4386 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4387 push_using_directive (TREE_PURPOSE (iter));
4389 ancestor = namespace_ancestor (current_decl_namespace (), used);
4390 ud = current_binding_level->using_directives;
4391 ud = tree_cons (used, ancestor, ud);
4392 current_binding_level->using_directives = ud;
4393 return ud;
4396 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4397 other definitions already in place. We get around this by making
4398 the value of the identifier point to a list of all the things that
4399 want to be referenced by that name. It is then up to the users of
4400 that name to decide what to do with that list.
4402 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4403 DECL_TEMPLATE_RESULT. It is dealt with the same way.
4405 FLAGS is a bitwise-or of the following values:
4406 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4407 namespace scope.
4408 PUSH_USING: DECL is being pushed as the result of a using
4409 declaration.
4411 The value returned may be a previous declaration if we guessed wrong
4412 about what language DECL should belong to (C or C++). Otherwise,
4413 it's always DECL (and never something that's not a _DECL). */
4415 tree
4416 push_overloaded_decl (tree decl, int flags)
4418 tree name = DECL_NAME (decl);
4419 tree old;
4420 tree new_binding;
4421 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4423 if (doing_global)
4424 old = namespace_binding (name, DECL_CONTEXT (decl));
4425 else
4426 old = lookup_name_current_level (name);
4428 if (old)
4430 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4432 tree t = TREE_TYPE (old);
4433 if (IS_AGGR_TYPE (t) && warn_shadow
4434 && (! DECL_IN_SYSTEM_HEADER (decl)
4435 || ! DECL_IN_SYSTEM_HEADER (old)))
4436 warning ("`%#D' hides constructor for `%#T'", decl, t);
4437 old = NULL_TREE;
4439 else if (is_overloaded_fn (old))
4441 tree tmp;
4443 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4445 tree fn = OVL_CURRENT (tmp);
4447 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4448 && !(flags & PUSH_USING)
4449 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4450 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4451 error ("`%#D' conflicts with previous using declaration `%#D'",
4452 decl, fn);
4454 if (duplicate_decls (decl, fn))
4455 return fn;
4458 else if (old == error_mark_node)
4459 /* Ignore the undefined symbol marker. */
4460 old = NULL_TREE;
4461 else
4463 cp_error_at ("previous non-function declaration `%#D'", old);
4464 error ("conflicts with function declaration `%#D'", decl);
4465 return decl;
4469 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4471 if (old && TREE_CODE (old) != OVERLOAD)
4472 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4473 else
4474 new_binding = ovl_cons (decl, old);
4475 if (flags & PUSH_USING)
4476 OVL_USED (new_binding) = 1;
4478 else
4479 /* NAME is not ambiguous. */
4480 new_binding = decl;
4482 if (doing_global)
4483 set_namespace_binding (name, current_namespace, new_binding);
4484 else
4486 /* We only create an OVERLOAD if there was a previous binding at
4487 this level, or if decl is a template. In the former case, we
4488 need to remove the old binding and replace it with the new
4489 binding. We must also run through the NAMES on the binding
4490 level where the name was bound to update the chain. */
4492 if (TREE_CODE (new_binding) == OVERLOAD && old)
4494 tree *d;
4496 for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4498 d = &TREE_CHAIN (*d))
4499 if (*d == old
4500 || (TREE_CODE (*d) == TREE_LIST
4501 && TREE_VALUE (*d) == old))
4503 if (TREE_CODE (*d) == TREE_LIST)
4504 /* Just replace the old binding with the new. */
4505 TREE_VALUE (*d) = new_binding;
4506 else
4507 /* Build a TREE_LIST to wrap the OVERLOAD. */
4508 *d = tree_cons (NULL_TREE, new_binding,
4509 TREE_CHAIN (*d));
4511 /* And update the CPLUS_BINDING node. */
4512 BINDING_VALUE (IDENTIFIER_BINDING (name))
4513 = new_binding;
4514 return decl;
4517 /* We should always find a previous binding in this case. */
4518 abort ();
4521 /* Install the new binding. */
4522 push_local_binding (name, new_binding, flags);
4525 return decl;
4528 /* Generate an implicit declaration for identifier FUNCTIONID
4529 as a function of type int (). Print a warning if appropriate. */
4531 tree
4532 implicitly_declare (tree functionid)
4534 register tree decl;
4536 /* We used to reuse an old implicit decl here,
4537 but this loses with inline functions because it can clobber
4538 the saved decl chains. */
4539 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4541 DECL_EXTERNAL (decl) = 1;
4542 TREE_PUBLIC (decl) = 1;
4544 /* ISO standard says implicit declarations are in the innermost block.
4545 So we record the decl in the standard fashion. */
4546 pushdecl (decl);
4547 rest_of_decl_compilation (decl, NULL, 0, 0);
4549 if (warn_implicit
4550 /* Only one warning per identifier. */
4551 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4553 pedwarn ("implicit declaration of function `%#D'", decl);
4556 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4558 return decl;
4561 /* Return zero if the declaration NEWDECL is valid
4562 when the declaration OLDDECL (assumed to be for the same name)
4563 has already been seen.
4564 Otherwise return an error message format string with a %s
4565 where the identifier should go. */
4567 static const char *
4568 redeclaration_error_message (tree newdecl, tree olddecl)
4570 if (TREE_CODE (newdecl) == TYPE_DECL)
4572 /* Because C++ can put things into name space for free,
4573 constructs like "typedef struct foo { ... } foo"
4574 would look like an erroneous redeclaration. */
4575 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4576 return 0;
4577 else
4578 return "redefinition of `%#D'";
4580 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4582 /* If this is a pure function, its olddecl will actually be
4583 the original initialization to `0' (which we force to call
4584 abort()). Don't complain about redefinition in this case. */
4585 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4586 return 0;
4588 /* If both functions come from different namespaces, this is not
4589 a redeclaration - this is a conflict with a used function. */
4590 if (DECL_NAMESPACE_SCOPE_P (olddecl)
4591 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4592 return "`%D' conflicts with used function";
4594 /* We'll complain about linkage mismatches in
4595 warn_extern_redeclared_static. */
4597 /* Defining the same name twice is no good. */
4598 if (DECL_INITIAL (olddecl) != NULL_TREE
4599 && DECL_INITIAL (newdecl) != NULL_TREE)
4601 if (DECL_NAME (olddecl) == NULL_TREE)
4602 return "`%#D' not declared in class";
4603 else
4604 return "redefinition of `%#D'";
4606 return 0;
4608 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4610 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4611 && (DECL_TEMPLATE_RESULT (newdecl)
4612 != DECL_TEMPLATE_RESULT (olddecl))
4613 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4614 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4615 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4616 && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4617 && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4618 return "redefinition of `%#D'";
4619 return 0;
4621 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4623 /* Objects declared at top level: */
4624 /* If at least one is a reference, it's ok. */
4625 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4626 return 0;
4627 /* Reject two definitions. */
4628 return "redefinition of `%#D'";
4630 else
4632 /* Objects declared with block scope: */
4633 /* Reject two definitions, and reject a definition
4634 together with an external reference. */
4635 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4636 return "redeclaration of `%#D'";
4637 return 0;
4641 /* Create a new label, named ID. */
4643 static tree
4644 make_label_decl (tree id, int local_p)
4646 tree decl;
4648 decl = build_decl (LABEL_DECL, id, void_type_node);
4649 if (expanding_p)
4650 /* Make sure every label has an rtx. */
4651 label_rtx (decl);
4653 DECL_CONTEXT (decl) = current_function_decl;
4654 DECL_MODE (decl) = VOIDmode;
4655 C_DECLARED_LABEL_FLAG (decl) = local_p;
4657 /* Say where one reference is to the label, for the sake of the
4658 error if it is not defined. */
4659 DECL_SOURCE_LINE (decl) = lineno;
4660 DECL_SOURCE_FILE (decl) = input_filename;
4662 /* Record the fact that this identifier is bound to this label. */
4663 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4665 return decl;
4668 /* Record this label on the list of used labels so that we can check
4669 at the end of the function to see whether or not the label was
4670 actually defined, and so we can check when the label is defined whether
4671 this use is valid. */
4673 static void
4674 use_label (tree decl)
4676 if (named_label_uses == NULL
4677 || named_label_uses->names_in_scope != current_binding_level->names
4678 || named_label_uses->label_decl != decl)
4680 struct named_label_use_list *new_ent;
4681 new_ent = ((struct named_label_use_list *)
4682 ggc_alloc (sizeof (struct named_label_use_list)));
4683 new_ent->label_decl = decl;
4684 new_ent->names_in_scope = current_binding_level->names;
4685 new_ent->binding_level = current_binding_level;
4686 new_ent->lineno_o_goto = lineno;
4687 new_ent->filename_o_goto = input_filename;
4688 new_ent->next = named_label_uses;
4689 named_label_uses = new_ent;
4693 /* Look for a label named ID in the current function. If one cannot
4694 be found, create one. (We keep track of used, but undefined,
4695 labels, and complain about them at the end of a function.) */
4697 tree
4698 lookup_label (tree id)
4700 tree decl;
4701 struct named_label_list *ent;
4703 /* You can't use labels at global scope. */
4704 if (current_function_decl == NULL_TREE)
4706 error ("label `%s' referenced outside of any function",
4707 IDENTIFIER_POINTER (id));
4708 return NULL_TREE;
4711 /* See if we've already got this label. */
4712 decl = IDENTIFIER_LABEL_VALUE (id);
4713 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4714 return decl;
4716 /* Record this label on the list of labels used in this function.
4717 We do this before calling make_label_decl so that we get the
4718 IDENTIFIER_LABEL_VALUE before the new label is declared. */
4719 ent = ((struct named_label_list *)
4720 ggc_alloc_cleared (sizeof (struct named_label_list)));
4721 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4722 ent->next = named_labels;
4723 named_labels = ent;
4725 /* We need a new label. */
4726 decl = make_label_decl (id, /*local_p=*/0);
4728 /* Now fill in the information we didn't have before. */
4729 ent->label_decl = decl;
4731 return decl;
4734 /* Declare a local label named ID. */
4736 tree
4737 declare_local_label (tree id)
4739 tree decl;
4741 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4742 this scope we can restore the old value of
4743 IDENTIFIER_TYPE_VALUE. */
4744 current_binding_level->shadowed_labels
4745 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4746 current_binding_level->shadowed_labels);
4747 /* Look for the label. */
4748 decl = make_label_decl (id, /*local_p=*/1);
4749 /* Now fill in the information we didn't have before. */
4750 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4752 return decl;
4755 /* Returns nonzero if it is ill-formed to jump past the declaration of
4756 DECL. Returns 2 if it's also a real problem. */
4758 static int
4759 decl_jump_unsafe (tree decl)
4761 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4762 return 0;
4764 if (DECL_INITIAL (decl) == NULL_TREE
4765 && pod_type_p (TREE_TYPE (decl)))
4766 return 0;
4768 /* This is really only important if we're crossing an initialization.
4769 The POD stuff is just pedantry; why should it matter if the class
4770 contains a field of pointer to member type? */
4771 if (DECL_INITIAL (decl)
4772 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4773 return 2;
4774 return 1;
4777 /* Check that a single previously seen jump to a newly defined label
4778 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4779 the jump context; NAMES are the names in scope in LEVEL at the jump
4780 context; FILE and LINE are the source position of the jump or 0. */
4782 static void
4783 check_previous_goto_1 (tree decl,
4784 struct cp_binding_level* level,
4785 tree names,
4786 const char* file,
4787 int line)
4789 int identified = 0;
4790 int saw_eh = 0;
4791 struct cp_binding_level *b = current_binding_level;
4792 for (; b; b = b->level_chain)
4794 tree new_decls = b->names;
4795 tree old_decls = (b == level ? names : NULL_TREE);
4796 for (; new_decls != old_decls;
4797 new_decls = TREE_CHAIN (new_decls))
4799 int problem = decl_jump_unsafe (new_decls);
4800 if (! problem)
4801 continue;
4803 if (! identified)
4805 if (decl)
4806 pedwarn ("jump to label `%D'", decl);
4807 else
4808 pedwarn ("jump to case label");
4810 if (file)
4811 pedwarn_with_file_and_line (file, line, " from here");
4812 identified = 1;
4815 if (problem > 1)
4816 cp_error_at (" crosses initialization of `%#D'",
4817 new_decls);
4818 else
4819 cp_pedwarn_at (" enters scope of non-POD `%#D'",
4820 new_decls);
4823 if (b == level)
4824 break;
4825 if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
4827 if (! identified)
4829 if (decl)
4830 pedwarn ("jump to label `%D'", decl);
4831 else
4832 pedwarn ("jump to case label");
4834 if (file)
4835 pedwarn_with_file_and_line (file, line, " from here");
4836 identified = 1;
4838 if (b->is_try_scope)
4839 error (" enters try block");
4840 else
4841 error (" enters catch block");
4842 saw_eh = 1;
4847 static void
4848 check_previous_goto (struct named_label_use_list* use)
4850 check_previous_goto_1 (use->label_decl, use->binding_level,
4851 use->names_in_scope, use->filename_o_goto,
4852 use->lineno_o_goto);
4855 static void
4856 check_switch_goto (struct cp_binding_level* level)
4858 check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0);
4861 /* Check that any previously seen jumps to a newly defined label DECL
4862 are OK. Called by define_label. */
4864 static void
4865 check_previous_gotos (tree decl)
4867 struct named_label_use_list **usep;
4869 if (! TREE_USED (decl))
4870 return;
4872 for (usep = &named_label_uses; *usep; )
4874 struct named_label_use_list *use = *usep;
4875 if (use->label_decl == decl)
4877 check_previous_goto (use);
4878 *usep = use->next;
4880 else
4881 usep = &(use->next);
4885 /* Check that a new jump to a label DECL is OK. Called by
4886 finish_goto_stmt. */
4888 void
4889 check_goto (tree decl)
4891 int identified = 0;
4892 tree bad;
4893 struct named_label_list *lab;
4895 /* We can't know where a computed goto is jumping. So we assume
4896 that it's OK. */
4897 if (! DECL_P (decl))
4898 return;
4900 /* If the label hasn't been defined yet, defer checking. */
4901 if (! DECL_INITIAL (decl))
4903 use_label (decl);
4904 return;
4907 for (lab = named_labels; lab; lab = lab->next)
4908 if (decl == lab->label_decl)
4909 break;
4911 /* If the label is not on named_labels it's a gcc local label, so
4912 it must be in an outer scope, so jumping to it is always OK. */
4913 if (lab == 0)
4914 return;
4916 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4917 && !identified)
4919 cp_pedwarn_at ("jump to label `%D'", decl);
4920 pedwarn (" from here");
4921 identified = 1;
4924 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4926 tree b = TREE_VALUE (bad);
4927 int u = decl_jump_unsafe (b);
4929 if (u > 1 && DECL_ARTIFICIAL (b))
4930 /* Can't skip init of __exception_info. */
4931 cp_error_at (" enters catch block", b);
4932 else if (u > 1)
4933 cp_error_at (" skips initialization of `%#D'", b);
4934 else
4935 cp_pedwarn_at (" enters scope of non-POD `%#D'", b);
4938 if (lab->in_try_scope)
4939 error (" enters try block");
4940 else if (lab->in_catch_scope)
4941 error (" enters catch block");
4944 /* Define a label, specifying the location in the source file.
4945 Return the LABEL_DECL node for the label, if the definition is valid.
4946 Otherwise return 0. */
4948 tree
4949 define_label (const char* filename, int line, tree name)
4951 tree decl = lookup_label (name);
4952 struct named_label_list *ent;
4953 register struct cp_binding_level *p;
4955 for (ent = named_labels; ent; ent = ent->next)
4956 if (ent->label_decl == decl)
4957 break;
4959 /* After labels, make any new cleanups in the function go into their
4960 own new (temporary) binding contour. */
4961 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
4962 p->more_cleanups_ok = 0;
4964 if (name == get_identifier ("wchar_t"))
4965 pedwarn ("label named wchar_t");
4967 if (DECL_INITIAL (decl) != NULL_TREE)
4969 error ("duplicate label `%D'", decl);
4970 return 0;
4972 else
4974 /* Mark label as having been defined. */
4975 DECL_INITIAL (decl) = error_mark_node;
4976 /* Say where in the source. */
4977 DECL_SOURCE_FILE (decl) = filename;
4978 DECL_SOURCE_LINE (decl) = line;
4979 if (ent)
4981 ent->names_in_scope = current_binding_level->names;
4982 ent->binding_level = current_binding_level;
4984 check_previous_gotos (decl);
4985 return decl;
4989 struct cp_switch
4991 struct cp_binding_level *level;
4992 struct cp_switch *next;
4993 /* The SWITCH_STMT being built. */
4994 tree switch_stmt;
4995 /* A splay-tree mapping the low element of a case range to the high
4996 element, or NULL_TREE if there is no high element. Used to
4997 determine whether or not a new case label duplicates an old case
4998 label. We need a tree, rather than simply a hash table, because
4999 of the GNU case range extension. */
5000 splay_tree cases;
5003 /* A stack of the currently active switch statements. The innermost
5004 switch statement is on the top of the stack. There is no need to
5005 mark the stack for garbage collection because it is only active
5006 during the processing of the body of a function, and we never
5007 collect at that point. */
5009 static struct cp_switch *switch_stack;
5011 /* Called right after a switch-statement condition is parsed.
5012 SWITCH_STMT is the switch statement being parsed. */
5014 void
5015 push_switch (tree switch_stmt)
5017 struct cp_switch *p
5018 = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
5019 p->level = current_binding_level;
5020 p->next = switch_stack;
5021 p->switch_stmt = switch_stmt;
5022 p->cases = splay_tree_new (case_compare, NULL, NULL);
5023 switch_stack = p;
5026 void
5027 pop_switch (void)
5029 struct cp_switch *cs;
5031 cs = switch_stack;
5032 splay_tree_delete (cs->cases);
5033 switch_stack = switch_stack->next;
5034 free (cs);
5037 /* Note that we've seen a definition of a case label, and complain if this
5038 is a bad place for one. */
5040 tree
5041 finish_case_label (tree low_value, tree high_value)
5043 tree cond, r;
5044 register struct cp_binding_level *p;
5046 if (! switch_stack)
5048 if (high_value)
5049 error ("case label not within a switch statement");
5050 else if (low_value)
5051 error ("case label `%E' not within a switch statement",
5052 low_value);
5053 else
5054 error ("`default' label not within a switch statement");
5055 return NULL_TREE;
5058 if (processing_template_decl)
5060 tree label;
5062 /* For templates, just add the case label; we'll do semantic
5063 analysis at instantiation-time. */
5064 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5065 return add_stmt (build_case_label (low_value, high_value, label));
5068 /* Find the condition on which this switch statement depends. */
5069 cond = SWITCH_COND (switch_stack->switch_stmt);
5070 if (cond && TREE_CODE (cond) == TREE_LIST)
5071 cond = TREE_VALUE (cond);
5073 r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5075 check_switch_goto (switch_stack->level);
5077 /* After labels, make any new cleanups in the function go into their
5078 own new (temporary) binding contour. */
5079 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5080 p->more_cleanups_ok = 0;
5082 return r;
5085 /* Return the list of declarations of the current level.
5086 Note that this list is in reverse order unless/until
5087 you nreverse it; and when you do nreverse it, you must
5088 store the result back using `storedecls' or you will lose. */
5090 tree
5091 getdecls (void)
5093 return current_binding_level->names;
5096 /* Return the list of type-tags (for structs, etc) of the current level. */
5098 tree
5099 gettags (void)
5101 return current_binding_level->tags;
5104 /* Store the list of declarations of the current level.
5105 This is done for the parameter declarations of a function being defined,
5106 after they are modified in the light of any missing parameters. */
5108 static void
5109 storedecls (tree decls)
5111 current_binding_level->names = decls;
5114 /* Similarly, store the list of tags of the current level. */
5116 void
5117 storetags (tree tags)
5119 current_binding_level->tags = tags;
5122 /* Return the type that should be used when TYPE's name is preceded
5123 by a tag such as 'struct' or 'union', or null if the name cannot
5124 be used in this way.
5126 For example, when processing the third line of:
5128 struct A;
5129 typedef struct A A;
5130 struct A;
5132 lookup of A will find the typedef. Given A's typedef, this function
5133 will return the type associated with "struct A". For the tag to be
5134 anything other than TYPE, TYPE must be a typedef whose original type
5135 has the same name and context as TYPE itself.
5137 It is not valid for a typedef of an anonymous type to be used with
5138 an explicit tag:
5140 typedef struct { ... } B;
5141 struct B;
5143 Return null for this case. */
5145 static tree
5146 follow_tag_typedef (tree type)
5148 tree original;
5150 original = original_type (type);
5151 if (! TYPE_NAME (original))
5152 return NULL_TREE;
5153 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5154 && (CP_DECL_CONTEXT (TYPE_NAME (original))
5155 == CP_DECL_CONTEXT (TYPE_NAME (type)))
5156 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5157 return original;
5158 else
5159 return NULL_TREE;
5162 /* Given NAME, an IDENTIFIER_NODE,
5163 return the structure (or union or enum) definition for that name.
5164 Searches binding levels from BINDING_LEVEL up to the global level.
5165 If THISLEVEL_ONLY is nonzero, searches only the specified context
5166 (but skips any tag-transparent contexts to find one that is
5167 meaningful for tags).
5168 FORM says which kind of type the caller wants;
5169 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5170 If the wrong kind of type is found, and it's not a template, an error is
5171 reported. */
5173 static tree
5174 lookup_tag (enum tree_code form,
5175 tree name,
5176 struct cp_binding_level* binding_level,
5177 int thislevel_only)
5179 register struct cp_binding_level *level;
5180 /* Nonzero if, we should look past a template parameter level, even
5181 if THISLEVEL_ONLY. */
5182 int allow_template_parms_p = 1;
5184 for (level = binding_level; level; level = level->level_chain)
5186 register tree tail;
5187 if (ANON_AGGRNAME_P (name))
5188 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5190 /* There's no need for error checking here, because
5191 anon names are unique throughout the compilation. */
5192 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5193 return TREE_VALUE (tail);
5195 else if (level->namespace_p)
5196 /* Do namespace lookup. */
5197 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5199 tree old = binding_for_name (name, tail);
5201 /* If we just skipped past a template parameter level,
5202 even though THISLEVEL_ONLY, and we find a template
5203 class declaration, then we use the _TYPE node for the
5204 template. See the example below. */
5205 if (thislevel_only && !allow_template_parms_p
5206 && old && BINDING_VALUE (old)
5207 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
5208 old = TREE_TYPE (BINDING_VALUE (old));
5209 else
5210 old = BINDING_TYPE (old);
5212 if (old)
5214 /* We've found something at this binding level. If it is
5215 a typedef, extract the tag it refers to. Lookup fails
5216 if the typedef doesn't refer to a taggable type. */
5217 old = follow_tag_typedef (old);
5218 if (!old)
5219 return NULL_TREE;
5220 if (TREE_CODE (old) != form
5221 && (form == ENUMERAL_TYPE
5222 || TREE_CODE (old) == ENUMERAL_TYPE))
5224 error ("`%#D' redeclared as %C", old, form);
5225 return NULL_TREE;
5227 return old;
5229 if (thislevel_only || tail == global_namespace)
5230 return NULL_TREE;
5232 else
5233 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5235 if (TREE_PURPOSE (tail) == name)
5237 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5239 if (code != form
5240 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5242 /* Definition isn't the kind we were looking for. */
5243 error ("`%#D' redeclared as %C", TREE_VALUE (tail), form);
5244 return NULL_TREE;
5246 return TREE_VALUE (tail);
5249 if (thislevel_only && ! level->tag_transparent)
5251 if (level->template_parms_p && allow_template_parms_p)
5253 /* We must deal with cases like this:
5255 template <class T> struct S;
5256 template <class T> struct S {};
5258 When looking up `S', for the second declaration, we
5259 would like to find the first declaration. But, we
5260 are in the pseudo-global level created for the
5261 template parameters, rather than the (surrounding)
5262 namespace level. Thus, we keep going one more level,
5263 even though THISLEVEL_ONLY is nonzero. */
5264 allow_template_parms_p = 0;
5265 continue;
5267 else
5268 return NULL_TREE;
5271 return NULL_TREE;
5274 /* Given a type, find the tag that was defined for it and return the tag name.
5275 Otherwise return 0. However, the value can never be 0
5276 in the cases in which this is used.
5278 C++: If NAME is nonzero, this is the new name to install. This is
5279 done when replacing anonymous tags with real tag names. */
5281 static tree
5282 lookup_tag_reverse (tree type, tree name)
5284 register struct cp_binding_level *level;
5286 for (level = current_binding_level; level; level = level->level_chain)
5288 register tree tail;
5289 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5291 if (TREE_VALUE (tail) == type)
5293 if (name)
5294 TREE_PURPOSE (tail) = name;
5295 return TREE_PURPOSE (tail);
5299 return NULL_TREE;
5302 /* Look up NAME in the NAMESPACE. */
5304 tree
5305 lookup_namespace_name (tree namespace, tree name)
5307 tree val;
5308 tree template_id = NULL_TREE;
5310 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5312 if (TREE_CODE (name) == NAMESPACE_DECL)
5313 /* This happens for A::B<int> when B is a namespace. */
5314 return name;
5315 else if (TREE_CODE (name) == TEMPLATE_DECL)
5317 /* This happens for A::B where B is a template, and there are no
5318 template arguments. */
5319 error ("invalid use of `%D'", name);
5320 return error_mark_node;
5323 namespace = ORIGINAL_NAMESPACE (namespace);
5325 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5327 template_id = name;
5328 name = TREE_OPERAND (name, 0);
5329 if (TREE_CODE (name) == OVERLOAD)
5330 name = DECL_NAME (OVL_CURRENT (name));
5331 else if (DECL_P (name))
5332 name = DECL_NAME (name);
5335 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5337 val = make_node (CPLUS_BINDING);
5338 if (!qualified_lookup_using_namespace (name, namespace, val, 0))
5339 return error_mark_node;
5341 if (BINDING_VALUE (val))
5343 val = BINDING_VALUE (val);
5345 if (template_id)
5347 if (DECL_CLASS_TEMPLATE_P (val))
5348 val = lookup_template_class (val,
5349 TREE_OPERAND (template_id, 1),
5350 /*in_decl=*/NULL_TREE,
5351 /*context=*/NULL_TREE,
5352 /*entering_scope=*/0,
5353 tf_error | tf_warning);
5354 else if (DECL_FUNCTION_TEMPLATE_P (val)
5355 || TREE_CODE (val) == OVERLOAD)
5356 val = lookup_template_function (val,
5357 TREE_OPERAND (template_id, 1));
5358 else
5360 error ("`%D::%D' is not a template",
5361 namespace, name);
5362 return error_mark_node;
5366 /* If we have a single function from a using decl, pull it out. */
5367 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5368 val = OVL_FUNCTION (val);
5370 /* Ignore built-in functions that haven't been prototyped yet. */
5371 if (!val || !DECL_P(val)
5372 || !DECL_LANG_SPECIFIC(val)
5373 || !DECL_ANTICIPATED (val))
5374 return val;
5377 error ("`%D' undeclared in namespace `%D'", name, namespace);
5378 return error_mark_node;
5381 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5383 static hashval_t
5384 typename_hash (const void* k)
5386 hashval_t hash;
5387 tree t = (tree) k;
5389 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5390 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5392 return hash;
5395 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5397 static int
5398 typename_compare (const void * k1, const void * k2)
5400 tree t1;
5401 tree t2;
5402 tree d1;
5403 tree d2;
5405 t1 = (tree) k1;
5406 t2 = (tree) k2;
5407 d1 = TYPE_NAME (t1);
5408 d2 = TYPE_NAME (t2);
5410 return (DECL_NAME (d1) == DECL_NAME (d2)
5411 && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5412 && ((TREE_TYPE (t1) != NULL_TREE)
5413 == (TREE_TYPE (t2) != NULL_TREE))
5414 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5415 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5418 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5419 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5420 is non-NULL, this type is being created by the implicit typename
5421 extension, and BASE_TYPE is a type named `t' in some base class of
5422 `T' which depends on template parameters.
5424 Returns the new TYPENAME_TYPE. */
5426 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5428 tree
5429 build_typename_type (tree context, tree name, tree fullname)
5431 tree t;
5432 tree d;
5433 PTR *e;
5435 if (typename_htab == NULL)
5437 typename_htab = htab_create_ggc (61, &typename_hash,
5438 &typename_compare, NULL);
5441 /* Build the TYPENAME_TYPE. */
5442 t = make_aggr_type (TYPENAME_TYPE);
5443 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5444 TYPENAME_TYPE_FULLNAME (t) = fullname;
5446 /* Build the corresponding TYPE_DECL. */
5447 d = build_decl (TYPE_DECL, name, t);
5448 TYPE_NAME (TREE_TYPE (d)) = d;
5449 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5450 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5451 DECL_ARTIFICIAL (d) = 1;
5453 /* See if we already have this type. */
5454 e = htab_find_slot (typename_htab, t, INSERT);
5455 if (*e)
5456 t = (tree) *e;
5457 else
5458 *e = t;
5460 return t;
5463 /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type,
5464 unless an error occurs, in which case error_mark_node is returned.
5465 If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5466 set, we return that, rather than the _TYPE it corresponds to, in
5467 other cases we look through the type decl. If TF_ERROR is set,
5468 complain about errors, otherwise be quiet. */
5470 tree
5471 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5473 tree fullname;
5475 if (TYPE_P (name))
5477 if (!(TYPE_LANG_SPECIFIC (name)
5478 && (CLASSTYPE_IS_TEMPLATE (name)
5479 || CLASSTYPE_USE_TEMPLATE (name))))
5480 name = TYPE_IDENTIFIER (name);
5481 else
5482 /* Create a TEMPLATE_ID_EXPR for the type. */
5483 name = build_nt (TEMPLATE_ID_EXPR,
5484 CLASSTYPE_TI_TEMPLATE (name),
5485 CLASSTYPE_TI_ARGS (name));
5487 else if (TREE_CODE (name) == TYPE_DECL)
5488 name = DECL_NAME (name);
5490 fullname = name;
5492 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5494 name = TREE_OPERAND (name, 0);
5495 if (TREE_CODE (name) == TEMPLATE_DECL)
5496 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5498 if (TREE_CODE (name) == TEMPLATE_DECL)
5500 error ("`%D' used without template parameters", name);
5501 return error_mark_node;
5503 if (TREE_CODE (name) != IDENTIFIER_NODE)
5504 abort ();
5506 if (TREE_CODE (context) == NAMESPACE_DECL)
5508 /* We can get here from typename_sub0 in the explicit_template_type
5509 expansion. Just fail. */
5510 if (complain & tf_error)
5511 error ("no class template named `%#T' in `%#T'",
5512 name, context);
5513 return error_mark_node;
5516 if (! uses_template_parms (context)
5517 || currently_open_class (context))
5519 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5521 tree tmpl = NULL_TREE;
5522 if (IS_AGGR_TYPE (context))
5523 tmpl = lookup_field (context, name, 0, 0);
5524 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5526 if (complain & tf_error)
5527 error ("no class template named `%#T' in `%#T'",
5528 name, context);
5529 return error_mark_node;
5532 if (complain & tf_error)
5534 if (complain & tf_parsing)
5535 perform_or_defer_access_check (context, tmpl);
5536 else
5537 enforce_access (context, tmpl);
5540 return lookup_template_class (tmpl,
5541 TREE_OPERAND (fullname, 1),
5542 NULL_TREE, context,
5543 /*entering_scope=*/0,
5544 tf_error | tf_warning);
5546 else
5548 tree t;
5550 if (!IS_AGGR_TYPE (context))
5552 if (complain & tf_error)
5553 error ("no type named `%#T' in `%#T'", name, context);
5554 return error_mark_node;
5557 t = lookup_field (context, name, 0, 1);
5558 if (t)
5560 if (TREE_CODE (t) != TYPE_DECL)
5562 if (complain & tf_error)
5563 error ("no type named `%#T' in `%#T'", name, context);
5564 return error_mark_node;
5567 if (complain & tf_error)
5569 if (complain & tf_parsing)
5570 perform_or_defer_access_check (context, t);
5571 else
5572 enforce_access (context, t);
5575 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5576 t = TREE_TYPE (t);
5578 return t;
5583 /* If the CONTEXT is not a template type, then either the field is
5584 there now or its never going to be. */
5585 if (!uses_template_parms (context))
5587 if (complain & tf_error)
5588 error ("no type named `%#T' in `%#T'", name, context);
5589 return error_mark_node;
5592 return build_typename_type (context, name, fullname);
5595 /* Resolve `CONTEXT::template NAME'. Returns an appropriate type,
5596 unless an error occurs, in which case error_mark_node is returned.
5597 If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5598 corresponds to. If COMPLAIN zero, don't complain about any errors
5599 that occur. */
5601 tree
5602 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5604 tree t;
5605 tree d;
5607 if (TYPE_P (name))
5608 name = TYPE_IDENTIFIER (name);
5609 else if (DECL_P (name))
5610 name = DECL_NAME (name);
5611 if (TREE_CODE (name) != IDENTIFIER_NODE)
5612 abort ();
5614 if (!uses_template_parms (context)
5615 || currently_open_class (context))
5617 tree tmpl = NULL_TREE;
5619 if (IS_AGGR_TYPE (context))
5620 tmpl = lookup_field (context, name, 0, 0);
5622 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5624 if (complain & tf_error)
5625 error ("no class template named `%#T' in `%#T'", name, context);
5626 return error_mark_node;
5629 if (complain & tf_error)
5631 if (complain & tf_parsing)
5632 perform_or_defer_access_check (context, tmpl);
5633 else
5634 enforce_access (context, tmpl);
5637 return tmpl;
5640 /* Build the UNBOUND_CLASS_TEMPLATE. */
5641 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5642 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5643 TREE_TYPE (t) = NULL_TREE;
5645 /* Build the corresponding TEMPLATE_DECL. */
5646 d = build_decl (TEMPLATE_DECL, name, t);
5647 TYPE_NAME (TREE_TYPE (d)) = d;
5648 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5649 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5650 DECL_ARTIFICIAL (d) = 1;
5652 return t;
5655 /* Select the right _DECL from multiple choices. */
5657 static tree
5658 select_decl (tree binding, int flags)
5660 tree val;
5661 val = BINDING_VALUE (binding);
5663 if (LOOKUP_NAMESPACES_ONLY (flags))
5665 /* We are not interested in types. */
5666 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5667 return val;
5668 return NULL_TREE;
5671 /* If we could have a type and
5672 we have nothing or we need a type and have none. */
5673 if (BINDING_TYPE (binding)
5674 && (!val || ((flags & LOOKUP_PREFER_TYPES)
5675 && TREE_CODE (val) != TYPE_DECL)))
5676 val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5677 /* Don't return non-types if we really prefer types. */
5678 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
5679 && (TREE_CODE (val) != TEMPLATE_DECL
5680 || !DECL_CLASS_TEMPLATE_P (val)))
5681 val = NULL_TREE;
5683 return val;
5686 /* Unscoped lookup of a global: iterate over current namespaces,
5687 considering using-directives. If SPACESP is non-NULL, store a list
5688 of the namespaces we've considered in it. */
5690 tree
5691 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5693 tree b = make_node (CPLUS_BINDING);
5694 tree initial = current_decl_namespace ();
5695 tree scope = initial;
5696 tree siter;
5697 struct cp_binding_level *level;
5698 tree val = NULL_TREE;
5700 if (spacesp)
5701 *spacesp = NULL_TREE;
5703 for (; !val; scope = CP_DECL_CONTEXT (scope))
5705 if (spacesp)
5706 *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5707 val = binding_for_name (name, scope);
5709 /* Ignore anticipated built-in functions. */
5710 if (val && BINDING_VALUE (val)
5711 && DECL_P (BINDING_VALUE (val))
5712 && DECL_LANG_SPECIFIC (BINDING_VALUE (val))
5713 && DECL_ANTICIPATED (BINDING_VALUE (val)))
5715 BINDING_VALUE (b) = NULL_TREE;
5716 BINDING_TYPE (b) = NULL_TREE;
5718 else
5720 /* Initialize binding for this context. */
5721 BINDING_VALUE (b) = BINDING_VALUE (val);
5722 BINDING_TYPE (b) = BINDING_TYPE (val);
5725 /* Add all _DECLs seen through local using-directives. */
5726 for (level = current_binding_level;
5727 !level->namespace_p;
5728 level = level->level_chain)
5729 if (!lookup_using_namespace (name, b, level->using_directives,
5730 scope, flags, spacesp))
5731 /* Give up because of error. */
5732 return error_mark_node;
5734 /* Add all _DECLs seen through global using-directives. */
5735 /* XXX local and global using lists should work equally. */
5736 siter = initial;
5737 while (1)
5739 if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter),
5740 scope, flags, spacesp))
5741 /* Give up because of error. */
5742 return error_mark_node;
5743 if (siter == scope) break;
5744 siter = CP_DECL_CONTEXT (siter);
5747 val = select_decl (b, flags);
5748 if (scope == global_namespace)
5749 break;
5751 return val;
5754 /* Combine prefer_type and namespaces_only into flags. */
5756 static int
5757 lookup_flags (int prefer_type, int namespaces_only)
5759 if (namespaces_only)
5760 return LOOKUP_PREFER_NAMESPACES;
5761 if (prefer_type > 1)
5762 return LOOKUP_PREFER_TYPES;
5763 if (prefer_type > 0)
5764 return LOOKUP_PREFER_BOTH;
5765 return 0;
5768 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5769 ignore it or not. Subroutine of lookup_name_real. */
5771 static tree
5772 qualify_lookup (tree val, int flags)
5774 if (val == NULL_TREE)
5775 return val;
5776 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5777 return val;
5778 if ((flags & LOOKUP_PREFER_TYPES) && TREE_CODE (val) == TYPE_DECL)
5779 return val;
5780 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5781 return NULL_TREE;
5782 return val;
5785 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5786 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
5787 bindings.
5789 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5790 declaration found. */
5792 tree
5793 lookup_qualified_name (tree scope, tree name, bool is_type_p, int flags)
5795 if (TREE_CODE (scope) == NAMESPACE_DECL)
5797 tree val;
5799 val = make_node (CPLUS_BINDING);
5800 flags |= LOOKUP_COMPLAIN;
5801 if (is_type_p)
5802 flags |= LOOKUP_PREFER_TYPES;
5803 if (!qualified_lookup_using_namespace (name, scope, val, flags))
5804 return NULL_TREE;
5805 return select_decl (val, flags);
5807 else
5808 return lookup_member (scope, name, 0, is_type_p);
5811 /* Check to see whether or not DECL is a variable that would have been
5812 in scope under the ARM, but is not in scope under the ANSI/ISO
5813 standard. If so, issue an error message. If name lookup would
5814 work in both cases, but return a different result, this function
5815 returns the result of ANSI/ISO lookup. Otherwise, it returns
5816 DECL. */
5818 tree
5819 check_for_out_of_scope_variable (tree decl)
5821 tree shadowed;
5823 /* We only care about out of scope variables. */
5824 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5825 return decl;
5827 shadowed = DECL_SHADOWED_FOR_VAR (decl);
5828 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5829 && DECL_DEAD_FOR_LOCAL (shadowed))
5830 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5831 if (!shadowed)
5832 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5833 if (shadowed)
5835 if (!DECL_ERROR_REPORTED (decl))
5837 warning ("name lookup of `%D' changed",
5838 DECL_NAME (decl));
5839 cp_warning_at (" matches this `%D' under ISO standard rules",
5840 shadowed);
5841 cp_warning_at (" matches this `%D' under old rules", decl);
5842 DECL_ERROR_REPORTED (decl) = 1;
5844 return shadowed;
5847 /* If we have already complained about this declaration, there's no
5848 need to do it again. */
5849 if (DECL_ERROR_REPORTED (decl))
5850 return decl;
5852 DECL_ERROR_REPORTED (decl) = 1;
5853 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5855 error ("name lookup of `%D' changed for new ISO `for' scoping",
5856 DECL_NAME (decl));
5857 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl);
5858 return error_mark_node;
5860 else
5862 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5863 DECL_NAME (decl));
5864 cp_pedwarn_at (" using obsolete binding at `%D'", decl);
5867 return decl;
5870 /* Look up NAME in the current binding level and its superiors in the
5871 namespace of variables, functions and typedefs. Return a ..._DECL
5872 node of some kind representing its definition if there is only one
5873 such declaration, or return a TREE_LIST with all the overloaded
5874 definitions if there are many, or return 0 if it is undefined.
5876 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5877 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5878 Otherwise we prefer non-TYPE_DECLs.
5880 If NONCLASS is nonzero, we don't look for the NAME in class scope,
5881 using IDENTIFIER_CLASS_VALUE. */
5883 tree
5884 lookup_name_real (tree name,
5885 int prefer_type,
5886 int nonclass,
5887 int namespaces_only,
5888 int flags)
5890 tree t;
5891 tree val = NULL_TREE;
5893 /* Conversion operators are handled specially because ordinary
5894 unqualified name lookup will not find template conversion
5895 operators. */
5896 if (IDENTIFIER_TYPENAME_P (name))
5898 struct cp_binding_level *level;
5900 for (level = current_binding_level;
5901 level && !level->namespace_p;
5902 level = level->level_chain)
5904 tree class_type;
5905 tree operators;
5907 /* A conversion operator can only be declared in a class
5908 scope. */
5909 if (level->parm_flag != 2)
5910 continue;
5912 /* Lookup the conversion operator in the class. */
5913 class_type = level->this_class;
5914 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5915 if (operators)
5916 return operators;
5919 return NULL_TREE;
5922 flags |= lookup_flags (prefer_type, namespaces_only);
5924 /* First, look in non-namespace scopes. */
5926 if (current_class_type == NULL_TREE)
5927 nonclass = 1;
5929 for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t))
5931 tree binding;
5933 if (!LOCAL_BINDING_P (t) && nonclass)
5934 /* We're not looking for class-scoped bindings, so keep going. */
5935 continue;
5937 /* If this is the kind of thing we're looking for, we're done. */
5938 if (qualify_lookup (BINDING_VALUE (t), flags))
5939 binding = BINDING_VALUE (t);
5940 else if ((flags & LOOKUP_PREFER_TYPES)
5941 && qualify_lookup (BINDING_TYPE (t), flags))
5942 binding = BINDING_TYPE (t);
5943 else
5944 binding = NULL_TREE;
5946 if (binding)
5948 val = binding;
5949 break;
5953 /* Now lookup in namespace scopes. */
5954 if (!val)
5956 t = unqualified_namespace_lookup (name, flags, 0);
5957 if (t)
5958 val = t;
5961 if (val)
5963 /* If we have a single function from a using decl, pull it out. */
5964 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5965 val = OVL_FUNCTION (val);
5968 return val;
5971 tree
5972 lookup_name_nonclass (tree name)
5974 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
5977 tree
5978 lookup_function_nonclass (tree name, tree args)
5980 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5983 tree
5984 lookup_name (tree name, int prefer_type)
5986 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
5989 /* Similar to `lookup_name' but look only in the innermost non-class
5990 binding level. */
5992 tree
5993 lookup_name_current_level (tree name)
5995 struct cp_binding_level *b;
5996 tree t = NULL_TREE;
5998 b = current_binding_level;
5999 while (b->parm_flag == 2)
6000 b = b->level_chain;
6002 if (b->namespace_p)
6004 t = IDENTIFIER_NAMESPACE_VALUE (name);
6006 /* extern "C" function() */
6007 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
6008 t = TREE_VALUE (t);
6010 else if (IDENTIFIER_BINDING (name)
6011 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
6013 while (1)
6015 if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
6016 return IDENTIFIER_VALUE (name);
6018 if (b->keep == 2)
6019 b = b->level_chain;
6020 else
6021 break;
6025 return t;
6028 /* Like lookup_name_current_level, but for types. */
6030 tree
6031 lookup_type_current_level (tree name)
6033 register tree t = NULL_TREE;
6035 my_friendly_assert (! current_binding_level->namespace_p, 980716);
6037 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
6038 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
6040 struct cp_binding_level *b = current_binding_level;
6041 while (1)
6043 if (purpose_member (name, b->type_shadowed))
6044 return REAL_IDENTIFIER_TYPE_VALUE (name);
6045 if (b->keep == 2)
6046 b = b->level_chain;
6047 else
6048 break;
6052 return t;
6056 /* Push the declarations of builtin types into the namespace.
6057 RID_INDEX is the index of the builtin type
6058 in the array RID_POINTERS. NAME is the name used when looking
6059 up the builtin type. TYPE is the _TYPE node for the builtin type. */
6061 void
6062 record_builtin_type (enum rid rid_index,
6063 const char* name,
6064 tree type)
6066 tree rname = NULL_TREE, tname = NULL_TREE;
6067 tree tdecl = NULL_TREE;
6069 if ((int) rid_index < (int) RID_MAX)
6070 rname = ridpointers[(int) rid_index];
6071 if (name)
6072 tname = get_identifier (name);
6074 if (tname)
6076 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6077 set_identifier_type_value (tname, NULL_TREE);
6078 if ((int) rid_index < (int) RID_MAX)
6079 /* Built-in types live in the global namespace. */
6080 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6082 if (rname != NULL_TREE)
6084 if (tname != NULL_TREE)
6086 set_identifier_type_value (rname, NULL_TREE);
6087 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6089 else
6091 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6092 set_identifier_type_value (rname, NULL_TREE);
6097 /* Record one of the standard Java types.
6098 * Declare it as having the given NAME.
6099 * If SIZE > 0, it is the size of one of the integral types;
6100 * otherwise it is the negative of the size of one of the other types. */
6102 static tree
6103 record_builtin_java_type (const char* name, int size)
6105 tree type, decl;
6106 if (size > 0)
6107 type = make_signed_type (size);
6108 else if (size > -32)
6109 { /* "__java_char" or ""__java_boolean". */
6110 type = make_unsigned_type (-size);
6111 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6113 else
6114 { /* "__java_float" or ""__java_double". */
6115 type = make_node (REAL_TYPE);
6116 TYPE_PRECISION (type) = - size;
6117 layout_type (type);
6119 record_builtin_type (RID_MAX, name, type);
6120 decl = TYPE_NAME (type);
6122 /* Suppress generate debug symbol entries for these types,
6123 since for normal C++ they are just clutter.
6124 However, push_lang_context undoes this if extern "Java" is seen. */
6125 DECL_IGNORED_P (decl) = 1;
6127 TYPE_FOR_JAVA (type) = 1;
6128 return type;
6131 /* Push a type into the namespace so that the back-ends ignore it. */
6133 static void
6134 record_unknown_type (tree type, const char* name)
6136 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6137 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
6138 DECL_IGNORED_P (decl) = 1;
6139 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6140 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6141 TYPE_ALIGN (type) = 1;
6142 TYPE_USER_ALIGN (type) = 0;
6143 TYPE_MODE (type) = TYPE_MODE (void_type_node);
6146 /* An string for which we should create an IDENTIFIER_NODE at
6147 startup. */
6149 typedef struct predefined_identifier
6151 /* The name of the identifier. */
6152 const char *const name;
6153 /* The place where the IDENTIFIER_NODE should be stored. */
6154 tree *const node;
6155 /* Nonzero if this is the name of a constructor or destructor. */
6156 const int ctor_or_dtor_p;
6157 } predefined_identifier;
6159 /* Create all the predefined identifiers. */
6161 static void
6162 initialize_predefined_identifiers (void)
6164 const predefined_identifier *pid;
6166 /* A table of identifiers to create at startup. */
6167 static const predefined_identifier predefined_identifiers[] = {
6168 { "C++", &lang_name_cplusplus, 0 },
6169 { "C", &lang_name_c, 0 },
6170 { "Java", &lang_name_java, 0 },
6171 { CTOR_NAME, &ctor_identifier, 1 },
6172 { "__base_ctor", &base_ctor_identifier, 1 },
6173 { "__comp_ctor", &complete_ctor_identifier, 1 },
6174 { DTOR_NAME, &dtor_identifier, 1 },
6175 { "__comp_dtor", &complete_dtor_identifier, 1 },
6176 { "__base_dtor", &base_dtor_identifier, 1 },
6177 { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6178 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6179 { "nelts", &nelts_identifier, 0 },
6180 { THIS_NAME, &this_identifier, 0 },
6181 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6182 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6183 { "_vptr", &vptr_identifier, 0 },
6184 { "__vtt_parm", &vtt_parm_identifier, 0 },
6185 { "std", &std_identifier, 0 },
6186 { NULL, NULL, 0 }
6189 for (pid = predefined_identifiers; pid->name; ++pid)
6191 *pid->node = get_identifier (pid->name);
6192 if (pid->ctor_or_dtor_p)
6193 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6197 /* Create the predefined scalar types of C,
6198 and some nodes representing standard constants (0, 1, (void *)0).
6199 Initialize the global binding level.
6200 Make definitions for built-in primitive functions. */
6202 void
6203 cxx_init_decl_processing (void)
6205 tree void_ftype;
6206 tree void_ftype_ptr;
6208 /* Create all the identifiers we need. */
6209 initialize_predefined_identifiers ();
6211 /* Fill in back-end hooks. */
6212 lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6214 /* Create the global variables. */
6215 push_to_top_level ();
6217 /* Enter the global namespace. */
6218 my_friendly_assert (global_namespace == NULL_TREE, 375);
6219 push_namespace (get_identifier ("::"));
6220 global_namespace = current_namespace;
6221 current_lang_name = NULL_TREE;
6223 /* Adjust various flags based on command-line settings. */
6224 if (! flag_permissive && ! pedantic)
6225 flag_pedantic_errors = 1;
6226 if (!flag_no_inline)
6228 flag_inline_trees = 1;
6229 flag_no_inline = 1;
6231 if (flag_inline_functions)
6233 flag_inline_trees = 2;
6234 flag_inline_functions = 0;
6237 /* Force minimum function alignment if using the least significant
6238 bit of function pointers to store the virtual bit. */
6239 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6240 && force_align_functions_log < 1)
6241 force_align_functions_log = 1;
6243 /* Initially, C. */
6244 current_lang_name = lang_name_c;
6246 current_function_decl = NULL_TREE;
6247 current_binding_level = NULL_BINDING_LEVEL;
6248 free_binding_level = NULL_BINDING_LEVEL;
6250 build_common_tree_nodes (flag_signed_char);
6252 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6253 TREE_TYPE (error_mark_list) = error_mark_node;
6255 /* Make the binding_level structure for global names. */
6256 pushlevel (0);
6257 global_binding_level = current_binding_level;
6258 /* The global level is the namespace level of ::. */
6259 NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6260 declare_namespace_level ();
6262 /* Create the `std' namespace. */
6263 push_namespace (std_identifier);
6264 std_node = current_namespace;
6265 pop_namespace ();
6267 c_common_nodes_and_builtins ();
6269 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6270 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6271 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6272 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6273 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6274 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6275 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6276 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6278 integer_two_node = build_int_2 (2, 0);
6279 TREE_TYPE (integer_two_node) = integer_type_node;
6280 integer_three_node = build_int_2 (3, 0);
6281 TREE_TYPE (integer_three_node) = integer_type_node;
6283 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6284 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6285 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6286 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6287 TYPE_PRECISION (boolean_type_node) = 1;
6288 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6289 boolean_false_node = build_int_2 (0, 0);
6290 TREE_TYPE (boolean_false_node) = boolean_type_node;
6291 boolean_true_node = build_int_2 (1, 0);
6292 TREE_TYPE (boolean_true_node) = boolean_type_node;
6294 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6296 #if 0
6297 record_builtin_type (RID_MAX, NULL, string_type_node);
6298 #endif
6300 delta_type_node = ptrdiff_type_node;
6301 vtable_index_type = ptrdiff_type_node;
6303 vtt_parm_type = build_pointer_type (const_ptr_type_node);
6304 void_ftype = build_function_type (void_type_node, void_list_node);
6305 void_ftype_ptr = build_function_type (void_type_node,
6306 tree_cons (NULL_TREE,
6307 ptr_type_node,
6308 void_list_node));
6309 void_ftype_ptr
6310 = build_exception_variant (void_ftype_ptr, empty_except_spec);
6312 /* C++ extensions */
6314 unknown_type_node = make_node (UNKNOWN_TYPE);
6315 record_unknown_type (unknown_type_node, "unknown type");
6317 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6318 TREE_TYPE (unknown_type_node) = unknown_type_node;
6320 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6321 result. */
6322 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6323 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6326 /* Make sure we get a unique function type, so we can give
6327 its pointer type a name. (This wins for gdb.) */
6328 tree vfunc_type = make_node (FUNCTION_TYPE);
6329 TREE_TYPE (vfunc_type) = integer_type_node;
6330 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6331 layout_type (vfunc_type);
6333 vtable_entry_type = build_pointer_type (vfunc_type);
6335 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6337 vtbl_type_node
6338 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6339 layout_type (vtbl_type_node);
6340 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6341 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6342 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6343 layout_type (vtbl_ptr_type_node);
6344 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6346 push_namespace (get_identifier ("__cxxabiv1"));
6347 abi_node = current_namespace;
6348 pop_namespace ();
6350 global_type_node = make_node (LANG_TYPE);
6351 record_unknown_type (global_type_node, "global type");
6353 /* Now, C++. */
6354 current_lang_name = lang_name_cplusplus;
6357 tree bad_alloc_type_node, newtype, deltype;
6358 tree ptr_ftype_sizetype;
6360 push_namespace (std_identifier);
6361 bad_alloc_type_node
6362 = xref_tag (class_type, get_identifier ("bad_alloc"),
6363 /*attributes=*/NULL_TREE, 1);
6364 pop_namespace ();
6365 ptr_ftype_sizetype
6366 = build_function_type (ptr_type_node,
6367 tree_cons (NULL_TREE,
6368 size_type_node,
6369 void_list_node));
6370 newtype = build_exception_variant
6371 (ptr_ftype_sizetype, add_exception_specifier
6372 (NULL_TREE, bad_alloc_type_node, -1));
6373 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6374 push_cp_library_fn (NEW_EXPR, newtype);
6375 push_cp_library_fn (VEC_NEW_EXPR, newtype);
6376 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6377 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6380 abort_fndecl
6381 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6383 /* Perform other language dependent initializations. */
6384 init_class_processing ();
6385 init_search_processing ();
6386 init_rtti_processing ();
6388 if (flag_exceptions)
6389 init_exception_processing ();
6391 if (! supports_one_only ())
6392 flag_weak = 0;
6394 make_fname_decl = cp_make_fname_decl;
6395 start_fname_decls ();
6397 /* Show we use EH for cleanups. */
6398 using_eh_for_cleanups ();
6400 /* Maintain consistency. Perhaps we should just complain if they
6401 say -fwritable-strings? */
6402 if (flag_writable_strings)
6403 flag_const_strings = 0;
6406 /* Generate an initializer for a function naming variable from
6407 NAME. NAME may be NULL, in which case we generate a special
6408 ERROR_MARK node which should be replaced later. */
6410 tree
6411 cp_fname_init (const char* name)
6413 tree domain = NULL_TREE;
6414 tree type;
6415 tree init = NULL_TREE;
6416 size_t length = 0;
6418 if (name)
6420 length = strlen (name);
6421 domain = build_index_type (size_int (length));
6422 init = build_string (length + 1, name);
6425 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6426 type = build_cplus_array_type (type, domain);
6428 if (init)
6429 TREE_TYPE (init) = type;
6430 else
6431 /* We don't know the value until instantiation time. Make
6432 something which will be digested now, but replaced later. */
6433 init = build (ERROR_MARK, type);
6435 return init;
6438 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6439 decl, NAME is the initialization string and TYPE_DEP indicates whether
6440 NAME depended on the type of the function. We make use of that to detect
6441 __PRETTY_FUNCTION__ inside a template fn. This is being done
6442 lazily at the point of first use, so we musn't push the decl now. */
6444 static tree
6445 cp_make_fname_decl (tree id, int type_dep)
6447 const char *const name = (type_dep && processing_template_decl
6448 ? NULL : fname_as_string (type_dep));
6449 tree init = cp_fname_init (name);
6450 tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6452 /* As we're using pushdecl_with_scope, we must set the context. */
6453 DECL_CONTEXT (decl) = current_function_decl;
6454 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6456 TREE_STATIC (decl) = 1;
6457 TREE_READONLY (decl) = 1;
6458 DECL_ARTIFICIAL (decl) = 1;
6459 DECL_INITIAL (decl) = init;
6461 TREE_USED (decl) = 1;
6463 if (current_function_decl)
6465 struct cp_binding_level *b = current_binding_level;
6466 while (b->level_chain->parm_flag == 0)
6467 b = b->level_chain;
6468 pushdecl_with_scope (decl, b);
6471 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6473 return decl;
6476 /* Make a definition for a builtin function named NAME in the current
6477 namespace, whose data type is TYPE and whose context is CONTEXT.
6478 TYPE should be a function type with argument types.
6480 CLASS and CODE tell later passes how to compile calls to this function.
6481 See tree.h for possible values.
6483 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6484 the name to be called if we can't opencode the function.
6485 If ATTRS is nonzero, use that for the function's attribute
6486 list. */
6488 static tree
6489 builtin_function_1 (const char* name,
6490 tree type,
6491 tree context,
6492 int code,
6493 enum built_in_class class,
6494 const char* libname,
6495 tree attrs)
6497 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6498 DECL_BUILT_IN_CLASS (decl) = class;
6499 DECL_FUNCTION_CODE (decl) = code;
6500 DECL_CONTEXT (decl) = context;
6502 pushdecl (decl);
6504 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6505 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6506 function in the namespace. */
6507 if (libname)
6508 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6509 make_decl_rtl (decl, NULL);
6511 /* Warn if a function in the namespace for users
6512 is used without an occasion to consider it declared. */
6513 if (name[0] != '_' || name[1] != '_')
6514 DECL_ANTICIPATED (decl) = 1;
6516 /* Possibly apply some default attributes to this built-in function. */
6517 if (attrs)
6518 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6519 else
6520 decl_attributes (&decl, NULL_TREE, 0);
6522 return decl;
6525 /* Entry point for the benefit of c_common_nodes_and_builtins.
6527 Make a defintion for a builtin function named NAME and whose data type
6528 is TYPE. TYPE should be a function type with argument types. This
6529 function places the anticipated declaration in the global namespace
6530 and additionally in the std namespace if appropriate.
6532 CLASS and CODE tell later passes how to compile calls to this function.
6533 See tree.h for possible values.
6535 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6536 the name to be called if we can't opencode the function.
6538 If ATTRS is nonzero, use that for the function's attribute
6539 list. */
6541 tree
6542 builtin_function (const char* name,
6543 tree type,
6544 int code,
6545 enum built_in_class class,
6546 const char* libname,
6547 tree attrs)
6549 /* All builtins that don't begin with an '_' should additionally
6550 go in the 'std' namespace. */
6551 if (name[0] != '_')
6553 push_namespace (std_identifier);
6554 builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6555 pop_namespace ();
6558 return builtin_function_1 (name, type, NULL_TREE, code,
6559 class, libname, attrs);
6562 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6563 function. Not called directly. */
6565 static tree
6566 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6568 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6569 DECL_EXTERNAL (fn) = 1;
6570 TREE_PUBLIC (fn) = 1;
6571 DECL_ARTIFICIAL (fn) = 1;
6572 TREE_NOTHROW (fn) = 1;
6573 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6574 SET_DECL_LANGUAGE (fn, lang_c);
6575 return fn;
6578 /* Returns the _DECL for a library function with C linkage.
6579 We assume that such functions never throw; if this is incorrect,
6580 callers should unset TREE_NOTHROW. */
6582 tree
6583 build_library_fn (tree name, tree type)
6585 return build_library_fn_1 (name, ERROR_MARK, type);
6588 /* Returns the _DECL for a library function with C++ linkage. */
6590 static tree
6591 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6593 tree fn = build_library_fn_1 (name, operator_code, type);
6594 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6595 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6596 SET_DECL_LANGUAGE (fn, lang_cplusplus);
6597 set_mangled_name_for_decl (fn);
6598 return fn;
6601 /* Like build_library_fn, but takes a C string instead of an
6602 IDENTIFIER_NODE. */
6604 tree
6605 build_library_fn_ptr (const char* name, tree type)
6607 return build_library_fn (get_identifier (name), type);
6610 /* Like build_cp_library_fn, but takes a C string instead of an
6611 IDENTIFIER_NODE. */
6613 tree
6614 build_cp_library_fn_ptr (const char* name, tree type)
6616 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6619 /* Like build_library_fn, but also pushes the function so that we will
6620 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
6622 tree
6623 push_library_fn (tree name, tree type)
6625 tree fn = build_library_fn (name, type);
6626 pushdecl_top_level (fn);
6627 return fn;
6630 /* Like build_cp_library_fn, but also pushes the function so that it
6631 will be found by normal lookup. */
6633 static tree
6634 push_cp_library_fn (enum tree_code operator_code, tree type)
6636 tree fn = build_cp_library_fn (ansi_opname (operator_code),
6637 operator_code,
6638 type);
6639 pushdecl (fn);
6640 return fn;
6643 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6644 a FUNCTION_TYPE. */
6646 tree
6647 push_void_library_fn (tree name, tree parmtypes)
6649 tree type = build_function_type (void_type_node, parmtypes);
6650 return push_library_fn (name, type);
6653 /* Like push_library_fn, but also note that this function throws
6654 and does not return. Used for __throw_foo and the like. */
6656 tree
6657 push_throw_library_fn (tree name, tree type)
6659 tree fn = push_library_fn (name, type);
6660 TREE_THIS_VOLATILE (fn) = 1;
6661 TREE_NOTHROW (fn) = 0;
6662 return fn;
6665 /* Apply default attributes to a function, if a system function with default
6666 attributes. */
6668 void
6669 cxx_insert_default_attributes (tree decl)
6671 if (!DECL_EXTERN_C_FUNCTION_P (decl))
6672 return;
6673 if (!TREE_PUBLIC (decl))
6674 return;
6675 c_common_insert_default_attributes (decl);
6678 /* When we call finish_struct for an anonymous union, we create
6679 default copy constructors and such. But, an anonymous union
6680 shouldn't have such things; this function undoes the damage to the
6681 anonymous union type T.
6683 (The reason that we create the synthesized methods is that we don't
6684 distinguish `union { int i; }' from `typedef union { int i; } U'.
6685 The first is an anonymous union; the second is just an ordinary
6686 union type.) */
6688 void
6689 fixup_anonymous_aggr (tree t)
6691 tree *q;
6693 /* Wipe out memory of synthesized methods */
6694 TYPE_HAS_CONSTRUCTOR (t) = 0;
6695 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6696 TYPE_HAS_INIT_REF (t) = 0;
6697 TYPE_HAS_CONST_INIT_REF (t) = 0;
6698 TYPE_HAS_ASSIGN_REF (t) = 0;
6699 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6701 /* Splice the implicitly generated functions out of the TYPE_METHODS
6702 list. */
6703 q = &TYPE_METHODS (t);
6704 while (*q)
6706 if (DECL_ARTIFICIAL (*q))
6707 *q = TREE_CHAIN (*q);
6708 else
6709 q = &TREE_CHAIN (*q);
6712 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
6713 if (TYPE_METHODS (t))
6714 cp_error_at ("an anonymous union cannot have function members", t);
6716 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6717 assignment operators (because they cannot have these methods themselves).
6718 For anonymous unions this is already checked because they are not allowed
6719 in any union, otherwise we have to check it. */
6720 if (TREE_CODE (t) != UNION_TYPE)
6722 tree field, type;
6724 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6725 if (TREE_CODE (field) == FIELD_DECL)
6727 type = TREE_TYPE (field);
6728 if (CLASS_TYPE_P (type))
6730 if (TYPE_NEEDS_CONSTRUCTING (type))
6731 cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6732 field);
6733 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6734 cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6735 field);
6736 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6737 cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6738 field);
6744 /* Make sure that a declaration with no declarator is well-formed, i.e.
6745 just declares a tagged type or anonymous union.
6747 Returns the type declared; or NULL_TREE if none. */
6749 tree
6750 check_tag_decl (tree declspecs)
6752 int found_type = 0;
6753 int saw_friend = 0;
6754 int saw_typedef = 0;
6755 tree ob_modifier = NULL_TREE;
6756 register tree link;
6757 /* If a class, struct, or enum type is declared by the DECLSPECS
6758 (i.e, if a class-specifier, enum-specifier, or non-typename
6759 elaborated-type-specifier appears in the DECLSPECS),
6760 DECLARED_TYPE is set to the corresponding type. */
6761 tree declared_type = NULL_TREE;
6762 bool error_p = false;
6764 for (link = declspecs; link; link = TREE_CHAIN (link))
6766 tree value = TREE_VALUE (link);
6768 if (TYPE_P (value)
6769 || TREE_CODE (value) == TYPE_DECL
6770 || (TREE_CODE (value) == IDENTIFIER_NODE
6771 && IDENTIFIER_GLOBAL_VALUE (value)
6772 && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6774 ++found_type;
6776 if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6778 if (! in_system_header)
6779 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6780 return NULL_TREE;
6783 if (TYPE_P (value)
6784 && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6785 || TREE_CODE (value) == ENUMERAL_TYPE))
6787 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6788 declared_type = value;
6791 else if (value == ridpointers[(int) RID_TYPEDEF])
6792 saw_typedef = 1;
6793 else if (value == ridpointers[(int) RID_FRIEND])
6795 if (current_class_type == NULL_TREE
6796 || current_scope () != current_class_type)
6797 ob_modifier = value;
6798 else
6799 saw_friend = 1;
6801 else if (value == ridpointers[(int) RID_STATIC]
6802 || value == ridpointers[(int) RID_EXTERN]
6803 || value == ridpointers[(int) RID_AUTO]
6804 || value == ridpointers[(int) RID_REGISTER]
6805 || value == ridpointers[(int) RID_INLINE]
6806 || value == ridpointers[(int) RID_VIRTUAL]
6807 || value == ridpointers[(int) RID_CONST]
6808 || value == ridpointers[(int) RID_VOLATILE]
6809 || value == ridpointers[(int) RID_EXPLICIT]
6810 || value == ridpointers[(int) RID_THREAD])
6811 ob_modifier = value;
6812 else if (value == error_mark_node)
6813 error_p = true;
6816 if (found_type > 1)
6817 error ("multiple types in one declaration");
6819 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6820 pedwarn ("declaration does not declare anything");
6821 /* Check for an anonymous union. */
6822 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6823 && TYPE_ANONYMOUS_P (declared_type))
6825 /* 7/3 In a simple-declaration, the optional init-declarator-list
6826 can be omitted only when declaring a class (clause 9) or
6827 enumeration (7.2), that is, when the decl-specifier-seq contains
6828 either a class-specifier, an elaborated-type-specifier with
6829 a class-key (9.1), or an enum-specifier. In these cases and
6830 whenever a class-specifier or enum-specifier is present in the
6831 decl-specifier-seq, the identifiers in these specifiers are among
6832 the names being declared by the declaration (as class-name,
6833 enum-names, or enumerators, depending on the syntax). In such
6834 cases, and except for the declaration of an unnamed bit-field (9.6),
6835 the decl-specifier-seq shall introduce one or more names into the
6836 program, or shall redeclare a name introduced by a previous
6837 declaration. [Example:
6838 enum { }; // ill-formed
6839 typedef class { }; // ill-formed
6840 --end example] */
6841 if (saw_typedef)
6843 error ("missing type-name in typedef-declaration");
6844 return NULL_TREE;
6846 /* Anonymous unions are objects, so they can have specifiers. */;
6847 SET_ANON_AGGR_TYPE_P (declared_type);
6849 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
6850 && !in_system_header)
6851 pedwarn ("ISO C++ prohibits anonymous structs");
6854 else if (ob_modifier)
6856 if (ob_modifier == ridpointers[(int) RID_INLINE]
6857 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6858 error ("`%D' can only be specified for functions", ob_modifier);
6859 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6860 error ("`%D' can only be specified inside a class", ob_modifier);
6861 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6862 error ("`%D' can only be specified for constructors",
6863 ob_modifier);
6864 else
6865 error ("`%D' can only be specified for objects and functions",
6866 ob_modifier);
6869 return declared_type;
6872 /* Called when a declaration is seen that contains no names to declare.
6873 If its type is a reference to a structure, union or enum inherited
6874 from a containing scope, shadow that tag name for the current scope
6875 with a forward reference.
6876 If its type defines a new named structure or union
6877 or defines an enum, it is valid but we need not do anything here.
6878 Otherwise, it is an error.
6880 C++: may have to grok the declspecs to learn about static,
6881 complain for anonymous unions.
6883 Returns the TYPE declared -- or NULL_TREE if none. */
6885 tree
6886 shadow_tag (tree declspecs)
6888 tree t = check_tag_decl (declspecs);
6890 if (!t)
6891 return NULL_TREE;
6893 maybe_process_partial_specialization (t);
6895 /* This is where the variables in an anonymous union are
6896 declared. An anonymous union declaration looks like:
6897 union { ... } ;
6898 because there is no declarator after the union, the parser
6899 sends that declaration here. */
6900 if (ANON_AGGR_TYPE_P (t))
6902 fixup_anonymous_aggr (t);
6904 if (TYPE_FIELDS (t))
6906 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6907 NULL);
6908 finish_anon_union (decl);
6912 return t;
6915 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6917 tree
6918 groktypename (tree typename)
6920 tree specs, attrs;
6921 tree type;
6922 if (TREE_CODE (typename) != TREE_LIST)
6923 return typename;
6924 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6925 type = grokdeclarator (TREE_VALUE (typename), specs,
6926 TYPENAME, 0, &attrs);
6927 if (attrs)
6928 cplus_decl_attributes (&type, attrs, 0);
6929 return type;
6932 /* Decode a declarator in an ordinary declaration or data definition.
6933 This is called as soon as the type information and variable name
6934 have been parsed, before parsing the initializer if any.
6935 Here we create the ..._DECL node, fill in its type,
6936 and put it on the list of decls for the current context.
6937 The ..._DECL node is returned as the value.
6939 Exception: for arrays where the length is not specified,
6940 the type is left null, to be filled in by `cp_finish_decl'.
6942 Function definitions do not come here; they go to start_function
6943 instead. However, external and forward declarations of functions
6944 do go through here. Structure field declarations are done by
6945 grokfield and not through here. */
6947 tree
6948 start_decl (tree declarator,
6949 tree declspecs,
6950 int initialized,
6951 tree attributes,
6952 tree prefix_attributes)
6954 tree decl;
6955 register tree type, tem;
6956 tree context;
6958 /* This should only be done once on the top most decl. */
6959 if (have_extern_spec)
6961 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
6962 declspecs);
6963 have_extern_spec = false;
6966 /* An object declared as __attribute__((deprecated)) suppresses
6967 warnings of uses of other deprecated items. */
6968 if (lookup_attribute ("deprecated", attributes))
6969 deprecated_state = DEPRECATED_SUPPRESS;
6971 attributes = chainon (attributes, prefix_attributes);
6973 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6974 &attributes);
6976 deprecated_state = DEPRECATED_NORMAL;
6978 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
6979 return NULL_TREE;
6981 type = TREE_TYPE (decl);
6983 if (type == error_mark_node)
6984 return NULL_TREE;
6986 context = DECL_CONTEXT (decl);
6988 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6989 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6991 /* When parsing the initializer, lookup should use the object's
6992 namespace. */
6993 push_decl_namespace (context);
6996 /* We are only interested in class contexts, later. */
6997 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6998 context = NULL_TREE;
7000 if (initialized)
7001 /* Is it valid for this decl to have an initializer at all?
7002 If not, set INITIALIZED to zero, which will indirectly
7003 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
7004 switch (TREE_CODE (decl))
7006 case TYPE_DECL:
7007 error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
7008 initialized = 0;
7009 break;
7011 case FUNCTION_DECL:
7012 error ("function `%#D' is initialized like a variable", decl);
7013 initialized = 0;
7014 break;
7016 default:
7017 break;
7020 if (initialized)
7022 if (! toplevel_bindings_p ()
7023 && DECL_EXTERNAL (decl))
7024 warning ("declaration of `%#D' has `extern' and is initialized",
7025 decl);
7026 DECL_EXTERNAL (decl) = 0;
7027 if (toplevel_bindings_p ())
7028 TREE_STATIC (decl) = 1;
7030 /* Tell `pushdecl' this is an initialized decl
7031 even though we don't yet have the initializer expression.
7032 Also tell `cp_finish_decl' it may store the real initializer. */
7033 DECL_INITIAL (decl) = error_mark_node;
7036 /* Set attributes here so if duplicate decl, will have proper attributes. */
7037 cplus_decl_attributes (&decl, attributes, 0);
7039 /* If #pragma weak was used, mark the decl weak now. */
7040 if (current_binding_level == global_binding_level)
7041 maybe_apply_pragma_weak (decl);
7043 if (TREE_CODE (decl) == FUNCTION_DECL
7044 && DECL_DECLARED_INLINE_P (decl)
7045 && DECL_UNINLINABLE (decl)
7046 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
7047 warning_with_decl (decl,
7048 "inline function `%s' given attribute noinline");
7050 if (context && COMPLETE_TYPE_P (complete_type (context)))
7052 push_nested_class (context);
7054 if (TREE_CODE (decl) == VAR_DECL)
7056 tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
7057 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7058 error ("`%#D' is not a static member of `%#T'", decl, context);
7059 else
7061 if (DECL_CONTEXT (field) != context)
7063 if (!same_type_p (DECL_CONTEXT (field), context))
7064 pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7065 DECL_CONTEXT (field), DECL_NAME (decl),
7066 context, DECL_NAME (decl));
7067 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7069 /* Static data member are tricky; an in-class initialization
7070 still doesn't provide a definition, so the in-class
7071 declaration will have DECL_EXTERNAL set, but will have an
7072 initialization. Thus, duplicate_decls won't warn
7073 about this situation, and so we check here. */
7074 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7075 error ("duplicate initialization of %D", decl);
7076 if (duplicate_decls (decl, field))
7077 decl = field;
7080 else
7082 tree field = check_classfn (context, decl);
7083 if (field && duplicate_decls (decl, field))
7084 decl = field;
7087 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
7088 DECL_IN_AGGR_P (decl) = 0;
7089 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7090 || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7092 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7093 /* [temp.expl.spec] An explicit specialization of a static data
7094 member of a template is a definition if the declaration
7095 includes an initializer; otherwise, it is a declaration.
7097 We check for processing_specialization so this only applies
7098 to the new specialization syntax. */
7099 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7100 DECL_EXTERNAL (decl) = 1;
7103 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7104 pedwarn ("declaration of `%#D' outside of class is not definition",
7105 decl);
7108 /* Enter this declaration into the symbol table. */
7109 tem = maybe_push_decl (decl);
7111 if (processing_template_decl)
7112 tem = push_template_decl (tem);
7114 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7115 /* Tell the back-end to use or not use .common as appropriate. If we say
7116 -fconserve-space, we want this to save .data space, at the expense of
7117 wrong semantics. If we say -fno-conserve-space, we want this to
7118 produce errors about redefs; to do this we force variables into the
7119 data segment. */
7120 DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7121 || !DECL_THREAD_LOCAL (tem))
7122 && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7123 #endif
7125 if (! processing_template_decl)
7126 start_decl_1 (tem);
7128 return tem;
7131 void
7132 start_decl_1 (tree decl)
7134 tree type = TREE_TYPE (decl);
7135 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7137 if (type == error_mark_node)
7138 return;
7140 maybe_push_cleanup_level (type);
7142 if (initialized)
7143 /* Is it valid for this decl to have an initializer at all?
7144 If not, set INITIALIZED to zero, which will indirectly
7145 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
7147 /* Don't allow initializations for incomplete types except for
7148 arrays which might be completed by the initialization. */
7149 if (COMPLETE_TYPE_P (complete_type (type)))
7150 ; /* A complete type is ok. */
7151 else if (TREE_CODE (type) != ARRAY_TYPE)
7153 error ("variable `%#D' has initializer but incomplete type",
7154 decl);
7155 initialized = 0;
7156 type = TREE_TYPE (decl) = error_mark_node;
7158 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7160 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7161 error ("elements of array `%#D' have incomplete type", decl);
7162 /* else we already gave an error in start_decl. */
7163 initialized = 0;
7167 if (!initialized
7168 && TREE_CODE (decl) != TYPE_DECL
7169 && TREE_CODE (decl) != TEMPLATE_DECL
7170 && type != error_mark_node
7171 && IS_AGGR_TYPE (type)
7172 && ! DECL_EXTERNAL (decl))
7174 if ((! processing_template_decl || ! uses_template_parms (type))
7175 && !COMPLETE_TYPE_P (complete_type (type)))
7177 error ("aggregate `%#D' has incomplete type and cannot be defined",
7178 decl);
7179 /* Change the type so that assemble_variable will give
7180 DECL an rtl we can live with: (mem (const_int 0)). */
7181 type = TREE_TYPE (decl) = error_mark_node;
7183 else
7185 /* If any base type in the hierarchy of TYPE needs a constructor,
7186 then we set initialized to 1. This way any nodes which are
7187 created for the purposes of initializing this aggregate
7188 will live as long as it does. This is necessary for global
7189 aggregates which do not have their initializers processed until
7190 the end of the file. */
7191 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7195 if (! initialized)
7196 DECL_INITIAL (decl) = NULL_TREE;
7199 /* Handle initialization of references.
7200 These three arguments are from `cp_finish_decl', and have the
7201 same meaning here that they do there.
7203 Quotes on semantics can be found in ARM 8.4.3. */
7205 static tree
7206 grok_reference_init (tree decl, tree type, tree init)
7208 tree tmp;
7210 if (init == NULL_TREE)
7212 if ((DECL_LANG_SPECIFIC (decl) == 0
7213 || DECL_IN_AGGR_P (decl) == 0)
7214 && ! DECL_THIS_EXTERN (decl))
7215 error ("`%D' declared as reference but not initialized", decl);
7216 return NULL_TREE;
7219 if (TREE_CODE (init) == CONSTRUCTOR)
7221 error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7222 return NULL_TREE;
7225 if (TREE_CODE (init) == TREE_LIST)
7226 init = build_compound_expr (init);
7228 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7229 init = convert_from_reference (init);
7231 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7232 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7234 /* Note: default conversion is only called in very special cases. */
7235 init = default_conversion (init);
7238 /* Convert INIT to the reference type TYPE. This may involve the
7239 creation of a temporary, whose lifetime must be the same as that
7240 of the reference. If so, a DECL_STMT for the temporary will be
7241 added just after the DECL_STMT for DECL. That's why we don't set
7242 DECL_INITIAL for local references (instead assigning to them
7243 explicitly); we need to allow the temporary to be initialized
7244 first. */
7245 tmp = convert_to_reference
7246 (type, init, CONV_IMPLICIT,
7247 LOOKUP_ONLYCONVERTING|LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND,
7248 decl);
7250 if (tmp == error_mark_node)
7251 return NULL_TREE;
7252 else if (tmp == NULL_TREE)
7254 error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7255 return NULL_TREE;
7258 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7259 return tmp;
7261 DECL_INITIAL (decl) = tmp;
7263 return NULL_TREE;
7266 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7267 array until we finish parsing the initializer. If that's the
7268 situation we're in, update DECL accordingly. */
7270 static void
7271 maybe_deduce_size_from_array_init (tree decl, tree init)
7273 tree type = TREE_TYPE (decl);
7275 if (TREE_CODE (type) == ARRAY_TYPE
7276 && TYPE_DOMAIN (type) == NULL_TREE
7277 && TREE_CODE (decl) != TYPE_DECL)
7279 /* do_default is really a C-ism to deal with tentative definitions.
7280 But let's leave it here to ease the eventual merge. */
7281 int do_default = !DECL_EXTERNAL (decl);
7282 tree initializer = init ? init : DECL_INITIAL (decl);
7283 int failure = complete_array_type (type, initializer, do_default);
7285 if (failure == 1)
7286 error ("initializer fails to determine size of `%D'", decl);
7288 if (failure == 2)
7290 if (do_default)
7291 error ("array size missing in `%D'", decl);
7292 /* If a `static' var's size isn't known, make it extern as
7293 well as static, so it does not get allocated. If it's not
7294 `static', then don't mark it extern; finish_incomplete_decl
7295 will give it a default size and it will get allocated. */
7296 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7297 DECL_EXTERNAL (decl) = 1;
7300 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7301 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7302 integer_zero_node))
7303 error ("zero-size array `%D'", decl);
7305 layout_decl (decl, 0);
7309 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7310 any appropriate error messages regarding the layout. */
7312 static void
7313 layout_var_decl (tree decl)
7315 tree type = TREE_TYPE (decl);
7316 #if 0
7317 tree ttype = target_type (type);
7318 #endif
7320 /* If we haven't already layed out this declaration, do so now.
7321 Note that we must not call complete type for an external object
7322 because it's type might involve templates that we are not
7323 supposed to isntantiate yet. (And it's perfectly valid to say
7324 `extern X x' for some incomplete type `X'.) */
7325 if (!DECL_EXTERNAL (decl))
7326 complete_type (type);
7327 if (!DECL_SIZE (decl)
7328 && TREE_TYPE (decl) != error_mark_node
7329 && (COMPLETE_TYPE_P (type)
7330 || (TREE_CODE (type) == ARRAY_TYPE
7331 && !TYPE_DOMAIN (type)
7332 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7333 layout_decl (decl, 0);
7335 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7337 /* An automatic variable with an incomplete type: that is an error.
7338 Don't talk about array types here, since we took care of that
7339 message in grokdeclarator. */
7340 error ("storage size of `%D' isn't known", decl);
7341 TREE_TYPE (decl) = error_mark_node;
7343 #if 0
7344 /* Keep this code around in case we later want to control debug info
7345 based on whether a type is "used". (jason 1999-11-11) */
7347 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7348 /* Let debugger know it should output info for this type. */
7349 note_debug_info_needed (ttype);
7351 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7352 note_debug_info_needed (DECL_CONTEXT (decl));
7353 #endif
7355 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7356 && DECL_SIZE (decl) != NULL_TREE
7357 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7359 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7360 constant_expression_warning (DECL_SIZE (decl));
7361 else
7362 error ("storage size of `%D' isn't constant", decl);
7365 if (TREE_STATIC (decl)
7366 && !DECL_ARTIFICIAL (decl)
7367 && current_function_decl
7368 && DECL_CONTEXT (decl) == current_function_decl)
7369 push_local_name (decl);
7372 /* If a local static variable is declared in an inline function, or if
7373 we have a weak definition, we must endeavor to create only one
7374 instance of the variable at link-time. */
7376 static void
7377 maybe_commonize_var (tree decl)
7379 /* Static data in a function with comdat linkage also has comdat
7380 linkage. */
7381 if (TREE_STATIC (decl)
7382 /* Don't mess with __FUNCTION__. */
7383 && ! DECL_ARTIFICIAL (decl)
7384 && current_function_decl
7385 && DECL_CONTEXT (decl) == current_function_decl
7386 && (DECL_DECLARED_INLINE_P (current_function_decl)
7387 || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
7388 && TREE_PUBLIC (current_function_decl))
7390 /* If flag_weak, we don't need to mess with this, as we can just
7391 make the function weak, and let it refer to its unique local
7392 copy. This works because we don't allow the function to be
7393 inlined. */
7394 if (! flag_weak)
7396 if (DECL_INTERFACE_KNOWN (current_function_decl))
7398 TREE_PUBLIC (decl) = 1;
7399 DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7401 else if (DECL_INITIAL (decl) == NULL_TREE
7402 || DECL_INITIAL (decl) == error_mark_node)
7404 TREE_PUBLIC (decl) = 1;
7405 DECL_COMMON (decl) = 1;
7407 /* else we lose. We can only do this if we can use common,
7408 which we can't if it has been initialized. */
7410 if (!TREE_PUBLIC (decl))
7412 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7413 cp_warning_at (" you can work around this by removing the initializer", decl);
7416 else
7417 comdat_linkage (decl);
7419 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7420 /* Set it up again; we might have set DECL_INITIAL since the last
7421 time. */
7422 comdat_linkage (decl);
7425 /* Issue an error message if DECL is an uninitialized const variable. */
7427 static void
7428 check_for_uninitialized_const_var (tree decl)
7430 tree type = TREE_TYPE (decl);
7432 /* ``Unless explicitly declared extern, a const object does not have
7433 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7434 7.1.6 */
7435 if (TREE_CODE (decl) == VAR_DECL
7436 && TREE_CODE (type) != REFERENCE_TYPE
7437 && CP_TYPE_CONST_P (type)
7438 && !TYPE_NEEDS_CONSTRUCTING (type)
7439 && !DECL_INITIAL (decl))
7440 error ("uninitialized const `%D'", decl);
7443 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
7444 returned is the next FIELD_DECL (possibly FIELD itself) that can be
7445 initialized. If there are no more such fields, the return value
7446 will be NULL. */
7448 static tree
7449 next_initializable_field (tree field)
7451 while (field
7452 && (TREE_CODE (field) != FIELD_DECL
7453 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7454 || DECL_ARTIFICIAL (field)))
7455 field = TREE_CHAIN (field);
7457 return field;
7460 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7461 brace-enclosed aggregate initializer.
7463 *INITP is one of a list of initializers describing a brace-enclosed
7464 initializer for an entity of the indicated aggregate TYPE. It may
7465 not presently match the shape of the TYPE; for example:
7467 struct S { int a; int b; };
7468 struct S a[] = { 1, 2, 3, 4 };
7470 Here *INITP will point to TREE_LIST of four elements, rather than a
7471 list of two elements, each itself a list of two elements. This
7472 routine transforms INIT from the former form into the latter. The
7473 revised initializer is returned. */
7475 static tree
7476 reshape_init (tree type, tree *initp)
7478 tree inits;
7479 tree old_init;
7480 tree old_init_value;
7481 tree new_init;
7482 bool brace_enclosed_p;
7484 old_init = *initp;
7485 old_init_value = (TREE_CODE (*initp) == TREE_LIST
7486 ? TREE_VALUE (*initp) : old_init);
7488 /* For some parse errors, OLD_INIT_VALUE may be NULL. */
7489 if (!old_init_value)
7491 my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
7492 TREE_VALUE (old_init) = error_mark_node;
7493 return old_init;
7496 /* If the initializer is brace-enclosed, pull initializers from the
7497 enclosed elements. Advance past the brace-enclosed initializer
7498 now. */
7499 if (TREE_CODE (old_init_value) == CONSTRUCTOR
7500 && TREE_HAS_CONSTRUCTOR (old_init_value))
7502 *initp = TREE_CHAIN (old_init);
7503 TREE_CHAIN (old_init) = NULL_TREE;
7504 inits = CONSTRUCTOR_ELTS (old_init_value);
7505 initp = &inits;
7506 brace_enclosed_p = true;
7508 else
7510 inits = NULL_TREE;
7511 brace_enclosed_p = false;
7514 /* A non-aggregate type is always initialized with a single
7515 initializer. */
7516 if (!CP_AGGREGATE_TYPE_P (type))
7518 *initp = TREE_CHAIN (old_init);
7519 TREE_CHAIN (old_init) = NULL_TREE;
7520 /* It is invalid to initialize a non-aggregate type with a
7521 brace-enclosed initializer. */
7522 if (brace_enclosed_p)
7524 error ("brace-enclosed initializer used to initialize `%T'",
7525 type);
7526 if (TREE_CODE (old_init) == TREE_LIST)
7527 TREE_VALUE (old_init) = error_mark_node;
7528 else
7529 old_init = error_mark_node;
7532 return old_init;
7535 /* [dcl.init.aggr]
7537 All implicit type conversions (clause _conv_) are considered when
7538 initializing the aggregate member with an initializer from an
7539 initializer-list. If the initializer can initialize a member,
7540 the member is initialized. Otherwise, if the member is itself a
7541 non-empty subaggregate, brace elision is assumed and the
7542 initializer is considered for the initialization of the first
7543 member of the subaggregate. */
7544 if (CLASS_TYPE_P (type)
7545 && !brace_enclosed_p
7546 && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7548 *initp = TREE_CHAIN (old_init);
7549 TREE_CHAIN (old_init) = NULL_TREE;
7550 return old_init;
7553 if (TREE_CODE (old_init_value) == STRING_CST
7554 && TREE_CODE (type) == ARRAY_TYPE
7555 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7557 /* [dcl.init.string]
7559 A char array (whether plain char, signed char, or unsigned char)
7560 can be initialized by a string-literal (optionally enclosed in
7561 braces); a wchar_t array can be initialized by a wide
7562 string-literal (optionally enclosed in braces). */
7563 new_init = old_init;
7564 /* Move past the initializer. */
7565 *initp = TREE_CHAIN (old_init);
7566 TREE_CHAIN (old_init) = NULL_TREE;
7568 else
7570 /* Build a CONSTRUCTOR to hold the contents of the aggregate. */
7571 new_init = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
7572 TREE_HAS_CONSTRUCTOR (new_init) = 1;
7574 if (CLASS_TYPE_P (type))
7576 tree field;
7578 field = next_initializable_field (TYPE_FIELDS (type));
7580 if (!field)
7582 /* [dcl.init.aggr]
7584 An initializer for an aggregate member that is an
7585 empty class shall have the form of an empty
7586 initializer-list {}. */
7587 if (!brace_enclosed_p)
7588 error ("initializer for `%T' must be brace-enclosed",
7589 type);
7591 else
7593 /* Loop through the initializable fields, gathering
7594 initializers. */
7595 while (*initp && field)
7597 tree field_init;
7599 field_init = reshape_init (TREE_TYPE (field), initp);
7600 TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7601 CONSTRUCTOR_ELTS (new_init) = field_init;
7602 /* [dcl.init.aggr]
7604 When a union is initialized with a brace-enclosed
7605 initializer, the braces shall only contain an
7606 initializer for the first member of the union. */
7607 if (TREE_CODE (type) == UNION_TYPE)
7608 break;
7609 if (TREE_PURPOSE (field_init))
7610 field = TREE_PURPOSE (field_init);
7611 field = next_initializable_field (TREE_CHAIN (field));
7615 else if (TREE_CODE (type) == ARRAY_TYPE)
7617 tree index;
7618 tree max_index;
7620 /* If the bound of the array is known, take no more initializers
7621 than are allowed. */
7622 max_index = (TYPE_DOMAIN (type)
7623 ? array_type_nelts (type) : NULL_TREE);
7624 /* Loop through the array elements, gathering initializers. */
7625 for (index = size_zero_node;
7626 *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7627 index = size_binop (PLUS_EXPR, index, size_one_node))
7629 tree element_init;
7631 element_init = reshape_init (TREE_TYPE (type), initp);
7632 TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7633 CONSTRUCTOR_ELTS (new_init) = element_init;
7634 if (TREE_PURPOSE (element_init))
7635 index = TREE_PURPOSE (element_init);
7638 else
7639 abort ();
7641 /* The initializers were placed in reverse order in the
7642 CONSTRUCTOR. */
7643 CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7645 if (TREE_CODE (old_init) == TREE_LIST)
7646 new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7649 /* If this was a brace-enclosed initializer and all of the
7650 initializers were not used up, there is a problem. */
7651 if (brace_enclosed_p && *initp)
7652 error ("too many initializers for `%T'", type);
7654 return new_init;
7657 /* Verify INIT (the initializer for DECL), and record the
7658 initialization in DECL_INITIAL, if appropriate.
7660 If the return value is non-NULL, it is an expression that must be
7661 evaluated dynamically to initialize DECL. */
7663 static tree
7664 check_initializer (tree decl, tree init, int flags)
7666 tree type = TREE_TYPE (decl);
7668 /* If `start_decl' didn't like having an initialization, ignore it now. */
7669 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7670 init = NULL_TREE;
7672 /* If an initializer is present, DECL_INITIAL has been
7673 error_mark_node, to indicate that an as-of-yet unevaluated
7674 initialization will occur. From now on, DECL_INITIAL reflects
7675 the static initialization -- if any -- of DECL. */
7676 DECL_INITIAL (decl) = NULL_TREE;
7678 /* Things that are going to be initialized need to have complete
7679 type. */
7680 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7682 if (type == error_mark_node)
7683 /* We will have already complained. */
7684 init = NULL_TREE;
7685 else if (init && COMPLETE_TYPE_P (type)
7686 && !TREE_CONSTANT (TYPE_SIZE (type)))
7688 error ("variable-sized object `%D' may not be initialized", decl);
7689 init = NULL_TREE;
7691 else if (TREE_CODE (type) == ARRAY_TYPE
7692 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7694 error ("elements of array `%#D' have incomplete type", decl);
7695 init = NULL_TREE;
7697 else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7699 error ("`%D' has incomplete type", decl);
7700 TREE_TYPE (decl) = error_mark_node;
7701 init = NULL_TREE;
7704 if (TREE_CODE (decl) == CONST_DECL)
7706 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7708 DECL_INITIAL (decl) = init;
7710 my_friendly_assert (init != NULL_TREE, 149);
7711 init = NULL_TREE;
7713 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7714 init = grok_reference_init (decl, type, init);
7715 else if (init)
7717 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7719 /* [dcl.init] paragraph 13,
7720 If T is a scalar type, then a declaration of the form
7721 T x = { a };
7722 is equivalent to
7723 T x = a;
7725 reshape_init will complain about the extra braces,
7726 and doesn't do anything useful in the case where TYPE is
7727 scalar, so just don't call it. */
7728 if (CP_AGGREGATE_TYPE_P (type))
7729 init = reshape_init (type, &init);
7732 /* If DECL has an array type without a specific bound, deduce the
7733 array size from the initializer. */
7734 maybe_deduce_size_from_array_init (decl, init);
7735 type = TREE_TYPE (decl);
7736 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7737 TREE_TYPE (init) = type;
7739 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7741 if (TREE_CODE (type) == ARRAY_TYPE)
7742 goto initialize_aggr;
7743 else if (TREE_CODE (init) == CONSTRUCTOR
7744 && TREE_HAS_CONSTRUCTOR (init))
7746 if (TYPE_NON_AGGREGATE_CLASS (type))
7748 error ("`%D' must be initialized by constructor, not by `{...}'",
7749 decl);
7750 init = error_mark_node;
7752 else
7753 goto dont_use_constructor;
7755 else
7757 int saved_stmts_are_full_exprs_p;
7759 initialize_aggr:
7760 saved_stmts_are_full_exprs_p = 0;
7761 if (building_stmt_tree ())
7763 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7764 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7766 init = build_aggr_init (decl, init, flags);
7767 if (building_stmt_tree ())
7768 current_stmt_tree ()->stmts_are_full_exprs_p =
7769 saved_stmts_are_full_exprs_p;
7770 return init;
7773 else
7775 dont_use_constructor:
7776 if (TREE_CODE (init) != TREE_VEC)
7777 init = store_init_value (decl, init);
7780 else if (DECL_EXTERNAL (decl))
7782 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7783 goto initialize_aggr;
7784 else if (IS_AGGR_TYPE (type))
7786 tree core_type = strip_array_types (type);
7788 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7789 error ("structure `%D' with uninitialized const members", decl);
7790 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7791 error ("structure `%D' with uninitialized reference members",
7792 decl);
7794 check_for_uninitialized_const_var (decl);
7796 else
7797 check_for_uninitialized_const_var (decl);
7799 if (init && init != error_mark_node)
7800 init = build (INIT_EXPR, type, decl, init);
7802 return init;
7805 /* If DECL is not a local variable, give it RTL. */
7807 static void
7808 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7810 int toplev = toplevel_bindings_p ();
7811 int defer_p;
7813 /* Handle non-variables up front. */
7814 if (TREE_CODE (decl) != VAR_DECL)
7816 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7817 return;
7820 /* If we see a class member here, it should be a static data
7821 member. */
7822 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7824 my_friendly_assert (TREE_STATIC (decl), 19990828);
7825 /* An in-class declaration of a static data member should be
7826 external; it is only a declaration, and not a definition. */
7827 if (init == NULL_TREE)
7828 my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7831 /* Set the DECL_ASSEMBLER_NAME for the variable. */
7832 if (asmspec)
7834 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7835 /* The `register' keyword, when used together with an
7836 asm-specification, indicates that the variable should be
7837 placed in a particular register. */
7838 if (DECL_REGISTER (decl))
7839 DECL_C_HARD_REGISTER (decl) = 1;
7842 /* We don't create any RTL for local variables. */
7843 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7844 return;
7846 /* We defer emission of local statics until the corresponding
7847 DECL_STMT is expanded. */
7848 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7850 /* We try to defer namespace-scope static constants so that they are
7851 not emitted into the object file unnecessarily. */
7852 if (!DECL_VIRTUAL_P (decl)
7853 && TREE_READONLY (decl)
7854 && DECL_INITIAL (decl) != NULL_TREE
7855 && DECL_INITIAL (decl) != error_mark_node
7856 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7857 && toplev
7858 && !TREE_PUBLIC (decl))
7860 /* Fool with the linkage of static consts according to #pragma
7861 interface. */
7862 if (!interface_unknown && !TREE_PUBLIC (decl))
7864 TREE_PUBLIC (decl) = 1;
7865 DECL_EXTERNAL (decl) = interface_only;
7868 defer_p = 1;
7870 /* Likewise for template instantiations. */
7871 else if (DECL_COMDAT (decl))
7872 defer_p = 1;
7874 /* If we're deferring the variable, we only need to make RTL if
7875 there's an ASMSPEC. Otherwise, we'll lazily create it later when
7876 we need it. (There's no way to lazily create RTL for things that
7877 have assembly specs because the information about the specifier
7878 isn't stored in the tree, yet) */
7879 if (defer_p && asmspec)
7880 make_decl_rtl (decl, asmspec);
7881 /* If we're not deferring, go ahead and assemble the variable. */
7882 else if (!defer_p)
7883 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7886 /* The old ARM scoping rules injected variables declared in the
7887 initialization statement of a for-statement into the surrounding
7888 scope. We support this usage, in order to be backward-compatible.
7889 DECL is a just-declared VAR_DECL; if necessary inject its
7890 declaration into the surrounding scope. */
7892 void
7893 maybe_inject_for_scope_var (tree decl)
7895 if (!DECL_NAME (decl))
7896 return;
7898 /* Declarations of __FUNCTION__ and its ilk appear magically when
7899 the variable is first used. If that happens to be inside a
7900 for-loop, we don't want to do anything special. */
7901 if (DECL_PRETTY_FUNCTION_P (decl))
7902 return;
7904 if (current_binding_level->is_for_scope)
7906 struct cp_binding_level *outer
7907 = current_binding_level->level_chain;
7909 /* Check to see if the same name is already bound at the outer
7910 level, either because it was directly declared, or because a
7911 dead for-decl got preserved. In either case, the code would
7912 not have been valid under the ARM scope rules, so clear
7913 is_for_scope for the current_binding_level.
7915 Otherwise, we need to preserve the temp slot for decl to last
7916 into the outer binding level. */
7918 tree outer_binding
7919 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
7921 if (outer_binding && BINDING_LEVEL (outer_binding) == outer
7922 && (TREE_CODE (BINDING_VALUE (outer_binding))
7923 == VAR_DECL)
7924 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7926 BINDING_VALUE (outer_binding)
7927 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7928 current_binding_level->is_for_scope = 0;
7933 /* Generate code to initialize DECL (a local variable). */
7935 static void
7936 initialize_local_var (tree decl, tree init)
7938 tree type = TREE_TYPE (decl);
7940 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
7941 || TREE_CODE (decl) == RESULT_DECL,
7942 20021010);
7943 my_friendly_assert (!TREE_STATIC (decl), 20021010);
7945 if (DECL_SIZE (decl) == NULL_TREE)
7947 /* If we used it already as memory, it must stay in memory. */
7948 DECL_INITIAL (decl) = NULL_TREE;
7949 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7952 if (DECL_SIZE (decl) && type != error_mark_node)
7954 int already_used;
7956 /* Compute and store the initial value. */
7957 already_used = TREE_USED (decl) || TREE_USED (type);
7959 /* Perform the initialization. */
7960 if (init)
7962 int saved_stmts_are_full_exprs_p;
7964 my_friendly_assert (building_stmt_tree (), 20000906);
7965 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7966 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7967 finish_expr_stmt (init);
7968 current_stmt_tree ()->stmts_are_full_exprs_p =
7969 saved_stmts_are_full_exprs_p;
7972 /* Set this to 0 so we can tell whether an aggregate which was
7973 initialized was ever used. Don't do this if it has a
7974 destructor, so we don't complain about the 'resource
7975 allocation is initialization' idiom. Now set
7976 attribute((unused)) on types so decls of that type will be
7977 marked used. (see TREE_USED, above.) */
7978 if (TYPE_NEEDS_CONSTRUCTING (type)
7979 && ! already_used
7980 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7981 && DECL_NAME (decl))
7982 TREE_USED (decl) = 0;
7983 else if (already_used)
7984 TREE_USED (decl) = 1;
7987 /* Generate a cleanup, if necessary. */
7988 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7990 tree cleanup;
7992 /* Compute the cleanup. */
7993 cleanup = cxx_maybe_build_cleanup (decl);
7995 /* Record the cleanup required for this declaration. */
7996 if (DECL_SIZE (decl) && cleanup)
7997 finish_decl_cleanup (decl, cleanup);
8001 /* Finish processing of a declaration;
8002 install its line number and initial value.
8003 If the length of an array type is not known before,
8004 it must be determined now, from the initial value, or it is an error.
8006 INIT holds the value of an initializer that should be allowed to escape
8007 the normal rules.
8009 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8010 if the (init) syntax was used. */
8012 void
8013 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
8015 register tree type;
8016 tree ttype = NULL_TREE;
8017 const char *asmspec = NULL;
8018 int was_readonly = 0;
8020 if (! decl)
8022 if (init)
8023 error ("assignment (not initialization) in declaration");
8024 return;
8027 /* If a name was specified, get the string. */
8028 if (current_binding_level == global_binding_level)
8029 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8030 if (asmspec_tree)
8031 asmspec = TREE_STRING_POINTER (asmspec_tree);
8033 if (init && TREE_CODE (init) == NAMESPACE_DECL)
8035 error ("cannot initialize `%D' to namespace `%D'",
8036 decl, init);
8037 init = NULL_TREE;
8040 if (current_class_type
8041 && CP_DECL_CONTEXT (decl) == current_class_type
8042 && TYPE_BEING_DEFINED (current_class_type)
8043 && (DECL_INITIAL (decl) || init))
8044 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8046 if (TREE_CODE (decl) == VAR_DECL
8047 && DECL_CONTEXT (decl)
8048 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8049 && DECL_CONTEXT (decl) != current_namespace
8050 && init)
8052 /* Leave the namespace of the object. */
8053 pop_decl_namespace ();
8056 type = TREE_TYPE (decl);
8058 if (type == error_mark_node)
8059 return;
8061 if (TYPE_HAS_MUTABLE_P (type))
8062 TREE_READONLY (decl) = 0;
8064 if (processing_template_decl)
8066 /* Add this declaration to the statement-tree. */
8067 if (at_function_scope_p ()
8068 && TREE_CODE (decl) != RESULT_DECL)
8069 add_decl_stmt (decl);
8071 if (init && DECL_INITIAL (decl))
8072 DECL_INITIAL (decl) = init;
8073 goto finish_end0;
8076 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8077 my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8079 /* Take care of TYPE_DECLs up front. */
8080 if (TREE_CODE (decl) == TYPE_DECL)
8082 if (type != error_mark_node
8083 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8085 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8086 warning ("shadowing previous type declaration of `%#D'", decl);
8087 set_identifier_type_value (DECL_NAME (decl), type);
8088 CLASSTYPE_GOT_SEMICOLON (type) = 1;
8091 /* If we have installed this as the canonical typedef for this
8092 type, and that type has not been defined yet, delay emitting
8093 the debug information for it, as we will emit it later. */
8094 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8095 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8096 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8098 rest_of_decl_compilation (decl, NULL,
8099 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8100 goto finish_end;
8103 if (TREE_CODE (decl) != FUNCTION_DECL)
8104 ttype = target_type (type);
8106 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8107 && TYPE_NEEDS_CONSTRUCTING (type))
8109 /* Currently, GNU C++ puts constants in text space, making them
8110 impossible to initialize. In the future, one would hope for
8111 an operating system which understood the difference between
8112 initialization and the running of a program. */
8113 was_readonly = 1;
8114 TREE_READONLY (decl) = 0;
8117 if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8119 /* This must override the asm specifier which was placed by
8120 grokclassfn. Lay this out fresh. */
8121 SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8122 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8123 make_decl_rtl (decl, asmspec);
8125 else if (TREE_CODE (decl) == RESULT_DECL)
8126 init = check_initializer (decl, init, flags);
8127 else if (TREE_CODE (decl) == VAR_DECL)
8129 /* Only PODs can have thread-local storage. Other types may require
8130 various kinds of non-trivial initialization. */
8131 if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8132 error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8133 decl, TREE_TYPE (decl));
8134 /* Convert the initializer to the type of DECL, if we have not
8135 already initialized DECL. */
8136 if (!DECL_INITIALIZED_P (decl)
8137 /* If !DECL_EXTERNAL then DECL is being defined. In the
8138 case of a static data member initialized inside the
8139 class-specifier, there can be an initializer even if DECL
8140 is *not* defined. */
8141 && (!DECL_EXTERNAL (decl) || init))
8143 init = check_initializer (decl, init, flags);
8144 /* Thread-local storage cannot be dynamically initialized. */
8145 if (DECL_THREAD_LOCAL (decl) && init)
8147 error ("`%D' is thread-local and so cannot be dynamically "
8148 "initialized", decl);
8149 init = NULL_TREE;
8151 /* Handle:
8153 [dcl.init]
8155 The memory occupied by any object of static storage
8156 duration is zero-initialized at program startup before
8157 any other initialization takes place.
8159 We cannot create an appropriate initializer until after
8160 the type of DECL is finalized. If DECL_INITIAL is set,
8161 then the DECL is statically initialized, and any
8162 necessary zero-initialization has already been performed. */
8163 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8164 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8165 /*static_storage_p=*/true);
8166 /* Remember that the initialization for this variable has
8167 taken place. */
8168 DECL_INITIALIZED_P (decl) = 1;
8170 /* If the variable has an array type, lay out the type, even if
8171 there is no initializer. It is valid to index through the
8172 array, and we must get TYPE_ALIGN set correctly on the array
8173 type. */
8174 else if (TREE_CODE (type) == ARRAY_TYPE)
8175 layout_type (type);
8178 /* Add this declaration to the statement-tree. This needs to happen
8179 after the call to check_initializer so that the DECL_STMT for a
8180 reference temp is added before the DECL_STMT for the reference itself. */
8181 if (building_stmt_tree ()
8182 && at_function_scope_p ()
8183 && TREE_CODE (decl) != RESULT_DECL)
8184 add_decl_stmt (decl);
8186 if (TREE_CODE (decl) == VAR_DECL)
8187 layout_var_decl (decl);
8189 /* Output the assembler code and/or RTL code for variables and functions,
8190 unless the type is an undefined structure or union.
8191 If not, it will get done when the type is completed. */
8192 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8193 || TREE_CODE (decl) == RESULT_DECL)
8195 if (TREE_CODE (decl) == VAR_DECL)
8196 maybe_commonize_var (decl);
8198 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8200 if (TREE_CODE (type) == FUNCTION_TYPE
8201 || TREE_CODE (type) == METHOD_TYPE)
8202 abstract_virtuals_error (decl,
8203 strip_array_types (TREE_TYPE (type)));
8204 else
8205 abstract_virtuals_error (decl, strip_array_types (type));
8207 if (TREE_CODE (decl) == FUNCTION_DECL
8208 || TREE_TYPE (decl) == error_mark_node)
8209 /* No initialization required. */
8211 else if (DECL_EXTERNAL (decl)
8212 && ! (DECL_LANG_SPECIFIC (decl)
8213 && DECL_NOT_REALLY_EXTERN (decl)))
8215 if (init)
8216 DECL_INITIAL (decl) = init;
8218 else
8220 /* A variable definition. */
8221 if (DECL_FUNCTION_SCOPE_P (decl))
8223 /* This is a local declaration. */
8224 if (doing_semantic_analysis_p ())
8225 maybe_inject_for_scope_var (decl);
8226 /* Initialize the local variable. */
8227 if (processing_template_decl)
8229 if (init || DECL_INITIAL (decl) == error_mark_node)
8230 DECL_INITIAL (decl) = init;
8232 else if (!TREE_STATIC (decl))
8233 initialize_local_var (decl, init);
8236 if (TREE_STATIC (decl))
8237 expand_static_init (decl, init);
8239 finish_end0:
8241 /* Undo call to `pushclass' that was done in `start_decl'
8242 due to initialization of qualified member variable.
8243 I.e., Foo::x = 10; */
8245 tree context = CP_DECL_CONTEXT (decl);
8246 if (context
8247 && TYPE_P (context)
8248 && (TREE_CODE (decl) == VAR_DECL
8249 /* We also have a pushclass done that we need to undo here
8250 if we're at top level and declare a method. */
8251 || TREE_CODE (decl) == FUNCTION_DECL)
8252 /* If size hasn't been set, we're still defining it,
8253 and therefore inside the class body; don't pop
8254 the binding level.. */
8255 && COMPLETE_TYPE_P (context)
8256 && context == current_class_type)
8257 pop_nested_class ();
8261 finish_end:
8263 if (was_readonly)
8264 TREE_READONLY (decl) = 1;
8267 /* This is here for a midend callback from c-common.c */
8269 void
8270 finish_decl (tree decl, tree init, tree asmspec_tree)
8272 cp_finish_decl (decl, init, asmspec_tree, 0);
8275 /* Returns a declaration for a VAR_DECL as if:
8277 extern "C" TYPE NAME;
8279 had been seen. Used to create compiler-generated global
8280 variables. */
8282 tree
8283 declare_global_var (tree name, tree type)
8285 tree decl;
8287 push_to_top_level ();
8288 decl = build_decl (VAR_DECL, name, type);
8289 TREE_PUBLIC (decl) = 1;
8290 DECL_EXTERNAL (decl) = 1;
8291 DECL_ARTIFICIAL (decl) = 1;
8292 pushdecl (decl);
8293 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8294 pop_from_top_level ();
8296 return decl;
8299 /* Returns a pointer to the `atexit' function. Note that if
8300 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8301 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8303 static tree
8304 get_atexit_node (void)
8306 tree atexit_fndecl;
8307 tree arg_types;
8308 tree fn_type;
8309 tree fn_ptr_type;
8310 const char *name;
8312 if (atexit_node)
8313 return atexit_node;
8315 if (flag_use_cxa_atexit)
8317 /* The declaration for `__cxa_atexit' is:
8319 int __cxa_atexit (void (*)(void *), void *, void *)
8321 We build up the argument types and then then function type
8322 itself. */
8324 /* First, build the pointer-to-function type for the first
8325 argument. */
8326 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8327 fn_type = build_function_type (void_type_node, arg_types);
8328 fn_ptr_type = build_pointer_type (fn_type);
8329 /* Then, build the rest of the argument types. */
8330 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8331 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8332 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8333 /* And the final __cxa_atexit type. */
8334 fn_type = build_function_type (integer_type_node, arg_types);
8335 fn_ptr_type = build_pointer_type (fn_type);
8336 name = "__cxa_atexit";
8338 else
8340 /* The declaration for `atexit' is:
8342 int atexit (void (*)());
8344 We build up the argument types and then then function type
8345 itself. */
8346 fn_type = build_function_type (void_type_node, void_list_node);
8347 fn_ptr_type = build_pointer_type (fn_type);
8348 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8349 /* Build the final atexit type. */
8350 fn_type = build_function_type (integer_type_node, arg_types);
8351 name = "atexit";
8354 /* Now, build the function declaration. */
8355 push_lang_context (lang_name_c);
8356 atexit_fndecl = build_library_fn_ptr (name, fn_type);
8357 mark_used (atexit_fndecl);
8358 pop_lang_context ();
8359 atexit_node = default_conversion (atexit_fndecl);
8361 return atexit_node;
8364 /* Returns the __dso_handle VAR_DECL. */
8366 static tree
8367 get_dso_handle_node (void)
8369 if (dso_handle_node)
8370 return dso_handle_node;
8372 /* Declare the variable. */
8373 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8374 ptr_type_node);
8376 return dso_handle_node;
8379 /* Begin a new function with internal linkage whose job will be simply
8380 to destroy some particular variable. */
8382 static tree
8383 start_cleanup_fn (void)
8385 static int counter = 0;
8386 int old_interface_only = interface_only;
8387 int old_interface_unknown = interface_unknown;
8388 char name[32];
8389 tree parmtypes;
8390 tree fntype;
8391 tree fndecl;
8393 push_to_top_level ();
8395 /* No need to mangle this. */
8396 push_lang_context (lang_name_c);
8398 interface_only = 0;
8399 interface_unknown = 1;
8401 /* Build the parameter-types. */
8402 parmtypes = void_list_node;
8403 /* Functions passed to __cxa_atexit take an additional parameter.
8404 We'll just ignore it. After we implement the new calling
8405 convention for destructors, we can eliminate the use of
8406 additional cleanup functions entirely in the -fnew-abi case. */
8407 if (flag_use_cxa_atexit)
8408 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8409 /* Build the function type itself. */
8410 fntype = build_function_type (void_type_node, parmtypes);
8411 /* Build the name of the function. */
8412 sprintf (name, "__tcf_%d", counter++);
8413 /* Build the function declaration. */
8414 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8415 /* It's a function with internal linkage, generated by the
8416 compiler. */
8417 TREE_PUBLIC (fndecl) = 0;
8418 DECL_ARTIFICIAL (fndecl) = 1;
8419 /* Make the function `inline' so that it is only emitted if it is
8420 actually needed. It is unlikely that it will be inlined, since
8421 it is only called via a function pointer, but we avoid unnecessary
8422 emissions this way. */
8423 DECL_INLINE (fndecl) = 1;
8424 /* Build the parameter. */
8425 if (flag_use_cxa_atexit)
8427 tree parmdecl;
8429 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8430 DECL_CONTEXT (parmdecl) = fndecl;
8431 TREE_USED (parmdecl) = 1;
8432 DECL_ARGUMENTS (fndecl) = parmdecl;
8435 pushdecl (fndecl);
8436 start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8438 interface_unknown = old_interface_unknown;
8439 interface_only = old_interface_only;
8441 pop_lang_context ();
8443 return current_function_decl;
8446 /* Finish the cleanup function begun by start_cleanup_fn. */
8448 static void
8449 end_cleanup_fn (void)
8451 expand_body (finish_function (0));
8453 pop_from_top_level ();
8456 /* Generate code to handle the destruction of DECL, an object with
8457 static storage duration. */
8459 void
8460 register_dtor_fn (tree decl)
8462 tree cleanup;
8463 tree compound_stmt;
8464 tree args;
8465 tree fcall;
8466 int saved_flag_access_control;
8468 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8469 return;
8471 /* Call build_cleanup before we enter the anonymous function so that
8472 any access checks will be done relative to the current scope,
8473 rather than the scope of the anonymous function. */
8474 build_cleanup (decl);
8476 /* Now start the function. */
8477 cleanup = start_cleanup_fn ();
8479 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8480 to the original function, rather than the anonymous one. That
8481 will make the back-end think that nested functions are in use,
8482 which causes confusion. */
8483 saved_flag_access_control = flag_access_control;
8484 scope_chain->check_access = flag_access_control = 0;
8485 fcall = build_cleanup (decl);
8486 scope_chain->check_access = flag_access_control = saved_flag_access_control;
8488 /* Create the body of the anonymous function. */
8489 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
8490 finish_expr_stmt (fcall);
8491 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
8492 end_cleanup_fn ();
8494 /* Call atexit with the cleanup function. */
8495 cxx_mark_addressable (cleanup);
8496 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8497 if (flag_use_cxa_atexit)
8499 args = tree_cons (NULL_TREE,
8500 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8501 NULL_TREE);
8502 args = tree_cons (NULL_TREE, null_pointer_node, args);
8503 args = tree_cons (NULL_TREE, cleanup, args);
8505 else
8506 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8507 finish_expr_stmt (build_function_call (get_atexit_node (), args));
8510 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8511 is its initializer. Generate code to handle the construction
8512 and destruction of DECL. */
8514 static void
8515 expand_static_init (tree decl, tree init)
8517 tree oldstatic;
8519 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8520 my_friendly_assert (TREE_STATIC (decl), 20021010);
8522 /* Some variables require no initialization. */
8523 if (!init
8524 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8525 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8526 return;
8528 oldstatic = value_member (decl, static_aggregates);
8530 if (oldstatic)
8532 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8533 error ("multiple initializations given for `%D'", decl);
8535 else if (! toplevel_bindings_p ())
8537 /* Emit code to perform this initialization but once. */
8538 tree if_stmt;
8539 tree then_clause;
8540 tree assignment;
8541 tree guard;
8542 tree guard_init;
8544 /* Emit code to perform this initialization but once. This code
8545 looks like:
8547 static int guard = 0;
8548 if (!guard) {
8549 // Do initialization.
8550 guard = 1;
8551 // Register variable for destruction at end of program.
8554 Note that the `temp' variable is only set to 1 *after* the
8555 initialization is complete. This ensures that an exception,
8556 thrown during the construction, will cause the variable to
8557 reinitialized when we pass through this code again, as per:
8559 [stmt.dcl]
8561 If the initialization exits by throwing an exception, the
8562 initialization is not complete, so it will be tried again
8563 the next time control enters the declaration.
8565 In theory, this process should be thread-safe, too; multiple
8566 threads should not be able to initialize the variable more
8567 than once. We don't yet attempt to ensure thread-safety. */
8569 /* Create the guard variable. */
8570 guard = get_guard (decl);
8572 /* Begin the conditional initialization. */
8573 if_stmt = begin_if_stmt ();
8574 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8575 then_clause = begin_compound_stmt (/*has_no_scope=*/0);
8577 /* Do the initialization itself. */
8578 assignment = init ? init : NULL_TREE;
8580 /* Once the assignment is complete, set TEMP to 1. Since the
8581 construction of the static object is complete at this point,
8582 we want to make sure TEMP is set to 1 even if a temporary
8583 constructed during the initialization throws an exception
8584 when it is destroyed. So, we combine the initialization and
8585 the assignment to TEMP into a single expression, ensuring
8586 that when we call finish_expr_stmt the cleanups will not be
8587 run until after TEMP is set to 1. */
8588 guard_init = set_guard (guard);
8589 if (assignment)
8591 assignment = tree_cons (NULL_TREE, assignment,
8592 build_tree_list (NULL_TREE,
8593 guard_init));
8594 assignment = build_compound_expr (assignment);
8596 else
8597 assignment = guard_init;
8598 finish_expr_stmt (assignment);
8600 /* Use atexit to register a function for destroying this static
8601 variable. */
8602 register_dtor_fn (decl);
8604 finish_compound_stmt (/*has_no_scope=*/0, then_clause);
8605 finish_then_clause (if_stmt);
8606 finish_if_stmt ();
8608 else
8609 static_aggregates = tree_cons (init, decl, static_aggregates);
8612 /* Finish the declaration of a catch-parameter. */
8614 tree
8615 start_handler_parms (tree declspecs, tree declarator)
8617 tree decl;
8618 if (declspecs)
8620 decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8621 1, NULL);
8622 if (decl == NULL_TREE)
8623 error ("invalid catch parameter");
8625 else
8626 decl = NULL_TREE;
8628 return decl;
8632 /* Make TYPE a complete type based on INITIAL_VALUE.
8633 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8634 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8637 complete_array_type (tree type, tree initial_value, int do_default)
8639 register tree maxindex = NULL_TREE;
8640 int value = 0;
8642 if (initial_value)
8644 /* An array of character type can be initialized from a
8645 brace-enclosed string constant. */
8646 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8647 && TREE_CODE (initial_value) == CONSTRUCTOR
8648 && CONSTRUCTOR_ELTS (initial_value)
8649 && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8650 == STRING_CST)
8651 && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8652 initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8654 /* Note MAXINDEX is really the maximum index, one less than the
8655 size. */
8656 if (TREE_CODE (initial_value) == STRING_CST)
8658 int eltsize
8659 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8660 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8661 / eltsize) - 1, 0);
8663 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8665 tree elts = CONSTRUCTOR_ELTS (initial_value);
8667 maxindex = ssize_int (-1);
8668 for (; elts; elts = TREE_CHAIN (elts))
8670 if (TREE_PURPOSE (elts))
8671 maxindex = TREE_PURPOSE (elts);
8672 else
8673 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8675 maxindex = copy_node (maxindex);
8677 else
8679 /* Make an error message unless that happened already. */
8680 if (initial_value != error_mark_node)
8681 value = 1;
8682 else
8683 initial_value = NULL_TREE;
8685 /* Prevent further error messages. */
8686 maxindex = build_int_2 (0, 0);
8690 if (!maxindex)
8692 if (do_default)
8693 maxindex = build_int_2 (0, 0);
8694 value = 2;
8697 if (maxindex)
8699 tree itype;
8700 tree domain;
8702 domain = build_index_type (maxindex);
8703 TYPE_DOMAIN (type) = domain;
8705 if (! TREE_TYPE (maxindex))
8706 TREE_TYPE (maxindex) = domain;
8707 if (initial_value)
8708 itype = TREE_TYPE (initial_value);
8709 else
8710 itype = NULL;
8711 if (itype && !TYPE_DOMAIN (itype))
8712 TYPE_DOMAIN (itype) = domain;
8713 /* The type of the main variant should never be used for arrays
8714 of different sizes. It should only ever be completed with the
8715 size of the array. */
8716 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8717 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8720 /* Lay out the type now that we can get the real answer. */
8722 layout_type (type);
8724 return value;
8727 /* Return zero if something is declared to be a member of type
8728 CTYPE when in the context of CUR_TYPE. STRING is the error
8729 message to print in that case. Otherwise, quietly return 1. */
8731 static int
8732 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8734 if (ctype && ctype != cur_type)
8736 if (flags == DTOR_FLAG)
8737 error ("destructor for alien class `%T' cannot be a member",
8738 ctype);
8739 else
8740 error ("constructor for alien class `%T' cannot be a member",
8741 ctype);
8742 return 0;
8744 return 1;
8747 /* Subroutine of `grokdeclarator'. */
8749 /* Generate errors possibly applicable for a given set of specifiers.
8750 This is for ARM $7.1.2. */
8752 static void
8753 bad_specifiers (tree object,
8754 const char* type,
8755 int virtualp,
8756 int quals,
8757 int inlinep,
8758 int friendp,
8759 int raises)
8761 if (virtualp)
8762 error ("`%D' declared as a `virtual' %s", object, type);
8763 if (inlinep)
8764 error ("`%D' declared as an `inline' %s", object, type);
8765 if (quals)
8766 error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8767 object, type);
8768 if (friendp)
8769 cp_error_at ("`%D' declared as a friend", object);
8770 if (raises
8771 && (TREE_CODE (object) == TYPE_DECL
8772 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8773 && !TYPE_REFFN_P (TREE_TYPE (object))
8774 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8775 cp_error_at ("`%D' declared with an exception specification", object);
8778 /* CTYPE is class type, or null if non-class.
8779 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8780 or METHOD_TYPE.
8781 DECLARATOR is the function's name.
8782 VIRTUALP is truthvalue of whether the function is virtual or not.
8783 FLAGS are to be passed through to `grokclassfn'.
8784 QUALS are qualifiers indicating whether the function is `const'
8785 or `volatile'.
8786 RAISES is a list of exceptions that this function can raise.
8787 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8788 not look, and -1 if we should not call `grokclassfn' at all.
8790 Returns `NULL_TREE' if something goes wrong, after issuing
8791 applicable error messages. */
8793 static tree
8794 grokfndecl (tree ctype,
8795 tree type,
8796 tree declarator,
8797 tree orig_declarator,
8798 int virtualp,
8799 enum overload_flags flags,
8800 tree quals,
8801 tree raises,
8802 int check,
8803 int friendp,
8804 int publicp,
8805 int inlinep,
8806 int funcdef_flag,
8807 int template_count,
8808 tree in_namespace)
8810 tree decl;
8811 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8812 int has_default_arg = 0;
8813 tree t;
8815 if (raises)
8816 type = build_exception_variant (type, raises);
8818 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8819 /* Propagate volatile out from type to decl. */
8820 if (TYPE_VOLATILE (type))
8821 TREE_THIS_VOLATILE (decl) = 1;
8823 /* If this decl has namespace scope, set that up. */
8824 if (in_namespace)
8825 set_decl_namespace (decl, in_namespace, friendp);
8826 else if (!ctype)
8827 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8829 /* `main' and builtins have implicit 'C' linkage. */
8830 if ((MAIN_NAME_P (declarator)
8831 || (IDENTIFIER_LENGTH (declarator) > 10
8832 && IDENTIFIER_POINTER (declarator)[0] == '_'
8833 && IDENTIFIER_POINTER (declarator)[1] == '_'
8834 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8835 && current_lang_name == lang_name_cplusplus
8836 && ctype == NULL_TREE
8837 /* NULL_TREE means global namespace. */
8838 && DECL_CONTEXT (decl) == NULL_TREE)
8839 SET_DECL_LANGUAGE (decl, lang_c);
8841 /* Should probably propagate const out from type to decl I bet (mrs). */
8842 if (staticp)
8844 DECL_STATIC_FUNCTION_P (decl) = 1;
8845 DECL_CONTEXT (decl) = ctype;
8848 if (ctype)
8849 DECL_CONTEXT (decl) = ctype;
8851 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8853 if (processing_template_decl)
8854 error ("cannot declare `::main' to be a template");
8855 if (inlinep)
8856 error ("cannot declare `::main' to be inline");
8857 if (!publicp)
8858 error ("cannot declare `::main' to be static");
8859 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8860 integer_type_node))
8861 error ("`main' must return `int'");
8862 inlinep = 0;
8863 publicp = 1;
8866 /* Members of anonymous types and local classes have no linkage; make
8867 them internal. */
8868 /* FIXME what if it gets a name from typedef? */
8869 if (ctype && (TYPE_ANONYMOUS_P (ctype)
8870 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8871 publicp = 0;
8873 if (publicp)
8875 /* [basic.link]: A name with no linkage (notably, the name of a class
8876 or enumeration declared in a local scope) shall not be used to
8877 declare an entity with linkage.
8879 Only check this for public decls for now. */
8880 t = no_linkage_check (TREE_TYPE (decl));
8881 if (t)
8883 if (TYPE_ANONYMOUS_P (t))
8885 if (DECL_EXTERN_C_P (decl))
8886 /* Allow this; it's pretty common in C. */;
8887 else
8889 pedwarn ("non-local function `%#D' uses anonymous type",
8890 decl);
8891 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8892 cp_pedwarn_at ("\
8893 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8894 TYPE_NAME (t));
8897 else
8898 pedwarn ("non-local function `%#D' uses local type `%T'",
8899 decl, t);
8903 TREE_PUBLIC (decl) = publicp;
8904 if (! publicp)
8906 DECL_INTERFACE_KNOWN (decl) = 1;
8907 DECL_NOT_REALLY_EXTERN (decl) = 1;
8910 /* If the declaration was declared inline, mark it as such. */
8911 if (inlinep)
8912 DECL_DECLARED_INLINE_P (decl) = 1;
8913 /* We inline functions that are explicitly declared inline, or, when
8914 the user explicitly asks us to, all functions. */
8915 if (DECL_DECLARED_INLINE_P (decl) || flag_inline_trees == 2)
8916 DECL_INLINE (decl) = 1;
8918 DECL_EXTERNAL (decl) = 1;
8919 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8921 error ("%smember function `%D' cannot have `%T' method qualifier",
8922 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8923 quals = NULL_TREE;
8926 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8927 grok_op_properties (decl, friendp);
8929 if (ctype && decl_function_context (decl))
8930 DECL_NO_STATIC_CHAIN (decl) = 1;
8932 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8933 if (TREE_PURPOSE (t)
8934 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8936 has_default_arg = 1;
8937 break;
8940 if (friendp
8941 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8943 if (funcdef_flag)
8944 error
8945 ("defining explicit specialization `%D' in friend declaration",
8946 orig_declarator);
8947 else
8949 tree fns = TREE_OPERAND (orig_declarator, 0);
8950 tree args = TREE_OPERAND (orig_declarator, 1);
8952 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8954 /* Something like `template <class T> friend void f<T>()'. */
8955 error ("invalid use of template-id `%D' in declaration of primary template",
8956 orig_declarator);
8957 return NULL_TREE;
8961 /* A friend declaration of the form friend void f<>(). Record
8962 the information in the TEMPLATE_ID_EXPR. */
8963 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8965 if (TREE_CODE (fns) == COMPONENT_REF)
8967 /* Due to bison parser ickiness, we will have already looked
8968 up an operator_name or PFUNCNAME within the current class
8969 (see template_id in parse.y). If the current class contains
8970 such a name, we'll get a COMPONENT_REF here. Undo that. */
8972 my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
8973 == current_class_type, 20001120);
8974 fns = TREE_OPERAND (fns, 1);
8976 my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
8977 || TREE_CODE (fns) == LOOKUP_EXPR
8978 || TREE_CODE (fns) == OVERLOAD, 20001120);
8979 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
8981 if (has_default_arg)
8983 error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8984 decl);
8985 return NULL_TREE;
8988 if (inlinep)
8990 error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8991 decl);
8992 return NULL_TREE;
8997 if (funcdef_flag)
8998 /* Make the init_value nonzero so pushdecl knows this is not
8999 tentative. error_mark_node is replaced later with the BLOCK. */
9000 DECL_INITIAL (decl) = error_mark_node;
9002 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9003 TREE_NOTHROW (decl) = 1;
9005 /* Caller will do the rest of this. */
9006 if (check < 0)
9007 return decl;
9009 if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
9010 DECL_CONSTRUCTOR_P (decl) = 1;
9012 /* Function gets the ugly name, field gets the nice one. This call
9013 may change the type of the function (because of default
9014 parameters)! */
9015 if (ctype != NULL_TREE)
9016 grokclassfn (ctype, decl, flags, quals);
9018 decl = check_explicit_specialization (orig_declarator, decl,
9019 template_count,
9020 2 * (funcdef_flag != 0) +
9021 4 * (friendp != 0));
9022 if (decl == error_mark_node)
9023 return NULL_TREE;
9025 if (ctype != NULL_TREE
9026 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9027 && check)
9029 tree old_decl;
9031 old_decl = check_classfn (ctype, decl);
9033 if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9034 /* Because grokfndecl is always supposed to return a
9035 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9036 here. We depend on our callers to figure out that its
9037 really a template that's being returned. */
9038 old_decl = DECL_TEMPLATE_RESULT (old_decl);
9040 if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9041 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9043 /* Remove the `this' parm added by grokclassfn.
9044 XXX Isn't this done in start_function, too? */
9045 revert_static_member_fn (decl);
9046 last_function_parms = TREE_CHAIN (last_function_parms);
9048 if (old_decl && DECL_ARTIFICIAL (old_decl))
9049 error ("definition of implicitly-declared `%D'", old_decl);
9051 if (old_decl)
9053 /* Since we've smashed OLD_DECL to its
9054 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9055 if (TREE_CODE (decl) == TEMPLATE_DECL)
9056 decl = DECL_TEMPLATE_RESULT (decl);
9058 /* Attempt to merge the declarations. This can fail, in
9059 the case of some invalid specialization declarations. */
9060 push_scope (ctype);
9061 if (!duplicate_decls (decl, old_decl))
9062 error ("no `%#D' member function declared in class `%T'",
9063 decl, ctype);
9064 pop_scope (ctype);
9065 return old_decl;
9069 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9070 return NULL_TREE;
9072 if (ctype == NULL_TREE || check)
9073 return decl;
9075 if (virtualp)
9076 DECL_VIRTUAL_P (decl) = 1;
9078 return decl;
9081 /* Create a VAR_DECL named NAME with the indicated TYPE.
9083 If SCOPE is non-NULL, it is the class type or namespace containing
9084 the variable. If SCOPE is NULL, the variable should is created in
9085 the innermost enclosings scope. */
9087 static tree
9088 grokvardecl (tree type,
9089 tree name,
9090 RID_BIT_TYPE * specbits_in,
9091 int initialized,
9092 int constp,
9093 tree scope)
9095 tree decl;
9096 RID_BIT_TYPE specbits;
9098 my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE,
9099 20020808);
9101 specbits = *specbits_in;
9103 /* Compute the scope in which to place the variable. */
9104 if (!scope)
9106 /* An explicit "extern" specifier indicates a namespace-scope
9107 variable. */
9108 if (RIDBIT_SETP (RID_EXTERN, specbits))
9109 scope = current_namespace;
9110 else if (!at_function_scope_p ())
9112 scope = current_scope ();
9113 if (!scope)
9114 scope = current_namespace;
9118 if (scope
9119 && (/* If the variable is a namespace-scope variable declared in a
9120 template, we need DECL_LANG_SPECIFIC. */
9121 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9122 /* Similarly for namespace-scope variables with language linkage
9123 other than C++. */
9124 || (TREE_CODE (scope) == NAMESPACE_DECL
9125 && current_lang_name != lang_name_cplusplus)
9126 /* Similarly for static data members. */
9127 || TYPE_P (scope)))
9128 decl = build_lang_decl (VAR_DECL, name, type);
9129 else
9130 decl = build_decl (VAR_DECL, name, type);
9132 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9133 set_decl_namespace (decl, scope, 0);
9134 else
9135 DECL_CONTEXT (decl) = scope;
9137 if (name && scope && current_lang_name != lang_name_c)
9138 /* We can't mangle lazily here because we don't have any
9139 way to recover whether or not a variable was `extern
9140 "C"' later. */
9141 mangle_decl (decl);
9143 if (RIDBIT_SETP (RID_EXTERN, specbits))
9145 DECL_THIS_EXTERN (decl) = 1;
9146 DECL_EXTERNAL (decl) = !initialized;
9149 /* In class context, static means one per class,
9150 public access, and static storage. */
9151 if (DECL_CLASS_SCOPE_P (decl))
9153 TREE_PUBLIC (decl) = 1;
9154 TREE_STATIC (decl) = 1;
9155 DECL_EXTERNAL (decl) = 0;
9157 /* At top level, either `static' or no s.c. makes a definition
9158 (perhaps tentative), and absence of `static' makes it public. */
9159 else if (toplevel_bindings_p ())
9161 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9162 && (DECL_THIS_EXTERN (decl) || ! constp));
9163 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9165 /* Not at top level, only `static' makes a static definition. */
9166 else
9168 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9169 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9172 if (RIDBIT_SETP (RID_THREAD, specbits))
9174 if (targetm.have_tls)
9175 DECL_THREAD_LOCAL (decl) = 1;
9176 else
9177 /* A mere warning is sure to result in improper semantics
9178 at runtime. Don't bother to allow this to compile. */
9179 error ("thread-local storage not supported for this target");
9182 if (TREE_PUBLIC (decl))
9184 /* [basic.link]: A name with no linkage (notably, the name of a class
9185 or enumeration declared in a local scope) shall not be used to
9186 declare an entity with linkage.
9188 Only check this for public decls for now. */
9189 tree t = no_linkage_check (TREE_TYPE (decl));
9190 if (t)
9192 if (TYPE_ANONYMOUS_P (t))
9193 /* Ignore for now; `enum { foo } e' is pretty common. */;
9194 else
9195 pedwarn ("non-local variable `%#D' uses local type `%T'",
9196 decl, t);
9200 return decl;
9203 /* Create and return a canonical pointer to member function type, for
9204 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9206 tree
9207 build_ptrmemfunc_type (tree type)
9209 tree field, fields;
9210 tree t;
9211 tree unqualified_variant = NULL_TREE;
9213 if (type == error_mark_node)
9214 return type;
9216 /* If a canonical type already exists for this type, use it. We use
9217 this method instead of type_hash_canon, because it only does a
9218 simple equality check on the list of field members. */
9220 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9221 return t;
9223 /* Make sure that we always have the unqualified pointer-to-member
9224 type first. */
9225 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9226 unqualified_variant
9227 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9229 t = make_aggr_type (RECORD_TYPE);
9230 /* Let the front-end know this is a pointer to member function... */
9231 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9232 /* ... and not really an aggregate. */
9233 SET_IS_AGGR_TYPE (t, 0);
9235 field = build_decl (FIELD_DECL, pfn_identifier, type);
9236 fields = field;
9238 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9239 TREE_CHAIN (field) = fields;
9240 fields = field;
9242 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9244 /* Zap out the name so that the back-end will give us the debugging
9245 information for this anonymous RECORD_TYPE. */
9246 TYPE_NAME (t) = NULL_TREE;
9248 /* If this is not the unqualified form of this pointer-to-member
9249 type, set the TYPE_MAIN_VARIANT for this type to be the
9250 unqualified type. Since they are actually RECORD_TYPEs that are
9251 not variants of each other, we must do this manually. */
9252 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9254 t = build_qualified_type (t, cp_type_quals (type));
9255 TYPE_MAIN_VARIANT (t) = unqualified_variant;
9256 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9257 TYPE_NEXT_VARIANT (unqualified_variant) = t;
9260 /* Cache this pointer-to-member type so that we can find it again
9261 later. */
9262 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9264 /* Seems to be wanted. */
9265 CLASSTYPE_GOT_SEMICOLON (t) = 1;
9267 return t;
9270 /* Create and return a pointer to data member type. */
9272 tree
9273 build_ptrmem_type (tree class_type, tree member_type)
9275 return build_pointer_type (build_offset_type (class_type, member_type));
9278 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9279 Check to see that the definition is valid. Issue appropriate error
9280 messages. Return 1 if the definition is particularly bad, or 0
9281 otherwise. */
9284 check_static_variable_definition (tree decl, tree type)
9286 /* Motion 10 at San Diego: If a static const integral data member is
9287 initialized with an integral constant expression, the initializer
9288 may appear either in the declaration (within the class), or in
9289 the definition, but not both. If it appears in the class, the
9290 member is a member constant. The file-scope definition is always
9291 required. */
9292 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9294 error ("invalid in-class initialization of static data member of non-integral type `%T'",
9295 type);
9296 /* If we just return the declaration, crashes will sometimes
9297 occur. We therefore return void_type_node, as if this was a
9298 friend declaration, to cause callers to completely ignore
9299 this declaration. */
9300 return 1;
9302 else if (!CP_TYPE_CONST_P (type))
9303 error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9304 decl);
9305 else if (pedantic && !INTEGRAL_TYPE_P (type))
9306 pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9308 return 0;
9311 /* Given the SIZE (i.e., number of elements) in an array, compute an
9312 appropriate index type for the array. If non-NULL, NAME is the
9313 name of the thing being declared. */
9315 tree
9316 compute_array_index_type (tree name, tree size)
9318 tree itype;
9320 /* If this involves a template parameter, it will be a constant at
9321 instantiation time, but we don't know what the value is yet.
9322 Even if no template parameters are involved, we may an expression
9323 that is not a constant; we don't even simplify `1 + 2' when
9324 processing a template. */
9325 if (processing_template_decl)
9327 /* Resolve a qualified reference to an enumerator or static
9328 const data member of ours. */
9329 if (TREE_CODE (size) == SCOPE_REF
9330 && TREE_OPERAND (size, 0) == current_class_type)
9332 tree t = lookup_field (current_class_type,
9333 TREE_OPERAND (size, 1), 0, 0);
9334 if (t)
9335 size = t;
9338 return build_index_type (build_min (MINUS_EXPR, sizetype,
9339 size, integer_one_node));
9342 /* The size might be the result of a cast. */
9343 STRIP_TYPE_NOPS (size);
9345 /* It might be a const variable or enumeration constant. */
9346 size = decl_constant_value (size);
9348 /* The array bound must be an integer type. */
9349 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9350 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9351 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9353 if (name)
9354 error ("size of array `%D' has non-integer type", name);
9355 else
9356 error ("size of array has non-integer type");
9357 size = integer_one_node;
9360 /* Normally, the array-bound will be a constant. */
9361 if (TREE_CODE (size) == INTEGER_CST)
9363 /* Check to see if the array bound overflowed. Make that an
9364 error, no matter how generous we're being. */
9365 int old_flag_pedantic_errors = flag_pedantic_errors;
9366 int old_pedantic = pedantic;
9367 pedantic = flag_pedantic_errors = 1;
9368 constant_expression_warning (size);
9369 pedantic = old_pedantic;
9370 flag_pedantic_errors = old_flag_pedantic_errors;
9372 /* An array must have a positive number of elements. */
9373 if (INT_CST_LT (size, integer_zero_node))
9375 if (name)
9376 error ("size of array `%D' is negative", name);
9377 else
9378 error ("size of array is negative");
9379 size = integer_one_node;
9381 /* Except that an extension we allow zero-sized arrays. We
9382 always allow them in system headers because glibc uses
9383 them. */
9384 else if (integer_zerop (size) && pedantic && !in_system_header)
9386 if (name)
9387 pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9388 else
9389 pedwarn ("ISO C++ forbids zero-size array");
9392 else if (TREE_CONSTANT (size))
9394 /* `(int) &fn' is not a valid array bound. */
9395 if (name)
9396 error ("size of array `%D' is not an integral constant-expression",
9397 name);
9398 else
9399 error ("size of array is not an integral constant-expression");
9402 /* Compute the index of the largest element in the array. It is
9403 one less than the number of elements in the array. */
9404 itype
9405 = fold (cp_build_binary_op (MINUS_EXPR,
9406 cp_convert (ssizetype, size),
9407 cp_convert (ssizetype,
9408 integer_one_node)));
9410 /* Check for variable-sized arrays. We allow such things as an
9411 extension, even though they are not allowed in ANSI/ISO C++. */
9412 if (!TREE_CONSTANT (itype))
9414 if (pedantic)
9416 if (name)
9417 pedwarn ("ISO C++ forbids variable-size array `%D'",
9418 name);
9419 else
9420 pedwarn ("ISO C++ forbids variable-size array");
9423 /* Create a variable-sized array index type. */
9424 itype = variable_size (itype);
9426 /* Make sure that there was no overflow when creating to a signed
9427 index type. (For example, on a 32-bit machine, an array with
9428 size 2^32 - 1 is too big.) */
9429 else if (TREE_OVERFLOW (itype))
9431 error ("overflow in array dimension");
9432 TREE_OVERFLOW (itype) = 0;
9435 /* Create and return the appropriate index type. */
9436 return build_index_type (itype);
9439 /* Returns the scope (if any) in which the entity declared by
9440 DECLARATOR will be located. If the entity was declared with an
9441 unqualified name, NULL_TREE is returned. */
9443 tree
9444 get_scope_of_declarator (tree declarator)
9446 if (!declarator)
9447 return NULL_TREE;
9449 switch (TREE_CODE (declarator))
9451 case CALL_EXPR:
9452 case ARRAY_REF:
9453 case INDIRECT_REF:
9454 case ADDR_EXPR:
9455 /* For any of these, the main declarator is the first operand. */
9456 return get_scope_of_declarator (TREE_OPERAND
9457 (declarator, 0));
9459 case SCOPE_REF:
9460 /* For a pointer-to-member, continue descending. */
9461 if (TREE_CODE (TREE_OPERAND (declarator, 1))
9462 == INDIRECT_REF)
9463 return get_scope_of_declarator (TREE_OPERAND
9464 (declarator, 1));
9465 /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9466 which the declaration occurs is the first operand. */
9467 return TREE_OPERAND (declarator, 0);
9469 case TREE_LIST:
9470 /* Attributes to be applied. The declarator is TREE_VALUE. */
9471 return get_scope_of_declarator (TREE_VALUE (declarator));
9473 default:
9474 /* Otherwise, we have a declarator-id which is not a qualified
9475 name; the entity will be declared in the current scope. */
9476 return NULL_TREE;
9480 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9481 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9482 with this type. */
9484 static tree
9485 create_array_type_for_decl (tree name, tree type, tree size)
9487 tree itype = NULL_TREE;
9488 const char* error_msg;
9490 /* If things have already gone awry, bail now. */
9491 if (type == error_mark_node || size == error_mark_node)
9492 return error_mark_node;
9494 /* Assume that everything will go OK. */
9495 error_msg = NULL;
9497 /* There are some types which cannot be array elements. */
9498 switch (TREE_CODE (type))
9500 case VOID_TYPE:
9501 error_msg = "array of void";
9502 break;
9504 case FUNCTION_TYPE:
9505 error_msg = "array of functions";
9506 break;
9508 case REFERENCE_TYPE:
9509 error_msg = "array of references";
9510 break;
9512 case OFFSET_TYPE:
9513 error_msg = "array of data members";
9514 break;
9516 case METHOD_TYPE:
9517 error_msg = "array of function members";
9518 break;
9520 default:
9521 break;
9524 /* If something went wrong, issue an error-message and return. */
9525 if (error_msg)
9527 if (name)
9528 error ("declaration of `%D' as %s", name, error_msg);
9529 else
9530 error ("creating %s", error_msg);
9532 return error_mark_node;
9535 /* [dcl.array]
9537 The constant expressions that specify the bounds of the arrays
9538 can be omitted only for the first member of the sequence. */
9539 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9541 if (name)
9542 error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9543 name);
9544 else
9545 error ("multidimensional array must have bounds for all dimensions except the first");
9547 return error_mark_node;
9550 /* Figure out the index type for the array. */
9551 if (size)
9552 itype = compute_array_index_type (name, size);
9554 return build_cplus_array_type (type, itype);
9557 /* Check that it's OK to declare a function with the indicated TYPE.
9558 SFK indicates the kind of special function (if any) that this
9559 function is. OPTYPE is the type given in a conversion operator
9560 declaration. Returns the actual return type of the function; that
9561 may be different than TYPE if an error occurs, or for certain
9562 special functions. */
9564 static tree
9565 check_special_function_return_type (special_function_kind sfk,
9566 tree type,
9567 tree optype)
9569 switch (sfk)
9571 case sfk_constructor:
9572 if (type)
9573 error ("return type specification for constructor invalid");
9575 type = void_type_node;
9576 break;
9578 case sfk_destructor:
9579 if (type)
9580 error ("return type specification for destructor invalid");
9581 type = void_type_node;
9582 break;
9584 case sfk_conversion:
9585 if (type && !same_type_p (type, optype))
9586 error ("operator `%T' declared to return `%T'", optype, type);
9587 else if (type)
9588 pedwarn ("return type specified for `operator %T'", optype);
9589 type = optype;
9590 break;
9592 default:
9593 abort ();
9594 break;
9597 return type;
9600 /* Given declspecs and a declarator (abstract or otherwise), determine
9601 the name and type of the object declared and construct a DECL node
9602 for it.
9604 DECLSPECS is a chain of tree_list nodes whose value fields
9605 are the storage classes and type specifiers.
9607 DECL_CONTEXT says which syntactic context this declaration is in:
9608 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9609 FUNCDEF for a function definition. Like NORMAL but a few different
9610 error messages in each case. Return value may be zero meaning
9611 this definition is too screwy to try to parse.
9612 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9613 handle member functions (which have FIELD context).
9614 Return value may be zero meaning this definition is too screwy to
9615 try to parse.
9616 PARM for a parameter declaration (either within a function prototype
9617 or before a function body). Make a PARM_DECL, or return void_type_node.
9618 CATCHPARM for a parameter declaration before a catch clause.
9619 TYPENAME if for a typename (in a cast or sizeof).
9620 Don't make a DECL node; just return the ..._TYPE node.
9621 FIELD for a struct or union field; make a FIELD_DECL.
9622 BITFIELD for a field with specified width.
9623 INITIALIZED is 1 if the decl has an initializer.
9625 ATTRLIST is a pointer to the list of attributes, which may be NULL
9626 if there are none; *ATTRLIST may be modified if attributes from inside
9627 the declarator should be applied to the declaration.
9629 When this function is called, scoping variables (such as
9630 CURRENT_CLASS_TYPE) should reflect the scope in which the
9631 declaration occurs, not the scope in which the new declaration will
9632 be placed. For example, on:
9634 void S::f() { ... }
9636 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9637 should not be `S'. */
9639 tree
9640 grokdeclarator (tree declarator,
9641 tree declspecs,
9642 enum decl_context decl_context,
9643 int initialized,
9644 tree* attrlist)
9646 RID_BIT_TYPE specbits;
9647 int nclasses = 0;
9648 tree spec;
9649 tree type = NULL_TREE;
9650 int longlong = 0;
9651 int type_quals;
9652 int virtualp, explicitp, friendp, inlinep, staticp;
9653 int explicit_int = 0;
9654 int explicit_char = 0;
9655 int defaulted_int = 0;
9656 int extern_langp = 0;
9657 tree dependant_name = NULL_TREE;
9659 tree typedef_decl = NULL_TREE;
9660 const char *name;
9661 tree typedef_type = NULL_TREE;
9662 int funcdef_flag = 0;
9663 enum tree_code innermost_code = ERROR_MARK;
9664 int bitfield = 0;
9665 #if 0
9666 /* See the code below that used this. */
9667 tree decl_attr = NULL_TREE;
9668 #endif
9670 /* Keep track of what sort of function is being processed
9671 so that we can warn about default return values, or explicit
9672 return values which do not match prescribed defaults. */
9673 special_function_kind sfk = sfk_none;
9675 tree dname = NULL_TREE;
9676 tree ctype = current_class_type;
9677 tree ctor_return_type = NULL_TREE;
9678 enum overload_flags flags = NO_SPECIAL;
9679 tree quals = NULL_TREE;
9680 tree raises = NULL_TREE;
9681 int template_count = 0;
9682 tree in_namespace = NULL_TREE;
9683 tree returned_attrs = NULL_TREE;
9684 tree scope = NULL_TREE;
9686 RIDBIT_RESET_ALL (specbits);
9687 if (decl_context == FUNCDEF)
9688 funcdef_flag = 1, decl_context = NORMAL;
9689 else if (decl_context == MEMFUNCDEF)
9690 funcdef_flag = -1, decl_context = FIELD;
9691 else if (decl_context == BITFIELD)
9692 bitfield = 1, decl_context = FIELD;
9694 /* Look inside a declarator for the name being declared
9695 and get it as a string, for an error message. */
9697 tree *next = &declarator;
9698 register tree decl;
9699 name = NULL;
9701 while (next && *next)
9703 decl = *next;
9704 switch (TREE_CODE (decl))
9706 case TREE_LIST:
9707 /* For attributes. */
9708 next = &TREE_VALUE (decl);
9709 break;
9711 case COND_EXPR:
9712 ctype = NULL_TREE;
9713 next = &TREE_OPERAND (decl, 0);
9714 break;
9716 case BIT_NOT_EXPR: /* For C++ destructors! */
9718 tree name = TREE_OPERAND (decl, 0);
9719 tree rename = NULL_TREE;
9721 my_friendly_assert (flags == NO_SPECIAL, 152);
9722 flags = DTOR_FLAG;
9723 sfk = sfk_destructor;
9724 if (TYPE_P (name))
9725 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9726 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9727 if (ctype == NULL_TREE)
9729 if (current_class_type == NULL_TREE)
9731 error ("destructors must be member functions");
9732 flags = NO_SPECIAL;
9734 else
9736 tree t = constructor_name (current_class_type);
9737 if (t != name)
9738 rename = t;
9741 else
9743 tree t = constructor_name (ctype);
9744 if (t != name)
9745 rename = t;
9748 if (rename)
9750 error ("destructor `%T' must match class name `%T'",
9751 name, rename);
9752 TREE_OPERAND (decl, 0) = rename;
9754 next = &name;
9756 break;
9758 case ADDR_EXPR: /* C++ reference declaration */
9759 /* Fall through. */
9760 case ARRAY_REF:
9761 case INDIRECT_REF:
9762 ctype = NULL_TREE;
9763 innermost_code = TREE_CODE (decl);
9764 next = &TREE_OPERAND (decl, 0);
9765 break;
9767 case CALL_EXPR:
9768 innermost_code = TREE_CODE (decl);
9769 if (decl_context == FIELD && ctype == NULL_TREE)
9770 ctype = current_class_type;
9771 if (ctype
9772 && TREE_OPERAND (decl, 0)
9773 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9774 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9775 ctype)))
9776 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9777 next = &TREE_OPERAND (decl, 0);
9778 decl = *next;
9779 if (ctype != NULL_TREE
9780 && decl != NULL_TREE && flags != DTOR_FLAG
9781 && constructor_name_p (decl, ctype))
9783 sfk = sfk_constructor;
9784 ctor_return_type = ctype;
9786 ctype = NULL_TREE;
9787 break;
9789 case TEMPLATE_ID_EXPR:
9791 tree fns = TREE_OPERAND (decl, 0);
9793 if (TREE_CODE (fns) == LOOKUP_EXPR)
9794 fns = TREE_OPERAND (fns, 0);
9796 dname = fns;
9797 if (TREE_CODE (dname) == COMPONENT_REF)
9798 dname = TREE_OPERAND (dname, 1);
9799 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9801 my_friendly_assert (is_overloaded_fn (dname),
9802 19990331);
9803 dname = DECL_NAME (get_first_fn (dname));
9806 /* Fall through. */
9808 case IDENTIFIER_NODE:
9809 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9810 dname = decl;
9812 next = 0;
9814 if (C_IS_RESERVED_WORD (dname))
9816 error ("declarator-id missing; using reserved word `%D'",
9817 dname);
9818 name = IDENTIFIER_POINTER (dname);
9820 else if (!IDENTIFIER_TYPENAME_P (dname))
9821 name = IDENTIFIER_POINTER (dname);
9822 else
9824 my_friendly_assert (flags == NO_SPECIAL, 154);
9825 flags = TYPENAME_FLAG;
9826 ctor_return_type = TREE_TYPE (dname);
9827 sfk = sfk_conversion;
9828 if (IDENTIFIER_GLOBAL_VALUE (dname)
9829 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9830 == TYPE_DECL))
9831 name = IDENTIFIER_POINTER (dname);
9832 else
9833 name = "<invalid operator>";
9835 break;
9837 /* C++ extension */
9838 case SCOPE_REF:
9840 /* Perform error checking, and decide on a ctype. */
9841 tree cname = TREE_OPERAND (decl, 0);
9842 if (cname == NULL_TREE)
9843 ctype = NULL_TREE;
9844 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9846 ctype = NULL_TREE;
9847 in_namespace = TREE_OPERAND (decl, 0);
9849 else if (! is_aggr_type (cname, 1))
9850 ctype = NULL_TREE;
9851 /* Must test TREE_OPERAND (decl, 1), in case user gives
9852 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9853 else if (TREE_OPERAND (decl, 1)
9854 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9855 ctype = cname;
9856 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9857 || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9859 /* This might be declaring a member of a template
9860 parm to be a friend. */
9861 ctype = cname;
9862 dependant_name = TREE_OPERAND (decl, 1);
9864 else if (ctype == NULL_TREE)
9865 ctype = cname;
9866 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9868 else
9870 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9872 error ("type `%T' is not derived from type `%T'",
9873 cname, ctype);
9874 ctype = NULL_TREE;
9876 else
9877 ctype = cname;
9880 /* It is valid to write:
9882 class C { void f(); };
9883 typedef C D;
9884 void D::f();
9886 The standard is not clear about whether `typedef const C D' is
9887 legal; as of 2002-09-15 the committee is considering
9888 that question. EDG 3.0 allows that syntax.
9889 Therefore, we do as well. */
9890 if (ctype)
9891 ctype = TYPE_MAIN_VARIANT (ctype);
9892 /* Update the declarator so that when we process it
9893 again the correct type is present. */
9894 TREE_OPERAND (decl, 0) = ctype;
9896 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9897 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9898 ctype))
9899 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9900 next = &TREE_OPERAND (decl, 1);
9901 decl = *next;
9902 if (ctype)
9904 if (TREE_CODE (decl) == IDENTIFIER_NODE
9905 && constructor_name_p (decl, ctype))
9907 sfk = sfk_constructor;
9908 ctor_return_type = ctype;
9910 else if (TREE_CODE (decl) == BIT_NOT_EXPR
9911 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9912 && constructor_name_p (TREE_OPERAND (decl, 0),
9913 ctype))
9915 sfk = sfk_destructor;
9916 ctor_return_type = ctype;
9917 flags = DTOR_FLAG;
9918 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9919 next = &TREE_OPERAND (decl, 0);
9923 break;
9925 case ERROR_MARK:
9926 next = 0;
9927 break;
9929 case TYPE_DECL:
9930 /* Parse error puts this typespec where
9931 a declarator should go. */
9932 error ("`%T' specified as declarator-id", DECL_NAME (decl));
9933 if (TREE_TYPE (decl) == current_class_type)
9934 error (" perhaps you want `%T' for a constructor",
9935 current_class_name);
9936 dname = DECL_NAME (decl);
9937 name = IDENTIFIER_POINTER (dname);
9939 /* Avoid giving two errors for this. */
9940 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9942 declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
9943 *next = dname;
9944 next = 0;
9945 break;
9947 case BASELINK:
9948 next = &BASELINK_FUNCTIONS (decl);
9949 break;
9951 case TEMPLATE_DECL:
9952 /* Sometimes, we see a template-name used as part of a
9953 decl-specifier like in
9954 std::allocator alloc;
9955 Handle that gracefully. */
9956 error ("invalid use of template-name '%E' in a declarator", decl);
9957 return error_mark_node;
9958 break;
9960 default:
9961 my_friendly_assert (0, 20020917);
9966 /* A function definition's declarator must have the form of
9967 a function declarator. */
9969 if (funcdef_flag && innermost_code != CALL_EXPR)
9970 return 0;
9972 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9973 && innermost_code != CALL_EXPR
9974 && ! (ctype && declspecs == NULL_TREE))
9976 error ("declaration of `%D' as non-function", dname);
9977 return void_type_node;
9980 /* Anything declared one level down from the top level
9981 must be one of the parameters of a function
9982 (because the body is at least two levels down). */
9984 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9985 by not allowing C++ class definitions to specify their parameters
9986 with xdecls (must be spec.d in the parmlist).
9988 Since we now wait to push a class scope until we are sure that
9989 we are in a legitimate method context, we must set oldcname
9990 explicitly (since current_class_name is not yet alive).
9992 We also want to avoid calling this a PARM if it is in a namespace. */
9994 if (decl_context == NORMAL && !toplevel_bindings_p ())
9996 struct cp_binding_level *b = current_binding_level;
9997 current_binding_level = b->level_chain;
9998 if (current_binding_level != 0 && toplevel_bindings_p ())
9999 decl_context = PARM;
10000 current_binding_level = b;
10003 if (name == NULL)
10004 name = decl_context == PARM ? "parameter" : "type name";
10006 /* Look through the decl specs and record which ones appear.
10007 Some typespecs are defined as built-in typenames.
10008 Others, the ones that are modifiers of other types,
10009 are represented by bits in SPECBITS: set the bits for
10010 the modifiers that appear. Storage class keywords are also in SPECBITS.
10012 If there is a typedef name or a type, store the type in TYPE.
10013 This includes builtin typedefs such as `int'.
10015 Set EXPLICIT_INT if the type is `int' or `char' and did not
10016 come from a user typedef.
10018 Set LONGLONG if `long' is mentioned twice.
10020 For C++, constructors and destructors have their own fast treatment. */
10022 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10024 register int i;
10025 register tree id;
10027 /* Certain parse errors slip through. For example,
10028 `int class;' is not caught by the parser. Try
10029 weakly to recover here. */
10030 if (TREE_CODE (spec) != TREE_LIST)
10031 return 0;
10033 id = TREE_VALUE (spec);
10035 /* If the entire declaration is itself tagged as deprecated then
10036 suppress reports of deprecated items. */
10037 if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10039 if (deprecated_state != DEPRECATED_SUPPRESS)
10040 warn_deprecated_use (id);
10043 if (TREE_CODE (id) == IDENTIFIER_NODE)
10045 if (id == ridpointers[(int) RID_INT]
10046 || id == ridpointers[(int) RID_CHAR]
10047 || id == ridpointers[(int) RID_BOOL]
10048 || id == ridpointers[(int) RID_WCHAR])
10050 if (type)
10052 if (id == ridpointers[(int) RID_BOOL])
10053 error ("`bool' is now a keyword");
10054 else
10055 error ("extraneous `%T' ignored", id);
10057 else
10059 if (id == ridpointers[(int) RID_INT])
10060 explicit_int = 1;
10061 else if (id == ridpointers[(int) RID_CHAR])
10062 explicit_char = 1;
10063 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10065 goto found;
10067 /* C++ aggregate types. */
10068 if (IDENTIFIER_HAS_TYPE_VALUE (id))
10070 if (type)
10071 error ("multiple declarations `%T' and `%T'", type, id);
10072 else
10073 type = IDENTIFIER_TYPE_VALUE (id);
10074 goto found;
10077 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10079 if (ridpointers[i] == id)
10081 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10083 if (pedantic && ! in_system_header && warn_long_long)
10084 pedwarn ("ISO C++ does not support `long long'");
10085 if (longlong)
10086 error ("`long long long' is too long for GCC");
10087 else
10088 longlong = 1;
10090 else if (RIDBIT_SETP (i, specbits))
10091 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10093 /* Diagnose "__thread extern" or "__thread static". */
10094 if (RIDBIT_SETP (RID_THREAD, specbits))
10096 if (i == (int)RID_EXTERN)
10097 error ("`__thread' before `extern'");
10098 else if (i == (int)RID_STATIC)
10099 error ("`__thread' before `static'");
10102 if (i == (int)RID_EXTERN
10103 && TREE_PURPOSE (spec) == error_mark_node)
10104 /* This extern was part of a language linkage. */
10105 extern_langp = 1;
10107 RIDBIT_SET (i, specbits);
10108 goto found;
10112 else if (TREE_CODE (id) == TYPE_DECL)
10114 if (type)
10115 error ("multiple declarations `%T' and `%T'", type,
10116 TREE_TYPE (id));
10117 else
10119 type = TREE_TYPE (id);
10120 TREE_VALUE (spec) = type;
10121 typedef_decl = id;
10123 goto found;
10125 if (type)
10126 error ("two or more data types in declaration of `%s'", name);
10127 else if (TREE_CODE (id) == IDENTIFIER_NODE)
10129 register tree t = lookup_name (id, 1);
10130 if (!t || TREE_CODE (t) != TYPE_DECL)
10131 error ("`%s' fails to be a typedef or built in type",
10132 IDENTIFIER_POINTER (id));
10133 else
10135 type = TREE_TYPE (t);
10136 typedef_decl = t;
10139 else if (id != error_mark_node)
10140 /* Can't change CLASS nodes into RECORD nodes here! */
10141 type = id;
10143 found: ;
10146 #if 0
10147 /* See the code below that used this. */
10148 if (typedef_decl)
10149 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10150 #endif
10151 typedef_type = type;
10153 /* No type at all: default to `int', and set DEFAULTED_INT
10154 because it was not a user-defined typedef. */
10156 if (type == NULL_TREE
10157 && (RIDBIT_SETP (RID_SIGNED, specbits)
10158 || RIDBIT_SETP (RID_UNSIGNED, specbits)
10159 || RIDBIT_SETP (RID_LONG, specbits)
10160 || RIDBIT_SETP (RID_SHORT, specbits)))
10162 /* These imply 'int'. */
10163 type = integer_type_node;
10164 defaulted_int = 1;
10167 if (sfk != sfk_none)
10168 type = check_special_function_return_type (sfk, type,
10169 ctor_return_type);
10170 else if (type == NULL_TREE)
10172 int is_main;
10174 explicit_int = -1;
10176 /* We handle `main' specially here, because 'main () { }' is so
10177 common. With no options, it is allowed. With -Wreturn-type,
10178 it is a warning. It is only an error with -pedantic-errors. */
10179 is_main = (funcdef_flag
10180 && dname && MAIN_NAME_P (dname)
10181 && ctype == NULL_TREE
10182 && in_namespace == NULL_TREE
10183 && current_namespace == global_namespace);
10185 if (in_system_header || flag_ms_extensions)
10186 /* Allow it, sigh. */;
10187 else if (pedantic || ! is_main)
10188 pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10189 name);
10190 else if (warn_return_type)
10191 warning ("ISO C++ forbids declaration of `%s' with no type",
10192 name);
10194 type = integer_type_node;
10197 ctype = NULL_TREE;
10199 /* Now process the modifiers that were specified
10200 and check for invalid combinations. */
10202 /* Long double is a special combination. */
10204 if (RIDBIT_SETP (RID_LONG, specbits)
10205 && TYPE_MAIN_VARIANT (type) == double_type_node)
10207 RIDBIT_RESET (RID_LONG, specbits);
10208 type = build_qualified_type (long_double_type_node,
10209 cp_type_quals (type));
10212 /* Check all other uses of type modifiers. */
10214 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10215 || RIDBIT_SETP (RID_SIGNED, specbits)
10216 || RIDBIT_SETP (RID_LONG, specbits)
10217 || RIDBIT_SETP (RID_SHORT, specbits))
10219 int ok = 0;
10221 if (TREE_CODE (type) == REAL_TYPE)
10222 error ("short, signed or unsigned invalid for `%s'", name);
10223 else if (TREE_CODE (type) != INTEGER_TYPE)
10224 error ("long, short, signed or unsigned invalid for `%s'", name);
10225 else if (RIDBIT_SETP (RID_LONG, specbits)
10226 && RIDBIT_SETP (RID_SHORT, specbits))
10227 error ("long and short specified together for `%s'", name);
10228 else if ((RIDBIT_SETP (RID_LONG, specbits)
10229 || RIDBIT_SETP (RID_SHORT, specbits))
10230 && explicit_char)
10231 error ("long or short specified with char for `%s'", name);
10232 else if ((RIDBIT_SETP (RID_LONG, specbits)
10233 || RIDBIT_SETP (RID_SHORT, specbits))
10234 && TREE_CODE (type) == REAL_TYPE)
10235 error ("long or short specified with floating type for `%s'", name);
10236 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10237 && RIDBIT_SETP (RID_UNSIGNED, specbits))
10238 error ("signed and unsigned given together for `%s'", name);
10239 else
10241 ok = 1;
10242 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10244 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10245 name);
10246 if (flag_pedantic_errors)
10247 ok = 0;
10251 /* Discard the type modifiers if they are invalid. */
10252 if (! ok)
10254 RIDBIT_RESET (RID_UNSIGNED, specbits);
10255 RIDBIT_RESET (RID_SIGNED, specbits);
10256 RIDBIT_RESET (RID_LONG, specbits);
10257 RIDBIT_RESET (RID_SHORT, specbits);
10258 longlong = 0;
10262 if (RIDBIT_SETP (RID_COMPLEX, specbits)
10263 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10265 error ("complex invalid for `%s'", name);
10266 RIDBIT_RESET (RID_COMPLEX, specbits);
10269 /* Decide whether an integer type is signed or not.
10270 Optionally treat bitfields as signed by default. */
10271 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10272 /* [class.bit]
10274 It is implementation-defined whether a plain (neither
10275 explicitly signed or unsigned) char, short, int, or long
10276 bit-field is signed or unsigned.
10278 Naturally, we extend this to long long as well. Note that
10279 this does not include wchar_t. */
10280 || (bitfield && !flag_signed_bitfields
10281 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10282 /* A typedef for plain `int' without `signed' can be
10283 controlled just like plain `int', but a typedef for
10284 `signed int' cannot be so controlled. */
10285 && !(typedef_decl
10286 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10287 && (TREE_CODE (type) == INTEGER_TYPE
10288 || TREE_CODE (type) == CHAR_TYPE)
10289 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10291 if (longlong)
10292 type = long_long_unsigned_type_node;
10293 else if (RIDBIT_SETP (RID_LONG, specbits))
10294 type = long_unsigned_type_node;
10295 else if (RIDBIT_SETP (RID_SHORT, specbits))
10296 type = short_unsigned_type_node;
10297 else if (type == char_type_node)
10298 type = unsigned_char_type_node;
10299 else if (typedef_decl)
10300 type = c_common_unsigned_type (type);
10301 else
10302 type = unsigned_type_node;
10304 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10305 && type == char_type_node)
10306 type = signed_char_type_node;
10307 else if (longlong)
10308 type = long_long_integer_type_node;
10309 else if (RIDBIT_SETP (RID_LONG, specbits))
10310 type = long_integer_type_node;
10311 else if (RIDBIT_SETP (RID_SHORT, specbits))
10312 type = short_integer_type_node;
10314 if (RIDBIT_SETP (RID_COMPLEX, specbits))
10316 /* If we just have "complex", it is equivalent to
10317 "complex double", but if any modifiers at all are specified it is
10318 the complex form of TYPE. E.g, "complex short" is
10319 "complex short int". */
10321 if (defaulted_int && ! longlong
10322 && ! (RIDBIT_SETP (RID_LONG, specbits)
10323 || RIDBIT_SETP (RID_SHORT, specbits)
10324 || RIDBIT_SETP (RID_SIGNED, specbits)
10325 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10326 type = complex_double_type_node;
10327 else if (type == integer_type_node)
10328 type = complex_integer_type_node;
10329 else if (type == float_type_node)
10330 type = complex_float_type_node;
10331 else if (type == double_type_node)
10332 type = complex_double_type_node;
10333 else if (type == long_double_type_node)
10334 type = complex_long_double_type_node;
10335 else
10336 type = build_complex_type (type);
10339 type_quals = TYPE_UNQUALIFIED;
10340 if (RIDBIT_SETP (RID_CONST, specbits))
10341 type_quals |= TYPE_QUAL_CONST;
10342 if (RIDBIT_SETP (RID_VOLATILE, specbits))
10343 type_quals |= TYPE_QUAL_VOLATILE;
10344 if (RIDBIT_SETP (RID_RESTRICT, specbits))
10345 type_quals |= TYPE_QUAL_RESTRICT;
10346 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10347 error ("qualifiers are not allowed on declaration of `operator %T'",
10348 ctor_return_type);
10350 type_quals |= cp_type_quals (type);
10351 type = cp_build_qualified_type_real
10352 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10353 ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10354 /* We might have ignored or rejected some of the qualifiers. */
10355 type_quals = cp_type_quals (type);
10357 staticp = 0;
10358 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10359 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10360 RIDBIT_RESET (RID_VIRTUAL, specbits);
10361 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10362 RIDBIT_RESET (RID_EXPLICIT, specbits);
10364 if (RIDBIT_SETP (RID_STATIC, specbits))
10365 staticp = 1 + (decl_context == FIELD);
10367 if (virtualp && staticp == 2)
10369 error ("member `%D' cannot be declared both virtual and static",
10370 dname);
10371 staticp = 0;
10373 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10374 RIDBIT_RESET (RID_FRIEND, specbits);
10376 if (dependant_name && !friendp)
10378 error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10379 return void_type_node;
10382 /* Warn if two storage classes are given. Default to `auto'. */
10384 if (RIDBIT_ANY_SET (specbits))
10386 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10387 if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10388 if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10389 if (decl_context == PARM && nclasses > 0)
10390 error ("storage class specifiers invalid in parameter declarations");
10391 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10393 if (decl_context == PARM)
10394 error ("typedef declaration invalid in parameter declaration");
10395 nclasses++;
10397 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10398 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10399 if (!nclasses && !friendp && extern_langp)
10400 nclasses++;
10403 /* Give error if `virtual' is used outside of class declaration. */
10404 if (virtualp
10405 && (current_class_name == NULL_TREE || decl_context != FIELD))
10407 error ("virtual outside class declaration");
10408 virtualp = 0;
10411 /* Static anonymous unions are dealt with here. */
10412 if (staticp && decl_context == TYPENAME
10413 && TREE_CODE (declspecs) == TREE_LIST
10414 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10415 decl_context = FIELD;
10417 /* Warn about storage classes that are invalid for certain
10418 kinds of declarations (parameters, typenames, etc.). */
10420 /* "static __thread" and "extern __thread" are allowed. */
10421 if (nclasses == 2
10422 && RIDBIT_SETP (RID_THREAD, specbits)
10423 && (RIDBIT_SETP (RID_EXTERN, specbits)
10424 || RIDBIT_SETP (RID_STATIC, specbits)))
10425 nclasses = 1;
10427 if (nclasses > 1)
10428 error ("multiple storage classes in declaration of `%s'", name);
10429 else if (decl_context != NORMAL && nclasses > 0)
10431 if ((decl_context == PARM || decl_context == CATCHPARM)
10432 && (RIDBIT_SETP (RID_REGISTER, specbits)
10433 || RIDBIT_SETP (RID_AUTO, specbits)))
10435 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10437 else if (decl_context == FIELD
10438 /* C++ allows static class elements */
10439 && RIDBIT_SETP (RID_STATIC, specbits))
10440 /* C++ also allows inlines and signed and unsigned elements,
10441 but in those cases we don't come in here. */
10443 else
10445 if (decl_context == FIELD)
10447 tree tmp = NULL_TREE;
10448 register int op = 0;
10450 if (declarator)
10452 /* Avoid trying to get an operand off an identifier node. */
10453 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10454 tmp = declarator;
10455 else
10456 tmp = TREE_OPERAND (declarator, 0);
10457 op = IDENTIFIER_OPNAME_P (tmp);
10458 if (IDENTIFIER_TYPENAME_P (tmp))
10460 if (IDENTIFIER_GLOBAL_VALUE (tmp)
10461 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10462 == TYPE_DECL))
10463 name = IDENTIFIER_POINTER (tmp);
10464 else
10465 name = "<invalid operator>";
10468 error ("storage class specified for %s `%s'",
10469 op ? "member operator" : "field",
10470 name);
10472 else
10474 if (decl_context == PARM || decl_context == CATCHPARM)
10475 error ("storage class specified for parameter `%s'", name);
10476 else
10477 error ("storage class specified for typename");
10479 RIDBIT_RESET (RID_REGISTER, specbits);
10480 RIDBIT_RESET (RID_AUTO, specbits);
10481 RIDBIT_RESET (RID_EXTERN, specbits);
10482 RIDBIT_RESET (RID_THREAD, specbits);
10485 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10487 if (toplevel_bindings_p ())
10489 /* It's common practice (and completely valid) to have a const
10490 be initialized and declared extern. */
10491 if (!(type_quals & TYPE_QUAL_CONST))
10492 warning ("`%s' initialized and declared `extern'", name);
10494 else
10495 error ("`%s' has both `extern' and initializer", name);
10497 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10498 && ! toplevel_bindings_p ())
10499 error ("nested function `%s' declared `extern'", name);
10500 else if (toplevel_bindings_p ())
10502 if (RIDBIT_SETP (RID_AUTO, specbits))
10503 error ("top-level declaration of `%s' specifies `auto'", name);
10505 else if (RIDBIT_SETP (RID_THREAD, specbits)
10506 && !RIDBIT_SETP (RID_EXTERN, specbits)
10507 && !RIDBIT_SETP (RID_STATIC, specbits))
10509 error ("function-scope `%s' implicitly auto and declared `__thread'",
10510 name);
10511 RIDBIT_RESET (RID_THREAD, specbits);
10514 if (nclasses > 0 && friendp)
10515 error ("storage class specifiers invalid in friend function declarations");
10517 scope = get_scope_of_declarator (declarator);
10519 /* Now figure out the structure of the declarator proper.
10520 Descend through it, creating more complex types, until we reach
10521 the declared identifier (or NULL_TREE, in an abstract declarator). */
10523 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10524 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10526 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10527 an INDIRECT_REF (for *...),
10528 a CALL_EXPR (for ...(...)),
10529 an identifier (for the name being declared)
10530 or a null pointer (for the place in an absolute declarator
10531 where the name was omitted).
10532 For the last two cases, we have just exited the loop.
10534 For C++ it could also be
10535 a SCOPE_REF (for class :: ...). In this case, we have converted
10536 sensible names to types, and those are the values we use to
10537 qualify the member name.
10538 an ADDR_EXPR (for &...),
10539 a BIT_NOT_EXPR (for destructors)
10541 At this point, TYPE is the type of elements of an array,
10542 or for a function to return, or for a pointer to point to.
10543 After this sequence of ifs, TYPE is the type of the
10544 array or function or pointer, and DECLARATOR has had its
10545 outermost layer removed. */
10547 if (type == error_mark_node)
10549 if (TREE_CODE (declarator) == SCOPE_REF)
10550 declarator = TREE_OPERAND (declarator, 1);
10551 else
10552 declarator = TREE_OPERAND (declarator, 0);
10553 continue;
10555 if (quals != NULL_TREE
10556 && (declarator == NULL_TREE
10557 || TREE_CODE (declarator) != SCOPE_REF))
10559 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10560 ctype = TYPE_METHOD_BASETYPE (type);
10561 if (ctype != NULL_TREE)
10563 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10564 grok_method_quals (ctype, dummy, quals);
10565 type = TREE_TYPE (dummy);
10566 quals = NULL_TREE;
10570 switch (TREE_CODE (declarator))
10572 case TREE_LIST:
10574 /* We encode a declarator with embedded attributes using
10575 a TREE_LIST. */
10576 tree attrs = TREE_PURPOSE (declarator);
10577 tree inner_decl;
10578 int attr_flags;
10580 declarator = TREE_VALUE (declarator);
10581 inner_decl = declarator;
10582 while (inner_decl != NULL_TREE
10583 && TREE_CODE (inner_decl) == TREE_LIST)
10584 inner_decl = TREE_VALUE (inner_decl);
10585 attr_flags = 0;
10586 if (inner_decl == NULL_TREE
10587 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10588 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10589 if (TREE_CODE (inner_decl) == CALL_EXPR)
10590 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10591 if (TREE_CODE (inner_decl) == ARRAY_REF)
10592 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10593 returned_attrs = decl_attributes (&type,
10594 chainon (returned_attrs, attrs),
10595 attr_flags);
10597 break;
10599 case ARRAY_REF:
10601 register tree size;
10603 size = TREE_OPERAND (declarator, 1);
10605 /* VC++ spells a zero-sized array with []. */
10606 if (size == NULL_TREE && decl_context == FIELD && ! staticp
10607 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
10608 size = integer_zero_node;
10610 declarator = TREE_OPERAND (declarator, 0);
10612 type = create_array_type_for_decl (dname, type, size);
10614 ctype = NULL_TREE;
10616 break;
10618 case CALL_EXPR:
10620 tree arg_types;
10621 int funcdecl_p;
10622 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10623 tree inner_decl = TREE_OPERAND (declarator, 0);
10625 /* Declaring a function type.
10626 Make sure we have a valid type for the function to return. */
10628 /* We now know that the TYPE_QUALS don't apply to the
10629 decl, but to its return type. */
10630 type_quals = TYPE_UNQUALIFIED;
10632 /* Warn about some types functions can't return. */
10634 if (TREE_CODE (type) == FUNCTION_TYPE)
10636 error ("`%s' declared as function returning a function", name);
10637 type = integer_type_node;
10639 if (TREE_CODE (type) == ARRAY_TYPE)
10641 error ("`%s' declared as function returning an array", name);
10642 type = integer_type_node;
10645 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10646 inner_decl = TREE_OPERAND (inner_decl, 1);
10648 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10649 inner_decl = dname;
10651 /* Pick up type qualifiers which should be applied to `this'. */
10652 quals = CALL_DECLARATOR_QUALS (declarator);
10654 /* Pick up the exception specifications. */
10655 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10657 /* Say it's a definition only for the CALL_EXPR
10658 closest to the identifier. */
10659 funcdecl_p
10660 = inner_decl
10661 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10662 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10663 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10665 if (ctype == NULL_TREE
10666 && decl_context == FIELD
10667 && funcdecl_p
10668 && (friendp == 0 || dname == current_class_name))
10669 ctype = current_class_type;
10671 if (ctype && sfk == sfk_conversion)
10672 TYPE_HAS_CONVERSION (ctype) = 1;
10673 if (ctype && constructor_name_p (dname, ctype))
10675 /* We are within a class's scope. If our declarator name
10676 is the same as the class name, and we are defining
10677 a function, then it is a constructor/destructor, and
10678 therefore returns a void type. */
10680 if (flags == DTOR_FLAG)
10682 /* ISO C++ 12.4/2. A destructor may not be
10683 declared const or volatile. A destructor may
10684 not be static. */
10685 if (staticp == 2)
10686 error ("destructor cannot be static member function");
10687 if (quals)
10689 error ("destructors may not be `%s'",
10690 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10691 quals = NULL_TREE;
10693 if (decl_context == FIELD)
10695 if (! member_function_or_else (ctype,
10696 current_class_type,
10697 flags))
10698 return void_type_node;
10701 else /* It's a constructor. */
10703 if (explicitp == 1)
10704 explicitp = 2;
10705 /* ISO C++ 12.1. A constructor may not be
10706 declared const or volatile. A constructor may
10707 not be virtual. A constructor may not be
10708 static. */
10709 if (staticp == 2)
10710 error ("constructor cannot be static member function");
10711 if (virtualp)
10713 pedwarn ("constructors cannot be declared virtual");
10714 virtualp = 0;
10716 if (quals)
10718 error ("constructors may not be `%s'",
10719 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10720 quals = NULL_TREE;
10723 RID_BIT_TYPE tmp_bits;
10724 memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10725 RIDBIT_RESET (RID_INLINE, tmp_bits);
10726 RIDBIT_RESET (RID_STATIC, tmp_bits);
10727 if (RIDBIT_ANY_SET (tmp_bits))
10728 error ("return value type specifier for constructor ignored");
10730 if (decl_context == FIELD)
10732 if (! member_function_or_else (ctype,
10733 current_class_type,
10734 flags))
10735 return void_type_node;
10736 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10737 if (sfk != sfk_constructor)
10738 return NULL_TREE;
10741 if (decl_context == FIELD)
10742 staticp = 0;
10744 else if (friendp)
10746 if (initialized)
10747 error ("can't initialize friend function `%s'", name);
10748 if (virtualp)
10750 /* Cannot be both friend and virtual. */
10751 error ("virtual functions cannot be friends");
10752 RIDBIT_RESET (RID_FRIEND, specbits);
10753 friendp = 0;
10755 if (decl_context == NORMAL)
10756 error ("friend declaration not in class definition");
10757 if (current_function_decl && funcdef_flag)
10758 error ("can't define friend function `%s' in a local class definition",
10759 name);
10762 /* Construct the function type and go to the next
10763 inner layer of declarator. */
10765 declarator = TREE_OPERAND (declarator, 0);
10767 /* FIXME: This is where default args should be fully
10768 processed. */
10770 arg_types = grokparms (inner_parms);
10772 if (declarator && flags == DTOR_FLAG)
10774 /* A destructor declared in the body of a class will
10775 be represented as a BIT_NOT_EXPR. But, we just
10776 want the underlying IDENTIFIER. */
10777 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10778 declarator = TREE_OPERAND (declarator, 0);
10780 if (arg_types != void_list_node)
10782 error ("destructors may not have parameters");
10783 arg_types = void_list_node;
10784 last_function_parms = NULL_TREE;
10788 /* ANSI says that `const int foo ();'
10789 does not make the function foo const. */
10790 type = build_function_type (type, arg_types);
10792 break;
10794 case ADDR_EXPR:
10795 case INDIRECT_REF:
10796 /* Filter out pointers-to-references and references-to-references.
10797 We can get these if a TYPE_DECL is used. */
10799 if (TREE_CODE (type) == REFERENCE_TYPE)
10801 error (TREE_CODE (declarator) == ADDR_EXPR
10802 ? "cannot declare reference to `%#T'"
10803 : "cannot declare pointer to `%#T'", type);
10804 type = TREE_TYPE (type);
10806 else if (VOID_TYPE_P (type)
10807 && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10808 error (ctype ? "cannot declare pointer to `%#T' member"
10809 : "cannot declare reference to `%#T'", type);
10811 /* Merge any constancy or volatility into the target type
10812 for the pointer. */
10814 /* We now know that the TYPE_QUALS don't apply to the decl,
10815 but to the target of the pointer. */
10816 type_quals = TYPE_UNQUALIFIED;
10818 if (TREE_CODE (declarator) == ADDR_EXPR)
10820 if (!VOID_TYPE_P (type))
10821 type = build_reference_type (type);
10823 else if (TREE_CODE (type) == METHOD_TYPE)
10824 type = build_ptrmemfunc_type (build_pointer_type (type));
10825 else if (ctype)
10826 type = build_ptrmem_type (ctype, type);
10827 else
10828 type = build_pointer_type (type);
10830 /* Process a list of type modifier keywords (such as
10831 const or volatile) that were given inside the `*' or `&'. */
10833 if (TREE_TYPE (declarator))
10835 register tree typemodlist;
10836 int erred = 0;
10837 int constp = 0;
10838 int volatilep = 0;
10839 int restrictp = 0;
10841 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10842 typemodlist = TREE_CHAIN (typemodlist))
10844 tree qualifier = TREE_VALUE (typemodlist);
10846 if (qualifier == ridpointers[(int) RID_CONST])
10848 constp++;
10849 type_quals |= TYPE_QUAL_CONST;
10851 else if (qualifier == ridpointers[(int) RID_VOLATILE])
10853 volatilep++;
10854 type_quals |= TYPE_QUAL_VOLATILE;
10856 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10858 restrictp++;
10859 type_quals |= TYPE_QUAL_RESTRICT;
10861 else if (!erred)
10863 erred = 1;
10864 error ("invalid type modifier within pointer declarator");
10867 if (constp > 1)
10868 pedwarn ("duplicate `const'");
10869 if (volatilep > 1)
10870 pedwarn ("duplicate `volatile'");
10871 if (restrictp > 1)
10872 pedwarn ("duplicate `restrict'");
10873 type = cp_build_qualified_type (type, type_quals);
10874 type_quals = cp_type_quals (type);
10876 declarator = TREE_OPERAND (declarator, 0);
10877 ctype = NULL_TREE;
10878 break;
10880 case SCOPE_REF:
10882 /* We have converted type names to NULL_TREE if the
10883 name was bogus, or to a _TYPE node, if not.
10885 The variable CTYPE holds the type we will ultimately
10886 resolve to. The code here just needs to build
10887 up appropriate member types. */
10888 tree sname = TREE_OPERAND (declarator, 1);
10889 tree t;
10891 /* Destructors can have their visibilities changed as well. */
10892 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10893 sname = TREE_OPERAND (sname, 0);
10895 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10897 /* We had a reference to a global decl, or
10898 perhaps we were given a non-aggregate typedef,
10899 in which case we cleared this out, and should just
10900 keep going as though it wasn't there. */
10901 declarator = sname;
10902 continue;
10904 ctype = TREE_OPERAND (declarator, 0);
10906 t = ctype;
10907 while (t != NULL_TREE && CLASS_TYPE_P (t))
10909 /* You're supposed to have one `template <...>'
10910 for every template class, but you don't need one
10911 for a full specialization. For example:
10913 template <class T> struct S{};
10914 template <> struct S<int> { void f(); };
10915 void S<int>::f () {}
10917 is correct; there shouldn't be a `template <>' for
10918 the definition of `S<int>::f'. */
10919 if (CLASSTYPE_TEMPLATE_INFO (t)
10920 && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
10921 || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10922 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
10923 template_count += 1;
10925 t = TYPE_MAIN_DECL (t);
10926 t = DECL_CONTEXT (t);
10929 if (sname == NULL_TREE)
10930 goto done_scoping;
10932 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10934 /* This is the `standard' use of the scoping operator:
10935 basetype :: member . */
10937 if (ctype == current_class_type)
10939 /* class A {
10940 void A::f ();
10943 Is this ill-formed? */
10945 if (pedantic)
10946 pedwarn ("extra qualification `%T::' on member `%s' ignored",
10947 ctype, name);
10949 else if (TREE_CODE (type) == FUNCTION_TYPE)
10951 if (current_class_type == NULL_TREE || friendp)
10952 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10953 TYPE_ARG_TYPES (type));
10954 else
10956 error ("cannot declare member function `%T::%s' within `%T'",
10957 ctype, name, current_class_type);
10958 return error_mark_node;
10961 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10962 || COMPLETE_TYPE_P (complete_type (ctype)))
10964 /* Have to move this code elsewhere in this function.
10965 this code is used for i.e., typedef int A::M; M *pm;
10967 It is? How? jason 10/2/94 */
10969 if (current_class_type)
10971 error ("cannot declare member `%T::%s' within `%T'",
10972 ctype, name, current_class_type);
10973 return void_type_node;
10976 else
10978 cxx_incomplete_type_error (NULL_TREE, ctype);
10979 return error_mark_node;
10982 declarator = sname;
10984 else if (TREE_CODE (sname) == SCOPE_REF)
10985 abort ();
10986 else
10988 done_scoping:
10989 declarator = TREE_OPERAND (declarator, 1);
10990 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10991 /* In this case, we will deal with it later. */
10993 else if (TREE_CODE (type) == FUNCTION_TYPE)
10994 type = build_cplus_method_type (ctype, TREE_TYPE (type),
10995 TYPE_ARG_TYPES (type));
10998 break;
11000 case BIT_NOT_EXPR:
11001 declarator = TREE_OPERAND (declarator, 0);
11002 break;
11004 case BASELINK:
11005 declarator = BASELINK_FUNCTIONS (declarator);
11006 break;
11008 case RECORD_TYPE:
11009 case UNION_TYPE:
11010 case ENUMERAL_TYPE:
11011 declarator = NULL_TREE;
11012 break;
11014 case ERROR_MARK:
11015 declarator = NULL_TREE;
11016 break;
11018 default:
11019 abort ();
11023 if (returned_attrs)
11025 if (attrlist)
11026 *attrlist = chainon (returned_attrs, *attrlist);
11027 else
11028 attrlist = &returned_attrs;
11031 /* Now TYPE has the actual type. */
11033 /* Did array size calculations overflow? */
11035 if (TREE_CODE (type) == ARRAY_TYPE
11036 && COMPLETE_TYPE_P (type)
11037 && TREE_OVERFLOW (TYPE_SIZE (type)))
11039 error ("size of array `%s' is too large", name);
11040 /* If we proceed with the array type as it is, we'll eventually
11041 crash in tree_low_cst(). */
11042 type = error_mark_node;
11045 if (decl_context == FIELD
11046 && !processing_template_decl
11047 && variably_modified_type_p (type))
11049 error ("data member may not have variably modified type `%T'", type);
11050 type = error_mark_node;
11053 if (explicitp == 1 || (explicitp && friendp))
11055 /* [dcl.fct.spec] The explicit specifier shall only be used in
11056 declarations of constructors within a class definition. */
11057 error ("only declarations of constructors can be `explicit'");
11058 explicitp = 0;
11061 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11063 if (current_class_name == NULL_TREE || decl_context == PARM || friendp)
11065 error ("non-member `%s' cannot be declared `mutable'", name);
11066 RIDBIT_RESET (RID_MUTABLE, specbits);
11068 else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11070 error ("non-object member `%s' cannot be declared `mutable'", name);
11071 RIDBIT_RESET (RID_MUTABLE, specbits);
11073 else if (TREE_CODE (type) == FUNCTION_TYPE
11074 || TREE_CODE (type) == METHOD_TYPE)
11076 error ("function `%s' cannot be declared `mutable'", name);
11077 RIDBIT_RESET (RID_MUTABLE, specbits);
11079 else if (staticp)
11081 error ("static `%s' cannot be declared `mutable'", name);
11082 RIDBIT_RESET (RID_MUTABLE, specbits);
11084 else if (type_quals & TYPE_QUAL_CONST)
11086 error ("const `%s' cannot be declared `mutable'", name);
11087 RIDBIT_RESET (RID_MUTABLE, specbits);
11091 if (declarator == NULL_TREE
11092 || TREE_CODE (declarator) == IDENTIFIER_NODE
11093 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11094 && (TREE_CODE (type) == FUNCTION_TYPE
11095 || TREE_CODE (type) == METHOD_TYPE)))
11096 /* OK */;
11097 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11099 error ("template-id `%D' used as a declarator", declarator);
11100 declarator = dname;
11102 else
11103 /* Unexpected declarator format. */
11104 abort ();
11106 /* If this is declaring a typedef name, return a TYPE_DECL. */
11108 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11110 tree decl;
11112 /* Note that the grammar rejects storage classes
11113 in typenames, fields or parameters. */
11114 if (current_lang_name == lang_name_java)
11115 TYPE_FOR_JAVA (type) = 1;
11117 if (decl_context == FIELD)
11119 if (constructor_name_p (declarator, current_class_type))
11120 pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11121 declarator);
11122 decl = build_lang_decl (TYPE_DECL, declarator, type);
11124 else
11126 decl = build_decl (TYPE_DECL, declarator, type);
11127 if (in_namespace || ctype)
11128 cp_error_at ("typedef name may not be a nested-name-specifier",
11129 decl);
11130 if (!current_function_decl)
11131 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11134 /* If the user declares "typedef struct {...} foo" then the
11135 struct will have an anonymous name. Fill that name in now.
11136 Nothing can refer to it, so nothing needs know about the name
11137 change. */
11138 if (type != error_mark_node
11139 && declarator
11140 && TYPE_NAME (type)
11141 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11142 && TYPE_ANONYMOUS_P (type)
11143 /* Don't do this if there are attributes. */
11144 && (!attrlist || !*attrlist)
11145 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11147 tree oldname = TYPE_NAME (type);
11148 tree t;
11150 /* Replace the anonymous name with the real name everywhere. */
11151 lookup_tag_reverse (type, declarator);
11152 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11153 if (TYPE_NAME (t) == oldname)
11154 TYPE_NAME (t) = decl;
11156 if (TYPE_LANG_SPECIFIC (type))
11157 TYPE_WAS_ANONYMOUS (type) = 1;
11159 /* If this is a typedef within a template class, the nested
11160 type is a (non-primary) template. The name for the
11161 template needs updating as well. */
11162 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11163 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11164 = TYPE_IDENTIFIER (type);
11166 /* FIXME remangle member functions; member functions of a
11167 type with external linkage have external linkage. */
11170 if (quals)
11172 if (ctype == NULL_TREE)
11174 if (TREE_CODE (type) != METHOD_TYPE)
11175 cp_error_at ("invalid type qualifier for non-member function type", decl);
11176 else
11177 ctype = TYPE_METHOD_BASETYPE (type);
11179 if (ctype != NULL_TREE)
11180 grok_method_quals (ctype, decl, quals);
11183 if (RIDBIT_SETP (RID_SIGNED, specbits)
11184 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11185 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11187 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11188 inlinep, friendp, raises != NULL_TREE);
11190 return decl;
11193 /* Detect the case of an array type of unspecified size
11194 which came, as such, direct from a typedef name.
11195 We must copy the type, so that the array's domain can be
11196 individually set by the object's initializer. */
11198 if (type && typedef_type
11199 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11200 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11201 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11203 /* Detect where we're using a typedef of function type to declare a
11204 function. last_function_parms will not be set, so we must create
11205 it now. */
11207 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11209 tree decls = NULL_TREE;
11210 tree args;
11212 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11214 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11216 TREE_CHAIN (decl) = decls;
11217 decls = decl;
11220 last_function_parms = nreverse (decls);
11223 /* If this is a type name (such as, in a cast or sizeof),
11224 compute the type and return it now. */
11226 if (decl_context == TYPENAME)
11228 /* Note that the grammar rejects storage classes
11229 in typenames, fields or parameters. */
11230 if (type_quals != TYPE_UNQUALIFIED)
11231 type_quals = TYPE_UNQUALIFIED;
11233 /* Special case: "friend class foo" looks like a TYPENAME context. */
11234 if (friendp)
11236 if (type_quals != TYPE_UNQUALIFIED)
11238 error ("type qualifiers specified for friend class declaration");
11239 type_quals = TYPE_UNQUALIFIED;
11241 if (inlinep)
11243 error ("`inline' specified for friend class declaration");
11244 inlinep = 0;
11247 if (!current_aggr)
11249 /* Don't allow friend declaration without a class-key. */
11250 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11251 pedwarn ("template parameters cannot be friends");
11252 else if (TREE_CODE (type) == TYPENAME_TYPE)
11253 pedwarn ("friend declaration requires class-key, "
11254 "i.e. `friend class %T::%D'",
11255 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11256 else
11257 pedwarn ("friend declaration requires class-key, "
11258 "i.e. `friend %#T'",
11259 type);
11262 /* Only try to do this stuff if we didn't already give up. */
11263 if (type != integer_type_node)
11265 /* A friendly class? */
11266 if (current_class_type)
11267 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11268 else
11269 error ("trying to make class `%T' a friend of global scope",
11270 type);
11272 type = void_type_node;
11275 else if (quals)
11277 if (ctype == NULL_TREE)
11279 if (TREE_CODE (type) != METHOD_TYPE)
11280 error ("invalid qualifiers on non-member function type");
11281 else
11282 ctype = TYPE_METHOD_BASETYPE (type);
11284 if (ctype)
11286 tree dummy = build_decl (TYPE_DECL, declarator, type);
11287 grok_method_quals (ctype, dummy, quals);
11288 type = TREE_TYPE (dummy);
11292 return type;
11294 else if (declarator == NULL_TREE && decl_context != PARM
11295 && decl_context != CATCHPARM
11296 && TREE_CODE (type) != UNION_TYPE
11297 && ! bitfield)
11299 error ("abstract declarator `%T' used as declaration", type);
11300 declarator = make_anon_name ();
11303 /* `void' at top level (not within pointer)
11304 is allowed only in typedefs or type names.
11305 We don't complain about parms either, but that is because
11306 a better error message can be made later. */
11308 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11310 if (! declarator)
11311 error ("unnamed variable or field declared void");
11312 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11314 if (IDENTIFIER_OPNAME_P (declarator))
11315 abort ();
11316 else
11317 error ("variable or field `%s' declared void", name);
11319 else
11320 error ("variable or field declared void");
11321 type = integer_type_node;
11324 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11325 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11327 if (decl_context == PARM || decl_context == CATCHPARM)
11329 if (ctype || in_namespace)
11330 error ("cannot use `::' in parameter declaration");
11332 /* A parameter declared as an array of T is really a pointer to T.
11333 One declared as a function is really a pointer to a function.
11334 One declared as a member is really a pointer to member. */
11336 if (TREE_CODE (type) == ARRAY_TYPE)
11338 /* Transfer const-ness of array into that of type pointed to. */
11339 type = build_pointer_type (TREE_TYPE (type));
11340 type_quals = TYPE_UNQUALIFIED;
11342 else if (TREE_CODE (type) == FUNCTION_TYPE)
11343 type = build_pointer_type (type);
11344 else if (TREE_CODE (type) == OFFSET_TYPE)
11345 type = build_pointer_type (type);
11349 register tree decl;
11351 if (decl_context == PARM)
11353 decl = cp_build_parm_decl (declarator, type);
11355 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11356 inlinep, friendp, raises != NULL_TREE);
11358 else if (decl_context == FIELD)
11360 if (type == error_mark_node)
11362 /* Happens when declaring arrays of sizes which
11363 are error_mark_node, for example. */
11364 decl = NULL_TREE;
11366 else if (in_namespace && !friendp)
11368 /* Something like struct S { int N::j; }; */
11369 error ("invalid use of `::'");
11370 decl = NULL_TREE;
11372 else if (TREE_CODE (type) == FUNCTION_TYPE)
11374 int publicp = 0;
11375 tree function_context;
11377 /* We catch the others as conflicts with the builtin
11378 typedefs. */
11379 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11381 error ("function `%D' cannot be declared friend",
11382 declarator);
11383 friendp = 0;
11386 if (friendp == 0)
11388 if (ctype == NULL_TREE)
11389 ctype = current_class_type;
11391 if (ctype == NULL_TREE)
11393 error ("can't make `%D' into a method -- not in a class",
11394 declarator);
11395 return void_type_node;
11398 /* ``A union may [ ... ] not [ have ] virtual functions.''
11399 ARM 9.5 */
11400 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11402 error ("function `%D' declared virtual inside a union",
11403 declarator);
11404 return void_type_node;
11407 if (declarator == ansi_opname (NEW_EXPR)
11408 || declarator == ansi_opname (VEC_NEW_EXPR)
11409 || declarator == ansi_opname (DELETE_EXPR)
11410 || declarator == ansi_opname (VEC_DELETE_EXPR))
11412 if (virtualp)
11414 error ("`%D' cannot be declared virtual, since it is always static",
11415 declarator);
11416 virtualp = 0;
11419 else if (staticp < 2)
11420 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11421 TYPE_ARG_TYPES (type));
11424 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11425 function_context = (ctype != NULL_TREE) ?
11426 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11427 publicp = (! friendp || ! staticp)
11428 && function_context == NULL_TREE;
11429 decl = grokfndecl (ctype, type,
11430 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11431 ? declarator : dname,
11432 declarator,
11433 virtualp, flags, quals, raises,
11434 friendp ? -1 : 0, friendp, publicp, inlinep,
11435 funcdef_flag, template_count, in_namespace);
11436 if (decl == NULL_TREE)
11437 return decl;
11438 #if 0
11439 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11440 /* The decl and setting of decl_attr is also turned off. */
11441 decl = build_decl_attribute_variant (decl, decl_attr);
11442 #endif
11444 /* [class.conv.ctor]
11446 A constructor declared without the function-specifier
11447 explicit that can be called with a single parameter
11448 specifies a conversion from the type of its first
11449 parameter to the type of its class. Such a constructor
11450 is called a converting constructor. */
11451 if (explicitp == 2)
11452 DECL_NONCONVERTING_P (decl) = 1;
11453 else if (DECL_CONSTRUCTOR_P (decl))
11455 /* The constructor can be called with exactly one
11456 parameter if there is at least one parameter, and
11457 any subsequent parameters have default arguments.
11458 Ignore any compiler-added parms. */
11459 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11461 if (arg_types == void_list_node
11462 || (arg_types
11463 && TREE_CHAIN (arg_types)
11464 && TREE_CHAIN (arg_types) != void_list_node
11465 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11466 DECL_NONCONVERTING_P (decl) = 1;
11469 else if (TREE_CODE (type) == METHOD_TYPE)
11471 /* We only get here for friend declarations of
11472 members of other classes. */
11473 /* All method decls are public, so tell grokfndecl to set
11474 TREE_PUBLIC, also. */
11475 decl = grokfndecl (ctype, type, declarator, declarator,
11476 virtualp, flags, quals, raises,
11477 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11478 template_count, in_namespace);
11479 if (decl == NULL_TREE)
11480 return NULL_TREE;
11482 else if (!staticp && ! processing_template_decl
11483 && !COMPLETE_TYPE_P (complete_type (type))
11484 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11486 if (declarator)
11487 error ("field `%D' has incomplete type", declarator);
11488 else
11489 error ("name `%T' has incomplete type", type);
11491 /* If we're instantiating a template, tell them which
11492 instantiation made the field's type be incomplete. */
11493 if (current_class_type
11494 && TYPE_NAME (current_class_type)
11495 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11496 && declspecs && TREE_VALUE (declspecs)
11497 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11498 error (" in instantiation of template `%T'",
11499 current_class_type);
11501 type = error_mark_node;
11502 decl = NULL_TREE;
11504 else
11506 if (friendp)
11508 error ("`%s' is neither function nor member function; cannot be declared friend",
11509 IDENTIFIER_POINTER (declarator));
11510 friendp = 0;
11512 decl = NULL_TREE;
11515 if (friendp)
11517 /* Friends are treated specially. */
11518 if (ctype == current_class_type)
11519 warning ("member functions are implicitly friends of their class");
11520 else
11522 tree t = NULL_TREE;
11523 if (decl && DECL_NAME (decl))
11525 if (template_class_depth (current_class_type) == 0)
11527 decl
11528 = check_explicit_specialization
11529 (declarator, decl,
11530 template_count, 2 * (funcdef_flag != 0) + 4);
11531 if (decl == error_mark_node)
11532 return error_mark_node;
11535 t = do_friend (ctype, declarator, decl,
11536 last_function_parms, *attrlist,
11537 flags, quals, funcdef_flag);
11539 if (t && funcdef_flag)
11540 return t;
11542 return void_type_node;
11546 /* Structure field. It may not be a function, except for C++ */
11548 if (decl == NULL_TREE)
11550 if (initialized)
11552 if (!staticp)
11554 /* An attempt is being made to initialize a non-static
11555 member. But, from [class.mem]:
11557 4 A member-declarator can contain a
11558 constant-initializer only if it declares a static
11559 member (_class.static_) of integral or enumeration
11560 type, see _class.static.data_.
11562 This used to be relatively common practice, but
11563 the rest of the compiler does not correctly
11564 handle the initialization unless the member is
11565 static so we make it static below. */
11566 pedwarn ("ISO C++ forbids initialization of member `%D'",
11567 declarator);
11568 pedwarn ("making `%D' static", declarator);
11569 staticp = 1;
11572 if (uses_template_parms (type))
11573 /* We'll check at instantiation time. */
11575 else if (check_static_variable_definition (declarator,
11576 type))
11577 /* If we just return the declaration, crashes
11578 will sometimes occur. We therefore return
11579 void_type_node, as if this was a friend
11580 declaration, to cause callers to completely
11581 ignore this declaration. */
11582 return void_type_node;
11585 /* 9.2p13 [class.mem] */
11586 if (constructor_name_p (declarator, current_class_type)
11587 /* The standard does not allow non-static data members
11588 here either, but we agreed at the 10/99 meeting
11589 to change that in TC 1 so that they are allowed in
11590 classes with no user-defined constructors. */
11591 && staticp)
11592 pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11593 declarator);
11595 if (staticp)
11597 /* C++ allows static class members. All other work
11598 for this is done by grokfield. */
11599 decl = build_lang_decl (VAR_DECL, declarator, type);
11600 TREE_STATIC (decl) = 1;
11601 /* In class context, 'static' means public access. */
11602 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11604 else
11606 decl = build_decl (FIELD_DECL, declarator, type);
11607 DECL_NONADDRESSABLE_P (decl) = bitfield;
11608 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11610 DECL_MUTABLE_P (decl) = 1;
11611 RIDBIT_RESET (RID_MUTABLE, specbits);
11615 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11616 inlinep, friendp, raises != NULL_TREE);
11619 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11621 tree original_name;
11622 int publicp = 0;
11624 if (! declarator)
11625 return NULL_TREE;
11627 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11628 original_name = dname;
11629 else
11630 original_name = declarator;
11632 if (RIDBIT_SETP (RID_AUTO, specbits))
11633 error ("storage class `auto' invalid for function `%s'", name);
11634 else if (RIDBIT_SETP (RID_REGISTER, specbits))
11635 error ("storage class `register' invalid for function `%s'", name);
11636 else if (RIDBIT_SETP (RID_THREAD, specbits))
11637 error ("storage class `__thread' invalid for function `%s'", name);
11639 /* Function declaration not at top level.
11640 Storage classes other than `extern' are not allowed
11641 and `extern' makes no difference. */
11642 if (! toplevel_bindings_p ()
11643 && (RIDBIT_SETP (RID_STATIC, specbits)
11644 || RIDBIT_SETP (RID_INLINE, specbits))
11645 && pedantic)
11647 if (RIDBIT_SETP (RID_STATIC, specbits))
11648 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11649 else
11650 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11653 if (ctype == NULL_TREE)
11655 if (virtualp)
11657 error ("virtual non-class function `%s'", name);
11658 virtualp = 0;
11661 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11662 type = build_cplus_method_type (ctype, TREE_TYPE (type),
11663 TYPE_ARG_TYPES (type));
11665 /* Record presence of `static'. */
11666 publicp = (ctype != NULL_TREE
11667 || RIDBIT_SETP (RID_EXTERN, specbits)
11668 || !RIDBIT_SETP (RID_STATIC, specbits));
11670 decl = grokfndecl (ctype, type, original_name, declarator,
11671 virtualp, flags, quals, raises,
11672 1, friendp,
11673 publicp, inlinep, funcdef_flag,
11674 template_count, in_namespace);
11675 if (decl == NULL_TREE)
11676 return NULL_TREE;
11678 if (staticp == 1)
11680 int invalid_static = 0;
11682 /* Don't allow a static member function in a class, and forbid
11683 declaring main to be static. */
11684 if (TREE_CODE (type) == METHOD_TYPE)
11686 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11687 invalid_static = 1;
11689 else if (current_function_decl)
11691 /* FIXME need arm citation */
11692 error ("cannot declare static function inside another function");
11693 invalid_static = 1;
11696 if (invalid_static)
11698 staticp = 0;
11699 RIDBIT_RESET (RID_STATIC, specbits);
11703 else
11705 /* It's a variable. */
11707 /* An uninitialized decl with `extern' is a reference. */
11708 decl = grokvardecl (type, declarator, &specbits,
11709 initialized,
11710 (type_quals & TYPE_QUAL_CONST) != 0,
11711 ctype ? ctype : in_namespace);
11712 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11713 inlinep, friendp, raises != NULL_TREE);
11715 if (ctype)
11717 DECL_CONTEXT (decl) = ctype;
11718 if (staticp == 1)
11720 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11721 staticp = 0;
11722 RIDBIT_RESET (RID_STATIC, specbits);
11724 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11726 error ("static member `%D' declared `register'", decl);
11727 RIDBIT_RESET (RID_REGISTER, specbits);
11729 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11731 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11732 decl);
11733 RIDBIT_RESET (RID_EXTERN, specbits);
11738 my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11740 /* Record `register' declaration for warnings on &
11741 and in case doing stupid register allocation. */
11743 if (RIDBIT_SETP (RID_REGISTER, specbits))
11744 DECL_REGISTER (decl) = 1;
11746 if (RIDBIT_SETP (RID_EXTERN, specbits))
11747 DECL_THIS_EXTERN (decl) = 1;
11749 if (RIDBIT_SETP (RID_STATIC, specbits))
11750 DECL_THIS_STATIC (decl) = 1;
11752 /* Record constancy and volatility. There's no need to do this
11753 when processing a template; we'll do this for the instantiated
11754 declaration based on the type of DECL. */
11755 if (!processing_template_decl)
11756 c_apply_type_quals_to_decl (type_quals, decl);
11758 return decl;
11762 /* Subroutine of start_function. Ensure that each of the parameter
11763 types (as listed in PARMS) is complete, as is required for a
11764 function definition. */
11766 static void
11767 require_complete_types_for_parms (tree parms)
11769 for (; parms; parms = TREE_CHAIN (parms))
11771 if (VOID_TYPE_P (TREE_TYPE (parms)))
11772 /* grokparms will have already issued an error */
11773 TREE_TYPE (parms) = error_mark_node;
11774 else if (complete_type_or_else (TREE_TYPE (parms), parms))
11776 layout_decl (parms, 0);
11777 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11779 else
11780 TREE_TYPE (parms) = error_mark_node;
11784 /* Returns nonzero if T is a local variable. */
11787 local_variable_p (tree t)
11789 if ((TREE_CODE (t) == VAR_DECL
11790 /* A VAR_DECL with a context that is a _TYPE is a static data
11791 member. */
11792 && !TYPE_P (CP_DECL_CONTEXT (t))
11793 /* Any other non-local variable must be at namespace scope. */
11794 && !DECL_NAMESPACE_SCOPE_P (t))
11795 || (TREE_CODE (t) == PARM_DECL))
11796 return 1;
11798 return 0;
11801 /* Returns nonzero if T is an automatic local variable or a label.
11802 (These are the declarations that need to be remapped when the code
11803 containing them is duplicated.) */
11806 nonstatic_local_decl_p (tree t)
11808 return ((local_variable_p (t) && !TREE_STATIC (t))
11809 || TREE_CODE (t) == LABEL_DECL
11810 || TREE_CODE (t) == RESULT_DECL);
11813 /* Like local_variable_p, but suitable for use as a tree-walking
11814 function. */
11816 static tree
11817 local_variable_p_walkfn (tree* tp,
11818 int* walk_subtrees ATTRIBUTE_UNUSED ,
11819 void* data ATTRIBUTE_UNUSED )
11821 return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11822 ? *tp : NULL_TREE);
11825 /* Check that ARG, which is a default-argument expression for a
11826 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11827 something goes wrong. DECL may also be a _TYPE node, rather than a
11828 DECL, if there is no DECL available. */
11830 tree
11831 check_default_argument (tree decl, tree arg)
11833 tree var;
11834 tree decl_type;
11836 if (TREE_CODE (arg) == DEFAULT_ARG)
11837 /* We get a DEFAULT_ARG when looking at an in-class declaration
11838 with a default argument. Ignore the argument for now; we'll
11839 deal with it after the class is complete. */
11840 return arg;
11842 if (processing_template_decl || uses_template_parms (arg))
11843 /* We don't do anything checking until instantiation-time. Note
11844 that there may be uninstantiated arguments even for an
11845 instantiated function, since default arguments are not
11846 instantiated until they are needed. */
11847 return arg;
11849 if (TYPE_P (decl))
11851 decl_type = decl;
11852 decl = NULL_TREE;
11854 else
11855 decl_type = TREE_TYPE (decl);
11857 if (arg == error_mark_node
11858 || decl == error_mark_node
11859 || TREE_TYPE (arg) == error_mark_node
11860 || decl_type == error_mark_node)
11861 /* Something already went wrong. There's no need to check
11862 further. */
11863 return error_mark_node;
11865 /* [dcl.fct.default]
11867 A default argument expression is implicitly converted to the
11868 parameter type. */
11869 if (!TREE_TYPE (arg)
11870 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11872 if (decl)
11873 error ("default argument for `%#D' has type `%T'",
11874 decl, TREE_TYPE (arg));
11875 else
11876 error ("default argument for parameter of type `%T' has type `%T'",
11877 decl_type, TREE_TYPE (arg));
11879 return error_mark_node;
11882 /* [dcl.fct.default]
11884 Local variables shall not be used in default argument
11885 expressions.
11887 The keyword `this' shall not be used in a default argument of a
11888 member function. */
11889 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11890 NULL);
11891 if (var)
11893 error ("default argument `%E' uses local variable `%D'",
11894 arg, var);
11895 return error_mark_node;
11898 /* All is well. */
11899 return arg;
11902 /* Decode the list of parameter types for a function type.
11903 Given the list of things declared inside the parens,
11904 return a list of types.
11906 We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11907 flag. If unset, we append void_list_node. A parmlist declared
11908 as `(void)' is accepted as the empty parmlist.
11910 Also set last_function_parms to the chain of PARM_DECLs. */
11912 static tree
11913 grokparms (tree first_parm)
11915 tree result = NULL_TREE;
11916 tree decls = NULL_TREE;
11917 int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11918 tree parm, chain;
11919 int any_error = 0;
11921 my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11923 for (parm = first_parm; parm != NULL_TREE; parm = chain)
11925 tree type = NULL_TREE;
11926 tree decl = TREE_VALUE (parm);
11927 tree init = TREE_PURPOSE (parm);
11928 tree specs, attrs;
11930 chain = TREE_CHAIN (parm);
11931 /* @@ weak defense against parse errors. */
11932 if (TREE_CODE (decl) != VOID_TYPE
11933 && TREE_CODE (decl) != TREE_LIST)
11935 /* Give various messages as the need arises. */
11936 if (TREE_CODE (decl) == STRING_CST)
11937 error ("invalid string constant `%E'", decl);
11938 else if (TREE_CODE (decl) == INTEGER_CST)
11939 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11940 continue;
11943 if (parm == void_list_node)
11944 break;
11946 split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
11947 decl = grokdeclarator (TREE_VALUE (decl), specs,
11948 PARM, init != NULL_TREE, &attrs);
11949 if (! decl || TREE_TYPE (decl) == error_mark_node)
11950 continue;
11952 if (attrs)
11953 cplus_decl_attributes (&decl, attrs, 0);
11955 type = TREE_TYPE (decl);
11956 if (VOID_TYPE_P (type))
11958 if (same_type_p (type, void_type_node)
11959 && !DECL_NAME (decl) && !result && !chain && !ellipsis)
11960 /* this is a parmlist of `(void)', which is ok. */
11961 break;
11962 cxx_incomplete_type_error (decl, type);
11963 /* It's not a good idea to actually create parameters of
11964 type `void'; other parts of the compiler assume that a
11965 void type terminates the parameter list. */
11966 type = error_mark_node;
11967 TREE_TYPE (decl) = error_mark_node;
11970 if (type != error_mark_node)
11972 /* Top-level qualifiers on the parameters are
11973 ignored for function types. */
11974 type = TYPE_MAIN_VARIANT (type);
11975 if (TREE_CODE (type) == METHOD_TYPE)
11977 error ("parameter `%D' invalidly declared method type", decl);
11978 type = build_pointer_type (type);
11979 TREE_TYPE (decl) = type;
11981 else if (TREE_CODE (type) == OFFSET_TYPE)
11983 error ("parameter `%D' invalidly declared offset type", decl);
11984 type = build_pointer_type (type);
11985 TREE_TYPE (decl) = type;
11987 else if (abstract_virtuals_error (decl, type))
11988 any_error = 1; /* Seems like a good idea. */
11989 else if (POINTER_TYPE_P (type))
11991 /* [dcl.fct]/6, parameter types cannot contain pointers
11992 (references) to arrays of unknown bound. */
11993 tree t = TREE_TYPE (type);
11994 int ptr = TYPE_PTR_P (type);
11996 while (1)
11998 if (TYPE_PTR_P (t))
11999 ptr = 1;
12000 else if (TREE_CODE (t) != ARRAY_TYPE)
12001 break;
12002 else if (!TYPE_DOMAIN (t))
12003 break;
12004 t = TREE_TYPE (t);
12006 if (TREE_CODE (t) == ARRAY_TYPE)
12007 error ("parameter `%D' includes %s to array of unknown bound `%T'",
12008 decl, ptr ? "pointer" : "reference", t);
12011 if (!any_error && init)
12012 init = check_default_argument (decl, init);
12013 else
12014 init = NULL_TREE;
12017 TREE_CHAIN (decl) = decls;
12018 decls = decl;
12019 result = tree_cons (init, type, result);
12021 decls = nreverse (decls);
12022 result = nreverse (result);
12023 if (!ellipsis)
12024 result = chainon (result, void_list_node);
12025 last_function_parms = decls;
12027 return result;
12031 /* D is a constructor or overloaded `operator='.
12033 Let T be the class in which D is declared. Then, this function
12034 returns:
12036 -1 if D's is an ill-formed constructor or copy assignment operator
12037 whose first parameter is of type `T'.
12038 0 if D is not a copy constructor or copy assignment
12039 operator.
12040 1 if D is a copy constructor or copy assignment operator whose
12041 first parameter is a reference to const qualified T.
12042 2 if D is a copy constructor or copy assignment operator whose
12043 first parameter is a reference to non-const qualified T.
12045 This function can be used as a predicate. Positive values indicate
12046 a copy constructor and nonzero values indicate a copy assignment
12047 operator. */
12050 copy_fn_p (tree d)
12052 tree args;
12053 tree arg_type;
12054 int result = 1;
12056 my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12058 if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12059 /* Instantiations of template member functions are never copy
12060 functions. Note that member functions of templated classes are
12061 represented as template functions internally, and we must
12062 accept those as copy functions. */
12063 return 0;
12065 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12066 if (!args)
12067 return 0;
12069 arg_type = TREE_VALUE (args);
12071 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12073 /* Pass by value copy assignment operator. */
12074 result = -1;
12076 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12077 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12079 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12080 result = 2;
12082 else
12083 return 0;
12085 args = TREE_CHAIN (args);
12087 if (args && args != void_list_node && !TREE_PURPOSE (args))
12088 /* There are more non-optional args. */
12089 return 0;
12091 return result;
12094 /* Remember any special properties of member function DECL. */
12096 void grok_special_member_properties (tree decl)
12098 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12099 ; /* Not special. */
12100 else if (DECL_CONSTRUCTOR_P (decl))
12102 int ctor = copy_fn_p (decl);
12104 if (ctor > 0)
12106 /* [class.copy]
12108 A non-template constructor for class X is a copy
12109 constructor if its first parameter is of type X&, const
12110 X&, volatile X& or const volatile X&, and either there
12111 are no other parameters or else all other parameters have
12112 default arguments. */
12113 TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12114 if (ctor > 1)
12115 TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12117 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12118 TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12120 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12122 /* [class.copy]
12124 A non-template assignment operator for class X is a copy
12125 assignment operator if its parameter is of type X, X&, const
12126 X&, volatile X& or const volatile X&. */
12128 int assop = copy_fn_p (decl);
12130 if (assop)
12132 TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12133 if (assop != 1)
12134 TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12135 if (DECL_PURE_VIRTUAL_P (decl))
12136 TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12141 /* Check a constructor DECL has the correct form. Complains
12142 if the class has a constructor of the form X(X). */
12145 grok_ctor_properties (tree ctype, tree decl)
12147 int ctor_parm = copy_fn_p (decl);
12149 if (ctor_parm < 0)
12151 /* [class.copy]
12153 A declaration of a constructor for a class X is ill-formed if
12154 its first parameter is of type (optionally cv-qualified) X
12155 and either there are no other parameters or else all other
12156 parameters have default arguments.
12158 We *don't* complain about member template instantiations that
12159 have this form, though; they can occur as we try to decide
12160 what constructor to use during overload resolution. Since
12161 overload resolution will never prefer such a constructor to
12162 the non-template copy constructor (which is either explicitly
12163 or implicitly defined), there's no need to worry about their
12164 existence. Theoretically, they should never even be
12165 instantiated, but that's hard to forestall. */
12166 error ("invalid constructor; you probably meant `%T (const %T&)'",
12167 ctype, ctype);
12168 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12169 return 0;
12172 return 1;
12175 /* An operator with this code is unary, but can also be binary. */
12177 static int
12178 ambi_op_p (enum tree_code code)
12180 return (code == INDIRECT_REF
12181 || code == ADDR_EXPR
12182 || code == CONVERT_EXPR
12183 || code == NEGATE_EXPR
12184 || code == PREINCREMENT_EXPR
12185 || code == PREDECREMENT_EXPR);
12188 /* An operator with this name can only be unary. */
12190 static int
12191 unary_op_p (enum tree_code code)
12193 return (code == TRUTH_NOT_EXPR
12194 || code == BIT_NOT_EXPR
12195 || code == COMPONENT_REF
12196 || code == TYPE_EXPR);
12199 /* Do a little sanity-checking on how they declared their operator. */
12201 void
12202 grok_op_properties (tree decl, int friendp)
12204 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12205 tree argtype;
12206 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12207 tree name = DECL_NAME (decl);
12208 enum tree_code operator_code;
12209 int arity;
12211 /* Count the number of arguments. */
12212 for (argtype = argtypes, arity = 0;
12213 argtype && argtype != void_list_node;
12214 argtype = TREE_CHAIN (argtype))
12215 ++arity;
12217 if (current_class_type == NULL_TREE)
12218 friendp = 1;
12220 if (DECL_CONV_FN_P (decl))
12221 operator_code = TYPE_EXPR;
12222 else
12225 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12226 if (ansi_opname (CODE) == name) \
12228 operator_code = (CODE); \
12229 break; \
12231 else if (ansi_assopname (CODE) == name) \
12233 operator_code = (CODE); \
12234 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12235 break; \
12238 #include "operators.def"
12239 #undef DEF_OPERATOR
12241 abort ();
12243 while (0);
12244 my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12245 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12247 if (! friendp)
12249 switch (operator_code)
12251 case CALL_EXPR:
12252 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12253 break;
12255 case ARRAY_REF:
12256 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12257 break;
12259 case COMPONENT_REF:
12260 case MEMBER_REF:
12261 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12262 break;
12264 case NEW_EXPR:
12265 TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12266 break;
12268 case DELETE_EXPR:
12269 TYPE_GETS_DELETE (current_class_type) |= 1;
12270 break;
12272 case VEC_NEW_EXPR:
12273 TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12274 break;
12276 case VEC_DELETE_EXPR:
12277 TYPE_GETS_DELETE (current_class_type) |= 2;
12278 break;
12280 default:
12281 break;
12285 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12287 /* When the compiler encounters the definition of A::operator new, it
12288 doesn't look at the class declaration to find out if it's static. */
12289 if (methodp)
12290 revert_static_member_fn (decl);
12292 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12294 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12296 if (methodp)
12297 revert_static_member_fn (decl);
12299 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12301 else
12303 /* An operator function must either be a non-static member function
12304 or have at least one parameter of a class, a reference to a class,
12305 an enumeration, or a reference to an enumeration. 13.4.0.6 */
12306 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12308 if (operator_code == TYPE_EXPR
12309 || operator_code == CALL_EXPR
12310 || operator_code == COMPONENT_REF
12311 || operator_code == ARRAY_REF
12312 || operator_code == NOP_EXPR)
12313 error ("`%D' must be a nonstatic member function", decl);
12314 else
12316 tree p = argtypes;
12318 if (DECL_STATIC_FUNCTION_P (decl))
12319 error ("`%D' must be either a non-static member function or a non-member function", decl);
12321 if (p)
12322 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
12324 tree arg = TREE_VALUE (p);
12325 if (TREE_CODE (arg) == REFERENCE_TYPE)
12326 arg = TREE_TYPE (arg);
12328 /* This lets bad template code slip through. */
12329 if (IS_AGGR_TYPE (arg)
12330 || TREE_CODE (arg) == ENUMERAL_TYPE
12331 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12332 || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
12333 goto foundaggr;
12335 error
12336 ("`%D' must have an argument of class or enumerated type",
12337 decl);
12338 foundaggr:
12343 if (operator_code == CALL_EXPR)
12344 return; /* No restrictions on args. */
12346 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12348 tree t = TREE_TYPE (name);
12349 if (! friendp)
12351 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12352 const char *what = 0;
12354 if (ref)
12355 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12357 if (TREE_CODE (t) == VOID_TYPE)
12358 what = "void";
12359 else if (t == current_class_type)
12360 what = "the same type";
12361 /* Don't force t to be complete here. */
12362 else if (IS_AGGR_TYPE (t)
12363 && COMPLETE_TYPE_P (t)
12364 && DERIVED_FROM_P (t, current_class_type))
12365 what = "a base class";
12367 if (what)
12368 warning ("conversion to %s%s will never use a type conversion operator",
12369 ref ? "a reference to " : "", what);
12372 if (operator_code == COND_EXPR)
12374 /* 13.4.0.3 */
12375 error ("ISO C++ prohibits overloading operator ?:");
12377 else if (ambi_op_p (operator_code))
12379 if (arity == 1)
12380 /* We pick the one-argument operator codes by default, so
12381 we don't have to change anything. */
12383 else if (arity == 2)
12385 /* If we thought this was a unary operator, we now know
12386 it to be a binary operator. */
12387 switch (operator_code)
12389 case INDIRECT_REF:
12390 operator_code = MULT_EXPR;
12391 break;
12393 case ADDR_EXPR:
12394 operator_code = BIT_AND_EXPR;
12395 break;
12397 case CONVERT_EXPR:
12398 operator_code = PLUS_EXPR;
12399 break;
12401 case NEGATE_EXPR:
12402 operator_code = MINUS_EXPR;
12403 break;
12405 case PREINCREMENT_EXPR:
12406 operator_code = POSTINCREMENT_EXPR;
12407 break;
12409 case PREDECREMENT_EXPR:
12410 operator_code = POSTDECREMENT_EXPR;
12411 break;
12413 default:
12414 abort ();
12417 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12419 if ((operator_code == POSTINCREMENT_EXPR
12420 || operator_code == POSTDECREMENT_EXPR)
12421 && ! processing_template_decl
12422 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12424 if (methodp)
12425 error ("postfix `%D' must take `int' as its argument",
12426 decl);
12427 else
12428 error
12429 ("postfix `%D' must take `int' as its second argument",
12430 decl);
12433 else
12435 if (methodp)
12436 error ("`%D' must take either zero or one argument", decl);
12437 else
12438 error ("`%D' must take either one or two arguments", decl);
12441 /* More Effective C++ rule 6. */
12442 if (warn_ecpp
12443 && (operator_code == POSTINCREMENT_EXPR
12444 || operator_code == POSTDECREMENT_EXPR
12445 || operator_code == PREINCREMENT_EXPR
12446 || operator_code == PREDECREMENT_EXPR))
12448 tree arg = TREE_VALUE (argtypes);
12449 tree ret = TREE_TYPE (TREE_TYPE (decl));
12450 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12451 arg = TREE_TYPE (arg);
12452 arg = TYPE_MAIN_VARIANT (arg);
12453 if (operator_code == PREINCREMENT_EXPR
12454 || operator_code == PREDECREMENT_EXPR)
12456 if (TREE_CODE (ret) != REFERENCE_TYPE
12457 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12458 arg))
12459 warning ("prefix `%D' should return `%T'", decl,
12460 build_reference_type (arg));
12462 else
12464 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12465 warning ("postfix `%D' should return `%T'", decl, arg);
12469 else if (unary_op_p (operator_code))
12471 if (arity != 1)
12473 if (methodp)
12474 error ("`%D' must take `void'", decl);
12475 else
12476 error ("`%D' must take exactly one argument", decl);
12479 else /* if (binary_op_p (operator_code)) */
12481 if (arity != 2)
12483 if (methodp)
12484 error ("`%D' must take exactly one argument", decl);
12485 else
12486 error ("`%D' must take exactly two arguments", decl);
12489 /* More Effective C++ rule 7. */
12490 if (warn_ecpp
12491 && (operator_code == TRUTH_ANDIF_EXPR
12492 || operator_code == TRUTH_ORIF_EXPR
12493 || operator_code == COMPOUND_EXPR))
12494 warning ("user-defined `%D' always evaluates both arguments",
12495 decl);
12498 /* Effective C++ rule 23. */
12499 if (warn_ecpp
12500 && arity == 2
12501 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12502 && (operator_code == PLUS_EXPR
12503 || operator_code == MINUS_EXPR
12504 || operator_code == TRUNC_DIV_EXPR
12505 || operator_code == MULT_EXPR
12506 || operator_code == TRUNC_MOD_EXPR)
12507 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12508 warning ("`%D' should return by value", decl);
12510 /* [over.oper]/8 */
12511 for (; argtypes && argtypes != void_list_node;
12512 argtypes = TREE_CHAIN (argtypes))
12513 if (TREE_PURPOSE (argtypes))
12515 TREE_PURPOSE (argtypes) = NULL_TREE;
12516 if (operator_code == POSTINCREMENT_EXPR
12517 || operator_code == POSTDECREMENT_EXPR)
12519 if (pedantic)
12520 pedwarn ("`%D' cannot have default arguments", decl);
12522 else
12523 error ("`%D' cannot have default arguments", decl);
12529 static const char *
12530 tag_name (enum tag_types code)
12532 switch (code)
12534 case record_type:
12535 return "struct";
12536 case class_type:
12537 return "class";
12538 case union_type:
12539 return "union ";
12540 case enum_type:
12541 return "enum";
12542 default:
12543 abort ();
12547 /* Get the struct, enum or union (CODE says which) with tag NAME.
12548 Define the tag as a forward-reference if it is not defined.
12550 C++: If a class derivation is given, process it here, and report
12551 an error if multiple derivation declarations are not identical.
12553 If this is a definition, come in through xref_tag and only look in
12554 the current frame for the name (since C++ allows new names in any
12555 scope.) */
12557 tree
12558 xref_tag (enum tag_types tag_code, tree name, tree attributes,
12559 bool globalize)
12561 enum tree_code code;
12562 register tree ref, t;
12563 struct cp_binding_level *b = current_binding_level;
12564 tree context = NULL_TREE;
12566 switch (tag_code)
12568 case record_type:
12569 case class_type:
12570 code = RECORD_TYPE;
12571 break;
12572 case union_type:
12573 code = UNION_TYPE;
12574 break;
12575 case enum_type:
12576 code = ENUMERAL_TYPE;
12577 break;
12578 default:
12579 abort ();
12582 /* If a cross reference is requested, look up the type
12583 already defined for this tag and return it. */
12584 if (TYPE_P (name))
12586 t = name;
12587 name = TYPE_IDENTIFIER (t);
12589 else
12590 t = IDENTIFIER_TYPE_VALUE (name);
12592 /* Warn about 'friend struct Inherited;' doing the wrong thing. */
12593 if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
12595 static int explained;
12596 tree shadowed;
12598 warning ("`%s %T' declares a new type at namespace scope",
12599 tag_name (tag_code), name);
12600 if (!explained++)
12601 warning (" names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
12602 tag_name (tag_code),
12603 constructor_name (current_class_type),
12604 TYPE_IDENTIFIER (t));
12606 /* We need to remove the class scope binding for the
12607 TYPENAME_TYPE as otherwise poplevel_class gets confused. */
12608 for (shadowed = b->class_shadowed;
12609 shadowed;
12610 shadowed = TREE_CHAIN (shadowed))
12611 if (TREE_TYPE (shadowed) == TYPE_NAME (t))
12613 TREE_PURPOSE (shadowed) = NULL_TREE;
12614 break;
12618 if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12619 && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
12620 t = NULL_TREE;
12622 if (! globalize)
12624 /* If we know we are defining this tag, only look it up in
12625 this scope and don't try to find it as a type. */
12626 ref = lookup_tag (code, name, b, 1);
12628 else
12630 if (t)
12632 ref = follow_tag_typedef (t);
12634 /* [dcl.type.elab] If the identifier resolves to a
12635 typedef-name or a template type-parameter, the
12636 elaborated-type-specifier is ill-formed. */
12637 if (!ref)
12639 pedwarn ("using typedef-name `%D' after `%s'",
12640 TYPE_NAME (t), tag_name (tag_code));
12641 ref = t;
12643 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12644 error ("using template type parameter `%T' after `%s'",
12645 t, tag_name (tag_code));
12647 else
12648 ref = lookup_tag (code, name, b, 0);
12650 if (! ref)
12652 /* Try finding it as a type declaration. If that wins,
12653 use it. */
12654 ref = lookup_name (name, 1);
12656 if (ref != NULL_TREE
12657 && processing_template_decl
12658 && DECL_CLASS_TEMPLATE_P (ref)
12659 && template_class_depth (current_class_type) == 0)
12660 /* Since GLOBALIZE is true, we're declaring a global
12661 template, so we want this type. */
12662 ref = DECL_TEMPLATE_RESULT (ref);
12664 if (ref && TREE_CODE (ref) == TYPE_DECL
12665 && TREE_CODE (TREE_TYPE (ref)) == code)
12666 ref = TREE_TYPE (ref);
12667 else
12668 ref = NULL_TREE;
12671 if (ref && current_class_type
12672 && template_class_depth (current_class_type)
12673 && PROCESSING_REAL_TEMPLATE_DECL_P ())
12675 /* Since GLOBALIZE is nonzero, we are not looking at a
12676 definition of this tag. Since, in addition, we are currently
12677 processing a (member) template declaration of a template
12678 class, we must be very careful; consider:
12680 template <class X>
12681 struct S1
12683 template <class U>
12684 struct S2
12685 { template <class V>
12686 friend struct S1; };
12688 Here, the S2::S1 declaration should not be confused with the
12689 outer declaration. In particular, the inner version should
12690 have a template parameter of level 2, not level 1. This
12691 would be particularly important if the member declaration
12692 were instead:
12694 template <class V = U> friend struct S1;
12696 say, when we should tsubst into `U' when instantiating
12697 S2. On the other hand, when presented with:
12699 template <class T>
12700 struct S1 {
12701 template <class U>
12702 struct S2 {};
12703 template <class U>
12704 friend struct S2;
12707 we must find the inner binding eventually. We
12708 accomplish this by making sure that the new type we
12709 create to represent this declaration has the right
12710 TYPE_CONTEXT. */
12711 context = TYPE_CONTEXT (ref);
12712 ref = NULL_TREE;
12716 if (! ref)
12718 /* If no such tag is yet defined, create a forward-reference node
12719 and record it as the "definition".
12720 When a real declaration of this type is found,
12721 the forward-reference will be altered into a real type. */
12722 if (code == ENUMERAL_TYPE)
12724 error ("use of enum `%#D' without previous declaration", name);
12726 ref = make_node (ENUMERAL_TYPE);
12728 /* Give the type a default layout like unsigned int
12729 to avoid crashing if it does not get defined. */
12730 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12731 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12732 TYPE_USER_ALIGN (ref) = 0;
12733 TREE_UNSIGNED (ref) = 1;
12734 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12735 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12736 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12738 /* Enable us to recognize when a type is created in class context.
12739 To do nested classes correctly, this should probably be cleared
12740 out when we leave this classes scope. Currently this in only
12741 done in `start_enum'. */
12743 pushtag (name, ref, globalize);
12745 else
12747 struct cp_binding_level *old_b = class_binding_level;
12749 ref = make_aggr_type (code);
12750 TYPE_CONTEXT (ref) = context;
12752 #ifdef NONNESTED_CLASSES
12753 /* Class types don't nest the way enums do. */
12754 class_binding_level = (struct cp_binding_level *)0;
12755 #endif
12756 pushtag (name, ref, globalize);
12757 class_binding_level = old_b;
12760 else
12762 if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12763 redeclare_class_template (ref, current_template_parms);
12766 TYPE_ATTRIBUTES (ref) = attributes;
12768 return ref;
12771 tree
12772 xref_tag_from_type (tree old, tree id, int globalize)
12774 enum tag_types tag_kind;
12776 if (TREE_CODE (old) == RECORD_TYPE)
12777 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12778 else
12779 tag_kind = union_type;
12781 if (id == NULL_TREE)
12782 id = TYPE_IDENTIFIER (old);
12784 return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
12787 /* REF is a type (named NAME), for which we have just seen some
12788 baseclasses. BINFO is a list of those baseclasses; the
12789 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12790 the base-class. CODE_TYPE_NODE indicates whether REF is a class,
12791 struct, or union. */
12793 void
12794 xref_basetypes (tree ref, tree binfo)
12796 /* In the declaration `A : X, Y, ... Z' we mark all the types
12797 (A, X, Y, ..., Z) so we can check for duplicates. */
12798 tree binfos;
12799 tree *basep;
12801 int i;
12802 enum tag_types tag_code;
12804 if (TREE_CODE (ref) == UNION_TYPE)
12806 error ("derived union `%T' invalid", ref);
12807 return;
12810 tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12812 /* First, make sure that any templates in base-classes are
12813 instantiated. This ensures that if we call ourselves recursively
12814 we do not get confused about which classes are marked and which
12815 are not. */
12816 basep = &binfo;
12817 while (*basep)
12819 tree basetype = TREE_VALUE (*basep);
12820 if (!(processing_template_decl && uses_template_parms (basetype))
12821 && !complete_type_or_else (basetype, NULL))
12822 /* An incomplete type. Remove it form the list. */
12823 *basep = TREE_CHAIN (*basep);
12824 else
12825 basep = &TREE_CHAIN (*basep);
12828 SET_CLASSTYPE_MARKED (ref);
12829 BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos
12830 = make_tree_vec (list_length (binfo));
12832 for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
12834 /* The base of a derived struct is public by default. */
12835 int via_public
12836 = (TREE_PURPOSE (binfo) == access_public_node
12837 || TREE_PURPOSE (binfo) == access_public_virtual_node
12838 || (tag_code != class_type
12839 && (TREE_PURPOSE (binfo) == access_default_node
12840 || TREE_PURPOSE (binfo) == access_default_virtual_node)));
12841 int via_protected
12842 = (TREE_PURPOSE (binfo) == access_protected_node
12843 || TREE_PURPOSE (binfo) == access_protected_virtual_node);
12844 int via_virtual
12845 = (TREE_PURPOSE (binfo) == access_private_virtual_node
12846 || TREE_PURPOSE (binfo) == access_protected_virtual_node
12847 || TREE_PURPOSE (binfo) == access_public_virtual_node
12848 || TREE_PURPOSE (binfo) == access_default_virtual_node);
12849 tree basetype = TREE_VALUE (binfo);
12850 tree base_binfo;
12852 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12853 basetype = TREE_TYPE (basetype);
12854 if (!basetype
12855 || (TREE_CODE (basetype) != RECORD_TYPE
12856 && TREE_CODE (basetype) != TYPENAME_TYPE
12857 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12858 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12860 error ("base type `%T' fails to be a struct or class type",
12861 TREE_VALUE (binfo));
12862 continue;
12865 if (CLASSTYPE_MARKED (basetype))
12867 if (basetype == ref)
12868 error ("recursive type `%T' undefined", basetype);
12869 else
12870 error ("duplicate base type `%T' invalid", basetype);
12871 continue;
12874 if (TYPE_FOR_JAVA (basetype)
12875 && (current_lang_depth () == 0))
12876 TYPE_FOR_JAVA (ref) = 1;
12878 /* Note that the BINFO records which describe individual
12879 inheritances are *not* shared in the lattice! They
12880 cannot be shared because a given baseclass may be
12881 inherited with different `accessibility' by different
12882 derived classes. (Each BINFO record describing an
12883 individual inheritance contains flags which say what
12884 the `accessibility' of that particular inheritance is.) */
12886 base_binfo
12887 = make_binfo (size_zero_node, basetype,
12888 CLASS_TYPE_P (basetype)
12889 ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
12890 CLASS_TYPE_P (basetype)
12891 ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
12893 TREE_VEC_ELT (binfos, i) = base_binfo;
12894 TREE_VIA_PUBLIC (base_binfo) = via_public;
12895 TREE_VIA_PROTECTED (base_binfo) = via_protected;
12896 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12897 BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
12899 /* We need to unshare the binfos now so that lookups during class
12900 definition work. */
12901 unshare_base_binfos (base_binfo);
12903 SET_CLASSTYPE_MARKED (basetype);
12905 /* We are free to modify these bits because they are meaningless
12906 at top level, and BASETYPE is a top-level type. */
12907 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12909 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12910 /* Converting to a virtual base class requires looking
12911 up the offset of the virtual base. */
12912 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12915 if (CLASS_TYPE_P (basetype))
12917 TYPE_HAS_NEW_OPERATOR (ref)
12918 |= TYPE_HAS_NEW_OPERATOR (basetype);
12919 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12920 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12921 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12922 /* If the base-class uses multiple inheritance, so do we. */
12923 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12924 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12925 /* Likewise, if converting to a base of the base may require
12926 code, then we may need to generate code to convert to a
12927 base as well. */
12928 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12929 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12932 i += 1;
12934 if (i)
12935 TREE_VEC_LENGTH (binfos) = i;
12936 else
12937 BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
12939 if (i > 1)
12941 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12942 /* If there is more than one non-empty they cannot be at the same
12943 address. */
12944 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12947 /* Unmark all the types. */
12948 while (--i >= 0)
12949 CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
12950 CLEAR_CLASSTYPE_MARKED (ref);
12952 /* Now that we know all the base-classes, set up the list of virtual
12953 bases. */
12954 get_vbase_types (ref);
12958 /* Begin compiling the definition of an enumeration type.
12959 NAME is its name (or null if anonymous).
12960 Returns the type object, as yet incomplete.
12961 Also records info about it so that build_enumerator
12962 may be used to declare the individual values as they are read. */
12964 tree
12965 start_enum (tree name)
12967 register tree enumtype = NULL_TREE;
12968 struct cp_binding_level *b = current_binding_level;
12970 /* If this is the real definition for a previous forward reference,
12971 fill in the contents in the same object that used to be the
12972 forward reference. */
12974 if (name != NULL_TREE)
12975 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12977 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
12979 error ("multiple definition of `%#T'", enumtype);
12980 cp_error_at ("previous definition here", enumtype);
12981 /* Clear out TYPE_VALUES, and start again. */
12982 TYPE_VALUES (enumtype) = NULL_TREE;
12984 else
12986 enumtype = make_node (ENUMERAL_TYPE);
12987 pushtag (name, enumtype, 0);
12990 return enumtype;
12993 /* After processing and defining all the values of an enumeration type,
12994 install their decls in the enumeration type and finish it off.
12995 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
12997 void
12998 finish_enum (tree enumtype)
13000 tree pair;
13001 tree minnode;
13002 tree maxnode;
13003 tree t;
13004 bool unsignedp;
13005 int lowprec;
13006 int highprec;
13007 int precision;
13009 /* We built up the VALUES in reverse order. */
13010 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13012 /* [dcl.enum]
13014 Following the closing brace of an enum-specifier, each
13015 enumerator has the type of its enumeration. Prior to the
13016 closing brace, the type of each enumerator is the type of
13017 its initializing value. */
13018 for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13019 TREE_TYPE (TREE_VALUE (pair)) = enumtype;
13021 /* For an enum defined in a template, all further processing is
13022 postponed until the template is instantiated. */
13023 if (processing_template_decl)
13025 if (at_function_scope_p ())
13026 add_stmt (build_min (TAG_DEFN, enumtype));
13027 return;
13030 /* Figure out what the minimum and maximum values of the enumerators
13031 are. */
13032 if (TYPE_VALUES (enumtype))
13034 minnode = maxnode = NULL_TREE;
13036 for (pair = TYPE_VALUES (enumtype);
13037 pair;
13038 pair = TREE_CHAIN (pair))
13040 tree value;
13042 value = DECL_INITIAL (TREE_VALUE (pair));
13044 if (!minnode)
13045 minnode = maxnode = value;
13046 else if (tree_int_cst_lt (maxnode, value))
13047 maxnode = value;
13048 else if (tree_int_cst_lt (value, minnode))
13049 minnode = value;
13052 else
13053 minnode = maxnode = integer_zero_node;
13055 /* Compute the number of bits require to represent all values of the
13056 enumeration. We must do this before the type of MINNODE and
13057 MAXNODE are transformed, since min_precision relies on the
13058 TREE_TYPE of the value it is passed. */
13059 unsignedp = tree_int_cst_sgn (minnode) >= 0;
13060 lowprec = min_precision (minnode, unsignedp);
13061 highprec = min_precision (maxnode, unsignedp);
13062 precision = MAX (lowprec, highprec);
13064 /* Set the TREE_TYPE for the values as well. That's so that when we
13065 call decl_constant_value we get an entity of the right type (but
13066 with the constant value). In addition, transform the TYPE_VALUES
13067 list to contain the values, rather than the CONST_DECLs for them. */
13068 for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13070 tree value = DECL_INITIAL (TREE_VALUE (pair));
13072 TREE_TYPE (value) = enumtype;
13073 TREE_VALUE (pair) = value;
13076 /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */
13077 TYPE_SIZE (enumtype) = NULL_TREE;
13078 TYPE_PRECISION (enumtype) = precision;
13079 if (unsignedp)
13080 fixup_unsigned_type (enumtype);
13081 else
13082 fixup_signed_type (enumtype);
13084 if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13085 /* Use the width of the narrowest normal C type which is wide
13086 enough. */
13087 TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size
13088 (precision, 1));
13089 else
13090 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
13092 TYPE_SIZE (enumtype) = NULL_TREE;
13093 layout_type (enumtype);
13095 /* Fix up all variant types of this enum type. */
13096 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13098 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13099 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13100 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13101 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13102 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13103 TYPE_MODE (t) = TYPE_MODE (enumtype);
13104 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13105 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13106 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13107 TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13110 /* Finish debugging output for this type. */
13111 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13114 /* Build and install a CONST_DECL for an enumeration constant of the
13115 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13116 Assignment of sequential values by default is handled here. */
13118 void
13119 build_enumerator (tree name, tree value, tree enumtype)
13121 tree decl;
13122 tree context;
13123 tree type;
13124 tree values;
13126 /* Remove no-op casts from the value. */
13127 if (value)
13128 STRIP_TYPE_NOPS (value);
13130 if (! processing_template_decl)
13132 /* Validate and default VALUE. */
13133 if (value != NULL_TREE)
13135 value = decl_constant_value (value);
13137 if (TREE_CODE (value) == INTEGER_CST)
13139 value = default_conversion (value);
13140 constant_expression_warning (value);
13142 else
13144 error ("enumerator value for `%D' not integer constant", name);
13145 value = NULL_TREE;
13149 /* Default based on previous value. */
13150 if (value == NULL_TREE)
13152 tree prev_value;
13154 if (TYPE_VALUES (enumtype))
13156 /* The next value is the previous value ... */
13157 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13158 /* ... plus one. */
13159 value = cp_build_binary_op (PLUS_EXPR,
13160 prev_value,
13161 integer_one_node);
13163 if (tree_int_cst_lt (value, prev_value))
13164 error ("overflow in enumeration values at `%D'", name);
13166 else
13167 value = integer_zero_node;
13170 /* Remove no-op casts from the value. */
13171 if (value)
13172 STRIP_TYPE_NOPS (value);
13173 #if 0
13174 /* To fix MAX_VAL enum consts. (bkoz) */
13175 TREE_TYPE (value) = integer_type_node;
13176 #endif
13179 /* We always have to copy here; not all INTEGER_CSTs are unshared.
13180 Even in other cases, we will later (in finish_enum) be setting
13181 the type of VALUE. But, we don't need to make a copy if this
13182 VALUE is one of the enumeration constants for this same
13183 enumeration type. */
13184 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
13185 if (TREE_VALUE (values) == value)
13186 break;
13187 /* If we didn't break out of the loop, then we do need a copy. */
13188 if (!values && value)
13189 value = copy_node (value);
13191 /* C++ associates enums with global, function, or class declarations. */
13192 context = current_scope ();
13194 /* Build the actual enumeration constant. Note that the enumeration
13195 constants have the type of their initializers until the
13196 enumeration is complete:
13198 [ dcl.enum ]
13200 Following the closing brace of an enum-specifier, each enumer-
13201 ator has the type of its enumeration. Prior to the closing
13202 brace, the type of each enumerator is the type of its
13203 initializing value.
13205 In finish_enum we will reset the type. Of course, if we're
13206 processing a template, there may be no value. */
13207 type = value ? TREE_TYPE (value) : NULL_TREE;
13209 if (context && context == current_class_type)
13210 /* This enum declaration is local to the class. We need the full
13211 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
13212 decl = build_lang_decl (CONST_DECL, name, type);
13213 else
13214 /* It's a global enum, or it's local to a function. (Note local to
13215 a function could mean local to a class method. */
13216 decl = build_decl (CONST_DECL, name, type);
13218 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13219 DECL_INITIAL (decl) = value;
13220 TREE_READONLY (decl) = 1;
13222 if (context && context == current_class_type)
13223 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13224 on the TYPE_FIELDS list for `S'. (That's so that you can say
13225 things like `S::i' later.) */
13226 finish_member_declaration (decl);
13227 else
13228 pushdecl (decl);
13230 /* Add this enumeration constant to the list for this type. */
13231 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13235 /* We're defining DECL. Make sure that it's type is OK. */
13237 static void
13238 check_function_type (tree decl, tree current_function_parms)
13240 tree fntype = TREE_TYPE (decl);
13241 tree return_type = complete_type (TREE_TYPE (fntype));
13243 /* In a function definition, arg types must be complete. */
13244 require_complete_types_for_parms (current_function_parms);
13246 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13248 error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13250 /* Make it return void instead, but don't change the
13251 type of the DECL_RESULT, in case we have a named return value. */
13252 if (TREE_CODE (fntype) == METHOD_TYPE)
13254 tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13255 TREE_TYPE (decl)
13256 = build_cplus_method_type (ctype,
13257 void_type_node,
13258 FUNCTION_ARG_CHAIN (decl));
13260 else
13261 TREE_TYPE (decl)
13262 = build_function_type (void_type_node,
13263 TYPE_ARG_TYPES (TREE_TYPE (decl)));
13264 TREE_TYPE (decl)
13265 = build_exception_variant (fntype,
13266 TYPE_RAISES_EXCEPTIONS (fntype));
13268 else
13269 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13272 /* Create the FUNCTION_DECL for a function definition.
13273 DECLSPECS and DECLARATOR are the parts of the declaration;
13274 they describe the function's name and the type it returns,
13275 but twisted together in a fashion that parallels the syntax of C.
13277 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13278 DECLARATOR is really the DECL for the function we are about to
13279 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13280 indicating that the function is an inline defined in-class.
13282 This function creates a binding context for the function body
13283 as well as setting up the FUNCTION_DECL in current_function_decl.
13285 Returns 1 on success. If the DECLARATOR is not suitable for a function
13286 (it defines a datum instead), we return 0, which tells
13287 yyparse to report a parse error.
13289 For C++, we must first check whether that datum makes any sense.
13290 For example, "class A local_a(1,2);" means that variable local_a
13291 is an aggregate of type A, which should have a constructor
13292 applied to it with the argument list [1, 2]. */
13295 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13297 tree decl1;
13298 tree ctype = NULL_TREE;
13299 tree fntype;
13300 tree restype;
13301 int doing_friend = 0;
13302 struct cp_binding_level *bl;
13303 tree current_function_parms;
13305 /* Sanity check. */
13306 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13307 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13309 /* This should only be done once on the top most decl. */
13310 if (have_extern_spec)
13312 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13313 have_extern_spec = false;
13316 if (flags & SF_PRE_PARSED)
13318 decl1 = declarator;
13320 fntype = TREE_TYPE (decl1);
13321 if (TREE_CODE (fntype) == METHOD_TYPE)
13322 ctype = TYPE_METHOD_BASETYPE (fntype);
13324 /* ISO C++ 11.4/5. A friend function defined in a class is in
13325 the (lexical) scope of the class in which it is defined. */
13326 if (!ctype && DECL_FRIEND_P (decl1))
13328 ctype = DECL_FRIEND_CONTEXT (decl1);
13330 /* CTYPE could be null here if we're dealing with a template;
13331 for example, `inline friend float foo()' inside a template
13332 will have no CTYPE set. */
13333 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13334 ctype = NULL_TREE;
13335 else
13336 doing_friend = 1;
13339 last_function_parms = DECL_ARGUMENTS (decl1);
13341 else
13343 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13344 /* If the declarator is not suitable for a function definition,
13345 cause a syntax error. */
13346 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13347 return 0;
13349 cplus_decl_attributes (&decl1, attrs, 0);
13351 /* If #pragma weak was used, mark the decl weak now. */
13352 if (current_binding_level == global_binding_level)
13353 maybe_apply_pragma_weak (decl1);
13355 fntype = TREE_TYPE (decl1);
13357 restype = TREE_TYPE (fntype);
13358 if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
13360 error ("semicolon missing after declaration of `%#T'", restype);
13361 shadow_tag (build_tree_list (NULL_TREE, restype));
13362 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13363 if (TREE_CODE (fntype) == FUNCTION_TYPE)
13364 fntype = build_function_type (integer_type_node,
13365 TYPE_ARG_TYPES (fntype));
13366 else
13367 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13368 integer_type_node,
13369 TYPE_ARG_TYPES (fntype));
13370 TREE_TYPE (decl1) = fntype;
13373 if (TREE_CODE (fntype) == METHOD_TYPE)
13374 ctype = TYPE_METHOD_BASETYPE (fntype);
13375 else if (DECL_MAIN_P (decl1))
13377 /* If this doesn't return integer_type, complain. */
13378 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13380 if (pedantic || warn_return_type)
13381 pedwarn ("return type for `main' changed to `int'");
13382 TREE_TYPE (decl1) = fntype = default_function_type;
13387 if (DECL_DECLARED_INLINE_P (decl1)
13388 && lookup_attribute ("noinline", attrs))
13389 warning_with_decl (decl1,
13390 "inline function `%s' given attribute noinline");
13392 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13393 /* This is a constructor, we must ensure that any default args
13394 introduced by this definition are propagated to the clones
13395 now. The clones are used directly in overload resolution. */
13396 adjust_clone_args (decl1);
13398 /* Sometimes we don't notice that a function is a static member, and
13399 build a METHOD_TYPE for it. Fix that up now. */
13400 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13401 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13403 revert_static_member_fn (decl1);
13404 last_function_parms = TREE_CHAIN (last_function_parms);
13405 ctype = NULL_TREE;
13408 /* Warn if function was previously implicitly declared
13409 (but not if we warned then). */
13410 if (! warn_implicit
13411 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13412 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13414 /* Set up current_class_type, and enter the scope of the class, if
13415 appropriate. */
13416 if (ctype)
13417 push_nested_class (ctype);
13418 else if (DECL_STATIC_FUNCTION_P (decl1))
13419 push_nested_class (DECL_CONTEXT (decl1));
13421 /* Now that we have entered the scope of the class, we must restore
13422 the bindings for any template parameters surrounding DECL1, if it
13423 is an inline member template. (Order is important; consider the
13424 case where a template parameter has the same name as a field of
13425 the class.) It is not until after this point that
13426 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13427 if (flags & SF_INCLASS_INLINE)
13428 maybe_begin_member_template_processing (decl1);
13430 /* Effective C++ rule 15. */
13431 if (warn_ecpp
13432 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13433 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13434 warning ("`operator=' should return a reference to `*this'");
13436 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13437 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13438 if (!DECL_INITIAL (decl1))
13439 DECL_INITIAL (decl1) = error_mark_node;
13441 /* This function exists in static storage.
13442 (This does not mean `static' in the C sense!) */
13443 TREE_STATIC (decl1) = 1;
13445 /* We must call push_template_decl after current_class_type is set
13446 up. (If we are processing inline definitions after exiting a
13447 class scope, current_class_type will be NULL_TREE until set above
13448 by push_nested_class.) */
13449 if (processing_template_decl)
13450 decl1 = push_template_decl (decl1);
13452 /* We are now in the scope of the function being defined. */
13453 current_function_decl = decl1;
13455 /* Save the parm names or decls from this function's declarator
13456 where store_parm_decls will find them. */
13457 current_function_parms = last_function_parms;
13459 /* Make sure the parameter and return types are reasonable. When
13460 you declare a function, these types can be incomplete, but they
13461 must be complete when you define the function. */
13462 if (! processing_template_decl)
13463 check_function_type (decl1, current_function_parms);
13465 /* Build the return declaration for the function. */
13466 restype = TREE_TYPE (fntype);
13467 /* Promote the value to int before returning it. */
13468 if (c_promoting_integer_type_p (restype))
13469 restype = type_promotes_to (restype);
13470 if (DECL_RESULT (decl1) == NULL_TREE)
13472 DECL_RESULT (decl1)
13473 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13474 c_apply_type_quals_to_decl (cp_type_quals (restype),
13475 DECL_RESULT (decl1));
13478 /* Initialize RTL machinery. We cannot do this until
13479 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13480 even when processing a template; this is how we get
13481 CFUN set up, and our per-function variables initialized.
13482 FIXME factor out the non-RTL stuff. */
13483 bl = current_binding_level;
13484 init_function_start (decl1, input_filename, lineno);
13485 current_binding_level = bl;
13487 /* Even though we're inside a function body, we still don't want to
13488 call expand_expr to calculate the size of a variable-sized array.
13489 We haven't necessarily assigned RTL to all variables yet, so it's
13490 not safe to try to expand expressions involving them. */
13491 immediate_size_expand = 0;
13492 cfun->x_dont_save_pending_sizes_p = 1;
13494 /* Start the statement-tree, start the tree now. */
13495 begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13497 /* Let the user know we're compiling this function. */
13498 announce_function (decl1);
13500 /* Record the decl so that the function name is defined.
13501 If we already have a decl for this name, and it is a FUNCTION_DECL,
13502 use the old decl. */
13503 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13505 /* A specialization is not used to guide overload resolution. */
13506 if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13507 && ! DECL_FUNCTION_MEMBER_P (decl1))
13508 decl1 = pushdecl (decl1);
13509 else
13511 /* We need to set the DECL_CONTEXT. */
13512 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13513 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13514 /* And make sure we have enough default args. */
13515 check_default_args (decl1);
13517 fntype = TREE_TYPE (decl1);
13520 /* Reset these in case the call to pushdecl changed them. */
13521 current_function_decl = decl1;
13522 cfun->decl = decl1;
13524 /* If we are (erroneously) defining a function that we have already
13525 defined before, wipe out what we knew before. */
13526 if (!DECL_PENDING_INLINE_P (decl1))
13527 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13529 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13531 /* We know that this was set up by `grokclassfn'. We do not
13532 wait until `store_parm_decls', since evil parse errors may
13533 never get us to that point. Here we keep the consistency
13534 between `current_class_type' and `current_class_ptr'. */
13535 tree t = DECL_ARGUMENTS (decl1);
13537 my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13538 162);
13539 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13540 19990811);
13542 cp_function_chain->x_current_class_ref
13543 = build_indirect_ref (t, NULL);
13544 cp_function_chain->x_current_class_ptr = t;
13546 /* Constructors and destructors need to know whether they're "in
13547 charge" of initializing virtual base classes. */
13548 t = TREE_CHAIN (t);
13549 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13551 current_in_charge_parm = t;
13552 t = TREE_CHAIN (t);
13554 if (DECL_HAS_VTT_PARM_P (decl1))
13556 if (DECL_NAME (t) != vtt_parm_identifier)
13557 abort ();
13558 current_vtt_parm = t;
13562 if (DECL_INTERFACE_KNOWN (decl1))
13564 tree ctx = decl_function_context (decl1);
13566 if (DECL_NOT_REALLY_EXTERN (decl1))
13567 DECL_EXTERNAL (decl1) = 0;
13569 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13570 && TREE_PUBLIC (ctx))
13571 /* This is a function in a local class in an extern inline
13572 function. */
13573 comdat_linkage (decl1);
13575 /* If this function belongs to an interface, it is public.
13576 If it belongs to someone else's interface, it is also external.
13577 This only affects inlines and template instantiations. */
13578 else if (interface_unknown == 0
13579 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13580 || flag_alt_external_templates))
13582 if (DECL_DECLARED_INLINE_P (decl1)
13583 || DECL_TEMPLATE_INSTANTIATION (decl1)
13584 || processing_template_decl)
13586 DECL_EXTERNAL (decl1)
13587 = (interface_only
13588 || (DECL_DECLARED_INLINE_P (decl1)
13589 && ! flag_implement_inlines
13590 && !DECL_VINDEX (decl1)));
13592 /* For WIN32 we also want to put these in linkonce sections. */
13593 maybe_make_one_only (decl1);
13595 else
13596 DECL_EXTERNAL (decl1) = 0;
13597 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13598 DECL_INTERFACE_KNOWN (decl1) = 1;
13600 else if (interface_unknown && interface_only
13601 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13602 || flag_alt_external_templates))
13604 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13605 interface, we will have interface_only set but not
13606 interface_known. In that case, we don't want to use the normal
13607 heuristics because someone will supply a #pragma implementation
13608 elsewhere, and deducing it here would produce a conflict. */
13609 comdat_linkage (decl1);
13610 DECL_EXTERNAL (decl1) = 0;
13611 DECL_INTERFACE_KNOWN (decl1) = 1;
13612 DECL_DEFER_OUTPUT (decl1) = 1;
13614 else
13616 /* This is a definition, not a reference.
13617 So clear DECL_EXTERNAL. */
13618 DECL_EXTERNAL (decl1) = 0;
13620 if ((DECL_DECLARED_INLINE_P (decl1)
13621 || DECL_TEMPLATE_INSTANTIATION (decl1))
13622 && ! DECL_INTERFACE_KNOWN (decl1)
13623 /* Don't try to defer nested functions for now. */
13624 && ! decl_function_context (decl1))
13625 DECL_DEFER_OUTPUT (decl1) = 1;
13626 else
13627 DECL_INTERFACE_KNOWN (decl1) = 1;
13630 pushlevel (0);
13631 current_binding_level->parm_flag = 1;
13633 ++function_depth;
13635 if (DECL_DESTRUCTOR_P (decl1))
13637 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13638 DECL_CONTEXT (dtor_label) = current_function_decl;
13641 start_fname_decls ();
13643 store_parm_decls (current_function_parms);
13645 return 1;
13648 /* Store the parameter declarations into the current function declaration.
13649 This is called after parsing the parameter declarations, before
13650 digesting the body of the function.
13652 Also install to binding contour return value identifier, if any. */
13654 static void
13655 store_parm_decls (tree current_function_parms)
13657 register tree fndecl = current_function_decl;
13658 register tree parm;
13660 /* This is a chain of any other decls that came in among the parm
13661 declarations. If a parm is declared with enum {foo, bar} x;
13662 then CONST_DECLs for foo and bar are put here. */
13663 tree nonparms = NULL_TREE;
13665 if (current_function_parms)
13667 /* This case is when the function was defined with an ANSI prototype.
13668 The parms already have decls, so we need not do anything here
13669 except record them as in effect
13670 and complain if any redundant old-style parm decls were written. */
13672 tree specparms = current_function_parms;
13673 tree next;
13675 /* Must clear this because it might contain TYPE_DECLs declared
13676 at class level. */
13677 storedecls (NULL_TREE);
13679 /* If we're doing semantic analysis, then we'll call pushdecl
13680 for each of these. We must do them in reverse order so that
13681 they end in the correct forward order. */
13682 specparms = nreverse (specparms);
13684 for (parm = specparms; parm; parm = next)
13686 next = TREE_CHAIN (parm);
13687 if (TREE_CODE (parm) == PARM_DECL)
13689 if (DECL_NAME (parm) == NULL_TREE
13690 || TREE_CODE (parm) != VOID_TYPE)
13691 pushdecl (parm);
13692 else
13693 error ("parameter `%D' declared void", parm);
13695 else
13697 /* If we find an enum constant or a type tag,
13698 put it aside for the moment. */
13699 TREE_CHAIN (parm) = NULL_TREE;
13700 nonparms = chainon (nonparms, parm);
13704 /* Get the decls in their original chain order and record in the
13705 function. This is all and only the PARM_DECLs that were
13706 pushed into scope by the loop above. */
13707 DECL_ARGUMENTS (fndecl) = getdecls ();
13708 storetags (gettags ());
13710 else
13711 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13713 /* Now store the final chain of decls for the arguments
13714 as the decl-chain of the current lexical scope.
13715 Put the enumerators in as well, at the front so that
13716 DECL_ARGUMENTS is not modified. */
13717 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13719 /* Do the starting of the exception specifications, if we have any. */
13720 if (flag_exceptions && !processing_template_decl
13721 && flag_enforce_eh_specs
13722 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13723 current_eh_spec_block = begin_eh_spec_block ();
13727 /* We have finished doing semantic analysis on DECL, but have not yet
13728 generated RTL for its body. Save away our current state, so that
13729 when we want to generate RTL later we know what to do. */
13731 static void
13732 save_function_data (tree decl)
13734 struct language_function *f;
13736 /* Save the language-specific per-function data so that we can
13737 get it back when we really expand this function. */
13738 my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13739 19990908);
13741 /* Make a copy. */
13742 f = ((struct language_function *)
13743 ggc_alloc (sizeof (struct language_function)));
13744 memcpy (f, cp_function_chain, sizeof (struct language_function));
13745 DECL_SAVED_FUNCTION_DATA (decl) = f;
13747 /* Clear out the bits we don't need. */
13748 f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13749 f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13750 f->x_named_label_uses = NULL;
13751 f->bindings = NULL;
13752 f->x_local_names = NULL;
13754 /* When we get back here again, we will be expanding. */
13755 f->x_expanding_p = 1;
13757 /* If we've already decided that we cannot inline this function, we
13758 must remember that fact when we actually go to expand the
13759 function. */
13760 if (current_function_cannot_inline)
13762 f->cannot_inline = current_function_cannot_inline;
13763 DECL_INLINE (decl) = 0;
13767 /* Add a note to mark the beginning of the main body of the constructor.
13768 This is used to set up the data structures for the cleanup regions for
13769 fully-constructed bases and members. */
13771 static void
13772 begin_constructor_body (void)
13776 /* Add a note to mark the end of the main body of the constructor. This is
13777 used to end the cleanup regions for fully-constructed bases and
13778 members. */
13780 static void
13781 finish_constructor_body (void)
13785 /* Do all the processing for the beginning of a destructor; set up the
13786 vtable pointers and cleanups for bases and members. */
13788 static void
13789 begin_destructor_body (void)
13791 tree if_stmt;
13792 tree compound_stmt;
13794 /* If the dtor is empty, and we know there is not any possible
13795 way we could use any vtable entries, before they are possibly
13796 set by a base class dtor, we don't have to setup the vtables,
13797 as we know that any base class dtor will set up any vtables
13798 it needs. We avoid MI, because one base class dtor can do a
13799 virtual dispatch to an overridden function that would need to
13800 have a non-related vtable set up, we cannot avoid setting up
13801 vtables in that case. We could change this to see if there
13802 is just one vtable.
13804 ??? In the destructor for a class, the vtables are set
13805 appropriately for that class. There will be no non-related
13806 vtables. jason 2001-12-11. */
13807 if_stmt = begin_if_stmt ();
13809 /* If it is not safe to avoid setting up the vtables, then
13810 someone will change the condition to be boolean_true_node.
13811 (Actually, for now, we do not have code to set the condition
13812 appropriately, so we just assume that we always need to
13813 initialize the vtables.) */
13814 finish_if_stmt_cond (boolean_true_node, if_stmt);
13816 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13818 /* Make all virtual function table pointers in non-virtual base
13819 classes point to CURRENT_CLASS_TYPE's virtual function
13820 tables. */
13821 initialize_vtbl_ptrs (current_class_ptr);
13823 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13824 finish_then_clause (if_stmt);
13825 finish_if_stmt ();
13827 /* And insert cleanups for our bases and members so that they
13828 will be properly destroyed if we throw. */
13829 push_base_cleanups ();
13832 /* At the end of every destructor we generate code to delete the object if
13833 necessary. Do that now. */
13835 static void
13836 finish_destructor_body (void)
13838 tree exprstmt;
13840 /* Any return from a destructor will end up here; that way all base
13841 and member cleanups will be run when the function returns. */
13842 add_stmt (build_stmt (LABEL_STMT, dtor_label));
13844 /* In a virtual destructor, we must call delete. */
13845 if (DECL_VIRTUAL_P (current_function_decl))
13847 tree if_stmt;
13848 tree virtual_size = cxx_sizeof (current_class_type);
13850 /* [class.dtor]
13852 At the point of definition of a virtual destructor (including
13853 an implicit definition), non-placement operator delete shall
13854 be looked up in the scope of the destructor's class and if
13855 found shall be accessible and unambiguous. */
13856 exprstmt = build_op_delete_call
13857 (DELETE_EXPR, current_class_ptr, virtual_size,
13858 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13860 if_stmt = begin_if_stmt ();
13861 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13862 current_in_charge_parm,
13863 integer_one_node),
13864 if_stmt);
13865 finish_expr_stmt (exprstmt);
13866 finish_then_clause (if_stmt);
13867 finish_if_stmt ();
13871 /* Do the necessary processing for the beginning of a function body, which
13872 in this case includes member-initializers, but not the catch clauses of
13873 a function-try-block. Currently, this means opening a binding level
13874 for the member-initializers (in a ctor) and member cleanups (in a dtor).
13875 In other functions, this isn't necessary, but it doesn't hurt. */
13877 tree
13878 begin_function_body (void)
13880 tree stmt;
13882 if (processing_template_decl)
13883 /* Do nothing now. */;
13884 else
13885 /* Always keep the BLOCK node associated with the outermost pair of
13886 curly braces of a function. These are needed for correct
13887 operation of dwarfout.c. */
13888 keep_next_level (1);
13890 stmt = begin_compound_stmt (0);
13891 COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13893 if (processing_template_decl)
13894 /* Do nothing now. */;
13895 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13896 begin_constructor_body ();
13897 else if (DECL_DESTRUCTOR_P (current_function_decl))
13898 begin_destructor_body ();
13900 return stmt;
13903 /* Do the processing for the end of a function body. Currently, this means
13904 closing out the cleanups for fully-constructed bases and members, and in
13905 the case of the destructor, deleting the object if desired. Again, this
13906 is only meaningful for [cd]tors, since they are the only functions where
13907 there is a significant distinction between the main body and any
13908 function catch clauses. Handling, say, main() return semantics here
13909 would be wrong, as flowing off the end of a function catch clause for
13910 main() would also need to return 0. */
13912 void
13913 finish_function_body (tree compstmt)
13915 /* Close the block. */
13916 finish_compound_stmt (0, compstmt);
13918 if (processing_template_decl)
13919 /* Do nothing now. */;
13920 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13921 finish_constructor_body ();
13922 else if (DECL_DESTRUCTOR_P (current_function_decl))
13923 finish_destructor_body ();
13926 /* Finish up a function declaration and compile that function
13927 all the way to assembler language output. The free the storage
13928 for the function definition.
13930 FLAGS is a bitwise or of the following values:
13931 2 - INCLASS_INLINE
13932 We just finished processing the body of an in-class inline
13933 function definition. (This processing will have taken place
13934 after the class definition is complete.) */
13936 tree
13937 finish_function (int flags)
13939 register tree fndecl = current_function_decl;
13940 tree fntype, ctype = NULL_TREE;
13941 int inclass_inline = (flags & 2) != 0;
13942 int nested;
13944 /* When we get some parse errors, we can end up without a
13945 current_function_decl, so cope. */
13946 if (fndecl == NULL_TREE)
13947 return error_mark_node;
13949 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13950 && DECL_VIRTUAL_P (fndecl)
13951 && !processing_template_decl)
13953 tree fnclass = DECL_CONTEXT (fndecl);
13954 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
13955 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
13958 nested = function_depth > 1;
13959 fntype = TREE_TYPE (fndecl);
13961 /* TREE_READONLY (fndecl) = 1;
13962 This caused &foo to be of type ptr-to-const-function
13963 which then got a warning when stored in a ptr-to-function variable. */
13965 my_friendly_assert (building_stmt_tree (), 20000911);
13967 finish_fname_decls ();
13969 /* For a cloned function, we've already got all the code we need;
13970 there's no need to add any extra bits. */
13971 if (!DECL_CLONED_FUNCTION_P (fndecl))
13973 if (DECL_MAIN_P (current_function_decl))
13975 /* Make it so that `main' always returns 0 by default. */
13976 #if VMS_TARGET
13977 finish_return_stmt (integer_one_node);
13978 #else
13979 finish_return_stmt (integer_zero_node);
13980 #endif
13983 /* Finish dealing with exception specifiers. */
13984 if (flag_exceptions && !processing_template_decl
13985 && flag_enforce_eh_specs
13986 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13987 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
13988 (TREE_TYPE (current_function_decl)),
13989 current_eh_spec_block);
13992 /* If we're saving up tree structure, tie off the function now. */
13993 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
13995 /* This must come after expand_function_end because cleanups might
13996 have declarations (from inline functions) that need to go into
13997 this function's blocks. */
13999 /* If the current binding level isn't the outermost binding level
14000 for this function, either there is a bug, or we have experienced
14001 syntax errors and the statement tree is malformed. */
14002 if (current_binding_level->parm_flag != 1)
14004 /* Make sure we have already experienced errors. */
14005 if (errorcount == 0)
14006 abort ();
14008 /* Throw away the broken statement tree and extra binding
14009 levels. */
14010 DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14012 while (current_binding_level->parm_flag != 1)
14014 if (current_binding_level->parm_flag == 2)
14015 pop_nested_class ();
14016 else
14017 poplevel (0, 0, 0);
14020 poplevel (1, 0, 1);
14022 /* Set up the named return value optimization, if we can. Here, we
14023 eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14024 for the nrv. genrtl_start_function and declare_return_variable
14025 handle making the nrv and RESULT_DECL share space. */
14026 if (current_function_return_value)
14028 tree r = current_function_return_value;
14029 /* This is only worth doing for fns that return in memory--and
14030 simpler, since we don't have to worry about promoted modes. */
14031 if (r != error_mark_node
14032 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl))))
14034 DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14035 walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14036 nullify_returns_r, r);
14038 else
14039 /* Clear it so genrtl_start_function and declare_return_variable
14040 know we're not optimizing. */
14041 current_function_return_value = NULL_TREE;
14044 /* Remember that we were in class scope. */
14045 if (current_class_name)
14046 ctype = current_class_type;
14048 /* Must mark the RESULT_DECL as being in this function. */
14049 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14051 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14052 to the FUNCTION_DECL node itself. */
14053 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14055 /* Save away current state, if appropriate. */
14056 if (!processing_template_decl)
14057 save_function_data (fndecl);
14059 /* If this function calls `setjmp' it cannot be inlined. When
14060 `longjmp' is called it is not guaranteed to restore the value of
14061 local variables that have been modified since the call to
14062 `setjmp'. So, if were to inline this function into some caller
14063 `c', then when we `longjmp', we might not restore all variables
14064 in `c'. (It might seem, at first blush, that there's no way for
14065 this function to modify local variables in `c', but their
14066 addresses may have been stored somewhere accessible to this
14067 function.) */
14068 if (!processing_template_decl && calls_setjmp_p (fndecl))
14069 DECL_UNINLINABLE (fndecl) = 1;
14071 /* Complain if there's just no return statement. */
14072 if (warn_return_type
14073 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14074 && !current_function_returns_value && !current_function_returns_null
14075 /* Don't complain if we abort or throw. */
14076 && !current_function_returns_abnormally
14077 && !DECL_NAME (DECL_RESULT (fndecl))
14078 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
14079 inline function, as we might never be compiled separately. */
14080 && (DECL_INLINE (fndecl) || processing_template_decl))
14081 warning ("no return statement in function returning non-void");
14083 /* Clear out memory we no longer need. */
14084 free_after_parsing (cfun);
14085 /* Since we never call rest_of_compilation, we never clear
14086 CFUN. Do so explicitly. */
14087 free_after_compilation (cfun);
14088 cfun = NULL;
14090 /* If this is an in-class inline definition, we may have to pop the
14091 bindings for the template parameters that we added in
14092 maybe_begin_member_template_processing when start_function was
14093 called. */
14094 if (inclass_inline)
14095 maybe_end_member_template_processing ();
14097 /* Leave the scope of the class. */
14098 if (ctype)
14099 pop_nested_class ();
14101 --function_depth;
14103 /* Clean up. */
14104 if (! nested)
14105 /* Let the error reporting routines know that we're outside a
14106 function. For a nested function, this value is used in
14107 cxx_pop_function_context and then reset via pop_function_context. */
14108 current_function_decl = NULL_TREE;
14110 return fndecl;
14113 /* Create the FUNCTION_DECL for a function definition.
14114 DECLSPECS and DECLARATOR are the parts of the declaration;
14115 they describe the return type and the name of the function,
14116 but twisted together in a fashion that parallels the syntax of C.
14118 This function creates a binding context for the function body
14119 as well as setting up the FUNCTION_DECL in current_function_decl.
14121 Returns a FUNCTION_DECL on success.
14123 If the DECLARATOR is not suitable for a function (it defines a datum
14124 instead), we return 0, which tells yyparse to report a parse error.
14126 May return void_type_node indicating that this method is actually
14127 a friend. See grokfield for more details.
14129 Came here with a `.pushlevel' .
14131 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14132 CHANGES TO CODE IN `grokfield'. */
14134 tree
14135 start_method (tree declspecs, tree declarator, tree attrlist)
14137 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14138 &attrlist);
14140 if (fndecl == error_mark_node)
14141 return error_mark_node;
14143 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14145 error ("invalid member function declaration");
14146 return error_mark_node;
14149 if (attrlist)
14150 cplus_decl_attributes (&fndecl, attrlist, 0);
14152 /* Pass friends other than inline friend functions back. */
14153 if (fndecl == void_type_node)
14154 return fndecl;
14156 if (DECL_IN_AGGR_P (fndecl))
14158 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14160 if (DECL_CONTEXT (fndecl)
14161 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14162 error ("`%D' is already defined in class `%T'", fndecl,
14163 DECL_CONTEXT (fndecl));
14165 return void_type_node;
14168 check_template_shadow (fndecl);
14170 DECL_DECLARED_INLINE_P (fndecl) = 1;
14172 if (flag_default_inline)
14173 DECL_INLINE (fndecl) = 1;
14175 /* We process method specializations in finish_struct_1. */
14176 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14177 fndecl = push_template_decl (fndecl);
14179 if (! DECL_FRIEND_P (fndecl))
14181 if (TREE_CHAIN (fndecl))
14183 fndecl = copy_node (fndecl);
14184 TREE_CHAIN (fndecl) = NULL_TREE;
14186 grok_special_member_properties (fndecl);
14189 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14191 /* Make a place for the parms */
14192 pushlevel (0);
14193 current_binding_level->parm_flag = 1;
14195 DECL_IN_AGGR_P (fndecl) = 1;
14196 return fndecl;
14199 /* Go through the motions of finishing a function definition.
14200 We don't compile this method until after the whole class has
14201 been processed.
14203 FINISH_METHOD must return something that looks as though it
14204 came from GROKFIELD (since we are defining a method, after all).
14206 This is called after parsing the body of the function definition.
14207 STMTS is the chain of statements that makes up the function body.
14209 DECL is the ..._DECL that `start_method' provided. */
14211 tree
14212 finish_method (tree decl)
14214 register tree fndecl = decl;
14215 tree old_initial;
14217 register tree link;
14219 if (decl == void_type_node)
14220 return decl;
14222 old_initial = DECL_INITIAL (fndecl);
14224 /* Undo the level for the parms (from start_method).
14225 This is like poplevel, but it causes nothing to be
14226 saved. Saving information here confuses symbol-table
14227 output routines. Besides, this information will
14228 be correctly output when this method is actually
14229 compiled. */
14231 /* Clear out the meanings of the local variables of this level;
14232 also record in each decl which block it belongs to. */
14234 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14236 if (DECL_NAME (link) != NULL_TREE)
14237 pop_binding (DECL_NAME (link), link);
14238 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14239 DECL_CONTEXT (link) = NULL_TREE;
14242 poplevel (0, 0, 0);
14244 DECL_INITIAL (fndecl) = old_initial;
14246 /* We used to check if the context of FNDECL was different from
14247 current_class_type as another way to get inside here. This didn't work
14248 for String.cc in libg++. */
14249 if (DECL_FRIEND_P (fndecl))
14251 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14252 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14253 decl = void_type_node;
14256 return decl;
14260 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14261 we can lay it out later, when and if its type becomes complete. */
14263 void
14264 maybe_register_incomplete_var (tree var)
14266 my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14268 /* Keep track of variables with incomplete types. */
14269 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14270 && DECL_EXTERNAL (var))
14272 tree inner_type = TREE_TYPE (var);
14274 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14275 inner_type = TREE_TYPE (inner_type);
14276 inner_type = TYPE_MAIN_VARIANT (inner_type);
14278 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14279 /* RTTI TD entries are created while defining the type_info. */
14280 || (TYPE_LANG_SPECIFIC (inner_type)
14281 && TYPE_BEING_DEFINED (inner_type)))
14282 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14286 /* Called when a class type (given by TYPE) is defined. If there are
14287 any existing VAR_DECLs whose type hsa been completed by this
14288 declaration, update them now. */
14290 void
14291 complete_vars (tree type)
14293 tree *list = &incomplete_vars;
14295 my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14296 while (*list)
14298 if (same_type_p (type, TREE_PURPOSE (*list)))
14300 tree var = TREE_VALUE (*list);
14301 /* Complete the type of the variable. The VAR_DECL itself
14302 will be laid out in expand_expr. */
14303 complete_type (TREE_TYPE (var));
14304 /* Remove this entry from the list. */
14305 *list = TREE_CHAIN (*list);
14307 else
14308 list = &TREE_CHAIN (*list);
14312 /* If DECL is of a type which needs a cleanup, build that cleanup
14313 here. */
14315 tree
14316 cxx_maybe_build_cleanup (tree decl)
14318 tree type = TREE_TYPE (decl);
14320 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14322 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14323 tree rval;
14325 if (TREE_CODE (type) == ARRAY_TYPE)
14326 rval = decl;
14327 else
14329 cxx_mark_addressable (decl);
14330 rval = build_unary_op (ADDR_EXPR, decl, 0);
14333 /* Optimize for space over speed here. */
14334 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14335 || flag_expensive_optimizations)
14336 flags |= LOOKUP_NONVIRTUAL;
14338 rval = build_delete (TREE_TYPE (rval), rval,
14339 sfk_complete_destructor, flags, 0);
14341 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14342 && ! TYPE_HAS_DESTRUCTOR (type))
14343 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
14344 build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
14346 return rval;
14348 return NULL_TREE;
14351 /* When a stmt has been parsed, this function is called. */
14353 void
14354 finish_stmt (void)
14356 /* Always assume this statement was not an expression statement. If
14357 it actually was an expression statement, its our callers
14358 responsibility to fix this up. */
14359 last_expr_type = NULL_TREE;
14362 /* DECL was originally constructed as a non-static member function,
14363 but turned out to be static. Update it accordingly. */
14365 void
14366 revert_static_member_fn (tree decl)
14368 tree tmp;
14369 tree function = TREE_TYPE (decl);
14370 tree args = TYPE_ARG_TYPES (function);
14372 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14373 != TYPE_UNQUALIFIED)
14374 error ("static member function `%#D' declared with type qualifiers",
14375 decl);
14377 args = TREE_CHAIN (args);
14378 tmp = build_function_type (TREE_TYPE (function), args);
14379 tmp = build_qualified_type (tmp, cp_type_quals (function));
14380 tmp = build_exception_variant (tmp,
14381 TYPE_RAISES_EXCEPTIONS (function));
14382 TREE_TYPE (decl) = tmp;
14383 if (DECL_ARGUMENTS (decl))
14384 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14385 DECL_STATIC_FUNCTION_P (decl) = 1;
14388 /* Initialize the variables used during compilation of a C++
14389 function. */
14391 void
14392 cxx_push_function_context (struct function * f)
14394 struct language_function *p
14395 = ((struct language_function *)
14396 ggc_alloc_cleared (sizeof (struct language_function)));
14397 f->language = p;
14399 /* It takes an explicit call to expand_body to generate RTL for a
14400 function. */
14401 expanding_p = 0;
14403 /* Whenever we start a new function, we destroy temporaries in the
14404 usual way. */
14405 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14408 /* Free the language-specific parts of F, now that we've finished
14409 compiling the function. */
14411 void
14412 cxx_pop_function_context (struct function * f)
14414 f->language = 0;
14417 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14418 one of the language-independent trees. */
14420 enum cp_tree_node_structure_enum
14421 cp_tree_node_structure (union lang_tree_node * t)
14423 switch (TREE_CODE (&t->generic))
14425 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
14426 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
14427 case CPLUS_BINDING: return TS_CP_BINDING;
14428 case OVERLOAD: return TS_CP_OVERLOAD;
14429 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14430 case PTRMEM_CST: return TS_CP_PTRMEM;
14431 case BASELINK: return TS_CP_BASELINK;
14432 case WRAPPER: return TS_CP_WRAPPER;
14433 case SRCLOC: return TS_CP_SRCLOC;
14434 default: return TS_CP_GENERIC;
14438 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14439 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
14441 tree
14442 identifier_global_value (tree t)
14444 return IDENTIFIER_GLOBAL_VALUE (t);
14447 /* Build the void_list_node (void_type_node having been created). */
14448 tree
14449 build_void_list_node (void)
14451 tree t = build_tree_list (NULL_TREE, void_type_node);
14452 TREE_PARMLIST (t) = 1;
14453 return t;
14456 static int
14457 cp_missing_noreturn_ok_p (tree decl)
14459 /* A missing noreturn is ok for the `main' function. */
14460 return DECL_MAIN_P (decl);
14463 #include "gt-cp-decl.h"
14464 #include "gtype-cp.h"