* decl.c (cxx_scope_descriptor): Fix thinko.
[official-gcc.git] / gcc / cp / decl.c
blob095c6fdd61b48a1d9d07f766d08906b574cb35da
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* Process declarations and symbol lookup for C++ front end.
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
28 /* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "expr.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "lex.h"
43 #include "output.h"
44 #include "except.h"
45 #include "toplev.h"
46 #include "hashtab.h"
47 #include "tm_p.h"
48 #include "target.h"
49 #include "c-common.h"
50 #include "c-pragma.h"
51 #include "diagnostic.h"
52 #include "debug.h"
53 #include "timevar.h"
55 static tree grokparms (tree);
56 static const char *redeclaration_error_message (tree, tree);
58 static void push_binding_level (cxx_scope *);
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 int decl_jump_unsafe (tree);
63 static void storedecls (tree);
64 static void require_complete_types_for_parms (tree);
65 static int ambi_op_p (enum tree_code);
66 static int unary_op_p (enum tree_code);
67 static cxx_saved_binding *store_bindings (tree, cxx_saved_binding *);
68 static tree lookup_tag_reverse (tree, tree);
69 static void push_local_name (tree);
70 static void warn_extern_redeclared_static (tree, tree);
71 static tree grok_reference_init (tree, tree, tree, tree *);
72 static tree grokfndecl (tree, tree, tree, tree, int,
73 enum overload_flags, tree,
74 tree, int, int, int, int, int, int, tree);
75 static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
76 static tree follow_tag_typedef (tree);
77 static tree lookup_tag (enum tree_code, tree,
78 struct cp_binding_level *, int);
79 static void set_identifier_type_value_with_scope
80 (tree, tree, struct cp_binding_level *);
81 static void record_unknown_type (tree, const char *);
82 static tree builtin_function_1 (const char *, tree, tree, int,
83 enum built_in_class, const char *,
84 tree);
85 static tree build_library_fn_1 (tree, enum tree_code, tree);
86 static int member_function_or_else (tree, tree, enum overload_flags);
87 static void bad_specifiers (tree, const char *, int, int, int, int,
88 int);
89 static tree maybe_process_template_type_declaration
90 (tree, int, struct cp_binding_level*);
91 static void check_for_uninitialized_const_var (tree);
92 static hashval_t typename_hash (const void *);
93 static int typename_compare (const void *, const void *);
94 static void push_binding (tree, tree, struct cp_binding_level*);
95 static void pop_binding (tree, tree);
96 static tree local_variable_p_walkfn (tree *, int *, void *);
97 static tree select_decl (cxx_binding *, int);
98 static int lookup_flags (int, int);
99 static tree qualify_lookup (tree, int);
100 static tree record_builtin_java_type (const char *, int);
101 static const char *tag_name (enum tag_types code);
102 static void find_class_binding_level (void);
103 static struct cp_binding_level *innermost_nonclass_level (void);
104 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
105 static int walk_globals_r (tree, void*);
106 static int walk_vtables_r (tree, void*);
107 static void add_decl_to_level (tree, struct cp_binding_level *);
108 static tree make_label_decl (tree, int);
109 static void use_label (tree);
110 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
111 const location_t *);
112 static void check_previous_goto (struct named_label_use_list *);
113 static void check_switch_goto (struct cp_binding_level *);
114 static void check_previous_gotos (tree);
115 static void pop_label (tree, tree);
116 static void pop_labels (tree);
117 static void maybe_deduce_size_from_array_init (tree, tree);
118 static void layout_var_decl (tree);
119 static void maybe_commonize_var (tree);
120 static tree check_initializer (tree, tree, int, tree *);
121 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
122 static void save_function_data (tree);
123 static void check_function_type (tree, tree);
124 static void begin_constructor_body (void);
125 static void finish_constructor_body (void);
126 static void begin_destructor_body (void);
127 static void finish_destructor_body (void);
128 static tree create_array_type_for_decl (tree, tree, tree);
129 static tree get_atexit_node (void);
130 static tree get_dso_handle_node (void);
131 static tree start_cleanup_fn (void);
132 static void end_cleanup_fn (void);
133 static tree cp_make_fname_decl (tree, int);
134 static void initialize_predefined_identifiers (void);
135 static tree check_special_function_return_type
136 (special_function_kind, tree, tree);
137 static tree push_cp_library_fn (enum tree_code, tree);
138 static tree build_cp_library_fn (tree, enum tree_code, tree);
139 static void store_parm_decls (tree);
140 static int cp_missing_noreturn_ok_p (tree);
141 static void initialize_local_var (tree, tree);
142 static void expand_static_init (tree, tree);
143 static tree next_initializable_field (tree);
144 static tree reshape_init (tree, tree *);
145 static tree build_typename_type (tree, tree, tree);
147 /* Erroneous argument lists can use this *IFF* they do not modify it. */
148 tree error_mark_list;
150 /* The following symbols are subsumed in the cp_global_trees array, and
151 listed here individually for documentation purposes.
153 C++ extensions
154 tree wchar_decl_node;
156 tree vtable_entry_type;
157 tree delta_type_node;
158 tree __t_desc_type_node;
159 tree ti_desc_type_node;
160 tree bltn_desc_type_node, ptr_desc_type_node;
161 tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
162 tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
163 tree ptm_desc_type_node;
164 tree base_desc_type_node;
166 tree class_type_node, record_type_node, union_type_node, enum_type_node;
167 tree unknown_type_node;
169 Array type `vtable_entry_type[]'
171 tree vtbl_type_node;
172 tree vtbl_ptr_type_node;
174 Namespaces,
176 tree std_node;
177 tree abi_node;
179 A FUNCTION_DECL which can call `abort'. Not necessarily the
180 one that the user will declare, but sufficient to be called
181 by routines that want to abort the program.
183 tree abort_fndecl;
185 The FUNCTION_DECL for the default `::operator delete'.
187 tree global_delete_fndecl;
189 Used by RTTI
190 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
191 tree tinfo_var_id;
195 tree cp_global_trees[CPTI_MAX];
197 /* Indicates that there is a type value in some namespace, although
198 that is not necessarily in scope at the moment. */
200 static GTY(()) tree global_type_node;
202 /* The node that holds the "name" of the global scope. */
203 static GTY(()) tree global_scope_name;
205 /* Used only for jumps to as-yet undefined labels, since jumps to
206 defined labels can have their validity checked immediately. */
208 struct named_label_use_list GTY(())
210 struct cp_binding_level *binding_level;
211 tree names_in_scope;
212 tree label_decl;
213 location_t o_goto_locus;
214 struct named_label_use_list *next;
217 #define named_label_uses cp_function_chain->x_named_label_uses
219 #define local_names cp_function_chain->x_local_names
221 /* A list of objects which have constructors or destructors
222 which reside in the global scope. The decl is stored in
223 the TREE_VALUE slot and the initializer is stored
224 in the TREE_PURPOSE slot. */
225 tree static_aggregates;
227 /* -- end of C++ */
229 /* A node for the integer constants 2, and 3. */
231 tree integer_two_node, integer_three_node;
233 /* Similar, for last_function_parm_tags. */
234 tree last_function_parms;
236 /* A list of all LABEL_DECLs in the function that have names. Here so
237 we can clear out their names' definitions at the end of the
238 function, and so we can check the validity of jumps to these labels. */
240 struct named_label_list GTY(())
242 struct cp_binding_level *binding_level;
243 tree names_in_scope;
244 tree old_value;
245 tree label_decl;
246 tree bad_decls;
247 struct named_label_list *next;
248 unsigned int in_try_scope : 1;
249 unsigned int in_catch_scope : 1;
252 #define named_labels cp_function_chain->x_named_labels
254 /* The name of the anonymous namespace, throughout this translation
255 unit. */
256 tree anonymous_namespace_name;
258 /* The number of function bodies which we are currently processing.
259 (Zero if we are at namespace scope, one inside the body of a
260 function, two inside the body of a function in a local class, etc.) */
261 int function_depth;
263 /* States indicating how grokdeclarator() should handle declspecs marked
264 with __attribute__((deprecated)). An object declared as
265 __attribute__((deprecated)) suppresses warnings of uses of other
266 deprecated items. */
268 enum deprecated_states {
269 DEPRECATED_NORMAL,
270 DEPRECATED_SUPPRESS
273 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
275 /* Set by add_implicitly_declared_members() to keep those members from
276 being flagged as deprecated or reported as using deprecated
277 types. */
278 int adding_implicit_members = 0;
280 /* True if a declaration with an `extern' linkage specifier is being
281 processed. */
282 bool have_extern_spec;
285 /* For each binding contour we allocate a binding_level structure
286 which records the names defined in that contour.
287 Contours include:
288 0) the global one
289 1) one for each function definition,
290 where internal declarations of the parameters appear.
291 2) one for each compound statement,
292 to record its declarations.
294 The current meaning of a name can be found by searching the levels
295 from the current one out to the global one.
297 Off to the side, may be the class_binding_level. This exists only
298 to catch class-local declarations. It is otherwise nonexistent.
300 Also there may be binding levels that catch cleanups that must be
301 run when exceptions occur. Thus, to see whether a name is bound in
302 the current scope, it is not enough to look in the
303 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
304 instead. */
306 /* Note that the information in the `names' component of the global contour
307 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
309 struct cp_binding_level GTY(())
311 /* A chain of _DECL nodes for all variables, constants, functions,
312 and typedef types. These are in the reverse of the order
313 supplied. There may be OVERLOADs on this list, too, but they
314 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
315 tree names;
317 /* Count of elements in names chain. */
318 size_t names_size;
320 /* A chain of NAMESPACE_DECL nodes. */
321 tree namespaces;
323 /* An array of static functions and variables (for namespaces only) */
324 varray_type static_decls;
326 /* A chain of VTABLE_DECL nodes. */
327 tree vtables;
329 /* A dictionary for looking up user-defined-types. */
330 binding_table type_decls;
332 /* A list of USING_DECL nodes. */
333 tree usings;
335 /* A list of used namespaces. PURPOSE is the namespace,
336 VALUE the common ancestor with this binding_level's namespace. */
337 tree using_directives;
339 /* If this binding level is the binding level for a class, then
340 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
341 is the name of an entity bound in the class. The TREE_TYPE is
342 the DECL bound by this name in the class. */
343 tree class_shadowed;
345 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
346 is used for all binding levels. In addition the TREE_VALUE is the
347 IDENTIFIER_TYPE_VALUE before we entered the class. */
348 tree type_shadowed;
350 /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local
351 label in this scope. The TREE_PURPOSE is the previous value of
352 the IDENTIFIER_LABEL VALUE. */
353 tree shadowed_labels;
355 /* For each level (except not the global one),
356 a chain of BLOCK nodes for all the levels
357 that were entered and exited one level down. */
358 tree blocks;
360 /* The entity (namespace, class, function) the scope of which this
361 binding contour corresponds to. Otherwise NULL. */
362 tree this_entity;
364 /* The binding level which this one is contained in (inherits from). */
365 struct cp_binding_level *level_chain;
367 /* List of VAR_DECLS saved from a previous for statement.
368 These would be dead in ISO-conforming code, but might
369 be referenced in ARM-era code. These are stored in a
370 TREE_LIST; the TREE_VALUE is the actual declaration. */
371 tree dead_vars_from_for;
373 /* Binding depth at which this level began. */
374 int binding_depth;
376 /* The kind of scope that this object represents. However, a
377 SK_TEMPLATE_SPEC scope is represented with KIND set to
378 SK_TEMPALTE_PARMS and EXPLICIT_SPEC_P set to true. */
379 enum scope_kind kind : 4;
381 /* True if this scope is an SK_TEMPLATE_SPEC scope. This field is
382 only valid if KIND == SK_TEMPLATE_PARMS. */
383 bool explicit_spec_p : 1;
385 /* 1 means make a BLOCK for this level regardless of all else.
386 2 for temporary binding contours created by the compiler. */
387 unsigned keep : 2;
389 /* Nonzero if this level can safely have additional
390 cleanup-needing variables added to it. */
391 unsigned more_cleanups_ok : 1;
392 unsigned have_cleanups : 1;
394 /* Nonzero if this level "doesn't exist" for tags. */
395 unsigned tag_transparent : 1;
397 /* 20 bits left to fill a 32-bit word. */
400 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
402 /* The binding level currently in effect. */
404 #define current_binding_level \
405 (*(cfun && cp_function_chain->bindings \
406 ? &cp_function_chain->bindings \
407 : &scope_chain->bindings))
409 /* The binding level of the current class, if any. */
411 #define class_binding_level scope_chain->class_bindings
413 /* A chain of binding_level structures awaiting reuse. */
415 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
417 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
419 static int keep_next_level_flag;
421 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
422 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
423 time the VAR_DECL was declared, the type was incomplete. */
425 static GTY(()) tree incomplete_vars;
427 #ifndef ENABLE_SCOPE_CHECKING
428 # define ENABLE_SCOPE_CHECKING 0
429 #else
430 # define ENABLE_SCOPE_CHECKING 1
431 #endif
433 static int binding_depth = 0;
434 static int is_class_level = 0;
436 static void
437 indent (int depth)
439 int i;
441 for (i = 0; i < depth * 2; i++)
442 putc (' ', stderr);
445 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
447 /* Return a string describing the kind of SCOPE we have. */
448 static const char *
449 cxx_scope_descriptor (cxx_scope *scope)
451 /* The order of this table must match the "scope_kind"
452 enumerators. */
453 static const char* scope_kind_names[] = {
454 "block-scope",
455 "try-scope",
456 "catch-scope",
457 "for-scope",
458 "function-parameter-scope",
459 "class-scope",
460 "namespace-scope",
461 "template-parameter-scope",
462 "template-explicit-spec-scope"
465 return scope_kind_names[scope->kind];
468 /* Output a debugging information about SCOPE when performning
469 ACTION at LINE. */
470 static void
471 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
473 const char *desc = cxx_scope_descriptor (scope);
474 if (scope->this_entity)
475 verbatim ("%s %s(%E) %p %d\n", action, desc,
476 scope->this_entity, (void *) scope, line);
477 else
478 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
481 /* Construct a scope that may be TAG-TRANSPARENT, the sub-blocks of
482 which may be KEPT. */
483 static inline cxx_scope *
484 make_cxx_scope (bool tag_transparent, int keep)
486 cxx_scope *scope;
488 /* Reuse or create a struct for this binding level. */
489 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
491 scope = free_binding_level;
492 free_binding_level = scope->level_chain;
494 else
495 scope = ggc_alloc (sizeof (cxx_scope));
497 memset (scope, 0, sizeof (cxx_scope));
498 scope->tag_transparent = tag_transparent;
499 scope->keep = keep;
500 scope->more_cleanups_ok = true;
502 return scope;
505 static void
506 push_binding_level (cxx_scope *newlevel)
508 /* Add this level to the front of the chain (stack) of levels that
509 are active. */
510 newlevel->level_chain = current_binding_level;
511 current_binding_level = newlevel;
513 if (ENABLE_SCOPE_CHECKING)
515 newlevel->binding_depth = binding_depth;
516 indent (binding_depth);
517 cxx_scope_debug (newlevel, input_location.line, "push");
518 is_class_level = 0;
519 binding_depth++;
523 /* Find the innermost enclosing class scope, and reset
524 CLASS_BINDING_LEVEL appropriately. */
526 static void
527 find_class_binding_level (void)
529 struct cp_binding_level *level = current_binding_level;
531 while (level && level->kind != sk_class)
532 level = level->level_chain;
533 if (level && level->kind == sk_class)
534 class_binding_level = level;
535 else
536 class_binding_level = 0;
539 static void
540 pop_binding_level (void)
542 if (NAMESPACE_LEVEL (global_namespace))
543 /* Cannot pop a level, if there are none left to pop. */
544 my_friendly_assert (!global_scope_p (current_binding_level), 20030527);
545 /* Pop the current level, and free the structure for reuse. */
546 if (ENABLE_SCOPE_CHECKING)
548 indent (--binding_depth);
549 cxx_scope_debug (current_binding_level, input_location.line, "pop");
550 if (is_class_level != (current_binding_level == class_binding_level))
552 indent (binding_depth);
553 verbatim ("XXX is_class_level != (current_binding_level "
554 "== class_binding_level)\n");
556 is_class_level = 0;
559 register struct cp_binding_level *level = current_binding_level;
560 current_binding_level = current_binding_level->level_chain;
561 level->level_chain = free_binding_level;
562 if (level->kind == sk_class)
563 level->type_decls = NULL;
564 else
565 binding_table_free (level->type_decls);
566 my_friendly_assert (!ENABLE_SCOPE_CHECKING
567 || level->binding_depth == binding_depth,
568 20030529);
569 free_binding_level = level;
570 find_class_binding_level ();
574 static void
575 suspend_binding_level (void)
577 if (class_binding_level)
578 current_binding_level = class_binding_level;
580 if (NAMESPACE_LEVEL (global_namespace))
581 /* Cannot suspend a level, if there are none left to suspend. */
582 my_friendly_assert (!global_scope_p (current_binding_level), 20030527);
583 /* Suspend the current level. */
584 if (ENABLE_SCOPE_CHECKING)
586 indent (--binding_depth);
587 cxx_scope_debug (current_binding_level, input_location.line, "suspend");
588 if (is_class_level != (current_binding_level == class_binding_level))
590 indent (binding_depth);
591 verbatim ("XXX is_class_level != (current_binding_level "
592 "== class_binding_level)\n");
594 is_class_level = 0;
596 current_binding_level = current_binding_level->level_chain;
597 find_class_binding_level ();
600 static void
601 resume_binding_level (struct cp_binding_level* b)
603 /* Resuming binding levels is meant only for namespaces,
604 and those cannot nest into classes. */
605 my_friendly_assert(!class_binding_level, 386);
606 /* Also, resuming a non-directly nested namespace is a no-no. */
607 my_friendly_assert(b->level_chain == current_binding_level, 386);
608 current_binding_level = b;
609 if (ENABLE_SCOPE_CHECKING)
611 b->binding_depth = binding_depth;
612 indent (binding_depth);
613 cxx_scope_debug (b, input_location.line, "resume");
614 is_class_level = 0;
615 binding_depth++;
619 /* Nonzero if we are currently in the global binding level. */
622 global_bindings_p (void)
624 return global_scope_p (current_binding_level);
627 /* Return the innermost binding level that is not for a class scope. */
629 static struct cp_binding_level *
630 innermost_nonclass_level (void)
632 struct cp_binding_level *b;
634 b = current_binding_level;
635 while (b->kind == sk_class)
636 b = b->level_chain;
638 return b;
641 /* Nonzero if we are currently in a toplevel binding level. This
642 means either the global binding level or a namespace in a toplevel
643 binding level. Since there are no non-toplevel namespace levels,
644 this really means any namespace or template parameter level. We
645 also include a class whose context is toplevel. */
648 toplevel_bindings_p (void)
650 struct cp_binding_level *b = innermost_nonclass_level ();
652 return b->kind == sk_namespace || b->kind == sk_template_parms;
655 /* Nonzero if this is a namespace scope, or if we are defining a class
656 which is itself at namespace scope, or whose enclosing class is
657 such a class, etc. */
660 namespace_bindings_p (void)
662 struct cp_binding_level *b = innermost_nonclass_level ();
664 return b->kind == sk_namespace;
667 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
668 unconditionally. Otherwise, use the normal logic to decide whether
669 or not to create a BLOCK. */
671 void
672 keep_next_level (int keep)
674 keep_next_level_flag = keep;
677 /* Nonzero if the current level needs to have a BLOCK made. */
680 kept_level_p (void)
682 return (current_binding_level->blocks != NULL_TREE
683 || current_binding_level->keep
684 || current_binding_level->names != NULL_TREE
685 || (current_binding_level->type_decls != NULL
686 && !current_binding_level->tag_transparent));
689 /* Returns the kind of the innermost scope. */
691 scope_kind
692 innermost_scope_kind (void)
694 return current_binding_level->kind;
697 /* Returns nonzero if this scope was created to store template
698 parameters. */
701 template_parm_scope_p (void)
703 return innermost_scope_kind () == sk_template_parms;
706 /* Returns the kind of template specialization we are currently
707 processing, given that it's declaration contained N_CLASS_SCOPES
708 explicit scope qualifications. */
710 tmpl_spec_kind
711 current_tmpl_spec_kind (int n_class_scopes)
713 int n_template_parm_scopes = 0;
714 int seen_specialization_p = 0;
715 int innermost_specialization_p = 0;
716 struct cp_binding_level *b;
718 /* Scan through the template parameter scopes. */
719 for (b = current_binding_level;
720 b->kind == sk_template_parms;
721 b = b->level_chain)
723 /* If we see a specialization scope inside a parameter scope,
724 then something is wrong. That corresponds to a declaration
725 like:
727 template <class T> template <> ...
729 which is always invalid since [temp.expl.spec] forbids the
730 specialization of a class member template if the enclosing
731 class templates are not explicitly specialized as well. */
732 if (b->explicit_spec_p)
734 if (n_template_parm_scopes == 0)
735 innermost_specialization_p = 1;
736 else
737 seen_specialization_p = 1;
739 else if (seen_specialization_p == 1)
740 return tsk_invalid_member_spec;
742 ++n_template_parm_scopes;
745 /* Handle explicit instantiations. */
746 if (processing_explicit_instantiation)
748 if (n_template_parm_scopes != 0)
749 /* We've seen a template parameter list during an explicit
750 instantiation. For example:
752 template <class T> template void f(int);
754 This is erroneous. */
755 return tsk_invalid_expl_inst;
756 else
757 return tsk_expl_inst;
760 if (n_template_parm_scopes < n_class_scopes)
761 /* We've not seen enough template headers to match all the
762 specialized classes present. For example:
764 template <class T> void R<T>::S<T>::f(int);
766 This is invalid; there needs to be one set of template
767 parameters for each class. */
768 return tsk_insufficient_parms;
769 else if (n_template_parm_scopes == n_class_scopes)
770 /* We're processing a non-template declaration (even though it may
771 be a member of a template class.) For example:
773 template <class T> void S<T>::f(int);
775 The `class T' maches the `S<T>', leaving no template headers
776 corresponding to the `f'. */
777 return tsk_none;
778 else if (n_template_parm_scopes > n_class_scopes + 1)
779 /* We've got too many template headers. For example:
781 template <> template <class T> void f (T);
783 There need to be more enclosing classes. */
784 return tsk_excessive_parms;
785 else
786 /* This must be a template. It's of the form:
788 template <class T> template <class U> void S<T>::f(U);
790 This is a specialization if the innermost level was a
791 specialization; otherwise it's just a definition of the
792 template. */
793 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
796 void
797 set_class_shadows (tree shadows)
799 class_binding_level->class_shadowed = shadows;
802 /* Enter a new binding level.
803 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
804 not for that of tags. */
806 void
807 pushlevel (int tag_transparent)
809 push_binding_level (make_cxx_scope (tag_transparent, keep_next_level_flag));
810 keep_next_level_flag = 0;
813 /* We're defining an object of type TYPE. If it needs a cleanup, but
814 we're not allowed to add any more objects with cleanups to the current
815 scope, create a new binding level. */
817 void
818 maybe_push_cleanup_level (tree type)
820 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
821 && current_binding_level->more_cleanups_ok == 0)
823 keep_next_level (2);
824 pushlevel (1);
825 clear_last_expr ();
826 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
830 /* Enter a new scope. The KIND indicates what kind of scope is being
831 created. */
833 void
834 begin_scope (scope_kind sk)
836 pushlevel (0);
837 if (sk == sk_template_spec)
839 current_binding_level->explicit_spec_p = true;
840 sk = sk_template_parms;
842 current_binding_level->kind = sk;
845 /* Exit the current scope. */
847 void
848 finish_scope (void)
850 poplevel (0, 0, 0);
853 /* Make DECL the innermost binding for ID. The LEVEL is the binding
854 level at which this declaration is being bound. */
856 static void
857 push_binding (tree id, tree decl, cxx_scope* level)
859 cxx_binding *binding = cxx_binding_make (decl, NULL);
861 /* Now, fill in the binding information. */
862 binding->previous = IDENTIFIER_BINDING (id);
863 BINDING_SCOPE (binding) = level;
864 INHERITED_VALUE_BINDING_P (binding) = 0;
865 LOCAL_BINDING_P (binding) = (level != class_binding_level);
867 /* And put it on the front of the list of bindings for ID. */
868 IDENTIFIER_BINDING (id) = binding;
871 /* Add DECL to the list of things declared in B. */
873 static void
874 add_decl_to_level (tree decl,
875 struct cp_binding_level* b)
877 if (TREE_CODE (decl) == NAMESPACE_DECL
878 && !DECL_NAMESPACE_ALIAS (decl))
880 TREE_CHAIN (decl) = b->namespaces;
881 b->namespaces = decl;
883 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
885 TREE_CHAIN (decl) = b->vtables;
886 b->vtables = decl;
888 else
890 /* We build up the list in reverse order, and reverse it later if
891 necessary. */
892 TREE_CHAIN (decl) = b->names;
893 b->names = decl;
894 b->names_size++;
896 /* If appropriate, add decl to separate list of statics */
897 if (b->kind == sk_namespace)
898 if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
899 || (TREE_CODE (decl) == FUNCTION_DECL
900 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
901 VARRAY_PUSH_TREE (b->static_decls, decl);
905 /* Bind DECL to ID in the current_binding_level, assumed to be a local
906 binding level. If PUSH_USING is set in FLAGS, we know that DECL
907 doesn't really belong to this binding level, that it got here
908 through a using-declaration. */
910 void
911 push_local_binding (tree id, tree decl, int flags)
913 struct cp_binding_level *b;
915 /* Skip over any local classes. This makes sense if we call
916 push_local_binding with a friend decl of a local class. */
917 b = innermost_nonclass_level ();
919 if (lookup_name_current_level (id))
921 /* Supplement the existing binding. */
922 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
923 /* It didn't work. Something else must be bound at this
924 level. Do not add DECL to the list of things to pop
925 later. */
926 return;
928 else
929 /* Create a new binding. */
930 push_binding (id, decl, b);
932 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
933 /* We must put the OVERLOAD into a TREE_LIST since the
934 TREE_CHAIN of an OVERLOAD is already used. Similarly for
935 decls that got here through a using-declaration. */
936 decl = build_tree_list (NULL_TREE, decl);
938 /* And put DECL on the list of things declared by the current
939 binding level. */
940 add_decl_to_level (decl, b);
943 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
944 binding was successful. */
947 push_class_binding (tree id, tree decl)
949 int result = 1;
950 cxx_binding *binding = IDENTIFIER_BINDING (id);
951 tree context;
953 timevar_push (TV_NAME_LOOKUP);
954 /* Note that we declared this value so that we can issue an error if
955 this is an invalid redeclaration of a name already used for some
956 other purpose. */
957 note_name_declared_in_class (id, decl);
959 if (binding && BINDING_SCOPE (binding) == class_binding_level)
960 /* Supplement the existing binding. */
961 result = supplement_binding (IDENTIFIER_BINDING (id), decl);
962 else
963 /* Create a new binding. */
964 push_binding (id, decl, class_binding_level);
966 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
967 class-level declaration. Note that we do not use DECL here
968 because of the possibility of the `struct stat' hack; if DECL is
969 a class-name or enum-name we might prefer a field-name, or some
970 such. */
971 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
973 /* If this is a binding from a base class, mark it as such. */
974 binding = IDENTIFIER_BINDING (id);
975 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
977 if (TREE_CODE (decl) == OVERLOAD)
978 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
979 else
981 my_friendly_assert (DECL_P (decl), 0);
982 context = context_for_name_lookup (decl);
985 if (is_properly_derived_from (current_class_type, context))
986 INHERITED_VALUE_BINDING_P (binding) = 1;
987 else
988 INHERITED_VALUE_BINDING_P (binding) = 0;
990 else if (BINDING_VALUE (binding) == decl)
991 /* We only encounter a TREE_LIST when push_class_decls detects an
992 ambiguity. Such an ambiguity can be overridden by a definition
993 in this class. */
994 INHERITED_VALUE_BINDING_P (binding) = 1;
996 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
999 /* Remove the binding for DECL which should be the innermost binding
1000 for ID. */
1002 static void
1003 pop_binding (tree id, tree decl)
1005 cxx_binding *binding;
1007 if (id == NULL_TREE)
1008 /* It's easiest to write the loops that call this function without
1009 checking whether or not the entities involved have names. We
1010 get here for such an entity. */
1011 return;
1013 /* Get the innermost binding for ID. */
1014 binding = IDENTIFIER_BINDING (id);
1016 /* The name should be bound. */
1017 my_friendly_assert (binding != NULL, 0);
1019 /* The DECL will be either the ordinary binding or the type
1020 binding for this identifier. Remove that binding. */
1021 if (BINDING_VALUE (binding) == decl)
1022 BINDING_VALUE (binding) = NULL_TREE;
1023 else if (BINDING_TYPE (binding) == decl)
1024 BINDING_TYPE (binding) = NULL_TREE;
1025 else
1026 abort ();
1028 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1030 /* We're completely done with the innermost binding for this
1031 identifier. Unhook it from the list of bindings. */
1032 IDENTIFIER_BINDING (id) = binding->previous;
1034 /* Add it to the free list. */
1035 cxx_binding_free (binding);
1037 /* Clear the BINDING_SCOPE so the garbage collector doesn't walk
1038 it. */
1039 BINDING_SCOPE (binding) = NULL;
1043 /* When a label goes out of scope, check to see if that label was used
1044 in a valid manner, and issue any appropriate warnings or errors. */
1046 static void
1047 pop_label (tree label, tree old_value)
1049 if (!processing_template_decl)
1051 if (DECL_INITIAL (label) == NULL_TREE)
1053 location_t location;
1055 cp_error_at ("label `%D' used but not defined", label);
1056 location.file = input_filename;
1057 location.line = 0;
1058 /* Avoid crashing later. */
1059 define_label (location, DECL_NAME (label));
1061 else if (warn_unused_label && !TREE_USED (label))
1062 cp_warning_at ("label `%D' defined but not used", label);
1065 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1068 /* At the end of a function, all labels declared within the function
1069 go out of scope. BLOCK is the top-level block for the
1070 function. */
1072 static void
1073 pop_labels (tree block)
1075 struct named_label_list *link;
1077 /* Clear out the definitions of all label names, since their scopes
1078 end here. */
1079 for (link = named_labels; link; link = link->next)
1081 pop_label (link->label_decl, link->old_value);
1082 /* Put the labels into the "variables" of the top-level block,
1083 so debugger can see them. */
1084 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1085 BLOCK_VARS (block) = link->label_decl;
1088 named_labels = NULL;
1091 /* Exit a binding level.
1092 Pop the level off, and restore the state of the identifier-decl mappings
1093 that were in effect when this level was entered.
1095 If KEEP == 1, this level had explicit declarations, so
1096 and create a "block" (a BLOCK node) for the level
1097 to record its declarations and subblocks for symbol table output.
1099 If FUNCTIONBODY is nonzero, this level is the body of a function,
1100 so create a block as if KEEP were set and also clear out all
1101 label names.
1103 If REVERSE is nonzero, reverse the order of decls before putting
1104 them into the BLOCK. */
1106 tree
1107 poplevel (int keep, int reverse, int functionbody)
1109 register tree link;
1110 /* The chain of decls was accumulated in reverse order.
1111 Put it into forward order, just for cleanliness. */
1112 tree decls;
1113 int tmp = functionbody;
1114 int real_functionbody;
1115 tree subblocks;
1116 tree block = NULL_TREE;
1117 tree decl;
1118 int leaving_for_scope;
1120 timevar_push (TV_NAME_LOOKUP);
1122 my_friendly_assert (current_binding_level->kind != sk_class, 19990916);
1124 real_functionbody = (current_binding_level->keep == 2
1125 ? ((functionbody = 0), tmp) : functionbody);
1126 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1128 my_friendly_assert (!current_binding_level->class_shadowed,
1129 19990414);
1131 /* We used to use KEEP == 2 to indicate that the new block should go
1132 at the beginning of the list of blocks at this binding level,
1133 rather than the end. This hack is no longer used. */
1134 my_friendly_assert (keep == 0 || keep == 1, 0);
1136 if (current_binding_level->keep == 1)
1137 keep = 1;
1139 /* Any uses of undefined labels, and any defined labels, now operate
1140 under constraints of next binding contour. */
1141 if (cfun && !functionbody)
1143 struct cp_binding_level *level_chain;
1144 level_chain = current_binding_level->level_chain;
1145 if (level_chain)
1147 struct named_label_use_list *uses;
1148 struct named_label_list *labels;
1149 for (labels = named_labels; labels; labels = labels->next)
1150 if (labels->binding_level == current_binding_level)
1152 tree decl;
1153 if (current_binding_level->kind == sk_try)
1154 labels->in_try_scope = 1;
1155 if (current_binding_level->kind == sk_catch)
1156 labels->in_catch_scope = 1;
1157 for (decl = labels->names_in_scope; decl;
1158 decl = TREE_CHAIN (decl))
1159 if (decl_jump_unsafe (decl))
1160 labels->bad_decls = tree_cons (NULL_TREE, decl,
1161 labels->bad_decls);
1162 labels->binding_level = level_chain;
1163 labels->names_in_scope = level_chain->names;
1166 for (uses = named_label_uses; uses; uses = uses->next)
1167 if (uses->binding_level == current_binding_level)
1169 uses->binding_level = level_chain;
1170 uses->names_in_scope = level_chain->names;
1175 /* Get the decls in the order they were written.
1176 Usually current_binding_level->names is in reverse order.
1177 But parameter decls were previously put in forward order. */
1179 if (reverse)
1180 current_binding_level->names
1181 = decls = nreverse (current_binding_level->names);
1182 else
1183 decls = current_binding_level->names;
1185 /* Output any nested inline functions within this block
1186 if they weren't already output. */
1187 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1188 if (TREE_CODE (decl) == FUNCTION_DECL
1189 && ! TREE_ASM_WRITTEN (decl)
1190 && DECL_INITIAL (decl) != NULL_TREE
1191 && TREE_ADDRESSABLE (decl)
1192 && decl_function_context (decl) == current_function_decl)
1194 /* If this decl was copied from a file-scope decl
1195 on account of a block-scope extern decl,
1196 propagate TREE_ADDRESSABLE to the file-scope decl. */
1197 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1198 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1199 else
1201 push_function_context ();
1202 output_inline_function (decl);
1203 pop_function_context ();
1207 /* When not in function-at-a-time mode, expand_end_bindings will
1208 warn about unused variables. But, in function-at-a-time mode
1209 expand_end_bindings is not passed the list of variables in the
1210 current scope, and therefore no warning is emitted. So, we
1211 explicitly warn here. */
1212 if (!processing_template_decl)
1213 warn_about_unused_variables (getdecls ());
1215 /* If there were any declarations or structure tags in that level,
1216 or if this level is a function body,
1217 create a BLOCK to record them for the life of this function. */
1218 block = NULL_TREE;
1219 if (keep == 1 || functionbody)
1220 block = make_node (BLOCK);
1221 if (block != NULL_TREE)
1223 BLOCK_VARS (block) = decls;
1224 BLOCK_SUBBLOCKS (block) = subblocks;
1227 /* In each subblock, record that this is its superior. */
1228 if (keep >= 0)
1229 for (link = subblocks; link; link = TREE_CHAIN (link))
1230 BLOCK_SUPERCONTEXT (link) = block;
1232 /* We still support the old for-scope rules, whereby the variables
1233 in a for-init statement were in scope after the for-statement
1234 ended. We only use the new rules if flag_new_for_scope is
1235 nonzero. */
1236 leaving_for_scope
1237 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
1239 /* Remove declarations for all the DECLs in this level. */
1240 for (link = decls; link; link = TREE_CHAIN (link))
1242 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1243 && DECL_NAME (link))
1245 cxx_binding *outer_binding
1246 = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
1247 tree ns_binding;
1249 if (!outer_binding)
1250 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1251 else
1252 ns_binding = NULL_TREE;
1254 if (outer_binding
1255 && (BINDING_SCOPE (outer_binding)
1256 == current_binding_level->level_chain))
1257 /* We have something like:
1259 int i;
1260 for (int i; ;);
1262 and we are leaving the `for' scope. There's no reason to
1263 keep the binding of the inner `i' in this case. */
1264 pop_binding (DECL_NAME (link), link);
1265 else if ((outer_binding
1266 && (TREE_CODE (BINDING_VALUE (outer_binding))
1267 == TYPE_DECL))
1268 || (ns_binding
1269 && TREE_CODE (ns_binding) == TYPE_DECL))
1270 /* Here, we have something like:
1272 typedef int I;
1274 void f () {
1275 for (int I; ;);
1278 We must pop the for-scope binding so we know what's a
1279 type and what isn't. */
1280 pop_binding (DECL_NAME (link), link);
1281 else
1283 /* Mark this VAR_DECL as dead so that we can tell we left it
1284 there only for backward compatibility. */
1285 DECL_DEAD_FOR_LOCAL (link) = 1;
1287 /* Keep track of what should have happened when we
1288 popped the binding. */
1289 if (outer_binding && BINDING_VALUE (outer_binding))
1290 DECL_SHADOWED_FOR_VAR (link)
1291 = BINDING_VALUE (outer_binding);
1293 /* Add it to the list of dead variables in the next
1294 outermost binding to that we can remove these when we
1295 leave that binding. */
1296 current_binding_level->level_chain->dead_vars_from_for
1297 = tree_cons (NULL_TREE, link,
1298 current_binding_level->level_chain->
1299 dead_vars_from_for);
1301 /* Although we don't pop the cxx_binding, we do clear
1302 its BINDING_SCOPE since the level is going away now. */
1303 BINDING_SCOPE (IDENTIFIER_BINDING (DECL_NAME (link))) = 0;
1306 else
1308 /* Remove the binding. */
1309 decl = link;
1310 if (TREE_CODE (decl) == TREE_LIST)
1311 decl = TREE_VALUE (decl);
1312 if (DECL_P (decl))
1313 pop_binding (DECL_NAME (decl), decl);
1314 else if (TREE_CODE (decl) == OVERLOAD)
1315 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1316 else
1317 abort ();
1321 /* Remove declarations for any `for' variables from inner scopes
1322 that we kept around. */
1323 for (link = current_binding_level->dead_vars_from_for;
1324 link; link = TREE_CHAIN (link))
1325 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1327 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1328 for (link = current_binding_level->type_shadowed;
1329 link; link = TREE_CHAIN (link))
1330 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1332 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1333 for (link = current_binding_level->shadowed_labels;
1334 link;
1335 link = TREE_CHAIN (link))
1336 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1338 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1339 list if a `using' declaration put them there. The debugging
1340 back-ends won't understand OVERLOAD, so we remove them here.
1341 Because the BLOCK_VARS are (temporarily) shared with
1342 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1343 popped all the bindings. */
1344 if (block)
1346 tree* d;
1348 for (d = &BLOCK_VARS (block); *d; )
1350 if (TREE_CODE (*d) == TREE_LIST)
1351 *d = TREE_CHAIN (*d);
1352 else
1353 d = &TREE_CHAIN (*d);
1357 /* If the level being exited is the top level of a function,
1358 check over all the labels. */
1359 if (functionbody)
1361 /* Since this is the top level block of a function, the vars are
1362 the function's parameters. Don't leave them in the BLOCK
1363 because they are found in the FUNCTION_DECL instead. */
1364 BLOCK_VARS (block) = 0;
1365 pop_labels (block);
1368 tmp = current_binding_level->keep;
1370 pop_binding_level ();
1371 if (functionbody)
1372 DECL_INITIAL (current_function_decl) = block;
1373 else if (block)
1374 current_binding_level->blocks
1375 = chainon (current_binding_level->blocks, block);
1377 /* If we did not make a block for the level just exited,
1378 any blocks made for inner levels
1379 (since they cannot be recorded as subblocks in that level)
1380 must be carried forward so they will later become subblocks
1381 of something else. */
1382 else if (subblocks)
1383 current_binding_level->blocks
1384 = chainon (current_binding_level->blocks, subblocks);
1386 /* Each and every BLOCK node created here in `poplevel' is important
1387 (e.g. for proper debugging information) so if we created one
1388 earlier, mark it as "used". */
1389 if (block)
1390 TREE_USED (block) = 1;
1392 /* Take care of compiler's internal binding structures. */
1393 if (tmp == 2)
1395 tree scope_stmts;
1397 scope_stmts
1398 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1399 if (block)
1401 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1402 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1405 block = poplevel (keep, reverse, functionbody);
1408 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1411 /* Delete the node BLOCK from the current binding level.
1412 This is used for the block inside a stmt expr ({...})
1413 so that the block can be reinserted where appropriate. */
1415 void
1416 delete_block (tree block)
1418 tree t;
1419 if (current_binding_level->blocks == block)
1420 current_binding_level->blocks = TREE_CHAIN (block);
1421 for (t = current_binding_level->blocks; t;)
1423 if (TREE_CHAIN (t) == block)
1424 TREE_CHAIN (t) = TREE_CHAIN (block);
1425 else
1426 t = TREE_CHAIN (t);
1428 TREE_CHAIN (block) = NULL_TREE;
1429 /* Clear TREE_USED which is always set by poplevel.
1430 The flag is set again if insert_block is called. */
1431 TREE_USED (block) = 0;
1434 /* Insert BLOCK at the end of the list of subblocks of the
1435 current binding level. This is used when a BIND_EXPR is expanded,
1436 to handle the BLOCK node inside the BIND_EXPR. */
1438 void
1439 insert_block (tree block)
1441 TREE_USED (block) = 1;
1442 current_binding_level->blocks
1443 = chainon (current_binding_level->blocks, block);
1446 /* Set the BLOCK node for the innermost scope
1447 (the one we are currently in). */
1449 void
1450 set_block (tree block ATTRIBUTE_UNUSED )
1452 /* The RTL expansion machinery requires us to provide this callback,
1453 but it is not applicable in function-at-a-time mode. */
1456 /* Do a pushlevel for class declarations. */
1458 void
1459 pushlevel_class (void)
1461 if (ENABLE_SCOPE_CHECKING)
1462 is_class_level = 1;
1464 begin_scope (sk_class);
1465 class_binding_level = current_binding_level;
1466 class_binding_level->this_entity = current_class_type;
1469 /* ...and a poplevel for class declarations. */
1471 void
1472 poplevel_class (void)
1474 register struct cp_binding_level *level = class_binding_level;
1475 tree shadowed;
1477 timevar_push (TV_NAME_LOOKUP);
1478 my_friendly_assert (level != 0, 354);
1480 /* If we're leaving a toplevel class, don't bother to do the setting
1481 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1482 shouldn't even be used when current_class_type isn't set, and second,
1483 if we don't touch it here, we're able to use the cache effect if the
1484 next time we're entering a class scope, it is the same class. */
1485 if (current_class_depth != 1)
1487 struct cp_binding_level* b;
1489 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1490 for (shadowed = level->class_shadowed;
1491 shadowed;
1492 shadowed = TREE_CHAIN (shadowed))
1493 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1495 /* Find the next enclosing class, and recreate
1496 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1497 b = level->level_chain;
1498 while (b && b->kind != sk_class)
1499 b = b->level_chain;
1501 if (b)
1502 for (shadowed = b->class_shadowed;
1503 shadowed;
1504 shadowed = TREE_CHAIN (shadowed))
1506 cxx_binding *binding;
1508 binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1509 while (binding && BINDING_SCOPE (binding) != b)
1510 binding = binding->previous;
1512 if (binding)
1513 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1514 = BINDING_VALUE (binding);
1517 else
1518 /* Remember to save what IDENTIFIER's were bound in this scope so we
1519 can recover from cache misses. */
1521 previous_class_type = current_class_type;
1522 previous_class_values = class_binding_level->class_shadowed;
1524 for (shadowed = level->type_shadowed;
1525 shadowed;
1526 shadowed = TREE_CHAIN (shadowed))
1527 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1529 /* Remove the bindings for all of the class-level declarations. */
1530 for (shadowed = level->class_shadowed;
1531 shadowed;
1532 shadowed = TREE_CHAIN (shadowed))
1533 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1535 /* Now, pop out of the binding level which we created up in the
1536 `pushlevel_class' routine. */
1537 if (ENABLE_SCOPE_CHECKING)
1538 is_class_level = 1;
1540 pop_binding_level ();
1541 timevar_pop (TV_NAME_LOOKUP);
1544 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1545 for any names in enclosing classes. */
1547 void
1548 clear_identifier_class_values (void)
1550 tree t;
1552 if (!class_binding_level)
1553 return;
1555 for (t = class_binding_level->class_shadowed;
1557 t = TREE_CHAIN (t))
1558 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1561 /* Returns nonzero if T is a virtual function table. */
1564 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1566 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1569 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1570 functions. */
1573 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1575 return (TREE_CODE (t) == TYPE_DECL
1576 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1577 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1580 /* Return the declarations that are members of the namespace NS. */
1582 tree
1583 cp_namespace_decls (tree ns)
1585 return NAMESPACE_LEVEL (ns)->names;
1588 struct walk_globals_data {
1589 walk_globals_pred p;
1590 walk_globals_fn f;
1591 void *data;
1594 /* Walk the vtable declarations in NAMESPACE. Whenever one is found
1595 for which P returns nonzero, call F with its address. If any call
1596 to F returns a nonzero value, return a nonzero value. */
1598 static int
1599 walk_vtables_r (tree namespace, void* data)
1601 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1602 walk_globals_fn f = wgd->f;
1603 void *d = wgd->data;
1604 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1605 int result = 0;
1607 for (; decl ; decl = TREE_CHAIN (decl))
1608 result |= (*f) (&decl, d);
1610 return result;
1613 /* Walk the vtable declarations. Whenever one is found for which P
1614 returns nonzero, call F with its address. If any call to F
1615 returns a nonzero value, return a nonzero value. */
1616 bool
1617 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1619 struct walk_globals_data wgd;
1620 wgd.p = p;
1621 wgd.f = f;
1622 wgd.data = data;
1624 return walk_namespaces (walk_vtables_r, &wgd);
1627 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1628 itself, calling F for each. The DATA is passed to F as well. */
1630 static int
1631 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1633 int result = 0;
1634 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
1636 result |= (*f) (namespace, data);
1638 for (; current; current = TREE_CHAIN (current))
1639 result |= walk_namespaces_r (current, f, data);
1641 return result;
1644 /* Walk all the namespaces, calling F for each. The DATA is passed to
1645 F as well. */
1648 walk_namespaces (walk_namespaces_fn f, void* data)
1650 return walk_namespaces_r (global_namespace, f, data);
1653 /* Walk the global declarations in NAMESPACE. Whenever one is found
1654 for which P returns nonzero, call F with its address. If any call
1655 to F returns a nonzero value, return a nonzero value. */
1657 static int
1658 walk_globals_r (tree namespace, void* data)
1660 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1661 walk_globals_pred p = wgd->p;
1662 walk_globals_fn f = wgd->f;
1663 void *d = wgd->data;
1664 tree *t;
1665 int result = 0;
1667 t = &NAMESPACE_LEVEL (namespace)->names;
1669 while (*t)
1671 tree glbl = *t;
1673 if ((*p) (glbl, d))
1674 result |= (*f) (t, d);
1676 /* If F changed *T, then *T still points at the next item to
1677 examine. */
1678 if (*t == glbl)
1679 t = &TREE_CHAIN (*t);
1682 return result;
1685 /* Walk the global declarations. Whenever one is found for which P
1686 returns true, call F with its address. If any call to F
1687 returns true, return true. */
1689 bool
1690 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1692 struct walk_globals_data wgd;
1693 wgd.p = p;
1694 wgd.f = f;
1695 wgd.data = data;
1697 return walk_namespaces (walk_globals_r, &wgd);
1700 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1701 DATA is non-NULL, this is the last time we will call
1702 wrapup_global_declarations for this NAMESPACE. */
1705 wrapup_globals_for_namespace (tree namespace, void* data)
1707 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
1708 varray_type statics = level->static_decls;
1709 tree *vec = &VARRAY_TREE (statics, 0);
1710 int len = VARRAY_ACTIVE_SIZE (statics);
1711 int last_time = (data != 0);
1713 if (last_time)
1715 check_global_declarations (vec, len);
1716 return 0;
1719 /* Write out any globals that need to be output. */
1720 return wrapup_global_declarations (vec, len);
1724 /* For debugging. */
1725 static int no_print_functions = 0;
1726 static int no_print_builtins = 0;
1728 /* Called from print_binding_level through binding_table_foreach to
1729 print the content of binding ENTRY. DATA is a pointer to line offset
1730 marker. */
1731 static void
1732 bt_print_entry (binding_entry entry, void *data)
1734 int *p = (int *) data;
1735 int len;
1737 if (entry->name == NULL)
1738 len = 3;
1739 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1740 len = 2;
1741 else
1742 len = 4;
1743 len = 4;
1745 *p += len;
1747 if (*p > 5)
1749 fprintf (stderr, "\n\t");
1750 *p = len;
1752 if (entry->name == NULL)
1754 print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
1755 fprintf (stderr, ">");
1757 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1758 print_node_brief (stderr, "", entry->type, 0);
1759 else
1761 print_node_brief (stderr, "<typedef", entry->name, 0);
1762 print_node_brief (stderr, "", entry->type, 0);
1763 fprintf (stderr, ">");
1767 void
1768 print_binding_level (struct cp_binding_level* lvl)
1770 tree t;
1771 int i = 0, len;
1772 fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks);
1773 if (lvl->tag_transparent)
1774 fprintf (stderr, " tag-transparent");
1775 if (lvl->more_cleanups_ok)
1776 fprintf (stderr, " more-cleanups-ok");
1777 if (lvl->have_cleanups)
1778 fprintf (stderr, " have-cleanups");
1779 fprintf (stderr, "\n");
1780 if (lvl->names)
1782 fprintf (stderr, " names:\t");
1783 /* We can probably fit 3 names to a line? */
1784 for (t = lvl->names; t; t = TREE_CHAIN (t))
1786 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1787 continue;
1788 if (no_print_builtins
1789 && (TREE_CODE (t) == TYPE_DECL)
1790 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1791 continue;
1793 /* Function decls tend to have longer names. */
1794 if (TREE_CODE (t) == FUNCTION_DECL)
1795 len = 3;
1796 else
1797 len = 2;
1798 i += len;
1799 if (i > 6)
1801 fprintf (stderr, "\n\t");
1802 i = len;
1804 print_node_brief (stderr, "", t, 0);
1805 if (t == error_mark_node)
1806 break;
1808 if (i)
1809 fprintf (stderr, "\n");
1811 if (lvl->type_decls)
1813 fprintf (stderr, " tags:\t");
1814 i = 0;
1815 binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
1816 if (i)
1817 fprintf (stderr, "\n");
1819 if (lvl->class_shadowed)
1821 fprintf (stderr, " class-shadowed:");
1822 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1824 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1826 fprintf (stderr, "\n");
1828 if (lvl->type_shadowed)
1830 fprintf (stderr, " type-shadowed:");
1831 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1833 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1835 fprintf (stderr, "\n");
1839 void
1840 print_other_binding_stack (struct cp_binding_level *stack)
1842 struct cp_binding_level *level;
1843 for (level = stack; !global_scope_p (level); level = level->level_chain)
1845 fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level);
1846 print_binding_level (level);
1850 void
1851 print_binding_stack (void)
1853 struct cp_binding_level *b;
1854 fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF
1855 "\nclass_binding_level=" HOST_PTR_PRINTF
1856 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n",
1857 (void *) current_binding_level, (void *) class_binding_level,
1858 (void *) NAMESPACE_LEVEL (global_namespace));
1859 if (class_binding_level)
1861 for (b = class_binding_level; b; b = b->level_chain)
1862 if (b == current_binding_level)
1863 break;
1864 if (b)
1865 b = class_binding_level;
1866 else
1867 b = current_binding_level;
1869 else
1870 b = current_binding_level;
1871 print_other_binding_stack (b);
1872 fprintf (stderr, "global:\n");
1873 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1876 /* Namespace binding access routines: The namespace_bindings field of
1877 the identifier is polymorphic, with three possible values:
1878 NULL_TREE, a list of "cxx_binding"s. */
1881 /* Push the initial binding contour of NAMESPACE-scope. Any subsequent
1882 push of NS is actually a resume. */
1883 static void
1884 initial_push_namespace_scope (tree ns)
1886 tree name = DECL_NAME (ns);
1887 cxx_scope *scope;
1889 begin_scope (sk_namespace);
1890 scope = current_binding_level;
1891 scope->type_decls = binding_table_new (name == std_identifier
1892 ? NAMESPACE_STD_HT_SIZE
1893 : (name == global_scope_name
1894 ? GLOBAL_SCOPE_HT_SIZE
1895 : NAMESPACE_ORDINARY_HT_SIZE));
1896 VARRAY_TREE_INIT (scope->static_decls,
1897 name == std_identifier || name == global_scope_name
1898 ? 200 : 10,
1899 "Static declarations");
1900 scope->this_entity = ns;
1901 NAMESPACE_LEVEL (ns) = scope;
1904 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
1905 select a name that is unique to this compilation unit. */
1907 void
1908 push_namespace (tree name)
1910 tree d = NULL_TREE;
1911 int need_new = 1;
1912 int implicit_use = 0;
1914 timevar_push (TV_NAME_LOOKUP);
1916 /* We should not get here if the global_namespace is not yet constructed
1917 nor if NAME designates the global namespace: The global scope is
1918 constructed elsewhere. */
1919 my_friendly_assert (global_namespace != NULL && name != global_scope_name,
1920 20030531);
1922 if (!name)
1924 /* The name of anonymous namespace is unique for the translation
1925 unit. */
1926 if (!anonymous_namespace_name)
1927 anonymous_namespace_name = get_file_function_name ('N');
1928 name = anonymous_namespace_name;
1929 d = IDENTIFIER_NAMESPACE_VALUE (name);
1930 if (d)
1931 /* Reopening anonymous namespace. */
1932 need_new = 0;
1933 implicit_use = 1;
1935 else
1937 /* Check whether this is an extended namespace definition. */
1938 d = IDENTIFIER_NAMESPACE_VALUE (name);
1939 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
1941 need_new = 0;
1942 if (DECL_NAMESPACE_ALIAS (d))
1944 error ("namespace alias `%D' not allowed here, assuming `%D'",
1945 d, DECL_NAMESPACE_ALIAS (d));
1946 d = DECL_NAMESPACE_ALIAS (d);
1951 if (need_new)
1953 /* Make a new namespace, binding the name to it. */
1954 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
1955 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
1956 d = pushdecl (d);
1957 initial_push_namespace_scope (d);
1959 else
1960 resume_binding_level (NAMESPACE_LEVEL (d));
1962 if (implicit_use)
1963 do_using_directive (d);
1964 /* Enter the name space. */
1965 current_namespace = d;
1967 timevar_pop (TV_NAME_LOOKUP);
1970 /* Pop from the scope of the current namespace. */
1972 void
1973 pop_namespace (void)
1975 my_friendly_assert (current_namespace != global_namespace, 20010801);
1976 current_namespace = CP_DECL_CONTEXT (current_namespace);
1977 /* The binding level is not popped, as it might be re-opened later. */
1978 suspend_binding_level ();
1981 /* Push into the scope of the namespace NS, even if it is deeply
1982 nested within another namespace. */
1984 void
1985 push_nested_namespace (tree ns)
1987 if (ns == global_namespace)
1988 push_to_top_level ();
1989 else
1991 push_nested_namespace (CP_DECL_CONTEXT (ns));
1992 push_namespace (DECL_NAME (ns));
1996 /* Pop back from the scope of the namespace NS, which was previously
1997 entered with push_nested_namespace. */
1999 void
2000 pop_nested_namespace (tree ns)
2002 timevar_push (TV_NAME_LOOKUP);
2003 while (ns != global_namespace)
2005 pop_namespace ();
2006 ns = CP_DECL_CONTEXT (ns);
2009 pop_from_top_level ();
2010 timevar_pop (TV_NAME_LOOKUP);
2014 /* Allocate storage for saving a C++ binding. */
2015 #define cxx_saved_binding_make() \
2016 (ggc_alloc (sizeof (cxx_saved_binding)))
2018 struct cxx_saved_binding GTY(())
2020 /* Link that chains saved C++ bindings for a given name into a stack. */
2021 cxx_saved_binding *previous;
2022 /* The name of the current binding. */
2023 tree identifier;
2024 /* The binding we're saving. */
2025 cxx_binding *binding;
2026 tree class_value;
2027 tree real_type_value;
2030 /* Subroutines for reverting temporarily to top-level for instantiation
2031 of templates and such. We actually need to clear out the class- and
2032 local-value slots of all identifiers, so that only the global values
2033 are at all visible. Simply setting current_binding_level to the global
2034 scope isn't enough, because more binding levels may be pushed. */
2035 struct saved_scope *scope_chain;
2037 static cxx_saved_binding *
2038 store_bindings (tree names, cxx_saved_binding *old_bindings)
2040 tree t;
2041 cxx_saved_binding *search_bindings = old_bindings;
2043 timevar_push (TV_NAME_LOOKUP);
2044 for (t = names; t; t = TREE_CHAIN (t))
2046 tree id;
2047 cxx_saved_binding *saved;
2048 cxx_saved_binding *t1;
2050 if (TREE_CODE (t) == TREE_LIST)
2051 id = TREE_PURPOSE (t);
2052 else
2053 id = DECL_NAME (t);
2055 if (!id
2056 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2057 we have no IDENTIFIER_BINDING if we have left the class
2058 scope, but cached the class-level declarations. */
2059 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2060 continue;
2062 for (t1 = search_bindings; t1; t1 = t1->previous)
2063 if (t1->identifier == id)
2064 goto skip_it;
2066 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2067 saved = cxx_saved_binding_make ();
2068 saved->previous = old_bindings;
2069 saved->identifier = id;
2070 saved->binding = IDENTIFIER_BINDING (id);
2071 saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2072 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2073 IDENTIFIER_BINDING (id) = NULL;
2074 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2075 old_bindings = saved;
2076 skip_it:
2079 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2082 void
2083 maybe_push_to_top_level (int pseudo)
2085 struct saved_scope *s;
2086 struct cp_binding_level *b;
2087 cxx_saved_binding *old_bindings;
2088 int need_pop;
2090 timevar_push (TV_NAME_LOOKUP);
2091 s = ggc_alloc_cleared (sizeof (struct saved_scope));
2093 b = scope_chain ? current_binding_level : 0;
2095 /* If we're in the middle of some function, save our state. */
2096 if (cfun)
2098 need_pop = 1;
2099 push_function_context_to (NULL_TREE);
2101 else
2102 need_pop = 0;
2104 old_bindings = NULL;
2105 if (scope_chain && previous_class_type)
2106 old_bindings = store_bindings (previous_class_values, old_bindings);
2108 /* Have to include the global scope, because class-scope decls
2109 aren't listed anywhere useful. */
2110 for (; b; b = b->level_chain)
2112 tree t;
2114 /* Template IDs are inserted into the global level. If they were
2115 inserted into namespace level, finish_file wouldn't find them
2116 when doing pending instantiations. Therefore, don't stop at
2117 namespace level, but continue until :: . */
2118 if (global_scope_p (b) || (pseudo && b->kind == sk_template_parms))
2119 break;
2121 old_bindings = store_bindings (b->names, old_bindings);
2122 /* We also need to check class_shadowed to save class-level type
2123 bindings, since pushclass doesn't fill in b->names. */
2124 if (b->kind == sk_class)
2125 old_bindings = store_bindings (b->class_shadowed, old_bindings);
2127 /* Unwind type-value slots back to top level. */
2128 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2129 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2131 s->prev = scope_chain;
2132 s->old_bindings = old_bindings;
2133 s->bindings = b;
2134 s->need_pop_function_context = need_pop;
2135 s->function_decl = current_function_decl;
2136 s->last_parms = last_function_parms;
2138 scope_chain = s;
2139 current_function_decl = NULL_TREE;
2140 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2141 current_lang_name = lang_name_cplusplus;
2142 current_namespace = global_namespace;
2143 timevar_pop (TV_NAME_LOOKUP);
2146 void
2147 push_to_top_level (void)
2149 maybe_push_to_top_level (0);
2152 void
2153 pop_from_top_level (void)
2155 struct saved_scope *s = scope_chain;
2156 cxx_saved_binding *saved;
2158 timevar_push (TV_NAME_LOOKUP);
2159 /* Clear out class-level bindings cache. */
2160 if (previous_class_type)
2161 invalidate_class_lookup_cache ();
2163 current_lang_base = 0;
2165 scope_chain = s->prev;
2166 for (saved = s->old_bindings; saved; saved = saved->previous)
2168 tree id = saved->identifier;
2170 IDENTIFIER_BINDING (id) = saved->binding;
2171 IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2172 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2175 /* If we were in the middle of compiling a function, restore our
2176 state. */
2177 if (s->need_pop_function_context)
2178 pop_function_context_from (NULL_TREE);
2179 current_function_decl = s->function_decl;
2180 last_function_parms = s->last_parms;
2181 timevar_pop (TV_NAME_LOOKUP);
2184 /* Push a definition of struct, union or enum tag named ID. into
2185 binding_level B. DECL is a TYPE_DECL for the type. We assume that
2186 the tag ID is not already defined. */
2188 static void
2189 set_identifier_type_value_with_scope (tree id,
2190 tree decl,
2191 struct cp_binding_level* b)
2193 tree type;
2195 if (b->kind != sk_namespace)
2197 /* Shadow the marker, not the real thing, so that the marker
2198 gets restored later. */
2199 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2200 b->type_shadowed
2201 = tree_cons (id, old_type_value, b->type_shadowed);
2202 type = decl ? TREE_TYPE (decl) : NULL_TREE;
2204 else
2206 cxx_binding *binding =
2207 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2208 if (decl)
2210 if (BINDING_VALUE (binding))
2211 supplement_binding (binding, decl);
2212 else
2213 BINDING_VALUE (binding) = decl;
2215 else
2216 abort ();
2217 /* Store marker instead of real type. */
2218 type = global_type_node;
2220 SET_IDENTIFIER_TYPE_VALUE (id, type);
2223 /* As set_identifier_type_value_with_scope, but using current_binding_level. */
2225 void
2226 set_identifier_type_value (tree id, tree decl)
2228 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2231 /* Return the type associated with id. */
2233 tree
2234 identifier_type_value (tree id)
2236 timevar_push (TV_NAME_LOOKUP);
2237 /* There is no type with that name, anywhere. */
2238 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2239 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2240 /* This is not the type marker, but the real thing. */
2241 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2242 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2243 /* Have to search for it. It must be on the global level, now.
2244 Ask lookup_name not to return non-types. */
2245 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2246 if (id)
2247 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2248 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2251 /* Pop off extraneous binding levels left over due to syntax errors.
2253 We don't pop past namespaces, as they might be valid. */
2255 void
2256 pop_everything (void)
2258 if (ENABLE_SCOPE_CHECKING)
2259 verbatim ("XXX entering pop_everything ()\n");
2260 while (!toplevel_bindings_p ())
2262 if (current_binding_level->kind == sk_class)
2263 pop_nested_class ();
2264 else
2265 poplevel (0, 0, 0);
2267 if (ENABLE_SCOPE_CHECKING)
2268 verbatim ("XXX leaving pop_everything ()\n");
2271 /* The type TYPE is being declared. If it is a class template, or a
2272 specialization of a class template, do any processing required and
2273 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
2274 being declared a friend. B is the binding level at which this TYPE
2275 should be bound.
2277 Returns the TYPE_DECL for TYPE, which may have been altered by this
2278 processing. */
2280 static tree
2281 maybe_process_template_type_declaration (tree type,
2282 int globalize,
2283 struct cp_binding_level* b)
2285 tree decl = TYPE_NAME (type);
2287 if (processing_template_parmlist)
2288 /* You can't declare a new template type in a template parameter
2289 list. But, you can declare a non-template type:
2291 template <class A*> struct S;
2293 is a forward-declaration of `A'. */
2295 else
2297 maybe_check_template_type (type);
2299 my_friendly_assert (IS_AGGR_TYPE (type)
2300 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2303 if (processing_template_decl)
2305 /* This may change after the call to
2306 push_template_decl_real, but we want the original value. */
2307 tree name = DECL_NAME (decl);
2309 decl = push_template_decl_real (decl, globalize);
2310 /* If the current binding level is the binding level for the
2311 template parameters (see the comment in
2312 begin_template_parm_list) and the enclosing level is a class
2313 scope, and we're not looking at a friend, push the
2314 declaration of the member class into the class scope. In the
2315 friend case, push_template_decl will already have put the
2316 friend into global scope, if appropriate. */
2317 if (TREE_CODE (type) != ENUMERAL_TYPE
2318 && !globalize && b->kind == sk_template_parms
2319 && b->level_chain->kind == sk_class)
2321 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2322 /* Put this UDT in the table of UDTs for the class, since
2323 that won't happen below because B is not the class
2324 binding level, but is instead the pseudo-global level. */
2325 if (b->level_chain->type_decls == NULL)
2326 b->level_chain->type_decls =
2327 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2328 binding_table_insert (b->level_chain->type_decls, name, type);
2329 if (!COMPLETE_TYPE_P (current_class_type))
2331 maybe_add_class_template_decl_list (current_class_type,
2332 type, /*friend_p=*/0);
2333 CLASSTYPE_NESTED_UTDS (current_class_type) =
2334 b->level_chain->type_decls;
2340 return decl;
2343 /* In C++, you don't have to write `struct S' to refer to `S'; you
2344 can just use `S'. We accomplish this by creating a TYPE_DECL as
2345 if the user had written `typedef struct S S'. Create and return
2346 the TYPE_DECL for TYPE. */
2348 tree
2349 create_implicit_typedef (tree name, tree type)
2351 tree decl;
2353 decl = build_decl (TYPE_DECL, name, type);
2354 DECL_ARTIFICIAL (decl) = 1;
2355 /* There are other implicit type declarations, like the one *within*
2356 a class that allows you to write `S::S'. We must distinguish
2357 amongst these. */
2358 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2359 TYPE_NAME (type) = decl;
2361 return decl;
2364 /* Remember a local name for name-mangling purposes. */
2366 static void
2367 push_local_name (tree decl)
2369 size_t i, nelts;
2370 tree t, name;
2372 timevar_push (TV_NAME_LOOKUP);
2373 if (!local_names)
2374 VARRAY_TREE_INIT (local_names, 8, "local_names");
2376 name = DECL_NAME (decl);
2378 nelts = VARRAY_ACTIVE_SIZE (local_names);
2379 for (i = 0; i < nelts; i++)
2381 t = VARRAY_TREE (local_names, i);
2382 if (DECL_NAME (t) == name)
2384 if (!DECL_LANG_SPECIFIC (decl))
2385 retrofit_lang_decl (decl);
2386 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2387 if (DECL_LANG_SPECIFIC (t))
2388 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2389 else
2390 DECL_DISCRIMINATOR (decl) = 1;
2392 VARRAY_TREE (local_names, i) = decl;
2393 timevar_pop (TV_NAME_LOOKUP);
2394 return;
2398 VARRAY_PUSH_TREE (local_names, decl);
2399 timevar_pop (TV_NAME_LOOKUP);
2402 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2403 Normally put it into the inner-most non-tag-transparent scope,
2404 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2405 The latter is needed for implicit declarations. */
2407 void
2408 pushtag (tree name, tree type, int globalize)
2410 register struct cp_binding_level *b;
2412 timevar_push (TV_NAME_LOOKUP);
2413 b = current_binding_level;
2414 while (b->tag_transparent
2415 || (b->kind == sk_class
2416 && (globalize
2417 /* We may be defining a new type in the initializer
2418 of a static member variable. We allow this when
2419 not pedantic, and it is particularly useful for
2420 type punning via an anonymous union. */
2421 || COMPLETE_TYPE_P (b->this_entity))))
2422 b = b->level_chain;
2424 if (b->type_decls == NULL)
2425 b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2426 binding_table_insert (b->type_decls, name, type);
2428 if (name)
2430 /* Do C++ gratuitous typedefing. */
2431 if (IDENTIFIER_TYPE_VALUE (name) != type)
2433 register tree d = NULL_TREE;
2434 int in_class = 0;
2435 tree context = TYPE_CONTEXT (type);
2437 if (! context)
2439 tree cs = current_scope ();
2441 if (! globalize)
2442 context = cs;
2443 else if (cs != NULL_TREE && TYPE_P (cs))
2444 /* When declaring a friend class of a local class, we want
2445 to inject the newly named class into the scope
2446 containing the local class, not the namespace scope. */
2447 context = decl_function_context (get_type_decl (cs));
2449 if (!context)
2450 context = current_namespace;
2452 if (b->kind == sk_class
2453 || (b->kind == sk_template_parms
2454 && b->level_chain->kind == sk_class))
2455 in_class = 1;
2457 if (current_lang_name == lang_name_java)
2458 TYPE_FOR_JAVA (type) = 1;
2460 d = create_implicit_typedef (name, type);
2461 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2462 if (! in_class)
2463 set_identifier_type_value_with_scope (name, d, b);
2465 d = maybe_process_template_type_declaration (type,
2466 globalize, b);
2468 if (b->kind == sk_class)
2470 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2471 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2472 class. But if it's a member template class, we
2473 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2474 is done later. */
2475 finish_member_declaration (d);
2476 else
2477 pushdecl_class_level (d);
2479 else
2480 d = pushdecl_with_scope (d, b);
2482 /* FIXME what if it gets a name from typedef? */
2483 if (ANON_AGGRNAME_P (name))
2484 DECL_IGNORED_P (d) = 1;
2486 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2488 /* If this is a local class, keep track of it. We need this
2489 information for name-mangling, and so that it is possible to find
2490 all function definitions in a translation unit in a convenient
2491 way. (It's otherwise tricky to find a member function definition
2492 it's only pointed to from within a local class.) */
2493 if (TYPE_CONTEXT (type)
2494 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2495 && !processing_template_decl)
2496 VARRAY_PUSH_TREE (local_classes, type);
2498 if (b->kind == sk_class
2499 && !COMPLETE_TYPE_P (current_class_type))
2501 maybe_add_class_template_decl_list (current_class_type,
2502 type, /*friend_p=*/0);
2503 CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
2507 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2508 /* Use the canonical TYPE_DECL for this node. */
2509 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2510 else
2512 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2513 will be the tagged type we just added to the current
2514 binding level. This fake NULL-named TYPE_DECL node helps
2515 dwarfout.c to know when it needs to output a
2516 representation of a tagged type, and it also gives us a
2517 convenient place to record the "scope start" address for
2518 the tagged type. */
2520 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2521 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2523 timevar_pop (TV_NAME_LOOKUP);
2526 /* Counter used to create anonymous type names. */
2528 static GTY(()) int anon_cnt;
2530 /* Return an IDENTIFIER which can be used as a name for
2531 anonymous structs and unions. */
2533 tree
2534 make_anon_name (void)
2536 char buf[32];
2538 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2539 return get_identifier (buf);
2542 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
2543 This keeps dbxout from getting confused. */
2545 void
2546 clear_anon_tags (void)
2548 register struct cp_binding_level *b;
2549 static int last_cnt = 0;
2551 /* Fast out if no new anon names were declared. */
2552 if (last_cnt == anon_cnt)
2553 return;
2555 b = current_binding_level;
2556 while (b->tag_transparent)
2557 b = b->level_chain;
2558 if (b->type_decls != NULL)
2559 binding_table_remove_anonymous_types (b->type_decls);
2560 last_cnt = anon_cnt;
2563 /* Subroutine of duplicate_decls: return truthvalue of whether
2564 or not types of these decls match.
2566 For C++, we must compare the parameter list so that `int' can match
2567 `int&' in a parameter position, but `int&' is not confused with
2568 `const int&'. */
2571 decls_match (tree newdecl, tree olddecl)
2573 int types_match;
2575 if (newdecl == olddecl)
2576 return 1;
2578 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2579 /* If the two DECLs are not even the same kind of thing, we're not
2580 interested in their types. */
2581 return 0;
2583 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2585 tree f1 = TREE_TYPE (newdecl);
2586 tree f2 = TREE_TYPE (olddecl);
2587 tree p1 = TYPE_ARG_TYPES (f1);
2588 tree p2 = TYPE_ARG_TYPES (f2);
2590 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2591 && ! (DECL_EXTERN_C_P (newdecl)
2592 && DECL_EXTERN_C_P (olddecl)))
2593 return 0;
2595 if (TREE_CODE (f1) != TREE_CODE (f2))
2596 return 0;
2598 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2600 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2601 && (DECL_BUILT_IN (olddecl)
2602 #ifndef NO_IMPLICIT_EXTERN_C
2603 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2604 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2605 #endif
2608 types_match = self_promoting_args_p (p1);
2609 if (p1 == void_list_node)
2610 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2612 #ifndef NO_IMPLICIT_EXTERN_C
2613 else if (p1 == NULL_TREE
2614 && (DECL_EXTERN_C_P (olddecl)
2615 && DECL_IN_SYSTEM_HEADER (olddecl)
2616 && !DECL_CLASS_SCOPE_P (olddecl))
2617 && (DECL_EXTERN_C_P (newdecl)
2618 && DECL_IN_SYSTEM_HEADER (newdecl)
2619 && !DECL_CLASS_SCOPE_P (newdecl)))
2621 types_match = self_promoting_args_p (p2);
2622 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2624 #endif
2625 else
2626 types_match = compparms (p1, p2);
2628 else
2629 types_match = 0;
2631 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2633 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2634 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2635 return 0;
2637 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2638 DECL_TEMPLATE_PARMS (olddecl)))
2639 return 0;
2641 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2642 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
2643 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
2644 else
2645 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2646 DECL_TEMPLATE_RESULT (newdecl));
2648 else
2650 if (TREE_TYPE (newdecl) == error_mark_node)
2651 types_match = TREE_TYPE (olddecl) == error_mark_node;
2652 else if (TREE_TYPE (olddecl) == NULL_TREE)
2653 types_match = TREE_TYPE (newdecl) == NULL_TREE;
2654 else if (TREE_TYPE (newdecl) == NULL_TREE)
2655 types_match = 0;
2656 else
2657 types_match = comptypes (TREE_TYPE (newdecl),
2658 TREE_TYPE (olddecl),
2659 COMPARE_REDECLARATION);
2662 return types_match;
2665 /* If NEWDECL is `static' and an `extern' was seen previously,
2666 warn about it. OLDDECL is the previous declaration.
2668 Note that this does not apply to the C++ case of declaring
2669 a variable `extern const' and then later `const'.
2671 Don't complain about built-in functions, since they are beyond
2672 the user's control. */
2674 static void
2675 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2677 static const char *const explicit_extern_static_warning
2678 = "`%D' was declared `extern' and later `static'";
2679 static const char *const implicit_extern_static_warning
2680 = "`%D' was declared implicitly `extern' and later `static'";
2682 tree name;
2684 if (TREE_CODE (newdecl) == TYPE_DECL
2685 || TREE_CODE (newdecl) == TEMPLATE_DECL
2686 || TREE_CODE (newdecl) == CONST_DECL
2687 || TREE_CODE (newdecl) == NAMESPACE_DECL)
2688 return;
2690 /* Don't get confused by static member functions; that's a different
2691 use of `static'. */
2692 if (TREE_CODE (newdecl) == FUNCTION_DECL
2693 && DECL_STATIC_FUNCTION_P (newdecl))
2694 return;
2696 /* If the old declaration was `static', or the new one isn't, then
2697 then everything is OK. */
2698 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2699 return;
2701 /* It's OK to declare a builtin function as `static'. */
2702 if (TREE_CODE (olddecl) == FUNCTION_DECL
2703 && DECL_ARTIFICIAL (olddecl))
2704 return;
2706 name = DECL_ASSEMBLER_NAME (newdecl);
2707 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2708 ? implicit_extern_static_warning
2709 : explicit_extern_static_warning, newdecl);
2710 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2713 /* Handle when a new declaration NEWDECL has the same name as an old
2714 one OLDDECL in the same binding contour. Prints an error message
2715 if appropriate.
2717 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2718 Otherwise, return 0. */
2721 duplicate_decls (tree newdecl, tree olddecl)
2723 unsigned olddecl_uid = DECL_UID (olddecl);
2724 int olddecl_friend = 0, types_match = 0;
2725 int new_defines_function = 0;
2727 if (newdecl == olddecl)
2728 return 1;
2730 types_match = decls_match (newdecl, olddecl);
2732 /* If either the type of the new decl or the type of the old decl is an
2733 error_mark_node, then that implies that we have already issued an
2734 error (earlier) for some bogus type specification, and in that case,
2735 it is rather pointless to harass the user with yet more error message
2736 about the same declaration, so just pretend the types match here. */
2737 if (TREE_TYPE (newdecl) == error_mark_node
2738 || TREE_TYPE (olddecl) == error_mark_node)
2739 types_match = 1;
2741 if (DECL_P (olddecl)
2742 && TREE_CODE (newdecl) == FUNCTION_DECL
2743 && TREE_CODE (olddecl) == FUNCTION_DECL
2744 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2746 if (DECL_DECLARED_INLINE_P (newdecl)
2747 && DECL_UNINLINABLE (newdecl)
2748 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2749 /* Already warned elsewhere. */;
2750 else if (DECL_DECLARED_INLINE_P (olddecl)
2751 && DECL_UNINLINABLE (olddecl)
2752 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2753 /* Already warned. */;
2754 else if (DECL_DECLARED_INLINE_P (newdecl)
2755 && DECL_UNINLINABLE (olddecl)
2756 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2758 warning ("%Hfunction '%D' redeclared as inline",
2759 &DECL_SOURCE_LOCATION (newdecl), newdecl);
2760 warning ("%Hprevious declaration of '%D' with attribute noinline",
2761 &DECL_SOURCE_LOCATION (olddecl), olddecl);
2763 else if (DECL_DECLARED_INLINE_P (olddecl)
2764 && DECL_UNINLINABLE (newdecl)
2765 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2767 warning ("%Hfunction '%D' redeclared with attribute noinline",
2768 &DECL_SOURCE_LOCATION (newdecl), newdecl);
2769 warning ("%Hprevious declaration of '%D' was inline",
2770 &DECL_SOURCE_LOCATION (olddecl), olddecl);
2774 /* Check for redeclaration and other discrepancies. */
2775 if (TREE_CODE (olddecl) == FUNCTION_DECL
2776 && DECL_ARTIFICIAL (olddecl))
2778 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2780 /* Avoid warnings redeclaring anticipated built-ins. */
2781 if (DECL_ANTICIPATED (olddecl))
2782 return 0;
2784 /* If you declare a built-in or predefined function name as static,
2785 the old definition is overridden, but optionally warn this was a
2786 bad choice of name. */
2787 if (! TREE_PUBLIC (newdecl))
2789 if (warn_shadow)
2790 warning ("shadowing %s function `%#D'",
2791 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2792 olddecl);
2793 /* Discard the old built-in function. */
2794 return 0;
2796 /* If the built-in is not ansi, then programs can override
2797 it even globally without an error. */
2798 else if (! DECL_BUILT_IN (olddecl))
2799 warning ("library function `%#D' redeclared as non-function `%#D'",
2800 olddecl, newdecl);
2801 else
2803 error ("declaration of `%#D'", newdecl);
2804 error ("conflicts with built-in declaration `%#D'",
2805 olddecl);
2807 return 0;
2809 else if (!types_match)
2811 /* Avoid warnings redeclaring anticipated built-ins. */
2812 if (DECL_ANTICIPATED (olddecl))
2813 ; /* Do nothing yet. */
2814 else if ((DECL_EXTERN_C_P (newdecl)
2815 && DECL_EXTERN_C_P (olddecl))
2816 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2817 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2819 /* A near match; override the builtin. */
2821 if (TREE_PUBLIC (newdecl))
2823 warning ("new declaration `%#D'", newdecl);
2824 warning ("ambiguates built-in declaration `%#D'",
2825 olddecl);
2827 else if (warn_shadow)
2828 warning ("shadowing %s function `%#D'",
2829 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2830 olddecl);
2832 else
2833 /* Discard the old built-in function. */
2834 return 0;
2836 /* Replace the old RTL to avoid problems with inlining. */
2837 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2839 /* Even if the types match, prefer the new declarations type
2840 for anticipated built-ins, for exception lists, etc... */
2841 else if (DECL_ANTICIPATED (olddecl))
2842 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2844 /* Whether or not the builtin can throw exceptions has no
2845 bearing on this declarator. */
2846 TREE_NOTHROW (olddecl) = 0;
2848 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2850 /* If a builtin function is redeclared as `static', merge
2851 the declarations, but make the original one static. */
2852 DECL_THIS_STATIC (olddecl) = 1;
2853 TREE_PUBLIC (olddecl) = 0;
2855 /* Make the old declaration consistent with the new one so
2856 that all remnants of the builtin-ness of this function
2857 will be banished. */
2858 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2859 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2862 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2864 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
2865 && TREE_CODE (newdecl) != TYPE_DECL
2866 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
2867 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
2868 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
2869 && TREE_CODE (olddecl) != TYPE_DECL
2870 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
2871 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2872 == TYPE_DECL))))
2874 /* We do nothing special here, because C++ does such nasty
2875 things with TYPE_DECLs. Instead, just let the TYPE_DECL
2876 get shadowed, and know that if we need to find a TYPE_DECL
2877 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2878 slot of the identifier. */
2879 return 0;
2882 if ((TREE_CODE (newdecl) == FUNCTION_DECL
2883 && DECL_FUNCTION_TEMPLATE_P (olddecl))
2884 || (TREE_CODE (olddecl) == FUNCTION_DECL
2885 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2886 return 0;
2888 error ("`%#D' redeclared as different kind of symbol", newdecl);
2889 if (TREE_CODE (olddecl) == TREE_LIST)
2890 olddecl = TREE_VALUE (olddecl);
2891 cp_error_at ("previous declaration of `%#D'", olddecl);
2893 /* New decl is completely inconsistent with the old one =>
2894 tell caller to replace the old one. */
2896 return 0;
2898 else if (!types_match)
2900 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
2901 /* These are certainly not duplicate declarations; they're
2902 from different scopes. */
2903 return 0;
2905 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2907 /* The name of a class template may not be declared to refer to
2908 any other template, class, function, object, namespace, value,
2909 or type in the same scope. */
2910 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
2911 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2913 error ("declaration of template `%#D'", newdecl);
2914 cp_error_at ("conflicts with previous declaration `%#D'",
2915 olddecl);
2917 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
2918 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
2919 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
2920 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
2921 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2922 DECL_TEMPLATE_PARMS (olddecl))
2923 /* Template functions can be disambiguated by
2924 return type. */
2925 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
2926 TREE_TYPE (TREE_TYPE (olddecl))))
2928 error ("new declaration `%#D'", newdecl);
2929 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
2931 return 0;
2933 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2935 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
2937 error ("declaration of C function `%#D' conflicts with",
2938 newdecl);
2939 cp_error_at ("previous declaration `%#D' here", olddecl);
2941 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2942 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2944 error ("new declaration `%#D'", newdecl);
2945 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
2947 else
2948 return 0;
2951 /* Already complained about this, so don't do so again. */
2952 else if (current_class_type == NULL_TREE
2953 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
2955 error ("conflicting declaration '%#D'", newdecl);
2956 cp_error_at ("'%D' has a previous declaration as `%#D'",
2957 olddecl, olddecl);
2958 return false;
2961 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2962 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2963 && (!DECL_TEMPLATE_INFO (newdecl)
2964 || (DECL_TI_TEMPLATE (newdecl)
2965 != DECL_TI_TEMPLATE (olddecl))))
2966 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2967 && (!DECL_TEMPLATE_INFO (olddecl)
2968 || (DECL_TI_TEMPLATE (olddecl)
2969 != DECL_TI_TEMPLATE (newdecl))))))
2970 /* It's OK to have a template specialization and a non-template
2971 with the same type, or to have specializations of two
2972 different templates with the same type. Note that if one is a
2973 specialization, and the other is an instantiation of the same
2974 template, that we do not exit at this point. That situation
2975 can occur if we instantiate a template class, and then
2976 specialize one of its methods. This situation is valid, but
2977 the declarations must be merged in the usual way. */
2978 return 0;
2979 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2980 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2981 && !DECL_USE_TEMPLATE (newdecl))
2982 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2983 && !DECL_USE_TEMPLATE (olddecl))))
2984 /* One of the declarations is a template instantiation, and the
2985 other is not a template at all. That's OK. */
2986 return 0;
2987 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
2988 && DECL_NAMESPACE_ALIAS (newdecl)
2989 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
2990 /* Redeclaration of namespace alias, ignore it. */
2991 return 1;
2992 else
2994 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2995 if (errmsg)
2997 error (errmsg, newdecl);
2998 if (DECL_NAME (olddecl) != NULL_TREE)
2999 cp_error_at ((DECL_INITIAL (olddecl)
3000 && namespace_bindings_p ())
3001 ? "`%#D' previously defined here"
3002 : "`%#D' previously declared here", olddecl);
3003 return 0;
3005 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3006 && DECL_INITIAL (olddecl) != NULL_TREE
3007 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3008 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3010 /* Prototype decl follows defn w/o prototype. */
3011 cp_warning_at ("prototype for `%#D'", newdecl);
3012 warning ("%Hfollows non-prototype definition here",
3013 &DECL_SOURCE_LOCATION (olddecl));
3015 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3016 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3018 /* extern "C" int foo ();
3019 int foo () { bar (); }
3020 is OK. */
3021 if (current_lang_depth () == 0)
3022 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3023 else
3025 cp_error_at ("previous declaration of `%#D' with %L linkage",
3026 olddecl, DECL_LANGUAGE (olddecl));
3027 error ("conflicts with new declaration with %L linkage",
3028 DECL_LANGUAGE (newdecl));
3032 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3034 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3036 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3037 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3038 int i = 1;
3040 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3041 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3043 for (; t1 && t1 != void_list_node;
3044 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3045 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3047 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3048 TREE_PURPOSE (t2)))
3050 pedwarn ("default argument given for parameter %d of `%#D'",
3051 i, newdecl);
3052 cp_pedwarn_at ("after previous specification in `%#D'",
3053 olddecl);
3055 else
3057 error ("default argument given for parameter %d of `%#D'",
3058 i, newdecl);
3059 cp_error_at ("after previous specification in `%#D'",
3060 olddecl);
3064 if (DECL_DECLARED_INLINE_P (newdecl)
3065 && ! DECL_DECLARED_INLINE_P (olddecl)
3066 && TREE_ADDRESSABLE (olddecl) && warn_inline)
3068 warning ("`%#D' was used before it was declared inline",
3069 newdecl);
3070 warning ("%Hprevious non-inline declaration here",
3071 &DECL_SOURCE_LOCATION (olddecl));
3076 /* Do not merge an implicit typedef with an explicit one. In:
3078 class A;
3080 typedef class A A __attribute__ ((foo));
3082 the attribute should apply only to the typedef. */
3083 if (TREE_CODE (olddecl) == TYPE_DECL
3084 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3085 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3086 return 0;
3088 /* If new decl is `static' and an `extern' was seen previously,
3089 warn about it. */
3090 warn_extern_redeclared_static (newdecl, olddecl);
3092 /* We have committed to returning 1 at this point. */
3093 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3095 /* Now that functions must hold information normally held
3096 by field decls, there is extra work to do so that
3097 declaration information does not get destroyed during
3098 definition. */
3099 if (DECL_VINDEX (olddecl))
3100 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3101 if (DECL_CONTEXT (olddecl))
3102 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3103 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3104 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3105 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3106 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3107 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3108 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3109 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3110 SET_OVERLOADED_OPERATOR_CODE
3111 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3112 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3114 /* Optionally warn about more than one declaration for the same
3115 name, but don't warn about a function declaration followed by a
3116 definition. */
3117 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3118 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3119 /* Don't warn about extern decl followed by definition. */
3120 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3121 /* Don't warn about friends, let add_friend take care of it. */
3122 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3124 warning ("redundant redeclaration of `%D' in same scope", newdecl);
3125 cp_warning_at ("previous declaration of `%D'", olddecl);
3129 /* Deal with C++: must preserve virtual function table size. */
3130 if (TREE_CODE (olddecl) == TYPE_DECL)
3132 register tree newtype = TREE_TYPE (newdecl);
3133 register tree oldtype = TREE_TYPE (olddecl);
3135 if (newtype != error_mark_node && oldtype != error_mark_node
3136 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3137 CLASSTYPE_FRIEND_CLASSES (newtype)
3138 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3140 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3143 /* Copy all the DECL_... slots specified in the new decl
3144 except for any that we copy here from the old type. */
3145 DECL_ATTRIBUTES (newdecl)
3146 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3148 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3150 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3151 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3152 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3153 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3155 /* If the new declaration is a definition, update the file and
3156 line information on the declaration. */
3157 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3158 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3160 DECL_SOURCE_LOCATION (olddecl)
3161 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3162 = DECL_SOURCE_LOCATION (newdecl);
3165 return 1;
3168 if (types_match)
3170 /* Automatically handles default parameters. */
3171 tree oldtype = TREE_TYPE (olddecl);
3172 tree newtype;
3174 /* Merge the data types specified in the two decls. */
3175 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3177 /* If merge_types produces a non-typedef type, just use the old type. */
3178 if (TREE_CODE (newdecl) == TYPE_DECL
3179 && newtype == DECL_ORIGINAL_TYPE (newdecl))
3180 newtype = oldtype;
3182 if (TREE_CODE (newdecl) == VAR_DECL)
3184 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3185 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3186 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
3187 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
3190 /* Do this after calling `merge_types' so that default
3191 parameters don't confuse us. */
3192 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3193 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3194 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3196 TREE_TYPE (newdecl) = build_exception_variant (newtype,
3197 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3198 TREE_TYPE (olddecl) = build_exception_variant (newtype,
3199 TYPE_RAISES_EXCEPTIONS (oldtype));
3201 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3202 && DECL_SOURCE_LINE (olddecl) != 0
3203 && flag_exceptions
3204 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3205 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3207 error ("declaration of `%F' throws different exceptions",
3208 newdecl);
3209 cp_error_at ("than previous declaration `%F'", olddecl);
3212 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3214 /* Lay the type out, unless already done. */
3215 if (! same_type_p (newtype, oldtype)
3216 && TREE_TYPE (newdecl) != error_mark_node
3217 && !(processing_template_decl && uses_template_parms (newdecl)))
3218 layout_type (TREE_TYPE (newdecl));
3220 if ((TREE_CODE (newdecl) == VAR_DECL
3221 || TREE_CODE (newdecl) == PARM_DECL
3222 || TREE_CODE (newdecl) == RESULT_DECL
3223 || TREE_CODE (newdecl) == FIELD_DECL
3224 || TREE_CODE (newdecl) == TYPE_DECL)
3225 && !(processing_template_decl && uses_template_parms (newdecl)))
3226 layout_decl (newdecl, 0);
3228 /* Merge the type qualifiers. */
3229 if (TREE_READONLY (newdecl))
3230 TREE_READONLY (olddecl) = 1;
3231 if (TREE_THIS_VOLATILE (newdecl))
3232 TREE_THIS_VOLATILE (olddecl) = 1;
3234 /* Merge the initialization information. */
3235 if (DECL_INITIAL (newdecl) == NULL_TREE
3236 && DECL_INITIAL (olddecl) != NULL_TREE)
3238 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3239 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3240 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3241 && DECL_LANG_SPECIFIC (newdecl)
3242 && DECL_LANG_SPECIFIC (olddecl))
3243 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3246 /* Merge the section attribute.
3247 We want to issue an error if the sections conflict but that must be
3248 done later in decl_attributes since we are called before attributes
3249 are assigned. */
3250 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3251 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3253 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3255 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3256 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3257 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
3258 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
3259 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
3260 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
3261 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
3262 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
3263 /* Keep the old RTL. */
3264 COPY_DECL_RTL (olddecl, newdecl);
3266 else if (TREE_CODE (newdecl) == VAR_DECL
3267 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3269 /* Keep the old RTL. We cannot keep the old RTL if the old
3270 declaration was for an incomplete object and the new
3271 declaration is not since many attributes of the RTL will
3272 change. */
3273 COPY_DECL_RTL (olddecl, newdecl);
3276 /* If cannot merge, then use the new type and qualifiers,
3277 and don't preserve the old rtl. */
3278 else
3280 /* Clean out any memory we had of the old declaration. */
3281 tree oldstatic = value_member (olddecl, static_aggregates);
3282 if (oldstatic)
3283 TREE_VALUE (oldstatic) = error_mark_node;
3285 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3286 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3287 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3288 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3291 /* Merge the storage class information. */
3292 merge_weak (newdecl, olddecl);
3294 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3295 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3296 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3297 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3298 if (! DECL_EXTERNAL (olddecl))
3299 DECL_EXTERNAL (newdecl) = 0;
3301 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3303 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3304 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3305 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3306 DECL_TEMPLATE_INSTANTIATED (newdecl)
3307 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3308 /* Don't really know how much of the language-specific
3309 values we should copy from old to new. */
3310 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3311 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
3312 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3313 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3314 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3315 DECL_INITIALIZED_IN_CLASS_P (newdecl)
3316 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3317 olddecl_friend = DECL_FRIEND_P (olddecl);
3319 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3320 if (TREE_CODE (newdecl) == FUNCTION_DECL
3321 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3323 DECL_BEFRIENDING_CLASSES (newdecl)
3324 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3325 DECL_BEFRIENDING_CLASSES (olddecl));
3326 /* DECL_THUNKS is only valid for virtual functions,
3327 otherwise it is a DECL_FRIEND_CONTEXT. */
3328 if (DECL_VIRTUAL_P (newdecl))
3329 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3333 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3335 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3336 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3338 /* If newdecl is not a specialization, then it is not a
3339 template-related function at all. And that means that we
3340 should have exited above, returning 0. */
3341 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3344 if (TREE_USED (olddecl))
3345 /* From [temp.expl.spec]:
3347 If a template, a member template or the member of a class
3348 template is explicitly specialized then that
3349 specialization shall be declared before the first use of
3350 that specialization that would cause an implicit
3351 instantiation to take place, in every translation unit in
3352 which such a use occurs. */
3353 error ("explicit specialization of %D after first use",
3354 olddecl);
3356 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3358 /* [temp.expl.spec/14] We don't inline explicit specialization
3359 just because the primary template says so. */
3361 else
3363 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3364 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3366 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3368 /* If either decl says `inline', this fn is inline, unless
3369 its definition was passed already. */
3370 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3371 DECL_INLINE (olddecl) = 1;
3372 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3374 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3375 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3378 /* Preserve abstractness on cloned [cd]tors. */
3379 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3381 if (! types_match)
3383 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3384 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3385 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3387 if (! types_match || new_defines_function)
3389 /* These need to be copied so that the names are available.
3390 Note that if the types do match, we'll preserve inline
3391 info and other bits, but if not, we won't. */
3392 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3393 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3395 if (new_defines_function)
3396 /* If defining a function declared with other language
3397 linkage, use the previously declared language linkage. */
3398 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3399 else if (types_match)
3401 /* If redeclaring a builtin function, and not a definition,
3402 it stays built in. */
3403 if (DECL_BUILT_IN (olddecl))
3405 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3406 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3407 /* If we're keeping the built-in definition, keep the rtl,
3408 regardless of declaration matches. */
3409 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3411 else
3412 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
3414 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3415 /* Don't clear out the arguments if we're redefining a function. */
3416 if (DECL_ARGUMENTS (olddecl))
3417 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3420 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3421 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3423 /* Now preserve various other info from the definition. */
3424 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3425 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3426 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3427 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3429 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3431 int function_size;
3433 function_size = sizeof (struct tree_decl);
3435 memcpy ((char *) olddecl + sizeof (struct tree_common),
3436 (char *) newdecl + sizeof (struct tree_common),
3437 function_size - sizeof (struct tree_common));
3439 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3440 /* If newdecl is a template instantiation, it is possible that
3441 the following sequence of events has occurred:
3443 o A friend function was declared in a class template. The
3444 class template was instantiated.
3446 o The instantiation of the friend declaration was
3447 recorded on the instantiation list, and is newdecl.
3449 o Later, however, instantiate_class_template called pushdecl
3450 on the newdecl to perform name injection. But, pushdecl in
3451 turn called duplicate_decls when it discovered that another
3452 declaration of a global function with the same name already
3453 existed.
3455 o Here, in duplicate_decls, we decided to clobber newdecl.
3457 If we're going to do that, we'd better make sure that
3458 olddecl, and not newdecl, is on the list of
3459 instantiations so that if we try to do the instantiation
3460 again we won't get the clobbered declaration. */
3461 reregister_specialization (newdecl,
3462 DECL_TI_TEMPLATE (newdecl),
3463 olddecl);
3465 else
3467 memcpy ((char *) olddecl + sizeof (struct tree_common),
3468 (char *) newdecl + sizeof (struct tree_common),
3469 sizeof (struct tree_decl) - sizeof (struct tree_common)
3470 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3473 DECL_UID (olddecl) = olddecl_uid;
3474 if (olddecl_friend)
3475 DECL_FRIEND_P (olddecl) = 1;
3477 /* NEWDECL contains the merged attribute lists.
3478 Update OLDDECL to be the same. */
3479 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3481 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3482 so that encode_section_info has a chance to look at the new decl
3483 flags and attributes. */
3484 if (DECL_RTL_SET_P (olddecl)
3485 && (TREE_CODE (olddecl) == FUNCTION_DECL
3486 || (TREE_CODE (olddecl) == VAR_DECL
3487 && TREE_STATIC (olddecl))))
3488 make_decl_rtl (olddecl, NULL);
3490 return 1;
3493 /* Record a decl-node X as belonging to the current lexical scope.
3494 Check for errors (such as an incompatible declaration for the same
3495 name already seen in the same scope).
3497 Returns either X or an old decl for the same name.
3498 If an old decl is returned, it may have been smashed
3499 to agree with what X says. */
3501 tree
3502 pushdecl (tree x)
3504 register tree t;
3505 register tree name;
3506 int need_new_binding;
3508 timevar_push (TV_NAME_LOOKUP);
3510 need_new_binding = 1;
3512 if (DECL_TEMPLATE_PARM_P (x))
3513 /* Template parameters have no context; they are not X::T even
3514 when declared within a class or namespace. */
3516 else
3518 if (current_function_decl && x != current_function_decl
3519 /* A local declaration for a function doesn't constitute
3520 nesting. */
3521 && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3522 /* A local declaration for an `extern' variable is in the
3523 scope of the current namespace, not the current
3524 function. */
3525 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3526 && !DECL_CONTEXT (x))
3527 DECL_CONTEXT (x) = current_function_decl;
3529 /* If this is the declaration for a namespace-scope function,
3530 but the declaration itself is in a local scope, mark the
3531 declaration. */
3532 if (TREE_CODE (x) == FUNCTION_DECL
3533 && DECL_NAMESPACE_SCOPE_P (x)
3534 && current_function_decl
3535 && x != current_function_decl)
3536 DECL_LOCAL_FUNCTION_P (x) = 1;
3539 name = DECL_NAME (x);
3540 if (name)
3542 int different_binding_level = 0;
3544 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3545 name = TREE_OPERAND (name, 0);
3547 /* In case this decl was explicitly namespace-qualified, look it
3548 up in its namespace context. */
3549 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3550 && namespace_bindings_p ())
3551 t = namespace_binding (name, DECL_CONTEXT (x));
3552 else
3553 t = lookup_name_current_level (name);
3555 /* [basic.link] If there is a visible declaration of an entity
3556 with linkage having the same name and type, ignoring entities
3557 declared outside the innermost enclosing namespace scope, the
3558 block scope declaration declares that same entity and
3559 receives the linkage of the previous declaration. */
3560 if (! t && current_function_decl && x != current_function_decl
3561 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3562 && DECL_EXTERNAL (x))
3564 /* Look in block scope. */
3565 t = IDENTIFIER_VALUE (name);
3566 /* Or in the innermost namespace. */
3567 if (! t)
3568 t = namespace_binding (name, DECL_CONTEXT (x));
3569 /* Does it have linkage? Note that if this isn't a DECL, it's an
3570 OVERLOAD, which is OK. */
3571 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3572 t = NULL_TREE;
3573 if (t)
3574 different_binding_level = 1;
3577 /* If we are declaring a function, and the result of name-lookup
3578 was an OVERLOAD, look for an overloaded instance that is
3579 actually the same as the function we are declaring. (If
3580 there is one, we have to merge our declaration with the
3581 previous declaration.) */
3582 if (t && TREE_CODE (t) == OVERLOAD)
3584 tree match;
3586 if (TREE_CODE (x) == FUNCTION_DECL)
3587 for (match = t; match; match = OVL_NEXT (match))
3589 if (decls_match (OVL_CURRENT (match), x))
3590 break;
3592 else
3593 /* Just choose one. */
3594 match = t;
3596 if (match)
3597 t = OVL_CURRENT (match);
3598 else
3599 t = NULL_TREE;
3602 if (t == error_mark_node)
3604 /* error_mark_node is 0 for a while during initialization! */
3605 t = NULL_TREE;
3606 cp_error_at ("`%#D' used prior to declaration", x);
3608 else if (t != NULL_TREE)
3610 if (different_binding_level)
3612 if (decls_match (x, t))
3613 /* The standard only says that the local extern
3614 inherits linkage from the previous decl; in
3615 particular, default args are not shared. It would
3616 be nice to propagate inlining info, though. FIXME. */
3617 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3619 else if (TREE_CODE (t) == PARM_DECL)
3621 if (DECL_CONTEXT (t) == NULL_TREE)
3622 /* This is probably caused by too many errors, but calling
3623 abort will say that if errors have occurred. */
3624 abort ();
3626 /* Check for duplicate params. */
3627 if (duplicate_decls (x, t))
3628 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3630 else if ((DECL_EXTERN_C_FUNCTION_P (x)
3631 || DECL_FUNCTION_TEMPLATE_P (x))
3632 && is_overloaded_fn (t))
3633 /* Don't do anything just yet. */;
3634 else if (t == wchar_decl_node)
3636 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3637 pedwarn ("redeclaration of `wchar_t' as `%T'",
3638 TREE_TYPE (x));
3640 /* Throw away the redeclaration. */
3641 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3643 else if (TREE_CODE (t) != TREE_CODE (x))
3645 if (duplicate_decls (x, t))
3646 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3648 else if (duplicate_decls (x, t))
3650 if (TREE_CODE (t) == TYPE_DECL)
3651 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3652 else if (TREE_CODE (t) == FUNCTION_DECL)
3653 check_default_args (t);
3655 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3657 else if (DECL_MAIN_P (x))
3659 /* A redeclaration of main, but not a duplicate of the
3660 previous one.
3662 [basic.start.main]
3664 This function shall not be overloaded. */
3665 cp_error_at ("invalid redeclaration of `%D'", t);
3666 error ("as `%D'", x);
3667 /* We don't try to push this declaration since that
3668 causes a crash. */
3669 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3673 check_template_shadow (x);
3675 /* If this is a function conjured up by the backend, massage it
3676 so it looks friendly. */
3677 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3679 retrofit_lang_decl (x);
3680 SET_DECL_LANGUAGE (x, lang_c);
3683 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3685 t = push_overloaded_decl (x, PUSH_LOCAL);
3686 if (t != x)
3687 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3688 if (!namespace_bindings_p ())
3689 /* We do not need to create a binding for this name;
3690 push_overloaded_decl will have already done so if
3691 necessary. */
3692 need_new_binding = 0;
3694 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3696 t = push_overloaded_decl (x, PUSH_GLOBAL);
3697 if (t == x)
3698 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3699 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3702 /* If declaring a type as a typedef, copy the type (unless we're
3703 at line 0), and install this TYPE_DECL as the new type's typedef
3704 name. See the extensive comment in ../c-decl.c (pushdecl). */
3705 if (TREE_CODE (x) == TYPE_DECL)
3707 tree type = TREE_TYPE (x);
3708 if (DECL_SOURCE_LINE (x) == 0)
3710 if (TYPE_NAME (type) == 0)
3711 TYPE_NAME (type) = x;
3713 else if (type != error_mark_node && TYPE_NAME (type) != x
3714 /* We don't want to copy the type when all we're
3715 doing is making a TYPE_DECL for the purposes of
3716 inlining. */
3717 && (!TYPE_NAME (type)
3718 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3720 DECL_ORIGINAL_TYPE (x) = type;
3721 type = build_type_copy (type);
3722 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3723 TYPE_NAME (type) = x;
3724 TREE_TYPE (x) = type;
3727 if (type != error_mark_node
3728 && TYPE_NAME (type)
3729 && TYPE_IDENTIFIER (type))
3730 set_identifier_type_value (DECL_NAME (x), x);
3733 /* Multiple external decls of the same identifier ought to match.
3735 We get warnings about inline functions where they are defined.
3736 We get warnings about other functions from push_overloaded_decl.
3738 Avoid duplicate warnings where they are used. */
3739 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3741 tree decl;
3743 decl = IDENTIFIER_NAMESPACE_VALUE (name);
3744 if (decl && TREE_CODE (decl) == OVERLOAD)
3745 decl = OVL_FUNCTION (decl);
3747 if (decl && decl != error_mark_node
3748 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3749 /* If different sort of thing, we already gave an error. */
3750 && TREE_CODE (decl) == TREE_CODE (x)
3751 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3753 pedwarn ("type mismatch with previous external decl of `%#D'", x);
3754 cp_pedwarn_at ("previous external decl of `%#D'", decl);
3758 /* This name is new in its binding level.
3759 Install the new declaration and return it. */
3760 if (namespace_bindings_p ())
3762 /* Install a global value. */
3764 /* If the first global decl has external linkage,
3765 warn if we later see static one. */
3766 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3767 TREE_PUBLIC (name) = 1;
3769 /* Bind the name for the entity. */
3770 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3771 && t != NULL_TREE)
3772 && (TREE_CODE (x) == TYPE_DECL
3773 || TREE_CODE (x) == VAR_DECL
3774 || TREE_CODE (x) == ALIAS_DECL
3775 || TREE_CODE (x) == NAMESPACE_DECL
3776 || TREE_CODE (x) == CONST_DECL
3777 || TREE_CODE (x) == TEMPLATE_DECL))
3778 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3780 /* Don't forget if the function was used via an implicit decl. */
3781 if (IDENTIFIER_IMPLICIT_DECL (name)
3782 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3783 TREE_USED (x) = 1;
3785 /* Don't forget if its address was taken in that way. */
3786 if (IDENTIFIER_IMPLICIT_DECL (name)
3787 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3788 TREE_ADDRESSABLE (x) = 1;
3790 /* Warn about mismatches against previous implicit decl. */
3791 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3792 /* If this real decl matches the implicit, don't complain. */
3793 && ! (TREE_CODE (x) == FUNCTION_DECL
3794 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3795 warning
3796 ("`%D' was previously implicitly declared to return `int'", x);
3798 /* If new decl is `static' and an `extern' was seen previously,
3799 warn about it. */
3800 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3801 warn_extern_redeclared_static (x, t);
3803 else
3805 /* Here to install a non-global value. */
3806 tree oldlocal = IDENTIFIER_VALUE (name);
3807 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3809 if (need_new_binding)
3811 push_local_binding (name, x, 0);
3812 /* Because push_local_binding will hook X on to the
3813 current_binding_level's name list, we don't want to
3814 do that again below. */
3815 need_new_binding = 0;
3818 /* If this is a TYPE_DECL, push it into the type value slot. */
3819 if (TREE_CODE (x) == TYPE_DECL)
3820 set_identifier_type_value (name, x);
3822 /* Clear out any TYPE_DECL shadowed by a namespace so that
3823 we won't think this is a type. The C struct hack doesn't
3824 go through namespaces. */
3825 if (TREE_CODE (x) == NAMESPACE_DECL)
3826 set_identifier_type_value (name, NULL_TREE);
3828 if (oldlocal)
3830 tree d = oldlocal;
3832 while (oldlocal
3833 && TREE_CODE (oldlocal) == VAR_DECL
3834 && DECL_DEAD_FOR_LOCAL (oldlocal))
3835 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
3837 if (oldlocal == NULL_TREE)
3838 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
3841 /* If this is an extern function declaration, see if we
3842 have a global definition or declaration for the function. */
3843 if (oldlocal == NULL_TREE
3844 && DECL_EXTERNAL (x)
3845 && oldglobal != NULL_TREE
3846 && TREE_CODE (x) == FUNCTION_DECL
3847 && TREE_CODE (oldglobal) == FUNCTION_DECL)
3849 /* We have one. Their types must agree. */
3850 if (decls_match (x, oldglobal))
3851 /* OK */;
3852 else
3854 warning ("extern declaration of `%#D' doesn't match", x);
3855 cp_warning_at ("global declaration `%#D'", oldglobal);
3858 /* If we have a local external declaration,
3859 and no file-scope declaration has yet been seen,
3860 then if we later have a file-scope decl it must not be static. */
3861 if (oldlocal == NULL_TREE
3862 && oldglobal == NULL_TREE
3863 && DECL_EXTERNAL (x)
3864 && TREE_PUBLIC (x))
3865 TREE_PUBLIC (name) = 1;
3867 /* Warn if shadowing an argument at the top level of the body. */
3868 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3869 /* Inline decls shadow nothing. */
3870 && !DECL_FROM_INLINE (x)
3871 && TREE_CODE (oldlocal) == PARM_DECL
3872 /* Don't check the `this' parameter. */
3873 && !DECL_ARTIFICIAL (oldlocal))
3875 bool err = false;
3877 /* Don't complain if it's from an enclosing function. */
3878 if (DECL_CONTEXT (oldlocal) == current_function_decl
3879 && TREE_CODE (x) != PARM_DECL)
3881 /* Go to where the parms should be and see if we find
3882 them there. */
3883 struct cp_binding_level *b = current_binding_level->level_chain;
3885 /* Skip the ctor/dtor cleanup level. */
3886 b = b->level_chain;
3888 /* ARM $8.3 */
3889 if (b->kind == sk_function_parms)
3891 error ("declaration of `%#D' shadows a parameter",
3892 name);
3893 err = true;
3897 if (warn_shadow && !err)
3898 shadow_warning (SW_PARAM,
3899 IDENTIFIER_POINTER (name), oldlocal);
3902 /* Maybe warn if shadowing something else. */
3903 else if (warn_shadow && !DECL_EXTERNAL (x)
3904 /* No shadow warnings for internally generated vars. */
3905 && ! DECL_ARTIFICIAL (x)
3906 /* No shadow warnings for vars made for inlining. */
3907 && ! DECL_FROM_INLINE (x))
3909 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
3910 && current_class_ptr
3911 && !TREE_STATIC (name))
3912 warning ("declaration of `%s' shadows a member of `this'",
3913 IDENTIFIER_POINTER (name));
3914 else if (oldlocal != NULL_TREE
3915 && TREE_CODE (oldlocal) == VAR_DECL)
3916 shadow_warning (SW_LOCAL,
3917 IDENTIFIER_POINTER (name), oldlocal);
3918 else if (oldglobal != NULL_TREE
3919 && TREE_CODE (oldglobal) == VAR_DECL)
3920 /* XXX shadow warnings in outer-more namespaces */
3921 shadow_warning (SW_GLOBAL,
3922 IDENTIFIER_POINTER (name), oldglobal);
3926 if (TREE_CODE (x) == FUNCTION_DECL)
3927 check_default_args (x);
3929 if (TREE_CODE (x) == VAR_DECL)
3930 maybe_register_incomplete_var (x);
3933 if (need_new_binding)
3934 add_decl_to_level (x,
3935 DECL_NAMESPACE_SCOPE_P (x)
3936 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
3937 : current_binding_level);
3939 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3942 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3943 caller to set DECL_CONTEXT properly. */
3945 static tree
3946 pushdecl_with_scope (tree x, struct cp_binding_level* level)
3948 register struct cp_binding_level *b;
3949 tree function_decl = current_function_decl;
3951 timevar_push (TV_NAME_LOOKUP);
3952 current_function_decl = NULL_TREE;
3953 if (level->kind == sk_class)
3955 b = class_binding_level;
3956 class_binding_level = level;
3957 pushdecl_class_level (x);
3958 class_binding_level = b;
3960 else
3962 b = current_binding_level;
3963 current_binding_level = level;
3964 x = pushdecl (x);
3965 current_binding_level = b;
3967 current_function_decl = function_decl;
3968 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3971 /* Like pushdecl, only it places X in the current namespace,
3972 if appropriate. */
3974 tree
3975 pushdecl_namespace_level (tree x)
3977 register struct cp_binding_level *b = current_binding_level;
3978 register tree t;
3980 timevar_push (TV_NAME_LOOKUP);
3981 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
3983 /* Now, the type_shadowed stack may screw us. Munge it so it does
3984 what we want. */
3985 if (TREE_CODE (x) == TYPE_DECL)
3987 tree name = DECL_NAME (x);
3988 tree newval;
3989 tree *ptr = (tree *)0;
3990 for (; !global_scope_p (b); b = b->level_chain)
3992 tree shadowed = b->type_shadowed;
3993 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3994 if (TREE_PURPOSE (shadowed) == name)
3996 ptr = &TREE_VALUE (shadowed);
3997 /* Can't break out of the loop here because sometimes
3998 a binding level will have duplicate bindings for
3999 PT names. It's gross, but I haven't time to fix it. */
4002 newval = TREE_TYPE (x);
4003 if (ptr == (tree *)0)
4005 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4006 up here if this is changed to an assertion. --KR */
4007 SET_IDENTIFIER_TYPE_VALUE (name, x);
4009 else
4011 *ptr = newval;
4014 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4017 /* Like pushdecl, only it places X in the global scope if appropriate.
4018 Calls cp_finish_decl to register the variable, initializing it with
4019 *INIT, if INIT is non-NULL. */
4021 static tree
4022 pushdecl_top_level_1 (tree x, tree *init)
4024 timevar_push (TV_NAME_LOOKUP);
4025 push_to_top_level ();
4026 x = pushdecl_namespace_level (x);
4027 if (init)
4028 cp_finish_decl (x, *init, NULL_TREE, 0);
4029 pop_from_top_level ();
4030 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4033 /* Like pushdecl, only it places X in the global scope if appropriate. */
4035 tree
4036 pushdecl_top_level (tree x)
4038 return pushdecl_top_level_1 (x, NULL);
4041 /* Like pushdecl, only it places X in the global scope if
4042 appropriate. Calls cp_finish_decl to register the variable,
4043 initializing it with INIT. */
4045 tree
4046 pushdecl_top_level_and_finish (tree x, tree init)
4048 return pushdecl_top_level_1 (x, &init);
4051 /* Make the declaration of X appear in CLASS scope. */
4053 bool
4054 pushdecl_class_level (tree x)
4056 tree name;
4057 bool is_valid = true;
4059 timevar_push (TV_NAME_LOOKUP);
4060 /* Get the name of X. */
4061 if (TREE_CODE (x) == OVERLOAD)
4062 name = DECL_NAME (get_first_fn (x));
4063 else
4064 name = DECL_NAME (x);
4066 if (name)
4068 is_valid = push_class_level_binding (name, x);
4069 if (TREE_CODE (x) == TYPE_DECL)
4070 set_identifier_type_value (name, x);
4072 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4074 /* If X is an anonymous aggregate, all of its members are
4075 treated as if they were members of the class containing the
4076 aggregate, for naming purposes. */
4077 tree f;
4079 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4081 location_t save_location = input_location;
4082 input_location = DECL_SOURCE_LOCATION (f);
4083 if (!pushdecl_class_level (f))
4084 is_valid = false;
4085 input_location = save_location;
4088 timevar_pop (TV_NAME_LOOKUP);
4090 return is_valid;
4093 /* Enter DECL into the symbol table, if that's appropriate. Returns
4094 DECL, or a modified version thereof. */
4096 tree
4097 maybe_push_decl (tree decl)
4099 tree type = TREE_TYPE (decl);
4101 /* Add this decl to the current binding level, but not if it comes
4102 from another scope, e.g. a static member variable. TEM may equal
4103 DECL or it may be a previous decl of the same name. */
4104 if (decl == error_mark_node
4105 || (TREE_CODE (decl) != PARM_DECL
4106 && DECL_CONTEXT (decl) != NULL_TREE
4107 /* Definitions of namespace members outside their namespace are
4108 possible. */
4109 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4110 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4111 || TREE_CODE (type) == UNKNOWN_TYPE
4112 /* The declaration of a template specialization does not affect
4113 the functions available for overload resolution, so we do not
4114 call pushdecl. */
4115 || (TREE_CODE (decl) == FUNCTION_DECL
4116 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4117 return decl;
4118 else
4119 return pushdecl (decl);
4122 /* Make the declaration(s) of X appear in CLASS scope under the name
4123 NAME. Returns true if the binding is valid. */
4125 bool
4126 push_class_level_binding (tree name, tree x)
4128 cxx_binding *binding;
4130 timevar_push (TV_NAME_LOOKUP);
4131 /* The class_binding_level will be NULL if x is a template
4132 parameter name in a member template. */
4133 if (!class_binding_level)
4134 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4136 /* Make sure that this new member does not have the same name
4137 as a template parameter. */
4138 if (TYPE_BEING_DEFINED (current_class_type))
4139 check_template_shadow (x);
4141 /* If this declaration shadows a declaration from an enclosing
4142 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4143 we leave this class. Record the shadowed declaration here. */
4144 binding = IDENTIFIER_BINDING (name);
4145 if (binding && BINDING_VALUE (binding))
4147 tree bval = BINDING_VALUE (binding);
4148 tree old_decl = NULL_TREE;
4150 if (INHERITED_VALUE_BINDING_P (binding))
4152 /* If the old binding was from a base class, and was for a
4153 tag name, slide it over to make room for the new binding.
4154 The old binding is still visible if explicitly qualified
4155 with a class-key. */
4156 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
4157 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4159 old_decl = BINDING_TYPE (binding);
4160 BINDING_TYPE (binding) = bval;
4161 BINDING_VALUE (binding) = NULL_TREE;
4162 INHERITED_VALUE_BINDING_P (binding) = 0;
4164 else
4165 old_decl = bval;
4167 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
4168 old_decl = bval;
4169 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
4170 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4171 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
4172 old_decl = bval;
4173 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
4174 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4176 if (old_decl)
4178 tree shadow;
4180 /* Find the previous binding of name on the class-shadowed
4181 list, and update it. */
4182 for (shadow = class_binding_level->class_shadowed;
4183 shadow;
4184 shadow = TREE_CHAIN (shadow))
4185 if (TREE_PURPOSE (shadow) == name
4186 && TREE_TYPE (shadow) == old_decl)
4188 BINDING_VALUE (binding) = x;
4189 INHERITED_VALUE_BINDING_P (binding) = 0;
4190 TREE_TYPE (shadow) = x;
4191 IDENTIFIER_CLASS_VALUE (name) = x;
4192 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4197 /* If we didn't replace an existing binding, put the binding on the
4198 stack of bindings for the identifier, and update the shadowed list. */
4199 if (push_class_binding (name, x))
4201 class_binding_level->class_shadowed
4202 = tree_cons (name, NULL,
4203 class_binding_level->class_shadowed);
4204 /* Record the value we are binding NAME to so that we can know
4205 what to pop later. */
4206 TREE_TYPE (class_binding_level->class_shadowed) = x;
4207 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4210 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
4213 /* Insert another USING_DECL into the current binding level, returning
4214 this declaration. If this is a redeclaration, do nothing, and
4215 return NULL_TREE if this not in namespace scope (in namespace
4216 scope, a using decl might extend any previous bindings). */
4218 tree
4219 push_using_decl (tree scope, tree name)
4221 tree decl;
4223 timevar_push (TV_NAME_LOOKUP);
4224 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4225 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4226 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4227 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4228 break;
4229 if (decl)
4230 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4231 namespace_bindings_p () ? decl : NULL_TREE);
4232 decl = build_lang_decl (USING_DECL, name, void_type_node);
4233 DECL_INITIAL (decl) = scope;
4234 TREE_CHAIN (decl) = current_binding_level->usings;
4235 current_binding_level->usings = decl;
4236 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4239 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4240 changed (i.e. there was already a directive), or the fresh
4241 TREE_LIST otherwise. */
4243 tree
4244 push_using_directive (tree used)
4246 tree ud = current_binding_level->using_directives;
4247 tree iter, ancestor;
4249 timevar_push (TV_NAME_LOOKUP);
4250 /* Check if we already have this. */
4251 if (purpose_member (used, ud) != NULL_TREE)
4252 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4254 ancestor = namespace_ancestor (current_decl_namespace (), used);
4255 ud = current_binding_level->using_directives;
4256 ud = tree_cons (used, ancestor, ud);
4257 current_binding_level->using_directives = ud;
4259 /* Recursively add all namespaces used. */
4260 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4261 push_using_directive (TREE_PURPOSE (iter));
4263 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4266 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4267 other definitions already in place. We get around this by making
4268 the value of the identifier point to a list of all the things that
4269 want to be referenced by that name. It is then up to the users of
4270 that name to decide what to do with that list.
4272 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4273 DECL_TEMPLATE_RESULT. It is dealt with the same way.
4275 FLAGS is a bitwise-or of the following values:
4276 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4277 namespace scope.
4278 PUSH_USING: DECL is being pushed as the result of a using
4279 declaration.
4281 The value returned may be a previous declaration if we guessed wrong
4282 about what language DECL should belong to (C or C++). Otherwise,
4283 it's always DECL (and never something that's not a _DECL). */
4285 tree
4286 push_overloaded_decl (tree decl, int flags)
4288 tree name = DECL_NAME (decl);
4289 tree old;
4290 tree new_binding;
4291 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4293 timevar_push (TV_NAME_LOOKUP);
4294 if (doing_global)
4295 old = namespace_binding (name, DECL_CONTEXT (decl));
4296 else
4297 old = lookup_name_current_level (name);
4299 if (old)
4301 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4303 tree t = TREE_TYPE (old);
4304 if (IS_AGGR_TYPE (t) && warn_shadow
4305 && (! DECL_IN_SYSTEM_HEADER (decl)
4306 || ! DECL_IN_SYSTEM_HEADER (old)))
4307 warning ("`%#D' hides constructor for `%#T'", decl, t);
4308 old = NULL_TREE;
4310 else if (is_overloaded_fn (old))
4312 tree tmp;
4314 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4316 tree fn = OVL_CURRENT (tmp);
4318 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4319 && !(flags & PUSH_USING)
4320 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4321 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4322 error ("`%#D' conflicts with previous using declaration `%#D'",
4323 decl, fn);
4325 if (duplicate_decls (decl, fn))
4326 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4329 else if (old == error_mark_node)
4330 /* Ignore the undefined symbol marker. */
4331 old = NULL_TREE;
4332 else
4334 cp_error_at ("previous non-function declaration `%#D'", old);
4335 error ("conflicts with function declaration `%#D'", decl);
4336 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4340 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4342 if (old && TREE_CODE (old) != OVERLOAD)
4343 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4344 else
4345 new_binding = ovl_cons (decl, old);
4346 if (flags & PUSH_USING)
4347 OVL_USED (new_binding) = 1;
4349 else
4350 /* NAME is not ambiguous. */
4351 new_binding = decl;
4353 if (doing_global)
4354 set_namespace_binding (name, current_namespace, new_binding);
4355 else
4357 /* We only create an OVERLOAD if there was a previous binding at
4358 this level, or if decl is a template. In the former case, we
4359 need to remove the old binding and replace it with the new
4360 binding. We must also run through the NAMES on the binding
4361 level where the name was bound to update the chain. */
4363 if (TREE_CODE (new_binding) == OVERLOAD && old)
4365 tree *d;
4367 for (d = &BINDING_SCOPE (IDENTIFIER_BINDING (name))->names;
4369 d = &TREE_CHAIN (*d))
4370 if (*d == old
4371 || (TREE_CODE (*d) == TREE_LIST
4372 && TREE_VALUE (*d) == old))
4374 if (TREE_CODE (*d) == TREE_LIST)
4375 /* Just replace the old binding with the new. */
4376 TREE_VALUE (*d) = new_binding;
4377 else
4378 /* Build a TREE_LIST to wrap the OVERLOAD. */
4379 *d = tree_cons (NULL_TREE, new_binding,
4380 TREE_CHAIN (*d));
4382 /* And update the cxx_binding node. */
4383 BINDING_VALUE (IDENTIFIER_BINDING (name))
4384 = new_binding;
4385 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4388 /* We should always find a previous binding in this case. */
4389 abort ();
4392 /* Install the new binding. */
4393 push_local_binding (name, new_binding, flags);
4396 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4399 /* Generate an implicit declaration for identifier FUNCTIONID
4400 as a function of type int (). Print a warning if appropriate. */
4402 tree
4403 implicitly_declare (tree functionid)
4405 register tree decl;
4407 /* We used to reuse an old implicit decl here,
4408 but this loses with inline functions because it can clobber
4409 the saved decl chains. */
4410 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4412 DECL_EXTERNAL (decl) = 1;
4413 TREE_PUBLIC (decl) = 1;
4415 /* ISO standard says implicit declarations are in the innermost block.
4416 So we record the decl in the standard fashion. */
4417 pushdecl (decl);
4418 rest_of_decl_compilation (decl, NULL, 0, 0);
4420 if (warn_implicit
4421 /* Only one warning per identifier. */
4422 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4424 pedwarn ("implicit declaration of function `%#D'", decl);
4427 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4429 return decl;
4432 /* Return zero if the declaration NEWDECL is valid
4433 when the declaration OLDDECL (assumed to be for the same name)
4434 has already been seen.
4435 Otherwise return an error message format string with a %s
4436 where the identifier should go. */
4438 static const char *
4439 redeclaration_error_message (tree newdecl, tree olddecl)
4441 if (TREE_CODE (newdecl) == TYPE_DECL)
4443 /* Because C++ can put things into name space for free,
4444 constructs like "typedef struct foo { ... } foo"
4445 would look like an erroneous redeclaration. */
4446 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4447 return 0;
4448 else
4449 return "redefinition of `%#D'";
4451 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4453 /* If this is a pure function, its olddecl will actually be
4454 the original initialization to `0' (which we force to call
4455 abort()). Don't complain about redefinition in this case. */
4456 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4457 return 0;
4459 /* If both functions come from different namespaces, this is not
4460 a redeclaration - this is a conflict with a used function. */
4461 if (DECL_NAMESPACE_SCOPE_P (olddecl)
4462 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4463 return "`%D' conflicts with used function";
4465 /* We'll complain about linkage mismatches in
4466 warn_extern_redeclared_static. */
4468 /* Defining the same name twice is no good. */
4469 if (DECL_INITIAL (olddecl) != NULL_TREE
4470 && DECL_INITIAL (newdecl) != NULL_TREE)
4472 if (DECL_NAME (olddecl) == NULL_TREE)
4473 return "`%#D' not declared in class";
4474 else
4475 return "redefinition of `%#D'";
4477 return 0;
4479 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4481 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4482 && (DECL_TEMPLATE_RESULT (newdecl)
4483 != DECL_TEMPLATE_RESULT (olddecl))
4484 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4485 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4486 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4487 && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4488 && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4489 return "redefinition of `%#D'";
4490 return 0;
4492 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4494 /* Objects declared at top level: */
4495 /* If at least one is a reference, it's ok. */
4496 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4497 return 0;
4498 /* Reject two definitions. */
4499 return "redefinition of `%#D'";
4501 else
4503 /* Objects declared with block scope: */
4504 /* Reject two definitions, and reject a definition
4505 together with an external reference. */
4506 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4507 return "redeclaration of `%#D'";
4508 return 0;
4512 /* Create a new label, named ID. */
4514 static tree
4515 make_label_decl (tree id, int local_p)
4517 tree decl;
4519 decl = build_decl (LABEL_DECL, id, void_type_node);
4521 DECL_CONTEXT (decl) = current_function_decl;
4522 DECL_MODE (decl) = VOIDmode;
4523 C_DECLARED_LABEL_FLAG (decl) = local_p;
4525 /* Say where one reference is to the label, for the sake of the
4526 error if it is not defined. */
4527 DECL_SOURCE_LOCATION (decl) = input_location;
4529 /* Record the fact that this identifier is bound to this label. */
4530 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4532 return decl;
4535 /* Record this label on the list of used labels so that we can check
4536 at the end of the function to see whether or not the label was
4537 actually defined, and so we can check when the label is defined whether
4538 this use is valid. */
4540 static void
4541 use_label (tree decl)
4543 if (named_label_uses == NULL
4544 || named_label_uses->names_in_scope != current_binding_level->names
4545 || named_label_uses->label_decl != decl)
4547 struct named_label_use_list *new_ent;
4548 new_ent = ggc_alloc (sizeof (struct named_label_use_list));
4549 new_ent->label_decl = decl;
4550 new_ent->names_in_scope = current_binding_level->names;
4551 new_ent->binding_level = current_binding_level;
4552 new_ent->o_goto_locus = input_location;
4553 new_ent->next = named_label_uses;
4554 named_label_uses = new_ent;
4558 /* Look for a label named ID in the current function. If one cannot
4559 be found, create one. (We keep track of used, but undefined,
4560 labels, and complain about them at the end of a function.) */
4562 tree
4563 lookup_label (tree id)
4565 tree decl;
4566 struct named_label_list *ent;
4568 timevar_push (TV_NAME_LOOKUP);
4569 /* You can't use labels at global scope. */
4570 if (current_function_decl == NULL_TREE)
4572 error ("label `%s' referenced outside of any function",
4573 IDENTIFIER_POINTER (id));
4574 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4577 /* See if we've already got this label. */
4578 decl = IDENTIFIER_LABEL_VALUE (id);
4579 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4580 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4582 /* Record this label on the list of labels used in this function.
4583 We do this before calling make_label_decl so that we get the
4584 IDENTIFIER_LABEL_VALUE before the new label is declared. */
4585 ent = ggc_alloc_cleared (sizeof (struct named_label_list));
4586 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4587 ent->next = named_labels;
4588 named_labels = ent;
4590 /* We need a new label. */
4591 decl = make_label_decl (id, /*local_p=*/0);
4593 /* Now fill in the information we didn't have before. */
4594 ent->label_decl = decl;
4596 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4599 /* Declare a local label named ID. */
4601 tree
4602 declare_local_label (tree id)
4604 tree decl;
4606 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4607 this scope we can restore the old value of
4608 IDENTIFIER_TYPE_VALUE. */
4609 current_binding_level->shadowed_labels
4610 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4611 current_binding_level->shadowed_labels);
4612 /* Look for the label. */
4613 decl = make_label_decl (id, /*local_p=*/1);
4614 /* Now fill in the information we didn't have before. */
4615 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4617 return decl;
4620 /* Returns nonzero if it is ill-formed to jump past the declaration of
4621 DECL. Returns 2 if it's also a real problem. */
4623 static int
4624 decl_jump_unsafe (tree decl)
4626 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4627 return 0;
4629 if (DECL_INITIAL (decl) == NULL_TREE
4630 && pod_type_p (TREE_TYPE (decl)))
4631 return 0;
4633 /* This is really only important if we're crossing an initialization.
4634 The POD stuff is just pedantry; why should it matter if the class
4635 contains a field of pointer to member type? */
4636 if (DECL_INITIAL (decl)
4637 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4638 return 2;
4639 return 1;
4642 /* Check that a single previously seen jump to a newly defined label
4643 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4644 the jump context; NAMES are the names in scope in LEVEL at the jump
4645 context; FILE and LINE are the source position of the jump or 0. */
4647 static void
4648 check_previous_goto_1 (tree decl,
4649 struct cp_binding_level* level,
4650 tree names, const location_t *locus)
4652 int identified = 0;
4653 int saw_eh = 0;
4654 struct cp_binding_level *b = current_binding_level;
4655 for (; b; b = b->level_chain)
4657 tree new_decls = b->names;
4658 tree old_decls = (b == level ? names : NULL_TREE);
4659 for (; new_decls != old_decls;
4660 new_decls = TREE_CHAIN (new_decls))
4662 int problem = decl_jump_unsafe (new_decls);
4663 if (! problem)
4664 continue;
4666 if (! identified)
4668 if (decl)
4669 pedwarn ("jump to label `%D'", decl);
4670 else
4671 pedwarn ("jump to case label");
4673 if (locus)
4674 pedwarn ("%H from here", locus);
4675 identified = 1;
4678 if (problem > 1)
4679 cp_error_at (" crosses initialization of `%#D'",
4680 new_decls);
4681 else
4682 cp_pedwarn_at (" enters scope of non-POD `%#D'",
4683 new_decls);
4686 if (b == level)
4687 break;
4688 if ((b->kind == sk_try || b->kind == sk_catch) && ! saw_eh)
4690 if (! identified)
4692 if (decl)
4693 pedwarn ("jump to label `%D'", decl);
4694 else
4695 pedwarn ("jump to case label");
4697 if (locus)
4698 pedwarn ("%H from here", locus);
4699 identified = 1;
4701 if (b->kind == sk_try)
4702 error (" enters try block");
4703 else
4704 error (" enters catch block");
4705 saw_eh = 1;
4710 static void
4711 check_previous_goto (struct named_label_use_list* use)
4713 check_previous_goto_1 (use->label_decl, use->binding_level,
4714 use->names_in_scope, &use->o_goto_locus);
4717 static void
4718 check_switch_goto (struct cp_binding_level* level)
4720 check_previous_goto_1 (NULL_TREE, level, level->names, NULL);
4723 /* Check that any previously seen jumps to a newly defined label DECL
4724 are OK. Called by define_label. */
4726 static void
4727 check_previous_gotos (tree decl)
4729 struct named_label_use_list **usep;
4731 if (! TREE_USED (decl))
4732 return;
4734 for (usep = &named_label_uses; *usep; )
4736 struct named_label_use_list *use = *usep;
4737 if (use->label_decl == decl)
4739 check_previous_goto (use);
4740 *usep = use->next;
4742 else
4743 usep = &(use->next);
4747 /* Check that a new jump to a label DECL is OK. Called by
4748 finish_goto_stmt. */
4750 void
4751 check_goto (tree decl)
4753 int identified = 0;
4754 tree bad;
4755 struct named_label_list *lab;
4757 /* We can't know where a computed goto is jumping. So we assume
4758 that it's OK. */
4759 if (! DECL_P (decl))
4760 return;
4762 /* If the label hasn't been defined yet, defer checking. */
4763 if (! DECL_INITIAL (decl))
4765 use_label (decl);
4766 return;
4769 for (lab = named_labels; lab; lab = lab->next)
4770 if (decl == lab->label_decl)
4771 break;
4773 /* If the label is not on named_labels it's a gcc local label, so
4774 it must be in an outer scope, so jumping to it is always OK. */
4775 if (lab == 0)
4776 return;
4778 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4779 && !identified)
4781 cp_pedwarn_at ("jump to label `%D'", decl);
4782 pedwarn (" from here");
4783 identified = 1;
4786 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4788 tree b = TREE_VALUE (bad);
4789 int u = decl_jump_unsafe (b);
4791 if (u > 1 && DECL_ARTIFICIAL (b))
4792 /* Can't skip init of __exception_info. */
4793 error ("%H enters catch block", &DECL_SOURCE_LOCATION (b));
4794 else if (u > 1)
4795 cp_error_at (" skips initialization of `%#D'", b);
4796 else
4797 cp_pedwarn_at (" enters scope of non-POD `%#D'", b);
4800 if (lab->in_try_scope)
4801 error (" enters try block");
4802 else if (lab->in_catch_scope)
4803 error (" enters catch block");
4806 /* Define a label, specifying the location in the source file.
4807 Return the LABEL_DECL node for the label. */
4809 tree
4810 define_label (location_t location, tree name)
4812 tree decl = lookup_label (name);
4813 struct named_label_list *ent;
4814 register struct cp_binding_level *p;
4816 timevar_push (TV_NAME_LOOKUP);
4817 for (ent = named_labels; ent; ent = ent->next)
4818 if (ent->label_decl == decl)
4819 break;
4821 /* After labels, make any new cleanups in the function go into their
4822 own new (temporary) binding contour. */
4823 for (p = current_binding_level;
4824 p->kind != sk_function_parms;
4825 p = p->level_chain)
4826 p->more_cleanups_ok = 0;
4828 if (name == get_identifier ("wchar_t"))
4829 pedwarn ("label named wchar_t");
4831 if (DECL_INITIAL (decl) != NULL_TREE)
4832 error ("duplicate label `%D'", decl);
4833 else
4835 /* Mark label as having been defined. */
4836 DECL_INITIAL (decl) = error_mark_node;
4837 /* Say where in the source. */
4838 DECL_SOURCE_LOCATION (decl) = location;
4839 if (ent)
4841 ent->names_in_scope = current_binding_level->names;
4842 ent->binding_level = current_binding_level;
4844 check_previous_gotos (decl);
4847 timevar_pop (TV_NAME_LOOKUP);
4848 return decl;
4851 struct cp_switch
4853 struct cp_binding_level *level;
4854 struct cp_switch *next;
4855 /* The SWITCH_STMT being built. */
4856 tree switch_stmt;
4857 /* A splay-tree mapping the low element of a case range to the high
4858 element, or NULL_TREE if there is no high element. Used to
4859 determine whether or not a new case label duplicates an old case
4860 label. We need a tree, rather than simply a hash table, because
4861 of the GNU case range extension. */
4862 splay_tree cases;
4865 /* A stack of the currently active switch statements. The innermost
4866 switch statement is on the top of the stack. There is no need to
4867 mark the stack for garbage collection because it is only active
4868 during the processing of the body of a function, and we never
4869 collect at that point. */
4871 static struct cp_switch *switch_stack;
4873 /* Called right after a switch-statement condition is parsed.
4874 SWITCH_STMT is the switch statement being parsed. */
4876 void
4877 push_switch (tree switch_stmt)
4879 struct cp_switch *p = xmalloc (sizeof (struct cp_switch));
4880 p->level = current_binding_level;
4881 p->next = switch_stack;
4882 p->switch_stmt = switch_stmt;
4883 p->cases = splay_tree_new (case_compare, NULL, NULL);
4884 switch_stack = p;
4887 void
4888 pop_switch (void)
4890 struct cp_switch *cs;
4892 cs = switch_stack;
4893 splay_tree_delete (cs->cases);
4894 switch_stack = switch_stack->next;
4895 free (cs);
4898 /* Note that we've seen a definition of a case label, and complain if this
4899 is a bad place for one. */
4901 tree
4902 finish_case_label (tree low_value, tree high_value)
4904 tree cond, r;
4905 register struct cp_binding_level *p;
4907 if (! switch_stack)
4909 if (high_value)
4910 error ("case label not within a switch statement");
4911 else if (low_value)
4912 error ("case label `%E' not within a switch statement",
4913 low_value);
4914 else
4915 error ("`default' label not within a switch statement");
4916 return NULL_TREE;
4919 if (processing_template_decl)
4921 tree label;
4923 /* For templates, just add the case label; we'll do semantic
4924 analysis at instantiation-time. */
4925 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4926 return add_stmt (build_case_label (low_value, high_value, label));
4929 /* Find the condition on which this switch statement depends. */
4930 cond = SWITCH_COND (switch_stack->switch_stmt);
4931 if (cond && TREE_CODE (cond) == TREE_LIST)
4932 cond = TREE_VALUE (cond);
4934 r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
4936 check_switch_goto (switch_stack->level);
4938 /* After labels, make any new cleanups in the function go into their
4939 own new (temporary) binding contour. */
4940 for (p = current_binding_level;
4941 p->kind != sk_function_parms;
4942 p = p->level_chain)
4943 p->more_cleanups_ok = 0;
4945 return r;
4948 /* Return the list of declarations of the current level.
4949 Note that this list is in reverse order unless/until
4950 you nreverse it; and when you do nreverse it, you must
4951 store the result back using `storedecls' or you will lose. */
4953 tree
4954 getdecls (void)
4956 return current_binding_level->names;
4959 /* Store the list of declarations of the current level.
4960 This is done for the parameter declarations of a function being defined,
4961 after they are modified in the light of any missing parameters. */
4963 static void
4964 storedecls (tree decls)
4966 current_binding_level->names = decls;
4969 /* Set the current binding TABLE for type declarations.. This is a
4970 temporary workaround of the fact that the data structure classtypes
4971 does not currently carry its allocated cxx_scope structure. */
4972 void
4973 cxx_remember_type_decls (binding_table table)
4975 current_binding_level->type_decls = table;
4978 /* Return the type that should be used when TYPE's name is preceded
4979 by a tag such as 'struct' or 'union', or null if the name cannot
4980 be used in this way.
4982 For example, when processing the third line of:
4984 struct A;
4985 typedef struct A A;
4986 struct A;
4988 lookup of A will find the typedef. Given A's typedef, this function
4989 will return the type associated with "struct A". For the tag to be
4990 anything other than TYPE, TYPE must be a typedef whose original type
4991 has the same name and context as TYPE itself.
4993 It is not valid for a typedef of an anonymous type to be used with
4994 an explicit tag:
4996 typedef struct { ... } B;
4997 struct B;
4999 Return null for this case. */
5001 static tree
5002 follow_tag_typedef (tree type)
5004 tree original;
5006 original = original_type (type);
5007 if (! TYPE_NAME (original))
5008 return NULL_TREE;
5009 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5010 && (CP_DECL_CONTEXT (TYPE_NAME (original))
5011 == CP_DECL_CONTEXT (TYPE_NAME (type)))
5012 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5013 return original;
5014 else
5015 return NULL_TREE;
5018 /* Given NAME, an IDENTIFIER_NODE,
5019 return the structure (or union or enum) definition for that name.
5020 Searches binding levels from BINDING_SCOPE up to the global level.
5021 If THISLEVEL_ONLY is nonzero, searches only the specified context
5022 (but skips any tag-transparent contexts to find one that is
5023 meaningful for tags).
5024 FORM says which kind of type the caller wants;
5025 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5026 If the wrong kind of type is found, and it's not a template, an error is
5027 reported. */
5029 static tree
5030 lookup_tag (enum tree_code form, tree name,
5031 struct cp_binding_level* binding_level, int thislevel_only)
5033 register struct cp_binding_level *level;
5034 /* Nonzero if, we should look past a template parameter level, even
5035 if THISLEVEL_ONLY. */
5036 int allow_template_parms_p = 1;
5037 bool type_is_anonymous = ANON_AGGRNAME_P (name);
5039 timevar_push (TV_NAME_LOOKUP);
5040 for (level = binding_level; level; level = level->level_chain)
5042 register tree tail;
5043 if (type_is_anonymous && level->type_decls != NULL)
5045 tree type = binding_table_find_anon_type (level->type_decls, name);
5046 /* There is no need for error checking here, because
5047 anon names are unique throughout the compilation. */
5048 if (type != NULL)
5049 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
5051 else if (level->kind == sk_namespace)
5052 /* Do namespace lookup. */
5053 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5055 cxx_binding *binding =
5056 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
5057 tree old;
5059 /* If we just skipped past a template parameter level,
5060 even though THISLEVEL_ONLY, and we find a template
5061 class declaration, then we use the _TYPE node for the
5062 template. See the example below. */
5063 if (thislevel_only && !allow_template_parms_p
5064 && binding && BINDING_VALUE (binding)
5065 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (binding)))
5066 old = BINDING_VALUE (binding);
5067 else if (binding)
5068 old = select_decl (binding, LOOKUP_PREFER_TYPES);
5069 else
5070 old = NULL_TREE;
5072 if (old)
5074 /* We've found something at this binding level. If it is
5075 a typedef, extract the tag it refers to. Lookup fails
5076 if the typedef doesn't refer to a taggable type. */
5077 old = TREE_TYPE (old);
5078 old = follow_tag_typedef (old);
5079 if (!old)
5080 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5081 if (TREE_CODE (old) != form
5082 && (form == ENUMERAL_TYPE
5083 || TREE_CODE (old) == ENUMERAL_TYPE))
5085 error ("`%#D' redeclared as %C", old, form);
5086 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5088 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
5090 if (thislevel_only || tail == global_namespace)
5091 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5093 else if (level->type_decls != NULL)
5095 binding_entry entry = binding_table_find (level->type_decls, name);
5096 if (entry != NULL)
5098 enum tree_code code = TREE_CODE (entry->type);
5100 if (code != form
5101 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5103 /* Definition isn't the kind we were looking for. */
5104 error ("`%#D' redeclared as %C", entry->type, form);
5105 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5107 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
5110 if (thislevel_only && ! level->tag_transparent)
5112 if (level->kind == sk_template_parms && allow_template_parms_p)
5114 /* We must deal with cases like this:
5116 template <class T> struct S;
5117 template <class T> struct S {};
5119 When looking up `S', for the second declaration, we
5120 would like to find the first declaration. But, we
5121 are in the pseudo-global level created for the
5122 template parameters, rather than the (surrounding)
5123 namespace level. Thus, we keep going one more level,
5124 even though THISLEVEL_ONLY is nonzero. */
5125 allow_template_parms_p = 0;
5126 continue;
5128 else
5129 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5132 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5135 /* Given a type, find the tag that was defined for it and return the tag name.
5136 Otherwise return 0. However, the value can never be 0
5137 in the cases in which this is used.
5139 C++: If NAME is nonzero, this is the new name to install. This is
5140 done when replacing anonymous tags with real tag names. */
5142 static tree
5143 lookup_tag_reverse (tree type, tree name)
5145 register struct cp_binding_level *level;
5147 timevar_push (TV_NAME_LOOKUP);
5148 for (level = current_binding_level; level; level = level->level_chain)
5150 binding_entry entry = level->type_decls == NULL
5151 ? NULL
5152 : binding_table_reverse_maybe_remap (level->type_decls, type, name);
5153 if (entry)
5154 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
5156 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5159 /* Look up NAME in the NAMESPACE. */
5161 tree
5162 lookup_namespace_name (tree namespace, tree name)
5164 tree val;
5165 tree template_id = NULL_TREE;
5166 cxx_binding binding;
5168 timevar_push (TV_NAME_LOOKUP);
5169 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5171 if (TREE_CODE (name) == NAMESPACE_DECL)
5172 /* This happens for A::B<int> when B is a namespace. */
5173 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5174 else if (TREE_CODE (name) == TEMPLATE_DECL)
5176 /* This happens for A::B where B is a template, and there are no
5177 template arguments. */
5178 error ("invalid use of `%D'", name);
5179 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5182 namespace = ORIGINAL_NAMESPACE (namespace);
5184 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5186 template_id = name;
5187 name = TREE_OPERAND (name, 0);
5188 if (TREE_CODE (name) == OVERLOAD)
5189 name = DECL_NAME (OVL_CURRENT (name));
5190 else if (DECL_P (name))
5191 name = DECL_NAME (name);
5194 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5196 cxx_binding_clear (&binding);
5197 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
5198 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5200 if (binding.value)
5202 val = binding.value;
5204 if (template_id)
5206 if (DECL_CLASS_TEMPLATE_P (val))
5207 val = lookup_template_class (val,
5208 TREE_OPERAND (template_id, 1),
5209 /*in_decl=*/NULL_TREE,
5210 /*context=*/NULL_TREE,
5211 /*entering_scope=*/0,
5212 tf_error | tf_warning);
5213 else if (DECL_FUNCTION_TEMPLATE_P (val)
5214 || TREE_CODE (val) == OVERLOAD)
5215 val = lookup_template_function (val,
5216 TREE_OPERAND (template_id, 1));
5217 else
5219 error ("`%D::%D' is not a template",
5220 namespace, name);
5221 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5225 /* If we have a single function from a using decl, pull it out. */
5226 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5227 val = OVL_FUNCTION (val);
5229 /* Ignore built-in functions that haven't been prototyped yet. */
5230 if (!val || !DECL_P(val)
5231 || !DECL_LANG_SPECIFIC(val)
5232 || !DECL_ANTICIPATED (val))
5233 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5236 error ("`%D' undeclared in namespace `%D'", name, namespace);
5237 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5240 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5242 static hashval_t
5243 typename_hash (const void* k)
5245 hashval_t hash;
5246 tree t = (tree) k;
5248 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5249 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5251 return hash;
5254 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5256 static int
5257 typename_compare (const void * k1, const void * k2)
5259 tree t1;
5260 tree t2;
5261 tree d1;
5262 tree d2;
5264 t1 = (tree) k1;
5265 t2 = (tree) k2;
5266 d1 = TYPE_NAME (t1);
5267 d2 = TYPE_NAME (t2);
5269 return (DECL_NAME (d1) == DECL_NAME (d2)
5270 && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5271 && ((TREE_TYPE (t1) != NULL_TREE)
5272 == (TREE_TYPE (t2) != NULL_TREE))
5273 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5274 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5277 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5278 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5279 is non-NULL, this type is being created by the implicit typename
5280 extension, and BASE_TYPE is a type named `t' in some base class of
5281 `T' which depends on template parameters.
5283 Returns the new TYPENAME_TYPE. */
5285 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5287 static tree
5288 build_typename_type (tree context, tree name, tree fullname)
5290 tree t;
5291 tree d;
5292 void **e;
5294 if (typename_htab == NULL)
5296 typename_htab = htab_create_ggc (61, &typename_hash,
5297 &typename_compare, NULL);
5300 /* Build the TYPENAME_TYPE. */
5301 t = make_aggr_type (TYPENAME_TYPE);
5302 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5303 TYPENAME_TYPE_FULLNAME (t) = fullname;
5305 /* Build the corresponding TYPE_DECL. */
5306 d = build_decl (TYPE_DECL, name, t);
5307 TYPE_NAME (TREE_TYPE (d)) = d;
5308 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5309 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5310 DECL_ARTIFICIAL (d) = 1;
5312 /* See if we already have this type. */
5313 e = htab_find_slot (typename_htab, t, INSERT);
5314 if (*e)
5315 t = (tree) *e;
5316 else
5317 *e = t;
5319 return t;
5322 /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type,
5323 unless an error occurs, in which case error_mark_node is returned.
5324 If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5325 set, we return that, rather than the _TYPE it corresponds to, in
5326 other cases we look through the type decl. If TF_ERROR is set,
5327 complain about errors, otherwise be quiet. */
5329 tree
5330 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5332 tree fullname;
5334 if (name == error_mark_node
5335 || context == NULL_TREE
5336 || context == error_mark_node)
5337 return error_mark_node;
5339 if (TYPE_P (name))
5341 if (!(TYPE_LANG_SPECIFIC (name)
5342 && (CLASSTYPE_IS_TEMPLATE (name)
5343 || CLASSTYPE_USE_TEMPLATE (name))))
5344 name = TYPE_IDENTIFIER (name);
5345 else
5346 /* Create a TEMPLATE_ID_EXPR for the type. */
5347 name = build_nt (TEMPLATE_ID_EXPR,
5348 CLASSTYPE_TI_TEMPLATE (name),
5349 CLASSTYPE_TI_ARGS (name));
5351 else if (TREE_CODE (name) == TYPE_DECL)
5352 name = DECL_NAME (name);
5354 fullname = name;
5356 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5358 name = TREE_OPERAND (name, 0);
5359 if (TREE_CODE (name) == TEMPLATE_DECL)
5360 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5362 if (TREE_CODE (name) == TEMPLATE_DECL)
5364 error ("`%D' used without template parameters", name);
5365 return error_mark_node;
5367 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 20030802);
5369 if (TREE_CODE (context) == NAMESPACE_DECL)
5371 /* We can get here from typename_sub0 in the explicit_template_type
5372 expansion. Just fail. */
5373 if (complain & tf_error)
5374 error ("no class template named `%#T' in `%#T'",
5375 name, context);
5376 return error_mark_node;
5379 if (! uses_template_parms (context)
5380 || currently_open_class (context))
5382 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5384 tree tmpl = NULL_TREE;
5385 if (IS_AGGR_TYPE (context))
5386 tmpl = lookup_field (context, name, 0, false);
5387 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5389 if (complain & tf_error)
5390 error ("no class template named `%#T' in `%#T'",
5391 name, context);
5392 return error_mark_node;
5395 if (complain & tf_error)
5396 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
5398 return lookup_template_class (tmpl,
5399 TREE_OPERAND (fullname, 1),
5400 NULL_TREE, context,
5401 /*entering_scope=*/0,
5402 tf_error | tf_warning | tf_user);
5404 else
5406 tree t;
5408 if (!IS_AGGR_TYPE (context))
5410 if (complain & tf_error)
5411 error ("no type named `%#T' in `%#T'", name, context);
5412 return error_mark_node;
5415 t = lookup_field (context, name, 0, true);
5416 if (t)
5418 if (TREE_CODE (t) != TYPE_DECL)
5420 if (complain & tf_error)
5421 error ("no type named `%#T' in `%#T'", name, context);
5422 return error_mark_node;
5425 if (complain & tf_error)
5426 perform_or_defer_access_check (TYPE_BINFO (context), t);
5428 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5429 t = TREE_TYPE (t);
5431 return t;
5436 /* If the CONTEXT is not a template type, then either the field is
5437 there now or its never going to be. */
5438 if (!uses_template_parms (context))
5440 if (complain & tf_error)
5441 error ("no type named `%#T' in `%#T'", name, context);
5442 return error_mark_node;
5445 return build_typename_type (context, name, fullname);
5448 /* Resolve `CONTEXT::template NAME'. Returns an appropriate type,
5449 unless an error occurs, in which case error_mark_node is returned.
5450 If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5451 corresponds to. If COMPLAIN zero, don't complain about any errors
5452 that occur. */
5454 tree
5455 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5457 tree t;
5458 tree d;
5460 if (TYPE_P (name))
5461 name = TYPE_IDENTIFIER (name);
5462 else if (DECL_P (name))
5463 name = DECL_NAME (name);
5464 if (TREE_CODE (name) != IDENTIFIER_NODE)
5465 abort ();
5467 if (!uses_template_parms (context)
5468 || currently_open_class (context))
5470 tree tmpl = NULL_TREE;
5472 if (IS_AGGR_TYPE (context))
5473 tmpl = lookup_field (context, name, 0, false);
5475 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5477 if (complain & tf_error)
5478 error ("no class template named `%#T' in `%#T'", name, context);
5479 return error_mark_node;
5482 if (complain & tf_error)
5483 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
5485 return tmpl;
5488 /* Build the UNBOUND_CLASS_TEMPLATE. */
5489 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5490 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5491 TREE_TYPE (t) = NULL_TREE;
5493 /* Build the corresponding TEMPLATE_DECL. */
5494 d = build_decl (TEMPLATE_DECL, name, t);
5495 TYPE_NAME (TREE_TYPE (d)) = d;
5496 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5497 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5498 DECL_ARTIFICIAL (d) = 1;
5500 return t;
5503 /* Select the right _DECL from multiple choices. */
5505 static tree
5506 select_decl (cxx_binding *binding, int flags)
5508 tree val;
5509 val = BINDING_VALUE (binding);
5511 timevar_push (TV_NAME_LOOKUP);
5512 if (LOOKUP_NAMESPACES_ONLY (flags))
5514 /* We are not interested in types. */
5515 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5516 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5517 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5520 /* If looking for a type, or if there is no non-type binding, select
5521 the value binding. */
5522 if (BINDING_TYPE (binding)
5523 && (!val || (flags & LOOKUP_PREFER_TYPES)))
5524 val = BINDING_TYPE (binding);
5525 /* Don't return non-types if we really prefer types. */
5526 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
5527 && (TREE_CODE (val) != TEMPLATE_DECL
5528 || !DECL_CLASS_TEMPLATE_P (val)))
5529 val = NULL_TREE;
5531 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5534 /* Unscoped lookup of a global: iterate over current namespaces,
5535 considering using-directives. If SPACESP is non-NULL, store a list
5536 of the namespaces we've considered in it. */
5538 tree
5539 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5541 tree initial = current_decl_namespace ();
5542 tree scope = initial;
5543 tree siter;
5544 struct cp_binding_level *level;
5545 tree val = NULL_TREE;
5546 cxx_binding binding;
5548 timevar_push (TV_NAME_LOOKUP);
5549 cxx_binding_clear (&binding);
5550 if (spacesp)
5551 *spacesp = NULL_TREE;
5553 for (; !val; scope = CP_DECL_CONTEXT (scope))
5555 cxx_binding *b =
5556 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
5557 if (spacesp)
5558 *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5560 /* Ignore anticipated built-in functions. */
5561 if (b && BINDING_VALUE (b)
5562 && DECL_P (BINDING_VALUE (b))
5563 && DECL_LANG_SPECIFIC (BINDING_VALUE (b))
5564 && DECL_ANTICIPATED (BINDING_VALUE (b)))
5565 /* Keep binding cleared. */;
5566 else if (b)
5568 /* Initialize binding for this context. */
5569 binding.value = BINDING_VALUE (b);
5570 binding.type = BINDING_TYPE (b);
5573 /* Add all _DECLs seen through local using-directives. */
5574 for (level = current_binding_level;
5575 level->kind != sk_namespace;
5576 level = level->level_chain)
5577 if (!lookup_using_namespace (name, &binding, level->using_directives,
5578 scope, flags, spacesp))
5579 /* Give up because of error. */
5580 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5582 /* Add all _DECLs seen through global using-directives. */
5583 /* XXX local and global using lists should work equally. */
5584 siter = initial;
5585 while (1)
5587 if (!lookup_using_namespace (name, &binding,
5588 DECL_NAMESPACE_USING (siter),
5589 scope, flags, spacesp))
5590 /* Give up because of error. */
5591 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5592 if (siter == scope) break;
5593 siter = CP_DECL_CONTEXT (siter);
5596 val = select_decl (&binding, flags);
5597 if (scope == global_namespace)
5598 break;
5600 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5603 /* Combine prefer_type and namespaces_only into flags. */
5605 static int
5606 lookup_flags (int prefer_type, int namespaces_only)
5608 if (namespaces_only)
5609 return LOOKUP_PREFER_NAMESPACES;
5610 if (prefer_type > 1)
5611 return LOOKUP_PREFER_TYPES;
5612 if (prefer_type > 0)
5613 return LOOKUP_PREFER_BOTH;
5614 return 0;
5617 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5618 ignore it or not. Subroutine of lookup_name_real. */
5620 static tree
5621 qualify_lookup (tree val, int flags)
5623 if (val == NULL_TREE)
5624 return val;
5625 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5626 return val;
5627 if ((flags & LOOKUP_PREFER_TYPES)
5628 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
5629 return val;
5630 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5631 return NULL_TREE;
5632 return val;
5635 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5636 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
5637 bindings.
5639 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5640 declaration found. If no suitable declaration can be found,
5641 ERROR_MARK_NODE is returned. Iif COMPLAIN is true and SCOPE is
5642 neither a class-type nor a namespace a diagnostic is issued. */
5644 tree
5645 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
5647 int flags = 0;
5649 if (TREE_CODE (scope) == NAMESPACE_DECL)
5651 cxx_binding binding;
5653 cxx_binding_clear (&binding);
5654 flags |= LOOKUP_COMPLAIN;
5655 if (is_type_p)
5656 flags |= LOOKUP_PREFER_TYPES;
5657 if (qualified_lookup_using_namespace (name, scope, &binding,
5658 flags))
5659 return select_decl (&binding, flags);
5661 else if (is_aggr_type (scope, complain))
5663 tree t;
5664 t = lookup_member (scope, name, 0, is_type_p);
5665 if (t)
5666 return t;
5669 return error_mark_node;
5672 /* Check to see whether or not DECL is a variable that would have been
5673 in scope under the ARM, but is not in scope under the ANSI/ISO
5674 standard. If so, issue an error message. If name lookup would
5675 work in both cases, but return a different result, this function
5676 returns the result of ANSI/ISO lookup. Otherwise, it returns
5677 DECL. */
5679 tree
5680 check_for_out_of_scope_variable (tree decl)
5682 tree shadowed;
5684 /* We only care about out of scope variables. */
5685 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5686 return decl;
5688 shadowed = DECL_SHADOWED_FOR_VAR (decl);
5689 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5690 && DECL_DEAD_FOR_LOCAL (shadowed))
5691 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5692 if (!shadowed)
5693 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5694 if (shadowed)
5696 if (!DECL_ERROR_REPORTED (decl))
5698 warning ("name lookup of `%D' changed",
5699 DECL_NAME (decl));
5700 cp_warning_at (" matches this `%D' under ISO standard rules",
5701 shadowed);
5702 cp_warning_at (" matches this `%D' under old rules", decl);
5703 DECL_ERROR_REPORTED (decl) = 1;
5705 return shadowed;
5708 /* If we have already complained about this declaration, there's no
5709 need to do it again. */
5710 if (DECL_ERROR_REPORTED (decl))
5711 return decl;
5713 DECL_ERROR_REPORTED (decl) = 1;
5714 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5716 error ("name lookup of `%D' changed for new ISO `for' scoping",
5717 DECL_NAME (decl));
5718 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl);
5719 return error_mark_node;
5721 else
5723 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5724 DECL_NAME (decl));
5725 cp_pedwarn_at (" using obsolete binding at `%D'", decl);
5728 return decl;
5731 /* Look up NAME in the current binding level and its superiors in the
5732 namespace of variables, functions and typedefs. Return a ..._DECL
5733 node of some kind representing its definition if there is only one
5734 such declaration, or return a TREE_LIST with all the overloaded
5735 definitions if there are many, or return 0 if it is undefined.
5737 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5738 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5739 Otherwise we prefer non-TYPE_DECLs.
5741 If NONCLASS is nonzero, we don't look for the NAME in class scope,
5742 using IDENTIFIER_CLASS_VALUE. */
5744 tree
5745 lookup_name_real (tree name, int prefer_type, int nonclass,
5746 int namespaces_only, int flags)
5748 cxx_binding *iter;
5749 tree val = NULL_TREE;
5751 timevar_push (TV_NAME_LOOKUP);
5752 /* Conversion operators are handled specially because ordinary
5753 unqualified name lookup will not find template conversion
5754 operators. */
5755 if (IDENTIFIER_TYPENAME_P (name))
5757 struct cp_binding_level *level;
5759 for (level = current_binding_level;
5760 level && level->kind != sk_namespace;
5761 level = level->level_chain)
5763 tree class_type;
5764 tree operators;
5766 /* A conversion operator can only be declared in a class
5767 scope. */
5768 if (level->kind != sk_class)
5769 continue;
5771 /* Lookup the conversion operator in the class. */
5772 class_type = level->this_entity;
5773 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5774 if (operators)
5775 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
5778 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5781 flags |= lookup_flags (prefer_type, namespaces_only);
5783 /* First, look in non-namespace scopes. */
5785 if (current_class_type == NULL_TREE)
5786 nonclass = 1;
5788 for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
5790 tree binding;
5792 if (!LOCAL_BINDING_P (iter) && nonclass)
5793 /* We're not looking for class-scoped bindings, so keep going. */
5794 continue;
5796 /* If this is the kind of thing we're looking for, we're done. */
5797 if (qualify_lookup (BINDING_VALUE (iter), flags))
5798 binding = BINDING_VALUE (iter);
5799 else if ((flags & LOOKUP_PREFER_TYPES)
5800 && qualify_lookup (BINDING_TYPE (iter), flags))
5801 binding = BINDING_TYPE (iter);
5802 else
5803 binding = NULL_TREE;
5805 if (binding)
5807 val = binding;
5808 break;
5812 /* Now lookup in namespace scopes. */
5813 if (!val)
5815 tree t = unqualified_namespace_lookup (name, flags, 0);
5816 if (t)
5817 val = t;
5820 if (val)
5822 /* If we have a single function from a using decl, pull it out. */
5823 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5824 val = OVL_FUNCTION (val);
5827 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5830 tree
5831 lookup_name_nonclass (tree name)
5833 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
5836 tree
5837 lookup_function_nonclass (tree name, tree args)
5839 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5842 tree
5843 lookup_name (tree name, int prefer_type)
5845 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
5848 /* Similar to `lookup_name' but look only in the innermost non-class
5849 binding level. */
5851 tree
5852 lookup_name_current_level (tree name)
5854 struct cp_binding_level *b;
5855 tree t = NULL_TREE;
5857 timevar_push (TV_NAME_LOOKUP);
5858 b = innermost_nonclass_level ();
5860 if (b->kind == sk_namespace)
5862 t = IDENTIFIER_NAMESPACE_VALUE (name);
5864 /* extern "C" function() */
5865 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5866 t = TREE_VALUE (t);
5868 else if (IDENTIFIER_BINDING (name)
5869 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5871 while (1)
5873 if (BINDING_SCOPE (IDENTIFIER_BINDING (name)) == b)
5874 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
5876 if (b->keep == 2)
5877 b = b->level_chain;
5878 else
5879 break;
5883 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5886 /* Like lookup_name_current_level, but for types. */
5888 tree
5889 lookup_type_current_level (tree name)
5891 register tree t = NULL_TREE;
5893 timevar_push (TV_NAME_LOOKUP);
5894 my_friendly_assert (current_binding_level->kind != sk_namespace,
5895 980716);
5897 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5898 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5900 struct cp_binding_level *b = current_binding_level;
5901 while (1)
5903 if (purpose_member (name, b->type_shadowed))
5904 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
5905 REAL_IDENTIFIER_TYPE_VALUE (name));
5906 if (b->keep == 2)
5907 b = b->level_chain;
5908 else
5909 break;
5913 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5918 /* A chain of TYPE_DECLs for the builtin types. */
5920 static GTY(()) tree builtin_type_decls;
5922 /* Return a chain of TYPE_DECLs for the builtin types. */
5924 tree
5925 cxx_builtin_type_decls ()
5927 return builtin_type_decls;
5930 /* Push the declarations of builtin types into the namespace.
5931 RID_INDEX is the index of the builtin type in the array
5932 RID_POINTERS. NAME is the name used when looking up the builtin
5933 type. TYPE is the _TYPE node for the builtin type. */
5935 void
5936 record_builtin_type (enum rid rid_index,
5937 const char* name,
5938 tree type)
5940 tree rname = NULL_TREE, tname = NULL_TREE;
5941 tree tdecl = NULL_TREE;
5943 if ((int) rid_index < (int) RID_MAX)
5944 rname = ridpointers[(int) rid_index];
5945 if (name)
5946 tname = get_identifier (name);
5948 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
5949 eliminated. Built-in types should not be looked up name; their
5950 names are keywords that the parser can recognize. However, there
5951 is code in c-common.c that uses identifier_global_value to look
5952 up built-in types by name. */
5953 if (tname)
5955 tdecl = build_decl (TYPE_DECL, tname, type);
5956 DECL_ARTIFICIAL (tdecl) = 1;
5957 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
5959 if (rname)
5961 if (!tdecl)
5963 tdecl = build_decl (TYPE_DECL, rname, type);
5964 DECL_ARTIFICIAL (tdecl) = 1;
5966 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
5969 if (!TYPE_NAME (type))
5970 TYPE_NAME (type) = tdecl;
5972 if (tdecl)
5974 TREE_CHAIN (tdecl) = builtin_type_decls;
5975 builtin_type_decls = tdecl;
5979 /* Record one of the standard Java types.
5980 * Declare it as having the given NAME.
5981 * If SIZE > 0, it is the size of one of the integral types;
5982 * otherwise it is the negative of the size of one of the other types. */
5984 static tree
5985 record_builtin_java_type (const char* name, int size)
5987 tree type, decl;
5988 if (size > 0)
5989 type = make_signed_type (size);
5990 else if (size > -32)
5991 { /* "__java_char" or ""__java_boolean". */
5992 type = make_unsigned_type (-size);
5993 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
5995 else
5996 { /* "__java_float" or ""__java_double". */
5997 type = make_node (REAL_TYPE);
5998 TYPE_PRECISION (type) = - size;
5999 layout_type (type);
6001 record_builtin_type (RID_MAX, name, type);
6002 decl = TYPE_NAME (type);
6004 /* Suppress generate debug symbol entries for these types,
6005 since for normal C++ they are just clutter.
6006 However, push_lang_context undoes this if extern "Java" is seen. */
6007 DECL_IGNORED_P (decl) = 1;
6009 TYPE_FOR_JAVA (type) = 1;
6010 return type;
6013 /* Push a type into the namespace so that the back-ends ignore it. */
6015 static void
6016 record_unknown_type (tree type, const char* name)
6018 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6019 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
6020 DECL_IGNORED_P (decl) = 1;
6021 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6022 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6023 TYPE_ALIGN (type) = 1;
6024 TYPE_USER_ALIGN (type) = 0;
6025 TYPE_MODE (type) = TYPE_MODE (void_type_node);
6028 /* An string for which we should create an IDENTIFIER_NODE at
6029 startup. */
6031 typedef struct predefined_identifier
6033 /* The name of the identifier. */
6034 const char *const name;
6035 /* The place where the IDENTIFIER_NODE should be stored. */
6036 tree *const node;
6037 /* Nonzero if this is the name of a constructor or destructor. */
6038 const int ctor_or_dtor_p;
6039 } predefined_identifier;
6041 /* Create all the predefined identifiers. */
6043 static void
6044 initialize_predefined_identifiers (void)
6046 const predefined_identifier *pid;
6048 /* A table of identifiers to create at startup. */
6049 static const predefined_identifier predefined_identifiers[] = {
6050 { "C++", &lang_name_cplusplus, 0 },
6051 { "C", &lang_name_c, 0 },
6052 { "Java", &lang_name_java, 0 },
6053 { CTOR_NAME, &ctor_identifier, 1 },
6054 { "__base_ctor", &base_ctor_identifier, 1 },
6055 { "__comp_ctor", &complete_ctor_identifier, 1 },
6056 { DTOR_NAME, &dtor_identifier, 1 },
6057 { "__comp_dtor", &complete_dtor_identifier, 1 },
6058 { "__base_dtor", &base_dtor_identifier, 1 },
6059 { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6060 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6061 { "nelts", &nelts_identifier, 0 },
6062 { THIS_NAME, &this_identifier, 0 },
6063 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6064 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6065 { "_vptr", &vptr_identifier, 0 },
6066 { "__vtt_parm", &vtt_parm_identifier, 0 },
6067 { "::", &global_scope_name, 0 },
6068 { "std", &std_identifier, 0 },
6069 { NULL, NULL, 0 }
6072 for (pid = predefined_identifiers; pid->name; ++pid)
6074 *pid->node = get_identifier (pid->name);
6075 if (pid->ctor_or_dtor_p)
6076 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6080 /* Create the predefined scalar types of C,
6081 and some nodes representing standard constants (0, 1, (void *)0).
6082 Initialize the global binding level.
6083 Make definitions for built-in primitive functions. */
6085 void
6086 cxx_init_decl_processing (void)
6088 tree void_ftype;
6089 tree void_ftype_ptr;
6091 /* Create all the identifiers we need. */
6092 initialize_predefined_identifiers ();
6094 /* Fill in back-end hooks. */
6095 lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6097 /* Create the global variables. */
6098 push_to_top_level ();
6100 current_function_decl = NULL_TREE;
6101 current_binding_level = NULL_BINDING_LEVEL;
6102 free_binding_level = NULL_BINDING_LEVEL;
6103 /* Enter the global namespace. */
6104 my_friendly_assert (global_namespace == NULL_TREE, 375);
6105 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
6106 void_type_node);
6107 initial_push_namespace_scope (global_namespace);
6109 current_lang_name = NULL_TREE;
6111 /* Adjust various flags based on command-line settings. */
6112 if (!flag_permissive)
6113 flag_pedantic_errors = 1;
6114 if (!flag_no_inline)
6116 flag_inline_trees = 1;
6117 flag_no_inline = 1;
6119 if (flag_inline_functions)
6121 flag_inline_trees = 2;
6122 flag_inline_functions = 0;
6125 /* Force minimum function alignment if using the least significant
6126 bit of function pointers to store the virtual bit. */
6127 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6128 && force_align_functions_log < 1)
6129 force_align_functions_log = 1;
6131 /* Initially, C. */
6132 current_lang_name = lang_name_c;
6134 build_common_tree_nodes (flag_signed_char);
6136 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6137 TREE_TYPE (error_mark_list) = error_mark_node;
6139 /* Create the `std' namespace. */
6140 push_namespace (std_identifier);
6141 std_node = current_namespace;
6142 pop_namespace ();
6144 c_common_nodes_and_builtins ();
6146 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6147 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6148 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6149 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6150 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6151 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6152 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6153 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6155 integer_two_node = build_int_2 (2, 0);
6156 TREE_TYPE (integer_two_node) = integer_type_node;
6157 integer_three_node = build_int_2 (3, 0);
6158 TREE_TYPE (integer_three_node) = integer_type_node;
6160 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6161 truthvalue_type_node = boolean_type_node;
6162 truthvalue_false_node = boolean_false_node;
6163 truthvalue_true_node = boolean_true_node;
6165 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6167 #if 0
6168 record_builtin_type (RID_MAX, NULL, string_type_node);
6169 #endif
6171 delta_type_node = ptrdiff_type_node;
6172 vtable_index_type = ptrdiff_type_node;
6174 vtt_parm_type = build_pointer_type (const_ptr_type_node);
6175 void_ftype = build_function_type (void_type_node, void_list_node);
6176 void_ftype_ptr = build_function_type (void_type_node,
6177 tree_cons (NULL_TREE,
6178 ptr_type_node,
6179 void_list_node));
6180 void_ftype_ptr
6181 = build_exception_variant (void_ftype_ptr, empty_except_spec);
6183 /* C++ extensions */
6185 unknown_type_node = make_node (UNKNOWN_TYPE);
6186 record_unknown_type (unknown_type_node, "unknown type");
6188 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6189 TREE_TYPE (unknown_type_node) = unknown_type_node;
6191 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6192 result. */
6193 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6194 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6197 /* Make sure we get a unique function type, so we can give
6198 its pointer type a name. (This wins for gdb.) */
6199 tree vfunc_type = make_node (FUNCTION_TYPE);
6200 TREE_TYPE (vfunc_type) = integer_type_node;
6201 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6202 layout_type (vfunc_type);
6204 vtable_entry_type = build_pointer_type (vfunc_type);
6206 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6208 vtbl_type_node
6209 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6210 layout_type (vtbl_type_node);
6211 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6212 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6213 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6214 layout_type (vtbl_ptr_type_node);
6215 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6217 push_namespace (get_identifier ("__cxxabiv1"));
6218 abi_node = current_namespace;
6219 pop_namespace ();
6221 global_type_node = make_node (LANG_TYPE);
6222 record_unknown_type (global_type_node, "global type");
6224 /* Now, C++. */
6225 current_lang_name = lang_name_cplusplus;
6228 tree bad_alloc_id;
6229 tree bad_alloc_type_node;
6230 tree bad_alloc_decl;
6231 tree newtype, deltype;
6232 tree ptr_ftype_sizetype;
6234 push_namespace (std_identifier);
6235 bad_alloc_id = get_identifier ("bad_alloc");
6236 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
6237 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
6238 bad_alloc_decl
6239 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
6240 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
6241 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
6242 pop_namespace ();
6244 ptr_ftype_sizetype
6245 = build_function_type (ptr_type_node,
6246 tree_cons (NULL_TREE,
6247 size_type_node,
6248 void_list_node));
6249 newtype = build_exception_variant
6250 (ptr_ftype_sizetype, add_exception_specifier
6251 (NULL_TREE, bad_alloc_type_node, -1));
6252 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6253 push_cp_library_fn (NEW_EXPR, newtype);
6254 push_cp_library_fn (VEC_NEW_EXPR, newtype);
6255 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6256 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6259 abort_fndecl
6260 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6262 /* Perform other language dependent initializations. */
6263 init_class_processing ();
6264 init_search_processing ();
6265 init_rtti_processing ();
6267 if (flag_exceptions)
6268 init_exception_processing ();
6270 if (! supports_one_only ())
6271 flag_weak = 0;
6273 make_fname_decl = cp_make_fname_decl;
6274 start_fname_decls ();
6276 /* Show we use EH for cleanups. */
6277 using_eh_for_cleanups ();
6279 /* Maintain consistency. Perhaps we should just complain if they
6280 say -fwritable-strings? */
6281 if (flag_writable_strings)
6282 flag_const_strings = 0;
6285 /* Generate an initializer for a function naming variable from
6286 NAME. NAME may be NULL, in which case we generate a special
6287 ERROR_MARK node which should be replaced later. */
6289 tree
6290 cp_fname_init (const char* name)
6292 tree domain = NULL_TREE;
6293 tree type;
6294 tree init = NULL_TREE;
6295 size_t length = 0;
6297 if (name)
6299 length = strlen (name);
6300 domain = build_index_type (size_int (length));
6301 init = build_string (length + 1, name);
6304 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6305 type = build_cplus_array_type (type, domain);
6307 if (init)
6308 TREE_TYPE (init) = type;
6309 else
6310 /* We don't know the value until instantiation time. Make
6311 something which will be digested now, but replaced later. */
6312 init = build (ERROR_MARK, type);
6314 return init;
6317 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6318 decl, NAME is the initialization string and TYPE_DEP indicates whether
6319 NAME depended on the type of the function. We make use of that to detect
6320 __PRETTY_FUNCTION__ inside a template fn. This is being done
6321 lazily at the point of first use, so we musn't push the decl now. */
6323 static tree
6324 cp_make_fname_decl (tree id, int type_dep)
6326 const char *const name = (type_dep && processing_template_decl
6327 ? NULL : fname_as_string (type_dep));
6328 tree init = cp_fname_init (name);
6329 tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6331 /* As we're using pushdecl_with_scope, we must set the context. */
6332 DECL_CONTEXT (decl) = current_function_decl;
6333 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6335 TREE_STATIC (decl) = 1;
6336 TREE_READONLY (decl) = 1;
6337 DECL_ARTIFICIAL (decl) = 1;
6338 DECL_INITIAL (decl) = init;
6340 TREE_USED (decl) = 1;
6342 if (current_function_decl)
6344 struct cp_binding_level *b = current_binding_level;
6345 while (b->level_chain->kind != sk_function_parms)
6346 b = b->level_chain;
6347 pushdecl_with_scope (decl, b);
6350 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6352 return decl;
6355 /* Make a definition for a builtin function named NAME in the current
6356 namespace, whose data type is TYPE and whose context is CONTEXT.
6357 TYPE should be a function type with argument types.
6359 CLASS and CODE tell later passes how to compile calls to this function.
6360 See tree.h for possible values.
6362 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6363 the name to be called if we can't opencode the function.
6364 If ATTRS is nonzero, use that for the function's attribute
6365 list. */
6367 static tree
6368 builtin_function_1 (const char* name,
6369 tree type,
6370 tree context,
6371 int code,
6372 enum built_in_class class,
6373 const char* libname,
6374 tree attrs)
6376 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6377 DECL_BUILT_IN_CLASS (decl) = class;
6378 DECL_FUNCTION_CODE (decl) = code;
6379 DECL_CONTEXT (decl) = context;
6381 pushdecl (decl);
6383 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6384 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6385 function in the namespace. */
6386 if (libname)
6387 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6388 make_decl_rtl (decl, NULL);
6390 /* Warn if a function in the namespace for users
6391 is used without an occasion to consider it declared. */
6392 if (name[0] != '_' || name[1] != '_')
6393 DECL_ANTICIPATED (decl) = 1;
6395 /* Possibly apply some default attributes to this built-in function. */
6396 if (attrs)
6397 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6398 else
6399 decl_attributes (&decl, NULL_TREE, 0);
6401 return decl;
6404 /* Entry point for the benefit of c_common_nodes_and_builtins.
6406 Make a definition for a builtin function named NAME and whose data type
6407 is TYPE. TYPE should be a function type with argument types. This
6408 function places the anticipated declaration in the global namespace
6409 and additionally in the std namespace if appropriate.
6411 CLASS and CODE tell later passes how to compile calls to this function.
6412 See tree.h for possible values.
6414 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6415 the name to be called if we can't opencode the function.
6417 If ATTRS is nonzero, use that for the function's attribute
6418 list. */
6420 tree
6421 builtin_function (const char* name,
6422 tree type,
6423 int code,
6424 enum built_in_class class,
6425 const char* libname,
6426 tree attrs)
6428 /* All builtins that don't begin with an '_' should additionally
6429 go in the 'std' namespace. */
6430 if (name[0] != '_')
6432 push_namespace (std_identifier);
6433 builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6434 pop_namespace ();
6437 return builtin_function_1 (name, type, NULL_TREE, code,
6438 class, libname, attrs);
6441 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6442 function. Not called directly. */
6444 static tree
6445 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6447 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6448 DECL_EXTERNAL (fn) = 1;
6449 TREE_PUBLIC (fn) = 1;
6450 DECL_ARTIFICIAL (fn) = 1;
6451 TREE_NOTHROW (fn) = 1;
6452 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6453 SET_DECL_LANGUAGE (fn, lang_c);
6454 return fn;
6457 /* Returns the _DECL for a library function with C linkage.
6458 We assume that such functions never throw; if this is incorrect,
6459 callers should unset TREE_NOTHROW. */
6461 tree
6462 build_library_fn (tree name, tree type)
6464 return build_library_fn_1 (name, ERROR_MARK, type);
6467 /* Returns the _DECL for a library function with C++ linkage. */
6469 static tree
6470 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6472 tree fn = build_library_fn_1 (name, operator_code, type);
6473 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6474 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6475 SET_DECL_LANGUAGE (fn, lang_cplusplus);
6476 set_mangled_name_for_decl (fn);
6477 return fn;
6480 /* Like build_library_fn, but takes a C string instead of an
6481 IDENTIFIER_NODE. */
6483 tree
6484 build_library_fn_ptr (const char* name, tree type)
6486 return build_library_fn (get_identifier (name), type);
6489 /* Like build_cp_library_fn, but takes a C string instead of an
6490 IDENTIFIER_NODE. */
6492 tree
6493 build_cp_library_fn_ptr (const char* name, tree type)
6495 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6498 /* Like build_library_fn, but also pushes the function so that we will
6499 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
6501 tree
6502 push_library_fn (tree name, tree type)
6504 tree fn = build_library_fn (name, type);
6505 pushdecl_top_level (fn);
6506 return fn;
6509 /* Like build_cp_library_fn, but also pushes the function so that it
6510 will be found by normal lookup. */
6512 static tree
6513 push_cp_library_fn (enum tree_code operator_code, tree type)
6515 tree fn = build_cp_library_fn (ansi_opname (operator_code),
6516 operator_code,
6517 type);
6518 pushdecl (fn);
6519 return fn;
6522 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6523 a FUNCTION_TYPE. */
6525 tree
6526 push_void_library_fn (tree name, tree parmtypes)
6528 tree type = build_function_type (void_type_node, parmtypes);
6529 return push_library_fn (name, type);
6532 /* Like push_library_fn, but also note that this function throws
6533 and does not return. Used for __throw_foo and the like. */
6535 tree
6536 push_throw_library_fn (tree name, tree type)
6538 tree fn = push_library_fn (name, type);
6539 TREE_THIS_VOLATILE (fn) = 1;
6540 TREE_NOTHROW (fn) = 0;
6541 return fn;
6544 /* When we call finish_struct for an anonymous union, we create
6545 default copy constructors and such. But, an anonymous union
6546 shouldn't have such things; this function undoes the damage to the
6547 anonymous union type T.
6549 (The reason that we create the synthesized methods is that we don't
6550 distinguish `union { int i; }' from `typedef union { int i; } U'.
6551 The first is an anonymous union; the second is just an ordinary
6552 union type.) */
6554 void
6555 fixup_anonymous_aggr (tree t)
6557 tree *q;
6559 /* Wipe out memory of synthesized methods */
6560 TYPE_HAS_CONSTRUCTOR (t) = 0;
6561 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6562 TYPE_HAS_INIT_REF (t) = 0;
6563 TYPE_HAS_CONST_INIT_REF (t) = 0;
6564 TYPE_HAS_ASSIGN_REF (t) = 0;
6565 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6567 /* Splice the implicitly generated functions out of the TYPE_METHODS
6568 list. */
6569 q = &TYPE_METHODS (t);
6570 while (*q)
6572 if (DECL_ARTIFICIAL (*q))
6573 *q = TREE_CHAIN (*q);
6574 else
6575 q = &TREE_CHAIN (*q);
6578 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
6579 if (TYPE_METHODS (t))
6580 error ("%Han anonymous union cannot have function members",
6581 &DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)));
6583 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6584 assignment operators (because they cannot have these methods themselves).
6585 For anonymous unions this is already checked because they are not allowed
6586 in any union, otherwise we have to check it. */
6587 if (TREE_CODE (t) != UNION_TYPE)
6589 tree field, type;
6591 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6592 if (TREE_CODE (field) == FIELD_DECL)
6594 type = TREE_TYPE (field);
6595 if (CLASS_TYPE_P (type))
6597 if (TYPE_NEEDS_CONSTRUCTING (type))
6598 cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6599 field);
6600 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6601 cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6602 field);
6603 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6604 cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6605 field);
6611 /* Make sure that a declaration with no declarator is well-formed, i.e.
6612 just declares a tagged type or anonymous union.
6614 Returns the type declared; or NULL_TREE if none. */
6616 tree
6617 check_tag_decl (tree declspecs)
6619 int found_type = 0;
6620 int saw_friend = 0;
6621 int saw_typedef = 0;
6622 tree ob_modifier = NULL_TREE;
6623 register tree link;
6624 /* If a class, struct, or enum type is declared by the DECLSPECS
6625 (i.e, if a class-specifier, enum-specifier, or non-typename
6626 elaborated-type-specifier appears in the DECLSPECS),
6627 DECLARED_TYPE is set to the corresponding type. */
6628 tree declared_type = NULL_TREE;
6629 bool error_p = false;
6631 for (link = declspecs; link; link = TREE_CHAIN (link))
6633 tree value = TREE_VALUE (link);
6635 if (TYPE_P (value)
6636 || TREE_CODE (value) == TYPE_DECL
6637 || (TREE_CODE (value) == IDENTIFIER_NODE
6638 && IDENTIFIER_GLOBAL_VALUE (value)
6639 && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6641 ++found_type;
6643 if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6645 if (! in_system_header)
6646 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6647 return NULL_TREE;
6650 if (TYPE_P (value)
6651 && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6652 || TREE_CODE (value) == ENUMERAL_TYPE))
6654 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6655 declared_type = value;
6658 else if (value == ridpointers[(int) RID_TYPEDEF])
6659 saw_typedef = 1;
6660 else if (value == ridpointers[(int) RID_FRIEND])
6662 if (current_class_type == NULL_TREE
6663 || current_scope () != current_class_type)
6664 ob_modifier = value;
6665 else
6666 saw_friend = 1;
6668 else if (value == ridpointers[(int) RID_STATIC]
6669 || value == ridpointers[(int) RID_EXTERN]
6670 || value == ridpointers[(int) RID_AUTO]
6671 || value == ridpointers[(int) RID_REGISTER]
6672 || value == ridpointers[(int) RID_INLINE]
6673 || value == ridpointers[(int) RID_VIRTUAL]
6674 || value == ridpointers[(int) RID_CONST]
6675 || value == ridpointers[(int) RID_VOLATILE]
6676 || value == ridpointers[(int) RID_EXPLICIT]
6677 || value == ridpointers[(int) RID_THREAD])
6678 ob_modifier = value;
6679 else if (value == error_mark_node)
6680 error_p = true;
6683 if (found_type > 1)
6684 error ("multiple types in one declaration");
6686 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6687 pedwarn ("declaration does not declare anything");
6688 /* Check for an anonymous union. */
6689 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6690 && TYPE_ANONYMOUS_P (declared_type))
6692 /* 7/3 In a simple-declaration, the optional init-declarator-list
6693 can be omitted only when declaring a class (clause 9) or
6694 enumeration (7.2), that is, when the decl-specifier-seq contains
6695 either a class-specifier, an elaborated-type-specifier with
6696 a class-key (9.1), or an enum-specifier. In these cases and
6697 whenever a class-specifier or enum-specifier is present in the
6698 decl-specifier-seq, the identifiers in these specifiers are among
6699 the names being declared by the declaration (as class-name,
6700 enum-names, or enumerators, depending on the syntax). In such
6701 cases, and except for the declaration of an unnamed bit-field (9.6),
6702 the decl-specifier-seq shall introduce one or more names into the
6703 program, or shall redeclare a name introduced by a previous
6704 declaration. [Example:
6705 enum { }; // ill-formed
6706 typedef class { }; // ill-formed
6707 --end example] */
6708 if (saw_typedef)
6710 error ("missing type-name in typedef-declaration");
6711 return NULL_TREE;
6713 /* Anonymous unions are objects, so they can have specifiers. */;
6714 SET_ANON_AGGR_TYPE_P (declared_type);
6716 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
6717 && !in_system_header)
6718 pedwarn ("ISO C++ prohibits anonymous structs");
6721 else if (ob_modifier)
6723 if (ob_modifier == ridpointers[(int) RID_INLINE]
6724 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6725 error ("`%D' can only be specified for functions", ob_modifier);
6726 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6727 error ("`%D' can only be specified inside a class", ob_modifier);
6728 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6729 error ("`%D' can only be specified for constructors",
6730 ob_modifier);
6731 else
6732 error ("`%D' can only be specified for objects and functions",
6733 ob_modifier);
6736 return declared_type;
6739 /* Called when a declaration is seen that contains no names to declare.
6740 If its type is a reference to a structure, union or enum inherited
6741 from a containing scope, shadow that tag name for the current scope
6742 with a forward reference.
6743 If its type defines a new named structure or union
6744 or defines an enum, it is valid but we need not do anything here.
6745 Otherwise, it is an error.
6747 C++: may have to grok the declspecs to learn about static,
6748 complain for anonymous unions.
6750 Returns the TYPE declared -- or NULL_TREE if none. */
6752 tree
6753 shadow_tag (tree declspecs)
6755 tree t = check_tag_decl (declspecs);
6757 if (!t)
6758 return NULL_TREE;
6760 maybe_process_partial_specialization (t);
6762 /* This is where the variables in an anonymous union are
6763 declared. An anonymous union declaration looks like:
6764 union { ... } ;
6765 because there is no declarator after the union, the parser
6766 sends that declaration here. */
6767 if (ANON_AGGR_TYPE_P (t))
6769 fixup_anonymous_aggr (t);
6771 if (TYPE_FIELDS (t))
6773 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6774 NULL);
6775 finish_anon_union (decl);
6779 return t;
6782 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6784 tree
6785 groktypename (tree typename)
6787 tree specs, attrs;
6788 tree type;
6789 if (TREE_CODE (typename) != TREE_LIST)
6790 return typename;
6791 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6792 type = grokdeclarator (TREE_VALUE (typename), specs,
6793 TYPENAME, 0, &attrs);
6794 if (attrs)
6795 cplus_decl_attributes (&type, attrs, 0);
6796 return type;
6799 /* Decode a declarator in an ordinary declaration or data definition.
6800 This is called as soon as the type information and variable name
6801 have been parsed, before parsing the initializer if any.
6802 Here we create the ..._DECL node, fill in its type,
6803 and put it on the list of decls for the current context.
6804 The ..._DECL node is returned as the value.
6806 Exception: for arrays where the length is not specified,
6807 the type is left null, to be filled in by `cp_finish_decl'.
6809 Function definitions do not come here; they go to start_function
6810 instead. However, external and forward declarations of functions
6811 do go through here. Structure field declarations are done by
6812 grokfield and not through here. */
6814 tree
6815 start_decl (tree declarator,
6816 tree declspecs,
6817 int initialized,
6818 tree attributes,
6819 tree prefix_attributes)
6821 tree decl;
6822 register tree type, tem;
6823 tree context;
6825 /* This should only be done once on the top most decl. */
6826 if (have_extern_spec)
6828 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
6829 declspecs);
6830 have_extern_spec = false;
6833 /* An object declared as __attribute__((deprecated)) suppresses
6834 warnings of uses of other deprecated items. */
6835 if (lookup_attribute ("deprecated", attributes))
6836 deprecated_state = DEPRECATED_SUPPRESS;
6838 attributes = chainon (attributes, prefix_attributes);
6840 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6841 &attributes);
6843 deprecated_state = DEPRECATED_NORMAL;
6845 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
6846 return NULL_TREE;
6848 type = TREE_TYPE (decl);
6850 if (type == error_mark_node)
6851 return NULL_TREE;
6853 context = DECL_CONTEXT (decl);
6855 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6856 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6858 /* When parsing the initializer, lookup should use the object's
6859 namespace. */
6860 push_decl_namespace (context);
6863 /* We are only interested in class contexts, later. */
6864 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6865 context = NULL_TREE;
6867 if (initialized)
6868 /* Is it valid for this decl to have an initializer at all?
6869 If not, set INITIALIZED to zero, which will indirectly
6870 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
6871 switch (TREE_CODE (decl))
6873 case TYPE_DECL:
6874 error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
6875 initialized = 0;
6876 break;
6878 case FUNCTION_DECL:
6879 error ("function `%#D' is initialized like a variable", decl);
6880 initialized = 0;
6881 break;
6883 default:
6884 break;
6887 if (initialized)
6889 if (! toplevel_bindings_p ()
6890 && DECL_EXTERNAL (decl))
6891 warning ("declaration of `%#D' has `extern' and is initialized",
6892 decl);
6893 DECL_EXTERNAL (decl) = 0;
6894 if (toplevel_bindings_p ())
6895 TREE_STATIC (decl) = 1;
6897 /* Tell `pushdecl' this is an initialized decl
6898 even though we don't yet have the initializer expression.
6899 Also tell `cp_finish_decl' it may store the real initializer. */
6900 DECL_INITIAL (decl) = error_mark_node;
6903 /* Set attributes here so if duplicate decl, will have proper attributes. */
6904 cplus_decl_attributes (&decl, attributes, 0);
6906 /* If #pragma weak was used, mark the decl weak now. */
6907 if (global_scope_p (current_binding_level))
6908 maybe_apply_pragma_weak (decl);
6910 if (TREE_CODE (decl) == FUNCTION_DECL
6911 && DECL_DECLARED_INLINE_P (decl)
6912 && DECL_UNINLINABLE (decl)
6913 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6914 warning ("%Hinline function '%D' given attribute noinline",
6915 &DECL_SOURCE_LOCATION (decl), decl);
6917 if (context && COMPLETE_TYPE_P (complete_type (context)))
6919 push_nested_class (context);
6921 if (TREE_CODE (decl) == VAR_DECL)
6923 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6924 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
6925 error ("`%#D' is not a static member of `%#T'", decl, context);
6926 else
6928 if (DECL_CONTEXT (field) != context)
6930 if (!same_type_p (DECL_CONTEXT (field), context))
6931 pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
6932 DECL_CONTEXT (field), DECL_NAME (decl),
6933 context, DECL_NAME (decl));
6934 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6936 /* Static data member are tricky; an in-class initialization
6937 still doesn't provide a definition, so the in-class
6938 declaration will have DECL_EXTERNAL set, but will have an
6939 initialization. Thus, duplicate_decls won't warn
6940 about this situation, and so we check here. */
6941 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
6942 error ("duplicate initialization of %D", decl);
6943 if (duplicate_decls (decl, field))
6944 decl = field;
6947 else
6949 tree field = check_classfn (context, decl);
6950 if (field && duplicate_decls (decl, field))
6951 decl = field;
6954 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6955 DECL_IN_AGGR_P (decl) = 0;
6956 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6957 || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
6959 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6960 /* [temp.expl.spec] An explicit specialization of a static data
6961 member of a template is a definition if the declaration
6962 includes an initializer; otherwise, it is a declaration.
6964 We check for processing_specialization so this only applies
6965 to the new specialization syntax. */
6966 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
6967 DECL_EXTERNAL (decl) = 1;
6970 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
6971 pedwarn ("declaration of `%#D' outside of class is not definition",
6972 decl);
6975 /* Enter this declaration into the symbol table. */
6976 tem = maybe_push_decl (decl);
6978 if (processing_template_decl)
6979 tem = push_template_decl (tem);
6981 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
6982 /* Tell the back-end to use or not use .common as appropriate. If we say
6983 -fconserve-space, we want this to save .data space, at the expense of
6984 wrong semantics. If we say -fno-conserve-space, we want this to
6985 produce errors about redefs; to do this we force variables into the
6986 data segment. */
6987 DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
6988 || !DECL_THREAD_LOCAL (tem))
6989 && (flag_conserve_space || ! TREE_PUBLIC (tem)));
6990 #endif
6992 if (! processing_template_decl)
6993 start_decl_1 (tem);
6995 return tem;
6998 void
6999 start_decl_1 (tree decl)
7001 tree type = TREE_TYPE (decl);
7002 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7004 if (type == error_mark_node)
7005 return;
7007 maybe_push_cleanup_level (type);
7009 if (initialized)
7010 /* Is it valid for this decl to have an initializer at all?
7011 If not, set INITIALIZED to zero, which will indirectly
7012 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
7014 /* Don't allow initializations for incomplete types except for
7015 arrays which might be completed by the initialization. */
7016 if (COMPLETE_TYPE_P (complete_type (type)))
7017 ; /* A complete type is ok. */
7018 else if (TREE_CODE (type) != ARRAY_TYPE)
7020 error ("variable `%#D' has initializer but incomplete type",
7021 decl);
7022 initialized = 0;
7023 type = TREE_TYPE (decl) = error_mark_node;
7025 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7027 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7028 error ("elements of array `%#D' have incomplete type", decl);
7029 /* else we already gave an error in start_decl. */
7030 initialized = 0;
7034 if (!initialized
7035 && TREE_CODE (decl) != TYPE_DECL
7036 && TREE_CODE (decl) != TEMPLATE_DECL
7037 && type != error_mark_node
7038 && IS_AGGR_TYPE (type)
7039 && ! DECL_EXTERNAL (decl))
7041 if ((! processing_template_decl || ! uses_template_parms (type))
7042 && !COMPLETE_TYPE_P (complete_type (type)))
7044 error ("aggregate `%#D' has incomplete type and cannot be defined",
7045 decl);
7046 /* Change the type so that assemble_variable will give
7047 DECL an rtl we can live with: (mem (const_int 0)). */
7048 type = TREE_TYPE (decl) = error_mark_node;
7050 else
7052 /* If any base type in the hierarchy of TYPE needs a constructor,
7053 then we set initialized to 1. This way any nodes which are
7054 created for the purposes of initializing this aggregate
7055 will live as long as it does. This is necessary for global
7056 aggregates which do not have their initializers processed until
7057 the end of the file. */
7058 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7062 if (! initialized)
7063 DECL_INITIAL (decl) = NULL_TREE;
7066 /* Handle initialization of references. DECL, TYPE, and INIT have the
7067 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
7068 but will be set to a new CLEANUP_STMT if a temporary is created
7069 that must be destroeyd subsequently.
7071 Returns an initializer expression to use to initialize DECL, or
7072 NULL if the initialization can be performed statically.
7074 Quotes on semantics can be found in ARM 8.4.3. */
7076 static tree
7077 grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
7079 tree tmp;
7081 if (init == NULL_TREE)
7083 if ((DECL_LANG_SPECIFIC (decl) == 0
7084 || DECL_IN_AGGR_P (decl) == 0)
7085 && ! DECL_THIS_EXTERN (decl))
7086 error ("`%D' declared as reference but not initialized", decl);
7087 return NULL_TREE;
7090 if (TREE_CODE (init) == CONSTRUCTOR)
7092 error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7093 return NULL_TREE;
7096 if (TREE_CODE (init) == TREE_LIST)
7097 init = build_x_compound_expr_from_list (init, "initializer");
7099 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7100 init = convert_from_reference (init);
7102 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7103 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7104 /* Note: default conversion is only called in very special cases. */
7105 init = decay_conversion (init);
7107 /* Convert INIT to the reference type TYPE. This may involve the
7108 creation of a temporary, whose lifetime must be the same as that
7109 of the reference. If so, a DECL_STMT for the temporary will be
7110 added just after the DECL_STMT for DECL. That's why we don't set
7111 DECL_INITIAL for local references (instead assigning to them
7112 explicitly); we need to allow the temporary to be initialized
7113 first. */
7114 tmp = initialize_reference (type, init, decl, cleanup);
7116 if (tmp == error_mark_node)
7117 return NULL_TREE;
7118 else if (tmp == NULL_TREE)
7120 error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7121 return NULL_TREE;
7124 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7125 return tmp;
7127 DECL_INITIAL (decl) = tmp;
7129 return NULL_TREE;
7132 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7133 array until we finish parsing the initializer. If that's the
7134 situation we're in, update DECL accordingly. */
7136 static void
7137 maybe_deduce_size_from_array_init (tree decl, tree init)
7139 tree type = TREE_TYPE (decl);
7141 if (TREE_CODE (type) == ARRAY_TYPE
7142 && TYPE_DOMAIN (type) == NULL_TREE
7143 && TREE_CODE (decl) != TYPE_DECL)
7145 /* do_default is really a C-ism to deal with tentative definitions.
7146 But let's leave it here to ease the eventual merge. */
7147 int do_default = !DECL_EXTERNAL (decl);
7148 tree initializer = init ? init : DECL_INITIAL (decl);
7149 int failure = complete_array_type (type, initializer, do_default);
7151 if (failure == 1)
7152 error ("initializer fails to determine size of `%D'", decl);
7154 if (failure == 2)
7156 if (do_default)
7157 error ("array size missing in `%D'", decl);
7158 /* If a `static' var's size isn't known, make it extern as
7159 well as static, so it does not get allocated. If it's not
7160 `static', then don't mark it extern; finish_incomplete_decl
7161 will give it a default size and it will get allocated. */
7162 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7163 DECL_EXTERNAL (decl) = 1;
7166 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7167 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7168 integer_zero_node))
7169 error ("zero-size array `%D'", decl);
7171 layout_decl (decl, 0);
7175 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7176 any appropriate error messages regarding the layout. */
7178 static void
7179 layout_var_decl (tree decl)
7181 tree type = TREE_TYPE (decl);
7182 #if 0
7183 tree ttype = target_type (type);
7184 #endif
7186 /* If we haven't already layed out this declaration, do so now.
7187 Note that we must not call complete type for an external object
7188 because it's type might involve templates that we are not
7189 supposed to instantiate yet. (And it's perfectly valid to say
7190 `extern X x' for some incomplete type `X'.) */
7191 if (!DECL_EXTERNAL (decl))
7192 complete_type (type);
7193 if (!DECL_SIZE (decl)
7194 && TREE_TYPE (decl) != error_mark_node
7195 && (COMPLETE_TYPE_P (type)
7196 || (TREE_CODE (type) == ARRAY_TYPE
7197 && !TYPE_DOMAIN (type)
7198 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7199 layout_decl (decl, 0);
7201 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7203 /* An automatic variable with an incomplete type: that is an error.
7204 Don't talk about array types here, since we took care of that
7205 message in grokdeclarator. */
7206 error ("storage size of `%D' isn't known", decl);
7207 TREE_TYPE (decl) = error_mark_node;
7209 #if 0
7210 /* Keep this code around in case we later want to control debug info
7211 based on whether a type is "used". (jason 1999-11-11) */
7213 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7214 /* Let debugger know it should output info for this type. */
7215 note_debug_info_needed (ttype);
7217 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7218 note_debug_info_needed (DECL_CONTEXT (decl));
7219 #endif
7221 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7222 && DECL_SIZE (decl) != NULL_TREE
7223 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7225 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7226 constant_expression_warning (DECL_SIZE (decl));
7227 else
7228 error ("storage size of `%D' isn't constant", decl);
7231 if (TREE_STATIC (decl)
7232 && !DECL_ARTIFICIAL (decl)
7233 && current_function_decl
7234 && DECL_CONTEXT (decl) == current_function_decl)
7235 push_local_name (decl);
7238 /* If a local static variable is declared in an inline function, or if
7239 we have a weak definition, we must endeavor to create only one
7240 instance of the variable at link-time. */
7242 static void
7243 maybe_commonize_var (tree decl)
7245 /* Static data in a function with comdat linkage also has comdat
7246 linkage. */
7247 if (TREE_STATIC (decl)
7248 /* Don't mess with __FUNCTION__. */
7249 && ! DECL_ARTIFICIAL (decl)
7250 && DECL_FUNCTION_SCOPE_P (decl)
7251 /* Unfortunately, import_export_decl has not always been called
7252 before the function is processed, so we cannot simply check
7253 DECL_COMDAT. */
7254 && (DECL_COMDAT (DECL_CONTEXT (decl))
7255 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
7256 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
7257 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
7259 if (flag_weak)
7261 /* With weak symbols, we simply make the variable COMDAT;
7262 that will cause copies in multiple translations units to
7263 be merged. */
7264 comdat_linkage (decl);
7266 else
7268 if (DECL_INITIAL (decl) == NULL_TREE
7269 || DECL_INITIAL (decl) == error_mark_node)
7271 /* Without weak symbols, we can use COMMON to merge
7272 uninitialized variables. */
7273 TREE_PUBLIC (decl) = 1;
7274 DECL_COMMON (decl) = 1;
7276 else
7278 /* While for initialized variables, we must use internal
7279 linkage -- which means that multiple copies will not
7280 be merged. */
7281 TREE_PUBLIC (decl) = 0;
7282 DECL_COMMON (decl) = 0;
7283 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7284 warning ("%H you can work around this by removing the initializer",
7285 &DECL_SOURCE_LOCATION (decl));
7289 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7290 /* Set it up again; we might have set DECL_INITIAL since the last
7291 time. */
7292 comdat_linkage (decl);
7295 /* Issue an error message if DECL is an uninitialized const variable. */
7297 static void
7298 check_for_uninitialized_const_var (tree decl)
7300 tree type = TREE_TYPE (decl);
7302 /* ``Unless explicitly declared extern, a const object does not have
7303 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7304 7.1.6 */
7305 if (TREE_CODE (decl) == VAR_DECL
7306 && TREE_CODE (type) != REFERENCE_TYPE
7307 && CP_TYPE_CONST_P (type)
7308 && !TYPE_NEEDS_CONSTRUCTING (type)
7309 && !DECL_INITIAL (decl))
7310 error ("uninitialized const `%D'", decl);
7313 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
7314 returned is the next FIELD_DECL (possibly FIELD itself) that can be
7315 initialized. If there are no more such fields, the return value
7316 will be NULL. */
7318 static tree
7319 next_initializable_field (tree field)
7321 while (field
7322 && (TREE_CODE (field) != FIELD_DECL
7323 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7324 || DECL_ARTIFICIAL (field)))
7325 field = TREE_CHAIN (field);
7327 return field;
7330 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7331 brace-enclosed aggregate initializer.
7333 *INITP is one of a list of initializers describing a brace-enclosed
7334 initializer for an entity of the indicated aggregate TYPE. It may
7335 not presently match the shape of the TYPE; for example:
7337 struct S { int a; int b; };
7338 struct S a[] = { 1, 2, 3, 4 };
7340 Here *INITP will point to TREE_LIST of four elements, rather than a
7341 list of two elements, each itself a list of two elements. This
7342 routine transforms INIT from the former form into the latter. The
7343 revised initializer is returned. */
7345 static tree
7346 reshape_init (tree type, tree *initp)
7348 tree inits;
7349 tree old_init;
7350 tree old_init_value;
7351 tree new_init;
7352 bool brace_enclosed_p;
7354 old_init = *initp;
7355 old_init_value = (TREE_CODE (*initp) == TREE_LIST
7356 ? TREE_VALUE (*initp) : old_init);
7358 my_friendly_assert (old_init_value, 20030723);
7360 /* If the initializer is brace-enclosed, pull initializers from the
7361 enclosed elements. Advance past the brace-enclosed initializer
7362 now. */
7363 if (TREE_CODE (old_init_value) == CONSTRUCTOR
7364 && TREE_TYPE (old_init_value) == NULL_TREE
7365 && TREE_HAS_CONSTRUCTOR (old_init_value))
7367 *initp = TREE_CHAIN (old_init);
7368 TREE_CHAIN (old_init) = NULL_TREE;
7369 inits = CONSTRUCTOR_ELTS (old_init_value);
7370 initp = &inits;
7371 brace_enclosed_p = true;
7373 else
7375 inits = NULL_TREE;
7376 brace_enclosed_p = false;
7379 /* A non-aggregate type is always initialized with a single
7380 initializer. */
7381 if (!CP_AGGREGATE_TYPE_P (type))
7383 *initp = TREE_CHAIN (old_init);
7384 TREE_CHAIN (old_init) = NULL_TREE;
7385 /* It is invalid to initialize a non-aggregate type with a
7386 brace-enclosed initializer. */
7387 if (brace_enclosed_p)
7389 error ("brace-enclosed initializer used to initialize `%T'",
7390 type);
7391 if (TREE_CODE (old_init) == TREE_LIST)
7392 TREE_VALUE (old_init) = error_mark_node;
7393 else
7394 old_init = error_mark_node;
7397 return old_init;
7400 /* [dcl.init.aggr]
7402 All implicit type conversions (clause _conv_) are considered when
7403 initializing the aggregate member with an initializer from an
7404 initializer-list. If the initializer can initialize a member,
7405 the member is initialized. Otherwise, if the member is itself a
7406 non-empty subaggregate, brace elision is assumed and the
7407 initializer is considered for the initialization of the first
7408 member of the subaggregate. */
7409 if (!brace_enclosed_p
7410 && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7412 *initp = TREE_CHAIN (old_init);
7413 TREE_CHAIN (old_init) = NULL_TREE;
7414 return old_init;
7417 if (TREE_CODE (old_init_value) == STRING_CST
7418 && TREE_CODE (type) == ARRAY_TYPE
7419 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7421 /* [dcl.init.string]
7423 A char array (whether plain char, signed char, or unsigned char)
7424 can be initialized by a string-literal (optionally enclosed in
7425 braces); a wchar_t array can be initialized by a wide
7426 string-literal (optionally enclosed in braces). */
7427 new_init = old_init;
7428 /* Move past the initializer. */
7429 *initp = TREE_CHAIN (old_init);
7430 TREE_CHAIN (old_init) = NULL_TREE;
7432 else
7434 /* Build a CONSTRUCTOR to hold the contents of the aggregate. */
7435 new_init = build_constructor (type, NULL_TREE);
7436 TREE_HAS_CONSTRUCTOR (new_init) = 1;
7438 if (CLASS_TYPE_P (type))
7440 tree field;
7442 field = next_initializable_field (TYPE_FIELDS (type));
7444 if (!field)
7446 /* [dcl.init.aggr]
7448 An initializer for an aggregate member that is an
7449 empty class shall have the form of an empty
7450 initializer-list {}. */
7451 if (!brace_enclosed_p)
7452 error ("initializer for `%T' must be brace-enclosed",
7453 type);
7455 else
7457 /* Loop through the initializable fields, gathering
7458 initializers. */
7459 while (*initp)
7461 tree field_init;
7463 /* Handle designated initializers, as an extension. */
7464 if (TREE_PURPOSE (*initp))
7466 if (pedantic)
7467 pedwarn ("ISO C++ does not allow designated initializers");
7468 field = lookup_field_1 (type, TREE_PURPOSE (*initp),
7469 /*want_type=*/false);
7470 if (!field || TREE_CODE (field) != FIELD_DECL)
7471 error ("`%T' has no non-static data member named `%D'",
7472 type, TREE_PURPOSE (*initp));
7474 if (!field)
7475 break;
7477 field_init = reshape_init (TREE_TYPE (field), initp);
7478 TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7479 CONSTRUCTOR_ELTS (new_init) = field_init;
7480 /* [dcl.init.aggr]
7482 When a union is initialized with a brace-enclosed
7483 initializer, the braces shall only contain an
7484 initializer for the first member of the union. */
7485 if (TREE_CODE (type) == UNION_TYPE)
7486 break;
7487 field = next_initializable_field (TREE_CHAIN (field));
7491 else if (TREE_CODE (type) == ARRAY_TYPE)
7493 tree index;
7494 tree max_index;
7496 /* If the bound of the array is known, take no more initializers
7497 than are allowed. */
7498 max_index = (TYPE_DOMAIN (type)
7499 ? array_type_nelts (type) : NULL_TREE);
7500 /* Loop through the array elements, gathering initializers. */
7501 for (index = size_zero_node;
7502 *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7503 index = size_binop (PLUS_EXPR, index, size_one_node))
7505 tree element_init;
7507 element_init = reshape_init (TREE_TYPE (type), initp);
7508 TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7509 CONSTRUCTOR_ELTS (new_init) = element_init;
7510 if (TREE_PURPOSE (element_init))
7511 index = TREE_PURPOSE (element_init);
7514 else
7515 abort ();
7517 /* The initializers were placed in reverse order in the
7518 CONSTRUCTOR. */
7519 CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7521 if (TREE_CODE (old_init) == TREE_LIST)
7522 new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7525 /* If this was a brace-enclosed initializer and all of the
7526 initializers were not used up, there is a problem. */
7527 if (brace_enclosed_p && *initp)
7528 error ("too many initializers for `%T'", type);
7530 return new_init;
7533 /* Verify INIT (the initializer for DECL), and record the
7534 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7535 grok_reference_init.
7537 If the return value is non-NULL, it is an expression that must be
7538 evaluated dynamically to initialize DECL. */
7540 static tree
7541 check_initializer (tree decl, tree init, int flags, tree *cleanup)
7543 tree type = TREE_TYPE (decl);
7545 /* If `start_decl' didn't like having an initialization, ignore it now. */
7546 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7547 init = NULL_TREE;
7549 /* If an initializer is present, DECL_INITIAL has been
7550 error_mark_node, to indicate that an as-of-yet unevaluated
7551 initialization will occur. From now on, DECL_INITIAL reflects
7552 the static initialization -- if any -- of DECL. */
7553 DECL_INITIAL (decl) = NULL_TREE;
7555 /* Things that are going to be initialized need to have complete
7556 type. */
7557 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7559 if (type == error_mark_node)
7560 /* We will have already complained. */
7561 init = NULL_TREE;
7562 else if (init && COMPLETE_TYPE_P (type)
7563 && !TREE_CONSTANT (TYPE_SIZE (type)))
7565 error ("variable-sized object `%D' may not be initialized", decl);
7566 init = NULL_TREE;
7568 else if (TREE_CODE (type) == ARRAY_TYPE
7569 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7571 error ("elements of array `%#D' have incomplete type", decl);
7572 init = NULL_TREE;
7574 else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7576 error ("`%D' has incomplete type", decl);
7577 TREE_TYPE (decl) = error_mark_node;
7578 init = NULL_TREE;
7581 if (TREE_CODE (decl) == CONST_DECL)
7583 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7585 DECL_INITIAL (decl) = init;
7587 my_friendly_assert (init != NULL_TREE, 149);
7588 init = NULL_TREE;
7590 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7591 init = grok_reference_init (decl, type, init, cleanup);
7592 else if (init)
7594 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7596 /* [dcl.init] paragraph 13,
7597 If T is a scalar type, then a declaration of the form
7598 T x = { a };
7599 is equivalent to
7600 T x = a;
7602 reshape_init will complain about the extra braces,
7603 and doesn't do anything useful in the case where TYPE is
7604 scalar, so just don't call it. */
7605 if (CP_AGGREGATE_TYPE_P (type))
7606 init = reshape_init (type, &init);
7608 if ((*targetm.vector_opaque_p) (type))
7610 error ("opaque vector types cannot be initialized");
7611 init = error_mark_node;
7615 /* If DECL has an array type without a specific bound, deduce the
7616 array size from the initializer. */
7617 maybe_deduce_size_from_array_init (decl, init);
7618 type = TREE_TYPE (decl);
7619 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7620 TREE_TYPE (init) = type;
7622 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7624 if (TREE_CODE (type) == ARRAY_TYPE)
7625 goto initialize_aggr;
7626 else if (TREE_CODE (init) == CONSTRUCTOR
7627 && TREE_HAS_CONSTRUCTOR (init))
7629 if (TYPE_NON_AGGREGATE_CLASS (type))
7631 error ("`%D' must be initialized by constructor, not by `{...}'",
7632 decl);
7633 init = error_mark_node;
7635 else
7636 goto dont_use_constructor;
7638 else
7640 int saved_stmts_are_full_exprs_p;
7642 initialize_aggr:
7643 saved_stmts_are_full_exprs_p = 0;
7644 if (building_stmt_tree ())
7646 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7647 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7649 init = build_aggr_init (decl, init, flags);
7650 if (building_stmt_tree ())
7651 current_stmt_tree ()->stmts_are_full_exprs_p =
7652 saved_stmts_are_full_exprs_p;
7653 return init;
7656 else
7658 dont_use_constructor:
7659 if (TREE_CODE (init) != TREE_VEC)
7660 init = store_init_value (decl, init);
7663 else if (DECL_EXTERNAL (decl))
7665 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7666 goto initialize_aggr;
7667 else if (IS_AGGR_TYPE (type))
7669 tree core_type = strip_array_types (type);
7671 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7672 error ("structure `%D' with uninitialized const members", decl);
7673 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7674 error ("structure `%D' with uninitialized reference members",
7675 decl);
7677 check_for_uninitialized_const_var (decl);
7679 else
7680 check_for_uninitialized_const_var (decl);
7682 if (init && init != error_mark_node)
7683 init = build (INIT_EXPR, type, decl, init);
7685 return init;
7688 /* If DECL is not a local variable, give it RTL. */
7690 static void
7691 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7693 int toplev = toplevel_bindings_p ();
7694 int defer_p;
7696 /* Handle non-variables up front. */
7697 if (TREE_CODE (decl) != VAR_DECL)
7699 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7700 return;
7703 /* If we see a class member here, it should be a static data
7704 member. */
7705 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7707 my_friendly_assert (TREE_STATIC (decl), 19990828);
7708 /* An in-class declaration of a static data member should be
7709 external; it is only a declaration, and not a definition. */
7710 if (init == NULL_TREE)
7711 my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7714 /* Set the DECL_ASSEMBLER_NAME for the variable. */
7715 if (asmspec)
7717 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7718 /* The `register' keyword, when used together with an
7719 asm-specification, indicates that the variable should be
7720 placed in a particular register. */
7721 if (DECL_REGISTER (decl))
7722 DECL_C_HARD_REGISTER (decl) = 1;
7725 /* We don't create any RTL for local variables. */
7726 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7727 return;
7729 /* We defer emission of local statics until the corresponding
7730 DECL_STMT is expanded. */
7731 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7733 /* We try to defer namespace-scope static constants so that they are
7734 not emitted into the object file unnecessarily. */
7735 if (!DECL_VIRTUAL_P (decl)
7736 && TREE_READONLY (decl)
7737 && DECL_INITIAL (decl) != NULL_TREE
7738 && DECL_INITIAL (decl) != error_mark_node
7739 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7740 && toplev
7741 && !TREE_PUBLIC (decl))
7743 /* Fool with the linkage of static consts according to #pragma
7744 interface. */
7745 if (!interface_unknown && !TREE_PUBLIC (decl))
7747 TREE_PUBLIC (decl) = 1;
7748 DECL_EXTERNAL (decl) = interface_only;
7751 defer_p = 1;
7753 /* Likewise for template instantiations. */
7754 else if (DECL_COMDAT (decl))
7755 defer_p = 1;
7757 /* If we're deferring the variable, we only need to make RTL if
7758 there's an ASMSPEC. Otherwise, we'll lazily create it later when
7759 we need it. (There's no way to lazily create RTL for things that
7760 have assembly specs because the information about the specifier
7761 isn't stored in the tree, yet) */
7762 if (defer_p && asmspec)
7763 make_decl_rtl (decl, asmspec);
7764 /* If we're not deferring, go ahead and assemble the variable. */
7765 else if (!defer_p)
7766 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7769 /* The old ARM scoping rules injected variables declared in the
7770 initialization statement of a for-statement into the surrounding
7771 scope. We support this usage, in order to be backward-compatible.
7772 DECL is a just-declared VAR_DECL; if necessary inject its
7773 declaration into the surrounding scope. */
7775 void
7776 maybe_inject_for_scope_var (tree decl)
7778 timevar_push (TV_NAME_LOOKUP);
7779 if (!DECL_NAME (decl))
7781 timevar_pop (TV_NAME_LOOKUP);
7782 return;
7785 /* Declarations of __FUNCTION__ and its ilk appear magically when
7786 the variable is first used. If that happens to be inside a
7787 for-loop, we don't want to do anything special. */
7788 if (DECL_PRETTY_FUNCTION_P (decl))
7790 timevar_pop (TV_NAME_LOOKUP);
7791 return;
7794 if (current_binding_level->kind == sk_for)
7796 struct cp_binding_level *outer
7797 = current_binding_level->level_chain;
7799 /* Check to see if the same name is already bound at the outer
7800 level, either because it was directly declared, or because a
7801 dead for-decl got preserved. In either case, the code would
7802 not have been valid under the ARM scope rules, so clear
7803 is_for_scope for the current_binding_level.
7805 Otherwise, we need to preserve the temp slot for decl to last
7806 into the outer binding level. */
7808 cxx_binding *outer_binding
7809 = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
7811 if (outer_binding && BINDING_SCOPE (outer_binding) == outer
7812 && (TREE_CODE (BINDING_VALUE (outer_binding)) == VAR_DECL)
7813 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7815 BINDING_VALUE (outer_binding)
7816 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7817 current_binding_level->kind = sk_block;
7820 timevar_pop (TV_NAME_LOOKUP);
7823 /* Generate code to initialize DECL (a local variable). */
7825 static void
7826 initialize_local_var (tree decl, tree init)
7828 tree type = TREE_TYPE (decl);
7829 tree cleanup;
7831 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
7832 || TREE_CODE (decl) == RESULT_DECL,
7833 20021010);
7834 my_friendly_assert (!TREE_STATIC (decl), 20021010);
7836 if (DECL_SIZE (decl) == NULL_TREE)
7838 /* If we used it already as memory, it must stay in memory. */
7839 DECL_INITIAL (decl) = NULL_TREE;
7840 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7843 if (DECL_SIZE (decl) && type != error_mark_node)
7845 int already_used;
7847 /* Compute and store the initial value. */
7848 already_used = TREE_USED (decl) || TREE_USED (type);
7850 /* Perform the initialization. */
7851 if (init)
7853 int saved_stmts_are_full_exprs_p;
7855 my_friendly_assert (building_stmt_tree (), 20000906);
7856 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7857 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7858 finish_expr_stmt (init);
7859 current_stmt_tree ()->stmts_are_full_exprs_p =
7860 saved_stmts_are_full_exprs_p;
7863 /* Set this to 0 so we can tell whether an aggregate which was
7864 initialized was ever used. Don't do this if it has a
7865 destructor, so we don't complain about the 'resource
7866 allocation is initialization' idiom. Now set
7867 attribute((unused)) on types so decls of that type will be
7868 marked used. (see TREE_USED, above.) */
7869 if (TYPE_NEEDS_CONSTRUCTING (type)
7870 && ! already_used
7871 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7872 && DECL_NAME (decl))
7873 TREE_USED (decl) = 0;
7874 else if (already_used)
7875 TREE_USED (decl) = 1;
7878 /* Generate a cleanup, if necessary. */
7879 cleanup = cxx_maybe_build_cleanup (decl);
7880 if (DECL_SIZE (decl) && cleanup)
7881 finish_decl_cleanup (decl, cleanup);
7884 /* Finish processing of a declaration;
7885 install its line number and initial value.
7886 If the length of an array type is not known before,
7887 it must be determined now, from the initial value, or it is an error.
7889 INIT holds the value of an initializer that should be allowed to escape
7890 the normal rules.
7892 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7893 if the (init) syntax was used. */
7895 void
7896 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
7898 tree type;
7899 tree ttype = NULL_TREE;
7900 tree cleanup;
7901 const char *asmspec = NULL;
7902 int was_readonly = 0;
7904 if (! decl)
7906 if (init)
7907 error ("assignment (not initialization) in declaration");
7908 return;
7911 my_friendly_assert (TREE_CODE (decl) != RESULT_DECL, 20030619);
7913 /* Assume no cleanup is required. */
7914 cleanup = NULL_TREE;
7916 /* If a name was specified, get the string. */
7917 if (global_scope_p (current_binding_level))
7918 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
7919 if (asmspec_tree)
7920 asmspec = TREE_STRING_POINTER (asmspec_tree);
7922 if (init && TREE_CODE (init) == NAMESPACE_DECL)
7924 error ("cannot initialize `%D' to namespace `%D'",
7925 decl, init);
7926 init = NULL_TREE;
7929 if (current_class_type
7930 && CP_DECL_CONTEXT (decl) == current_class_type
7931 && TYPE_BEING_DEFINED (current_class_type)
7932 && (DECL_INITIAL (decl) || init))
7933 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
7935 if (TREE_CODE (decl) == VAR_DECL
7936 && DECL_CONTEXT (decl)
7937 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
7938 && DECL_CONTEXT (decl) != current_namespace
7939 && init)
7941 /* Leave the namespace of the object. */
7942 pop_decl_namespace ();
7945 type = TREE_TYPE (decl);
7947 if (type == error_mark_node)
7948 goto finish_end0;
7950 if (TYPE_HAS_MUTABLE_P (type))
7951 TREE_READONLY (decl) = 0;
7953 if (processing_template_decl)
7955 /* Add this declaration to the statement-tree. */
7956 if (at_function_scope_p ())
7957 add_decl_stmt (decl);
7959 if (init && DECL_INITIAL (decl))
7960 DECL_INITIAL (decl) = init;
7961 goto finish_end0;
7964 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7965 my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
7967 /* Take care of TYPE_DECLs up front. */
7968 if (TREE_CODE (decl) == TYPE_DECL)
7970 if (type != error_mark_node
7971 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
7973 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
7974 warning ("shadowing previous type declaration of `%#D'", decl);
7975 set_identifier_type_value (DECL_NAME (decl), decl);
7978 /* If we have installed this as the canonical typedef for this
7979 type, and that type has not been defined yet, delay emitting
7980 the debug information for it, as we will emit it later. */
7981 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
7982 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
7983 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7985 rest_of_decl_compilation (decl, NULL,
7986 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
7987 goto finish_end;
7990 if (TREE_CODE (decl) != FUNCTION_DECL)
7991 ttype = target_type (type);
7993 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
7994 && (TYPE_NEEDS_CONSTRUCTING (type)
7995 || TREE_CODE (type) == REFERENCE_TYPE))
7997 /* Currently, GNU C++ puts constants in text space, making them
7998 impossible to initialize. In the future, one would hope for
7999 an operating system which understood the difference between
8000 initialization and the running of a program. */
8001 was_readonly = 1;
8002 TREE_READONLY (decl) = 0;
8005 if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8007 /* This must override the asm specifier which was placed by
8008 grokclassfn. Lay this out fresh. */
8009 SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8010 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8011 make_decl_rtl (decl, asmspec);
8013 else if (TREE_CODE (decl) == VAR_DECL)
8015 /* Only PODs can have thread-local storage. Other types may require
8016 various kinds of non-trivial initialization. */
8017 if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8018 error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8019 decl, TREE_TYPE (decl));
8020 /* Convert the initializer to the type of DECL, if we have not
8021 already initialized DECL. */
8022 if (!DECL_INITIALIZED_P (decl)
8023 /* If !DECL_EXTERNAL then DECL is being defined. In the
8024 case of a static data member initialized inside the
8025 class-specifier, there can be an initializer even if DECL
8026 is *not* defined. */
8027 && (!DECL_EXTERNAL (decl) || init))
8029 init = check_initializer (decl, init, flags, &cleanup);
8030 /* Thread-local storage cannot be dynamically initialized. */
8031 if (DECL_THREAD_LOCAL (decl) && init)
8033 error ("`%D' is thread-local and so cannot be dynamically "
8034 "initialized", decl);
8035 init = NULL_TREE;
8037 /* Handle:
8039 [dcl.init]
8041 The memory occupied by any object of static storage
8042 duration is zero-initialized at program startup before
8043 any other initialization takes place.
8045 We cannot create an appropriate initializer until after
8046 the type of DECL is finalized. If DECL_INITIAL is set,
8047 then the DECL is statically initialized, and any
8048 necessary zero-initialization has already been performed. */
8049 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8050 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8051 /*nelts=*/NULL_TREE,
8052 /*static_storage_p=*/true);
8053 /* Remember that the initialization for this variable has
8054 taken place. */
8055 DECL_INITIALIZED_P (decl) = 1;
8057 /* If the variable has an array type, lay out the type, even if
8058 there is no initializer. It is valid to index through the
8059 array, and we must get TYPE_ALIGN set correctly on the array
8060 type. */
8061 else if (TREE_CODE (type) == ARRAY_TYPE)
8062 layout_type (type);
8065 /* Add this declaration to the statement-tree. This needs to happen
8066 after the call to check_initializer so that the DECL_STMT for a
8067 reference temp is added before the DECL_STMT for the reference itself. */
8068 if (at_function_scope_p ())
8069 add_decl_stmt (decl);
8071 if (TREE_CODE (decl) == VAR_DECL)
8072 layout_var_decl (decl);
8074 /* Output the assembler code and/or RTL code for variables and functions,
8075 unless the type is an undefined structure or union.
8076 If not, it will get done when the type is completed. */
8077 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
8079 if (TREE_CODE (decl) == VAR_DECL)
8080 maybe_commonize_var (decl);
8082 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8084 if (TREE_CODE (type) == FUNCTION_TYPE
8085 || TREE_CODE (type) == METHOD_TYPE)
8086 abstract_virtuals_error (decl,
8087 strip_array_types (TREE_TYPE (type)));
8088 else
8089 abstract_virtuals_error (decl, strip_array_types (type));
8091 if (TREE_CODE (decl) == FUNCTION_DECL
8092 || TREE_TYPE (decl) == error_mark_node)
8093 /* No initialization required. */
8095 else if (DECL_EXTERNAL (decl)
8096 && ! (DECL_LANG_SPECIFIC (decl)
8097 && DECL_NOT_REALLY_EXTERN (decl)))
8099 if (init)
8100 DECL_INITIAL (decl) = init;
8102 else
8104 /* A variable definition. */
8105 if (DECL_FUNCTION_SCOPE_P (decl))
8107 /* This is a local declaration. */
8108 maybe_inject_for_scope_var (decl);
8109 /* Initialize the local variable. */
8110 if (processing_template_decl)
8112 if (init || DECL_INITIAL (decl) == error_mark_node)
8113 DECL_INITIAL (decl) = init;
8115 else if (!TREE_STATIC (decl))
8116 initialize_local_var (decl, init);
8119 if (TREE_STATIC (decl))
8120 expand_static_init (decl, init);
8122 finish_end0:
8124 /* Undo call to `pushclass' that was done in `start_decl'
8125 due to initialization of qualified member variable.
8126 I.e., Foo::x = 10; */
8128 tree context = CP_DECL_CONTEXT (decl);
8129 if (context
8130 && TYPE_P (context)
8131 && (TREE_CODE (decl) == VAR_DECL
8132 /* We also have a pushclass done that we need to undo here
8133 if we're at top level and declare a method. */
8134 || TREE_CODE (decl) == FUNCTION_DECL)
8135 /* If size hasn't been set, we're still defining it,
8136 and therefore inside the class body; don't pop
8137 the binding level.. */
8138 && COMPLETE_TYPE_P (context)
8139 && context == current_class_type)
8140 pop_nested_class ();
8144 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
8145 reference, insert it in the statement-tree now. */
8146 if (cleanup)
8147 add_stmt (cleanup);
8149 finish_end:
8151 if (was_readonly)
8152 TREE_READONLY (decl) = 1;
8154 /* If this was marked 'used', be sure it will be output. */
8155 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
8156 mark_referenced (DECL_ASSEMBLER_NAME (decl));
8159 /* This is here for a midend callback from c-common.c */
8161 void
8162 finish_decl (tree decl, tree init, tree asmspec_tree)
8164 cp_finish_decl (decl, init, asmspec_tree, 0);
8167 /* Returns a declaration for a VAR_DECL as if:
8169 extern "C" TYPE NAME;
8171 had been seen. Used to create compiler-generated global
8172 variables. */
8174 tree
8175 declare_global_var (tree name, tree type)
8177 tree decl;
8179 push_to_top_level ();
8180 decl = build_decl (VAR_DECL, name, type);
8181 TREE_PUBLIC (decl) = 1;
8182 DECL_EXTERNAL (decl) = 1;
8183 DECL_ARTIFICIAL (decl) = 1;
8184 pushdecl (decl);
8185 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8186 pop_from_top_level ();
8188 return decl;
8191 /* Returns a pointer to the `atexit' function. Note that if
8192 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8193 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8195 static tree
8196 get_atexit_node (void)
8198 tree atexit_fndecl;
8199 tree arg_types;
8200 tree fn_type;
8201 tree fn_ptr_type;
8202 const char *name;
8204 if (atexit_node)
8205 return atexit_node;
8207 if (flag_use_cxa_atexit)
8209 /* The declaration for `__cxa_atexit' is:
8211 int __cxa_atexit (void (*)(void *), void *, void *)
8213 We build up the argument types and then then function type
8214 itself. */
8216 /* First, build the pointer-to-function type for the first
8217 argument. */
8218 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8219 fn_type = build_function_type (void_type_node, arg_types);
8220 fn_ptr_type = build_pointer_type (fn_type);
8221 /* Then, build the rest of the argument types. */
8222 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8223 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8224 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8225 /* And the final __cxa_atexit type. */
8226 fn_type = build_function_type (integer_type_node, arg_types);
8227 fn_ptr_type = build_pointer_type (fn_type);
8228 name = "__cxa_atexit";
8230 else
8232 /* The declaration for `atexit' is:
8234 int atexit (void (*)());
8236 We build up the argument types and then then function type
8237 itself. */
8238 fn_type = build_function_type (void_type_node, void_list_node);
8239 fn_ptr_type = build_pointer_type (fn_type);
8240 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8241 /* Build the final atexit type. */
8242 fn_type = build_function_type (integer_type_node, arg_types);
8243 name = "atexit";
8246 /* Now, build the function declaration. */
8247 push_lang_context (lang_name_c);
8248 atexit_fndecl = build_library_fn_ptr (name, fn_type);
8249 mark_used (atexit_fndecl);
8250 pop_lang_context ();
8251 atexit_node = decay_conversion (atexit_fndecl);
8253 return atexit_node;
8256 /* Returns the __dso_handle VAR_DECL. */
8258 static tree
8259 get_dso_handle_node (void)
8261 if (dso_handle_node)
8262 return dso_handle_node;
8264 /* Declare the variable. */
8265 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8266 ptr_type_node);
8268 return dso_handle_node;
8271 /* Begin a new function with internal linkage whose job will be simply
8272 to destroy some particular variable. */
8274 static GTY(()) int start_cleanup_cnt;
8276 static tree
8277 start_cleanup_fn (void)
8279 int old_interface_only = interface_only;
8280 int old_interface_unknown = interface_unknown;
8281 char name[32];
8282 tree parmtypes;
8283 tree fntype;
8284 tree fndecl;
8286 push_to_top_level ();
8288 /* No need to mangle this. */
8289 push_lang_context (lang_name_c);
8291 interface_only = 0;
8292 interface_unknown = 1;
8294 /* Build the parameter-types. */
8295 parmtypes = void_list_node;
8296 /* Functions passed to __cxa_atexit take an additional parameter.
8297 We'll just ignore it. After we implement the new calling
8298 convention for destructors, we can eliminate the use of
8299 additional cleanup functions entirely in the -fnew-abi case. */
8300 if (flag_use_cxa_atexit)
8301 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8302 /* Build the function type itself. */
8303 fntype = build_function_type (void_type_node, parmtypes);
8304 /* Build the name of the function. */
8305 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
8306 /* Build the function declaration. */
8307 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8308 /* It's a function with internal linkage, generated by the
8309 compiler. */
8310 TREE_PUBLIC (fndecl) = 0;
8311 DECL_ARTIFICIAL (fndecl) = 1;
8312 /* Make the function `inline' so that it is only emitted if it is
8313 actually needed. It is unlikely that it will be inlined, since
8314 it is only called via a function pointer, but we avoid unnecessary
8315 emissions this way. */
8316 DECL_INLINE (fndecl) = 1;
8317 /* Build the parameter. */
8318 if (flag_use_cxa_atexit)
8320 tree parmdecl;
8322 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8323 DECL_CONTEXT (parmdecl) = fndecl;
8324 TREE_USED (parmdecl) = 1;
8325 DECL_ARGUMENTS (fndecl) = parmdecl;
8328 pushdecl (fndecl);
8329 start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8331 interface_unknown = old_interface_unknown;
8332 interface_only = old_interface_only;
8334 pop_lang_context ();
8336 return current_function_decl;
8339 /* Finish the cleanup function begun by start_cleanup_fn. */
8341 static void
8342 end_cleanup_fn (void)
8344 expand_or_defer_fn (finish_function (0));
8346 pop_from_top_level ();
8349 /* Generate code to handle the destruction of DECL, an object with
8350 static storage duration. */
8352 void
8353 register_dtor_fn (tree decl)
8355 tree cleanup;
8356 tree compound_stmt;
8357 tree args;
8358 tree fcall;
8360 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8361 return;
8363 /* Call build_cleanup before we enter the anonymous function so that
8364 any access checks will be done relative to the current scope,
8365 rather than the scope of the anonymous function. */
8366 build_cleanup (decl);
8368 /* Now start the function. */
8369 cleanup = start_cleanup_fn ();
8371 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8372 to the original function, rather than the anonymous one. That
8373 will make the back-end think that nested functions are in use,
8374 which causes confusion. */
8376 push_deferring_access_checks (dk_no_check);
8377 fcall = build_cleanup (decl);
8378 pop_deferring_access_checks ();
8380 /* Create the body of the anonymous function. */
8381 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
8382 finish_expr_stmt (fcall);
8383 finish_compound_stmt (compound_stmt);
8384 end_cleanup_fn ();
8386 /* Call atexit with the cleanup function. */
8387 cxx_mark_addressable (cleanup);
8388 mark_used (cleanup);
8389 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8390 if (flag_use_cxa_atexit)
8392 args = tree_cons (NULL_TREE,
8393 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8394 NULL_TREE);
8395 args = tree_cons (NULL_TREE, null_pointer_node, args);
8396 args = tree_cons (NULL_TREE, cleanup, args);
8398 else
8399 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8400 finish_expr_stmt (build_function_call (get_atexit_node (), args));
8403 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8404 is its initializer. Generate code to handle the construction
8405 and destruction of DECL. */
8407 static void
8408 expand_static_init (tree decl, tree init)
8410 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8411 my_friendly_assert (TREE_STATIC (decl), 20021010);
8413 /* Some variables require no initialization. */
8414 if (!init
8415 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8416 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8417 return;
8419 if (! toplevel_bindings_p ())
8421 /* Emit code to perform this initialization but once. */
8422 tree if_stmt;
8423 tree then_clause;
8424 tree assignment;
8425 tree guard;
8426 tree guard_init;
8428 /* Emit code to perform this initialization but once. This code
8429 looks like:
8431 static int guard = 0;
8432 if (!guard) {
8433 // Do initialization.
8434 guard = 1;
8435 // Register variable for destruction at end of program.
8438 Note that the `temp' variable is only set to 1 *after* the
8439 initialization is complete. This ensures that an exception,
8440 thrown during the construction, will cause the variable to
8441 reinitialized when we pass through this code again, as per:
8443 [stmt.dcl]
8445 If the initialization exits by throwing an exception, the
8446 initialization is not complete, so it will be tried again
8447 the next time control enters the declaration.
8449 In theory, this process should be thread-safe, too; multiple
8450 threads should not be able to initialize the variable more
8451 than once. We don't yet attempt to ensure thread-safety. */
8453 /* Create the guard variable. */
8454 guard = get_guard (decl);
8456 /* Begin the conditional initialization. */
8457 if_stmt = begin_if_stmt ();
8458 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8459 then_clause = begin_compound_stmt (/*has_no_scope=*/false);
8461 /* Do the initialization itself. */
8462 assignment = init ? init : NULL_TREE;
8464 /* Once the assignment is complete, set TEMP to 1. Since the
8465 construction of the static object is complete at this point,
8466 we want to make sure TEMP is set to 1 even if a temporary
8467 constructed during the initialization throws an exception
8468 when it is destroyed. So, we combine the initialization and
8469 the assignment to TEMP into a single expression, ensuring
8470 that when we call finish_expr_stmt the cleanups will not be
8471 run until after TEMP is set to 1. */
8472 guard_init = set_guard (guard);
8473 if (assignment)
8474 assignment = build_compound_expr (assignment, guard_init);
8475 else
8476 assignment = guard_init;
8477 finish_expr_stmt (assignment);
8479 /* Use atexit to register a function for destroying this static
8480 variable. */
8481 register_dtor_fn (decl);
8483 finish_compound_stmt (then_clause);
8484 finish_then_clause (if_stmt);
8485 finish_if_stmt ();
8487 else
8488 static_aggregates = tree_cons (init, decl, static_aggregates);
8491 /* Finish the declaration of a catch-parameter. */
8493 tree
8494 start_handler_parms (tree declspecs, tree declarator)
8496 tree decl;
8497 if (declspecs)
8499 decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8500 1, NULL);
8501 if (decl == NULL_TREE)
8502 error ("invalid catch parameter");
8504 else
8505 decl = NULL_TREE;
8507 return decl;
8511 /* Make TYPE a complete type based on INITIAL_VALUE.
8512 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8513 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8516 complete_array_type (tree type, tree initial_value, int do_default)
8518 register tree maxindex = NULL_TREE;
8519 int value = 0;
8521 if (initial_value)
8523 /* An array of character type can be initialized from a
8524 brace-enclosed string constant. */
8525 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8526 && TREE_CODE (initial_value) == CONSTRUCTOR
8527 && CONSTRUCTOR_ELTS (initial_value)
8528 && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8529 == STRING_CST)
8530 && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8531 initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8533 /* Note MAXINDEX is really the maximum index, one less than the
8534 size. */
8535 if (TREE_CODE (initial_value) == STRING_CST)
8537 int eltsize
8538 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8539 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8540 / eltsize) - 1, 0);
8542 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8544 tree elts = CONSTRUCTOR_ELTS (initial_value);
8546 maxindex = ssize_int (-1);
8547 for (; elts; elts = TREE_CHAIN (elts))
8549 if (TREE_PURPOSE (elts))
8550 maxindex = TREE_PURPOSE (elts);
8551 else
8552 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8554 maxindex = copy_node (maxindex);
8556 else
8558 /* Make an error message unless that happened already. */
8559 if (initial_value != error_mark_node)
8560 value = 1;
8561 else
8562 initial_value = NULL_TREE;
8564 /* Prevent further error messages. */
8565 maxindex = build_int_2 (0, 0);
8569 if (!maxindex)
8571 if (do_default)
8572 maxindex = build_int_2 (0, 0);
8573 value = 2;
8576 if (maxindex)
8578 tree itype;
8579 tree domain;
8581 domain = build_index_type (maxindex);
8582 TYPE_DOMAIN (type) = domain;
8584 if (! TREE_TYPE (maxindex))
8585 TREE_TYPE (maxindex) = domain;
8586 if (initial_value)
8587 itype = TREE_TYPE (initial_value);
8588 else
8589 itype = NULL;
8590 if (itype && !TYPE_DOMAIN (itype))
8591 TYPE_DOMAIN (itype) = domain;
8592 /* The type of the main variant should never be used for arrays
8593 of different sizes. It should only ever be completed with the
8594 size of the array. */
8595 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8596 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8599 /* Lay out the type now that we can get the real answer. */
8601 layout_type (type);
8603 return value;
8606 /* Return zero if something is declared to be a member of type
8607 CTYPE when in the context of CUR_TYPE. STRING is the error
8608 message to print in that case. Otherwise, quietly return 1. */
8610 static int
8611 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8613 if (ctype && ctype != cur_type)
8615 if (flags == DTOR_FLAG)
8616 error ("destructor for alien class `%T' cannot be a member",
8617 ctype);
8618 else
8619 error ("constructor for alien class `%T' cannot be a member",
8620 ctype);
8621 return 0;
8623 return 1;
8626 /* Subroutine of `grokdeclarator'. */
8628 /* Generate errors possibly applicable for a given set of specifiers.
8629 This is for ARM $7.1.2. */
8631 static void
8632 bad_specifiers (tree object,
8633 const char* type,
8634 int virtualp,
8635 int quals,
8636 int inlinep,
8637 int friendp,
8638 int raises)
8640 if (virtualp)
8641 error ("`%D' declared as a `virtual' %s", object, type);
8642 if (inlinep)
8643 error ("`%D' declared as an `inline' %s", object, type);
8644 if (quals)
8645 error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8646 object, type);
8647 if (friendp)
8648 cp_error_at ("`%D' declared as a friend", object);
8649 if (raises
8650 && (TREE_CODE (object) == TYPE_DECL
8651 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8652 && !TYPE_REFFN_P (TREE_TYPE (object))
8653 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8654 cp_error_at ("`%D' declared with an exception specification", object);
8657 /* CTYPE is class type, or null if non-class.
8658 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8659 or METHOD_TYPE.
8660 DECLARATOR is the function's name.
8661 VIRTUALP is truthvalue of whether the function is virtual or not.
8662 FLAGS are to be passed through to `grokclassfn'.
8663 QUALS are qualifiers indicating whether the function is `const'
8664 or `volatile'.
8665 RAISES is a list of exceptions that this function can raise.
8666 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8667 not look, and -1 if we should not call `grokclassfn' at all.
8669 Returns `NULL_TREE' if something goes wrong, after issuing
8670 applicable error messages. */
8672 static tree
8673 grokfndecl (tree ctype,
8674 tree type,
8675 tree declarator,
8676 tree orig_declarator,
8677 int virtualp,
8678 enum overload_flags flags,
8679 tree quals,
8680 tree raises,
8681 int check,
8682 int friendp,
8683 int publicp,
8684 int inlinep,
8685 int funcdef_flag,
8686 int template_count,
8687 tree in_namespace)
8689 tree decl;
8690 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8691 int has_default_arg = 0;
8692 tree t;
8694 if (raises)
8695 type = build_exception_variant (type, raises);
8697 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8698 /* Propagate volatile out from type to decl. */
8699 if (TYPE_VOLATILE (type))
8700 TREE_THIS_VOLATILE (decl) = 1;
8702 /* If this decl has namespace scope, set that up. */
8703 if (in_namespace)
8704 set_decl_namespace (decl, in_namespace, friendp);
8705 else if (!ctype)
8706 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8708 /* `main' and builtins have implicit 'C' linkage. */
8709 if ((MAIN_NAME_P (declarator)
8710 || (IDENTIFIER_LENGTH (declarator) > 10
8711 && IDENTIFIER_POINTER (declarator)[0] == '_'
8712 && IDENTIFIER_POINTER (declarator)[1] == '_'
8713 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8714 && current_lang_name == lang_name_cplusplus
8715 && ctype == NULL_TREE
8716 /* NULL_TREE means global namespace. */
8717 && DECL_CONTEXT (decl) == NULL_TREE)
8718 SET_DECL_LANGUAGE (decl, lang_c);
8720 /* Should probably propagate const out from type to decl I bet (mrs). */
8721 if (staticp)
8723 DECL_STATIC_FUNCTION_P (decl) = 1;
8724 DECL_CONTEXT (decl) = ctype;
8727 if (ctype)
8728 DECL_CONTEXT (decl) = ctype;
8730 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8732 if (processing_template_decl)
8733 error ("cannot declare `::main' to be a template");
8734 if (inlinep)
8735 error ("cannot declare `::main' to be inline");
8736 if (!publicp)
8737 error ("cannot declare `::main' to be static");
8738 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8739 integer_type_node))
8740 error ("`main' must return `int'");
8741 inlinep = 0;
8742 publicp = 1;
8745 /* Members of anonymous types and local classes have no linkage; make
8746 them internal. */
8747 /* FIXME what if it gets a name from typedef? */
8748 if (ctype && (TYPE_ANONYMOUS_P (ctype)
8749 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8750 publicp = 0;
8752 if (publicp)
8754 /* [basic.link]: A name with no linkage (notably, the name of a class
8755 or enumeration declared in a local scope) shall not be used to
8756 declare an entity with linkage.
8758 Only check this for public decls for now. See core 319, 389. */
8759 t = no_linkage_check (TREE_TYPE (decl));
8760 if (t)
8762 if (TYPE_ANONYMOUS_P (t))
8764 if (DECL_EXTERN_C_P (decl))
8765 /* Allow this; it's pretty common in C. */;
8766 else
8768 pedwarn ("non-local function `%#D' uses anonymous type",
8769 decl);
8770 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8771 cp_pedwarn_at ("\
8772 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8773 TYPE_NAME (t));
8776 else
8777 pedwarn ("non-local function `%#D' uses local type `%T'",
8778 decl, t);
8782 TREE_PUBLIC (decl) = publicp;
8783 if (! publicp)
8785 DECL_INTERFACE_KNOWN (decl) = 1;
8786 DECL_NOT_REALLY_EXTERN (decl) = 1;
8789 /* If the declaration was declared inline, mark it as such. */
8790 if (inlinep)
8791 DECL_DECLARED_INLINE_P (decl) = 1;
8792 /* We inline functions that are explicitly declared inline, or, when
8793 the user explicitly asks us to, all functions. */
8794 if (DECL_DECLARED_INLINE_P (decl)
8795 || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag))
8796 DECL_INLINE (decl) = 1;
8798 DECL_EXTERNAL (decl) = 1;
8799 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8801 error ("%smember function `%D' cannot have `%T' method qualifier",
8802 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8803 quals = NULL_TREE;
8806 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8807 grok_op_properties (decl, friendp, /*complain=*/true);
8809 if (ctype && decl_function_context (decl))
8810 DECL_NO_STATIC_CHAIN (decl) = 1;
8812 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8813 if (TREE_PURPOSE (t)
8814 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8816 has_default_arg = 1;
8817 break;
8820 if (friendp
8821 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8823 if (funcdef_flag)
8824 error
8825 ("defining explicit specialization `%D' in friend declaration",
8826 orig_declarator);
8827 else
8829 tree fns = TREE_OPERAND (orig_declarator, 0);
8830 tree args = TREE_OPERAND (orig_declarator, 1);
8832 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8834 /* Something like `template <class T> friend void f<T>()'. */
8835 error ("invalid use of template-id `%D' in declaration of primary template",
8836 orig_declarator);
8837 return NULL_TREE;
8841 /* A friend declaration of the form friend void f<>(). Record
8842 the information in the TEMPLATE_ID_EXPR. */
8843 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8845 if (TREE_CODE (fns) == COMPONENT_REF)
8847 /* Due to bison parser ickiness, we will have already looked
8848 up an operator_name or PFUNCNAME within the current class
8849 (see template_id in parse.y). If the current class contains
8850 such a name, we'll get a COMPONENT_REF here. Undo that. */
8852 my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
8853 == current_class_type, 20001120);
8854 fns = TREE_OPERAND (fns, 1);
8856 my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
8857 || TREE_CODE (fns) == OVERLOAD, 20001120);
8858 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
8860 if (has_default_arg)
8862 error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8863 decl);
8864 return NULL_TREE;
8867 if (inlinep)
8869 error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8870 decl);
8871 return NULL_TREE;
8876 if (funcdef_flag)
8877 /* Make the init_value nonzero so pushdecl knows this is not
8878 tentative. error_mark_node is replaced later with the BLOCK. */
8879 DECL_INITIAL (decl) = error_mark_node;
8881 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8882 TREE_NOTHROW (decl) = 1;
8884 /* Caller will do the rest of this. */
8885 if (check < 0)
8886 return decl;
8888 if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
8889 DECL_CONSTRUCTOR_P (decl) = 1;
8891 /* Function gets the ugly name, field gets the nice one. This call
8892 may change the type of the function (because of default
8893 parameters)! */
8894 if (ctype != NULL_TREE)
8895 grokclassfn (ctype, decl, flags, quals);
8897 decl = check_explicit_specialization (orig_declarator, decl,
8898 template_count,
8899 2 * (funcdef_flag != 0) +
8900 4 * (friendp != 0));
8901 if (decl == error_mark_node)
8902 return NULL_TREE;
8904 if (ctype != NULL_TREE
8905 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
8906 && check)
8908 tree old_decl;
8910 old_decl = check_classfn (ctype, decl);
8912 if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
8913 /* Because grokfndecl is always supposed to return a
8914 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8915 here. We depend on our callers to figure out that its
8916 really a template that's being returned. */
8917 old_decl = DECL_TEMPLATE_RESULT (old_decl);
8919 if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
8920 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8922 /* Remove the `this' parm added by grokclassfn.
8923 XXX Isn't this done in start_function, too? */
8924 revert_static_member_fn (decl);
8925 last_function_parms = TREE_CHAIN (last_function_parms);
8927 if (old_decl && DECL_ARTIFICIAL (old_decl))
8928 error ("definition of implicitly-declared `%D'", old_decl);
8930 if (old_decl)
8932 bool ok;
8934 /* Since we've smashed OLD_DECL to its
8935 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8936 if (TREE_CODE (decl) == TEMPLATE_DECL)
8937 decl = DECL_TEMPLATE_RESULT (decl);
8939 /* Attempt to merge the declarations. This can fail, in
8940 the case of some invalid specialization declarations. */
8941 push_scope (ctype);
8942 ok = duplicate_decls (decl, old_decl);
8943 pop_scope (ctype);
8944 if (!ok)
8946 error ("no `%#D' member function declared in class `%T'",
8947 decl, ctype);
8948 return NULL_TREE;
8950 return old_decl;
8954 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8955 return NULL_TREE;
8957 if (ctype == NULL_TREE || check)
8958 return decl;
8960 if (virtualp)
8961 DECL_VIRTUAL_P (decl) = 1;
8963 return decl;
8966 /* Create a VAR_DECL named NAME with the indicated TYPE.
8968 If SCOPE is non-NULL, it is the class type or namespace containing
8969 the variable. If SCOPE is NULL, the variable should is created in
8970 the innermost enclosings scope. */
8972 static tree
8973 grokvardecl (tree type,
8974 tree name,
8975 RID_BIT_TYPE * specbits_in,
8976 int initialized,
8977 int constp,
8978 tree scope)
8980 tree decl;
8981 RID_BIT_TYPE specbits;
8983 my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE,
8984 20020808);
8986 specbits = *specbits_in;
8988 /* Compute the scope in which to place the variable. */
8989 if (!scope)
8991 /* An explicit "extern" specifier indicates a namespace-scope
8992 variable. */
8993 if (RIDBIT_SETP (RID_EXTERN, specbits))
8994 scope = current_namespace;
8995 else if (!at_function_scope_p ())
8997 scope = current_scope ();
8998 if (!scope)
8999 scope = current_namespace;
9003 if (scope
9004 && (/* If the variable is a namespace-scope variable declared in a
9005 template, we need DECL_LANG_SPECIFIC. */
9006 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9007 /* Similarly for namespace-scope variables with language linkage
9008 other than C++. */
9009 || (TREE_CODE (scope) == NAMESPACE_DECL
9010 && current_lang_name != lang_name_cplusplus)
9011 /* Similarly for static data members. */
9012 || TYPE_P (scope)))
9013 decl = build_lang_decl (VAR_DECL, name, type);
9014 else
9015 decl = build_decl (VAR_DECL, name, type);
9017 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9018 set_decl_namespace (decl, scope, 0);
9019 else
9020 DECL_CONTEXT (decl) = scope;
9022 if (name && scope && current_lang_name != lang_name_c)
9023 /* We can't mangle lazily here because we don't have any
9024 way to recover whether or not a variable was `extern
9025 "C"' later. */
9026 mangle_decl (decl);
9028 if (RIDBIT_SETP (RID_EXTERN, specbits))
9030 DECL_THIS_EXTERN (decl) = 1;
9031 DECL_EXTERNAL (decl) = !initialized;
9034 /* In class context, static means one per class,
9035 public access, and static storage. */
9036 if (DECL_CLASS_SCOPE_P (decl))
9038 TREE_PUBLIC (decl) = 1;
9039 TREE_STATIC (decl) = 1;
9040 DECL_EXTERNAL (decl) = 0;
9042 /* At top level, either `static' or no s.c. makes a definition
9043 (perhaps tentative), and absence of `static' makes it public. */
9044 else if (toplevel_bindings_p ())
9046 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9047 && (DECL_THIS_EXTERN (decl) || ! constp));
9048 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9050 /* Not at top level, only `static' makes a static definition. */
9051 else
9053 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9054 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9057 if (RIDBIT_SETP (RID_THREAD, specbits))
9059 if (targetm.have_tls)
9060 DECL_THREAD_LOCAL (decl) = 1;
9061 else
9062 /* A mere warning is sure to result in improper semantics
9063 at runtime. Don't bother to allow this to compile. */
9064 error ("thread-local storage not supported for this target");
9067 if (TREE_PUBLIC (decl))
9069 /* [basic.link]: A name with no linkage (notably, the name of a class
9070 or enumeration declared in a local scope) shall not be used to
9071 declare an entity with linkage.
9073 Only check this for public decls for now. */
9074 tree t = no_linkage_check (TREE_TYPE (decl));
9075 if (t)
9077 if (TYPE_ANONYMOUS_P (t))
9078 /* Ignore for now; `enum { foo } e' is pretty common. */;
9079 else
9080 pedwarn ("non-local variable `%#D' uses local type `%T'",
9081 decl, t);
9085 return decl;
9088 /* Create and return a canonical pointer to member function type, for
9089 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9091 tree
9092 build_ptrmemfunc_type (tree type)
9094 tree field, fields;
9095 tree t;
9096 tree unqualified_variant = NULL_TREE;
9098 if (type == error_mark_node)
9099 return type;
9101 /* If a canonical type already exists for this type, use it. We use
9102 this method instead of type_hash_canon, because it only does a
9103 simple equality check on the list of field members. */
9105 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9106 return t;
9108 /* Make sure that we always have the unqualified pointer-to-member
9109 type first. */
9110 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9111 unqualified_variant
9112 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9114 t = make_aggr_type (RECORD_TYPE);
9115 /* Let the front-end know this is a pointer to member function... */
9116 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9117 /* ... and not really an aggregate. */
9118 SET_IS_AGGR_TYPE (t, 0);
9120 field = build_decl (FIELD_DECL, pfn_identifier, type);
9121 fields = field;
9123 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9124 TREE_CHAIN (field) = fields;
9125 fields = field;
9127 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9129 /* Zap out the name so that the back-end will give us the debugging
9130 information for this anonymous RECORD_TYPE. */
9131 TYPE_NAME (t) = NULL_TREE;
9133 /* If this is not the unqualified form of this pointer-to-member
9134 type, set the TYPE_MAIN_VARIANT for this type to be the
9135 unqualified type. Since they are actually RECORD_TYPEs that are
9136 not variants of each other, we must do this manually. */
9137 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9139 t = build_qualified_type (t, cp_type_quals (type));
9140 TYPE_MAIN_VARIANT (t) = unqualified_variant;
9141 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9142 TYPE_NEXT_VARIANT (unqualified_variant) = t;
9145 /* Cache this pointer-to-member type so that we can find it again
9146 later. */
9147 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9149 return t;
9152 /* Create and return a pointer to data member type. */
9154 tree
9155 build_ptrmem_type (tree class_type, tree member_type)
9157 if (TREE_CODE (member_type) == METHOD_TYPE)
9159 tree arg_types;
9161 arg_types = TYPE_ARG_TYPES (member_type);
9162 class_type = (cp_build_qualified_type
9163 (class_type,
9164 cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types)))));
9165 member_type
9166 = build_method_type_directly (class_type,
9167 TREE_TYPE (member_type),
9168 TREE_CHAIN (arg_types));
9169 return build_ptrmemfunc_type (build_pointer_type (member_type));
9171 else
9173 my_friendly_assert (TREE_CODE (member_type) != FUNCTION_TYPE,
9174 20030716);
9175 return build_offset_type (class_type, member_type);
9179 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9180 Check to see that the definition is valid. Issue appropriate error
9181 messages. Return 1 if the definition is particularly bad, or 0
9182 otherwise. */
9185 check_static_variable_definition (tree decl, tree type)
9187 /* Motion 10 at San Diego: If a static const integral data member is
9188 initialized with an integral constant expression, the initializer
9189 may appear either in the declaration (within the class), or in
9190 the definition, but not both. If it appears in the class, the
9191 member is a member constant. The file-scope definition is always
9192 required. */
9193 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9195 error ("invalid in-class initialization of static data member of non-integral type `%T'",
9196 type);
9197 /* If we just return the declaration, crashes will sometimes
9198 occur. We therefore return void_type_node, as if this was a
9199 friend declaration, to cause callers to completely ignore
9200 this declaration. */
9201 return 1;
9203 else if (!CP_TYPE_CONST_P (type))
9204 error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9205 decl);
9206 else if (pedantic && !INTEGRAL_TYPE_P (type))
9207 pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9209 return 0;
9212 /* Given the SIZE (i.e., number of elements) in an array, compute an
9213 appropriate index type for the array. If non-NULL, NAME is the
9214 name of the thing being declared. */
9216 tree
9217 compute_array_index_type (tree name, tree size)
9219 tree itype;
9221 /* If this involves a template parameter, it will be a constant at
9222 instantiation time, but we don't know what the value is yet.
9223 Even if no template parameters are involved, we may an expression
9224 that is not a constant; we don't even simplify `1 + 2' when
9225 processing a template. */
9226 if (processing_template_decl)
9228 /* Resolve a qualified reference to an enumerator or static
9229 const data member of ours. */
9230 if (TREE_CODE (size) == SCOPE_REF
9231 && TREE_OPERAND (size, 0) == current_class_type)
9233 tree t = lookup_field (current_class_type,
9234 TREE_OPERAND (size, 1), 0, false);
9235 if (t)
9236 size = t;
9239 return build_index_type (build_min (MINUS_EXPR, sizetype,
9240 size, integer_one_node));
9243 /* The size might be the result of a cast. */
9244 STRIP_TYPE_NOPS (size);
9246 /* It might be a const variable or enumeration constant. */
9247 size = decl_constant_value (size);
9249 /* The array bound must be an integer type. */
9250 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9251 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9252 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9254 if (name)
9255 error ("size of array `%D' has non-integer type", name);
9256 else
9257 error ("size of array has non-integer type");
9258 size = integer_one_node;
9261 /* Normally, the array-bound will be a constant. */
9262 if (TREE_CODE (size) == INTEGER_CST)
9264 /* Check to see if the array bound overflowed. Make that an
9265 error, no matter how generous we're being. */
9266 int old_flag_pedantic_errors = flag_pedantic_errors;
9267 int old_pedantic = pedantic;
9268 pedantic = flag_pedantic_errors = 1;
9269 constant_expression_warning (size);
9270 pedantic = old_pedantic;
9271 flag_pedantic_errors = old_flag_pedantic_errors;
9273 /* An array must have a positive number of elements. */
9274 if (INT_CST_LT (size, integer_zero_node))
9276 if (name)
9277 error ("size of array `%D' is negative", name);
9278 else
9279 error ("size of array is negative");
9280 size = integer_one_node;
9282 /* Except that an extension we allow zero-sized arrays. We
9283 always allow them in system headers because glibc uses
9284 them. */
9285 else if (integer_zerop (size) && pedantic && !in_system_header)
9287 if (name)
9288 pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9289 else
9290 pedwarn ("ISO C++ forbids zero-size array");
9293 else if (TREE_CONSTANT (size))
9295 /* `(int) &fn' is not a valid array bound. */
9296 if (name)
9297 error ("size of array `%D' is not an integral constant-expression",
9298 name);
9299 else
9300 error ("size of array is not an integral constant-expression");
9303 /* Compute the index of the largest element in the array. It is
9304 one less than the number of elements in the array. */
9305 itype
9306 = fold (cp_build_binary_op (MINUS_EXPR,
9307 cp_convert (ssizetype, size),
9308 cp_convert (ssizetype,
9309 integer_one_node)));
9311 /* Check for variable-sized arrays. We allow such things as an
9312 extension, even though they are not allowed in ANSI/ISO C++. */
9313 if (!TREE_CONSTANT (itype))
9315 if (pedantic)
9317 if (name)
9318 pedwarn ("ISO C++ forbids variable-size array `%D'",
9319 name);
9320 else
9321 pedwarn ("ISO C++ forbids variable-size array");
9324 /* Create a variable-sized array index type. */
9325 itype = variable_size (itype);
9327 /* Make sure that there was no overflow when creating to a signed
9328 index type. (For example, on a 32-bit machine, an array with
9329 size 2^32 - 1 is too big.) */
9330 else if (TREE_OVERFLOW (itype))
9332 error ("overflow in array dimension");
9333 TREE_OVERFLOW (itype) = 0;
9336 /* Create and return the appropriate index type. */
9337 return build_index_type (itype);
9340 /* Returns the scope (if any) in which the entity declared by
9341 DECLARATOR will be located. If the entity was declared with an
9342 unqualified name, NULL_TREE is returned. */
9344 tree
9345 get_scope_of_declarator (tree declarator)
9347 if (!declarator)
9348 return NULL_TREE;
9350 switch (TREE_CODE (declarator))
9352 case CALL_EXPR:
9353 case ARRAY_REF:
9354 case INDIRECT_REF:
9355 case ADDR_EXPR:
9356 /* For any of these, the main declarator is the first operand. */
9357 return get_scope_of_declarator (TREE_OPERAND
9358 (declarator, 0));
9360 case SCOPE_REF:
9361 /* For a pointer-to-member, continue descending. */
9362 if (TREE_CODE (TREE_OPERAND (declarator, 1))
9363 == INDIRECT_REF)
9364 return get_scope_of_declarator (TREE_OPERAND
9365 (declarator, 1));
9366 /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9367 which the declaration occurs is the first operand. */
9368 return TREE_OPERAND (declarator, 0);
9370 case TREE_LIST:
9371 /* Attributes to be applied. The declarator is TREE_VALUE. */
9372 return get_scope_of_declarator (TREE_VALUE (declarator));
9374 default:
9375 /* Otherwise, we have a declarator-id which is not a qualified
9376 name; the entity will be declared in the current scope. */
9377 return NULL_TREE;
9381 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9382 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9383 with this type. */
9385 static tree
9386 create_array_type_for_decl (tree name, tree type, tree size)
9388 tree itype = NULL_TREE;
9389 const char* error_msg;
9391 /* If things have already gone awry, bail now. */
9392 if (type == error_mark_node || size == error_mark_node)
9393 return error_mark_node;
9395 /* Assume that everything will go OK. */
9396 error_msg = NULL;
9398 /* There are some types which cannot be array elements. */
9399 switch (TREE_CODE (type))
9401 case VOID_TYPE:
9402 error_msg = "array of void";
9403 break;
9405 case FUNCTION_TYPE:
9406 error_msg = "array of functions";
9407 break;
9409 case REFERENCE_TYPE:
9410 error_msg = "array of references";
9411 break;
9413 case METHOD_TYPE:
9414 error_msg = "array of function members";
9415 break;
9417 default:
9418 break;
9421 /* If something went wrong, issue an error-message and return. */
9422 if (error_msg)
9424 if (name)
9425 error ("declaration of `%D' as %s", name, error_msg);
9426 else
9427 error ("creating %s", error_msg);
9429 return error_mark_node;
9432 /* [dcl.array]
9434 The constant expressions that specify the bounds of the arrays
9435 can be omitted only for the first member of the sequence. */
9436 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9438 if (name)
9439 error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9440 name);
9441 else
9442 error ("multidimensional array must have bounds for all dimensions except the first");
9444 return error_mark_node;
9447 /* Figure out the index type for the array. */
9448 if (size)
9449 itype = compute_array_index_type (name, size);
9451 return build_cplus_array_type (type, itype);
9454 /* Check that it's OK to declare a function with the indicated TYPE.
9455 SFK indicates the kind of special function (if any) that this
9456 function is. OPTYPE is the type given in a conversion operator
9457 declaration. Returns the actual return type of the function; that
9458 may be different than TYPE if an error occurs, or for certain
9459 special functions. */
9461 static tree
9462 check_special_function_return_type (special_function_kind sfk,
9463 tree type,
9464 tree optype)
9466 switch (sfk)
9468 case sfk_constructor:
9469 if (type)
9470 error ("return type specification for constructor invalid");
9472 type = void_type_node;
9473 break;
9475 case sfk_destructor:
9476 if (type)
9477 error ("return type specification for destructor invalid");
9478 type = void_type_node;
9479 break;
9481 case sfk_conversion:
9482 if (type && !same_type_p (type, optype))
9483 error ("operator `%T' declared to return `%T'", optype, type);
9484 else if (type)
9485 pedwarn ("return type specified for `operator %T'", optype);
9486 type = optype;
9487 break;
9489 default:
9490 abort ();
9491 break;
9494 return type;
9497 /* Given declspecs and a declarator (abstract or otherwise), determine
9498 the name and type of the object declared and construct a DECL node
9499 for it.
9501 DECLSPECS is a chain of tree_list nodes whose value fields
9502 are the storage classes and type specifiers.
9504 DECL_CONTEXT says which syntactic context this declaration is in:
9505 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9506 FUNCDEF for a function definition. Like NORMAL but a few different
9507 error messages in each case. Return value may be zero meaning
9508 this definition is too screwy to try to parse.
9509 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9510 handle member functions (which have FIELD context).
9511 Return value may be zero meaning this definition is too screwy to
9512 try to parse.
9513 PARM for a parameter declaration (either within a function prototype
9514 or before a function body). Make a PARM_DECL, or return void_type_node.
9515 CATCHPARM for a parameter declaration before a catch clause.
9516 TYPENAME if for a typename (in a cast or sizeof).
9517 Don't make a DECL node; just return the ..._TYPE node.
9518 FIELD for a struct or union field; make a FIELD_DECL.
9519 BITFIELD for a field with specified width.
9520 INITIALIZED is 1 if the decl has an initializer.
9522 ATTRLIST is a pointer to the list of attributes, which may be NULL
9523 if there are none; *ATTRLIST may be modified if attributes from inside
9524 the declarator should be applied to the declaration.
9526 When this function is called, scoping variables (such as
9527 CURRENT_CLASS_TYPE) should reflect the scope in which the
9528 declaration occurs, not the scope in which the new declaration will
9529 be placed. For example, on:
9531 void S::f() { ... }
9533 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9534 should not be `S'. */
9536 tree
9537 grokdeclarator (tree declarator,
9538 tree declspecs,
9539 enum decl_context decl_context,
9540 int initialized,
9541 tree* attrlist)
9543 RID_BIT_TYPE specbits;
9544 int nclasses = 0;
9545 tree spec;
9546 tree type = NULL_TREE;
9547 int longlong = 0;
9548 int type_quals;
9549 int virtualp, explicitp, friendp, inlinep, staticp;
9550 int explicit_int = 0;
9551 int explicit_char = 0;
9552 int defaulted_int = 0;
9553 int extern_langp = 0;
9554 tree dependant_name = NULL_TREE;
9556 tree typedef_decl = NULL_TREE;
9557 const char *name;
9558 tree typedef_type = NULL_TREE;
9559 int funcdef_flag = 0;
9560 enum tree_code innermost_code = ERROR_MARK;
9561 int bitfield = 0;
9562 #if 0
9563 /* See the code below that used this. */
9564 tree decl_attr = NULL_TREE;
9565 #endif
9567 /* Keep track of what sort of function is being processed
9568 so that we can warn about default return values, or explicit
9569 return values which do not match prescribed defaults. */
9570 special_function_kind sfk = sfk_none;
9572 tree dname = NULL_TREE;
9573 tree ctype = current_class_type;
9574 tree ctor_return_type = NULL_TREE;
9575 enum overload_flags flags = NO_SPECIAL;
9576 tree quals = NULL_TREE;
9577 tree raises = NULL_TREE;
9578 int template_count = 0;
9579 tree in_namespace = NULL_TREE;
9580 tree returned_attrs = NULL_TREE;
9581 tree scope = NULL_TREE;
9583 RIDBIT_RESET_ALL (specbits);
9584 if (decl_context == FUNCDEF)
9585 funcdef_flag = 1, decl_context = NORMAL;
9586 else if (decl_context == MEMFUNCDEF)
9587 funcdef_flag = -1, decl_context = FIELD;
9588 else if (decl_context == BITFIELD)
9589 bitfield = 1, decl_context = FIELD;
9591 /* Look inside a declarator for the name being declared
9592 and get it as a string, for an error message. */
9594 tree *next = &declarator;
9595 register tree decl;
9596 name = NULL;
9598 while (next && *next)
9600 decl = *next;
9601 switch (TREE_CODE (decl))
9603 case TREE_LIST:
9604 /* For attributes. */
9605 next = &TREE_VALUE (decl);
9606 break;
9608 case COND_EXPR:
9609 ctype = NULL_TREE;
9610 next = &TREE_OPERAND (decl, 0);
9611 break;
9613 case BIT_NOT_EXPR: /* For C++ destructors! */
9615 tree name = TREE_OPERAND (decl, 0);
9616 tree rename = NULL_TREE;
9618 my_friendly_assert (flags == NO_SPECIAL, 152);
9619 flags = DTOR_FLAG;
9620 sfk = sfk_destructor;
9621 if (TYPE_P (name))
9622 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9623 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9624 if (ctype == NULL_TREE)
9626 if (current_class_type == NULL_TREE)
9628 error ("destructors must be member functions");
9629 flags = NO_SPECIAL;
9631 else
9633 tree t = constructor_name (current_class_type);
9634 if (t != name)
9635 rename = t;
9638 else
9640 tree t = constructor_name (ctype);
9641 if (t != name)
9642 rename = t;
9645 if (rename)
9647 error ("destructor `%T' must match class name `%T'",
9648 name, rename);
9649 TREE_OPERAND (decl, 0) = rename;
9651 next = &name;
9653 break;
9655 case ADDR_EXPR: /* C++ reference declaration */
9656 /* Fall through. */
9657 case ARRAY_REF:
9658 case INDIRECT_REF:
9659 ctype = NULL_TREE;
9660 innermost_code = TREE_CODE (decl);
9661 next = &TREE_OPERAND (decl, 0);
9662 break;
9664 case CALL_EXPR:
9665 innermost_code = TREE_CODE (decl);
9666 if (decl_context == FIELD && ctype == NULL_TREE)
9667 ctype = current_class_type;
9668 if (ctype
9669 && TREE_OPERAND (decl, 0)
9670 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9671 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9672 ctype)))
9673 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9674 next = &TREE_OPERAND (decl, 0);
9675 decl = *next;
9676 if (ctype != NULL_TREE
9677 && decl != NULL_TREE && flags != DTOR_FLAG
9678 && constructor_name_p (decl, ctype))
9680 sfk = sfk_constructor;
9681 ctor_return_type = ctype;
9683 ctype = NULL_TREE;
9684 break;
9686 case TEMPLATE_ID_EXPR:
9688 tree fns = TREE_OPERAND (decl, 0);
9690 dname = fns;
9691 if (TREE_CODE (dname) == COMPONENT_REF)
9692 dname = TREE_OPERAND (dname, 1);
9693 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9695 my_friendly_assert (is_overloaded_fn (dname),
9696 19990331);
9697 dname = DECL_NAME (get_first_fn (dname));
9700 /* Fall through. */
9702 case IDENTIFIER_NODE:
9703 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9704 dname = decl;
9706 next = 0;
9708 if (C_IS_RESERVED_WORD (dname))
9710 error ("declarator-id missing; using reserved word `%D'",
9711 dname);
9712 name = IDENTIFIER_POINTER (dname);
9714 else if (!IDENTIFIER_TYPENAME_P (dname))
9715 name = IDENTIFIER_POINTER (dname);
9716 else
9718 my_friendly_assert (flags == NO_SPECIAL, 154);
9719 flags = TYPENAME_FLAG;
9720 ctor_return_type = TREE_TYPE (dname);
9721 sfk = sfk_conversion;
9722 if (IDENTIFIER_GLOBAL_VALUE (dname)
9723 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9724 == TYPE_DECL))
9725 name = IDENTIFIER_POINTER (dname);
9726 else
9727 name = "<invalid operator>";
9729 break;
9731 /* C++ extension */
9732 case SCOPE_REF:
9734 /* Perform error checking, and decide on a ctype. */
9735 tree cname = TREE_OPERAND (decl, 0);
9736 if (cname == NULL_TREE)
9737 ctype = NULL_TREE;
9738 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9740 ctype = NULL_TREE;
9741 in_namespace = TREE_OPERAND (decl, 0);
9743 else if (! is_aggr_type (cname, 1))
9744 ctype = NULL_TREE;
9745 /* Must test TREE_OPERAND (decl, 1), in case user gives
9746 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9747 else if (TREE_OPERAND (decl, 1)
9748 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9749 ctype = cname;
9750 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9751 || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9753 /* This might be declaring a member of a template
9754 parm to be a friend. */
9755 ctype = cname;
9756 dependant_name = TREE_OPERAND (decl, 1);
9758 else if (ctype == NULL_TREE)
9759 ctype = cname;
9760 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9762 else
9764 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9766 error ("type `%T' is not derived from type `%T'",
9767 cname, ctype);
9768 ctype = NULL_TREE;
9770 else
9771 ctype = cname;
9774 /* It is valid to write:
9776 class C { void f(); };
9777 typedef C D;
9778 void D::f();
9780 The standard is not clear about whether `typedef const C D' is
9781 legal; as of 2002-09-15 the committee is considering
9782 that question. EDG 3.0 allows that syntax.
9783 Therefore, we do as well. */
9784 if (ctype)
9785 ctype = TYPE_MAIN_VARIANT (ctype);
9786 /* Update the declarator so that when we process it
9787 again the correct type is present. */
9788 TREE_OPERAND (decl, 0) = ctype;
9790 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9791 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9792 ctype))
9793 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9794 next = &TREE_OPERAND (decl, 1);
9795 decl = *next;
9796 if (ctype)
9798 tree name = decl;
9800 if (TREE_CODE (name) == BIT_NOT_EXPR)
9801 name = TREE_OPERAND (name, 0);
9803 if (!constructor_name_p (decl, ctype))
9805 else if (decl == name)
9807 sfk = sfk_constructor;
9808 ctor_return_type = ctype;
9810 else
9812 sfk = sfk_destructor;
9813 ctor_return_type = ctype;
9814 flags = DTOR_FLAG;
9815 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9816 next = &TREE_OPERAND (decl, 0);
9820 break;
9822 case ERROR_MARK:
9823 next = 0;
9824 break;
9826 case TYPE_DECL:
9827 /* Parse error puts this typespec where
9828 a declarator should go. */
9829 error ("`%T' specified as declarator-id", DECL_NAME (decl));
9830 if (TREE_TYPE (decl) == current_class_type)
9831 error (" perhaps you want `%T' for a constructor",
9832 current_class_name);
9833 dname = DECL_NAME (decl);
9834 name = IDENTIFIER_POINTER (dname);
9836 /* Avoid giving two errors for this. */
9837 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9839 declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
9840 *next = dname;
9841 next = 0;
9842 break;
9844 case BASELINK:
9845 next = &BASELINK_FUNCTIONS (decl);
9846 break;
9848 case TEMPLATE_DECL:
9849 /* Sometimes, we see a template-name used as part of a
9850 decl-specifier like in
9851 std::allocator alloc;
9852 Handle that gracefully. */
9853 error ("invalid use of template-name '%E' in a declarator", decl);
9854 return error_mark_node;
9855 break;
9857 default:
9858 my_friendly_assert (0, 20020917);
9863 /* A function definition's declarator must have the form of
9864 a function declarator. */
9866 if (funcdef_flag && innermost_code != CALL_EXPR)
9867 return 0;
9869 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9870 && innermost_code != CALL_EXPR
9871 && ! (ctype && declspecs == NULL_TREE))
9873 error ("declaration of `%D' as non-function", dname);
9874 return void_type_node;
9877 /* Anything declared one level down from the top level
9878 must be one of the parameters of a function
9879 (because the body is at least two levels down). */
9881 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9882 by not allowing C++ class definitions to specify their parameters
9883 with xdecls (must be spec.d in the parmlist).
9885 Since we now wait to push a class scope until we are sure that
9886 we are in a legitimate method context, we must set oldcname
9887 explicitly (since current_class_name is not yet alive).
9889 We also want to avoid calling this a PARM if it is in a namespace. */
9891 if (decl_context == NORMAL && !toplevel_bindings_p ())
9893 struct cp_binding_level *b = current_binding_level;
9894 current_binding_level = b->level_chain;
9895 if (current_binding_level != 0 && toplevel_bindings_p ())
9896 decl_context = PARM;
9897 current_binding_level = b;
9900 if (name == NULL)
9901 name = decl_context == PARM ? "parameter" : "type name";
9903 /* Look through the decl specs and record which ones appear.
9904 Some typespecs are defined as built-in typenames.
9905 Others, the ones that are modifiers of other types,
9906 are represented by bits in SPECBITS: set the bits for
9907 the modifiers that appear. Storage class keywords are also in SPECBITS.
9909 If there is a typedef name or a type, store the type in TYPE.
9910 This includes builtin typedefs such as `int'.
9912 Set EXPLICIT_INT if the type is `int' or `char' and did not
9913 come from a user typedef.
9915 Set LONGLONG if `long' is mentioned twice.
9917 For C++, constructors and destructors have their own fast treatment. */
9919 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9921 register int i;
9922 register tree id;
9924 /* Certain parse errors slip through. For example,
9925 `int class;' is not caught by the parser. Try
9926 weakly to recover here. */
9927 if (TREE_CODE (spec) != TREE_LIST)
9928 return 0;
9930 id = TREE_VALUE (spec);
9932 /* If the entire declaration is itself tagged as deprecated then
9933 suppress reports of deprecated items. */
9934 if (!adding_implicit_members && id && TREE_DEPRECATED (id))
9936 if (deprecated_state != DEPRECATED_SUPPRESS)
9937 warn_deprecated_use (id);
9940 if (TREE_CODE (id) == IDENTIFIER_NODE)
9942 if (id == ridpointers[(int) RID_INT]
9943 || id == ridpointers[(int) RID_CHAR]
9944 || id == ridpointers[(int) RID_BOOL]
9945 || id == ridpointers[(int) RID_WCHAR])
9947 if (type)
9949 if (id == ridpointers[(int) RID_BOOL])
9950 error ("`bool' is now a keyword");
9951 else
9952 error ("extraneous `%T' ignored", id);
9954 else
9956 if (id == ridpointers[(int) RID_INT])
9957 explicit_int = 1;
9958 else if (id == ridpointers[(int) RID_CHAR])
9959 explicit_char = 1;
9960 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
9962 goto found;
9964 /* C++ aggregate types. */
9965 if (IDENTIFIER_HAS_TYPE_VALUE (id))
9967 if (type)
9968 error ("multiple declarations `%T' and `%T'", type, id);
9969 else
9970 type = IDENTIFIER_TYPE_VALUE (id);
9971 goto found;
9974 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
9976 if (ridpointers[i] == id)
9978 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
9980 if (pedantic && ! in_system_header && warn_long_long)
9981 pedwarn ("ISO C++ does not support `long long'");
9982 if (longlong)
9983 error ("`long long long' is too long for GCC");
9984 else
9985 longlong = 1;
9987 else if (RIDBIT_SETP (i, specbits))
9988 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
9990 /* Diagnose "__thread extern" or "__thread static". */
9991 if (RIDBIT_SETP (RID_THREAD, specbits))
9993 if (i == (int)RID_EXTERN)
9994 error ("`__thread' before `extern'");
9995 else if (i == (int)RID_STATIC)
9996 error ("`__thread' before `static'");
9999 if (i == (int)RID_EXTERN
10000 && TREE_PURPOSE (spec) == error_mark_node)
10001 /* This extern was part of a language linkage. */
10002 extern_langp = 1;
10004 RIDBIT_SET (i, specbits);
10005 goto found;
10009 else if (TREE_CODE (id) == TYPE_DECL)
10011 if (type)
10012 error ("multiple declarations `%T' and `%T'", type,
10013 TREE_TYPE (id));
10014 else
10016 type = TREE_TYPE (id);
10017 TREE_VALUE (spec) = type;
10018 typedef_decl = id;
10020 goto found;
10022 if (type)
10023 error ("two or more data types in declaration of `%s'", name);
10024 else if (TREE_CODE (id) == IDENTIFIER_NODE)
10026 register tree t = lookup_name (id, 1);
10027 if (!t || TREE_CODE (t) != TYPE_DECL)
10028 error ("`%s' fails to be a typedef or built in type",
10029 IDENTIFIER_POINTER (id));
10030 else
10032 type = TREE_TYPE (t);
10033 typedef_decl = t;
10036 else if (id != error_mark_node)
10037 /* Can't change CLASS nodes into RECORD nodes here! */
10038 type = id;
10040 found: ;
10043 #if 0
10044 /* See the code below that used this. */
10045 if (typedef_decl)
10046 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10047 #endif
10048 typedef_type = type;
10050 /* No type at all: default to `int', and set DEFAULTED_INT
10051 because it was not a user-defined typedef. */
10053 if (type == NULL_TREE
10054 && (RIDBIT_SETP (RID_SIGNED, specbits)
10055 || RIDBIT_SETP (RID_UNSIGNED, specbits)
10056 || RIDBIT_SETP (RID_LONG, specbits)
10057 || RIDBIT_SETP (RID_SHORT, specbits)))
10059 /* These imply 'int'. */
10060 type = integer_type_node;
10061 defaulted_int = 1;
10064 if (sfk != sfk_none)
10065 type = check_special_function_return_type (sfk, type,
10066 ctor_return_type);
10067 else if (type == NULL_TREE)
10069 int is_main;
10071 explicit_int = -1;
10073 /* We handle `main' specially here, because 'main () { }' is so
10074 common. With no options, it is allowed. With -Wreturn-type,
10075 it is a warning. It is only an error with -pedantic-errors. */
10076 is_main = (funcdef_flag
10077 && dname && MAIN_NAME_P (dname)
10078 && ctype == NULL_TREE
10079 && in_namespace == NULL_TREE
10080 && current_namespace == global_namespace);
10082 if (in_system_header || flag_ms_extensions)
10083 /* Allow it, sigh. */;
10084 else if (pedantic || ! is_main)
10085 pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10086 name);
10087 else if (warn_return_type)
10088 warning ("ISO C++ forbids declaration of `%s' with no type",
10089 name);
10091 type = integer_type_node;
10094 ctype = NULL_TREE;
10096 /* Now process the modifiers that were specified
10097 and check for invalid combinations. */
10099 /* Long double is a special combination. */
10101 if (RIDBIT_SETP (RID_LONG, specbits)
10102 && TYPE_MAIN_VARIANT (type) == double_type_node)
10104 RIDBIT_RESET (RID_LONG, specbits);
10105 type = build_qualified_type (long_double_type_node,
10106 cp_type_quals (type));
10109 /* Check all other uses of type modifiers. */
10111 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10112 || RIDBIT_SETP (RID_SIGNED, specbits)
10113 || RIDBIT_SETP (RID_LONG, specbits)
10114 || RIDBIT_SETP (RID_SHORT, specbits))
10116 int ok = 0;
10118 if (TREE_CODE (type) == REAL_TYPE)
10119 error ("short, signed or unsigned invalid for `%s'", name);
10120 else if (TREE_CODE (type) != INTEGER_TYPE)
10121 error ("long, short, signed or unsigned invalid for `%s'", name);
10122 else if (RIDBIT_SETP (RID_LONG, specbits)
10123 && RIDBIT_SETP (RID_SHORT, specbits))
10124 error ("long and short specified together for `%s'", name);
10125 else if ((RIDBIT_SETP (RID_LONG, specbits)
10126 || RIDBIT_SETP (RID_SHORT, specbits))
10127 && explicit_char)
10128 error ("long or short specified with char for `%s'", name);
10129 else if ((RIDBIT_SETP (RID_LONG, specbits)
10130 || RIDBIT_SETP (RID_SHORT, specbits))
10131 && TREE_CODE (type) == REAL_TYPE)
10132 error ("long or short specified with floating type for `%s'", name);
10133 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10134 && RIDBIT_SETP (RID_UNSIGNED, specbits))
10135 error ("signed and unsigned given together for `%s'", name);
10136 else
10138 ok = 1;
10139 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10141 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10142 name);
10143 if (flag_pedantic_errors)
10144 ok = 0;
10148 /* Discard the type modifiers if they are invalid. */
10149 if (! ok)
10151 RIDBIT_RESET (RID_UNSIGNED, specbits);
10152 RIDBIT_RESET (RID_SIGNED, specbits);
10153 RIDBIT_RESET (RID_LONG, specbits);
10154 RIDBIT_RESET (RID_SHORT, specbits);
10155 longlong = 0;
10159 if (RIDBIT_SETP (RID_COMPLEX, specbits)
10160 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10162 error ("complex invalid for `%s'", name);
10163 RIDBIT_RESET (RID_COMPLEX, specbits);
10166 /* Decide whether an integer type is signed or not.
10167 Optionally treat bitfields as signed by default. */
10168 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10169 /* [class.bit]
10171 It is implementation-defined whether a plain (neither
10172 explicitly signed or unsigned) char, short, int, or long
10173 bit-field is signed or unsigned.
10175 Naturally, we extend this to long long as well. Note that
10176 this does not include wchar_t. */
10177 || (bitfield && !flag_signed_bitfields
10178 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10179 /* A typedef for plain `int' without `signed' can be
10180 controlled just like plain `int', but a typedef for
10181 `signed int' cannot be so controlled. */
10182 && !(typedef_decl
10183 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10184 && (TREE_CODE (type) == INTEGER_TYPE
10185 || TREE_CODE (type) == CHAR_TYPE)
10186 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10188 if (longlong)
10189 type = long_long_unsigned_type_node;
10190 else if (RIDBIT_SETP (RID_LONG, specbits))
10191 type = long_unsigned_type_node;
10192 else if (RIDBIT_SETP (RID_SHORT, specbits))
10193 type = short_unsigned_type_node;
10194 else if (type == char_type_node)
10195 type = unsigned_char_type_node;
10196 else if (typedef_decl)
10197 type = c_common_unsigned_type (type);
10198 else
10199 type = unsigned_type_node;
10201 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10202 && type == char_type_node)
10203 type = signed_char_type_node;
10204 else if (longlong)
10205 type = long_long_integer_type_node;
10206 else if (RIDBIT_SETP (RID_LONG, specbits))
10207 type = long_integer_type_node;
10208 else if (RIDBIT_SETP (RID_SHORT, specbits))
10209 type = short_integer_type_node;
10211 if (RIDBIT_SETP (RID_COMPLEX, specbits))
10213 /* If we just have "complex", it is equivalent to
10214 "complex double", but if any modifiers at all are specified it is
10215 the complex form of TYPE. E.g, "complex short" is
10216 "complex short int". */
10218 if (defaulted_int && ! longlong
10219 && ! (RIDBIT_SETP (RID_LONG, specbits)
10220 || RIDBIT_SETP (RID_SHORT, specbits)
10221 || RIDBIT_SETP (RID_SIGNED, specbits)
10222 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10223 type = complex_double_type_node;
10224 else if (type == integer_type_node)
10225 type = complex_integer_type_node;
10226 else if (type == float_type_node)
10227 type = complex_float_type_node;
10228 else if (type == double_type_node)
10229 type = complex_double_type_node;
10230 else if (type == long_double_type_node)
10231 type = complex_long_double_type_node;
10232 else
10233 type = build_complex_type (type);
10236 type_quals = TYPE_UNQUALIFIED;
10237 if (RIDBIT_SETP (RID_CONST, specbits))
10238 type_quals |= TYPE_QUAL_CONST;
10239 if (RIDBIT_SETP (RID_VOLATILE, specbits))
10240 type_quals |= TYPE_QUAL_VOLATILE;
10241 if (RIDBIT_SETP (RID_RESTRICT, specbits))
10242 type_quals |= TYPE_QUAL_RESTRICT;
10243 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10244 error ("qualifiers are not allowed on declaration of `operator %T'",
10245 ctor_return_type);
10247 type_quals |= cp_type_quals (type);
10248 type = cp_build_qualified_type_real
10249 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10250 ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10251 /* We might have ignored or rejected some of the qualifiers. */
10252 type_quals = cp_type_quals (type);
10254 staticp = 0;
10255 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10256 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10257 RIDBIT_RESET (RID_VIRTUAL, specbits);
10258 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10259 RIDBIT_RESET (RID_EXPLICIT, specbits);
10261 if (RIDBIT_SETP (RID_STATIC, specbits))
10262 staticp = 1 + (decl_context == FIELD);
10264 if (virtualp && staticp == 2)
10266 error ("member `%D' cannot be declared both virtual and static",
10267 dname);
10268 staticp = 0;
10270 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10271 RIDBIT_RESET (RID_FRIEND, specbits);
10273 if (dependant_name && !friendp)
10275 error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10276 return void_type_node;
10279 /* Warn if two storage classes are given. Default to `auto'. */
10281 if (RIDBIT_ANY_SET (specbits))
10283 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10284 if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10285 if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10286 if (decl_context == PARM && nclasses > 0)
10287 error ("storage class specifiers invalid in parameter declarations");
10288 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10290 if (decl_context == PARM)
10291 error ("typedef declaration invalid in parameter declaration");
10292 nclasses++;
10294 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10295 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10296 if (!nclasses && !friendp && extern_langp)
10297 nclasses++;
10300 /* Give error if `virtual' is used outside of class declaration. */
10301 if (virtualp
10302 && (current_class_name == NULL_TREE || decl_context != FIELD))
10304 error ("virtual outside class declaration");
10305 virtualp = 0;
10308 /* Static anonymous unions are dealt with here. */
10309 if (staticp && decl_context == TYPENAME
10310 && TREE_CODE (declspecs) == TREE_LIST
10311 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10312 decl_context = FIELD;
10314 /* Warn about storage classes that are invalid for certain
10315 kinds of declarations (parameters, typenames, etc.). */
10317 /* "static __thread" and "extern __thread" are allowed. */
10318 if (nclasses == 2
10319 && RIDBIT_SETP (RID_THREAD, specbits)
10320 && (RIDBIT_SETP (RID_EXTERN, specbits)
10321 || RIDBIT_SETP (RID_STATIC, specbits)))
10322 nclasses = 1;
10324 if (nclasses > 1)
10325 error ("multiple storage classes in declaration of `%s'", name);
10326 else if (decl_context != NORMAL && nclasses > 0)
10328 if ((decl_context == PARM || decl_context == CATCHPARM)
10329 && (RIDBIT_SETP (RID_REGISTER, specbits)
10330 || RIDBIT_SETP (RID_AUTO, specbits)))
10332 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10334 else if (decl_context == FIELD
10335 /* C++ allows static class elements */
10336 && RIDBIT_SETP (RID_STATIC, specbits))
10337 /* C++ also allows inlines and signed and unsigned elements,
10338 but in those cases we don't come in here. */
10340 else
10342 if (decl_context == FIELD)
10344 tree tmp = NULL_TREE;
10345 register int op = 0;
10347 if (declarator)
10349 /* Avoid trying to get an operand off an identifier node. */
10350 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10351 tmp = declarator;
10352 else
10353 tmp = TREE_OPERAND (declarator, 0);
10354 op = IDENTIFIER_OPNAME_P (tmp);
10355 if (IDENTIFIER_TYPENAME_P (tmp))
10357 if (IDENTIFIER_GLOBAL_VALUE (tmp)
10358 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10359 == TYPE_DECL))
10360 name = IDENTIFIER_POINTER (tmp);
10361 else
10362 name = "<invalid operator>";
10365 error ("storage class specified for %s `%s'",
10366 op ? "member operator" : "field",
10367 name);
10369 else
10371 if (decl_context == PARM || decl_context == CATCHPARM)
10372 error ("storage class specified for parameter `%s'", name);
10373 else
10374 error ("storage class specified for typename");
10376 RIDBIT_RESET (RID_REGISTER, specbits);
10377 RIDBIT_RESET (RID_AUTO, specbits);
10378 RIDBIT_RESET (RID_EXTERN, specbits);
10379 RIDBIT_RESET (RID_THREAD, specbits);
10382 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10384 if (toplevel_bindings_p ())
10386 /* It's common practice (and completely valid) to have a const
10387 be initialized and declared extern. */
10388 if (!(type_quals & TYPE_QUAL_CONST))
10389 warning ("`%s' initialized and declared `extern'", name);
10391 else
10392 error ("`%s' has both `extern' and initializer", name);
10394 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10395 && ! toplevel_bindings_p ())
10396 error ("nested function `%s' declared `extern'", name);
10397 else if (toplevel_bindings_p ())
10399 if (RIDBIT_SETP (RID_AUTO, specbits))
10400 error ("top-level declaration of `%s' specifies `auto'", name);
10402 else if (RIDBIT_SETP (RID_THREAD, specbits)
10403 && !RIDBIT_SETP (RID_EXTERN, specbits)
10404 && !RIDBIT_SETP (RID_STATIC, specbits))
10406 error ("function-scope `%s' implicitly auto and declared `__thread'",
10407 name);
10408 RIDBIT_RESET (RID_THREAD, specbits);
10411 if (nclasses > 0 && friendp)
10412 error ("storage class specifiers invalid in friend function declarations");
10414 scope = get_scope_of_declarator (declarator);
10416 /* Now figure out the structure of the declarator proper.
10417 Descend through it, creating more complex types, until we reach
10418 the declared identifier (or NULL_TREE, in an abstract declarator). */
10420 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10421 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10423 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10424 an INDIRECT_REF (for *...),
10425 a CALL_EXPR (for ...(...)),
10426 an identifier (for the name being declared)
10427 or a null pointer (for the place in an absolute declarator
10428 where the name was omitted).
10429 For the last two cases, we have just exited the loop.
10431 For C++ it could also be
10432 a SCOPE_REF (for class :: ...). In this case, we have converted
10433 sensible names to types, and those are the values we use to
10434 qualify the member name.
10435 an ADDR_EXPR (for &...),
10436 a BIT_NOT_EXPR (for destructors)
10438 At this point, TYPE is the type of elements of an array,
10439 or for a function to return, or for a pointer to point to.
10440 After this sequence of ifs, TYPE is the type of the
10441 array or function or pointer, and DECLARATOR has had its
10442 outermost layer removed. */
10444 if (type == error_mark_node)
10446 if (declarator == error_mark_node)
10447 return error_mark_node;
10448 else if (TREE_CODE (declarator) == SCOPE_REF)
10449 declarator = TREE_OPERAND (declarator, 1);
10450 else
10451 declarator = TREE_OPERAND (declarator, 0);
10452 continue;
10454 if (quals != NULL_TREE
10455 && (declarator == NULL_TREE
10456 || TREE_CODE (declarator) != SCOPE_REF))
10458 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10459 ctype = TYPE_METHOD_BASETYPE (type);
10460 if (ctype != NULL_TREE)
10462 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10463 grok_method_quals (ctype, dummy, quals);
10464 type = TREE_TYPE (dummy);
10465 quals = NULL_TREE;
10469 switch (TREE_CODE (declarator))
10471 case TREE_LIST:
10473 /* We encode a declarator with embedded attributes using
10474 a TREE_LIST. */
10475 tree attrs = TREE_PURPOSE (declarator);
10476 tree inner_decl;
10477 int attr_flags;
10479 declarator = TREE_VALUE (declarator);
10480 inner_decl = declarator;
10481 while (inner_decl != NULL_TREE
10482 && TREE_CODE (inner_decl) == TREE_LIST)
10483 inner_decl = TREE_VALUE (inner_decl);
10484 attr_flags = 0;
10485 if (inner_decl == NULL_TREE
10486 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10487 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10488 if (TREE_CODE (inner_decl) == CALL_EXPR)
10489 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10490 if (TREE_CODE (inner_decl) == ARRAY_REF)
10491 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10492 returned_attrs = decl_attributes (&type,
10493 chainon (returned_attrs, attrs),
10494 attr_flags);
10496 break;
10498 case ARRAY_REF:
10500 tree size = TREE_OPERAND (declarator, 1);
10501 declarator = TREE_OPERAND (declarator, 0);
10503 type = create_array_type_for_decl (dname, type, size);
10505 ctype = NULL_TREE;
10507 break;
10509 case CALL_EXPR:
10511 tree arg_types;
10512 int funcdecl_p;
10513 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10514 tree inner_decl = TREE_OPERAND (declarator, 0);
10516 /* Declaring a function type.
10517 Make sure we have a valid type for the function to return. */
10519 /* We now know that the TYPE_QUALS don't apply to the
10520 decl, but to its return type. */
10521 type_quals = TYPE_UNQUALIFIED;
10523 /* Warn about some types functions can't return. */
10525 if (TREE_CODE (type) == FUNCTION_TYPE)
10527 error ("`%s' declared as function returning a function", name);
10528 type = integer_type_node;
10530 if (TREE_CODE (type) == ARRAY_TYPE)
10532 error ("`%s' declared as function returning an array", name);
10533 type = integer_type_node;
10536 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10537 inner_decl = TREE_OPERAND (inner_decl, 1);
10539 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10540 inner_decl = dname;
10542 /* Pick up type qualifiers which should be applied to `this'. */
10543 quals = CALL_DECLARATOR_QUALS (declarator);
10545 /* Pick up the exception specifications. */
10546 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10548 /* Say it's a definition only for the CALL_EXPR
10549 closest to the identifier. */
10550 funcdecl_p
10551 = inner_decl
10552 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10553 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10554 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10556 if (ctype == NULL_TREE
10557 && decl_context == FIELD
10558 && funcdecl_p
10559 && (friendp == 0 || dname == current_class_name))
10560 ctype = current_class_type;
10562 if (ctype && sfk == sfk_conversion)
10563 TYPE_HAS_CONVERSION (ctype) = 1;
10564 if (ctype && constructor_name_p (dname, ctype))
10566 /* We are within a class's scope. If our declarator name
10567 is the same as the class name, and we are defining
10568 a function, then it is a constructor/destructor, and
10569 therefore returns a void type. */
10571 if (flags == DTOR_FLAG)
10573 /* ISO C++ 12.4/2. A destructor may not be
10574 declared const or volatile. A destructor may
10575 not be static. */
10576 if (staticp == 2)
10577 error ("destructor cannot be static member function");
10578 if (quals)
10580 error ("destructors may not be `%s'",
10581 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10582 quals = NULL_TREE;
10584 if (decl_context == FIELD)
10586 if (! member_function_or_else (ctype,
10587 current_class_type,
10588 flags))
10589 return void_type_node;
10592 else /* It's a constructor. */
10594 if (explicitp == 1)
10595 explicitp = 2;
10596 /* ISO C++ 12.1. A constructor may not be
10597 declared const or volatile. A constructor may
10598 not be virtual. A constructor may not be
10599 static. */
10600 if (staticp == 2)
10601 error ("constructor cannot be static member function");
10602 if (virtualp)
10604 pedwarn ("constructors cannot be declared virtual");
10605 virtualp = 0;
10607 if (quals)
10609 error ("constructors may not be `%s'",
10610 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10611 quals = NULL_TREE;
10614 RID_BIT_TYPE tmp_bits;
10615 memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10616 RIDBIT_RESET (RID_INLINE, tmp_bits);
10617 RIDBIT_RESET (RID_STATIC, tmp_bits);
10618 if (RIDBIT_ANY_SET (tmp_bits))
10619 error ("return value type specifier for constructor ignored");
10621 if (decl_context == FIELD)
10623 if (! member_function_or_else (ctype,
10624 current_class_type,
10625 flags))
10626 return void_type_node;
10627 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10628 if (sfk != sfk_constructor)
10629 return NULL_TREE;
10632 if (decl_context == FIELD)
10633 staticp = 0;
10635 else if (friendp)
10637 if (initialized)
10638 error ("can't initialize friend function `%s'", name);
10639 if (virtualp)
10641 /* Cannot be both friend and virtual. */
10642 error ("virtual functions cannot be friends");
10643 RIDBIT_RESET (RID_FRIEND, specbits);
10644 friendp = 0;
10646 if (decl_context == NORMAL)
10647 error ("friend declaration not in class definition");
10648 if (current_function_decl && funcdef_flag)
10649 error ("can't define friend function `%s' in a local class definition",
10650 name);
10653 /* Construct the function type and go to the next
10654 inner layer of declarator. */
10656 declarator = TREE_OPERAND (declarator, 0);
10658 arg_types = grokparms (inner_parms);
10660 if (declarator && flags == DTOR_FLAG)
10662 /* A destructor declared in the body of a class will
10663 be represented as a BIT_NOT_EXPR. But, we just
10664 want the underlying IDENTIFIER. */
10665 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10666 declarator = TREE_OPERAND (declarator, 0);
10668 if (arg_types != void_list_node)
10670 error ("destructors may not have parameters");
10671 arg_types = void_list_node;
10672 last_function_parms = NULL_TREE;
10676 /* ANSI says that `const int foo ();'
10677 does not make the function foo const. */
10678 type = build_function_type (type, arg_types);
10680 break;
10682 case ADDR_EXPR:
10683 case INDIRECT_REF:
10684 /* Filter out pointers-to-references and references-to-references.
10685 We can get these if a TYPE_DECL is used. */
10687 if (TREE_CODE (type) == REFERENCE_TYPE)
10689 error (TREE_CODE (declarator) == ADDR_EXPR
10690 ? "cannot declare reference to `%#T'"
10691 : "cannot declare pointer to `%#T'", type);
10692 type = TREE_TYPE (type);
10694 else if (VOID_TYPE_P (type)
10695 && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10696 error (ctype ? "cannot declare pointer to `%#T' member"
10697 : "cannot declare reference to `%#T'", type);
10699 /* Merge any constancy or volatility into the target type
10700 for the pointer. */
10702 /* We now know that the TYPE_QUALS don't apply to the decl,
10703 but to the target of the pointer. */
10704 type_quals = TYPE_UNQUALIFIED;
10706 if (TREE_CODE (declarator) == ADDR_EXPR)
10708 if (!VOID_TYPE_P (type))
10709 type = build_reference_type (type);
10711 else if (TREE_CODE (type) == METHOD_TYPE)
10712 type = build_ptrmemfunc_type (build_pointer_type (type));
10713 else if (ctype)
10714 type = build_ptrmem_type (ctype, type);
10715 else
10716 type = build_pointer_type (type);
10718 /* Process a list of type modifier keywords (such as
10719 const or volatile) that were given inside the `*' or `&'. */
10721 if (TREE_TYPE (declarator))
10723 register tree typemodlist;
10724 int erred = 0;
10725 int constp = 0;
10726 int volatilep = 0;
10727 int restrictp = 0;
10729 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10730 typemodlist = TREE_CHAIN (typemodlist))
10732 tree qualifier = TREE_VALUE (typemodlist);
10734 if (qualifier == ridpointers[(int) RID_CONST])
10736 constp++;
10737 type_quals |= TYPE_QUAL_CONST;
10739 else if (qualifier == ridpointers[(int) RID_VOLATILE])
10741 volatilep++;
10742 type_quals |= TYPE_QUAL_VOLATILE;
10744 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10746 restrictp++;
10747 type_quals |= TYPE_QUAL_RESTRICT;
10749 else if (!erred)
10751 erred = 1;
10752 error ("invalid type modifier within pointer declarator");
10755 if (constp > 1)
10756 pedwarn ("duplicate `const'");
10757 if (volatilep > 1)
10758 pedwarn ("duplicate `volatile'");
10759 if (restrictp > 1)
10760 pedwarn ("duplicate `restrict'");
10761 type = cp_build_qualified_type (type, type_quals);
10762 type_quals = cp_type_quals (type);
10764 declarator = TREE_OPERAND (declarator, 0);
10765 ctype = NULL_TREE;
10766 break;
10768 case SCOPE_REF:
10770 /* We have converted type names to NULL_TREE if the
10771 name was bogus, or to a _TYPE node, if not.
10773 The variable CTYPE holds the type we will ultimately
10774 resolve to. The code here just needs to build
10775 up appropriate member types. */
10776 tree sname = TREE_OPERAND (declarator, 1);
10777 tree t;
10779 /* Destructors can have their visibilities changed as well. */
10780 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10781 sname = TREE_OPERAND (sname, 0);
10783 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10785 /* We had a reference to a global decl, or
10786 perhaps we were given a non-aggregate typedef,
10787 in which case we cleared this out, and should just
10788 keep going as though it wasn't there. */
10789 declarator = sname;
10790 continue;
10792 ctype = TREE_OPERAND (declarator, 0);
10794 t = ctype;
10795 while (t != NULL_TREE && CLASS_TYPE_P (t))
10797 /* You're supposed to have one `template <...>'
10798 for every template class, but you don't need one
10799 for a full specialization. For example:
10801 template <class T> struct S{};
10802 template <> struct S<int> { void f(); };
10803 void S<int>::f () {}
10805 is correct; there shouldn't be a `template <>' for
10806 the definition of `S<int>::f'. */
10807 if (CLASSTYPE_TEMPLATE_INFO (t)
10808 && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
10809 || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10810 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
10811 template_count += 1;
10813 t = TYPE_MAIN_DECL (t);
10814 t = DECL_CONTEXT (t);
10817 if (sname == NULL_TREE)
10818 goto done_scoping;
10820 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10822 /* This is the `standard' use of the scoping operator:
10823 basetype :: member . */
10825 if (ctype == current_class_type)
10827 /* class A {
10828 void A::f ();
10831 Is this ill-formed? */
10833 if (pedantic)
10834 pedwarn ("extra qualification `%T::' on member `%s' ignored",
10835 ctype, name);
10837 else if (TREE_CODE (type) == FUNCTION_TYPE)
10839 if (current_class_type == NULL_TREE || friendp)
10840 type
10841 = build_method_type_directly (ctype,
10842 TREE_TYPE (type),
10843 TYPE_ARG_TYPES (type));
10844 else
10846 error ("cannot declare member function `%T::%s' within `%T'",
10847 ctype, name, current_class_type);
10848 return error_mark_node;
10851 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10852 || COMPLETE_TYPE_P (complete_type (ctype)))
10854 /* Have to move this code elsewhere in this function.
10855 this code is used for i.e., typedef int A::M; M *pm;
10857 It is? How? jason 10/2/94 */
10859 if (current_class_type)
10861 error ("cannot declare member `%T::%s' within `%T'",
10862 ctype, name, current_class_type);
10863 return void_type_node;
10866 else
10868 cxx_incomplete_type_error (NULL_TREE, ctype);
10869 return error_mark_node;
10872 declarator = sname;
10874 else if (TREE_CODE (sname) == SCOPE_REF)
10875 abort ();
10876 else
10878 done_scoping:
10879 declarator = TREE_OPERAND (declarator, 1);
10880 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10881 /* In this case, we will deal with it later. */
10883 else if (TREE_CODE (type) == FUNCTION_TYPE)
10884 type = build_method_type_directly (ctype,
10885 TREE_TYPE (type),
10886 TYPE_ARG_TYPES (type));
10889 break;
10891 case BIT_NOT_EXPR:
10892 declarator = TREE_OPERAND (declarator, 0);
10893 break;
10895 case BASELINK:
10896 declarator = BASELINK_FUNCTIONS (declarator);
10897 break;
10899 case RECORD_TYPE:
10900 case UNION_TYPE:
10901 case ENUMERAL_TYPE:
10902 declarator = NULL_TREE;
10903 break;
10905 case ERROR_MARK:
10906 declarator = NULL_TREE;
10907 break;
10909 default:
10910 abort ();
10914 if (returned_attrs)
10916 if (attrlist)
10917 *attrlist = chainon (returned_attrs, *attrlist);
10918 else
10919 attrlist = &returned_attrs;
10922 /* Now TYPE has the actual type. */
10924 /* Did array size calculations overflow? */
10926 if (TREE_CODE (type) == ARRAY_TYPE
10927 && COMPLETE_TYPE_P (type)
10928 && TREE_OVERFLOW (TYPE_SIZE (type)))
10930 error ("size of array `%s' is too large", name);
10931 /* If we proceed with the array type as it is, we'll eventually
10932 crash in tree_low_cst(). */
10933 type = error_mark_node;
10936 if ((decl_context == FIELD || decl_context == PARM)
10937 && !processing_template_decl
10938 && variably_modified_type_p (type))
10940 if (decl_context == FIELD)
10941 error ("data member may not have variably modified type `%T'", type);
10942 else
10943 error ("parameter may not have variably modified type `%T'", type);
10944 type = error_mark_node;
10947 if (explicitp == 1 || (explicitp && friendp))
10949 /* [dcl.fct.spec] The explicit specifier shall only be used in
10950 declarations of constructors within a class definition. */
10951 error ("only declarations of constructors can be `explicit'");
10952 explicitp = 0;
10955 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10957 if (decl_context != FIELD || friendp)
10959 error ("non-member `%s' cannot be declared `mutable'", name);
10960 RIDBIT_RESET (RID_MUTABLE, specbits);
10962 else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
10964 error ("non-object member `%s' cannot be declared `mutable'", name);
10965 RIDBIT_RESET (RID_MUTABLE, specbits);
10967 else if (TREE_CODE (type) == FUNCTION_TYPE
10968 || TREE_CODE (type) == METHOD_TYPE)
10970 error ("function `%s' cannot be declared `mutable'", name);
10971 RIDBIT_RESET (RID_MUTABLE, specbits);
10973 else if (staticp)
10975 error ("static `%s' cannot be declared `mutable'", name);
10976 RIDBIT_RESET (RID_MUTABLE, specbits);
10978 else if (type_quals & TYPE_QUAL_CONST)
10980 error ("const `%s' cannot be declared `mutable'", name);
10981 RIDBIT_RESET (RID_MUTABLE, specbits);
10985 if (declarator == NULL_TREE
10986 || TREE_CODE (declarator) == IDENTIFIER_NODE
10987 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
10988 && (TREE_CODE (type) == FUNCTION_TYPE
10989 || TREE_CODE (type) == METHOD_TYPE)))
10990 /* OK */;
10991 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10993 error ("template-id `%D' used as a declarator", declarator);
10994 declarator = dname;
10996 else
10997 /* Unexpected declarator format. */
10998 abort ();
11000 /* If this is declaring a typedef name, return a TYPE_DECL. */
11002 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11004 tree decl;
11006 /* Note that the grammar rejects storage classes
11007 in typenames, fields or parameters. */
11008 if (current_lang_name == lang_name_java)
11009 TYPE_FOR_JAVA (type) = 1;
11011 if (decl_context == FIELD)
11013 if (constructor_name_p (declarator, current_class_type))
11014 pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11015 declarator);
11016 decl = build_lang_decl (TYPE_DECL, declarator, type);
11018 else
11020 decl = build_decl (TYPE_DECL, declarator, type);
11021 if (in_namespace || ctype)
11022 error ("%Htypedef name may not be a nested-name-specifier",
11023 &DECL_SOURCE_LOCATION (decl));
11024 if (!current_function_decl)
11025 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11028 /* If the user declares "typedef struct {...} foo" then the
11029 struct will have an anonymous name. Fill that name in now.
11030 Nothing can refer to it, so nothing needs know about the name
11031 change. */
11032 if (type != error_mark_node
11033 && declarator
11034 && TYPE_NAME (type)
11035 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11036 && TYPE_ANONYMOUS_P (type)
11037 /* Don't do this if there are attributes. */
11038 && (!attrlist || !*attrlist)
11039 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11041 tree oldname = TYPE_NAME (type);
11042 tree t;
11044 /* Replace the anonymous name with the real name everywhere. */
11045 lookup_tag_reverse (type, declarator);
11046 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11047 if (TYPE_NAME (t) == oldname)
11048 TYPE_NAME (t) = decl;
11050 if (TYPE_LANG_SPECIFIC (type))
11051 TYPE_WAS_ANONYMOUS (type) = 1;
11053 /* If this is a typedef within a template class, the nested
11054 type is a (non-primary) template. The name for the
11055 template needs updating as well. */
11056 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11057 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11058 = TYPE_IDENTIFIER (type);
11060 /* FIXME remangle member functions; member functions of a
11061 type with external linkage have external linkage. */
11064 if (quals)
11066 if (ctype == NULL_TREE)
11068 if (TREE_CODE (type) != METHOD_TYPE)
11069 error ("%Hinvalid type qualifier for non-member function type",
11070 &DECL_SOURCE_LOCATION (decl));
11071 else
11072 ctype = TYPE_METHOD_BASETYPE (type);
11074 if (ctype != NULL_TREE)
11075 grok_method_quals (ctype, decl, quals);
11078 if (RIDBIT_SETP (RID_SIGNED, specbits)
11079 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11080 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11082 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11083 inlinep, friendp, raises != NULL_TREE);
11085 return decl;
11088 /* Detect the case of an array type of unspecified size
11089 which came, as such, direct from a typedef name.
11090 We must copy the type, so that the array's domain can be
11091 individually set by the object's initializer. */
11093 if (type && typedef_type
11094 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11095 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11096 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11098 /* Detect where we're using a typedef of function type to declare a
11099 function. last_function_parms will not be set, so we must create
11100 it now. */
11102 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11104 tree decls = NULL_TREE;
11105 tree args;
11107 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11109 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11111 TREE_CHAIN (decl) = decls;
11112 decls = decl;
11115 last_function_parms = nreverse (decls);
11118 /* If this is a type name (such as, in a cast or sizeof),
11119 compute the type and return it now. */
11121 if (decl_context == TYPENAME)
11123 /* Note that the grammar rejects storage classes
11124 in typenames, fields or parameters. */
11125 if (type_quals != TYPE_UNQUALIFIED)
11126 type_quals = TYPE_UNQUALIFIED;
11128 /* Special case: "friend class foo" looks like a TYPENAME context. */
11129 if (friendp)
11131 if (type_quals != TYPE_UNQUALIFIED)
11133 error ("type qualifiers specified for friend class declaration");
11134 type_quals = TYPE_UNQUALIFIED;
11136 if (inlinep)
11138 error ("`inline' specified for friend class declaration");
11139 inlinep = 0;
11142 if (!current_aggr)
11144 /* Don't allow friend declaration without a class-key. */
11145 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11146 pedwarn ("template parameters cannot be friends");
11147 else if (TREE_CODE (type) == TYPENAME_TYPE)
11148 pedwarn ("friend declaration requires class-key, "
11149 "i.e. `friend class %T::%D'",
11150 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11151 else
11152 pedwarn ("friend declaration requires class-key, "
11153 "i.e. `friend %#T'",
11154 type);
11157 /* Only try to do this stuff if we didn't already give up. */
11158 if (type != integer_type_node)
11160 /* A friendly class? */
11161 if (current_class_type)
11162 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
11163 /*complain=*/true);
11164 else
11165 error ("trying to make class `%T' a friend of global scope",
11166 type);
11168 type = void_type_node;
11171 else if (quals)
11173 if (ctype == NULL_TREE)
11175 if (TREE_CODE (type) != METHOD_TYPE)
11176 error ("invalid qualifiers on non-member function type");
11177 else
11178 ctype = TYPE_METHOD_BASETYPE (type);
11180 if (ctype)
11182 tree dummy = build_decl (TYPE_DECL, declarator, type);
11183 grok_method_quals (ctype, dummy, quals);
11184 type = TREE_TYPE (dummy);
11188 return type;
11190 else if (declarator == NULL_TREE && decl_context != PARM
11191 && decl_context != CATCHPARM
11192 && TREE_CODE (type) != UNION_TYPE
11193 && ! bitfield)
11195 error ("abstract declarator `%T' used as declaration", type);
11196 declarator = make_anon_name ();
11199 /* `void' at top level (not within pointer)
11200 is allowed only in typedefs or type names.
11201 We don't complain about parms either, but that is because
11202 a better error message can be made later. */
11204 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11206 if (! declarator)
11207 error ("unnamed variable or field declared void");
11208 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11210 if (IDENTIFIER_OPNAME_P (declarator))
11211 abort ();
11212 else
11213 error ("variable or field `%s' declared void", name);
11215 else
11216 error ("variable or field declared void");
11217 type = integer_type_node;
11220 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11221 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11223 if (decl_context == PARM || decl_context == CATCHPARM)
11225 if (ctype || in_namespace)
11226 error ("cannot use `::' in parameter declaration");
11228 /* A parameter declared as an array of T is really a pointer to T.
11229 One declared as a function is really a pointer to a function.
11230 One declared as a member is really a pointer to member. */
11232 if (TREE_CODE (type) == ARRAY_TYPE)
11234 /* Transfer const-ness of array into that of type pointed to. */
11235 type = build_pointer_type (TREE_TYPE (type));
11236 type_quals = TYPE_UNQUALIFIED;
11238 else if (TREE_CODE (type) == FUNCTION_TYPE)
11239 type = build_pointer_type (type);
11243 register tree decl;
11245 if (decl_context == PARM)
11247 decl = cp_build_parm_decl (declarator, type);
11249 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11250 inlinep, friendp, raises != NULL_TREE);
11252 else if (decl_context == FIELD)
11254 /* The C99 flexible array extension. */
11255 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
11256 && TYPE_DOMAIN (type) == NULL_TREE)
11258 tree itype = compute_array_index_type (dname, integer_zero_node);
11259 type = build_cplus_array_type (TREE_TYPE (type), itype);
11262 if (type == error_mark_node)
11264 /* Happens when declaring arrays of sizes which
11265 are error_mark_node, for example. */
11266 decl = NULL_TREE;
11268 else if (in_namespace && !friendp)
11270 /* Something like struct S { int N::j; }; */
11271 error ("invalid use of `::'");
11272 decl = NULL_TREE;
11274 else if (TREE_CODE (type) == FUNCTION_TYPE)
11276 int publicp = 0;
11277 tree function_context;
11279 /* We catch the others as conflicts with the builtin
11280 typedefs. */
11281 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11283 error ("function `%D' cannot be declared friend",
11284 declarator);
11285 friendp = 0;
11288 if (friendp == 0)
11290 if (ctype == NULL_TREE)
11291 ctype = current_class_type;
11293 if (ctype == NULL_TREE)
11295 error ("can't make `%D' into a method -- not in a class",
11296 declarator);
11297 return void_type_node;
11300 /* ``A union may [ ... ] not [ have ] virtual functions.''
11301 ARM 9.5 */
11302 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11304 error ("function `%D' declared virtual inside a union",
11305 declarator);
11306 return void_type_node;
11309 if (declarator == ansi_opname (NEW_EXPR)
11310 || declarator == ansi_opname (VEC_NEW_EXPR)
11311 || declarator == ansi_opname (DELETE_EXPR)
11312 || declarator == ansi_opname (VEC_DELETE_EXPR))
11314 if (virtualp)
11316 error ("`%D' cannot be declared virtual, since it is always static",
11317 declarator);
11318 virtualp = 0;
11321 else if (staticp < 2)
11322 type = build_method_type_directly (ctype,
11323 TREE_TYPE (type),
11324 TYPE_ARG_TYPES (type));
11327 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11328 function_context = (ctype != NULL_TREE) ?
11329 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11330 publicp = (! friendp || ! staticp)
11331 && function_context == NULL_TREE;
11332 decl = grokfndecl (ctype, type,
11333 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11334 ? declarator : dname,
11335 declarator,
11336 virtualp, flags, quals, raises,
11337 friendp ? -1 : 0, friendp, publicp, inlinep,
11338 funcdef_flag, template_count, in_namespace);
11339 if (decl == NULL_TREE)
11340 return decl;
11341 #if 0
11342 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11343 /* The decl and setting of decl_attr is also turned off. */
11344 decl = build_decl_attribute_variant (decl, decl_attr);
11345 #endif
11347 /* [class.conv.ctor]
11349 A constructor declared without the function-specifier
11350 explicit that can be called with a single parameter
11351 specifies a conversion from the type of its first
11352 parameter to the type of its class. Such a constructor
11353 is called a converting constructor. */
11354 if (explicitp == 2)
11355 DECL_NONCONVERTING_P (decl) = 1;
11356 else if (DECL_CONSTRUCTOR_P (decl))
11358 /* The constructor can be called with exactly one
11359 parameter if there is at least one parameter, and
11360 any subsequent parameters have default arguments.
11361 Ignore any compiler-added parms. */
11362 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11364 if (arg_types == void_list_node
11365 || (arg_types
11366 && TREE_CHAIN (arg_types)
11367 && TREE_CHAIN (arg_types) != void_list_node
11368 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11369 DECL_NONCONVERTING_P (decl) = 1;
11372 else if (TREE_CODE (type) == METHOD_TYPE)
11374 /* We only get here for friend declarations of
11375 members of other classes. */
11376 /* All method decls are public, so tell grokfndecl to set
11377 TREE_PUBLIC, also. */
11378 decl = grokfndecl (ctype, type,
11379 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11380 ? declarator : dname,
11381 declarator,
11382 virtualp, flags, quals, raises,
11383 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11384 template_count, in_namespace);
11385 if (decl == NULL_TREE)
11386 return NULL_TREE;
11388 else if (!staticp && ! processing_template_decl
11389 && !COMPLETE_TYPE_P (complete_type (type))
11390 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11392 if (declarator)
11393 error ("field `%D' has incomplete type", declarator);
11394 else
11395 error ("name `%T' has incomplete type", type);
11397 /* If we're instantiating a template, tell them which
11398 instantiation made the field's type be incomplete. */
11399 if (current_class_type
11400 && TYPE_NAME (current_class_type)
11401 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11402 && declspecs && TREE_VALUE (declspecs)
11403 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11404 error (" in instantiation of template `%T'",
11405 current_class_type);
11407 type = error_mark_node;
11408 decl = NULL_TREE;
11410 else
11412 if (friendp)
11414 error ("`%s' is neither function nor member function; cannot be declared friend",
11415 IDENTIFIER_POINTER (declarator));
11416 friendp = 0;
11418 decl = NULL_TREE;
11421 if (friendp)
11423 /* Friends are treated specially. */
11424 if (ctype == current_class_type)
11425 warning ("member functions are implicitly friends of their class");
11426 else if (decl && DECL_NAME (decl))
11428 if (template_class_depth (current_class_type) == 0)
11430 decl = check_explicit_specialization
11431 (declarator, decl, template_count,
11432 2 * (funcdef_flag != 0) + 4);
11433 if (decl == error_mark_node)
11434 return error_mark_node;
11437 decl = do_friend (ctype, declarator, decl,
11438 last_function_parms, *attrlist,
11439 flags, quals, funcdef_flag);
11440 return decl;
11442 else
11443 return void_type_node;
11446 /* Structure field. It may not be a function, except for C++ */
11448 if (decl == NULL_TREE)
11450 if (initialized)
11452 if (!staticp)
11454 /* An attempt is being made to initialize a non-static
11455 member. But, from [class.mem]:
11457 4 A member-declarator can contain a
11458 constant-initializer only if it declares a static
11459 member (_class.static_) of integral or enumeration
11460 type, see _class.static.data_.
11462 This used to be relatively common practice, but
11463 the rest of the compiler does not correctly
11464 handle the initialization unless the member is
11465 static so we make it static below. */
11466 pedwarn ("ISO C++ forbids initialization of member `%D'",
11467 declarator);
11468 pedwarn ("making `%D' static", declarator);
11469 staticp = 1;
11472 if (uses_template_parms (type))
11473 /* We'll check at instantiation time. */
11475 else if (check_static_variable_definition (declarator,
11476 type))
11477 /* If we just return the declaration, crashes
11478 will sometimes occur. We therefore return
11479 void_type_node, as if this was a friend
11480 declaration, to cause callers to completely
11481 ignore this declaration. */
11482 return void_type_node;
11485 if (staticp)
11487 /* [class.mem] forbids static data members with the
11488 same name as the enclosing class. Non-static data
11489 members are checked in check_field_decls. */
11490 if (constructor_name_p (declarator, current_class_type))
11491 pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11492 declarator);
11494 /* C++ allows static class members. All other work
11495 for this is done by grokfield. */
11496 decl = build_lang_decl (VAR_DECL, declarator, type);
11497 TREE_STATIC (decl) = 1;
11498 /* In class context, 'static' means public access. */
11499 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11501 else
11503 decl = build_decl (FIELD_DECL, declarator, type);
11504 DECL_NONADDRESSABLE_P (decl) = bitfield;
11505 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11507 DECL_MUTABLE_P (decl) = 1;
11508 RIDBIT_RESET (RID_MUTABLE, specbits);
11512 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11513 inlinep, friendp, raises != NULL_TREE);
11516 else if (TREE_CODE (type) == FUNCTION_TYPE
11517 || TREE_CODE (type) == METHOD_TYPE)
11519 tree original_name;
11520 int publicp = 0;
11522 if (! declarator)
11523 return NULL_TREE;
11525 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11526 original_name = dname;
11527 else
11528 original_name = declarator;
11530 if (RIDBIT_SETP (RID_AUTO, specbits))
11531 error ("storage class `auto' invalid for function `%s'", name);
11532 else if (RIDBIT_SETP (RID_REGISTER, specbits))
11533 error ("storage class `register' invalid for function `%s'", name);
11534 else if (RIDBIT_SETP (RID_THREAD, specbits))
11535 error ("storage class `__thread' invalid for function `%s'", name);
11537 /* Function declaration not at top level.
11538 Storage classes other than `extern' are not allowed
11539 and `extern' makes no difference. */
11540 if (! toplevel_bindings_p ()
11541 && (RIDBIT_SETP (RID_STATIC, specbits)
11542 || RIDBIT_SETP (RID_INLINE, specbits))
11543 && pedantic)
11545 if (RIDBIT_SETP (RID_STATIC, specbits))
11546 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11547 else
11548 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11551 if (ctype == NULL_TREE)
11553 if (virtualp)
11555 error ("virtual non-class function `%s'", name);
11556 virtualp = 0;
11559 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11560 type = build_method_type_directly (ctype,
11561 TREE_TYPE (type),
11562 TYPE_ARG_TYPES (type));
11564 /* Record presence of `static'. */
11565 publicp = (ctype != NULL_TREE
11566 || RIDBIT_SETP (RID_EXTERN, specbits)
11567 || !RIDBIT_SETP (RID_STATIC, specbits));
11569 decl = grokfndecl (ctype, type, original_name, declarator,
11570 virtualp, flags, quals, raises,
11571 1, friendp,
11572 publicp, inlinep, funcdef_flag,
11573 template_count, in_namespace);
11574 if (decl == NULL_TREE)
11575 return NULL_TREE;
11577 if (staticp == 1)
11579 int invalid_static = 0;
11581 /* Don't allow a static member function in a class, and forbid
11582 declaring main to be static. */
11583 if (TREE_CODE (type) == METHOD_TYPE)
11585 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11586 invalid_static = 1;
11588 else if (current_function_decl)
11590 /* FIXME need arm citation */
11591 error ("cannot declare static function inside another function");
11592 invalid_static = 1;
11595 if (invalid_static)
11597 staticp = 0;
11598 RIDBIT_RESET (RID_STATIC, specbits);
11602 else
11604 /* It's a variable. */
11606 /* An uninitialized decl with `extern' is a reference. */
11607 decl = grokvardecl (type, declarator, &specbits,
11608 initialized,
11609 (type_quals & TYPE_QUAL_CONST) != 0,
11610 ctype ? ctype : in_namespace);
11611 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11612 inlinep, friendp, raises != NULL_TREE);
11614 if (ctype)
11616 DECL_CONTEXT (decl) = ctype;
11617 if (staticp == 1)
11619 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11620 staticp = 0;
11621 RIDBIT_RESET (RID_STATIC, specbits);
11623 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11625 error ("static member `%D' declared `register'", decl);
11626 RIDBIT_RESET (RID_REGISTER, specbits);
11628 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11630 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11631 decl);
11632 RIDBIT_RESET (RID_EXTERN, specbits);
11637 my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11639 /* Record `register' declaration for warnings on &
11640 and in case doing stupid register allocation. */
11642 if (RIDBIT_SETP (RID_REGISTER, specbits))
11643 DECL_REGISTER (decl) = 1;
11645 if (RIDBIT_SETP (RID_EXTERN, specbits))
11646 DECL_THIS_EXTERN (decl) = 1;
11648 if (RIDBIT_SETP (RID_STATIC, specbits))
11649 DECL_THIS_STATIC (decl) = 1;
11651 /* Record constancy and volatility. There's no need to do this
11652 when processing a template; we'll do this for the instantiated
11653 declaration based on the type of DECL. */
11654 if (!processing_template_decl)
11655 c_apply_type_quals_to_decl (type_quals, decl);
11657 return decl;
11661 /* Subroutine of start_function. Ensure that each of the parameter
11662 types (as listed in PARMS) is complete, as is required for a
11663 function definition. */
11665 static void
11666 require_complete_types_for_parms (tree parms)
11668 for (; parms; parms = TREE_CHAIN (parms))
11670 if (VOID_TYPE_P (TREE_TYPE (parms)))
11671 /* grokparms will have already issued an error */
11672 TREE_TYPE (parms) = error_mark_node;
11673 else if (complete_type_or_else (TREE_TYPE (parms), parms))
11675 layout_decl (parms, 0);
11676 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11678 else
11679 TREE_TYPE (parms) = error_mark_node;
11683 /* Returns nonzero if T is a local variable. */
11686 local_variable_p (tree t)
11688 if ((TREE_CODE (t) == VAR_DECL
11689 /* A VAR_DECL with a context that is a _TYPE is a static data
11690 member. */
11691 && !TYPE_P (CP_DECL_CONTEXT (t))
11692 /* Any other non-local variable must be at namespace scope. */
11693 && !DECL_NAMESPACE_SCOPE_P (t))
11694 || (TREE_CODE (t) == PARM_DECL))
11695 return 1;
11697 return 0;
11700 /* Returns nonzero if T is an automatic local variable or a label.
11701 (These are the declarations that need to be remapped when the code
11702 containing them is duplicated.) */
11705 nonstatic_local_decl_p (tree t)
11707 return ((local_variable_p (t) && !TREE_STATIC (t))
11708 || TREE_CODE (t) == LABEL_DECL
11709 || TREE_CODE (t) == RESULT_DECL);
11712 /* Like local_variable_p, but suitable for use as a tree-walking
11713 function. */
11715 static tree
11716 local_variable_p_walkfn (tree* tp,
11717 int* walk_subtrees ATTRIBUTE_UNUSED ,
11718 void* data ATTRIBUTE_UNUSED )
11720 return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11721 ? *tp : NULL_TREE);
11724 /* Check that ARG, which is a default-argument expression for a
11725 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11726 something goes wrong. DECL may also be a _TYPE node, rather than a
11727 DECL, if there is no DECL available. */
11729 tree
11730 check_default_argument (tree decl, tree arg)
11732 tree var;
11733 tree decl_type;
11735 if (TREE_CODE (arg) == DEFAULT_ARG)
11736 /* We get a DEFAULT_ARG when looking at an in-class declaration
11737 with a default argument. Ignore the argument for now; we'll
11738 deal with it after the class is complete. */
11739 return arg;
11741 if (processing_template_decl || uses_template_parms (arg))
11742 /* We don't do anything checking until instantiation-time. Note
11743 that there may be uninstantiated arguments even for an
11744 instantiated function, since default arguments are not
11745 instantiated until they are needed. */
11746 return arg;
11748 if (TYPE_P (decl))
11750 decl_type = decl;
11751 decl = NULL_TREE;
11753 else
11754 decl_type = TREE_TYPE (decl);
11756 if (arg == error_mark_node
11757 || decl == error_mark_node
11758 || TREE_TYPE (arg) == error_mark_node
11759 || decl_type == error_mark_node)
11760 /* Something already went wrong. There's no need to check
11761 further. */
11762 return error_mark_node;
11764 /* [dcl.fct.default]
11766 A default argument expression is implicitly converted to the
11767 parameter type. */
11768 if (!TREE_TYPE (arg)
11769 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11771 if (decl)
11772 error ("default argument for `%#D' has type `%T'",
11773 decl, TREE_TYPE (arg));
11774 else
11775 error ("default argument for parameter of type `%T' has type `%T'",
11776 decl_type, TREE_TYPE (arg));
11778 return error_mark_node;
11781 /* [dcl.fct.default]
11783 Local variables shall not be used in default argument
11784 expressions.
11786 The keyword `this' shall not be used in a default argument of a
11787 member function. */
11788 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11789 NULL);
11790 if (var)
11792 error ("default argument `%E' uses local variable `%D'",
11793 arg, var);
11794 return error_mark_node;
11797 /* All is well. */
11798 return arg;
11801 /* Decode the list of parameter types for a function type.
11802 Given the list of things declared inside the parens,
11803 return a list of types.
11805 We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11806 flag. If unset, we append void_list_node. A parmlist declared
11807 as `(void)' is accepted as the empty parmlist.
11809 Also set last_function_parms to the chain of PARM_DECLs. */
11811 static tree
11812 grokparms (tree first_parm)
11814 tree result = NULL_TREE;
11815 tree decls = NULL_TREE;
11816 int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11817 tree parm, chain;
11818 int any_error = 0;
11820 my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11822 for (parm = first_parm; parm != NULL_TREE; parm = chain)
11824 tree type = NULL_TREE;
11825 tree decl = TREE_VALUE (parm);
11826 tree init = TREE_PURPOSE (parm);
11827 tree specs, attrs;
11829 chain = TREE_CHAIN (parm);
11830 /* @@ weak defense against parse errors. */
11831 if (TREE_CODE (decl) != VOID_TYPE
11832 && TREE_CODE (decl) != TREE_LIST)
11834 /* Give various messages as the need arises. */
11835 if (TREE_CODE (decl) == STRING_CST)
11836 error ("invalid string constant `%E'", decl);
11837 else if (TREE_CODE (decl) == INTEGER_CST)
11838 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11839 continue;
11842 if (parm == void_list_node)
11843 break;
11845 split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
11846 decl = grokdeclarator (TREE_VALUE (decl), specs,
11847 PARM, init != NULL_TREE, &attrs);
11848 if (! decl || TREE_TYPE (decl) == error_mark_node)
11849 continue;
11851 if (attrs)
11852 cplus_decl_attributes (&decl, attrs, 0);
11854 type = TREE_TYPE (decl);
11855 if (VOID_TYPE_P (type))
11857 if (same_type_p (type, void_type_node)
11858 && !DECL_NAME (decl) && !result && !chain && !ellipsis)
11859 /* this is a parmlist of `(void)', which is ok. */
11860 break;
11861 cxx_incomplete_type_error (decl, type);
11862 /* It's not a good idea to actually create parameters of
11863 type `void'; other parts of the compiler assume that a
11864 void type terminates the parameter list. */
11865 type = error_mark_node;
11866 TREE_TYPE (decl) = error_mark_node;
11869 if (type != error_mark_node)
11871 /* Top-level qualifiers on the parameters are
11872 ignored for function types. */
11873 type = cp_build_qualified_type (type, 0);
11874 if (TREE_CODE (type) == METHOD_TYPE)
11876 error ("parameter `%D' invalidly declared method type", decl);
11877 type = build_pointer_type (type);
11878 TREE_TYPE (decl) = type;
11880 else if (abstract_virtuals_error (decl, type))
11881 any_error = 1; /* Seems like a good idea. */
11882 else if (POINTER_TYPE_P (type))
11884 /* [dcl.fct]/6, parameter types cannot contain pointers
11885 (references) to arrays of unknown bound. */
11886 tree t = TREE_TYPE (type);
11887 int ptr = TYPE_PTR_P (type);
11889 while (1)
11891 if (TYPE_PTR_P (t))
11892 ptr = 1;
11893 else if (TREE_CODE (t) != ARRAY_TYPE)
11894 break;
11895 else if (!TYPE_DOMAIN (t))
11896 break;
11897 t = TREE_TYPE (t);
11899 if (TREE_CODE (t) == ARRAY_TYPE)
11900 error ("parameter `%D' includes %s to array of unknown bound `%T'",
11901 decl, ptr ? "pointer" : "reference", t);
11904 if (!any_error && init)
11905 init = check_default_argument (decl, init);
11906 else
11907 init = NULL_TREE;
11910 TREE_CHAIN (decl) = decls;
11911 decls = decl;
11912 result = tree_cons (init, type, result);
11914 decls = nreverse (decls);
11915 result = nreverse (result);
11916 if (!ellipsis)
11917 result = chainon (result, void_list_node);
11918 last_function_parms = decls;
11920 return result;
11924 /* D is a constructor or overloaded `operator='.
11926 Let T be the class in which D is declared. Then, this function
11927 returns:
11929 -1 if D's is an ill-formed constructor or copy assignment operator
11930 whose first parameter is of type `T'.
11931 0 if D is not a copy constructor or copy assignment
11932 operator.
11933 1 if D is a copy constructor or copy assignment operator whose
11934 first parameter is a reference to const qualified T.
11935 2 if D is a copy constructor or copy assignment operator whose
11936 first parameter is a reference to non-const qualified T.
11938 This function can be used as a predicate. Positive values indicate
11939 a copy constructor and nonzero values indicate a copy assignment
11940 operator. */
11943 copy_fn_p (tree d)
11945 tree args;
11946 tree arg_type;
11947 int result = 1;
11949 my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
11951 if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
11952 /* Instantiations of template member functions are never copy
11953 functions. Note that member functions of templated classes are
11954 represented as template functions internally, and we must
11955 accept those as copy functions. */
11956 return 0;
11958 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11959 if (!args)
11960 return 0;
11962 arg_type = TREE_VALUE (args);
11964 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
11966 /* Pass by value copy assignment operator. */
11967 result = -1;
11969 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
11970 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
11972 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
11973 result = 2;
11975 else
11976 return 0;
11978 args = TREE_CHAIN (args);
11980 if (args && args != void_list_node && !TREE_PURPOSE (args))
11981 /* There are more non-optional args. */
11982 return 0;
11984 return result;
11987 /* Remember any special properties of member function DECL. */
11989 void grok_special_member_properties (tree decl)
11991 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
11992 ; /* Not special. */
11993 else if (DECL_CONSTRUCTOR_P (decl))
11995 int ctor = copy_fn_p (decl);
11997 if (ctor > 0)
11999 /* [class.copy]
12001 A non-template constructor for class X is a copy
12002 constructor if its first parameter is of type X&, const
12003 X&, volatile X& or const volatile X&, and either there
12004 are no other parameters or else all other parameters have
12005 default arguments. */
12006 TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12007 if (ctor > 1)
12008 TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12010 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12011 TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12013 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12015 /* [class.copy]
12017 A non-template assignment operator for class X is a copy
12018 assignment operator if its parameter is of type X, X&, const
12019 X&, volatile X& or const volatile X&. */
12021 int assop = copy_fn_p (decl);
12023 if (assop)
12025 TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12026 if (assop != 1)
12027 TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12028 if (DECL_PURE_VIRTUAL_P (decl))
12029 TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12034 /* Check a constructor DECL has the correct form. Complains
12035 if the class has a constructor of the form X(X). */
12038 grok_ctor_properties (tree ctype, tree decl)
12040 int ctor_parm = copy_fn_p (decl);
12042 if (ctor_parm < 0)
12044 /* [class.copy]
12046 A declaration of a constructor for a class X is ill-formed if
12047 its first parameter is of type (optionally cv-qualified) X
12048 and either there are no other parameters or else all other
12049 parameters have default arguments.
12051 We *don't* complain about member template instantiations that
12052 have this form, though; they can occur as we try to decide
12053 what constructor to use during overload resolution. Since
12054 overload resolution will never prefer such a constructor to
12055 the non-template copy constructor (which is either explicitly
12056 or implicitly defined), there's no need to worry about their
12057 existence. Theoretically, they should never even be
12058 instantiated, but that's hard to forestall. */
12059 error ("invalid constructor; you probably meant `%T (const %T&)'",
12060 ctype, ctype);
12061 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12062 return 0;
12065 return 1;
12068 /* An operator with this code is unary, but can also be binary. */
12070 static int
12071 ambi_op_p (enum tree_code code)
12073 return (code == INDIRECT_REF
12074 || code == ADDR_EXPR
12075 || code == CONVERT_EXPR
12076 || code == NEGATE_EXPR
12077 || code == PREINCREMENT_EXPR
12078 || code == PREDECREMENT_EXPR);
12081 /* An operator with this name can only be unary. */
12083 static int
12084 unary_op_p (enum tree_code code)
12086 return (code == TRUTH_NOT_EXPR
12087 || code == BIT_NOT_EXPR
12088 || code == COMPONENT_REF
12089 || code == TYPE_EXPR);
12092 /* DECL is a declaration for an overloaded operator. Returns true if
12093 the declaration is valid; false otherwise. If COMPLAIN is true,
12094 errors are issued for invalid declarations. */
12096 bool
12097 grok_op_properties (tree decl, int friendp, bool complain)
12099 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12100 tree argtype;
12101 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12102 tree name = DECL_NAME (decl);
12103 enum tree_code operator_code;
12104 int arity;
12105 bool ok;
12107 /* Assume that the declaration is valid. */
12108 ok = true;
12110 /* Count the number of arguments. */
12111 for (argtype = argtypes, arity = 0;
12112 argtype && argtype != void_list_node;
12113 argtype = TREE_CHAIN (argtype))
12114 ++arity;
12116 if (current_class_type == NULL_TREE)
12117 friendp = 1;
12119 if (DECL_CONV_FN_P (decl))
12120 operator_code = TYPE_EXPR;
12121 else
12124 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12125 if (ansi_opname (CODE) == name) \
12127 operator_code = (CODE); \
12128 break; \
12130 else if (ansi_assopname (CODE) == name) \
12132 operator_code = (CODE); \
12133 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12134 break; \
12137 #include "operators.def"
12138 #undef DEF_OPERATOR
12140 abort ();
12142 while (0);
12143 my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12144 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12146 if (! friendp)
12148 switch (operator_code)
12150 case NEW_EXPR:
12151 TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12152 break;
12154 case DELETE_EXPR:
12155 TYPE_GETS_DELETE (current_class_type) |= 1;
12156 break;
12158 case VEC_NEW_EXPR:
12159 TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12160 break;
12162 case VEC_DELETE_EXPR:
12163 TYPE_GETS_DELETE (current_class_type) |= 2;
12164 break;
12166 default:
12167 break;
12171 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12173 /* When the compiler encounters the definition of A::operator new, it
12174 doesn't look at the class declaration to find out if it's static. */
12175 if (methodp)
12176 revert_static_member_fn (decl);
12178 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12180 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12182 if (methodp)
12183 revert_static_member_fn (decl);
12185 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12187 else
12189 /* An operator function must either be a non-static member function
12190 or have at least one parameter of a class, a reference to a class,
12191 an enumeration, or a reference to an enumeration. 13.4.0.6 */
12192 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12194 if (operator_code == TYPE_EXPR
12195 || operator_code == CALL_EXPR
12196 || operator_code == COMPONENT_REF
12197 || operator_code == ARRAY_REF
12198 || operator_code == NOP_EXPR)
12199 error ("`%D' must be a nonstatic member function", decl);
12200 else
12202 tree p;
12204 if (DECL_STATIC_FUNCTION_P (decl))
12205 error ("`%D' must be either a non-static member function or a non-member function", decl);
12207 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
12209 tree arg = non_reference (TREE_VALUE (p));
12210 /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used
12211 because these checks are performed even on
12212 template functions. */
12213 if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE)
12214 break;
12217 if (!p || p == void_list_node)
12219 if (!complain)
12220 return false;
12222 error ("`%D' must have an argument of class or "
12223 "enumerated type",
12224 decl);
12225 ok = false;
12230 /* There are no restrictions on the arguments to an overloaded
12231 "operator ()". */
12232 if (operator_code == CALL_EXPR)
12233 return ok;
12235 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12237 tree t = TREE_TYPE (name);
12238 if (! friendp)
12240 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12241 const char *what = 0;
12243 if (ref)
12244 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12246 if (TREE_CODE (t) == VOID_TYPE)
12247 what = "void";
12248 else if (t == current_class_type)
12249 what = "the same type";
12250 /* Don't force t to be complete here. */
12251 else if (IS_AGGR_TYPE (t)
12252 && COMPLETE_TYPE_P (t)
12253 && DERIVED_FROM_P (t, current_class_type))
12254 what = "a base class";
12256 if (what && warn_conversion)
12257 warning ("conversion to %s%s will never use a type conversion operator",
12258 ref ? "a reference to " : "", what);
12261 if (operator_code == COND_EXPR)
12263 /* 13.4.0.3 */
12264 error ("ISO C++ prohibits overloading operator ?:");
12266 else if (ambi_op_p (operator_code))
12268 if (arity == 1)
12269 /* We pick the one-argument operator codes by default, so
12270 we don't have to change anything. */
12272 else if (arity == 2)
12274 /* If we thought this was a unary operator, we now know
12275 it to be a binary operator. */
12276 switch (operator_code)
12278 case INDIRECT_REF:
12279 operator_code = MULT_EXPR;
12280 break;
12282 case ADDR_EXPR:
12283 operator_code = BIT_AND_EXPR;
12284 break;
12286 case CONVERT_EXPR:
12287 operator_code = PLUS_EXPR;
12288 break;
12290 case NEGATE_EXPR:
12291 operator_code = MINUS_EXPR;
12292 break;
12294 case PREINCREMENT_EXPR:
12295 operator_code = POSTINCREMENT_EXPR;
12296 break;
12298 case PREDECREMENT_EXPR:
12299 operator_code = POSTDECREMENT_EXPR;
12300 break;
12302 default:
12303 abort ();
12306 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12308 if ((operator_code == POSTINCREMENT_EXPR
12309 || operator_code == POSTDECREMENT_EXPR)
12310 && ! processing_template_decl
12311 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12313 if (methodp)
12314 error ("postfix `%D' must take `int' as its argument",
12315 decl);
12316 else
12317 error
12318 ("postfix `%D' must take `int' as its second argument",
12319 decl);
12322 else
12324 if (methodp)
12325 error ("`%D' must take either zero or one argument", decl);
12326 else
12327 error ("`%D' must take either one or two arguments", decl);
12330 /* More Effective C++ rule 6. */
12331 if (warn_ecpp
12332 && (operator_code == POSTINCREMENT_EXPR
12333 || operator_code == POSTDECREMENT_EXPR
12334 || operator_code == PREINCREMENT_EXPR
12335 || operator_code == PREDECREMENT_EXPR))
12337 tree arg = TREE_VALUE (argtypes);
12338 tree ret = TREE_TYPE (TREE_TYPE (decl));
12339 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12340 arg = TREE_TYPE (arg);
12341 arg = TYPE_MAIN_VARIANT (arg);
12342 if (operator_code == PREINCREMENT_EXPR
12343 || operator_code == PREDECREMENT_EXPR)
12345 if (TREE_CODE (ret) != REFERENCE_TYPE
12346 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12347 arg))
12348 warning ("prefix `%D' should return `%T'", decl,
12349 build_reference_type (arg));
12351 else
12353 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12354 warning ("postfix `%D' should return `%T'", decl, arg);
12358 else if (unary_op_p (operator_code))
12360 if (arity != 1)
12362 if (methodp)
12363 error ("`%D' must take `void'", decl);
12364 else
12365 error ("`%D' must take exactly one argument", decl);
12368 else /* if (binary_op_p (operator_code)) */
12370 if (arity != 2)
12372 if (methodp)
12373 error ("`%D' must take exactly one argument", decl);
12374 else
12375 error ("`%D' must take exactly two arguments", decl);
12378 /* More Effective C++ rule 7. */
12379 if (warn_ecpp
12380 && (operator_code == TRUTH_ANDIF_EXPR
12381 || operator_code == TRUTH_ORIF_EXPR
12382 || operator_code == COMPOUND_EXPR))
12383 warning ("user-defined `%D' always evaluates both arguments",
12384 decl);
12387 /* Effective C++ rule 23. */
12388 if (warn_ecpp
12389 && arity == 2
12390 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12391 && (operator_code == PLUS_EXPR
12392 || operator_code == MINUS_EXPR
12393 || operator_code == TRUNC_DIV_EXPR
12394 || operator_code == MULT_EXPR
12395 || operator_code == TRUNC_MOD_EXPR)
12396 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12397 warning ("`%D' should return by value", decl);
12399 /* [over.oper]/8 */
12400 for (; argtypes && argtypes != void_list_node;
12401 argtypes = TREE_CHAIN (argtypes))
12402 if (TREE_PURPOSE (argtypes))
12404 TREE_PURPOSE (argtypes) = NULL_TREE;
12405 if (operator_code == POSTINCREMENT_EXPR
12406 || operator_code == POSTDECREMENT_EXPR)
12408 if (pedantic)
12409 pedwarn ("`%D' cannot have default arguments", decl);
12411 else
12412 error ("`%D' cannot have default arguments", decl);
12417 return ok;
12420 static const char *
12421 tag_name (enum tag_types code)
12423 switch (code)
12425 case record_type:
12426 return "struct";
12427 case class_type:
12428 return "class";
12429 case union_type:
12430 return "union ";
12431 case enum_type:
12432 return "enum";
12433 default:
12434 abort ();
12438 /* Name lookup in an elaborated-type-specifier (after the keyword
12439 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
12440 elaborated-type-specifier is invalid, issue a diagnostic and return
12441 error_mark_node; otherwise, return the *_TYPE to which it referred.
12442 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
12444 tree
12445 check_elaborated_type_specifier (enum tag_types tag_code,
12446 tree decl,
12447 bool allow_template_p)
12449 tree type;
12451 /* In the case of:
12453 struct S { struct S *p; };
12455 name lookup will find the TYPE_DECL for the implicit "S::S"
12456 typedef. Adjust for that here. */
12457 if (DECL_SELF_REFERENCE_P (decl))
12458 decl = TYPE_NAME (TREE_TYPE (decl));
12460 type = TREE_TYPE (decl);
12462 /* [dcl.type.elab]
12464 If the identifier resolves to a typedef-name or a template
12465 type-parameter, the elaborated-type-specifier is ill-formed.
12467 In other words, the only legitimate declaration to use in the
12468 elaborated type specifier is the implicit typedef created when
12469 the type is declared. */
12470 if (!DECL_IMPLICIT_TYPEDEF_P (decl))
12472 error ("using typedef-name `%D' after `%s'", decl, tag_name (tag_code));
12473 return IS_AGGR_TYPE (type) ? type : error_mark_node;
12476 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12478 error ("using template type parameter `%T' after `%s'",
12479 type, tag_name (tag_code));
12480 return error_mark_node;
12482 else if (TREE_CODE (type) != RECORD_TYPE
12483 && TREE_CODE (type) != UNION_TYPE
12484 && tag_code != enum_type)
12486 error ("`%T' referred to as `%s'", type, tag_name (tag_code));
12487 return error_mark_node;
12489 else if (TREE_CODE (type) != ENUMERAL_TYPE
12490 && tag_code == enum_type)
12492 error ("`%T' referred to as enum", type);
12493 return error_mark_node;
12495 else if (!allow_template_p
12496 && TREE_CODE (type) == RECORD_TYPE
12497 && CLASSTYPE_IS_TEMPLATE (type))
12499 /* If a class template appears as elaborated type specifier
12500 without a template header such as:
12502 template <class T> class C {};
12503 void f(class C); // No template header here
12505 then the required template argument is missing. */
12507 error ("template argument required for `%s %T'",
12508 tag_name (tag_code),
12509 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
12510 return error_mark_node;
12513 return type;
12516 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
12517 Define the tag as a forward-reference if it is not defined.
12519 If a declaration is given, process it here, and report an error if
12520 multiple declarations are not identical. ATTRIBUTE is the attribute
12521 appeared in this declaration.
12523 GLOBALIZE is false when this is also a definition. Only look in
12524 the current frame for the name (since C++ allows new names in any
12525 scope.)
12527 TEMPLATE_HEADER_P is true when this declaration is preceded by
12528 a set of template parameters. */
12530 tree
12531 xref_tag (enum tag_types tag_code, tree name, tree attributes,
12532 bool globalize, bool template_header_p)
12534 enum tree_code code;
12535 register tree t;
12536 struct cp_binding_level *b = current_binding_level;
12537 tree context = NULL_TREE;
12539 timevar_push (TV_NAME_LOOKUP);
12541 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
12543 switch (tag_code)
12545 case record_type:
12546 case class_type:
12547 code = RECORD_TYPE;
12548 break;
12549 case union_type:
12550 code = UNION_TYPE;
12551 break;
12552 case enum_type:
12553 code = ENUMERAL_TYPE;
12554 break;
12555 default:
12556 abort ();
12559 if (! globalize)
12561 /* If we know we are defining this tag, only look it up in
12562 this scope and don't try to find it as a type. */
12563 t = lookup_tag (code, name, b, 1);
12565 else
12567 tree decl = lookup_name (name, 1);
12569 if (decl && DECL_CLASS_TEMPLATE_P (decl))
12570 decl = DECL_TEMPLATE_RESULT (decl);
12572 if (decl && TREE_CODE (decl) == TYPE_DECL)
12574 /* Two cases we need to consider when deciding if a class
12575 template is allowed as an elaborated type specifier:
12576 1. It is a self reference to its own class.
12577 2. It comes with a template header.
12579 For example:
12581 template <class T> class C {
12582 class C *c1; // DECL_SELF_REFERENCE_P is true
12583 class D;
12585 template <class U> class C; // template_header_p is true
12586 template <class T> class C<T>::D {
12587 class C *c2; // DECL_SELF_REFERENCE_P is true
12588 }; */
12590 t = check_elaborated_type_specifier (tag_code,
12591 decl,
12592 template_header_p
12593 | DECL_SELF_REFERENCE_P (decl));
12594 if (t == error_mark_node)
12595 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12597 else
12598 t = NULL_TREE;
12600 if (t && current_class_type
12601 && template_class_depth (current_class_type)
12602 && template_header_p)
12604 /* Since GLOBALIZE is nonzero, we are not looking at a
12605 definition of this tag. Since, in addition, we are currently
12606 processing a (member) template declaration of a template
12607 class, we must be very careful; consider:
12609 template <class X>
12610 struct S1
12612 template <class U>
12613 struct S2
12614 { template <class V>
12615 friend struct S1; };
12617 Here, the S2::S1 declaration should not be confused with the
12618 outer declaration. In particular, the inner version should
12619 have a template parameter of level 2, not level 1. This
12620 would be particularly important if the member declaration
12621 were instead:
12623 template <class V = U> friend struct S1;
12625 say, when we should tsubst into `U' when instantiating
12626 S2. On the other hand, when presented with:
12628 template <class T>
12629 struct S1 {
12630 template <class U>
12631 struct S2 {};
12632 template <class U>
12633 friend struct S2;
12636 we must find the inner binding eventually. We
12637 accomplish this by making sure that the new type we
12638 create to represent this declaration has the right
12639 TYPE_CONTEXT. */
12640 context = TYPE_CONTEXT (t);
12641 t = NULL_TREE;
12645 if (! t)
12647 /* If no such tag is yet defined, create a forward-reference node
12648 and record it as the "definition".
12649 When a real declaration of this type is found,
12650 the forward-reference will be altered into a real type. */
12651 if (code == ENUMERAL_TYPE)
12653 error ("use of enum `%#D' without previous declaration", name);
12655 t = make_node (ENUMERAL_TYPE);
12657 /* Give the type a default layout like unsigned int
12658 to avoid crashing if it does not get defined. */
12659 TYPE_MODE (t) = TYPE_MODE (unsigned_type_node);
12660 TYPE_ALIGN (t) = TYPE_ALIGN (unsigned_type_node);
12661 TYPE_USER_ALIGN (t) = 0;
12662 TREE_UNSIGNED (t) = 1;
12663 TYPE_PRECISION (t) = TYPE_PRECISION (unsigned_type_node);
12664 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (unsigned_type_node);
12665 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (unsigned_type_node);
12667 /* Enable us to recognize when a type is created in class context.
12668 To do nested classes correctly, this should probably be cleared
12669 out when we leave this classes scope. Currently this in only
12670 done in `start_enum'. */
12672 pushtag (name, t, globalize);
12674 else
12676 t = make_aggr_type (code);
12677 TYPE_CONTEXT (t) = context;
12678 pushtag (name, t, globalize);
12681 else
12683 if (!globalize && processing_template_decl && IS_AGGR_TYPE (t))
12684 redeclare_class_template (t, current_template_parms);
12687 TYPE_ATTRIBUTES (t) = attributes;
12689 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
12692 tree
12693 xref_tag_from_type (tree old, tree id, int globalize)
12695 enum tag_types tag_kind;
12697 if (TREE_CODE (old) == RECORD_TYPE)
12698 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12699 else
12700 tag_kind = union_type;
12702 if (id == NULL_TREE)
12703 id = TYPE_IDENTIFIER (old);
12705 return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize, false);
12708 /* REF is a type (named NAME), for which we have just seen some
12709 baseclasses. BASE_LIST is a list of those baseclasses; the
12710 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12711 the base-class. TREE_VIA_VIRTUAL indicates virtual
12712 inheritance. CODE_TYPE_NODE indicates whether REF is a class,
12713 struct, or union. */
12715 void
12716 xref_basetypes (tree ref, tree base_list)
12718 /* In the declaration `A : X, Y, ... Z' we mark all the types
12719 (A, X, Y, ..., Z) so we can check for duplicates. */
12720 tree *basep;
12722 int i;
12723 enum tag_types tag_code;
12725 if (ref == error_mark_node)
12726 return;
12728 if (TREE_CODE (ref) == UNION_TYPE)
12730 error ("derived union `%T' invalid", ref);
12731 return;
12734 tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12736 /* First, make sure that any templates in base-classes are
12737 instantiated. This ensures that if we call ourselves recursively
12738 we do not get confused about which classes are marked and which
12739 are not. */
12740 basep = &base_list;
12741 while (*basep)
12743 tree basetype = TREE_VALUE (*basep);
12744 if (!(processing_template_decl && uses_template_parms (basetype))
12745 && !complete_type_or_else (basetype, NULL))
12746 /* An incomplete type. Remove it from the list. */
12747 *basep = TREE_CHAIN (*basep);
12748 else
12749 basep = &TREE_CHAIN (*basep);
12752 SET_CLASSTYPE_MARKED (ref);
12753 i = list_length (base_list);
12754 if (i)
12756 tree binfo = TYPE_BINFO (ref);
12757 tree binfos = make_tree_vec (i);
12758 tree accesses = make_tree_vec (i);
12760 BINFO_BASETYPES (binfo) = binfos;
12761 BINFO_BASEACCESSES (binfo) = accesses;
12763 for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
12765 tree access = TREE_PURPOSE (base_list);
12766 int via_virtual = TREE_VIA_VIRTUAL (base_list);
12767 tree basetype = TREE_VALUE (base_list);
12768 tree base_binfo;
12770 if (access == access_default_node)
12771 /* The base of a derived struct is public by default. */
12772 access = (tag_code == class_type
12773 ? access_private_node : access_public_node);
12775 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12776 basetype = TREE_TYPE (basetype);
12777 if (!basetype
12778 || (TREE_CODE (basetype) != RECORD_TYPE
12779 && TREE_CODE (basetype) != TYPENAME_TYPE
12780 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12781 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12783 error ("base type `%T' fails to be a struct or class type",
12784 basetype);
12785 continue;
12788 if (CLASSTYPE_MARKED (basetype))
12790 if (basetype == ref)
12791 error ("recursive type `%T' undefined", basetype);
12792 else
12793 error ("duplicate base type `%T' invalid", basetype);
12794 continue;
12797 if (TYPE_FOR_JAVA (basetype)
12798 && (current_lang_depth () == 0))
12799 TYPE_FOR_JAVA (ref) = 1;
12801 if (CLASS_TYPE_P (basetype))
12803 base_binfo = TYPE_BINFO (basetype);
12804 /* This flag will be in the binfo of the base type, we must
12805 clear it after copying the base binfos. */
12806 BINFO_DEPENDENT_BASE_P (base_binfo)
12807 = dependent_type_p (basetype);
12809 else
12810 base_binfo = make_binfo (size_zero_node, basetype,
12811 NULL_TREE, NULL_TREE);
12813 TREE_VEC_ELT (binfos, i) = base_binfo;
12814 TREE_VEC_ELT (accesses, i) = access;
12815 /* This flag will be in the binfo of the base type, we must
12816 clear it after copying the base binfos. */
12817 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12819 SET_CLASSTYPE_MARKED (basetype);
12821 /* We are free to modify these bits because they are meaningless
12822 at top level, and BASETYPE is a top-level type. */
12823 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12825 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12826 /* Converting to a virtual base class requires looking
12827 up the offset of the virtual base. */
12828 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12831 if (CLASS_TYPE_P (basetype))
12833 TYPE_HAS_NEW_OPERATOR (ref)
12834 |= TYPE_HAS_NEW_OPERATOR (basetype);
12835 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12836 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12837 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12838 /* If the base-class uses multiple inheritance, so do we. */
12839 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12840 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12841 /* Likewise, if converting to a base of the base may require
12842 code, then we may need to generate code to convert to a
12843 base as well. */
12844 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12845 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12847 i++;
12849 if (i)
12850 TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
12851 else
12852 BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
12854 if (i > 1)
12856 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12857 /* If there is more than one non-empty they cannot be at the same
12858 address. */
12859 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12863 /* Copy the base binfos, collect the virtual bases and set the
12864 inheritance order chain. */
12865 copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
12866 CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
12868 /* Unmark all the types. */
12869 while (i--)
12871 tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
12873 CLEAR_CLASSTYPE_MARKED (basetype);
12874 if (CLASS_TYPE_P (basetype))
12876 TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
12877 BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
12880 CLEAR_CLASSTYPE_MARKED (ref);
12884 /* Begin compiling the definition of an enumeration type.
12885 NAME is its name (or null if anonymous).
12886 Returns the type object, as yet incomplete.
12887 Also records info about it so that build_enumerator
12888 may be used to declare the individual values as they are read. */
12890 tree
12891 start_enum (tree name)
12893 register tree enumtype = NULL_TREE;
12894 struct cp_binding_level *b = current_binding_level;
12896 /* If this is the real definition for a previous forward reference,
12897 fill in the contents in the same object that used to be the
12898 forward reference. */
12900 if (name != NULL_TREE)
12901 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12903 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
12905 error ("multiple definition of `%#T'", enumtype);
12906 error ("%Hprevious definition here",
12907 &DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)));
12908 /* Clear out TYPE_VALUES, and start again. */
12909 TYPE_VALUES (enumtype) = NULL_TREE;
12911 else
12913 enumtype = make_node (ENUMERAL_TYPE);
12914 pushtag (name, enumtype, 0);
12917 return enumtype;
12920 /* After processing and defining all the values of an enumeration type,
12921 install their decls in the enumeration type and finish it off.
12922 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
12924 void
12925 finish_enum (tree enumtype)
12927 tree values;
12928 tree decl;
12929 tree value;
12930 tree minnode;
12931 tree maxnode;
12932 tree t;
12933 bool unsignedp;
12934 int lowprec;
12935 int highprec;
12936 int precision;
12937 integer_type_kind itk;
12938 tree underlying_type = NULL_TREE;
12940 /* We built up the VALUES in reverse order. */
12941 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
12943 /* For an enum defined in a template, just set the type of the values;
12944 all further processing is postponed until the template is
12945 instantiated. We need to set the type so that tsubst of a CONST_DECL
12946 works. */
12947 if (processing_template_decl)
12949 for (values = TYPE_VALUES (enumtype);
12950 values;
12951 values = TREE_CHAIN (values))
12952 TREE_TYPE (TREE_VALUE (values)) = enumtype;
12953 if (at_function_scope_p ())
12954 add_stmt (build_min (TAG_DEFN, enumtype));
12955 return;
12958 /* Determine the minimum and maximum values of the enumerators. */
12959 if (TYPE_VALUES (enumtype))
12961 minnode = maxnode = NULL_TREE;
12963 for (values = TYPE_VALUES (enumtype);
12964 values;
12965 values = TREE_CHAIN (values))
12967 decl = TREE_VALUE (values);
12969 /* [dcl.enum]: Following the closing brace of an enum-specifier,
12970 each enumerator has the type of its enumeration. Prior to the
12971 closing brace, the type of each enumerator is the type of its
12972 initializing value. */
12973 TREE_TYPE (decl) = enumtype;
12975 /* Update the minimum and maximum values, if appropriate. */
12976 value = DECL_INITIAL (decl);
12977 /* Figure out what the minimum and maximum values of the
12978 enumerators are. */
12979 if (!minnode)
12980 minnode = maxnode = value;
12981 else if (tree_int_cst_lt (maxnode, value))
12982 maxnode = value;
12983 else if (tree_int_cst_lt (value, minnode))
12984 minnode = value;
12986 /* Set the TREE_TYPE for the values as well. That's so that when
12987 we call decl_constant_value we get an entity of the right type
12988 (but with the constant value). But first make a copy so we
12989 don't clobber shared INTEGER_CSTs. */
12990 if (TREE_TYPE (value) != enumtype)
12992 value = DECL_INITIAL (decl) = copy_node (value);
12993 TREE_TYPE (value) = enumtype;
12997 else
12998 /* [dcl.enum]
13000 If the enumerator-list is empty, the underlying type is as if
13001 the enumeration had a single enumerator with value 0. */
13002 minnode = maxnode = integer_zero_node;
13004 /* Compute the number of bits require to represent all values of the
13005 enumeration. We must do this before the type of MINNODE and
13006 MAXNODE are transformed, since min_precision relies on the
13007 TREE_TYPE of the value it is passed. */
13008 unsignedp = tree_int_cst_sgn (minnode) >= 0;
13009 lowprec = min_precision (minnode, unsignedp);
13010 highprec = min_precision (maxnode, unsignedp);
13011 precision = MAX (lowprec, highprec);
13013 /* Determine the underlying type of the enumeration.
13015 [dcl.enum]
13017 The underlying type of an enumeration is an integral type that
13018 can represent all the enumerator values defined in the
13019 enumeration. It is implementation-defined which integral type is
13020 used as the underlying type for an enumeration except that the
13021 underlying type shall not be larger than int unless the value of
13022 an enumerator cannot fit in an int or unsigned int.
13024 We use "int" or an "unsigned int" as the underlying type, even if
13025 a smaller integral type would work, unless the user has
13026 explicitly requested that we use the smallest possible type. */
13027 for (itk = (flag_short_enums ? itk_char : itk_int);
13028 itk != itk_none;
13029 itk++)
13031 underlying_type = integer_types[itk];
13032 if (TYPE_PRECISION (underlying_type) >= precision
13033 && TREE_UNSIGNED (underlying_type) == unsignedp)
13034 break;
13036 if (itk == itk_none)
13038 /* DR 377
13040 IF no integral type can represent all the enumerator values, the
13041 enumeration is ill-formed. */
13042 error ("no integral type can represent all of the enumerator values "
13043 "for `%T'", enumtype);
13044 precision = TYPE_PRECISION (long_long_integer_type_node);
13045 underlying_type = integer_types[itk_unsigned_long_long];
13048 /* Compute the minium and maximum values for the type.
13050 [dcl.enum]
13052 For an enumeration where emin is the smallest enumerator and emax
13053 is the largest, the values of the enumeration are the values of the
13054 underlying type in the range bmin to bmax, where bmin and bmax are,
13055 respectively, the smallest and largest values of the smallest bit-
13056 field that can store emin and emax. */
13057 TYPE_PRECISION (enumtype) = precision;
13058 set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp);
13060 /* [dcl.enum]
13062 The value of sizeof() applied to an enumeration type, an object
13063 of an enumeration type, or an enumerator, is the value of sizeof()
13064 applied to the underlying type. */
13065 TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
13066 TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
13067 TYPE_MODE (enumtype) = TYPE_MODE (underlying_type);
13068 TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
13069 TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
13070 TREE_UNSIGNED (enumtype) = TREE_UNSIGNED (underlying_type);
13072 /* Convert each of the enumerators to the type of the underlying
13073 type of the enumeration. */
13074 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
13076 decl = TREE_VALUE (values);
13077 value = perform_implicit_conversion (underlying_type,
13078 DECL_INITIAL (decl));
13079 TREE_TYPE (value) = enumtype;
13080 DECL_INITIAL (decl) = value;
13081 TREE_VALUE (values) = value;
13084 /* Fix up all variant types of this enum type. */
13085 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13087 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13088 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13089 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13090 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13091 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13092 TYPE_MODE (t) = TYPE_MODE (enumtype);
13093 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13094 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13095 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13096 TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13099 /* Finish debugging output for this type. */
13100 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13103 /* Build and install a CONST_DECL for an enumeration constant of the
13104 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13105 Assignment of sequential values by default is handled here. */
13107 void
13108 build_enumerator (tree name, tree value, tree enumtype)
13110 tree decl;
13111 tree context;
13112 tree type;
13114 /* Remove no-op casts from the value. */
13115 if (value)
13116 STRIP_TYPE_NOPS (value);
13118 if (! processing_template_decl)
13120 /* Validate and default VALUE. */
13121 if (value != NULL_TREE)
13123 value = decl_constant_value (value);
13125 if (TREE_CODE (value) == INTEGER_CST)
13127 value = perform_integral_promotions (value);
13128 constant_expression_warning (value);
13130 else
13132 error ("enumerator value for `%D' not integer constant", name);
13133 value = NULL_TREE;
13137 /* Default based on previous value. */
13138 if (value == NULL_TREE)
13140 tree prev_value;
13142 if (TYPE_VALUES (enumtype))
13144 /* The next value is the previous value ... */
13145 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13146 /* ... plus one. */
13147 value = cp_build_binary_op (PLUS_EXPR,
13148 prev_value,
13149 integer_one_node);
13151 if (tree_int_cst_lt (value, prev_value))
13152 error ("overflow in enumeration values at `%D'", name);
13154 else
13155 value = integer_zero_node;
13158 /* Remove no-op casts from the value. */
13159 STRIP_TYPE_NOPS (value);
13162 /* C++ associates enums with global, function, or class declarations. */
13163 context = current_scope ();
13164 if (!context)
13165 context = current_namespace;
13167 /* Build the actual enumeration constant. Note that the enumeration
13168 constants have the type of their initializers until the
13169 enumeration is complete:
13171 [ dcl.enum ]
13173 Following the closing brace of an enum-specifier, each enumer-
13174 ator has the type of its enumeration. Prior to the closing
13175 brace, the type of each enumerator is the type of its
13176 initializing value.
13178 In finish_enum we will reset the type. Of course, if we're
13179 processing a template, there may be no value. */
13180 type = value ? TREE_TYPE (value) : NULL_TREE;
13182 if (context && context == current_class_type)
13183 /* This enum declaration is local to the class. We need the full
13184 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
13185 decl = build_lang_decl (CONST_DECL, name, type);
13186 else
13187 /* It's a global enum, or it's local to a function. (Note local to
13188 a function could mean local to a class method. */
13189 decl = build_decl (CONST_DECL, name, type);
13191 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13192 TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
13193 DECL_INITIAL (decl) = value;
13195 if (context && context == current_class_type)
13196 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13197 on the TYPE_FIELDS list for `S'. (That's so that you can say
13198 things like `S::i' later.) */
13199 finish_member_declaration (decl);
13200 else
13201 pushdecl (decl);
13203 /* Add this enumeration constant to the list for this type. */
13204 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13208 /* We're defining DECL. Make sure that it's type is OK. */
13210 static void
13211 check_function_type (tree decl, tree current_function_parms)
13213 tree fntype = TREE_TYPE (decl);
13214 tree return_type = complete_type (TREE_TYPE (fntype));
13216 /* In a function definition, arg types must be complete. */
13217 require_complete_types_for_parms (current_function_parms);
13219 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13221 error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13223 /* Make it return void instead, but don't change the
13224 type of the DECL_RESULT, in case we have a named return value. */
13225 if (TREE_CODE (fntype) == METHOD_TYPE)
13227 tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13228 TREE_TYPE (decl)
13229 = build_method_type_directly (ctype,
13230 void_type_node,
13231 FUNCTION_ARG_CHAIN (decl));
13233 else
13234 TREE_TYPE (decl)
13235 = build_function_type (void_type_node,
13236 TYPE_ARG_TYPES (TREE_TYPE (decl)));
13237 TREE_TYPE (decl)
13238 = build_exception_variant (fntype,
13239 TYPE_RAISES_EXCEPTIONS (fntype));
13241 else
13242 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13245 /* Create the FUNCTION_DECL for a function definition.
13246 DECLSPECS and DECLARATOR are the parts of the declaration;
13247 they describe the function's name and the type it returns,
13248 but twisted together in a fashion that parallels the syntax of C.
13250 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13251 DECLARATOR is really the DECL for the function we are about to
13252 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13253 indicating that the function is an inline defined in-class.
13255 This function creates a binding context for the function body
13256 as well as setting up the FUNCTION_DECL in current_function_decl.
13258 Returns 1 on success. If the DECLARATOR is not suitable for a function
13259 (it defines a datum instead), we return 0, which tells
13260 yyparse to report a parse error.
13262 For C++, we must first check whether that datum makes any sense.
13263 For example, "class A local_a(1,2);" means that variable local_a
13264 is an aggregate of type A, which should have a constructor
13265 applied to it with the argument list [1, 2]. */
13268 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13270 tree decl1;
13271 tree ctype = NULL_TREE;
13272 tree fntype;
13273 tree restype;
13274 int doing_friend = 0;
13275 struct cp_binding_level *bl;
13276 tree current_function_parms;
13278 /* Sanity check. */
13279 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13280 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13282 /* This should only be done once on the top most decl. */
13283 if (have_extern_spec)
13285 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13286 have_extern_spec = false;
13289 if (flags & SF_PRE_PARSED)
13291 decl1 = declarator;
13293 fntype = TREE_TYPE (decl1);
13294 if (TREE_CODE (fntype) == METHOD_TYPE)
13295 ctype = TYPE_METHOD_BASETYPE (fntype);
13297 /* ISO C++ 11.4/5. A friend function defined in a class is in
13298 the (lexical) scope of the class in which it is defined. */
13299 if (!ctype && DECL_FRIEND_P (decl1))
13301 ctype = DECL_FRIEND_CONTEXT (decl1);
13303 /* CTYPE could be null here if we're dealing with a template;
13304 for example, `inline friend float foo()' inside a template
13305 will have no CTYPE set. */
13306 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13307 ctype = NULL_TREE;
13308 else
13309 doing_friend = 1;
13312 last_function_parms = DECL_ARGUMENTS (decl1);
13314 else
13316 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13317 /* If the declarator is not suitable for a function definition,
13318 cause a syntax error. */
13319 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13320 return 0;
13322 cplus_decl_attributes (&decl1, attrs, 0);
13324 /* If #pragma weak was used, mark the decl weak now. */
13325 if (global_scope_p (current_binding_level))
13326 maybe_apply_pragma_weak (decl1);
13328 fntype = TREE_TYPE (decl1);
13330 restype = TREE_TYPE (fntype);
13332 if (TREE_CODE (fntype) == METHOD_TYPE)
13333 ctype = TYPE_METHOD_BASETYPE (fntype);
13334 else if (DECL_MAIN_P (decl1))
13336 /* If this doesn't return integer_type, or a typedef to
13337 integer_type, complain. */
13338 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl1)), integer_type_node))
13340 if (pedantic || warn_return_type)
13341 pedwarn ("return type for `main' changed to `int'");
13342 TREE_TYPE (decl1) = fntype = default_function_type;
13347 if (DECL_DECLARED_INLINE_P (decl1)
13348 && lookup_attribute ("noinline", attrs))
13349 warning ("%Hinline function '%D' given attribute noinline",
13350 &DECL_SOURCE_LOCATION (decl1), decl1);
13352 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13353 /* This is a constructor, we must ensure that any default args
13354 introduced by this definition are propagated to the clones
13355 now. The clones are used directly in overload resolution. */
13356 adjust_clone_args (decl1);
13358 /* Sometimes we don't notice that a function is a static member, and
13359 build a METHOD_TYPE for it. Fix that up now. */
13360 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13361 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13363 revert_static_member_fn (decl1);
13364 last_function_parms = TREE_CHAIN (last_function_parms);
13365 ctype = NULL_TREE;
13368 /* Warn if function was previously implicitly declared
13369 (but not if we warned then). */
13370 if (! warn_implicit
13371 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13372 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13374 /* Set up current_class_type, and enter the scope of the class, if
13375 appropriate. */
13376 if (ctype)
13377 push_nested_class (ctype);
13378 else if (DECL_STATIC_FUNCTION_P (decl1))
13379 push_nested_class (DECL_CONTEXT (decl1));
13381 /* Now that we have entered the scope of the class, we must restore
13382 the bindings for any template parameters surrounding DECL1, if it
13383 is an inline member template. (Order is important; consider the
13384 case where a template parameter has the same name as a field of
13385 the class.) It is not until after this point that
13386 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13387 if (flags & SF_INCLASS_INLINE)
13388 maybe_begin_member_template_processing (decl1);
13390 /* Effective C++ rule 15. */
13391 if (warn_ecpp
13392 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13393 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13394 warning ("`operator=' should return a reference to `*this'");
13396 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13397 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13398 if (!DECL_INITIAL (decl1))
13399 DECL_INITIAL (decl1) = error_mark_node;
13401 /* This function exists in static storage.
13402 (This does not mean `static' in the C sense!) */
13403 TREE_STATIC (decl1) = 1;
13405 /* We must call push_template_decl after current_class_type is set
13406 up. (If we are processing inline definitions after exiting a
13407 class scope, current_class_type will be NULL_TREE until set above
13408 by push_nested_class.) */
13409 if (processing_template_decl)
13410 decl1 = push_template_decl (decl1);
13412 /* We are now in the scope of the function being defined. */
13413 current_function_decl = decl1;
13415 /* Save the parm names or decls from this function's declarator
13416 where store_parm_decls will find them. */
13417 current_function_parms = last_function_parms;
13419 /* Make sure the parameter and return types are reasonable. When
13420 you declare a function, these types can be incomplete, but they
13421 must be complete when you define the function. */
13422 if (! processing_template_decl)
13423 check_function_type (decl1, current_function_parms);
13425 /* Build the return declaration for the function. */
13426 restype = TREE_TYPE (fntype);
13427 /* Promote the value to int before returning it. */
13428 if (c_promoting_integer_type_p (restype))
13429 restype = type_promotes_to (restype);
13430 if (DECL_RESULT (decl1) == NULL_TREE)
13432 DECL_RESULT (decl1)
13433 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13434 c_apply_type_quals_to_decl (cp_type_quals (restype),
13435 DECL_RESULT (decl1));
13438 /* Initialize RTL machinery. We cannot do this until
13439 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13440 even when processing a template; this is how we get
13441 CFUN set up, and our per-function variables initialized.
13442 FIXME factor out the non-RTL stuff. */
13443 bl = current_binding_level;
13444 allocate_struct_function (decl1);
13445 current_binding_level = bl;
13447 /* Even though we're inside a function body, we still don't want to
13448 call expand_expr to calculate the size of a variable-sized array.
13449 We haven't necessarily assigned RTL to all variables yet, so it's
13450 not safe to try to expand expressions involving them. */
13451 immediate_size_expand = 0;
13452 cfun->x_dont_save_pending_sizes_p = 1;
13454 /* Start the statement-tree, start the tree now. */
13455 begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13457 /* Don't double-count statements in templates. */
13458 DECL_ESTIMATED_INSNS (decl1) = 0;
13460 /* Let the user know we're compiling this function. */
13461 announce_function (decl1);
13463 /* Record the decl so that the function name is defined.
13464 If we already have a decl for this name, and it is a FUNCTION_DECL,
13465 use the old decl. */
13466 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13468 /* A specialization is not used to guide overload resolution. */
13469 if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13470 && ! DECL_FUNCTION_MEMBER_P (decl1))
13471 decl1 = pushdecl (decl1);
13472 else
13474 /* We need to set the DECL_CONTEXT. */
13475 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13476 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13477 /* And make sure we have enough default args. */
13478 check_default_args (decl1);
13480 fntype = TREE_TYPE (decl1);
13483 /* Reset these in case the call to pushdecl changed them. */
13484 current_function_decl = decl1;
13485 cfun->decl = decl1;
13487 /* If we are (erroneously) defining a function that we have already
13488 defined before, wipe out what we knew before. */
13489 if (!DECL_PENDING_INLINE_P (decl1))
13490 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13492 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13494 /* We know that this was set up by `grokclassfn'. We do not
13495 wait until `store_parm_decls', since evil parse errors may
13496 never get us to that point. Here we keep the consistency
13497 between `current_class_type' and `current_class_ptr'. */
13498 tree t = DECL_ARGUMENTS (decl1);
13500 my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13501 162);
13502 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13503 19990811);
13505 cp_function_chain->x_current_class_ref
13506 = build_indirect_ref (t, NULL);
13507 cp_function_chain->x_current_class_ptr = t;
13509 /* Constructors and destructors need to know whether they're "in
13510 charge" of initializing virtual base classes. */
13511 t = TREE_CHAIN (t);
13512 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13514 current_in_charge_parm = t;
13515 t = TREE_CHAIN (t);
13517 if (DECL_HAS_VTT_PARM_P (decl1))
13519 if (DECL_NAME (t) != vtt_parm_identifier)
13520 abort ();
13521 current_vtt_parm = t;
13525 if (DECL_INTERFACE_KNOWN (decl1))
13527 tree ctx = decl_function_context (decl1);
13529 if (DECL_NOT_REALLY_EXTERN (decl1))
13530 DECL_EXTERNAL (decl1) = 0;
13532 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13533 && TREE_PUBLIC (ctx))
13534 /* This is a function in a local class in an extern inline
13535 function. */
13536 comdat_linkage (decl1);
13538 /* If this function belongs to an interface, it is public.
13539 If it belongs to someone else's interface, it is also external.
13540 This only affects inlines and template instantiations. */
13541 else if (interface_unknown == 0
13542 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13543 || flag_alt_external_templates))
13545 if (DECL_DECLARED_INLINE_P (decl1)
13546 || DECL_TEMPLATE_INSTANTIATION (decl1)
13547 || processing_template_decl)
13549 DECL_EXTERNAL (decl1)
13550 = (interface_only
13551 || (DECL_DECLARED_INLINE_P (decl1)
13552 && ! flag_implement_inlines
13553 && !DECL_VINDEX (decl1)));
13555 /* For WIN32 we also want to put these in linkonce sections. */
13556 maybe_make_one_only (decl1);
13558 else
13559 DECL_EXTERNAL (decl1) = 0;
13560 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13561 DECL_INTERFACE_KNOWN (decl1) = 1;
13563 else if (interface_unknown && interface_only
13564 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13565 || flag_alt_external_templates))
13567 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13568 interface, we will have interface_only set but not
13569 interface_known. In that case, we don't want to use the normal
13570 heuristics because someone will supply a #pragma implementation
13571 elsewhere, and deducing it here would produce a conflict. */
13572 comdat_linkage (decl1);
13573 DECL_EXTERNAL (decl1) = 0;
13574 DECL_INTERFACE_KNOWN (decl1) = 1;
13575 DECL_DEFER_OUTPUT (decl1) = 1;
13577 else
13579 /* This is a definition, not a reference.
13580 So clear DECL_EXTERNAL. */
13581 DECL_EXTERNAL (decl1) = 0;
13583 if ((DECL_DECLARED_INLINE_P (decl1)
13584 || DECL_TEMPLATE_INSTANTIATION (decl1))
13585 && ! DECL_INTERFACE_KNOWN (decl1)
13586 /* Don't try to defer nested functions for now. */
13587 && ! decl_function_context (decl1))
13588 DECL_DEFER_OUTPUT (decl1) = 1;
13589 else
13590 DECL_INTERFACE_KNOWN (decl1) = 1;
13593 begin_scope (sk_function_parms);
13595 ++function_depth;
13597 if (DECL_DESTRUCTOR_P (decl1))
13599 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13600 DECL_CONTEXT (dtor_label) = current_function_decl;
13603 start_fname_decls ();
13605 store_parm_decls (current_function_parms);
13607 return 1;
13610 /* Store the parameter declarations into the current function declaration.
13611 This is called after parsing the parameter declarations, before
13612 digesting the body of the function.
13614 Also install to binding contour return value identifier, if any. */
13616 static void
13617 store_parm_decls (tree current_function_parms)
13619 register tree fndecl = current_function_decl;
13620 register tree parm;
13622 /* This is a chain of any other decls that came in among the parm
13623 declarations. If a parm is declared with enum {foo, bar} x;
13624 then CONST_DECLs for foo and bar are put here. */
13625 tree nonparms = NULL_TREE;
13627 if (current_function_parms)
13629 /* This case is when the function was defined with an ANSI prototype.
13630 The parms already have decls, so we need not do anything here
13631 except record them as in effect
13632 and complain if any redundant old-style parm decls were written. */
13634 tree specparms = current_function_parms;
13635 tree next;
13637 /* Must clear this because it might contain TYPE_DECLs declared
13638 at class level. */
13639 storedecls (NULL_TREE);
13641 /* If we're doing semantic analysis, then we'll call pushdecl
13642 for each of these. We must do them in reverse order so that
13643 they end in the correct forward order. */
13644 specparms = nreverse (specparms);
13646 for (parm = specparms; parm; parm = next)
13648 next = TREE_CHAIN (parm);
13649 if (TREE_CODE (parm) == PARM_DECL)
13651 if (DECL_NAME (parm) == NULL_TREE
13652 || TREE_CODE (parm) != VOID_TYPE)
13653 pushdecl (parm);
13654 else
13655 error ("parameter `%D' declared void", parm);
13657 else
13659 /* If we find an enum constant or a type tag,
13660 put it aside for the moment. */
13661 TREE_CHAIN (parm) = NULL_TREE;
13662 nonparms = chainon (nonparms, parm);
13666 /* Get the decls in their original chain order and record in the
13667 function. This is all and only the PARM_DECLs that were
13668 pushed into scope by the loop above. */
13669 DECL_ARGUMENTS (fndecl) = getdecls ();
13671 else
13672 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13674 /* Now store the final chain of decls for the arguments
13675 as the decl-chain of the current lexical scope.
13676 Put the enumerators in as well, at the front so that
13677 DECL_ARGUMENTS is not modified. */
13678 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13680 /* Do the starting of the exception specifications, if we have any. */
13681 if (flag_exceptions && !processing_template_decl
13682 && flag_enforce_eh_specs
13683 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13684 current_eh_spec_block = begin_eh_spec_block ();
13688 /* We have finished doing semantic analysis on DECL, but have not yet
13689 generated RTL for its body. Save away our current state, so that
13690 when we want to generate RTL later we know what to do. */
13692 static void
13693 save_function_data (tree decl)
13695 struct language_function *f;
13697 /* Save the language-specific per-function data so that we can
13698 get it back when we really expand this function. */
13699 my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13700 19990908);
13702 /* Make a copy. */
13703 f = ggc_alloc (sizeof (struct language_function));
13704 memcpy (f, cp_function_chain, sizeof (struct language_function));
13705 DECL_SAVED_FUNCTION_DATA (decl) = f;
13707 /* Clear out the bits we don't need. */
13708 f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13709 f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13710 f->x_named_label_uses = NULL;
13711 f->bindings = NULL;
13712 f->x_local_names = NULL;
13714 /* If we've already decided that we cannot inline this function, we
13715 must remember that fact when we actually go to expand the
13716 function. */
13717 if (current_function_cannot_inline)
13719 f->cannot_inline = current_function_cannot_inline;
13720 DECL_INLINE (decl) = 0;
13724 /* Add a note to mark the beginning of the main body of the constructor.
13725 This is used to set up the data structures for the cleanup regions for
13726 fully-constructed bases and members. */
13728 static void
13729 begin_constructor_body (void)
13733 /* Add a note to mark the end of the main body of the constructor. This is
13734 used to end the cleanup regions for fully-constructed bases and
13735 members. */
13737 static void
13738 finish_constructor_body (void)
13742 /* Do all the processing for the beginning of a destructor; set up the
13743 vtable pointers and cleanups for bases and members. */
13745 static void
13746 begin_destructor_body (void)
13748 tree if_stmt;
13749 tree compound_stmt;
13751 /* If the dtor is empty, and we know there is not any possible
13752 way we could use any vtable entries, before they are possibly
13753 set by a base class dtor, we don't have to setup the vtables,
13754 as we know that any base class dtor will set up any vtables
13755 it needs. We avoid MI, because one base class dtor can do a
13756 virtual dispatch to an overridden function that would need to
13757 have a non-related vtable set up, we cannot avoid setting up
13758 vtables in that case. We could change this to see if there
13759 is just one vtable.
13761 ??? In the destructor for a class, the vtables are set
13762 appropriately for that class. There will be no non-related
13763 vtables. jason 2001-12-11. */
13764 if_stmt = begin_if_stmt ();
13766 /* If it is not safe to avoid setting up the vtables, then
13767 someone will change the condition to be boolean_true_node.
13768 (Actually, for now, we do not have code to set the condition
13769 appropriately, so we just assume that we always need to
13770 initialize the vtables.) */
13771 finish_if_stmt_cond (boolean_true_node, if_stmt);
13773 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
13775 /* Make all virtual function table pointers in non-virtual base
13776 classes point to CURRENT_CLASS_TYPE's virtual function
13777 tables. */
13778 initialize_vtbl_ptrs (current_class_ptr);
13780 finish_compound_stmt (compound_stmt);
13781 finish_then_clause (if_stmt);
13782 finish_if_stmt ();
13784 /* And insert cleanups for our bases and members so that they
13785 will be properly destroyed if we throw. */
13786 push_base_cleanups ();
13789 /* At the end of every destructor we generate code to delete the object if
13790 necessary. Do that now. */
13792 static void
13793 finish_destructor_body (void)
13795 tree exprstmt;
13797 /* Any return from a destructor will end up here; that way all base
13798 and member cleanups will be run when the function returns. */
13799 add_stmt (build_stmt (LABEL_STMT, dtor_label));
13801 /* In a virtual destructor, we must call delete. */
13802 if (DECL_VIRTUAL_P (current_function_decl))
13804 tree if_stmt;
13805 tree virtual_size = cxx_sizeof (current_class_type);
13807 /* [class.dtor]
13809 At the point of definition of a virtual destructor (including
13810 an implicit definition), non-placement operator delete shall
13811 be looked up in the scope of the destructor's class and if
13812 found shall be accessible and unambiguous. */
13813 exprstmt = build_op_delete_call
13814 (DELETE_EXPR, current_class_ptr, virtual_size,
13815 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13817 if_stmt = begin_if_stmt ();
13818 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13819 current_in_charge_parm,
13820 integer_one_node),
13821 if_stmt);
13822 finish_expr_stmt (exprstmt);
13823 finish_then_clause (if_stmt);
13824 finish_if_stmt ();
13828 /* Do the necessary processing for the beginning of a function body, which
13829 in this case includes member-initializers, but not the catch clauses of
13830 a function-try-block. Currently, this means opening a binding level
13831 for the member-initializers (in a ctor) and member cleanups (in a dtor).
13832 In other functions, this isn't necessary, but it doesn't hurt. */
13834 tree
13835 begin_function_body (void)
13837 tree stmt;
13839 if (processing_template_decl)
13840 /* Do nothing now. */;
13841 else
13842 /* Always keep the BLOCK node associated with the outermost pair of
13843 curly braces of a function. These are needed for correct
13844 operation of dwarfout.c. */
13845 keep_next_level (1);
13847 stmt = begin_compound_stmt (/*has_no_scope=*/false);
13848 COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13850 if (processing_template_decl)
13851 /* Do nothing now. */;
13852 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13853 begin_constructor_body ();
13854 else if (DECL_DESTRUCTOR_P (current_function_decl))
13855 begin_destructor_body ();
13857 return stmt;
13860 /* Do the processing for the end of a function body. Currently, this means
13861 closing out the cleanups for fully-constructed bases and members, and in
13862 the case of the destructor, deleting the object if desired. Again, this
13863 is only meaningful for [cd]tors, since they are the only functions where
13864 there is a significant distinction between the main body and any
13865 function catch clauses. Handling, say, main() return semantics here
13866 would be wrong, as flowing off the end of a function catch clause for
13867 main() would also need to return 0. */
13869 void
13870 finish_function_body (tree compstmt)
13872 /* Close the block. */
13873 finish_compound_stmt (compstmt);
13875 if (processing_template_decl)
13876 /* Do nothing now. */;
13877 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13878 finish_constructor_body ();
13879 else if (DECL_DESTRUCTOR_P (current_function_decl))
13880 finish_destructor_body ();
13883 /* Finish up a function declaration and compile that function
13884 all the way to assembler language output. The free the storage
13885 for the function definition.
13887 FLAGS is a bitwise or of the following values:
13888 2 - INCLASS_INLINE
13889 We just finished processing the body of an in-class inline
13890 function definition. (This processing will have taken place
13891 after the class definition is complete.) */
13893 tree
13894 finish_function (int flags)
13896 register tree fndecl = current_function_decl;
13897 tree fntype, ctype = NULL_TREE;
13898 int inclass_inline = (flags & 2) != 0;
13899 int nested;
13901 /* When we get some parse errors, we can end up without a
13902 current_function_decl, so cope. */
13903 if (fndecl == NULL_TREE)
13904 return error_mark_node;
13906 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13907 && DECL_VIRTUAL_P (fndecl)
13908 && !processing_template_decl)
13910 tree fnclass = DECL_CONTEXT (fndecl);
13911 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
13912 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
13915 nested = function_depth > 1;
13916 fntype = TREE_TYPE (fndecl);
13918 /* TREE_READONLY (fndecl) = 1;
13919 This caused &foo to be of type ptr-to-const-function
13920 which then got a warning when stored in a ptr-to-function variable. */
13922 my_friendly_assert (building_stmt_tree (), 20000911);
13924 finish_fname_decls ();
13926 /* For a cloned function, we've already got all the code we need;
13927 there's no need to add any extra bits. */
13928 if (!DECL_CLONED_FUNCTION_P (fndecl))
13930 if (DECL_MAIN_P (current_function_decl))
13932 /* Make it so that `main' always returns 0 by default. */
13933 #if VMS_TARGET
13934 finish_return_stmt (integer_one_node);
13935 #else
13936 finish_return_stmt (integer_zero_node);
13937 #endif
13940 /* Finish dealing with exception specifiers. */
13941 if (flag_exceptions && !processing_template_decl
13942 && flag_enforce_eh_specs
13943 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13944 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
13945 (TREE_TYPE (current_function_decl)),
13946 current_eh_spec_block);
13949 /* If we're saving up tree structure, tie off the function now. */
13950 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
13952 /* If this function can't throw any exceptions, remember that. */
13953 if (!processing_template_decl
13954 && !cp_function_chain->can_throw
13955 && !flag_non_call_exceptions)
13956 TREE_NOTHROW (fndecl) = 1;
13958 /* This must come after expand_function_end because cleanups might
13959 have declarations (from inline functions) that need to go into
13960 this function's blocks. */
13962 /* If the current binding level isn't the outermost binding level
13963 for this function, either there is a bug, or we have experienced
13964 syntax errors and the statement tree is malformed. */
13965 if (current_binding_level->kind != sk_function_parms)
13967 /* Make sure we have already experienced errors. */
13968 if (errorcount == 0)
13969 abort ();
13971 /* Throw away the broken statement tree and extra binding
13972 levels. */
13973 DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
13975 while (current_binding_level->kind != sk_function_parms)
13977 if (current_binding_level->kind == sk_class)
13978 pop_nested_class ();
13979 else
13980 poplevel (0, 0, 0);
13983 poplevel (1, 0, 1);
13985 /* Statements should always be full-expressions at the outermost set
13986 of curly braces for a function. */
13987 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
13989 /* Set up the named return value optimization, if we can. Here, we
13990 eliminate the copy from the nrv into the RESULT_DECL and any cleanup
13991 for the nrv. genrtl_start_function and declare_return_variable
13992 handle making the nrv and RESULT_DECL share space. */
13993 if (current_function_return_value)
13995 tree r = current_function_return_value;
13996 tree outer;
13998 if (r != error_mark_node
13999 /* This is only worth doing for fns that return in memory--and
14000 simpler, since we don't have to worry about promoted modes. */
14001 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
14002 /* Only allow this for variables declared in the outer scope of
14003 the function so we know that their lifetime always ends with a
14004 return; see g++.dg/opt/nrv6.C. We could be more flexible if
14005 we were to do this optimization in tree-ssa. */
14006 /* Skip the artificial function body block. */
14007 && (outer = BLOCK_SUBBLOCKS (BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl))),
14008 chain_member (r, BLOCK_VARS (outer))))
14011 DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14012 walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14013 nullify_returns_r, r);
14015 else
14016 /* Clear it so genrtl_start_function and declare_return_variable
14017 know we're not optimizing. */
14018 current_function_return_value = NULL_TREE;
14021 /* Remember that we were in class scope. */
14022 if (current_class_name)
14023 ctype = current_class_type;
14025 /* Must mark the RESULT_DECL as being in this function. */
14026 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14028 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14029 to the FUNCTION_DECL node itself. */
14030 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14032 /* Save away current state, if appropriate. */
14033 if (!processing_template_decl)
14034 save_function_data (fndecl);
14036 /* If this function calls `setjmp' it cannot be inlined. When
14037 `longjmp' is called it is not guaranteed to restore the value of
14038 local variables that have been modified since the call to
14039 `setjmp'. So, if were to inline this function into some caller
14040 `c', then when we `longjmp', we might not restore all variables
14041 in `c'. (It might seem, at first blush, that there's no way for
14042 this function to modify local variables in `c', but their
14043 addresses may have been stored somewhere accessible to this
14044 function.) */
14045 if (!processing_template_decl && calls_setjmp_p (fndecl))
14046 DECL_UNINLINABLE (fndecl) = 1;
14048 /* Complain if there's just no return statement. */
14049 if (warn_return_type
14050 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14051 && !current_function_returns_value && !current_function_returns_null
14052 /* Don't complain if we abort or throw. */
14053 && !current_function_returns_abnormally
14054 && !DECL_NAME (DECL_RESULT (fndecl))
14055 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
14056 inline function, as we might never be compiled separately. */
14057 && (DECL_INLINE (fndecl) || processing_template_decl))
14058 warning ("no return statement in function returning non-void");
14060 /* We're leaving the context of this function, so zap cfun. It's still in
14061 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
14062 cfun = NULL;
14063 current_function_decl = NULL;
14065 /* If this is an in-class inline definition, we may have to pop the
14066 bindings for the template parameters that we added in
14067 maybe_begin_member_template_processing when start_function was
14068 called. */
14069 if (inclass_inline)
14070 maybe_end_member_template_processing ();
14072 /* Leave the scope of the class. */
14073 if (ctype)
14074 pop_nested_class ();
14076 --function_depth;
14078 /* Clean up. */
14079 if (! nested)
14080 /* Let the error reporting routines know that we're outside a
14081 function. For a nested function, this value is used in
14082 cxx_pop_function_context and then reset via pop_function_context. */
14083 current_function_decl = NULL_TREE;
14085 return fndecl;
14088 /* Create the FUNCTION_DECL for a function definition.
14089 DECLSPECS and DECLARATOR are the parts of the declaration;
14090 they describe the return type and the name of the function,
14091 but twisted together in a fashion that parallels the syntax of C.
14093 This function creates a binding context for the function body
14094 as well as setting up the FUNCTION_DECL in current_function_decl.
14096 Returns a FUNCTION_DECL on success.
14098 If the DECLARATOR is not suitable for a function (it defines a datum
14099 instead), we return 0, which tells yyparse to report a parse error.
14101 May return void_type_node indicating that this method is actually
14102 a friend. See grokfield for more details.
14104 Came here with a `.pushlevel' .
14106 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14107 CHANGES TO CODE IN `grokfield'. */
14109 tree
14110 start_method (tree declspecs, tree declarator, tree attrlist)
14112 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14113 &attrlist);
14115 if (fndecl == error_mark_node)
14116 return error_mark_node;
14118 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14120 error ("invalid member function declaration");
14121 return error_mark_node;
14124 if (attrlist)
14125 cplus_decl_attributes (&fndecl, attrlist, 0);
14127 /* Pass friends other than inline friend functions back. */
14128 if (fndecl == void_type_node)
14129 return fndecl;
14131 if (DECL_IN_AGGR_P (fndecl))
14133 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14135 if (DECL_CONTEXT (fndecl)
14136 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14137 error ("`%D' is already defined in class `%T'", fndecl,
14138 DECL_CONTEXT (fndecl));
14140 return void_type_node;
14143 check_template_shadow (fndecl);
14145 DECL_DECLARED_INLINE_P (fndecl) = 1;
14146 if (flag_default_inline)
14147 DECL_INLINE (fndecl) = 1;
14149 /* We process method specializations in finish_struct_1. */
14150 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14152 fndecl = push_template_decl (fndecl);
14153 if (fndecl == error_mark_node)
14154 return fndecl;
14157 if (! DECL_FRIEND_P (fndecl))
14159 if (TREE_CHAIN (fndecl))
14161 fndecl = copy_node (fndecl);
14162 TREE_CHAIN (fndecl) = NULL_TREE;
14164 grok_special_member_properties (fndecl);
14167 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14169 /* Make a place for the parms */
14170 begin_scope (sk_function_parms);
14172 DECL_IN_AGGR_P (fndecl) = 1;
14173 return fndecl;
14176 /* Go through the motions of finishing a function definition.
14177 We don't compile this method until after the whole class has
14178 been processed.
14180 FINISH_METHOD must return something that looks as though it
14181 came from GROKFIELD (since we are defining a method, after all).
14183 This is called after parsing the body of the function definition.
14184 STMTS is the chain of statements that makes up the function body.
14186 DECL is the ..._DECL that `start_method' provided. */
14188 tree
14189 finish_method (tree decl)
14191 register tree fndecl = decl;
14192 tree old_initial;
14194 register tree link;
14196 if (decl == void_type_node)
14197 return decl;
14199 old_initial = DECL_INITIAL (fndecl);
14201 /* Undo the level for the parms (from start_method).
14202 This is like poplevel, but it causes nothing to be
14203 saved. Saving information here confuses symbol-table
14204 output routines. Besides, this information will
14205 be correctly output when this method is actually
14206 compiled. */
14208 /* Clear out the meanings of the local variables of this level;
14209 also record in each decl which block it belongs to. */
14211 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14213 if (DECL_NAME (link) != NULL_TREE)
14214 pop_binding (DECL_NAME (link), link);
14215 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14216 DECL_CONTEXT (link) = NULL_TREE;
14219 poplevel (0, 0, 0);
14221 DECL_INITIAL (fndecl) = old_initial;
14223 /* We used to check if the context of FNDECL was different from
14224 current_class_type as another way to get inside here. This didn't work
14225 for String.cc in libg++. */
14226 if (DECL_FRIEND_P (fndecl))
14228 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14229 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14230 decl = void_type_node;
14233 return decl;
14237 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14238 we can lay it out later, when and if its type becomes complete. */
14240 void
14241 maybe_register_incomplete_var (tree var)
14243 my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14245 /* Keep track of variables with incomplete types. */
14246 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14247 && DECL_EXTERNAL (var))
14249 tree inner_type = TREE_TYPE (var);
14251 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14252 inner_type = TREE_TYPE (inner_type);
14253 inner_type = TYPE_MAIN_VARIANT (inner_type);
14255 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14256 /* RTTI TD entries are created while defining the type_info. */
14257 || (TYPE_LANG_SPECIFIC (inner_type)
14258 && TYPE_BEING_DEFINED (inner_type)))
14259 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14263 /* Called when a class type (given by TYPE) is defined. If there are
14264 any existing VAR_DECLs whose type hsa been completed by this
14265 declaration, update them now. */
14267 void
14268 complete_vars (tree type)
14270 tree *list = &incomplete_vars;
14272 my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14273 while (*list)
14275 if (same_type_p (type, TREE_PURPOSE (*list)))
14277 tree var = TREE_VALUE (*list);
14278 /* Complete the type of the variable. The VAR_DECL itself
14279 will be laid out in expand_expr. */
14280 complete_type (TREE_TYPE (var));
14281 /* Remove this entry from the list. */
14282 *list = TREE_CHAIN (*list);
14284 else
14285 list = &TREE_CHAIN (*list);
14289 /* If DECL is of a type which needs a cleanup, build that cleanup
14290 here. */
14292 tree
14293 cxx_maybe_build_cleanup (tree decl)
14295 tree type = TREE_TYPE (decl);
14297 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14299 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14300 tree rval;
14302 if (TREE_CODE (type) == ARRAY_TYPE)
14303 rval = decl;
14304 else
14306 cxx_mark_addressable (decl);
14307 rval = build_unary_op (ADDR_EXPR, decl, 0);
14310 /* Optimize for space over speed here. */
14311 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14312 || flag_expensive_optimizations)
14313 flags |= LOOKUP_NONVIRTUAL;
14315 rval = build_delete (TREE_TYPE (rval), rval,
14316 sfk_complete_destructor, flags, 0);
14318 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14319 && ! TYPE_HAS_DESTRUCTOR (type))
14320 rval = build_compound_expr (rval, build_vbase_delete (type, decl));
14322 return rval;
14324 return NULL_TREE;
14327 /* When a stmt has been parsed, this function is called. */
14329 void
14330 finish_stmt (void)
14332 /* Always assume this statement was not an expression statement. If
14333 it actually was an expression statement, its our callers
14334 responsibility to fix this up. */
14335 last_expr_type = NULL_TREE;
14338 /* DECL was originally constructed as a non-static member function,
14339 but turned out to be static. Update it accordingly. */
14341 void
14342 revert_static_member_fn (tree decl)
14344 tree tmp;
14345 tree function = TREE_TYPE (decl);
14346 tree args = TYPE_ARG_TYPES (function);
14348 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14349 != TYPE_UNQUALIFIED)
14350 error ("static member function `%#D' declared with type qualifiers",
14351 decl);
14353 args = TREE_CHAIN (args);
14354 tmp = build_function_type (TREE_TYPE (function), args);
14355 tmp = build_qualified_type (tmp, cp_type_quals (function));
14356 tmp = build_exception_variant (tmp,
14357 TYPE_RAISES_EXCEPTIONS (function));
14358 TREE_TYPE (decl) = tmp;
14359 if (DECL_ARGUMENTS (decl))
14360 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14361 DECL_STATIC_FUNCTION_P (decl) = 1;
14364 /* Initialize the variables used during compilation of a C++
14365 function. */
14367 void
14368 cxx_push_function_context (struct function * f)
14370 struct language_function *p
14371 = ggc_alloc_cleared (sizeof (struct language_function));
14372 f->language = p;
14374 /* Whenever we start a new function, we destroy temporaries in the
14375 usual way. */
14376 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14378 if (f->decl)
14380 tree fn = f->decl;
14382 current_function_is_thunk = DECL_THUNK_P (fn);
14384 if (DECL_SAVED_FUNCTION_DATA (fn))
14386 /* If we already parsed this function, and we're just expanding it
14387 now, restore saved state. */
14388 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
14390 /* If we decided that we didn't want to inline this function,
14391 make sure the back-end knows that. */
14392 if (!current_function_cannot_inline)
14393 current_function_cannot_inline = cp_function_chain->cannot_inline;
14395 /* We don't need the saved data anymore. Unless this is an inline
14396 function; we need the named return value info for
14397 cp_copy_res_decl_for_inlining. */
14398 if (! DECL_INLINE (fn))
14399 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
14404 /* Free the language-specific parts of F, now that we've finished
14405 compiling the function. */
14407 void
14408 cxx_pop_function_context (struct function * f)
14410 f->language = 0;
14413 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14414 one of the language-independent trees. */
14416 enum cp_tree_node_structure_enum
14417 cp_tree_node_structure (union lang_tree_node * t)
14419 switch (TREE_CODE (&t->generic))
14421 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
14422 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
14423 case OVERLOAD: return TS_CP_OVERLOAD;
14424 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14425 case PTRMEM_CST: return TS_CP_PTRMEM;
14426 case BASELINK: return TS_CP_BASELINK;
14427 case WRAPPER: return TS_CP_WRAPPER;
14428 default: return TS_CP_GENERIC;
14432 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14433 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
14435 tree
14436 identifier_global_value (tree t)
14438 return IDENTIFIER_GLOBAL_VALUE (t);
14441 /* Build the void_list_node (void_type_node having been created). */
14442 tree
14443 build_void_list_node (void)
14445 tree t = build_tree_list (NULL_TREE, void_type_node);
14446 TREE_PARMLIST (t) = 1;
14447 return t;
14450 static int
14451 cp_missing_noreturn_ok_p (tree decl)
14453 /* A missing noreturn is ok for the `main' function. */
14454 return DECL_MAIN_P (decl);
14457 #include "gt-cp-decl.h"
14458 #include "gtype-cp.h"