PR c++/12114
[official-gcc.git] / gcc / cp / decl.c
blob4c287ea7158be7e5405be9ba17df18344d89d97c
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 /* 1 for the level that holds the parameters of a function.
374 2 for the level that holds a class declaration. */
375 unsigned parm_flag : 2;
377 /* 1 means make a BLOCK for this level regardless of all else.
378 2 for temporary binding contours created by the compiler. */
379 unsigned keep : 2;
381 /* Nonzero if this level "doesn't exist" for tags. */
382 unsigned tag_transparent : 1;
384 /* Nonzero if this level can safely have additional
385 cleanup-needing variables added to it. */
386 unsigned more_cleanups_ok : 1;
387 unsigned have_cleanups : 1;
389 /* Nonzero if this scope is for storing the decls for template
390 parameters and generic decls; these decls will be discarded and
391 replaced with a TEMPLATE_DECL. */
392 unsigned template_parms_p : 1;
394 /* Nonzero if this scope corresponds to the `<>' in a
395 `template <>' clause. Whenever this flag is set,
396 TEMPLATE_PARMS_P will be set as well. */
397 unsigned template_spec_p : 1;
399 /* This is set for a namespace binding level. */
400 unsigned namespace_p : 1;
402 /* True if this level is that of a for-statement where we need to
403 worry about ambiguous (ARM or ISO) scope rules. */
404 unsigned is_for_scope : 1;
406 /* True if this level corresponds to a TRY block. Currently this
407 information is only available while building the tree structure. */
408 unsigned is_try_scope : 1;
410 /* True if this level corresponds to a CATCH block. Currently this
411 information is only available while building the tree structure. */
412 unsigned is_catch_scope : 1;
414 /* Three bits left for this word. */
416 /* Binding depth at which this level began. */
417 unsigned binding_depth;
420 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
422 /* The binding level currently in effect. */
424 #define current_binding_level \
425 (*(cfun && cp_function_chain->bindings \
426 ? &cp_function_chain->bindings \
427 : &scope_chain->bindings))
429 /* The binding level of the current class, if any. */
431 #define class_binding_level scope_chain->class_bindings
433 /* A chain of binding_level structures awaiting reuse. */
435 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
437 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
439 static int keep_next_level_flag;
441 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
442 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
443 time the VAR_DECL was declared, the type was incomplete. */
445 static GTY(()) tree incomplete_vars;
447 #ifndef ENABLE_SCOPE_CHECKING
448 # define ENABLE_SCOPE_CHECKING 0
449 #else
450 # define ENABLE_SCOPE_CHECKING 1
451 #endif
453 static int binding_depth = 0;
454 static int is_class_level = 0;
456 static void
457 indent (int depth)
459 int i;
461 for (i = 0; i < depth * 2; i++)
462 putc (' ', stderr);
465 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
467 /* Return a string describing the kind of SCOPE we have. */
468 static const char *
469 cxx_scope_descriptor (cxx_scope *scope)
471 const char *desc;
473 if (scope->namespace_p)
474 desc = "namespace-scope";
475 else if (scope->parm_flag == 1)
476 desc = "function-prototype-scope";
477 else if (scope->parm_flag == 2)
478 desc = "class-scope";
479 else if (scope->is_for_scope)
480 desc = "for-scope";
481 else if (scope->is_try_scope)
482 desc = "try-scope";
483 else if (scope->is_catch_scope)
484 desc = "catch-scope";
485 else if (scope->template_spec_p)
486 desc = "template-explicit-spec-scope";
487 else if (scope->template_parms_p)
488 desc = "template-prototype-scope";
489 else
490 desc = "block-scope";
492 return desc;
495 /* Output a debugging information about SCOPE when performning
496 ACTION at LINE. */
497 static void
498 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
500 const char *desc = cxx_scope_descriptor (scope);
501 if (scope->this_entity)
502 verbatim ("%s %s(%E) %p %d\n", action, desc,
503 scope->this_entity, (void *) scope, line);
504 else
505 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
508 /* Construct a scope that may be TAG-TRANSPARENT, the sub-blocks of
509 which may be KEPT. */
510 static inline cxx_scope *
511 make_cxx_scope (bool tag_transparent, int keep)
513 cxx_scope *scope;
515 /* Reuse or create a struct for this binding level. */
516 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
518 scope = free_binding_level;
519 free_binding_level = scope->level_chain;
521 else
522 scope = ggc_alloc (sizeof (cxx_scope));
524 memset (scope, 0, sizeof (cxx_scope));
525 scope->tag_transparent = tag_transparent;
526 scope->keep = keep;
527 scope->more_cleanups_ok = true;
529 return scope;
532 static void
533 push_binding_level (cxx_scope *newlevel)
535 /* Add this level to the front of the chain (stack) of levels that
536 are active. */
537 newlevel->level_chain = current_binding_level;
538 current_binding_level = newlevel;
540 if (ENABLE_SCOPE_CHECKING)
542 newlevel->binding_depth = binding_depth;
543 indent (binding_depth);
544 cxx_scope_debug (newlevel, input_location.line, "push");
545 is_class_level = 0;
546 binding_depth++;
550 /* Find the innermost enclosing class scope, and reset
551 CLASS_BINDING_LEVEL appropriately. */
553 static void
554 find_class_binding_level (void)
556 struct cp_binding_level *level = current_binding_level;
558 while (level && level->parm_flag != 2)
559 level = level->level_chain;
560 if (level && level->parm_flag == 2)
561 class_binding_level = level;
562 else
563 class_binding_level = 0;
566 static void
567 pop_binding_level (void)
569 if (NAMESPACE_LEVEL (global_namespace))
570 /* Cannot pop a level, if there are none left to pop. */
571 my_friendly_assert (!global_scope_p (current_binding_level), 20030527);
572 /* Pop the current level, and free the structure for reuse. */
573 if (ENABLE_SCOPE_CHECKING)
575 indent (--binding_depth);
576 cxx_scope_debug (current_binding_level, input_location.line, "pop");
577 if (is_class_level != (current_binding_level == class_binding_level))
579 indent (binding_depth);
580 verbatim ("XXX is_class_level != (current_binding_level "
581 "== class_binding_level)\n");
583 is_class_level = 0;
586 register struct cp_binding_level *level = current_binding_level;
587 current_binding_level = current_binding_level->level_chain;
588 level->level_chain = free_binding_level;
589 if (level->parm_flag == 2)
590 level->type_decls = NULL;
591 else
592 binding_table_free (level->type_decls);
593 my_friendly_assert (!ENABLE_SCOPE_CHECKING
594 || level->binding_depth == binding_depth,
595 20030529);
596 free_binding_level = level;
597 find_class_binding_level ();
601 static void
602 suspend_binding_level (void)
604 if (class_binding_level)
605 current_binding_level = class_binding_level;
607 if (NAMESPACE_LEVEL (global_namespace))
608 /* Cannot suspend a level, if there are none left to suspend. */
609 my_friendly_assert (!global_scope_p (current_binding_level), 20030527);
610 /* Suspend the current level. */
611 if (ENABLE_SCOPE_CHECKING)
613 indent (--binding_depth);
614 cxx_scope_debug (current_binding_level, input_location.line, "suspend");
615 if (is_class_level != (current_binding_level == class_binding_level))
617 indent (binding_depth);
618 verbatim ("XXX is_class_level != (current_binding_level "
619 "== class_binding_level)\n");
621 is_class_level = 0;
623 current_binding_level = current_binding_level->level_chain;
624 find_class_binding_level ();
627 static void
628 resume_binding_level (struct cp_binding_level* b)
630 /* Resuming binding levels is meant only for namespaces,
631 and those cannot nest into classes. */
632 my_friendly_assert(!class_binding_level, 386);
633 /* Also, resuming a non-directly nested namespace is a no-no. */
634 my_friendly_assert(b->level_chain == current_binding_level, 386);
635 current_binding_level = b;
636 if (ENABLE_SCOPE_CHECKING)
638 b->binding_depth = binding_depth;
639 indent (binding_depth);
640 cxx_scope_debug (b, input_location.line, "resume");
641 is_class_level = 0;
642 binding_depth++;
646 /* Nonzero if we are currently in the global binding level. */
649 global_bindings_p (void)
651 return global_scope_p (current_binding_level);
654 /* Return the innermost binding level that is not for a class scope. */
656 static struct cp_binding_level *
657 innermost_nonclass_level (void)
659 struct cp_binding_level *b;
661 b = current_binding_level;
662 while (b->parm_flag == 2)
663 b = b->level_chain;
665 return b;
668 /* Nonzero if we are currently in a toplevel binding level. This
669 means either the global binding level or a namespace in a toplevel
670 binding level. Since there are no non-toplevel namespace levels,
671 this really means any namespace or template parameter level. We
672 also include a class whose context is toplevel. */
675 toplevel_bindings_p (void)
677 struct cp_binding_level *b = innermost_nonclass_level ();
679 return b->namespace_p || b->template_parms_p;
682 /* Nonzero if this is a namespace scope, or if we are defining a class
683 which is itself at namespace scope, or whose enclosing class is
684 such a class, etc. */
687 namespace_bindings_p (void)
689 struct cp_binding_level *b = innermost_nonclass_level ();
691 return b->namespace_p;
694 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
695 unconditionally. Otherwise, use the normal logic to decide whether
696 or not to create a BLOCK. */
698 void
699 keep_next_level (int keep)
701 keep_next_level_flag = keep;
704 /* Nonzero if the current level needs to have a BLOCK made. */
707 kept_level_p (void)
709 return (current_binding_level->blocks != NULL_TREE
710 || current_binding_level->keep
711 || current_binding_level->names != NULL_TREE
712 || (current_binding_level->type_decls != NULL
713 && !current_binding_level->tag_transparent));
716 /* Returns nonzero if this scope was created to store template
717 parameters. */
720 template_parm_scope_p (void)
722 return current_binding_level->template_parms_p;
725 /* Returns the kind of template specialization we are currently
726 processing, given that it's declaration contained N_CLASS_SCOPES
727 explicit scope qualifications. */
729 tmpl_spec_kind
730 current_tmpl_spec_kind (int n_class_scopes)
732 int n_template_parm_scopes = 0;
733 int seen_specialization_p = 0;
734 int innermost_specialization_p = 0;
735 struct cp_binding_level *b;
737 /* Scan through the template parameter scopes. */
738 for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
740 /* If we see a specialization scope inside a parameter scope,
741 then something is wrong. That corresponds to a declaration
742 like:
744 template <class T> template <> ...
746 which is always invalid since [temp.expl.spec] forbids the
747 specialization of a class member template if the enclosing
748 class templates are not explicitly specialized as well. */
749 if (b->template_spec_p)
751 if (n_template_parm_scopes == 0)
752 innermost_specialization_p = 1;
753 else
754 seen_specialization_p = 1;
756 else if (seen_specialization_p == 1)
757 return tsk_invalid_member_spec;
759 ++n_template_parm_scopes;
762 /* Handle explicit instantiations. */
763 if (processing_explicit_instantiation)
765 if (n_template_parm_scopes != 0)
766 /* We've seen a template parameter list during an explicit
767 instantiation. For example:
769 template <class T> template void f(int);
771 This is erroneous. */
772 return tsk_invalid_expl_inst;
773 else
774 return tsk_expl_inst;
777 if (n_template_parm_scopes < n_class_scopes)
778 /* We've not seen enough template headers to match all the
779 specialized classes present. For example:
781 template <class T> void R<T>::S<T>::f(int);
783 This is invalid; there needs to be one set of template
784 parameters for each class. */
785 return tsk_insufficient_parms;
786 else if (n_template_parm_scopes == n_class_scopes)
787 /* We're processing a non-template declaration (even though it may
788 be a member of a template class.) For example:
790 template <class T> void S<T>::f(int);
792 The `class T' maches the `S<T>', leaving no template headers
793 corresponding to the `f'. */
794 return tsk_none;
795 else if (n_template_parm_scopes > n_class_scopes + 1)
796 /* We've got too many template headers. For example:
798 template <> template <class T> void f (T);
800 There need to be more enclosing classes. */
801 return tsk_excessive_parms;
802 else
803 /* This must be a template. It's of the form:
805 template <class T> template <class U> void S<T>::f(U);
807 This is a specialization if the innermost level was a
808 specialization; otherwise it's just a definition of the
809 template. */
810 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
813 void
814 set_class_shadows (tree shadows)
816 class_binding_level->class_shadowed = shadows;
819 /* Enter a new binding level.
820 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
821 not for that of tags. */
823 void
824 pushlevel (int tag_transparent)
826 if (cfun && !doing_semantic_analysis_p ())
827 return;
829 push_binding_level (make_cxx_scope (tag_transparent, keep_next_level_flag));
830 keep_next_level_flag = 0;
833 /* We're defining an object of type TYPE. If it needs a cleanup, but
834 we're not allowed to add any more objects with cleanups to the current
835 scope, create a new binding level. */
837 void
838 maybe_push_cleanup_level (tree type)
840 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
841 && current_binding_level->more_cleanups_ok == 0)
843 keep_next_level (2);
844 pushlevel (1);
845 clear_last_expr ();
846 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
850 /* Enter a new scope. The KIND indicates what kind of scope is being
851 created. */
853 void
854 begin_scope (scope_kind sk)
856 pushlevel (0);
858 switch (sk)
860 case sk_block:
861 break;
863 case sk_try:
864 current_binding_level->is_try_scope = 1;
865 break;
867 case sk_catch:
868 current_binding_level->is_catch_scope = 1;
869 break;
871 case sk_for:
872 current_binding_level->is_for_scope = 1;
873 break;
875 case sk_template_spec:
876 current_binding_level->template_spec_p = 1;
877 /* Fall through. */
879 case sk_template_parms:
880 current_binding_level->template_parms_p = 1;
881 break;
883 default:
884 abort ();
888 /* Exit the current scope. */
890 void
891 finish_scope (void)
893 poplevel (0, 0, 0);
896 /* Make DECL the innermost binding for ID. The LEVEL is the binding
897 level at which this declaration is being bound. */
899 static void
900 push_binding (tree id, tree decl, cxx_scope* level)
902 cxx_binding *binding = cxx_binding_make (decl, NULL);
904 /* Now, fill in the binding information. */
905 binding->previous = IDENTIFIER_BINDING (id);
906 BINDING_SCOPE (binding) = level;
907 INHERITED_VALUE_BINDING_P (binding) = 0;
908 LOCAL_BINDING_P (binding) = (level != class_binding_level);
910 /* And put it on the front of the list of bindings for ID. */
911 IDENTIFIER_BINDING (id) = binding;
914 /* ID is already bound in the current scope. But, DECL is an
915 additional binding for ID in the same scope. This is the `struct
916 stat' hack whereby a non-typedef class-name or enum-name can be
917 bound at the same level as some other kind of entity. It's the
918 responsibility of the caller to check that inserting this name is
919 valid here. Returns nonzero if the new binding was successful. */
922 add_binding (cxx_binding *binding, tree decl)
924 tree bval = BINDING_VALUE (binding);
925 int ok = 1;
927 timevar_push (TV_NAME_LOOKUP);
928 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
929 /* The new name is the type name. */
930 BINDING_TYPE (binding) = decl;
931 else if (!bval)
932 /* This situation arises when push_class_level_binding moves an
933 inherited type-binding out of the way to make room for a new
934 value binding. */
935 BINDING_VALUE (binding) = decl;
936 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
938 /* The old binding was a type name. It was placed in
939 BINDING_VALUE because it was thought, at the point it was
940 declared, to be the only entity with such a name. Move the
941 type name into the type slot; it is now hidden by the new
942 binding. */
943 BINDING_TYPE (binding) = bval;
944 BINDING_VALUE (binding) = decl;
945 INHERITED_VALUE_BINDING_P (binding) = 0;
947 else if (TREE_CODE (bval) == TYPE_DECL
948 && TREE_CODE (decl) == TYPE_DECL
949 && DECL_NAME (decl) == DECL_NAME (bval)
950 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
951 /* If either type involves template parameters, we must
952 wait until instantiation. */
953 || uses_template_parms (TREE_TYPE (decl))
954 || uses_template_parms (TREE_TYPE (bval))))
955 /* We have two typedef-names, both naming the same type to have
956 the same name. This is OK because of:
958 [dcl.typedef]
960 In a given scope, a typedef specifier can be used to redefine
961 the name of any type declared in that scope to refer to the
962 type to which it already refers. */
963 ok = 0;
964 /* There can be two block-scope declarations of the same variable,
965 so long as they are `extern' declarations. However, there cannot
966 be two declarations of the same static data member:
968 [class.mem]
970 A member shall not be declared twice in the
971 member-specification. */
972 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
973 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
974 && !DECL_CLASS_SCOPE_P (decl))
976 duplicate_decls (decl, BINDING_VALUE (binding));
977 ok = 0;
979 else
981 error ("declaration of `%#D'", decl);
982 cp_error_at ("conflicts with previous declaration `%#D'",
983 BINDING_VALUE (binding));
984 ok = 0;
987 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
990 /* Add DECL to the list of things declared in B. */
992 static void
993 add_decl_to_level (tree decl,
994 struct cp_binding_level* b)
996 if (TREE_CODE (decl) == NAMESPACE_DECL
997 && !DECL_NAMESPACE_ALIAS (decl))
999 TREE_CHAIN (decl) = b->namespaces;
1000 b->namespaces = decl;
1002 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
1004 TREE_CHAIN (decl) = b->vtables;
1005 b->vtables = decl;
1007 else
1009 /* We build up the list in reverse order, and reverse it later if
1010 necessary. */
1011 TREE_CHAIN (decl) = b->names;
1012 b->names = decl;
1013 b->names_size++;
1015 /* If appropriate, add decl to separate list of statics */
1016 if (b->namespace_p)
1017 if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
1018 || (TREE_CODE (decl) == FUNCTION_DECL
1019 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
1020 VARRAY_PUSH_TREE (b->static_decls, decl);
1024 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1025 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1026 doesn't really belong to this binding level, that it got here
1027 through a using-declaration. */
1029 void
1030 push_local_binding (tree id, tree decl, int flags)
1032 struct cp_binding_level *b;
1034 /* Skip over any local classes. This makes sense if we call
1035 push_local_binding with a friend decl of a local class. */
1036 b = current_binding_level;
1037 while (b->parm_flag == 2)
1038 b = b->level_chain;
1040 if (lookup_name_current_level (id))
1042 /* Supplement the existing binding. */
1043 if (!add_binding (IDENTIFIER_BINDING (id), decl))
1044 /* It didn't work. Something else must be bound at this
1045 level. Do not add DECL to the list of things to pop
1046 later. */
1047 return;
1049 else
1050 /* Create a new binding. */
1051 push_binding (id, decl, b);
1053 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1054 /* We must put the OVERLOAD into a TREE_LIST since the
1055 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1056 decls that got here through a using-declaration. */
1057 decl = build_tree_list (NULL_TREE, decl);
1059 /* And put DECL on the list of things declared by the current
1060 binding level. */
1061 add_decl_to_level (decl, b);
1064 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1065 binding was successful. */
1068 push_class_binding (tree id, tree decl)
1070 int result = 1;
1071 cxx_binding *binding = IDENTIFIER_BINDING (id);
1072 tree context;
1074 timevar_push (TV_NAME_LOOKUP);
1075 /* Note that we declared this value so that we can issue an error if
1076 this is an invalid redeclaration of a name already used for some
1077 other purpose. */
1078 note_name_declared_in_class (id, decl);
1080 if (binding && BINDING_SCOPE (binding) == class_binding_level)
1081 /* Supplement the existing binding. */
1082 result = add_binding (IDENTIFIER_BINDING (id), decl);
1083 else
1084 /* Create a new binding. */
1085 push_binding (id, decl, class_binding_level);
1087 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1088 class-level declaration. Note that we do not use DECL here
1089 because of the possibility of the `struct stat' hack; if DECL is
1090 a class-name or enum-name we might prefer a field-name, or some
1091 such. */
1092 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1094 /* If this is a binding from a base class, mark it as such. */
1095 binding = IDENTIFIER_BINDING (id);
1096 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1098 if (TREE_CODE (decl) == OVERLOAD)
1099 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1100 else
1102 my_friendly_assert (DECL_P (decl), 0);
1103 context = context_for_name_lookup (decl);
1106 if (is_properly_derived_from (current_class_type, context))
1107 INHERITED_VALUE_BINDING_P (binding) = 1;
1108 else
1109 INHERITED_VALUE_BINDING_P (binding) = 0;
1111 else if (BINDING_VALUE (binding) == decl)
1112 /* We only encounter a TREE_LIST when push_class_decls detects an
1113 ambiguity. Such an ambiguity can be overridden by a definition
1114 in this class. */
1115 INHERITED_VALUE_BINDING_P (binding) = 1;
1117 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1120 /* Remove the binding for DECL which should be the innermost binding
1121 for ID. */
1123 static void
1124 pop_binding (tree id, tree decl)
1126 cxx_binding *binding;
1128 if (id == NULL_TREE)
1129 /* It's easiest to write the loops that call this function without
1130 checking whether or not the entities involved have names. We
1131 get here for such an entity. */
1132 return;
1134 /* Get the innermost binding for ID. */
1135 binding = IDENTIFIER_BINDING (id);
1137 /* The name should be bound. */
1138 my_friendly_assert (binding != NULL, 0);
1140 /* The DECL will be either the ordinary binding or the type
1141 binding for this identifier. Remove that binding. */
1142 if (BINDING_VALUE (binding) == decl)
1143 BINDING_VALUE (binding) = NULL_TREE;
1144 else if (BINDING_TYPE (binding) == decl)
1145 BINDING_TYPE (binding) = NULL_TREE;
1146 else
1147 abort ();
1149 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1151 /* We're completely done with the innermost binding for this
1152 identifier. Unhook it from the list of bindings. */
1153 IDENTIFIER_BINDING (id) = binding->previous;
1155 /* Add it to the free list. */
1156 cxx_binding_free (binding);
1158 /* Clear the BINDING_SCOPE so the garbage collector doesn't walk
1159 it. */
1160 BINDING_SCOPE (binding) = NULL;
1164 /* When a label goes out of scope, check to see if that label was used
1165 in a valid manner, and issue any appropriate warnings or errors. */
1167 static void
1168 pop_label (tree label, tree old_value)
1170 if (!processing_template_decl && doing_semantic_analysis_p ())
1172 if (DECL_INITIAL (label) == NULL_TREE)
1174 location_t location;
1176 cp_error_at ("label `%D' used but not defined", label);
1177 location.file = input_filename;
1178 location.line = 0;
1179 /* Avoid crashing later. */
1180 define_label (location, DECL_NAME (label));
1182 else if (warn_unused_label && !TREE_USED (label))
1183 cp_warning_at ("label `%D' defined but not used", label);
1186 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1189 /* At the end of a function, all labels declared within the function
1190 go out of scope. BLOCK is the top-level block for the
1191 function. */
1193 static void
1194 pop_labels (tree block)
1196 struct named_label_list *link;
1198 /* Clear out the definitions of all label names, since their scopes
1199 end here. */
1200 for (link = named_labels; link; link = link->next)
1202 pop_label (link->label_decl, link->old_value);
1203 /* Put the labels into the "variables" of the top-level block,
1204 so debugger can see them. */
1205 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1206 BLOCK_VARS (block) = link->label_decl;
1209 named_labels = NULL;
1212 /* Exit a binding level.
1213 Pop the level off, and restore the state of the identifier-decl mappings
1214 that were in effect when this level was entered.
1216 If KEEP == 1, this level had explicit declarations, so
1217 and create a "block" (a BLOCK node) for the level
1218 to record its declarations and subblocks for symbol table output.
1220 If FUNCTIONBODY is nonzero, this level is the body of a function,
1221 so create a block as if KEEP were set and also clear out all
1222 label names.
1224 If REVERSE is nonzero, reverse the order of decls before putting
1225 them into the BLOCK. */
1227 tree
1228 poplevel (int keep, int reverse, int functionbody)
1230 register tree link;
1231 /* The chain of decls was accumulated in reverse order.
1232 Put it into forward order, just for cleanliness. */
1233 tree decls;
1234 int tmp = functionbody;
1235 int real_functionbody;
1236 tree subblocks;
1237 tree block = NULL_TREE;
1238 tree decl;
1239 int leaving_for_scope;
1241 timevar_push (TV_NAME_LOOKUP);
1242 if (cfun && !doing_semantic_analysis_p ())
1243 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1245 my_friendly_assert (current_binding_level->parm_flag != 2,
1246 19990916);
1248 real_functionbody = (current_binding_level->keep == 2
1249 ? ((functionbody = 0), tmp) : functionbody);
1250 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1252 my_friendly_assert (!current_binding_level->class_shadowed,
1253 19990414);
1255 /* We used to use KEEP == 2 to indicate that the new block should go
1256 at the beginning of the list of blocks at this binding level,
1257 rather than the end. This hack is no longer used. */
1258 my_friendly_assert (keep == 0 || keep == 1, 0);
1260 if (current_binding_level->keep == 1)
1261 keep = 1;
1263 /* Any uses of undefined labels, and any defined labels, now operate
1264 under constraints of next binding contour. */
1265 if (cfun && !functionbody)
1267 struct cp_binding_level *level_chain;
1268 level_chain = current_binding_level->level_chain;
1269 if (level_chain)
1271 struct named_label_use_list *uses;
1272 struct named_label_list *labels;
1273 for (labels = named_labels; labels; labels = labels->next)
1274 if (labels->binding_level == current_binding_level)
1276 tree decl;
1277 if (current_binding_level->is_try_scope)
1278 labels->in_try_scope = 1;
1279 if (current_binding_level->is_catch_scope)
1280 labels->in_catch_scope = 1;
1281 for (decl = labels->names_in_scope; decl;
1282 decl = TREE_CHAIN (decl))
1283 if (decl_jump_unsafe (decl))
1284 labels->bad_decls = tree_cons (NULL_TREE, decl,
1285 labels->bad_decls);
1286 labels->binding_level = level_chain;
1287 labels->names_in_scope = level_chain->names;
1290 for (uses = named_label_uses; uses; uses = uses->next)
1291 if (uses->binding_level == current_binding_level)
1293 uses->binding_level = level_chain;
1294 uses->names_in_scope = level_chain->names;
1299 /* Get the decls in the order they were written.
1300 Usually current_binding_level->names is in reverse order.
1301 But parameter decls were previously put in forward order. */
1303 if (reverse)
1304 current_binding_level->names
1305 = decls = nreverse (current_binding_level->names);
1306 else
1307 decls = current_binding_level->names;
1309 /* Output any nested inline functions within this block
1310 if they weren't already output. */
1311 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1312 if (TREE_CODE (decl) == FUNCTION_DECL
1313 && ! TREE_ASM_WRITTEN (decl)
1314 && DECL_INITIAL (decl) != NULL_TREE
1315 && TREE_ADDRESSABLE (decl)
1316 && decl_function_context (decl) == current_function_decl)
1318 /* If this decl was copied from a file-scope decl
1319 on account of a block-scope extern decl,
1320 propagate TREE_ADDRESSABLE to the file-scope decl. */
1321 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1322 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1323 else
1325 push_function_context ();
1326 output_inline_function (decl);
1327 pop_function_context ();
1331 /* When not in function-at-a-time mode, expand_end_bindings will
1332 warn about unused variables. But, in function-at-a-time mode
1333 expand_end_bindings is not passed the list of variables in the
1334 current scope, and therefore no warning is emitted. So, we
1335 explicitly warn here. */
1336 if (!processing_template_decl)
1337 warn_about_unused_variables (getdecls ());
1339 /* If there were any declarations or structure tags in that level,
1340 or if this level is a function body,
1341 create a BLOCK to record them for the life of this function. */
1342 block = NULL_TREE;
1343 if (keep == 1 || functionbody)
1344 block = make_node (BLOCK);
1345 if (block != NULL_TREE)
1347 BLOCK_VARS (block) = decls;
1348 BLOCK_SUBBLOCKS (block) = subblocks;
1351 /* In each subblock, record that this is its superior. */
1352 if (keep >= 0)
1353 for (link = subblocks; link; link = TREE_CHAIN (link))
1354 BLOCK_SUPERCONTEXT (link) = block;
1356 /* We still support the old for-scope rules, whereby the variables
1357 in a for-init statement were in scope after the for-statement
1358 ended. We only use the new rules if flag_new_for_scope is
1359 nonzero. */
1360 leaving_for_scope
1361 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1363 /* Remove declarations for all the DECLs in this level. */
1364 for (link = decls; link; link = TREE_CHAIN (link))
1366 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1367 && DECL_NAME (link))
1369 cxx_binding *outer_binding
1370 = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
1371 tree ns_binding;
1373 if (!outer_binding)
1374 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1375 else
1376 ns_binding = NULL_TREE;
1378 if (outer_binding
1379 && (BINDING_SCOPE (outer_binding)
1380 == current_binding_level->level_chain))
1381 /* We have something like:
1383 int i;
1384 for (int i; ;);
1386 and we are leaving the `for' scope. There's no reason to
1387 keep the binding of the inner `i' in this case. */
1388 pop_binding (DECL_NAME (link), link);
1389 else if ((outer_binding
1390 && (TREE_CODE (BINDING_VALUE (outer_binding))
1391 == TYPE_DECL))
1392 || (ns_binding
1393 && TREE_CODE (ns_binding) == TYPE_DECL))
1394 /* Here, we have something like:
1396 typedef int I;
1398 void f () {
1399 for (int I; ;);
1402 We must pop the for-scope binding so we know what's a
1403 type and what isn't. */
1404 pop_binding (DECL_NAME (link), link);
1405 else
1407 /* Mark this VAR_DECL as dead so that we can tell we left it
1408 there only for backward compatibility. */
1409 DECL_DEAD_FOR_LOCAL (link) = 1;
1411 /* Keep track of what should have happened when we
1412 popped the binding. */
1413 if (outer_binding && BINDING_VALUE (outer_binding))
1414 DECL_SHADOWED_FOR_VAR (link)
1415 = BINDING_VALUE (outer_binding);
1417 /* Add it to the list of dead variables in the next
1418 outermost binding to that we can remove these when we
1419 leave that binding. */
1420 current_binding_level->level_chain->dead_vars_from_for
1421 = tree_cons (NULL_TREE, link,
1422 current_binding_level->level_chain->
1423 dead_vars_from_for);
1425 /* Although we don't pop the cxx_binding, we do clear
1426 its BINDING_SCOPE since the level is going away now. */
1427 BINDING_SCOPE (IDENTIFIER_BINDING (DECL_NAME (link))) = 0;
1430 else
1432 /* Remove the binding. */
1433 decl = link;
1434 if (TREE_CODE (decl) == TREE_LIST)
1435 decl = TREE_VALUE (decl);
1436 if (DECL_P (decl))
1437 pop_binding (DECL_NAME (decl), decl);
1438 else if (TREE_CODE (decl) == OVERLOAD)
1439 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1440 else
1441 abort ();
1445 /* Remove declarations for any `for' variables from inner scopes
1446 that we kept around. */
1447 for (link = current_binding_level->dead_vars_from_for;
1448 link; link = TREE_CHAIN (link))
1449 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1451 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1452 for (link = current_binding_level->type_shadowed;
1453 link; link = TREE_CHAIN (link))
1454 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1456 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1457 for (link = current_binding_level->shadowed_labels;
1458 link;
1459 link = TREE_CHAIN (link))
1460 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1462 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1463 list if a `using' declaration put them there. The debugging
1464 back-ends won't understand OVERLOAD, so we remove them here.
1465 Because the BLOCK_VARS are (temporarily) shared with
1466 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1467 popped all the bindings. */
1468 if (block)
1470 tree* d;
1472 for (d = &BLOCK_VARS (block); *d; )
1474 if (TREE_CODE (*d) == TREE_LIST)
1475 *d = TREE_CHAIN (*d);
1476 else
1477 d = &TREE_CHAIN (*d);
1481 /* If the level being exited is the top level of a function,
1482 check over all the labels. */
1483 if (functionbody)
1485 /* Since this is the top level block of a function, the vars are
1486 the function's parameters. Don't leave them in the BLOCK
1487 because they are found in the FUNCTION_DECL instead. */
1488 BLOCK_VARS (block) = 0;
1489 pop_labels (block);
1492 tmp = current_binding_level->keep;
1494 pop_binding_level ();
1495 if (functionbody)
1496 DECL_INITIAL (current_function_decl) = block;
1497 else if (block)
1498 current_binding_level->blocks
1499 = chainon (current_binding_level->blocks, block);
1501 /* If we did not make a block for the level just exited,
1502 any blocks made for inner levels
1503 (since they cannot be recorded as subblocks in that level)
1504 must be carried forward so they will later become subblocks
1505 of something else. */
1506 else if (subblocks)
1507 current_binding_level->blocks
1508 = chainon (current_binding_level->blocks, subblocks);
1510 /* Each and every BLOCK node created here in `poplevel' is important
1511 (e.g. for proper debugging information) so if we created one
1512 earlier, mark it as "used". */
1513 if (block)
1514 TREE_USED (block) = 1;
1516 /* Take care of compiler's internal binding structures. */
1517 if (tmp == 2)
1519 tree scope_stmts;
1521 scope_stmts
1522 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1523 if (block)
1525 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1526 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1529 block = poplevel (keep, reverse, functionbody);
1532 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1535 /* Delete the node BLOCK from the current binding level.
1536 This is used for the block inside a stmt expr ({...})
1537 so that the block can be reinserted where appropriate. */
1539 void
1540 delete_block (tree block)
1542 tree t;
1543 if (current_binding_level->blocks == block)
1544 current_binding_level->blocks = TREE_CHAIN (block);
1545 for (t = current_binding_level->blocks; t;)
1547 if (TREE_CHAIN (t) == block)
1548 TREE_CHAIN (t) = TREE_CHAIN (block);
1549 else
1550 t = TREE_CHAIN (t);
1552 TREE_CHAIN (block) = NULL_TREE;
1553 /* Clear TREE_USED which is always set by poplevel.
1554 The flag is set again if insert_block is called. */
1555 TREE_USED (block) = 0;
1558 /* Insert BLOCK at the end of the list of subblocks of the
1559 current binding level. This is used when a BIND_EXPR is expanded,
1560 to handle the BLOCK node inside the BIND_EXPR. */
1562 void
1563 insert_block (tree block)
1565 TREE_USED (block) = 1;
1566 current_binding_level->blocks
1567 = chainon (current_binding_level->blocks, block);
1570 /* Set the BLOCK node for the innermost scope
1571 (the one we are currently in). */
1573 void
1574 set_block (tree block ATTRIBUTE_UNUSED )
1576 /* The RTL expansion machinery requires us to provide this callback,
1577 but it is not applicable in function-at-a-time mode. */
1578 my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1581 /* Do a pushlevel for class declarations. */
1583 void
1584 pushlevel_class (void)
1586 if (ENABLE_SCOPE_CHECKING)
1587 is_class_level = 1;
1589 push_binding_level (make_cxx_scope (false, 0));
1591 class_binding_level = current_binding_level;
1592 class_binding_level->parm_flag = 2;
1593 class_binding_level->this_entity = current_class_type;
1596 /* ...and a poplevel for class declarations. */
1598 void
1599 poplevel_class (void)
1601 register struct cp_binding_level *level = class_binding_level;
1602 tree shadowed;
1604 timevar_push (TV_NAME_LOOKUP);
1605 my_friendly_assert (level != 0, 354);
1607 /* If we're leaving a toplevel class, don't bother to do the setting
1608 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1609 shouldn't even be used when current_class_type isn't set, and second,
1610 if we don't touch it here, we're able to use the cache effect if the
1611 next time we're entering a class scope, it is the same class. */
1612 if (current_class_depth != 1)
1614 struct cp_binding_level* b;
1616 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1617 for (shadowed = level->class_shadowed;
1618 shadowed;
1619 shadowed = TREE_CHAIN (shadowed))
1620 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1622 /* Find the next enclosing class, and recreate
1623 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1624 b = level->level_chain;
1625 while (b && b->parm_flag != 2)
1626 b = b->level_chain;
1628 if (b)
1629 for (shadowed = b->class_shadowed;
1630 shadowed;
1631 shadowed = TREE_CHAIN (shadowed))
1633 cxx_binding *binding;
1635 binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1636 while (binding && BINDING_SCOPE (binding) != b)
1637 binding = binding->previous;
1639 if (binding)
1640 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1641 = BINDING_VALUE (binding);
1644 else
1645 /* Remember to save what IDENTIFIER's were bound in this scope so we
1646 can recover from cache misses. */
1648 previous_class_type = current_class_type;
1649 previous_class_values = class_binding_level->class_shadowed;
1651 for (shadowed = level->type_shadowed;
1652 shadowed;
1653 shadowed = TREE_CHAIN (shadowed))
1654 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1656 /* Remove the bindings for all of the class-level declarations. */
1657 for (shadowed = level->class_shadowed;
1658 shadowed;
1659 shadowed = TREE_CHAIN (shadowed))
1660 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1662 /* Now, pop out of the binding level which we created up in the
1663 `pushlevel_class' routine. */
1664 if (ENABLE_SCOPE_CHECKING)
1665 is_class_level = 1;
1667 pop_binding_level ();
1668 timevar_pop (TV_NAME_LOOKUP);
1671 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1672 for any names in enclosing classes. */
1674 void
1675 clear_identifier_class_values (void)
1677 tree t;
1679 if (!class_binding_level)
1680 return;
1682 for (t = class_binding_level->class_shadowed;
1684 t = TREE_CHAIN (t))
1685 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1688 /* Returns nonzero if T is a virtual function table. */
1691 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1693 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1696 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1697 functions. */
1700 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1702 return (TREE_CODE (t) == TYPE_DECL
1703 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1704 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1707 /* Return the declarations that are members of the namespace NS. */
1709 tree
1710 cp_namespace_decls (tree ns)
1712 return NAMESPACE_LEVEL (ns)->names;
1715 struct walk_globals_data {
1716 walk_globals_pred p;
1717 walk_globals_fn f;
1718 void *data;
1721 /* Walk the vtable declarations in NAMESPACE. Whenever one is found
1722 for which P returns nonzero, call F with its address. If any call
1723 to F returns a nonzero value, return a nonzero value. */
1725 static int
1726 walk_vtables_r (tree namespace, void* data)
1728 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1729 walk_globals_fn f = wgd->f;
1730 void *d = wgd->data;
1731 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1732 int result = 0;
1734 for (; decl ; decl = TREE_CHAIN (decl))
1735 result |= (*f) (&decl, d);
1737 return result;
1740 /* Walk the vtable declarations. Whenever one is found for which P
1741 returns nonzero, call F with its address. If any call to F
1742 returns a nonzero value, return a nonzero value. */
1743 bool
1744 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1746 struct walk_globals_data wgd;
1747 wgd.p = p;
1748 wgd.f = f;
1749 wgd.data = data;
1751 return walk_namespaces (walk_vtables_r, &wgd);
1754 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1755 itself, calling F for each. The DATA is passed to F as well. */
1757 static int
1758 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1760 int result = 0;
1761 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
1763 result |= (*f) (namespace, data);
1765 for (; current; current = TREE_CHAIN (current))
1766 result |= walk_namespaces_r (current, f, data);
1768 return result;
1771 /* Walk all the namespaces, calling F for each. The DATA is passed to
1772 F as well. */
1775 walk_namespaces (walk_namespaces_fn f, void* data)
1777 return walk_namespaces_r (global_namespace, f, data);
1780 /* Walk the global declarations in NAMESPACE. Whenever one is found
1781 for which P returns nonzero, call F with its address. If any call
1782 to F returns a nonzero value, return a nonzero value. */
1784 static int
1785 walk_globals_r (tree namespace, void* data)
1787 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1788 walk_globals_pred p = wgd->p;
1789 walk_globals_fn f = wgd->f;
1790 void *d = wgd->data;
1791 tree *t;
1792 int result = 0;
1794 t = &NAMESPACE_LEVEL (namespace)->names;
1796 while (*t)
1798 tree glbl = *t;
1800 if ((*p) (glbl, d))
1801 result |= (*f) (t, d);
1803 /* If F changed *T, then *T still points at the next item to
1804 examine. */
1805 if (*t == glbl)
1806 t = &TREE_CHAIN (*t);
1809 return result;
1812 /* Walk the global declarations. Whenever one is found for which P
1813 returns true, call F with its address. If any call to F
1814 returns true, return true. */
1816 bool
1817 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1819 struct walk_globals_data wgd;
1820 wgd.p = p;
1821 wgd.f = f;
1822 wgd.data = data;
1824 return walk_namespaces (walk_globals_r, &wgd);
1827 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1828 DATA is non-NULL, this is the last time we will call
1829 wrapup_global_declarations for this NAMESPACE. */
1832 wrapup_globals_for_namespace (tree namespace, void* data)
1834 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
1835 varray_type statics = level->static_decls;
1836 tree *vec = &VARRAY_TREE (statics, 0);
1837 int len = VARRAY_ACTIVE_SIZE (statics);
1838 int last_time = (data != 0);
1840 if (last_time)
1842 check_global_declarations (vec, len);
1843 return 0;
1846 /* Write out any globals that need to be output. */
1847 return wrapup_global_declarations (vec, len);
1851 /* For debugging. */
1852 static int no_print_functions = 0;
1853 static int no_print_builtins = 0;
1855 /* Called from print_binding_level through binding_table_foreach to
1856 print the content of binding ENTRY. DATA is a pointer to line offset
1857 marker. */
1858 static void
1859 bt_print_entry (binding_entry entry, void *data)
1861 int *p = (int *) data;
1862 int len;
1864 if (entry->name == NULL)
1865 len = 3;
1866 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1867 len = 2;
1868 else
1869 len = 4;
1870 len = 4;
1872 *p += len;
1874 if (*p > 5)
1876 fprintf (stderr, "\n\t");
1877 *p = len;
1879 if (entry->name == NULL)
1881 print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
1882 fprintf (stderr, ">");
1884 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1885 print_node_brief (stderr, "", entry->type, 0);
1886 else
1888 print_node_brief (stderr, "<typedef", entry->name, 0);
1889 print_node_brief (stderr, "", entry->type, 0);
1890 fprintf (stderr, ">");
1894 void
1895 print_binding_level (struct cp_binding_level* lvl)
1897 tree t;
1898 int i = 0, len;
1899 fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks);
1900 if (lvl->tag_transparent)
1901 fprintf (stderr, " tag-transparent");
1902 if (lvl->more_cleanups_ok)
1903 fprintf (stderr, " more-cleanups-ok");
1904 if (lvl->have_cleanups)
1905 fprintf (stderr, " have-cleanups");
1906 fprintf (stderr, "\n");
1907 if (lvl->names)
1909 fprintf (stderr, " names:\t");
1910 /* We can probably fit 3 names to a line? */
1911 for (t = lvl->names; t; t = TREE_CHAIN (t))
1913 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1914 continue;
1915 if (no_print_builtins
1916 && (TREE_CODE (t) == TYPE_DECL)
1917 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1918 continue;
1920 /* Function decls tend to have longer names. */
1921 if (TREE_CODE (t) == FUNCTION_DECL)
1922 len = 3;
1923 else
1924 len = 2;
1925 i += len;
1926 if (i > 6)
1928 fprintf (stderr, "\n\t");
1929 i = len;
1931 print_node_brief (stderr, "", t, 0);
1932 if (t == error_mark_node)
1933 break;
1935 if (i)
1936 fprintf (stderr, "\n");
1938 if (lvl->type_decls)
1940 fprintf (stderr, " tags:\t");
1941 i = 0;
1942 binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
1943 if (i)
1944 fprintf (stderr, "\n");
1946 if (lvl->class_shadowed)
1948 fprintf (stderr, " class-shadowed:");
1949 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1951 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1953 fprintf (stderr, "\n");
1955 if (lvl->type_shadowed)
1957 fprintf (stderr, " type-shadowed:");
1958 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1960 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1962 fprintf (stderr, "\n");
1966 void
1967 print_other_binding_stack (struct cp_binding_level *stack)
1969 struct cp_binding_level *level;
1970 for (level = stack; !global_scope_p (level); level = level->level_chain)
1972 fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level);
1973 print_binding_level (level);
1977 void
1978 print_binding_stack (void)
1980 struct cp_binding_level *b;
1981 fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF
1982 "\nclass_binding_level=" HOST_PTR_PRINTF
1983 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n",
1984 (void *) current_binding_level, (void *) class_binding_level,
1985 (void *) NAMESPACE_LEVEL (global_namespace));
1986 if (class_binding_level)
1988 for (b = class_binding_level; b; b = b->level_chain)
1989 if (b == current_binding_level)
1990 break;
1991 if (b)
1992 b = class_binding_level;
1993 else
1994 b = current_binding_level;
1996 else
1997 b = current_binding_level;
1998 print_other_binding_stack (b);
1999 fprintf (stderr, "global:\n");
2000 print_binding_level (NAMESPACE_LEVEL (global_namespace));
2003 /* Namespace binding access routines: The namespace_bindings field of
2004 the identifier is polymorphic, with three possible values:
2005 NULL_TREE, a list of "cxx_binding"s. */
2008 /* Push the initial binding contour of NAMESPACE-scope. Any subsequent
2009 push of NS is actually a resume. */
2010 static void
2011 initial_push_namespace_scope (tree ns)
2013 tree name = DECL_NAME (ns);
2014 cxx_scope *scope;
2016 pushlevel (0);
2017 scope = current_binding_level;
2018 scope->namespace_p = true;
2019 scope->type_decls = binding_table_new (name == std_identifier
2020 ? NAMESPACE_STD_HT_SIZE
2021 : (name == global_scope_name
2022 ? GLOBAL_SCOPE_HT_SIZE
2023 : NAMESPACE_ORDINARY_HT_SIZE));
2024 VARRAY_TREE_INIT (scope->static_decls,
2025 name == std_identifier || name == global_scope_name
2026 ? 200 : 10,
2027 "Static declarations");
2028 scope->this_entity = ns;
2029 NAMESPACE_LEVEL (ns) = scope;
2032 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2033 select a name that is unique to this compilation unit. */
2035 void
2036 push_namespace (tree name)
2038 tree d = NULL_TREE;
2039 int need_new = 1;
2040 int implicit_use = 0;
2042 timevar_push (TV_NAME_LOOKUP);
2044 /* We should not get here if the global_namespace is not yet constructed
2045 nor if NAME designates the global namespace: The global scope is
2046 constructed elsewhere. */
2047 my_friendly_assert (global_namespace != NULL && name != global_scope_name,
2048 20030531);
2050 if (!name)
2052 /* The name of anonymous namespace is unique for the translation
2053 unit. */
2054 if (!anonymous_namespace_name)
2055 anonymous_namespace_name = get_file_function_name ('N');
2056 name = anonymous_namespace_name;
2057 d = IDENTIFIER_NAMESPACE_VALUE (name);
2058 if (d)
2059 /* Reopening anonymous namespace. */
2060 need_new = 0;
2061 implicit_use = 1;
2063 else
2065 /* Check whether this is an extended namespace definition. */
2066 d = IDENTIFIER_NAMESPACE_VALUE (name);
2067 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2069 need_new = 0;
2070 if (DECL_NAMESPACE_ALIAS (d))
2072 error ("namespace alias `%D' not allowed here, assuming `%D'",
2073 d, DECL_NAMESPACE_ALIAS (d));
2074 d = DECL_NAMESPACE_ALIAS (d);
2079 if (need_new)
2081 /* Make a new namespace, binding the name to it. */
2082 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2083 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2084 d = pushdecl (d);
2085 initial_push_namespace_scope (d);
2087 else
2088 resume_binding_level (NAMESPACE_LEVEL (d));
2090 if (implicit_use)
2091 do_using_directive (d);
2092 /* Enter the name space. */
2093 current_namespace = d;
2095 timevar_pop (TV_NAME_LOOKUP);
2098 /* Pop from the scope of the current namespace. */
2100 void
2101 pop_namespace (void)
2103 my_friendly_assert (current_namespace != global_namespace, 20010801);
2104 current_namespace = CP_DECL_CONTEXT (current_namespace);
2105 /* The binding level is not popped, as it might be re-opened later. */
2106 suspend_binding_level ();
2109 /* Push into the scope of the namespace NS, even if it is deeply
2110 nested within another namespace. */
2112 void
2113 push_nested_namespace (tree ns)
2115 if (ns == global_namespace)
2116 push_to_top_level ();
2117 else
2119 push_nested_namespace (CP_DECL_CONTEXT (ns));
2120 push_namespace (DECL_NAME (ns));
2124 /* Pop back from the scope of the namespace NS, which was previously
2125 entered with push_nested_namespace. */
2127 void
2128 pop_nested_namespace (tree ns)
2130 timevar_push (TV_NAME_LOOKUP);
2131 while (ns != global_namespace)
2133 pop_namespace ();
2134 ns = CP_DECL_CONTEXT (ns);
2137 pop_from_top_level ();
2138 timevar_pop (TV_NAME_LOOKUP);
2142 /* Allocate storage for saving a C++ binding. */
2143 #define cxx_saved_binding_make() \
2144 (ggc_alloc (sizeof (cxx_saved_binding)))
2146 struct cxx_saved_binding GTY(())
2148 /* Link that chains saved C++ bindings for a given name into a stack. */
2149 cxx_saved_binding *previous;
2150 /* The name of the current binding. */
2151 tree identifier;
2152 /* The binding we're saving. */
2153 cxx_binding *binding;
2154 tree class_value;
2155 tree real_type_value;
2158 /* Subroutines for reverting temporarily to top-level for instantiation
2159 of templates and such. We actually need to clear out the class- and
2160 local-value slots of all identifiers, so that only the global values
2161 are at all visible. Simply setting current_binding_level to the global
2162 scope isn't enough, because more binding levels may be pushed. */
2163 struct saved_scope *scope_chain;
2165 static cxx_saved_binding *
2166 store_bindings (tree names, cxx_saved_binding *old_bindings)
2168 tree t;
2169 cxx_saved_binding *search_bindings = old_bindings;
2171 timevar_push (TV_NAME_LOOKUP);
2172 for (t = names; t; t = TREE_CHAIN (t))
2174 tree id;
2175 cxx_saved_binding *saved;
2176 cxx_saved_binding *t1;
2178 if (TREE_CODE (t) == TREE_LIST)
2179 id = TREE_PURPOSE (t);
2180 else
2181 id = DECL_NAME (t);
2183 if (!id
2184 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2185 we have no IDENTIFIER_BINDING if we have left the class
2186 scope, but cached the class-level declarations. */
2187 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2188 continue;
2190 for (t1 = search_bindings; t1; t1 = t1->previous)
2191 if (t1->identifier == id)
2192 goto skip_it;
2194 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2195 saved = cxx_saved_binding_make ();
2196 saved->previous = old_bindings;
2197 saved->identifier = id;
2198 saved->binding = IDENTIFIER_BINDING (id);
2199 saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2200 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2201 IDENTIFIER_BINDING (id) = NULL;
2202 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2203 old_bindings = saved;
2204 skip_it:
2207 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2210 void
2211 maybe_push_to_top_level (int pseudo)
2213 struct saved_scope *s;
2214 struct cp_binding_level *b;
2215 cxx_saved_binding *old_bindings;
2216 int need_pop;
2218 timevar_push (TV_NAME_LOOKUP);
2219 s = ggc_alloc_cleared (sizeof (struct saved_scope));
2221 b = scope_chain ? current_binding_level : 0;
2223 /* If we're in the middle of some function, save our state. */
2224 if (cfun)
2226 need_pop = 1;
2227 push_function_context_to (NULL_TREE);
2229 else
2230 need_pop = 0;
2232 old_bindings = NULL;
2233 if (scope_chain && previous_class_type)
2234 old_bindings = store_bindings (previous_class_values, old_bindings);
2236 /* Have to include the global scope, because class-scope decls
2237 aren't listed anywhere useful. */
2238 for (; b; b = b->level_chain)
2240 tree t;
2242 /* Template IDs are inserted into the global level. If they were
2243 inserted into namespace level, finish_file wouldn't find them
2244 when doing pending instantiations. Therefore, don't stop at
2245 namespace level, but continue until :: . */
2246 if (global_scope_p (b) || (pseudo && b->template_parms_p))
2247 break;
2249 old_bindings = store_bindings (b->names, old_bindings);
2250 /* We also need to check class_shadowed to save class-level type
2251 bindings, since pushclass doesn't fill in b->names. */
2252 if (b->parm_flag == 2)
2253 old_bindings = store_bindings (b->class_shadowed, old_bindings);
2255 /* Unwind type-value slots back to top level. */
2256 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2257 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2259 s->prev = scope_chain;
2260 s->old_bindings = old_bindings;
2261 s->bindings = b;
2262 s->need_pop_function_context = need_pop;
2263 s->function_decl = current_function_decl;
2264 s->last_parms = last_function_parms;
2266 scope_chain = s;
2267 current_function_decl = NULL_TREE;
2268 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2269 current_lang_name = lang_name_cplusplus;
2270 current_namespace = global_namespace;
2271 timevar_pop (TV_NAME_LOOKUP);
2274 void
2275 push_to_top_level (void)
2277 maybe_push_to_top_level (0);
2280 void
2281 pop_from_top_level (void)
2283 struct saved_scope *s = scope_chain;
2284 cxx_saved_binding *saved;
2286 timevar_push (TV_NAME_LOOKUP);
2287 /* Clear out class-level bindings cache. */
2288 if (previous_class_type)
2289 invalidate_class_lookup_cache ();
2291 current_lang_base = 0;
2293 scope_chain = s->prev;
2294 for (saved = s->old_bindings; saved; saved = saved->previous)
2296 tree id = saved->identifier;
2298 IDENTIFIER_BINDING (id) = saved->binding;
2299 IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2300 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2303 /* If we were in the middle of compiling a function, restore our
2304 state. */
2305 if (s->need_pop_function_context)
2306 pop_function_context_from (NULL_TREE);
2307 current_function_decl = s->function_decl;
2308 last_function_parms = s->last_parms;
2309 timevar_pop (TV_NAME_LOOKUP);
2312 /* Push a definition of struct, union or enum tag named ID. into
2313 binding_level B. DECL is a TYPE_DECL for the type. We assume that
2314 the tag ID is not already defined. */
2316 static void
2317 set_identifier_type_value_with_scope (tree id,
2318 tree decl,
2319 struct cp_binding_level* b)
2321 tree type;
2323 if (!b->namespace_p)
2325 /* Shadow the marker, not the real thing, so that the marker
2326 gets restored later. */
2327 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2328 b->type_shadowed
2329 = tree_cons (id, old_type_value, b->type_shadowed);
2330 type = decl ? TREE_TYPE (decl) : NULL_TREE;
2332 else
2334 cxx_binding *binding =
2335 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2336 if (decl)
2338 if (BINDING_VALUE (binding))
2339 add_binding (binding, decl);
2340 else
2341 BINDING_VALUE (binding) = decl;
2343 else
2344 abort ();
2345 /* Store marker instead of real type. */
2346 type = global_type_node;
2348 SET_IDENTIFIER_TYPE_VALUE (id, type);
2351 /* As set_identifier_type_value_with_scope, but using current_binding_level. */
2353 void
2354 set_identifier_type_value (tree id, tree decl)
2356 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2359 /* Return the type associated with id. */
2361 tree
2362 identifier_type_value (tree id)
2364 timevar_push (TV_NAME_LOOKUP);
2365 /* There is no type with that name, anywhere. */
2366 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2367 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2368 /* This is not the type marker, but the real thing. */
2369 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2370 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2371 /* Have to search for it. It must be on the global level, now.
2372 Ask lookup_name not to return non-types. */
2373 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2374 if (id)
2375 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2376 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2379 /* Pop off extraneous binding levels left over due to syntax errors.
2381 We don't pop past namespaces, as they might be valid. */
2383 void
2384 pop_everything (void)
2386 if (ENABLE_SCOPE_CHECKING)
2387 verbatim ("XXX entering pop_everything ()\n");
2388 while (!toplevel_bindings_p ())
2390 if (current_binding_level->parm_flag == 2)
2391 pop_nested_class ();
2392 else
2393 poplevel (0, 0, 0);
2395 if (ENABLE_SCOPE_CHECKING)
2396 verbatim ("XXX leaving pop_everything ()\n");
2399 /* The type TYPE is being declared. If it is a class template, or a
2400 specialization of a class template, do any processing required and
2401 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
2402 being declared a friend. B is the binding level at which this TYPE
2403 should be bound.
2405 Returns the TYPE_DECL for TYPE, which may have been altered by this
2406 processing. */
2408 static tree
2409 maybe_process_template_type_declaration (tree type,
2410 int globalize,
2411 struct cp_binding_level* b)
2413 tree decl = TYPE_NAME (type);
2415 if (processing_template_parmlist)
2416 /* You can't declare a new template type in a template parameter
2417 list. But, you can declare a non-template type:
2419 template <class A*> struct S;
2421 is a forward-declaration of `A'. */
2423 else
2425 maybe_check_template_type (type);
2427 my_friendly_assert (IS_AGGR_TYPE (type)
2428 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2431 if (processing_template_decl)
2433 /* This may change after the call to
2434 push_template_decl_real, but we want the original value. */
2435 tree name = DECL_NAME (decl);
2437 decl = push_template_decl_real (decl, globalize);
2438 /* If the current binding level is the binding level for the
2439 template parameters (see the comment in
2440 begin_template_parm_list) and the enclosing level is a class
2441 scope, and we're not looking at a friend, push the
2442 declaration of the member class into the class scope. In the
2443 friend case, push_template_decl will already have put the
2444 friend into global scope, if appropriate. */
2445 if (TREE_CODE (type) != ENUMERAL_TYPE
2446 && !globalize && b->template_parms_p
2447 && b->level_chain->parm_flag == 2)
2449 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2450 /* Put this UDT in the table of UDTs for the class, since
2451 that won't happen below because B is not the class
2452 binding level, but is instead the pseudo-global level. */
2453 if (b->level_chain->type_decls == NULL)
2454 b->level_chain->type_decls =
2455 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2456 binding_table_insert (b->level_chain->type_decls, name, type);
2457 if (!COMPLETE_TYPE_P (current_class_type))
2459 maybe_add_class_template_decl_list (current_class_type,
2460 type, /*friend_p=*/0);
2461 CLASSTYPE_NESTED_UTDS (current_class_type) =
2462 b->level_chain->type_decls;
2468 return decl;
2471 /* In C++, you don't have to write `struct S' to refer to `S'; you
2472 can just use `S'. We accomplish this by creating a TYPE_DECL as
2473 if the user had written `typedef struct S S'. Create and return
2474 the TYPE_DECL for TYPE. */
2476 tree
2477 create_implicit_typedef (tree name, tree type)
2479 tree decl;
2481 decl = build_decl (TYPE_DECL, name, type);
2482 DECL_ARTIFICIAL (decl) = 1;
2483 /* There are other implicit type declarations, like the one *within*
2484 a class that allows you to write `S::S'. We must distinguish
2485 amongst these. */
2486 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2487 TYPE_NAME (type) = decl;
2489 return decl;
2492 /* Remember a local name for name-mangling purposes. */
2494 static void
2495 push_local_name (tree decl)
2497 size_t i, nelts;
2498 tree t, name;
2500 timevar_push (TV_NAME_LOOKUP);
2501 if (!local_names)
2502 VARRAY_TREE_INIT (local_names, 8, "local_names");
2504 name = DECL_NAME (decl);
2506 nelts = VARRAY_ACTIVE_SIZE (local_names);
2507 for (i = 0; i < nelts; i++)
2509 t = VARRAY_TREE (local_names, i);
2510 if (DECL_NAME (t) == name)
2512 if (!DECL_LANG_SPECIFIC (decl))
2513 retrofit_lang_decl (decl);
2514 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2515 if (DECL_LANG_SPECIFIC (t))
2516 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2517 else
2518 DECL_DISCRIMINATOR (decl) = 1;
2520 VARRAY_TREE (local_names, i) = decl;
2521 timevar_pop (TV_NAME_LOOKUP);
2522 return;
2526 VARRAY_PUSH_TREE (local_names, decl);
2527 timevar_pop (TV_NAME_LOOKUP);
2530 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2531 Normally put it into the inner-most non-tag-transparent scope,
2532 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2533 The latter is needed for implicit declarations. */
2535 void
2536 pushtag (tree name, tree type, int globalize)
2538 register struct cp_binding_level *b;
2540 timevar_push (TV_NAME_LOOKUP);
2541 b = current_binding_level;
2542 while (b->tag_transparent
2543 || (b->parm_flag == 2
2544 && (globalize
2545 /* We may be defining a new type in the initializer
2546 of a static member variable. We allow this when
2547 not pedantic, and it is particularly useful for
2548 type punning via an anonymous union. */
2549 || COMPLETE_TYPE_P (b->this_entity))))
2550 b = b->level_chain;
2552 if (b->type_decls == NULL)
2553 b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2554 binding_table_insert (b->type_decls, name, type);
2556 if (name)
2558 /* Do C++ gratuitous typedefing. */
2559 if (IDENTIFIER_TYPE_VALUE (name) != type)
2561 register tree d = NULL_TREE;
2562 int in_class = 0;
2563 tree context = TYPE_CONTEXT (type);
2565 if (! context)
2567 tree cs = current_scope ();
2569 if (! globalize)
2570 context = cs;
2571 else if (cs != NULL_TREE && TYPE_P (cs))
2572 /* When declaring a friend class of a local class, we want
2573 to inject the newly named class into the scope
2574 containing the local class, not the namespace scope. */
2575 context = decl_function_context (get_type_decl (cs));
2577 if (!context)
2578 context = current_namespace;
2580 if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2581 || b->parm_flag == 2)
2582 in_class = 1;
2584 if (current_lang_name == lang_name_java)
2585 TYPE_FOR_JAVA (type) = 1;
2587 d = create_implicit_typedef (name, type);
2588 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2589 if (! in_class)
2590 set_identifier_type_value_with_scope (name, d, b);
2592 d = maybe_process_template_type_declaration (type,
2593 globalize, b);
2595 if (b->parm_flag == 2)
2597 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2598 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2599 class. But if it's a member template class, we
2600 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2601 is done later. */
2602 finish_member_declaration (d);
2603 else
2604 pushdecl_class_level (d);
2606 else
2607 d = pushdecl_with_scope (d, b);
2609 /* FIXME what if it gets a name from typedef? */
2610 if (ANON_AGGRNAME_P (name))
2611 DECL_IGNORED_P (d) = 1;
2613 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2615 /* If this is a local class, keep track of it. We need this
2616 information for name-mangling, and so that it is possible to find
2617 all function definitions in a translation unit in a convenient
2618 way. (It's otherwise tricky to find a member function definition
2619 it's only pointed to from within a local class.) */
2620 if (TYPE_CONTEXT (type)
2621 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2622 && !processing_template_decl)
2623 VARRAY_PUSH_TREE (local_classes, type);
2625 if (b->parm_flag == 2)
2627 if (!COMPLETE_TYPE_P (current_class_type))
2629 maybe_add_class_template_decl_list (current_class_type,
2630 type, /*friend_p=*/0);
2631 CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
2636 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2637 /* Use the canonical TYPE_DECL for this node. */
2638 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2639 else
2641 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2642 will be the tagged type we just added to the current
2643 binding level. This fake NULL-named TYPE_DECL node helps
2644 dwarfout.c to know when it needs to output a
2645 representation of a tagged type, and it also gives us a
2646 convenient place to record the "scope start" address for
2647 the tagged type. */
2649 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2650 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2652 timevar_pop (TV_NAME_LOOKUP);
2655 /* Counter used to create anonymous type names. */
2657 static GTY(()) int anon_cnt;
2659 /* Return an IDENTIFIER which can be used as a name for
2660 anonymous structs and unions. */
2662 tree
2663 make_anon_name (void)
2665 char buf[32];
2667 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2668 return get_identifier (buf);
2671 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
2672 This keeps dbxout from getting confused. */
2674 void
2675 clear_anon_tags (void)
2677 register struct cp_binding_level *b;
2678 static int last_cnt = 0;
2680 /* Fast out if no new anon names were declared. */
2681 if (last_cnt == anon_cnt)
2682 return;
2684 b = current_binding_level;
2685 while (b->tag_transparent)
2686 b = b->level_chain;
2687 if (b->type_decls != NULL)
2688 binding_table_remove_anonymous_types (b->type_decls);
2689 last_cnt = anon_cnt;
2692 /* Subroutine of duplicate_decls: return truthvalue of whether
2693 or not types of these decls match.
2695 For C++, we must compare the parameter list so that `int' can match
2696 `int&' in a parameter position, but `int&' is not confused with
2697 `const int&'. */
2700 decls_match (tree newdecl, tree olddecl)
2702 int types_match;
2704 if (newdecl == olddecl)
2705 return 1;
2707 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2708 /* If the two DECLs are not even the same kind of thing, we're not
2709 interested in their types. */
2710 return 0;
2712 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2714 tree f1 = TREE_TYPE (newdecl);
2715 tree f2 = TREE_TYPE (olddecl);
2716 tree p1 = TYPE_ARG_TYPES (f1);
2717 tree p2 = TYPE_ARG_TYPES (f2);
2719 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2720 && ! (DECL_EXTERN_C_P (newdecl)
2721 && DECL_EXTERN_C_P (olddecl)))
2722 return 0;
2724 if (TREE_CODE (f1) != TREE_CODE (f2))
2725 return 0;
2727 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2729 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2730 && (DECL_BUILT_IN (olddecl)
2731 #ifndef NO_IMPLICIT_EXTERN_C
2732 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2733 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2734 #endif
2737 types_match = self_promoting_args_p (p1);
2738 if (p1 == void_list_node)
2739 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2741 #ifndef NO_IMPLICIT_EXTERN_C
2742 else if (p1 == NULL_TREE
2743 && (DECL_EXTERN_C_P (olddecl)
2744 && DECL_IN_SYSTEM_HEADER (olddecl)
2745 && !DECL_CLASS_SCOPE_P (olddecl))
2746 && (DECL_EXTERN_C_P (newdecl)
2747 && DECL_IN_SYSTEM_HEADER (newdecl)
2748 && !DECL_CLASS_SCOPE_P (newdecl)))
2750 types_match = self_promoting_args_p (p2);
2751 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2753 #endif
2754 else
2755 types_match = compparms (p1, p2);
2757 else
2758 types_match = 0;
2760 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2762 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2763 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2764 return 0;
2766 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2767 DECL_TEMPLATE_PARMS (olddecl)))
2768 return 0;
2770 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2771 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
2772 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
2773 else
2774 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2775 DECL_TEMPLATE_RESULT (newdecl));
2777 else
2779 if (TREE_TYPE (newdecl) == error_mark_node)
2780 types_match = TREE_TYPE (olddecl) == error_mark_node;
2781 else if (TREE_TYPE (olddecl) == NULL_TREE)
2782 types_match = TREE_TYPE (newdecl) == NULL_TREE;
2783 else if (TREE_TYPE (newdecl) == NULL_TREE)
2784 types_match = 0;
2785 else
2786 types_match = comptypes (TREE_TYPE (newdecl),
2787 TREE_TYPE (olddecl),
2788 COMPARE_REDECLARATION);
2791 return types_match;
2794 /* If NEWDECL is `static' and an `extern' was seen previously,
2795 warn about it. OLDDECL is the previous declaration.
2797 Note that this does not apply to the C++ case of declaring
2798 a variable `extern const' and then later `const'.
2800 Don't complain about built-in functions, since they are beyond
2801 the user's control. */
2803 static void
2804 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2806 static const char *const explicit_extern_static_warning
2807 = "`%D' was declared `extern' and later `static'";
2808 static const char *const implicit_extern_static_warning
2809 = "`%D' was declared implicitly `extern' and later `static'";
2811 tree name;
2813 if (TREE_CODE (newdecl) == TYPE_DECL
2814 || TREE_CODE (newdecl) == TEMPLATE_DECL
2815 || TREE_CODE (newdecl) == CONST_DECL)
2816 return;
2818 /* Don't get confused by static member functions; that's a different
2819 use of `static'. */
2820 if (TREE_CODE (newdecl) == FUNCTION_DECL
2821 && DECL_STATIC_FUNCTION_P (newdecl))
2822 return;
2824 /* If the old declaration was `static', or the new one isn't, then
2825 then everything is OK. */
2826 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2827 return;
2829 /* It's OK to declare a builtin function as `static'. */
2830 if (TREE_CODE (olddecl) == FUNCTION_DECL
2831 && DECL_ARTIFICIAL (olddecl))
2832 return;
2834 name = DECL_ASSEMBLER_NAME (newdecl);
2835 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2836 ? implicit_extern_static_warning
2837 : explicit_extern_static_warning, newdecl);
2838 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2841 /* Handle when a new declaration NEWDECL has the same name as an old
2842 one OLDDECL in the same binding contour. Prints an error message
2843 if appropriate.
2845 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2846 Otherwise, return 0. */
2849 duplicate_decls (tree newdecl, tree olddecl)
2851 unsigned olddecl_uid = DECL_UID (olddecl);
2852 int olddecl_friend = 0, types_match = 0;
2853 int new_defines_function = 0;
2855 if (newdecl == olddecl)
2856 return 1;
2858 types_match = decls_match (newdecl, olddecl);
2860 /* If either the type of the new decl or the type of the old decl is an
2861 error_mark_node, then that implies that we have already issued an
2862 error (earlier) for some bogus type specification, and in that case,
2863 it is rather pointless to harass the user with yet more error message
2864 about the same declaration, so just pretend the types match here. */
2865 if (TREE_TYPE (newdecl) == error_mark_node
2866 || TREE_TYPE (olddecl) == error_mark_node)
2867 types_match = 1;
2869 if (DECL_P (olddecl)
2870 && TREE_CODE (newdecl) == FUNCTION_DECL
2871 && TREE_CODE (olddecl) == FUNCTION_DECL
2872 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2874 if (DECL_DECLARED_INLINE_P (newdecl)
2875 && DECL_UNINLINABLE (newdecl)
2876 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2877 /* Already warned elsewhere. */;
2878 else if (DECL_DECLARED_INLINE_P (olddecl)
2879 && DECL_UNINLINABLE (olddecl)
2880 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2881 /* Already warned. */;
2882 else if (DECL_DECLARED_INLINE_P (newdecl)
2883 && DECL_UNINLINABLE (olddecl)
2884 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2886 warning ("%Hfunction '%D' redeclared as inline",
2887 &DECL_SOURCE_LOCATION (newdecl), newdecl);
2888 warning ("%Hprevious declaration of '%D' with attribute noinline",
2889 &DECL_SOURCE_LOCATION (olddecl), olddecl);
2891 else if (DECL_DECLARED_INLINE_P (olddecl)
2892 && DECL_UNINLINABLE (newdecl)
2893 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2895 warning ("%Hfunction '%D' redeclared with attribute noinline",
2896 &DECL_SOURCE_LOCATION (newdecl), newdecl);
2897 warning ("%Hprevious declaration of '%D' was inline",
2898 &DECL_SOURCE_LOCATION (olddecl), olddecl);
2902 /* Check for redeclaration and other discrepancies. */
2903 if (TREE_CODE (olddecl) == FUNCTION_DECL
2904 && DECL_ARTIFICIAL (olddecl))
2906 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2908 /* Avoid warnings redeclaring anticipated built-ins. */
2909 if (DECL_ANTICIPATED (olddecl))
2910 return 0;
2912 /* If you declare a built-in or predefined function name as static,
2913 the old definition is overridden, but optionally warn this was a
2914 bad choice of name. */
2915 if (! TREE_PUBLIC (newdecl))
2917 if (warn_shadow)
2918 warning ("shadowing %s function `%#D'",
2919 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2920 olddecl);
2921 /* Discard the old built-in function. */
2922 return 0;
2924 /* If the built-in is not ansi, then programs can override
2925 it even globally without an error. */
2926 else if (! DECL_BUILT_IN (olddecl))
2927 warning ("library function `%#D' redeclared as non-function `%#D'",
2928 olddecl, newdecl);
2929 else
2931 error ("declaration of `%#D'", newdecl);
2932 error ("conflicts with built-in declaration `%#D'",
2933 olddecl);
2935 return 0;
2937 else if (!types_match)
2939 /* Avoid warnings redeclaring anticipated built-ins. */
2940 if (DECL_ANTICIPATED (olddecl))
2941 ; /* Do nothing yet. */
2942 else if ((DECL_EXTERN_C_P (newdecl)
2943 && DECL_EXTERN_C_P (olddecl))
2944 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2945 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2947 /* A near match; override the builtin. */
2949 if (TREE_PUBLIC (newdecl))
2951 warning ("new declaration `%#D'", newdecl);
2952 warning ("ambiguates built-in declaration `%#D'",
2953 olddecl);
2955 else if (warn_shadow)
2956 warning ("shadowing %s function `%#D'",
2957 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2958 olddecl);
2960 else
2961 /* Discard the old built-in function. */
2962 return 0;
2964 /* Replace the old RTL to avoid problems with inlining. */
2965 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2967 /* Even if the types match, prefer the new declarations type
2968 for anticipated built-ins, for exception lists, etc... */
2969 else if (DECL_ANTICIPATED (olddecl))
2970 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2972 /* Whether or not the builtin can throw exceptions has no
2973 bearing on this declarator. */
2974 TREE_NOTHROW (olddecl) = 0;
2976 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2978 /* If a builtin function is redeclared as `static', merge
2979 the declarations, but make the original one static. */
2980 DECL_THIS_STATIC (olddecl) = 1;
2981 TREE_PUBLIC (olddecl) = 0;
2983 /* Make the old declaration consistent with the new one so
2984 that all remnants of the builtin-ness of this function
2985 will be banished. */
2986 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2987 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2990 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2992 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
2993 && TREE_CODE (newdecl) != TYPE_DECL
2994 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
2995 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
2996 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
2997 && TREE_CODE (olddecl) != TYPE_DECL
2998 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
2999 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3000 == TYPE_DECL))))
3002 /* We do nothing special here, because C++ does such nasty
3003 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3004 get shadowed, and know that if we need to find a TYPE_DECL
3005 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3006 slot of the identifier. */
3007 return 0;
3010 if ((TREE_CODE (newdecl) == FUNCTION_DECL
3011 && DECL_FUNCTION_TEMPLATE_P (olddecl))
3012 || (TREE_CODE (olddecl) == FUNCTION_DECL
3013 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3014 return 0;
3016 error ("`%#D' redeclared as different kind of symbol", newdecl);
3017 if (TREE_CODE (olddecl) == TREE_LIST)
3018 olddecl = TREE_VALUE (olddecl);
3019 cp_error_at ("previous declaration of `%#D'", olddecl);
3021 /* New decl is completely inconsistent with the old one =>
3022 tell caller to replace the old one. */
3024 return 0;
3026 else if (!types_match)
3028 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3029 /* These are certainly not duplicate declarations; they're
3030 from different scopes. */
3031 return 0;
3033 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3035 /* The name of a class template may not be declared to refer to
3036 any other template, class, function, object, namespace, value,
3037 or type in the same scope. */
3038 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3039 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3041 error ("declaration of template `%#D'", newdecl);
3042 cp_error_at ("conflicts with previous declaration `%#D'",
3043 olddecl);
3045 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3046 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3047 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3048 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3049 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3050 DECL_TEMPLATE_PARMS (olddecl))
3051 /* Template functions can be disambiguated by
3052 return type. */
3053 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3054 TREE_TYPE (TREE_TYPE (olddecl))))
3056 error ("new declaration `%#D'", newdecl);
3057 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3059 return 0;
3061 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3063 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3065 error ("declaration of C function `%#D' conflicts with",
3066 newdecl);
3067 cp_error_at ("previous declaration `%#D' here", olddecl);
3069 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3070 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3072 error ("new declaration `%#D'", newdecl);
3073 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3075 else
3076 return 0;
3079 /* Already complained about this, so don't do so again. */
3080 else if (current_class_type == NULL_TREE
3081 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3083 error ("conflicting types for `%#D'", newdecl);
3084 cp_error_at ("previous declaration as `%#D'", olddecl);
3087 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3088 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3089 && (!DECL_TEMPLATE_INFO (newdecl)
3090 || (DECL_TI_TEMPLATE (newdecl)
3091 != DECL_TI_TEMPLATE (olddecl))))
3092 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3093 && (!DECL_TEMPLATE_INFO (olddecl)
3094 || (DECL_TI_TEMPLATE (olddecl)
3095 != DECL_TI_TEMPLATE (newdecl))))))
3096 /* It's OK to have a template specialization and a non-template
3097 with the same type, or to have specializations of two
3098 different templates with the same type. Note that if one is a
3099 specialization, and the other is an instantiation of the same
3100 template, that we do not exit at this point. That situation
3101 can occur if we instantiate a template class, and then
3102 specialize one of its methods. This situation is valid, but
3103 the declarations must be merged in the usual way. */
3104 return 0;
3105 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3106 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3107 && !DECL_USE_TEMPLATE (newdecl))
3108 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3109 && !DECL_USE_TEMPLATE (olddecl))))
3110 /* One of the declarations is a template instantiation, and the
3111 other is not a template at all. That's OK. */
3112 return 0;
3113 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3114 && DECL_NAMESPACE_ALIAS (newdecl)
3115 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3116 /* Redeclaration of namespace alias, ignore it. */
3117 return 1;
3118 else
3120 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3121 if (errmsg)
3123 error (errmsg, newdecl);
3124 if (DECL_NAME (olddecl) != NULL_TREE)
3125 cp_error_at ((DECL_INITIAL (olddecl)
3126 && namespace_bindings_p ())
3127 ? "`%#D' previously defined here"
3128 : "`%#D' previously declared here", olddecl);
3129 return 0;
3131 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3132 && DECL_INITIAL (olddecl) != NULL_TREE
3133 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3134 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3136 /* Prototype decl follows defn w/o prototype. */
3137 cp_warning_at ("prototype for `%#D'", newdecl);
3138 warning ("%Hfollows non-prototype definition here",
3139 &DECL_SOURCE_LOCATION (olddecl));
3141 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3142 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3144 /* extern "C" int foo ();
3145 int foo () { bar (); }
3146 is OK. */
3147 if (current_lang_depth () == 0)
3148 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3149 else
3151 cp_error_at ("previous declaration of `%#D' with %L linkage",
3152 olddecl, DECL_LANGUAGE (olddecl));
3153 error ("conflicts with new declaration with %L linkage",
3154 DECL_LANGUAGE (newdecl));
3158 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3160 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3162 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3163 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3164 int i = 1;
3166 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3167 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3169 for (; t1 && t1 != void_list_node;
3170 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3171 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3173 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3174 TREE_PURPOSE (t2)))
3176 pedwarn ("default argument given for parameter %d of `%#D'",
3177 i, newdecl);
3178 cp_pedwarn_at ("after previous specification in `%#D'",
3179 olddecl);
3181 else
3183 error ("default argument given for parameter %d of `%#D'",
3184 i, newdecl);
3185 cp_error_at ("after previous specification in `%#D'",
3186 olddecl);
3190 if (DECL_DECLARED_INLINE_P (newdecl)
3191 && ! DECL_DECLARED_INLINE_P (olddecl)
3192 && TREE_ADDRESSABLE (olddecl) && warn_inline)
3194 warning ("`%#D' was used before it was declared inline",
3195 newdecl);
3196 warning ("%Hprevious non-inline declaration here",
3197 &DECL_SOURCE_LOCATION (olddecl));
3202 /* Do not merge an implicit typedef with an explicit one. In:
3204 class A;
3206 typedef class A A __attribute__ ((foo));
3208 the attribute should apply only to the typedef. */
3209 if (TREE_CODE (olddecl) == TYPE_DECL
3210 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3211 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3212 return 0;
3214 /* If new decl is `static' and an `extern' was seen previously,
3215 warn about it. */
3216 warn_extern_redeclared_static (newdecl, olddecl);
3218 /* We have committed to returning 1 at this point. */
3219 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3221 /* Now that functions must hold information normally held
3222 by field decls, there is extra work to do so that
3223 declaration information does not get destroyed during
3224 definition. */
3225 if (DECL_VINDEX (olddecl))
3226 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3227 if (DECL_CONTEXT (olddecl))
3228 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3229 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3230 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3231 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3232 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3233 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3234 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3235 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3236 SET_OVERLOADED_OPERATOR_CODE
3237 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3238 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3240 /* Optionally warn about more than one declaration for the same
3241 name, but don't warn about a function declaration followed by a
3242 definition. */
3243 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3244 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3245 /* Don't warn about extern decl followed by definition. */
3246 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3247 /* Don't warn about friends, let add_friend take care of it. */
3248 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3250 warning ("redundant redeclaration of `%D' in same scope", newdecl);
3251 cp_warning_at ("previous declaration of `%D'", olddecl);
3255 /* Deal with C++: must preserve virtual function table size. */
3256 if (TREE_CODE (olddecl) == TYPE_DECL)
3258 register tree newtype = TREE_TYPE (newdecl);
3259 register tree oldtype = TREE_TYPE (olddecl);
3261 if (newtype != error_mark_node && oldtype != error_mark_node
3262 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3263 CLASSTYPE_FRIEND_CLASSES (newtype)
3264 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3266 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3269 /* Copy all the DECL_... slots specified in the new decl
3270 except for any that we copy here from the old type. */
3271 DECL_ATTRIBUTES (newdecl)
3272 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3274 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3276 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3277 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3278 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3279 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3281 /* If the new declaration is a definition, update the file and
3282 line information on the declaration. */
3283 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3284 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3286 DECL_SOURCE_LOCATION (olddecl)
3287 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3288 = DECL_SOURCE_LOCATION (newdecl);
3291 return 1;
3294 if (types_match)
3296 /* Automatically handles default parameters. */
3297 tree oldtype = TREE_TYPE (olddecl);
3298 tree newtype;
3300 /* Merge the data types specified in the two decls. */
3301 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3303 /* If merge_types produces a non-typedef type, just use the old type. */
3304 if (TREE_CODE (newdecl) == TYPE_DECL
3305 && newtype == DECL_ORIGINAL_TYPE (newdecl))
3306 newtype = oldtype;
3308 if (TREE_CODE (newdecl) == VAR_DECL)
3310 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3311 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3312 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
3313 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
3316 /* Do this after calling `merge_types' so that default
3317 parameters don't confuse us. */
3318 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3319 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3320 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3322 TREE_TYPE (newdecl) = build_exception_variant (newtype,
3323 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3324 TREE_TYPE (olddecl) = build_exception_variant (newtype,
3325 TYPE_RAISES_EXCEPTIONS (oldtype));
3327 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3328 && DECL_SOURCE_LINE (olddecl) != 0
3329 && flag_exceptions
3330 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3331 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3333 error ("declaration of `%F' throws different exceptions",
3334 newdecl);
3335 cp_error_at ("than previous declaration `%F'", olddecl);
3338 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3340 /* Lay the type out, unless already done. */
3341 if (! same_type_p (newtype, oldtype)
3342 && TREE_TYPE (newdecl) != error_mark_node
3343 && !(processing_template_decl && uses_template_parms (newdecl)))
3344 layout_type (TREE_TYPE (newdecl));
3346 if ((TREE_CODE (newdecl) == VAR_DECL
3347 || TREE_CODE (newdecl) == PARM_DECL
3348 || TREE_CODE (newdecl) == RESULT_DECL
3349 || TREE_CODE (newdecl) == FIELD_DECL
3350 || TREE_CODE (newdecl) == TYPE_DECL)
3351 && !(processing_template_decl && uses_template_parms (newdecl)))
3352 layout_decl (newdecl, 0);
3354 /* Merge the type qualifiers. */
3355 if (TREE_READONLY (newdecl))
3356 TREE_READONLY (olddecl) = 1;
3357 if (TREE_THIS_VOLATILE (newdecl))
3358 TREE_THIS_VOLATILE (olddecl) = 1;
3360 /* Merge the initialization information. */
3361 if (DECL_INITIAL (newdecl) == NULL_TREE
3362 && DECL_INITIAL (olddecl) != NULL_TREE)
3364 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3365 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3366 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3367 && DECL_LANG_SPECIFIC (newdecl)
3368 && DECL_LANG_SPECIFIC (olddecl))
3369 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3372 /* Merge the section attribute.
3373 We want to issue an error if the sections conflict but that must be
3374 done later in decl_attributes since we are called before attributes
3375 are assigned. */
3376 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3377 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3379 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3381 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3382 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3383 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
3384 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
3385 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
3386 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
3387 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
3388 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
3389 /* Keep the old RTL. */
3390 COPY_DECL_RTL (olddecl, newdecl);
3392 else if (TREE_CODE (newdecl) == VAR_DECL
3393 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3395 /* Keep the old RTL. We cannot keep the old RTL if the old
3396 declaration was for an incomplete object and the new
3397 declaration is not since many attributes of the RTL will
3398 change. */
3399 COPY_DECL_RTL (olddecl, newdecl);
3402 /* If cannot merge, then use the new type and qualifiers,
3403 and don't preserve the old rtl. */
3404 else
3406 /* Clean out any memory we had of the old declaration. */
3407 tree oldstatic = value_member (olddecl, static_aggregates);
3408 if (oldstatic)
3409 TREE_VALUE (oldstatic) = error_mark_node;
3411 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3412 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3413 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3414 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3417 /* Merge the storage class information. */
3418 merge_weak (newdecl, olddecl);
3420 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3421 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3422 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3423 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3424 if (! DECL_EXTERNAL (olddecl))
3425 DECL_EXTERNAL (newdecl) = 0;
3427 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3429 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3430 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3431 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3432 DECL_TEMPLATE_INSTANTIATED (newdecl)
3433 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3434 /* Don't really know how much of the language-specific
3435 values we should copy from old to new. */
3436 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3437 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
3438 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3439 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3440 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3441 DECL_INITIALIZED_IN_CLASS_P (newdecl)
3442 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3443 olddecl_friend = DECL_FRIEND_P (olddecl);
3445 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3446 if (TREE_CODE (newdecl) == FUNCTION_DECL
3447 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3449 DECL_BEFRIENDING_CLASSES (newdecl)
3450 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3451 DECL_BEFRIENDING_CLASSES (olddecl));
3452 /* DECL_THUNKS is only valid for virtual functions,
3453 otherwise it is a DECL_FRIEND_CONTEXT. */
3454 if (DECL_VIRTUAL_P (newdecl))
3455 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3459 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3461 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3462 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3464 /* If newdecl is not a specialization, then it is not a
3465 template-related function at all. And that means that we
3466 should have exited above, returning 0. */
3467 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3470 if (TREE_USED (olddecl))
3471 /* From [temp.expl.spec]:
3473 If a template, a member template or the member of a class
3474 template is explicitly specialized then that
3475 specialization shall be declared before the first use of
3476 that specialization that would cause an implicit
3477 instantiation to take place, in every translation unit in
3478 which such a use occurs. */
3479 error ("explicit specialization of %D after first use",
3480 olddecl);
3482 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3484 /* [temp.expl.spec/14] We don't inline explicit specialization
3485 just because the primary template says so. */
3487 else
3489 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3490 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3492 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3494 /* If either decl says `inline', this fn is inline, unless
3495 its definition was passed already. */
3496 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3497 DECL_INLINE (olddecl) = 1;
3498 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3500 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3501 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3504 /* Preserve abstractness on cloned [cd]tors. */
3505 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3507 if (! types_match)
3509 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3510 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3511 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3513 if (! types_match || new_defines_function)
3515 /* These need to be copied so that the names are available.
3516 Note that if the types do match, we'll preserve inline
3517 info and other bits, but if not, we won't. */
3518 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3519 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3521 if (new_defines_function)
3522 /* If defining a function declared with other language
3523 linkage, use the previously declared language linkage. */
3524 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3525 else if (types_match)
3527 /* If redeclaring a builtin function, and not a definition,
3528 it stays built in. */
3529 if (DECL_BUILT_IN (olddecl))
3531 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3532 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3533 /* If we're keeping the built-in definition, keep the rtl,
3534 regardless of declaration matches. */
3535 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3537 else
3538 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
3540 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3541 /* Don't clear out the arguments if we're redefining a function. */
3542 if (DECL_ARGUMENTS (olddecl))
3543 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3546 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3547 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3549 /* Now preserve various other info from the definition. */
3550 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3551 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3552 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3553 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3555 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3557 int function_size;
3559 function_size = sizeof (struct tree_decl);
3561 memcpy ((char *) olddecl + sizeof (struct tree_common),
3562 (char *) newdecl + sizeof (struct tree_common),
3563 function_size - sizeof (struct tree_common));
3565 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3566 /* If newdecl is a template instantiation, it is possible that
3567 the following sequence of events has occurred:
3569 o A friend function was declared in a class template. The
3570 class template was instantiated.
3572 o The instantiation of the friend declaration was
3573 recorded on the instantiation list, and is newdecl.
3575 o Later, however, instantiate_class_template called pushdecl
3576 on the newdecl to perform name injection. But, pushdecl in
3577 turn called duplicate_decls when it discovered that another
3578 declaration of a global function with the same name already
3579 existed.
3581 o Here, in duplicate_decls, we decided to clobber newdecl.
3583 If we're going to do that, we'd better make sure that
3584 olddecl, and not newdecl, is on the list of
3585 instantiations so that if we try to do the instantiation
3586 again we won't get the clobbered declaration. */
3587 reregister_specialization (newdecl,
3588 DECL_TI_TEMPLATE (newdecl),
3589 olddecl);
3591 else
3593 memcpy ((char *) olddecl + sizeof (struct tree_common),
3594 (char *) newdecl + sizeof (struct tree_common),
3595 sizeof (struct tree_decl) - sizeof (struct tree_common)
3596 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3599 DECL_UID (olddecl) = olddecl_uid;
3600 if (olddecl_friend)
3601 DECL_FRIEND_P (olddecl) = 1;
3603 /* NEWDECL contains the merged attribute lists.
3604 Update OLDDECL to be the same. */
3605 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3607 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3608 so that encode_section_info has a chance to look at the new decl
3609 flags and attributes. */
3610 if (DECL_RTL_SET_P (olddecl)
3611 && (TREE_CODE (olddecl) == FUNCTION_DECL
3612 || (TREE_CODE (olddecl) == VAR_DECL
3613 && TREE_STATIC (olddecl))))
3614 make_decl_rtl (olddecl, NULL);
3616 return 1;
3619 /* Record a decl-node X as belonging to the current lexical scope.
3620 Check for errors (such as an incompatible declaration for the same
3621 name already seen in the same scope).
3623 Returns either X or an old decl for the same name.
3624 If an old decl is returned, it may have been smashed
3625 to agree with what X says. */
3627 tree
3628 pushdecl (tree x)
3630 register tree t;
3631 register tree name;
3632 int need_new_binding;
3634 timevar_push (TV_NAME_LOOKUP);
3635 /* We shouldn't be calling pushdecl when we're generating RTL for a
3636 function that we already did semantic analysis on previously. */
3637 my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3638 19990913);
3640 need_new_binding = 1;
3642 if (DECL_TEMPLATE_PARM_P (x))
3643 /* Template parameters have no context; they are not X::T even
3644 when declared within a class or namespace. */
3646 else
3648 if (current_function_decl && x != current_function_decl
3649 /* A local declaration for a function doesn't constitute
3650 nesting. */
3651 && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3652 /* A local declaration for an `extern' variable is in the
3653 scope of the current namespace, not the current
3654 function. */
3655 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3656 && !DECL_CONTEXT (x))
3657 DECL_CONTEXT (x) = current_function_decl;
3659 /* If this is the declaration for a namespace-scope function,
3660 but the declaration itself is in a local scope, mark the
3661 declaration. */
3662 if (TREE_CODE (x) == FUNCTION_DECL
3663 && DECL_NAMESPACE_SCOPE_P (x)
3664 && current_function_decl
3665 && x != current_function_decl)
3666 DECL_LOCAL_FUNCTION_P (x) = 1;
3669 name = DECL_NAME (x);
3670 if (name)
3672 int different_binding_level = 0;
3674 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3675 name = TREE_OPERAND (name, 0);
3677 /* In case this decl was explicitly namespace-qualified, look it
3678 up in its namespace context. */
3679 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3680 && namespace_bindings_p ())
3681 t = namespace_binding (name, DECL_CONTEXT (x));
3682 else
3683 t = lookup_name_current_level (name);
3685 /* [basic.link] If there is a visible declaration of an entity
3686 with linkage having the same name and type, ignoring entities
3687 declared outside the innermost enclosing namespace scope, the
3688 block scope declaration declares that same entity and
3689 receives the linkage of the previous declaration. */
3690 if (! t && current_function_decl && x != current_function_decl
3691 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3692 && DECL_EXTERNAL (x))
3694 /* Look in block scope. */
3695 t = IDENTIFIER_VALUE (name);
3696 /* Or in the innermost namespace. */
3697 if (! t)
3698 t = namespace_binding (name, DECL_CONTEXT (x));
3699 /* Does it have linkage? Note that if this isn't a DECL, it's an
3700 OVERLOAD, which is OK. */
3701 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3702 t = NULL_TREE;
3703 if (t)
3704 different_binding_level = 1;
3707 /* If we are declaring a function, and the result of name-lookup
3708 was an OVERLOAD, look for an overloaded instance that is
3709 actually the same as the function we are declaring. (If
3710 there is one, we have to merge our declaration with the
3711 previous declaration.) */
3712 if (t && TREE_CODE (t) == OVERLOAD)
3714 tree match;
3716 if (TREE_CODE (x) == FUNCTION_DECL)
3717 for (match = t; match; match = OVL_NEXT (match))
3719 if (decls_match (OVL_CURRENT (match), x))
3720 break;
3722 else
3723 /* Just choose one. */
3724 match = t;
3726 if (match)
3727 t = OVL_CURRENT (match);
3728 else
3729 t = NULL_TREE;
3732 if (t == error_mark_node)
3734 /* error_mark_node is 0 for a while during initialization! */
3735 t = NULL_TREE;
3736 cp_error_at ("`%#D' used prior to declaration", x);
3738 else if (t != NULL_TREE)
3740 if (different_binding_level)
3742 if (decls_match (x, t))
3743 /* The standard only says that the local extern
3744 inherits linkage from the previous decl; in
3745 particular, default args are not shared. It would
3746 be nice to propagate inlining info, though. FIXME. */
3747 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3749 else if (TREE_CODE (t) == PARM_DECL)
3751 if (DECL_CONTEXT (t) == NULL_TREE)
3752 /* This is probably caused by too many errors, but calling
3753 abort will say that if errors have occurred. */
3754 abort ();
3756 /* Check for duplicate params. */
3757 if (duplicate_decls (x, t))
3758 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3760 else if ((DECL_EXTERN_C_FUNCTION_P (x)
3761 || DECL_FUNCTION_TEMPLATE_P (x))
3762 && is_overloaded_fn (t))
3763 /* Don't do anything just yet. */;
3764 else if (t == wchar_decl_node)
3766 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3767 pedwarn ("redeclaration of `wchar_t' as `%T'",
3768 TREE_TYPE (x));
3770 /* Throw away the redeclaration. */
3771 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3773 else if (TREE_CODE (t) != TREE_CODE (x))
3775 if (duplicate_decls (x, t))
3776 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3778 else if (duplicate_decls (x, t))
3780 if (TREE_CODE (t) == TYPE_DECL)
3781 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3782 else if (TREE_CODE (t) == FUNCTION_DECL)
3783 check_default_args (t);
3785 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3787 else if (DECL_MAIN_P (x))
3789 /* A redeclaration of main, but not a duplicate of the
3790 previous one.
3792 [basic.start.main]
3794 This function shall not be overloaded. */
3795 cp_error_at ("invalid redeclaration of `%D'", t);
3796 error ("as `%D'", x);
3797 /* We don't try to push this declaration since that
3798 causes a crash. */
3799 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3803 check_template_shadow (x);
3805 /* If this is a function conjured up by the backend, massage it
3806 so it looks friendly. */
3807 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3809 retrofit_lang_decl (x);
3810 SET_DECL_LANGUAGE (x, lang_c);
3813 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3815 t = push_overloaded_decl (x, PUSH_LOCAL);
3816 if (t != x)
3817 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3818 if (!namespace_bindings_p ())
3819 /* We do not need to create a binding for this name;
3820 push_overloaded_decl will have already done so if
3821 necessary. */
3822 need_new_binding = 0;
3824 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3826 t = push_overloaded_decl (x, PUSH_GLOBAL);
3827 if (t == x)
3828 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3829 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3832 /* If declaring a type as a typedef, copy the type (unless we're
3833 at line 0), and install this TYPE_DECL as the new type's typedef
3834 name. See the extensive comment in ../c-decl.c (pushdecl). */
3835 if (TREE_CODE (x) == TYPE_DECL)
3837 tree type = TREE_TYPE (x);
3838 if (DECL_SOURCE_LINE (x) == 0)
3840 if (TYPE_NAME (type) == 0)
3841 TYPE_NAME (type) = x;
3843 else if (type != error_mark_node && TYPE_NAME (type) != x
3844 /* We don't want to copy the type when all we're
3845 doing is making a TYPE_DECL for the purposes of
3846 inlining. */
3847 && (!TYPE_NAME (type)
3848 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3850 DECL_ORIGINAL_TYPE (x) = type;
3851 type = build_type_copy (type);
3852 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3853 TYPE_NAME (type) = x;
3854 TREE_TYPE (x) = type;
3857 if (type != error_mark_node
3858 && TYPE_NAME (type)
3859 && TYPE_IDENTIFIER (type))
3860 set_identifier_type_value (DECL_NAME (x), x);
3863 /* Multiple external decls of the same identifier ought to match.
3865 We get warnings about inline functions where they are defined.
3866 We get warnings about other functions from push_overloaded_decl.
3868 Avoid duplicate warnings where they are used. */
3869 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3871 tree decl;
3873 decl = IDENTIFIER_NAMESPACE_VALUE (name);
3874 if (decl && TREE_CODE (decl) == OVERLOAD)
3875 decl = OVL_FUNCTION (decl);
3877 if (decl && decl != error_mark_node
3878 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3879 /* If different sort of thing, we already gave an error. */
3880 && TREE_CODE (decl) == TREE_CODE (x)
3881 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3883 pedwarn ("type mismatch with previous external decl of `%#D'", x);
3884 cp_pedwarn_at ("previous external decl of `%#D'", decl);
3888 /* This name is new in its binding level.
3889 Install the new declaration and return it. */
3890 if (namespace_bindings_p ())
3892 /* Install a global value. */
3894 /* If the first global decl has external linkage,
3895 warn if we later see static one. */
3896 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3897 TREE_PUBLIC (name) = 1;
3899 /* Bind the name for the entity. */
3900 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3901 && t != NULL_TREE)
3902 && (TREE_CODE (x) == TYPE_DECL
3903 || TREE_CODE (x) == VAR_DECL
3904 || TREE_CODE (x) == ALIAS_DECL
3905 || TREE_CODE (x) == NAMESPACE_DECL
3906 || TREE_CODE (x) == CONST_DECL
3907 || TREE_CODE (x) == TEMPLATE_DECL))
3908 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3910 /* Don't forget if the function was used via an implicit decl. */
3911 if (IDENTIFIER_IMPLICIT_DECL (name)
3912 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3913 TREE_USED (x) = 1;
3915 /* Don't forget if its address was taken in that way. */
3916 if (IDENTIFIER_IMPLICIT_DECL (name)
3917 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3918 TREE_ADDRESSABLE (x) = 1;
3920 /* Warn about mismatches against previous implicit decl. */
3921 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3922 /* If this real decl matches the implicit, don't complain. */
3923 && ! (TREE_CODE (x) == FUNCTION_DECL
3924 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3925 warning
3926 ("`%D' was previously implicitly declared to return `int'", x);
3928 /* If new decl is `static' and an `extern' was seen previously,
3929 warn about it. */
3930 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3931 warn_extern_redeclared_static (x, t);
3933 else
3935 /* Here to install a non-global value. */
3936 tree oldlocal = IDENTIFIER_VALUE (name);
3937 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3939 if (need_new_binding)
3941 push_local_binding (name, x, 0);
3942 /* Because push_local_binding will hook X on to the
3943 current_binding_level's name list, we don't want to
3944 do that again below. */
3945 need_new_binding = 0;
3948 /* If this is a TYPE_DECL, push it into the type value slot. */
3949 if (TREE_CODE (x) == TYPE_DECL)
3950 set_identifier_type_value (name, x);
3952 /* Clear out any TYPE_DECL shadowed by a namespace so that
3953 we won't think this is a type. The C struct hack doesn't
3954 go through namespaces. */
3955 if (TREE_CODE (x) == NAMESPACE_DECL)
3956 set_identifier_type_value (name, NULL_TREE);
3958 if (oldlocal)
3960 tree d = oldlocal;
3962 while (oldlocal
3963 && TREE_CODE (oldlocal) == VAR_DECL
3964 && DECL_DEAD_FOR_LOCAL (oldlocal))
3965 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
3967 if (oldlocal == NULL_TREE)
3968 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
3971 /* If this is an extern function declaration, see if we
3972 have a global definition or declaration for the function. */
3973 if (oldlocal == NULL_TREE
3974 && DECL_EXTERNAL (x)
3975 && oldglobal != NULL_TREE
3976 && TREE_CODE (x) == FUNCTION_DECL
3977 && TREE_CODE (oldglobal) == FUNCTION_DECL)
3979 /* We have one. Their types must agree. */
3980 if (decls_match (x, oldglobal))
3981 /* OK */;
3982 else
3984 warning ("extern declaration of `%#D' doesn't match", x);
3985 cp_warning_at ("global declaration `%#D'", oldglobal);
3988 /* If we have a local external declaration,
3989 and no file-scope declaration has yet been seen,
3990 then if we later have a file-scope decl it must not be static. */
3991 if (oldlocal == NULL_TREE
3992 && oldglobal == NULL_TREE
3993 && DECL_EXTERNAL (x)
3994 && TREE_PUBLIC (x))
3995 TREE_PUBLIC (name) = 1;
3997 /* Warn if shadowing an argument at the top level of the body. */
3998 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3999 /* Inline decls shadow nothing. */
4000 && !DECL_FROM_INLINE (x)
4001 && TREE_CODE (oldlocal) == PARM_DECL
4002 /* Don't check the `this' parameter. */
4003 && !DECL_ARTIFICIAL (oldlocal))
4005 bool err = false;
4007 /* Don't complain if it's from an enclosing function. */
4008 if (DECL_CONTEXT (oldlocal) == current_function_decl
4009 && TREE_CODE (x) != PARM_DECL)
4011 /* Go to where the parms should be and see if we find
4012 them there. */
4013 struct cp_binding_level *b = current_binding_level->level_chain;
4015 /* Skip the ctor/dtor cleanup level. */
4016 b = b->level_chain;
4018 /* ARM $8.3 */
4019 if (b->parm_flag == 1)
4021 error ("declaration of `%#D' shadows a parameter",
4022 name);
4023 err = true;
4027 if (warn_shadow && !err)
4028 shadow_warning (SW_PARAM,
4029 IDENTIFIER_POINTER (name), oldlocal);
4032 /* Maybe warn if shadowing something else. */
4033 else if (warn_shadow && !DECL_EXTERNAL (x)
4034 /* No shadow warnings for internally generated vars. */
4035 && ! DECL_ARTIFICIAL (x)
4036 /* No shadow warnings for vars made for inlining. */
4037 && ! DECL_FROM_INLINE (x))
4039 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4040 && current_class_ptr
4041 && !TREE_STATIC (name))
4042 warning ("declaration of `%s' shadows a member of `this'",
4043 IDENTIFIER_POINTER (name));
4044 else if (oldlocal != NULL_TREE
4045 && TREE_CODE (oldlocal) == VAR_DECL)
4046 shadow_warning (SW_LOCAL,
4047 IDENTIFIER_POINTER (name), oldlocal);
4048 else if (oldglobal != NULL_TREE
4049 && TREE_CODE (oldglobal) == VAR_DECL)
4050 /* XXX shadow warnings in outer-more namespaces */
4051 shadow_warning (SW_GLOBAL,
4052 IDENTIFIER_POINTER (name), oldglobal);
4056 if (TREE_CODE (x) == FUNCTION_DECL)
4057 check_default_args (x);
4059 if (TREE_CODE (x) == VAR_DECL)
4060 maybe_register_incomplete_var (x);
4063 if (need_new_binding)
4064 add_decl_to_level (x,
4065 DECL_NAMESPACE_SCOPE_P (x)
4066 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4067 : current_binding_level);
4069 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4072 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4073 caller to set DECL_CONTEXT properly. */
4075 static tree
4076 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4078 register struct cp_binding_level *b;
4079 tree function_decl = current_function_decl;
4081 timevar_push (TV_NAME_LOOKUP);
4082 current_function_decl = NULL_TREE;
4083 if (level->parm_flag == 2)
4085 b = class_binding_level;
4086 class_binding_level = level;
4087 pushdecl_class_level (x);
4088 class_binding_level = b;
4090 else
4092 b = current_binding_level;
4093 current_binding_level = level;
4094 x = pushdecl (x);
4095 current_binding_level = b;
4097 current_function_decl = function_decl;
4098 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4101 /* Like pushdecl, only it places X in the current namespace,
4102 if appropriate. */
4104 tree
4105 pushdecl_namespace_level (tree x)
4107 register struct cp_binding_level *b = current_binding_level;
4108 register tree t;
4110 timevar_push (TV_NAME_LOOKUP);
4111 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4113 /* Now, the type_shadowed stack may screw us. Munge it so it does
4114 what we want. */
4115 if (TREE_CODE (x) == TYPE_DECL)
4117 tree name = DECL_NAME (x);
4118 tree newval;
4119 tree *ptr = (tree *)0;
4120 for (; !global_scope_p (b); b = b->level_chain)
4122 tree shadowed = b->type_shadowed;
4123 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4124 if (TREE_PURPOSE (shadowed) == name)
4126 ptr = &TREE_VALUE (shadowed);
4127 /* Can't break out of the loop here because sometimes
4128 a binding level will have duplicate bindings for
4129 PT names. It's gross, but I haven't time to fix it. */
4132 newval = TREE_TYPE (x);
4133 if (ptr == (tree *)0)
4135 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4136 up here if this is changed to an assertion. --KR */
4137 SET_IDENTIFIER_TYPE_VALUE (name, x);
4139 else
4141 *ptr = newval;
4144 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4147 /* Like pushdecl, only it places X in the global scope if appropriate.
4148 Calls cp_finish_decl to register the variable, initializing it with
4149 *INIT, if INIT is non-NULL. */
4151 static tree
4152 pushdecl_top_level_1 (tree x, tree *init)
4154 timevar_push (TV_NAME_LOOKUP);
4155 push_to_top_level ();
4156 x = pushdecl_namespace_level (x);
4157 if (init)
4158 cp_finish_decl (x, *init, NULL_TREE, 0);
4159 pop_from_top_level ();
4160 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4163 /* Like pushdecl, only it places X in the global scope if appropriate. */
4165 tree
4166 pushdecl_top_level (tree x)
4168 return pushdecl_top_level_1 (x, NULL);
4171 /* Like pushdecl, only it places X in the global scope if
4172 appropriate. Calls cp_finish_decl to register the variable,
4173 initializing it with INIT. */
4175 tree
4176 pushdecl_top_level_and_finish (tree x, tree init)
4178 return pushdecl_top_level_1 (x, &init);
4181 /* Make the declaration of X appear in CLASS scope. */
4183 bool
4184 pushdecl_class_level (tree x)
4186 tree name;
4187 bool is_valid = true;
4189 timevar_push (TV_NAME_LOOKUP);
4190 /* Get the name of X. */
4191 if (TREE_CODE (x) == OVERLOAD)
4192 name = DECL_NAME (get_first_fn (x));
4193 else
4194 name = DECL_NAME (x);
4196 if (name)
4198 is_valid = push_class_level_binding (name, x);
4199 if (TREE_CODE (x) == TYPE_DECL)
4200 set_identifier_type_value (name, x);
4202 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4204 /* If X is an anonymous aggregate, all of its members are
4205 treated as if they were members of the class containing the
4206 aggregate, for naming purposes. */
4207 tree f;
4209 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4211 location_t save_location = input_location;
4212 input_location = DECL_SOURCE_LOCATION (f);
4213 if (!pushdecl_class_level (f))
4214 is_valid = false;
4215 input_location = save_location;
4218 timevar_pop (TV_NAME_LOOKUP);
4220 return is_valid;
4223 /* Enter DECL into the symbol table, if that's appropriate. Returns
4224 DECL, or a modified version thereof. */
4226 tree
4227 maybe_push_decl (tree decl)
4229 tree type = TREE_TYPE (decl);
4231 /* Add this decl to the current binding level, but not if it comes
4232 from another scope, e.g. a static member variable. TEM may equal
4233 DECL or it may be a previous decl of the same name. */
4234 if (decl == error_mark_node
4235 || (TREE_CODE (decl) != PARM_DECL
4236 && DECL_CONTEXT (decl) != NULL_TREE
4237 /* Definitions of namespace members outside their namespace are
4238 possible. */
4239 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4240 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4241 || TREE_CODE (type) == UNKNOWN_TYPE
4242 /* The declaration of a template specialization does not affect
4243 the functions available for overload resolution, so we do not
4244 call pushdecl. */
4245 || (TREE_CODE (decl) == FUNCTION_DECL
4246 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4247 return decl;
4248 else
4249 return pushdecl (decl);
4252 /* Make the declaration(s) of X appear in CLASS scope under the name
4253 NAME. Returns true if the binding is valid. */
4255 bool
4256 push_class_level_binding (tree name, tree x)
4258 cxx_binding *binding;
4260 timevar_push (TV_NAME_LOOKUP);
4261 /* The class_binding_level will be NULL if x is a template
4262 parameter name in a member template. */
4263 if (!class_binding_level)
4264 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4266 /* Make sure that this new member does not have the same name
4267 as a template parameter. */
4268 if (TYPE_BEING_DEFINED (current_class_type))
4269 check_template_shadow (x);
4271 /* If this declaration shadows a declaration from an enclosing
4272 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4273 we leave this class. Record the shadowed declaration here. */
4274 binding = IDENTIFIER_BINDING (name);
4275 if (binding && BINDING_VALUE (binding))
4277 tree bval = BINDING_VALUE (binding);
4278 tree old_decl = NULL_TREE;
4280 if (INHERITED_VALUE_BINDING_P (binding))
4282 /* If the old binding was from a base class, and was for a
4283 tag name, slide it over to make room for the new binding.
4284 The old binding is still visible if explicitly qualified
4285 with a class-key. */
4286 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
4287 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4289 old_decl = BINDING_TYPE (binding);
4290 BINDING_TYPE (binding) = bval;
4291 BINDING_VALUE (binding) = NULL_TREE;
4292 INHERITED_VALUE_BINDING_P (binding) = 0;
4294 else
4295 old_decl = bval;
4297 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
4298 old_decl = bval;
4299 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
4300 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4301 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
4302 old_decl = bval;
4303 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
4304 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4306 if (old_decl)
4308 tree shadow;
4310 /* Find the previous binding of name on the class-shadowed
4311 list, and update it. */
4312 for (shadow = class_binding_level->class_shadowed;
4313 shadow;
4314 shadow = TREE_CHAIN (shadow))
4315 if (TREE_PURPOSE (shadow) == name
4316 && TREE_TYPE (shadow) == old_decl)
4318 BINDING_VALUE (binding) = x;
4319 INHERITED_VALUE_BINDING_P (binding) = 0;
4320 TREE_TYPE (shadow) = x;
4321 IDENTIFIER_CLASS_VALUE (name) = x;
4322 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4327 /* If we didn't replace an existing binding, put the binding on the
4328 stack of bindings for the identifier, and update the shadowed list. */
4329 if (push_class_binding (name, x))
4331 class_binding_level->class_shadowed
4332 = tree_cons (name, NULL,
4333 class_binding_level->class_shadowed);
4334 /* Record the value we are binding NAME to so that we can know
4335 what to pop later. */
4336 TREE_TYPE (class_binding_level->class_shadowed) = x;
4337 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4340 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
4343 /* Insert another USING_DECL into the current binding level, returning
4344 this declaration. If this is a redeclaration, do nothing, and
4345 return NULL_TREE if this not in namespace scope (in namespace
4346 scope, a using decl might extend any previous bindings). */
4348 tree
4349 push_using_decl (tree scope, tree name)
4351 tree decl;
4353 timevar_push (TV_NAME_LOOKUP);
4354 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4355 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4356 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4357 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4358 break;
4359 if (decl)
4360 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4361 namespace_bindings_p () ? decl : NULL_TREE);
4362 decl = build_lang_decl (USING_DECL, name, void_type_node);
4363 DECL_INITIAL (decl) = scope;
4364 TREE_CHAIN (decl) = current_binding_level->usings;
4365 current_binding_level->usings = decl;
4366 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4369 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4370 changed (i.e. there was already a directive), or the fresh
4371 TREE_LIST otherwise. */
4373 tree
4374 push_using_directive (tree used)
4376 tree ud = current_binding_level->using_directives;
4377 tree iter, ancestor;
4379 timevar_push (TV_NAME_LOOKUP);
4380 /* Check if we already have this. */
4381 if (purpose_member (used, ud) != NULL_TREE)
4382 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4384 ancestor = namespace_ancestor (current_decl_namespace (), used);
4385 ud = current_binding_level->using_directives;
4386 ud = tree_cons (used, ancestor, ud);
4387 current_binding_level->using_directives = ud;
4389 /* Recursively add all namespaces used. */
4390 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4391 push_using_directive (TREE_PURPOSE (iter));
4393 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4396 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4397 other definitions already in place. We get around this by making
4398 the value of the identifier point to a list of all the things that
4399 want to be referenced by that name. It is then up to the users of
4400 that name to decide what to do with that list.
4402 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4403 DECL_TEMPLATE_RESULT. It is dealt with the same way.
4405 FLAGS is a bitwise-or of the following values:
4406 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4407 namespace scope.
4408 PUSH_USING: DECL is being pushed as the result of a using
4409 declaration.
4411 The value returned may be a previous declaration if we guessed wrong
4412 about what language DECL should belong to (C or C++). Otherwise,
4413 it's always DECL (and never something that's not a _DECL). */
4415 tree
4416 push_overloaded_decl (tree decl, int flags)
4418 tree name = DECL_NAME (decl);
4419 tree old;
4420 tree new_binding;
4421 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4423 timevar_push (TV_NAME_LOOKUP);
4424 if (doing_global)
4425 old = namespace_binding (name, DECL_CONTEXT (decl));
4426 else
4427 old = lookup_name_current_level (name);
4429 if (old)
4431 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4433 tree t = TREE_TYPE (old);
4434 if (IS_AGGR_TYPE (t) && warn_shadow
4435 && (! DECL_IN_SYSTEM_HEADER (decl)
4436 || ! DECL_IN_SYSTEM_HEADER (old)))
4437 warning ("`%#D' hides constructor for `%#T'", decl, t);
4438 old = NULL_TREE;
4440 else if (is_overloaded_fn (old))
4442 tree tmp;
4444 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4446 tree fn = OVL_CURRENT (tmp);
4448 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4449 && !(flags & PUSH_USING)
4450 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4451 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4452 error ("`%#D' conflicts with previous using declaration `%#D'",
4453 decl, fn);
4455 if (duplicate_decls (decl, fn))
4456 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4459 else if (old == error_mark_node)
4460 /* Ignore the undefined symbol marker. */
4461 old = NULL_TREE;
4462 else
4464 cp_error_at ("previous non-function declaration `%#D'", old);
4465 error ("conflicts with function declaration `%#D'", decl);
4466 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4470 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4472 if (old && TREE_CODE (old) != OVERLOAD)
4473 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4474 else
4475 new_binding = ovl_cons (decl, old);
4476 if (flags & PUSH_USING)
4477 OVL_USED (new_binding) = 1;
4479 else
4480 /* NAME is not ambiguous. */
4481 new_binding = decl;
4483 if (doing_global)
4484 set_namespace_binding (name, current_namespace, new_binding);
4485 else
4487 /* We only create an OVERLOAD if there was a previous binding at
4488 this level, or if decl is a template. In the former case, we
4489 need to remove the old binding and replace it with the new
4490 binding. We must also run through the NAMES on the binding
4491 level where the name was bound to update the chain. */
4493 if (TREE_CODE (new_binding) == OVERLOAD && old)
4495 tree *d;
4497 for (d = &BINDING_SCOPE (IDENTIFIER_BINDING (name))->names;
4499 d = &TREE_CHAIN (*d))
4500 if (*d == old
4501 || (TREE_CODE (*d) == TREE_LIST
4502 && TREE_VALUE (*d) == old))
4504 if (TREE_CODE (*d) == TREE_LIST)
4505 /* Just replace the old binding with the new. */
4506 TREE_VALUE (*d) = new_binding;
4507 else
4508 /* Build a TREE_LIST to wrap the OVERLOAD. */
4509 *d = tree_cons (NULL_TREE, new_binding,
4510 TREE_CHAIN (*d));
4512 /* And update the cxx_binding node. */
4513 BINDING_VALUE (IDENTIFIER_BINDING (name))
4514 = new_binding;
4515 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4518 /* We should always find a previous binding in this case. */
4519 abort ();
4522 /* Install the new binding. */
4523 push_local_binding (name, new_binding, flags);
4526 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4529 /* Generate an implicit declaration for identifier FUNCTIONID
4530 as a function of type int (). Print a warning if appropriate. */
4532 tree
4533 implicitly_declare (tree functionid)
4535 register tree decl;
4537 /* We used to reuse an old implicit decl here,
4538 but this loses with inline functions because it can clobber
4539 the saved decl chains. */
4540 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4542 DECL_EXTERNAL (decl) = 1;
4543 TREE_PUBLIC (decl) = 1;
4545 /* ISO standard says implicit declarations are in the innermost block.
4546 So we record the decl in the standard fashion. */
4547 pushdecl (decl);
4548 rest_of_decl_compilation (decl, NULL, 0, 0);
4550 if (warn_implicit
4551 /* Only one warning per identifier. */
4552 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4554 pedwarn ("implicit declaration of function `%#D'", decl);
4557 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4559 return decl;
4562 /* Return zero if the declaration NEWDECL is valid
4563 when the declaration OLDDECL (assumed to be for the same name)
4564 has already been seen.
4565 Otherwise return an error message format string with a %s
4566 where the identifier should go. */
4568 static const char *
4569 redeclaration_error_message (tree newdecl, tree olddecl)
4571 if (TREE_CODE (newdecl) == TYPE_DECL)
4573 /* Because C++ can put things into name space for free,
4574 constructs like "typedef struct foo { ... } foo"
4575 would look like an erroneous redeclaration. */
4576 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4577 return 0;
4578 else
4579 return "redefinition of `%#D'";
4581 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4583 /* If this is a pure function, its olddecl will actually be
4584 the original initialization to `0' (which we force to call
4585 abort()). Don't complain about redefinition in this case. */
4586 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4587 return 0;
4589 /* If both functions come from different namespaces, this is not
4590 a redeclaration - this is a conflict with a used function. */
4591 if (DECL_NAMESPACE_SCOPE_P (olddecl)
4592 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4593 return "`%D' conflicts with used function";
4595 /* We'll complain about linkage mismatches in
4596 warn_extern_redeclared_static. */
4598 /* Defining the same name twice is no good. */
4599 if (DECL_INITIAL (olddecl) != NULL_TREE
4600 && DECL_INITIAL (newdecl) != NULL_TREE)
4602 if (DECL_NAME (olddecl) == NULL_TREE)
4603 return "`%#D' not declared in class";
4604 else
4605 return "redefinition of `%#D'";
4607 return 0;
4609 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4611 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4612 && (DECL_TEMPLATE_RESULT (newdecl)
4613 != DECL_TEMPLATE_RESULT (olddecl))
4614 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4615 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4616 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4617 && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4618 && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4619 return "redefinition of `%#D'";
4620 return 0;
4622 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4624 /* Objects declared at top level: */
4625 /* If at least one is a reference, it's ok. */
4626 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4627 return 0;
4628 /* Reject two definitions. */
4629 return "redefinition of `%#D'";
4631 else
4633 /* Objects declared with block scope: */
4634 /* Reject two definitions, and reject a definition
4635 together with an external reference. */
4636 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4637 return "redeclaration of `%#D'";
4638 return 0;
4642 /* Create a new label, named ID. */
4644 static tree
4645 make_label_decl (tree id, int local_p)
4647 tree decl;
4649 decl = build_decl (LABEL_DECL, id, void_type_node);
4650 if (expanding_p)
4651 /* Make sure every label has an rtx. */
4652 label_rtx (decl);
4654 DECL_CONTEXT (decl) = current_function_decl;
4655 DECL_MODE (decl) = VOIDmode;
4656 C_DECLARED_LABEL_FLAG (decl) = local_p;
4658 /* Say where one reference is to the label, for the sake of the
4659 error if it is not defined. */
4660 DECL_SOURCE_LOCATION (decl) = input_location;
4662 /* Record the fact that this identifier is bound to this label. */
4663 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4665 return decl;
4668 /* Record this label on the list of used labels so that we can check
4669 at the end of the function to see whether or not the label was
4670 actually defined, and so we can check when the label is defined whether
4671 this use is valid. */
4673 static void
4674 use_label (tree decl)
4676 if (named_label_uses == NULL
4677 || named_label_uses->names_in_scope != current_binding_level->names
4678 || named_label_uses->label_decl != decl)
4680 struct named_label_use_list *new_ent;
4681 new_ent = ggc_alloc (sizeof (struct named_label_use_list));
4682 new_ent->label_decl = decl;
4683 new_ent->names_in_scope = current_binding_level->names;
4684 new_ent->binding_level = current_binding_level;
4685 new_ent->o_goto_locus = input_location;
4686 new_ent->next = named_label_uses;
4687 named_label_uses = new_ent;
4691 /* Look for a label named ID in the current function. If one cannot
4692 be found, create one. (We keep track of used, but undefined,
4693 labels, and complain about them at the end of a function.) */
4695 tree
4696 lookup_label (tree id)
4698 tree decl;
4699 struct named_label_list *ent;
4701 timevar_push (TV_NAME_LOOKUP);
4702 /* You can't use labels at global scope. */
4703 if (current_function_decl == NULL_TREE)
4705 error ("label `%s' referenced outside of any function",
4706 IDENTIFIER_POINTER (id));
4707 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4710 /* See if we've already got this label. */
4711 decl = IDENTIFIER_LABEL_VALUE (id);
4712 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4713 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4715 /* Record this label on the list of labels used in this function.
4716 We do this before calling make_label_decl so that we get the
4717 IDENTIFIER_LABEL_VALUE before the new label is declared. */
4718 ent = ggc_alloc_cleared (sizeof (struct named_label_list));
4719 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4720 ent->next = named_labels;
4721 named_labels = ent;
4723 /* We need a new label. */
4724 decl = make_label_decl (id, /*local_p=*/0);
4726 /* Now fill in the information we didn't have before. */
4727 ent->label_decl = decl;
4729 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4732 /* Declare a local label named ID. */
4734 tree
4735 declare_local_label (tree id)
4737 tree decl;
4739 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4740 this scope we can restore the old value of
4741 IDENTIFIER_TYPE_VALUE. */
4742 current_binding_level->shadowed_labels
4743 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4744 current_binding_level->shadowed_labels);
4745 /* Look for the label. */
4746 decl = make_label_decl (id, /*local_p=*/1);
4747 /* Now fill in the information we didn't have before. */
4748 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4750 return decl;
4753 /* Returns nonzero if it is ill-formed to jump past the declaration of
4754 DECL. Returns 2 if it's also a real problem. */
4756 static int
4757 decl_jump_unsafe (tree decl)
4759 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4760 return 0;
4762 if (DECL_INITIAL (decl) == NULL_TREE
4763 && pod_type_p (TREE_TYPE (decl)))
4764 return 0;
4766 /* This is really only important if we're crossing an initialization.
4767 The POD stuff is just pedantry; why should it matter if the class
4768 contains a field of pointer to member type? */
4769 if (DECL_INITIAL (decl)
4770 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4771 return 2;
4772 return 1;
4775 /* Check that a single previously seen jump to a newly defined label
4776 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4777 the jump context; NAMES are the names in scope in LEVEL at the jump
4778 context; FILE and LINE are the source position of the jump or 0. */
4780 static void
4781 check_previous_goto_1 (tree decl,
4782 struct cp_binding_level* level,
4783 tree names, const location_t *locus)
4785 int identified = 0;
4786 int saw_eh = 0;
4787 struct cp_binding_level *b = current_binding_level;
4788 for (; b; b = b->level_chain)
4790 tree new_decls = b->names;
4791 tree old_decls = (b == level ? names : NULL_TREE);
4792 for (; new_decls != old_decls;
4793 new_decls = TREE_CHAIN (new_decls))
4795 int problem = decl_jump_unsafe (new_decls);
4796 if (! problem)
4797 continue;
4799 if (! identified)
4801 if (decl)
4802 pedwarn ("jump to label `%D'", decl);
4803 else
4804 pedwarn ("jump to case label");
4806 if (locus)
4807 pedwarn ("%H from here", locus);
4808 identified = 1;
4811 if (problem > 1)
4812 cp_error_at (" crosses initialization of `%#D'",
4813 new_decls);
4814 else
4815 cp_pedwarn_at (" enters scope of non-POD `%#D'",
4816 new_decls);
4819 if (b == level)
4820 break;
4821 if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
4823 if (! identified)
4825 if (decl)
4826 pedwarn ("jump to label `%D'", decl);
4827 else
4828 pedwarn ("jump to case label");
4830 if (locus)
4831 pedwarn ("%H from here", locus);
4832 identified = 1;
4834 if (b->is_try_scope)
4835 error (" enters try block");
4836 else
4837 error (" enters catch block");
4838 saw_eh = 1;
4843 static void
4844 check_previous_goto (struct named_label_use_list* use)
4846 check_previous_goto_1 (use->label_decl, use->binding_level,
4847 use->names_in_scope, &use->o_goto_locus);
4850 static void
4851 check_switch_goto (struct cp_binding_level* level)
4853 check_previous_goto_1 (NULL_TREE, level, level->names, NULL);
4856 /* Check that any previously seen jumps to a newly defined label DECL
4857 are OK. Called by define_label. */
4859 static void
4860 check_previous_gotos (tree decl)
4862 struct named_label_use_list **usep;
4864 if (! TREE_USED (decl))
4865 return;
4867 for (usep = &named_label_uses; *usep; )
4869 struct named_label_use_list *use = *usep;
4870 if (use->label_decl == decl)
4872 check_previous_goto (use);
4873 *usep = use->next;
4875 else
4876 usep = &(use->next);
4880 /* Check that a new jump to a label DECL is OK. Called by
4881 finish_goto_stmt. */
4883 void
4884 check_goto (tree decl)
4886 int identified = 0;
4887 tree bad;
4888 struct named_label_list *lab;
4890 /* We can't know where a computed goto is jumping. So we assume
4891 that it's OK. */
4892 if (! DECL_P (decl))
4893 return;
4895 /* If the label hasn't been defined yet, defer checking. */
4896 if (! DECL_INITIAL (decl))
4898 use_label (decl);
4899 return;
4902 for (lab = named_labels; lab; lab = lab->next)
4903 if (decl == lab->label_decl)
4904 break;
4906 /* If the label is not on named_labels it's a gcc local label, so
4907 it must be in an outer scope, so jumping to it is always OK. */
4908 if (lab == 0)
4909 return;
4911 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4912 && !identified)
4914 cp_pedwarn_at ("jump to label `%D'", decl);
4915 pedwarn (" from here");
4916 identified = 1;
4919 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4921 tree b = TREE_VALUE (bad);
4922 int u = decl_jump_unsafe (b);
4924 if (u > 1 && DECL_ARTIFICIAL (b))
4925 /* Can't skip init of __exception_info. */
4926 error ("%H enters catch block", &DECL_SOURCE_LOCATION (b));
4927 else if (u > 1)
4928 cp_error_at (" skips initialization of `%#D'", b);
4929 else
4930 cp_pedwarn_at (" enters scope of non-POD `%#D'", b);
4933 if (lab->in_try_scope)
4934 error (" enters try block");
4935 else if (lab->in_catch_scope)
4936 error (" enters catch block");
4939 /* Define a label, specifying the location in the source file.
4940 Return the LABEL_DECL node for the label, if the definition is valid.
4941 Otherwise return 0. */
4943 tree
4944 define_label (location_t location, tree name)
4946 tree decl = lookup_label (name);
4947 struct named_label_list *ent;
4948 register struct cp_binding_level *p;
4950 timevar_push (TV_NAME_LOOKUP);
4951 for (ent = named_labels; ent; ent = ent->next)
4952 if (ent->label_decl == decl)
4953 break;
4955 /* After labels, make any new cleanups in the function go into their
4956 own new (temporary) binding contour. */
4957 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
4958 p->more_cleanups_ok = 0;
4960 if (name == get_identifier ("wchar_t"))
4961 pedwarn ("label named wchar_t");
4963 if (DECL_INITIAL (decl) != NULL_TREE)
4965 error ("duplicate label `%D'", decl);
4966 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4968 else
4970 /* Mark label as having been defined. */
4971 DECL_INITIAL (decl) = error_mark_node;
4972 /* Say where in the source. */
4973 DECL_SOURCE_LOCATION (decl) = location;
4974 if (ent)
4976 ent->names_in_scope = current_binding_level->names;
4977 ent->binding_level = current_binding_level;
4979 check_previous_gotos (decl);
4980 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4982 timevar_pop (TV_NAME_LOOKUP);
4985 struct cp_switch
4987 struct cp_binding_level *level;
4988 struct cp_switch *next;
4989 /* The SWITCH_STMT being built. */
4990 tree switch_stmt;
4991 /* A splay-tree mapping the low element of a case range to the high
4992 element, or NULL_TREE if there is no high element. Used to
4993 determine whether or not a new case label duplicates an old case
4994 label. We need a tree, rather than simply a hash table, because
4995 of the GNU case range extension. */
4996 splay_tree cases;
4999 /* A stack of the currently active switch statements. The innermost
5000 switch statement is on the top of the stack. There is no need to
5001 mark the stack for garbage collection because it is only active
5002 during the processing of the body of a function, and we never
5003 collect at that point. */
5005 static struct cp_switch *switch_stack;
5007 /* Called right after a switch-statement condition is parsed.
5008 SWITCH_STMT is the switch statement being parsed. */
5010 void
5011 push_switch (tree switch_stmt)
5013 struct cp_switch *p = xmalloc (sizeof (struct cp_switch));
5014 p->level = current_binding_level;
5015 p->next = switch_stack;
5016 p->switch_stmt = switch_stmt;
5017 p->cases = splay_tree_new (case_compare, NULL, NULL);
5018 switch_stack = p;
5021 void
5022 pop_switch (void)
5024 struct cp_switch *cs;
5026 cs = switch_stack;
5027 splay_tree_delete (cs->cases);
5028 switch_stack = switch_stack->next;
5029 free (cs);
5032 /* Note that we've seen a definition of a case label, and complain if this
5033 is a bad place for one. */
5035 tree
5036 finish_case_label (tree low_value, tree high_value)
5038 tree cond, r;
5039 register struct cp_binding_level *p;
5041 if (! switch_stack)
5043 if (high_value)
5044 error ("case label not within a switch statement");
5045 else if (low_value)
5046 error ("case label `%E' not within a switch statement",
5047 low_value);
5048 else
5049 error ("`default' label not within a switch statement");
5050 return NULL_TREE;
5053 if (processing_template_decl)
5055 tree label;
5057 /* For templates, just add the case label; we'll do semantic
5058 analysis at instantiation-time. */
5059 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5060 return add_stmt (build_case_label (low_value, high_value, label));
5063 /* Find the condition on which this switch statement depends. */
5064 cond = SWITCH_COND (switch_stack->switch_stmt);
5065 if (cond && TREE_CODE (cond) == TREE_LIST)
5066 cond = TREE_VALUE (cond);
5068 r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5070 check_switch_goto (switch_stack->level);
5072 /* After labels, make any new cleanups in the function go into their
5073 own new (temporary) binding contour. */
5074 for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5075 p->more_cleanups_ok = 0;
5077 return r;
5080 /* Return the list of declarations of the current level.
5081 Note that this list is in reverse order unless/until
5082 you nreverse it; and when you do nreverse it, you must
5083 store the result back using `storedecls' or you will lose. */
5085 tree
5086 getdecls (void)
5088 return current_binding_level->names;
5091 /* Store the list of declarations of the current level.
5092 This is done for the parameter declarations of a function being defined,
5093 after they are modified in the light of any missing parameters. */
5095 static void
5096 storedecls (tree decls)
5098 current_binding_level->names = decls;
5101 /* Set the current binding TABLE for type declarations.. This is a
5102 temporary workaround of the fact that the data structure classtypes
5103 does not currently carry its allocated cxx_scope structure. */
5104 void
5105 cxx_remember_type_decls (binding_table table)
5107 current_binding_level->type_decls = table;
5110 /* Return the type that should be used when TYPE's name is preceded
5111 by a tag such as 'struct' or 'union', or null if the name cannot
5112 be used in this way.
5114 For example, when processing the third line of:
5116 struct A;
5117 typedef struct A A;
5118 struct A;
5120 lookup of A will find the typedef. Given A's typedef, this function
5121 will return the type associated with "struct A". For the tag to be
5122 anything other than TYPE, TYPE must be a typedef whose original type
5123 has the same name and context as TYPE itself.
5125 It is not valid for a typedef of an anonymous type to be used with
5126 an explicit tag:
5128 typedef struct { ... } B;
5129 struct B;
5131 Return null for this case. */
5133 static tree
5134 follow_tag_typedef (tree type)
5136 tree original;
5138 original = original_type (type);
5139 if (! TYPE_NAME (original))
5140 return NULL_TREE;
5141 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5142 && (CP_DECL_CONTEXT (TYPE_NAME (original))
5143 == CP_DECL_CONTEXT (TYPE_NAME (type)))
5144 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5145 return original;
5146 else
5147 return NULL_TREE;
5150 /* Given NAME, an IDENTIFIER_NODE,
5151 return the structure (or union or enum) definition for that name.
5152 Searches binding levels from BINDING_SCOPE up to the global level.
5153 If THISLEVEL_ONLY is nonzero, searches only the specified context
5154 (but skips any tag-transparent contexts to find one that is
5155 meaningful for tags).
5156 FORM says which kind of type the caller wants;
5157 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5158 If the wrong kind of type is found, and it's not a template, an error is
5159 reported. */
5161 static tree
5162 lookup_tag (enum tree_code form, tree name,
5163 struct cp_binding_level* binding_level, int thislevel_only)
5165 register struct cp_binding_level *level;
5166 /* Nonzero if, we should look past a template parameter level, even
5167 if THISLEVEL_ONLY. */
5168 int allow_template_parms_p = 1;
5169 bool type_is_anonymous = ANON_AGGRNAME_P (name);
5171 timevar_push (TV_NAME_LOOKUP);
5172 for (level = binding_level; level; level = level->level_chain)
5174 register tree tail;
5175 if (type_is_anonymous && level->type_decls != NULL)
5177 tree type = binding_table_find_anon_type (level->type_decls, name);
5178 /* There is no need for error checking here, because
5179 anon names are unique throughout the compilation. */
5180 if (type != NULL)
5181 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
5183 else if (level->namespace_p)
5184 /* Do namespace lookup. */
5185 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5187 cxx_binding *binding =
5188 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
5189 tree old;
5191 /* If we just skipped past a template parameter level,
5192 even though THISLEVEL_ONLY, and we find a template
5193 class declaration, then we use the _TYPE node for the
5194 template. See the example below. */
5195 if (thislevel_only && !allow_template_parms_p
5196 && binding && BINDING_VALUE (binding)
5197 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (binding)))
5198 old = BINDING_VALUE (binding);
5199 else if (binding)
5200 old = select_decl (binding, LOOKUP_PREFER_TYPES);
5201 else
5202 old = NULL_TREE;
5204 if (old)
5206 /* We've found something at this binding level. If it is
5207 a typedef, extract the tag it refers to. Lookup fails
5208 if the typedef doesn't refer to a taggable type. */
5209 old = TREE_TYPE (old);
5210 old = follow_tag_typedef (old);
5211 if (!old)
5212 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5213 if (TREE_CODE (old) != form
5214 && (form == ENUMERAL_TYPE
5215 || TREE_CODE (old) == ENUMERAL_TYPE))
5217 error ("`%#D' redeclared as %C", old, form);
5218 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5220 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
5222 if (thislevel_only || tail == global_namespace)
5223 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5225 else if (level->type_decls != NULL)
5227 binding_entry entry = binding_table_find (level->type_decls, name);
5228 if (entry != NULL)
5230 enum tree_code code = TREE_CODE (entry->type);
5232 if (code != form
5233 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5235 /* Definition isn't the kind we were looking for. */
5236 error ("`%#D' redeclared as %C", entry->type, form);
5237 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5239 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
5242 if (thislevel_only && ! level->tag_transparent)
5244 if (level->template_parms_p && allow_template_parms_p)
5246 /* We must deal with cases like this:
5248 template <class T> struct S;
5249 template <class T> struct S {};
5251 When looking up `S', for the second declaration, we
5252 would like to find the first declaration. But, we
5253 are in the pseudo-global level created for the
5254 template parameters, rather than the (surrounding)
5255 namespace level. Thus, we keep going one more level,
5256 even though THISLEVEL_ONLY is nonzero. */
5257 allow_template_parms_p = 0;
5258 continue;
5260 else
5261 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5264 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5267 /* Given a type, find the tag that was defined for it and return the tag name.
5268 Otherwise return 0. However, the value can never be 0
5269 in the cases in which this is used.
5271 C++: If NAME is nonzero, this is the new name to install. This is
5272 done when replacing anonymous tags with real tag names. */
5274 static tree
5275 lookup_tag_reverse (tree type, tree name)
5277 register struct cp_binding_level *level;
5279 timevar_push (TV_NAME_LOOKUP);
5280 for (level = current_binding_level; level; level = level->level_chain)
5282 binding_entry entry = level->type_decls == NULL
5283 ? NULL
5284 : binding_table_reverse_maybe_remap (level->type_decls, type, name);
5285 if (entry)
5286 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
5288 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5291 /* Look up NAME in the NAMESPACE. */
5293 tree
5294 lookup_namespace_name (tree namespace, tree name)
5296 tree val;
5297 tree template_id = NULL_TREE;
5298 cxx_binding binding;
5300 timevar_push (TV_NAME_LOOKUP);
5301 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5303 if (TREE_CODE (name) == NAMESPACE_DECL)
5304 /* This happens for A::B<int> when B is a namespace. */
5305 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5306 else if (TREE_CODE (name) == TEMPLATE_DECL)
5308 /* This happens for A::B where B is a template, and there are no
5309 template arguments. */
5310 error ("invalid use of `%D'", name);
5311 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5314 namespace = ORIGINAL_NAMESPACE (namespace);
5316 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5318 template_id = name;
5319 name = TREE_OPERAND (name, 0);
5320 if (TREE_CODE (name) == OVERLOAD)
5321 name = DECL_NAME (OVL_CURRENT (name));
5322 else if (DECL_P (name))
5323 name = DECL_NAME (name);
5326 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5328 cxx_binding_clear (&binding);
5329 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
5330 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5332 if (binding.value)
5334 val = binding.value;
5336 if (template_id)
5338 if (DECL_CLASS_TEMPLATE_P (val))
5339 val = lookup_template_class (val,
5340 TREE_OPERAND (template_id, 1),
5341 /*in_decl=*/NULL_TREE,
5342 /*context=*/NULL_TREE,
5343 /*entering_scope=*/0,
5344 tf_error | tf_warning);
5345 else if (DECL_FUNCTION_TEMPLATE_P (val)
5346 || TREE_CODE (val) == OVERLOAD)
5347 val = lookup_template_function (val,
5348 TREE_OPERAND (template_id, 1));
5349 else
5351 error ("`%D::%D' is not a template",
5352 namespace, name);
5353 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5357 /* If we have a single function from a using decl, pull it out. */
5358 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5359 val = OVL_FUNCTION (val);
5361 /* Ignore built-in functions that haven't been prototyped yet. */
5362 if (!val || !DECL_P(val)
5363 || !DECL_LANG_SPECIFIC(val)
5364 || !DECL_ANTICIPATED (val))
5365 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5368 error ("`%D' undeclared in namespace `%D'", name, namespace);
5369 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5372 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5374 static hashval_t
5375 typename_hash (const void* k)
5377 hashval_t hash;
5378 tree t = (tree) k;
5380 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5381 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5383 return hash;
5386 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5388 static int
5389 typename_compare (const void * k1, const void * k2)
5391 tree t1;
5392 tree t2;
5393 tree d1;
5394 tree d2;
5396 t1 = (tree) k1;
5397 t2 = (tree) k2;
5398 d1 = TYPE_NAME (t1);
5399 d2 = TYPE_NAME (t2);
5401 return (DECL_NAME (d1) == DECL_NAME (d2)
5402 && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5403 && ((TREE_TYPE (t1) != NULL_TREE)
5404 == (TREE_TYPE (t2) != NULL_TREE))
5405 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5406 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5409 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5410 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5411 is non-NULL, this type is being created by the implicit typename
5412 extension, and BASE_TYPE is a type named `t' in some base class of
5413 `T' which depends on template parameters.
5415 Returns the new TYPENAME_TYPE. */
5417 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5419 static tree
5420 build_typename_type (tree context, tree name, tree fullname)
5422 tree t;
5423 tree d;
5424 void **e;
5426 if (typename_htab == NULL)
5428 typename_htab = htab_create_ggc (61, &typename_hash,
5429 &typename_compare, NULL);
5432 /* Build the TYPENAME_TYPE. */
5433 t = make_aggr_type (TYPENAME_TYPE);
5434 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5435 TYPENAME_TYPE_FULLNAME (t) = fullname;
5437 /* Build the corresponding TYPE_DECL. */
5438 d = build_decl (TYPE_DECL, name, t);
5439 TYPE_NAME (TREE_TYPE (d)) = d;
5440 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5441 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5442 DECL_ARTIFICIAL (d) = 1;
5444 /* See if we already have this type. */
5445 e = htab_find_slot (typename_htab, t, INSERT);
5446 if (*e)
5447 t = (tree) *e;
5448 else
5449 *e = t;
5451 return t;
5454 /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type,
5455 unless an error occurs, in which case error_mark_node is returned.
5456 If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5457 set, we return that, rather than the _TYPE it corresponds to, in
5458 other cases we look through the type decl. If TF_ERROR is set,
5459 complain about errors, otherwise be quiet. */
5461 tree
5462 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5464 tree fullname;
5466 if (name == error_mark_node
5467 || context == NULL_TREE
5468 || context == error_mark_node)
5469 return error_mark_node;
5471 if (TYPE_P (name))
5473 if (!(TYPE_LANG_SPECIFIC (name)
5474 && (CLASSTYPE_IS_TEMPLATE (name)
5475 || CLASSTYPE_USE_TEMPLATE (name))))
5476 name = TYPE_IDENTIFIER (name);
5477 else
5478 /* Create a TEMPLATE_ID_EXPR for the type. */
5479 name = build_nt (TEMPLATE_ID_EXPR,
5480 CLASSTYPE_TI_TEMPLATE (name),
5481 CLASSTYPE_TI_ARGS (name));
5483 else if (TREE_CODE (name) == TYPE_DECL)
5484 name = DECL_NAME (name);
5486 fullname = name;
5488 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5490 name = TREE_OPERAND (name, 0);
5491 if (TREE_CODE (name) == TEMPLATE_DECL)
5492 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5494 if (TREE_CODE (name) == TEMPLATE_DECL)
5496 error ("`%D' used without template parameters", name);
5497 return error_mark_node;
5499 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 20030802);
5501 if (TREE_CODE (context) == NAMESPACE_DECL)
5503 /* We can get here from typename_sub0 in the explicit_template_type
5504 expansion. Just fail. */
5505 if (complain & tf_error)
5506 error ("no class template named `%#T' in `%#T'",
5507 name, context);
5508 return error_mark_node;
5511 if (! uses_template_parms (context)
5512 || currently_open_class (context))
5514 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5516 tree tmpl = NULL_TREE;
5517 if (IS_AGGR_TYPE (context))
5518 tmpl = lookup_field (context, name, 0, false);
5519 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5521 if (complain & tf_error)
5522 error ("no class template named `%#T' in `%#T'",
5523 name, context);
5524 return error_mark_node;
5527 if (complain & tf_error)
5528 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
5530 return lookup_template_class (tmpl,
5531 TREE_OPERAND (fullname, 1),
5532 NULL_TREE, context,
5533 /*entering_scope=*/0,
5534 tf_error | tf_warning | tf_user);
5536 else
5538 tree t;
5540 if (!IS_AGGR_TYPE (context))
5542 if (complain & tf_error)
5543 error ("no type named `%#T' in `%#T'", name, context);
5544 return error_mark_node;
5547 t = lookup_field (context, name, 0, true);
5548 if (t)
5550 if (TREE_CODE (t) != TYPE_DECL)
5552 if (complain & tf_error)
5553 error ("no type named `%#T' in `%#T'", name, context);
5554 return error_mark_node;
5557 if (complain & tf_error)
5558 perform_or_defer_access_check (TYPE_BINFO (context), t);
5560 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5561 t = TREE_TYPE (t);
5563 return t;
5568 /* If the CONTEXT is not a template type, then either the field is
5569 there now or its never going to be. */
5570 if (!uses_template_parms (context))
5572 if (complain & tf_error)
5573 error ("no type named `%#T' in `%#T'", name, context);
5574 return error_mark_node;
5577 return build_typename_type (context, name, fullname);
5580 /* Resolve `CONTEXT::template NAME'. Returns an appropriate type,
5581 unless an error occurs, in which case error_mark_node is returned.
5582 If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5583 corresponds to. If COMPLAIN zero, don't complain about any errors
5584 that occur. */
5586 tree
5587 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5589 tree t;
5590 tree d;
5592 if (TYPE_P (name))
5593 name = TYPE_IDENTIFIER (name);
5594 else if (DECL_P (name))
5595 name = DECL_NAME (name);
5596 if (TREE_CODE (name) != IDENTIFIER_NODE)
5597 abort ();
5599 if (!uses_template_parms (context)
5600 || currently_open_class (context))
5602 tree tmpl = NULL_TREE;
5604 if (IS_AGGR_TYPE (context))
5605 tmpl = lookup_field (context, name, 0, false);
5607 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5609 if (complain & tf_error)
5610 error ("no class template named `%#T' in `%#T'", name, context);
5611 return error_mark_node;
5614 if (complain & tf_error)
5615 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
5617 return tmpl;
5620 /* Build the UNBOUND_CLASS_TEMPLATE. */
5621 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5622 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5623 TREE_TYPE (t) = NULL_TREE;
5625 /* Build the corresponding TEMPLATE_DECL. */
5626 d = build_decl (TEMPLATE_DECL, name, t);
5627 TYPE_NAME (TREE_TYPE (d)) = d;
5628 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5629 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5630 DECL_ARTIFICIAL (d) = 1;
5632 return t;
5635 /* Select the right _DECL from multiple choices. */
5637 static tree
5638 select_decl (cxx_binding *binding, int flags)
5640 tree val;
5641 val = BINDING_VALUE (binding);
5643 timevar_push (TV_NAME_LOOKUP);
5644 if (LOOKUP_NAMESPACES_ONLY (flags))
5646 /* We are not interested in types. */
5647 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5648 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5649 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5652 /* If looking for a type, or if there is no non-type binding, select
5653 the value binding. */
5654 if (BINDING_TYPE (binding)
5655 && (!val || (flags & LOOKUP_PREFER_TYPES)))
5656 val = BINDING_TYPE (binding);
5657 /* Don't return non-types if we really prefer types. */
5658 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
5659 && (TREE_CODE (val) != TEMPLATE_DECL
5660 || !DECL_CLASS_TEMPLATE_P (val)))
5661 val = NULL_TREE;
5663 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5666 /* Unscoped lookup of a global: iterate over current namespaces,
5667 considering using-directives. If SPACESP is non-NULL, store a list
5668 of the namespaces we've considered in it. */
5670 tree
5671 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5673 tree initial = current_decl_namespace ();
5674 tree scope = initial;
5675 tree siter;
5676 struct cp_binding_level *level;
5677 tree val = NULL_TREE;
5678 cxx_binding binding;
5680 timevar_push (TV_NAME_LOOKUP);
5681 cxx_binding_clear (&binding);
5682 if (spacesp)
5683 *spacesp = NULL_TREE;
5685 for (; !val; scope = CP_DECL_CONTEXT (scope))
5687 cxx_binding *b =
5688 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
5689 if (spacesp)
5690 *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5692 /* Ignore anticipated built-in functions. */
5693 if (b && BINDING_VALUE (b)
5694 && DECL_P (BINDING_VALUE (b))
5695 && DECL_LANG_SPECIFIC (BINDING_VALUE (b))
5696 && DECL_ANTICIPATED (BINDING_VALUE (b)))
5697 /* Keep binding cleared. */;
5698 else if (b)
5700 /* Initialize binding for this context. */
5701 binding.value = BINDING_VALUE (b);
5702 binding.type = BINDING_TYPE (b);
5705 /* Add all _DECLs seen through local using-directives. */
5706 for (level = current_binding_level;
5707 !level->namespace_p;
5708 level = level->level_chain)
5709 if (!lookup_using_namespace (name, &binding, level->using_directives,
5710 scope, flags, spacesp))
5711 /* Give up because of error. */
5712 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5714 /* Add all _DECLs seen through global using-directives. */
5715 /* XXX local and global using lists should work equally. */
5716 siter = initial;
5717 while (1)
5719 if (!lookup_using_namespace (name, &binding,
5720 DECL_NAMESPACE_USING (siter),
5721 scope, flags, spacesp))
5722 /* Give up because of error. */
5723 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5724 if (siter == scope) break;
5725 siter = CP_DECL_CONTEXT (siter);
5728 val = select_decl (&binding, flags);
5729 if (scope == global_namespace)
5730 break;
5732 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5735 /* Combine prefer_type and namespaces_only into flags. */
5737 static int
5738 lookup_flags (int prefer_type, int namespaces_only)
5740 if (namespaces_only)
5741 return LOOKUP_PREFER_NAMESPACES;
5742 if (prefer_type > 1)
5743 return LOOKUP_PREFER_TYPES;
5744 if (prefer_type > 0)
5745 return LOOKUP_PREFER_BOTH;
5746 return 0;
5749 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5750 ignore it or not. Subroutine of lookup_name_real. */
5752 static tree
5753 qualify_lookup (tree val, int flags)
5755 if (val == NULL_TREE)
5756 return val;
5757 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5758 return val;
5759 if ((flags & LOOKUP_PREFER_TYPES)
5760 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
5761 return val;
5762 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5763 return NULL_TREE;
5764 return val;
5767 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5768 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
5769 bindings.
5771 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5772 declaration found. If no suitable declaration can be found,
5773 ERROR_MARK_NODE is returned. Iif COMPLAIN is true and SCOPE is
5774 neither a class-type nor a namespace a diagnostic is issued. */
5776 tree
5777 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
5779 int flags = 0;
5781 if (TREE_CODE (scope) == NAMESPACE_DECL)
5783 cxx_binding binding;
5785 cxx_binding_clear (&binding);
5786 flags |= LOOKUP_COMPLAIN;
5787 if (is_type_p)
5788 flags |= LOOKUP_PREFER_TYPES;
5789 if (qualified_lookup_using_namespace (name, scope, &binding,
5790 flags))
5791 return select_decl (&binding, flags);
5793 else if (is_aggr_type (scope, complain))
5795 tree t;
5796 t = lookup_member (scope, name, 0, is_type_p);
5797 if (t)
5798 return t;
5801 return error_mark_node;
5804 /* Check to see whether or not DECL is a variable that would have been
5805 in scope under the ARM, but is not in scope under the ANSI/ISO
5806 standard. If so, issue an error message. If name lookup would
5807 work in both cases, but return a different result, this function
5808 returns the result of ANSI/ISO lookup. Otherwise, it returns
5809 DECL. */
5811 tree
5812 check_for_out_of_scope_variable (tree decl)
5814 tree shadowed;
5816 /* We only care about out of scope variables. */
5817 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5818 return decl;
5820 shadowed = DECL_SHADOWED_FOR_VAR (decl);
5821 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5822 && DECL_DEAD_FOR_LOCAL (shadowed))
5823 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5824 if (!shadowed)
5825 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5826 if (shadowed)
5828 if (!DECL_ERROR_REPORTED (decl))
5830 warning ("name lookup of `%D' changed",
5831 DECL_NAME (decl));
5832 cp_warning_at (" matches this `%D' under ISO standard rules",
5833 shadowed);
5834 cp_warning_at (" matches this `%D' under old rules", decl);
5835 DECL_ERROR_REPORTED (decl) = 1;
5837 return shadowed;
5840 /* If we have already complained about this declaration, there's no
5841 need to do it again. */
5842 if (DECL_ERROR_REPORTED (decl))
5843 return decl;
5845 DECL_ERROR_REPORTED (decl) = 1;
5846 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5848 error ("name lookup of `%D' changed for new ISO `for' scoping",
5849 DECL_NAME (decl));
5850 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl);
5851 return error_mark_node;
5853 else
5855 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5856 DECL_NAME (decl));
5857 cp_pedwarn_at (" using obsolete binding at `%D'", decl);
5860 return decl;
5863 /* Look up NAME in the current binding level and its superiors in the
5864 namespace of variables, functions and typedefs. Return a ..._DECL
5865 node of some kind representing its definition if there is only one
5866 such declaration, or return a TREE_LIST with all the overloaded
5867 definitions if there are many, or return 0 if it is undefined.
5869 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5870 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5871 Otherwise we prefer non-TYPE_DECLs.
5873 If NONCLASS is nonzero, we don't look for the NAME in class scope,
5874 using IDENTIFIER_CLASS_VALUE. */
5876 tree
5877 lookup_name_real (tree name, int prefer_type, int nonclass,
5878 int namespaces_only, int flags)
5880 cxx_binding *iter;
5881 tree val = NULL_TREE;
5883 timevar_push (TV_NAME_LOOKUP);
5884 /* Conversion operators are handled specially because ordinary
5885 unqualified name lookup will not find template conversion
5886 operators. */
5887 if (IDENTIFIER_TYPENAME_P (name))
5889 struct cp_binding_level *level;
5891 for (level = current_binding_level;
5892 level && !level->namespace_p;
5893 level = level->level_chain)
5895 tree class_type;
5896 tree operators;
5898 /* A conversion operator can only be declared in a class
5899 scope. */
5900 if (level->parm_flag != 2)
5901 continue;
5903 /* Lookup the conversion operator in the class. */
5904 class_type = level->this_entity;
5905 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5906 if (operators)
5907 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
5910 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5913 flags |= lookup_flags (prefer_type, namespaces_only);
5915 /* First, look in non-namespace scopes. */
5917 if (current_class_type == NULL_TREE)
5918 nonclass = 1;
5920 for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
5922 tree binding;
5924 if (!LOCAL_BINDING_P (iter) && nonclass)
5925 /* We're not looking for class-scoped bindings, so keep going. */
5926 continue;
5928 /* If this is the kind of thing we're looking for, we're done. */
5929 if (qualify_lookup (BINDING_VALUE (iter), flags))
5930 binding = BINDING_VALUE (iter);
5931 else if ((flags & LOOKUP_PREFER_TYPES)
5932 && qualify_lookup (BINDING_TYPE (iter), flags))
5933 binding = BINDING_TYPE (iter);
5934 else
5935 binding = NULL_TREE;
5937 if (binding)
5939 val = binding;
5940 break;
5944 /* Now lookup in namespace scopes. */
5945 if (!val)
5947 tree t = unqualified_namespace_lookup (name, flags, 0);
5948 if (t)
5949 val = t;
5952 if (val)
5954 /* If we have a single function from a using decl, pull it out. */
5955 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5956 val = OVL_FUNCTION (val);
5959 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5962 tree
5963 lookup_name_nonclass (tree name)
5965 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
5968 tree
5969 lookup_function_nonclass (tree name, tree args)
5971 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5974 tree
5975 lookup_name (tree name, int prefer_type)
5977 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
5980 /* Similar to `lookup_name' but look only in the innermost non-class
5981 binding level. */
5983 tree
5984 lookup_name_current_level (tree name)
5986 struct cp_binding_level *b;
5987 tree t = NULL_TREE;
5989 timevar_push (TV_NAME_LOOKUP);
5990 b = current_binding_level;
5991 while (b->parm_flag == 2)
5992 b = b->level_chain;
5994 if (b->namespace_p)
5996 t = IDENTIFIER_NAMESPACE_VALUE (name);
5998 /* extern "C" function() */
5999 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
6000 t = TREE_VALUE (t);
6002 else if (IDENTIFIER_BINDING (name)
6003 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
6005 while (1)
6007 if (BINDING_SCOPE (IDENTIFIER_BINDING (name)) == b)
6008 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
6010 if (b->keep == 2)
6011 b = b->level_chain;
6012 else
6013 break;
6017 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6020 /* Like lookup_name_current_level, but for types. */
6022 tree
6023 lookup_type_current_level (tree name)
6025 register tree t = NULL_TREE;
6027 timevar_push (TV_NAME_LOOKUP);
6028 my_friendly_assert (! current_binding_level->namespace_p, 980716);
6030 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
6031 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
6033 struct cp_binding_level *b = current_binding_level;
6034 while (1)
6036 if (purpose_member (name, b->type_shadowed))
6037 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
6038 REAL_IDENTIFIER_TYPE_VALUE (name));
6039 if (b->keep == 2)
6040 b = b->level_chain;
6041 else
6042 break;
6046 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6050 /* Push the declarations of builtin types into the namespace.
6051 RID_INDEX is the index of the builtin type in the array
6052 RID_POINTERS. NAME is the name used when looking up the builtin
6053 type. TYPE is the _TYPE node for the builtin type. */
6055 void
6056 record_builtin_type (enum rid rid_index,
6057 const char* name,
6058 tree type)
6060 tree rname = NULL_TREE, tname = NULL_TREE;
6061 tree tdecl = NULL_TREE;
6063 if ((int) rid_index < (int) RID_MAX)
6064 rname = ridpointers[(int) rid_index];
6065 if (name)
6066 tname = get_identifier (name);
6068 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
6069 eliminated. Built-in types should not be looked up name; their
6070 names are keywords that the parser can recognize. However, there
6071 is code in c-common.c that uses identifier_global_value to look
6072 up built-in types by name. */
6073 if (tname)
6075 tdecl = build_decl (TYPE_DECL, tname, type);
6076 DECL_ARTIFICIAL (tdecl) = 1;
6077 if (tname)
6078 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6080 if (rname)
6082 if (!tdecl)
6084 tdecl = build_decl (TYPE_DECL, rname, type);
6085 DECL_ARTIFICIAL (tdecl) = 1;
6087 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6090 if (!TYPE_NAME (type))
6091 TYPE_NAME (type) = tdecl;
6094 /* Record one of the standard Java types.
6095 * Declare it as having the given NAME.
6096 * If SIZE > 0, it is the size of one of the integral types;
6097 * otherwise it is the negative of the size of one of the other types. */
6099 static tree
6100 record_builtin_java_type (const char* name, int size)
6102 tree type, decl;
6103 if (size > 0)
6104 type = make_signed_type (size);
6105 else if (size > -32)
6106 { /* "__java_char" or ""__java_boolean". */
6107 type = make_unsigned_type (-size);
6108 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6110 else
6111 { /* "__java_float" or ""__java_double". */
6112 type = make_node (REAL_TYPE);
6113 TYPE_PRECISION (type) = - size;
6114 layout_type (type);
6116 record_builtin_type (RID_MAX, name, type);
6117 decl = TYPE_NAME (type);
6119 /* Suppress generate debug symbol entries for these types,
6120 since for normal C++ they are just clutter.
6121 However, push_lang_context undoes this if extern "Java" is seen. */
6122 DECL_IGNORED_P (decl) = 1;
6124 TYPE_FOR_JAVA (type) = 1;
6125 return type;
6128 /* Push a type into the namespace so that the back-ends ignore it. */
6130 static void
6131 record_unknown_type (tree type, const char* name)
6133 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6134 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
6135 DECL_IGNORED_P (decl) = 1;
6136 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6137 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6138 TYPE_ALIGN (type) = 1;
6139 TYPE_USER_ALIGN (type) = 0;
6140 TYPE_MODE (type) = TYPE_MODE (void_type_node);
6143 /* An string for which we should create an IDENTIFIER_NODE at
6144 startup. */
6146 typedef struct predefined_identifier
6148 /* The name of the identifier. */
6149 const char *const name;
6150 /* The place where the IDENTIFIER_NODE should be stored. */
6151 tree *const node;
6152 /* Nonzero if this is the name of a constructor or destructor. */
6153 const int ctor_or_dtor_p;
6154 } predefined_identifier;
6156 /* Create all the predefined identifiers. */
6158 static void
6159 initialize_predefined_identifiers (void)
6161 const predefined_identifier *pid;
6163 /* A table of identifiers to create at startup. */
6164 static const predefined_identifier predefined_identifiers[] = {
6165 { "C++", &lang_name_cplusplus, 0 },
6166 { "C", &lang_name_c, 0 },
6167 { "Java", &lang_name_java, 0 },
6168 { CTOR_NAME, &ctor_identifier, 1 },
6169 { "__base_ctor", &base_ctor_identifier, 1 },
6170 { "__comp_ctor", &complete_ctor_identifier, 1 },
6171 { DTOR_NAME, &dtor_identifier, 1 },
6172 { "__comp_dtor", &complete_dtor_identifier, 1 },
6173 { "__base_dtor", &base_dtor_identifier, 1 },
6174 { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6175 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6176 { "nelts", &nelts_identifier, 0 },
6177 { THIS_NAME, &this_identifier, 0 },
6178 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6179 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6180 { "_vptr", &vptr_identifier, 0 },
6181 { "__vtt_parm", &vtt_parm_identifier, 0 },
6182 { "::", &global_scope_name, 0 },
6183 { "std", &std_identifier, 0 },
6184 { NULL, NULL, 0 }
6187 for (pid = predefined_identifiers; pid->name; ++pid)
6189 *pid->node = get_identifier (pid->name);
6190 if (pid->ctor_or_dtor_p)
6191 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6195 /* Create the predefined scalar types of C,
6196 and some nodes representing standard constants (0, 1, (void *)0).
6197 Initialize the global binding level.
6198 Make definitions for built-in primitive functions. */
6200 void
6201 cxx_init_decl_processing (void)
6203 tree void_ftype;
6204 tree void_ftype_ptr;
6206 /* Create all the identifiers we need. */
6207 initialize_predefined_identifiers ();
6209 /* Fill in back-end hooks. */
6210 lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6212 /* Create the global variables. */
6213 push_to_top_level ();
6215 current_function_decl = NULL_TREE;
6216 current_binding_level = NULL_BINDING_LEVEL;
6217 free_binding_level = NULL_BINDING_LEVEL;
6218 /* Enter the global namespace. */
6219 my_friendly_assert (global_namespace == NULL_TREE, 375);
6220 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
6221 void_type_node);
6222 initial_push_namespace_scope (global_namespace);
6224 current_lang_name = NULL_TREE;
6226 /* Adjust various flags based on command-line settings. */
6227 if (!flag_permissive)
6228 flag_pedantic_errors = 1;
6229 if (!flag_no_inline)
6231 flag_inline_trees = 1;
6232 flag_no_inline = 1;
6234 if (flag_inline_functions)
6236 flag_inline_trees = 2;
6237 flag_inline_functions = 0;
6240 /* Force minimum function alignment if using the least significant
6241 bit of function pointers to store the virtual bit. */
6242 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6243 && force_align_functions_log < 1)
6244 force_align_functions_log = 1;
6246 /* Initially, C. */
6247 current_lang_name = lang_name_c;
6249 build_common_tree_nodes (flag_signed_char);
6251 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6252 TREE_TYPE (error_mark_list) = error_mark_node;
6254 /* Create the `std' namespace. */
6255 push_namespace (std_identifier);
6256 std_node = current_namespace;
6257 pop_namespace ();
6259 c_common_nodes_and_builtins ();
6261 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6262 java_short_type_node = record_builtin_java_type ("__java_short", 16);
6263 java_int_type_node = record_builtin_java_type ("__java_int", 32);
6264 java_long_type_node = record_builtin_java_type ("__java_long", 64);
6265 java_float_type_node = record_builtin_java_type ("__java_float", -32);
6266 java_double_type_node = record_builtin_java_type ("__java_double", -64);
6267 java_char_type_node = record_builtin_java_type ("__java_char", -16);
6268 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6270 integer_two_node = build_int_2 (2, 0);
6271 TREE_TYPE (integer_two_node) = integer_type_node;
6272 integer_three_node = build_int_2 (3, 0);
6273 TREE_TYPE (integer_three_node) = integer_type_node;
6275 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6276 truthvalue_type_node = boolean_type_node;
6277 truthvalue_false_node = boolean_false_node;
6278 truthvalue_true_node = boolean_true_node;
6280 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6282 #if 0
6283 record_builtin_type (RID_MAX, NULL, string_type_node);
6284 #endif
6286 delta_type_node = ptrdiff_type_node;
6287 vtable_index_type = ptrdiff_type_node;
6289 vtt_parm_type = build_pointer_type (const_ptr_type_node);
6290 void_ftype = build_function_type (void_type_node, void_list_node);
6291 void_ftype_ptr = build_function_type (void_type_node,
6292 tree_cons (NULL_TREE,
6293 ptr_type_node,
6294 void_list_node));
6295 void_ftype_ptr
6296 = build_exception_variant (void_ftype_ptr, empty_except_spec);
6298 /* C++ extensions */
6300 unknown_type_node = make_node (UNKNOWN_TYPE);
6301 record_unknown_type (unknown_type_node, "unknown type");
6303 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6304 TREE_TYPE (unknown_type_node) = unknown_type_node;
6306 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6307 result. */
6308 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6309 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6312 /* Make sure we get a unique function type, so we can give
6313 its pointer type a name. (This wins for gdb.) */
6314 tree vfunc_type = make_node (FUNCTION_TYPE);
6315 TREE_TYPE (vfunc_type) = integer_type_node;
6316 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6317 layout_type (vfunc_type);
6319 vtable_entry_type = build_pointer_type (vfunc_type);
6321 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6323 vtbl_type_node
6324 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6325 layout_type (vtbl_type_node);
6326 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6327 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6328 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6329 layout_type (vtbl_ptr_type_node);
6330 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6332 push_namespace (get_identifier ("__cxxabiv1"));
6333 abi_node = current_namespace;
6334 pop_namespace ();
6336 global_type_node = make_node (LANG_TYPE);
6337 record_unknown_type (global_type_node, "global type");
6339 /* Now, C++. */
6340 current_lang_name = lang_name_cplusplus;
6343 tree bad_alloc_id;
6344 tree bad_alloc_type_node;
6345 tree bad_alloc_decl;
6346 tree newtype, deltype;
6347 tree ptr_ftype_sizetype;
6349 push_namespace (std_identifier);
6350 bad_alloc_id = get_identifier ("bad_alloc");
6351 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
6352 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
6353 bad_alloc_decl
6354 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
6355 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
6356 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
6357 pop_namespace ();
6359 ptr_ftype_sizetype
6360 = build_function_type (ptr_type_node,
6361 tree_cons (NULL_TREE,
6362 size_type_node,
6363 void_list_node));
6364 newtype = build_exception_variant
6365 (ptr_ftype_sizetype, add_exception_specifier
6366 (NULL_TREE, bad_alloc_type_node, -1));
6367 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6368 push_cp_library_fn (NEW_EXPR, newtype);
6369 push_cp_library_fn (VEC_NEW_EXPR, newtype);
6370 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6371 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6374 abort_fndecl
6375 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6377 /* Perform other language dependent initializations. */
6378 init_class_processing ();
6379 init_search_processing ();
6380 init_rtti_processing ();
6382 if (flag_exceptions)
6383 init_exception_processing ();
6385 if (! supports_one_only ())
6386 flag_weak = 0;
6388 make_fname_decl = cp_make_fname_decl;
6389 start_fname_decls ();
6391 /* Show we use EH for cleanups. */
6392 using_eh_for_cleanups ();
6394 /* Maintain consistency. Perhaps we should just complain if they
6395 say -fwritable-strings? */
6396 if (flag_writable_strings)
6397 flag_const_strings = 0;
6400 /* Generate an initializer for a function naming variable from
6401 NAME. NAME may be NULL, in which case we generate a special
6402 ERROR_MARK node which should be replaced later. */
6404 tree
6405 cp_fname_init (const char* name)
6407 tree domain = NULL_TREE;
6408 tree type;
6409 tree init = NULL_TREE;
6410 size_t length = 0;
6412 if (name)
6414 length = strlen (name);
6415 domain = build_index_type (size_int (length));
6416 init = build_string (length + 1, name);
6419 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6420 type = build_cplus_array_type (type, domain);
6422 if (init)
6423 TREE_TYPE (init) = type;
6424 else
6425 /* We don't know the value until instantiation time. Make
6426 something which will be digested now, but replaced later. */
6427 init = build (ERROR_MARK, type);
6429 return init;
6432 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6433 decl, NAME is the initialization string and TYPE_DEP indicates whether
6434 NAME depended on the type of the function. We make use of that to detect
6435 __PRETTY_FUNCTION__ inside a template fn. This is being done
6436 lazily at the point of first use, so we musn't push the decl now. */
6438 static tree
6439 cp_make_fname_decl (tree id, int type_dep)
6441 const char *const name = (type_dep && processing_template_decl
6442 ? NULL : fname_as_string (type_dep));
6443 tree init = cp_fname_init (name);
6444 tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6446 /* As we're using pushdecl_with_scope, we must set the context. */
6447 DECL_CONTEXT (decl) = current_function_decl;
6448 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6450 TREE_STATIC (decl) = 1;
6451 TREE_READONLY (decl) = 1;
6452 DECL_ARTIFICIAL (decl) = 1;
6453 DECL_INITIAL (decl) = init;
6455 TREE_USED (decl) = 1;
6457 if (current_function_decl)
6459 struct cp_binding_level *b = current_binding_level;
6460 while (b->level_chain->parm_flag == 0)
6461 b = b->level_chain;
6462 pushdecl_with_scope (decl, b);
6465 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6467 return decl;
6470 /* Make a definition for a builtin function named NAME in the current
6471 namespace, whose data type is TYPE and whose context is CONTEXT.
6472 TYPE should be a function type with argument types.
6474 CLASS and CODE tell later passes how to compile calls to this function.
6475 See tree.h for possible values.
6477 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6478 the name to be called if we can't opencode the function.
6479 If ATTRS is nonzero, use that for the function's attribute
6480 list. */
6482 static tree
6483 builtin_function_1 (const char* name,
6484 tree type,
6485 tree context,
6486 int code,
6487 enum built_in_class class,
6488 const char* libname,
6489 tree attrs)
6491 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6492 DECL_BUILT_IN_CLASS (decl) = class;
6493 DECL_FUNCTION_CODE (decl) = code;
6494 DECL_CONTEXT (decl) = context;
6496 pushdecl (decl);
6498 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6499 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6500 function in the namespace. */
6501 if (libname)
6502 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6503 make_decl_rtl (decl, NULL);
6505 /* Warn if a function in the namespace for users
6506 is used without an occasion to consider it declared. */
6507 if (name[0] != '_' || name[1] != '_')
6508 DECL_ANTICIPATED (decl) = 1;
6510 /* Possibly apply some default attributes to this built-in function. */
6511 if (attrs)
6512 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6513 else
6514 decl_attributes (&decl, NULL_TREE, 0);
6516 return decl;
6519 /* Entry point for the benefit of c_common_nodes_and_builtins.
6521 Make a definition for a builtin function named NAME and whose data type
6522 is TYPE. TYPE should be a function type with argument types. This
6523 function places the anticipated declaration in the global namespace
6524 and additionally in the std namespace if appropriate.
6526 CLASS and CODE tell later passes how to compile calls to this function.
6527 See tree.h for possible values.
6529 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6530 the name to be called if we can't opencode the function.
6532 If ATTRS is nonzero, use that for the function's attribute
6533 list. */
6535 tree
6536 builtin_function (const char* name,
6537 tree type,
6538 int code,
6539 enum built_in_class class,
6540 const char* libname,
6541 tree attrs)
6543 /* All builtins that don't begin with an '_' should additionally
6544 go in the 'std' namespace. */
6545 if (name[0] != '_')
6547 push_namespace (std_identifier);
6548 builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6549 pop_namespace ();
6552 return builtin_function_1 (name, type, NULL_TREE, code,
6553 class, libname, attrs);
6556 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6557 function. Not called directly. */
6559 static tree
6560 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6562 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6563 DECL_EXTERNAL (fn) = 1;
6564 TREE_PUBLIC (fn) = 1;
6565 DECL_ARTIFICIAL (fn) = 1;
6566 TREE_NOTHROW (fn) = 1;
6567 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6568 SET_DECL_LANGUAGE (fn, lang_c);
6569 return fn;
6572 /* Returns the _DECL for a library function with C linkage.
6573 We assume that such functions never throw; if this is incorrect,
6574 callers should unset TREE_NOTHROW. */
6576 tree
6577 build_library_fn (tree name, tree type)
6579 return build_library_fn_1 (name, ERROR_MARK, type);
6582 /* Returns the _DECL for a library function with C++ linkage. */
6584 static tree
6585 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6587 tree fn = build_library_fn_1 (name, operator_code, type);
6588 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6589 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6590 SET_DECL_LANGUAGE (fn, lang_cplusplus);
6591 set_mangled_name_for_decl (fn);
6592 return fn;
6595 /* Like build_library_fn, but takes a C string instead of an
6596 IDENTIFIER_NODE. */
6598 tree
6599 build_library_fn_ptr (const char* name, tree type)
6601 return build_library_fn (get_identifier (name), type);
6604 /* Like build_cp_library_fn, but takes a C string instead of an
6605 IDENTIFIER_NODE. */
6607 tree
6608 build_cp_library_fn_ptr (const char* name, tree type)
6610 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6613 /* Like build_library_fn, but also pushes the function so that we will
6614 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
6616 tree
6617 push_library_fn (tree name, tree type)
6619 tree fn = build_library_fn (name, type);
6620 pushdecl_top_level (fn);
6621 return fn;
6624 /* Like build_cp_library_fn, but also pushes the function so that it
6625 will be found by normal lookup. */
6627 static tree
6628 push_cp_library_fn (enum tree_code operator_code, tree type)
6630 tree fn = build_cp_library_fn (ansi_opname (operator_code),
6631 operator_code,
6632 type);
6633 pushdecl (fn);
6634 return fn;
6637 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6638 a FUNCTION_TYPE. */
6640 tree
6641 push_void_library_fn (tree name, tree parmtypes)
6643 tree type = build_function_type (void_type_node, parmtypes);
6644 return push_library_fn (name, type);
6647 /* Like push_library_fn, but also note that this function throws
6648 and does not return. Used for __throw_foo and the like. */
6650 tree
6651 push_throw_library_fn (tree name, tree type)
6653 tree fn = push_library_fn (name, type);
6654 TREE_THIS_VOLATILE (fn) = 1;
6655 TREE_NOTHROW (fn) = 0;
6656 return fn;
6659 /* When we call finish_struct for an anonymous union, we create
6660 default copy constructors and such. But, an anonymous union
6661 shouldn't have such things; this function undoes the damage to the
6662 anonymous union type T.
6664 (The reason that we create the synthesized methods is that we don't
6665 distinguish `union { int i; }' from `typedef union { int i; } U'.
6666 The first is an anonymous union; the second is just an ordinary
6667 union type.) */
6669 void
6670 fixup_anonymous_aggr (tree t)
6672 tree *q;
6674 /* Wipe out memory of synthesized methods */
6675 TYPE_HAS_CONSTRUCTOR (t) = 0;
6676 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6677 TYPE_HAS_INIT_REF (t) = 0;
6678 TYPE_HAS_CONST_INIT_REF (t) = 0;
6679 TYPE_HAS_ASSIGN_REF (t) = 0;
6680 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6682 /* Splice the implicitly generated functions out of the TYPE_METHODS
6683 list. */
6684 q = &TYPE_METHODS (t);
6685 while (*q)
6687 if (DECL_ARTIFICIAL (*q))
6688 *q = TREE_CHAIN (*q);
6689 else
6690 q = &TREE_CHAIN (*q);
6693 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
6694 if (TYPE_METHODS (t))
6695 error ("%Han anonymous union cannot have function members",
6696 &DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)));
6698 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6699 assignment operators (because they cannot have these methods themselves).
6700 For anonymous unions this is already checked because they are not allowed
6701 in any union, otherwise we have to check it. */
6702 if (TREE_CODE (t) != UNION_TYPE)
6704 tree field, type;
6706 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6707 if (TREE_CODE (field) == FIELD_DECL)
6709 type = TREE_TYPE (field);
6710 if (CLASS_TYPE_P (type))
6712 if (TYPE_NEEDS_CONSTRUCTING (type))
6713 cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6714 field);
6715 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6716 cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6717 field);
6718 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6719 cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6720 field);
6726 /* Make sure that a declaration with no declarator is well-formed, i.e.
6727 just declares a tagged type or anonymous union.
6729 Returns the type declared; or NULL_TREE if none. */
6731 tree
6732 check_tag_decl (tree declspecs)
6734 int found_type = 0;
6735 int saw_friend = 0;
6736 int saw_typedef = 0;
6737 tree ob_modifier = NULL_TREE;
6738 register tree link;
6739 /* If a class, struct, or enum type is declared by the DECLSPECS
6740 (i.e, if a class-specifier, enum-specifier, or non-typename
6741 elaborated-type-specifier appears in the DECLSPECS),
6742 DECLARED_TYPE is set to the corresponding type. */
6743 tree declared_type = NULL_TREE;
6744 bool error_p = false;
6746 for (link = declspecs; link; link = TREE_CHAIN (link))
6748 tree value = TREE_VALUE (link);
6750 if (TYPE_P (value)
6751 || TREE_CODE (value) == TYPE_DECL
6752 || (TREE_CODE (value) == IDENTIFIER_NODE
6753 && IDENTIFIER_GLOBAL_VALUE (value)
6754 && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6756 ++found_type;
6758 if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6760 if (! in_system_header)
6761 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6762 return NULL_TREE;
6765 if (TYPE_P (value)
6766 && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6767 || TREE_CODE (value) == ENUMERAL_TYPE))
6769 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6770 declared_type = value;
6773 else if (value == ridpointers[(int) RID_TYPEDEF])
6774 saw_typedef = 1;
6775 else if (value == ridpointers[(int) RID_FRIEND])
6777 if (current_class_type == NULL_TREE
6778 || current_scope () != current_class_type)
6779 ob_modifier = value;
6780 else
6781 saw_friend = 1;
6783 else if (value == ridpointers[(int) RID_STATIC]
6784 || value == ridpointers[(int) RID_EXTERN]
6785 || value == ridpointers[(int) RID_AUTO]
6786 || value == ridpointers[(int) RID_REGISTER]
6787 || value == ridpointers[(int) RID_INLINE]
6788 || value == ridpointers[(int) RID_VIRTUAL]
6789 || value == ridpointers[(int) RID_CONST]
6790 || value == ridpointers[(int) RID_VOLATILE]
6791 || value == ridpointers[(int) RID_EXPLICIT]
6792 || value == ridpointers[(int) RID_THREAD])
6793 ob_modifier = value;
6794 else if (value == error_mark_node)
6795 error_p = true;
6798 if (found_type > 1)
6799 error ("multiple types in one declaration");
6801 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6802 pedwarn ("declaration does not declare anything");
6803 /* Check for an anonymous union. */
6804 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6805 && TYPE_ANONYMOUS_P (declared_type))
6807 /* 7/3 In a simple-declaration, the optional init-declarator-list
6808 can be omitted only when declaring a class (clause 9) or
6809 enumeration (7.2), that is, when the decl-specifier-seq contains
6810 either a class-specifier, an elaborated-type-specifier with
6811 a class-key (9.1), or an enum-specifier. In these cases and
6812 whenever a class-specifier or enum-specifier is present in the
6813 decl-specifier-seq, the identifiers in these specifiers are among
6814 the names being declared by the declaration (as class-name,
6815 enum-names, or enumerators, depending on the syntax). In such
6816 cases, and except for the declaration of an unnamed bit-field (9.6),
6817 the decl-specifier-seq shall introduce one or more names into the
6818 program, or shall redeclare a name introduced by a previous
6819 declaration. [Example:
6820 enum { }; // ill-formed
6821 typedef class { }; // ill-formed
6822 --end example] */
6823 if (saw_typedef)
6825 error ("missing type-name in typedef-declaration");
6826 return NULL_TREE;
6828 /* Anonymous unions are objects, so they can have specifiers. */;
6829 SET_ANON_AGGR_TYPE_P (declared_type);
6831 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
6832 && !in_system_header)
6833 pedwarn ("ISO C++ prohibits anonymous structs");
6836 else if (ob_modifier)
6838 if (ob_modifier == ridpointers[(int) RID_INLINE]
6839 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6840 error ("`%D' can only be specified for functions", ob_modifier);
6841 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6842 error ("`%D' can only be specified inside a class", ob_modifier);
6843 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6844 error ("`%D' can only be specified for constructors",
6845 ob_modifier);
6846 else
6847 error ("`%D' can only be specified for objects and functions",
6848 ob_modifier);
6851 return declared_type;
6854 /* Called when a declaration is seen that contains no names to declare.
6855 If its type is a reference to a structure, union or enum inherited
6856 from a containing scope, shadow that tag name for the current scope
6857 with a forward reference.
6858 If its type defines a new named structure or union
6859 or defines an enum, it is valid but we need not do anything here.
6860 Otherwise, it is an error.
6862 C++: may have to grok the declspecs to learn about static,
6863 complain for anonymous unions.
6865 Returns the TYPE declared -- or NULL_TREE if none. */
6867 tree
6868 shadow_tag (tree declspecs)
6870 tree t = check_tag_decl (declspecs);
6872 if (!t)
6873 return NULL_TREE;
6875 maybe_process_partial_specialization (t);
6877 /* This is where the variables in an anonymous union are
6878 declared. An anonymous union declaration looks like:
6879 union { ... } ;
6880 because there is no declarator after the union, the parser
6881 sends that declaration here. */
6882 if (ANON_AGGR_TYPE_P (t))
6884 fixup_anonymous_aggr (t);
6886 if (TYPE_FIELDS (t))
6888 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6889 NULL);
6890 finish_anon_union (decl);
6894 return t;
6897 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6899 tree
6900 groktypename (tree typename)
6902 tree specs, attrs;
6903 tree type;
6904 if (TREE_CODE (typename) != TREE_LIST)
6905 return typename;
6906 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6907 type = grokdeclarator (TREE_VALUE (typename), specs,
6908 TYPENAME, 0, &attrs);
6909 if (attrs)
6910 cplus_decl_attributes (&type, attrs, 0);
6911 return type;
6914 /* Decode a declarator in an ordinary declaration or data definition.
6915 This is called as soon as the type information and variable name
6916 have been parsed, before parsing the initializer if any.
6917 Here we create the ..._DECL node, fill in its type,
6918 and put it on the list of decls for the current context.
6919 The ..._DECL node is returned as the value.
6921 Exception: for arrays where the length is not specified,
6922 the type is left null, to be filled in by `cp_finish_decl'.
6924 Function definitions do not come here; they go to start_function
6925 instead. However, external and forward declarations of functions
6926 do go through here. Structure field declarations are done by
6927 grokfield and not through here. */
6929 tree
6930 start_decl (tree declarator,
6931 tree declspecs,
6932 int initialized,
6933 tree attributes,
6934 tree prefix_attributes)
6936 tree decl;
6937 register tree type, tem;
6938 tree context;
6940 /* This should only be done once on the top most decl. */
6941 if (have_extern_spec)
6943 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
6944 declspecs);
6945 have_extern_spec = false;
6948 /* An object declared as __attribute__((deprecated)) suppresses
6949 warnings of uses of other deprecated items. */
6950 if (lookup_attribute ("deprecated", attributes))
6951 deprecated_state = DEPRECATED_SUPPRESS;
6953 attributes = chainon (attributes, prefix_attributes);
6955 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6956 &attributes);
6958 deprecated_state = DEPRECATED_NORMAL;
6960 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
6961 return NULL_TREE;
6963 type = TREE_TYPE (decl);
6965 if (type == error_mark_node)
6966 return NULL_TREE;
6968 context = DECL_CONTEXT (decl);
6970 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6971 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6973 /* When parsing the initializer, lookup should use the object's
6974 namespace. */
6975 push_decl_namespace (context);
6978 /* We are only interested in class contexts, later. */
6979 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6980 context = NULL_TREE;
6982 if (initialized)
6983 /* Is it valid for this decl to have an initializer at all?
6984 If not, set INITIALIZED to zero, which will indirectly
6985 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
6986 switch (TREE_CODE (decl))
6988 case TYPE_DECL:
6989 error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
6990 initialized = 0;
6991 break;
6993 case FUNCTION_DECL:
6994 error ("function `%#D' is initialized like a variable", decl);
6995 initialized = 0;
6996 break;
6998 default:
6999 break;
7002 if (initialized)
7004 if (! toplevel_bindings_p ()
7005 && DECL_EXTERNAL (decl))
7006 warning ("declaration of `%#D' has `extern' and is initialized",
7007 decl);
7008 DECL_EXTERNAL (decl) = 0;
7009 if (toplevel_bindings_p ())
7010 TREE_STATIC (decl) = 1;
7012 /* Tell `pushdecl' this is an initialized decl
7013 even though we don't yet have the initializer expression.
7014 Also tell `cp_finish_decl' it may store the real initializer. */
7015 DECL_INITIAL (decl) = error_mark_node;
7018 /* Set attributes here so if duplicate decl, will have proper attributes. */
7019 cplus_decl_attributes (&decl, attributes, 0);
7021 /* If #pragma weak was used, mark the decl weak now. */
7022 if (global_scope_p (current_binding_level))
7023 maybe_apply_pragma_weak (decl);
7025 if (TREE_CODE (decl) == FUNCTION_DECL
7026 && DECL_DECLARED_INLINE_P (decl)
7027 && DECL_UNINLINABLE (decl)
7028 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
7029 warning ("%Hinline function '%D' given attribute noinline",
7030 &DECL_SOURCE_LOCATION (decl), decl);
7032 if (context && COMPLETE_TYPE_P (complete_type (context)))
7034 push_nested_class (context);
7036 if (TREE_CODE (decl) == VAR_DECL)
7038 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
7039 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7040 error ("`%#D' is not a static member of `%#T'", decl, context);
7041 else
7043 if (DECL_CONTEXT (field) != context)
7045 if (!same_type_p (DECL_CONTEXT (field), context))
7046 pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7047 DECL_CONTEXT (field), DECL_NAME (decl),
7048 context, DECL_NAME (decl));
7049 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7051 /* Static data member are tricky; an in-class initialization
7052 still doesn't provide a definition, so the in-class
7053 declaration will have DECL_EXTERNAL set, but will have an
7054 initialization. Thus, duplicate_decls won't warn
7055 about this situation, and so we check here. */
7056 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7057 error ("duplicate initialization of %D", decl);
7058 if (duplicate_decls (decl, field))
7059 decl = field;
7062 else
7064 tree field = check_classfn (context, decl);
7065 if (field && duplicate_decls (decl, field))
7066 decl = field;
7069 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
7070 DECL_IN_AGGR_P (decl) = 0;
7071 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7072 || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7074 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7075 /* [temp.expl.spec] An explicit specialization of a static data
7076 member of a template is a definition if the declaration
7077 includes an initializer; otherwise, it is a declaration.
7079 We check for processing_specialization so this only applies
7080 to the new specialization syntax. */
7081 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7082 DECL_EXTERNAL (decl) = 1;
7085 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7086 pedwarn ("declaration of `%#D' outside of class is not definition",
7087 decl);
7090 /* Enter this declaration into the symbol table. */
7091 tem = maybe_push_decl (decl);
7093 if (processing_template_decl)
7094 tem = push_template_decl (tem);
7096 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7097 /* Tell the back-end to use or not use .common as appropriate. If we say
7098 -fconserve-space, we want this to save .data space, at the expense of
7099 wrong semantics. If we say -fno-conserve-space, we want this to
7100 produce errors about redefs; to do this we force variables into the
7101 data segment. */
7102 DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7103 || !DECL_THREAD_LOCAL (tem))
7104 && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7105 #endif
7107 if (! processing_template_decl)
7108 start_decl_1 (tem);
7110 return tem;
7113 void
7114 start_decl_1 (tree decl)
7116 tree type = TREE_TYPE (decl);
7117 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7119 if (type == error_mark_node)
7120 return;
7122 maybe_push_cleanup_level (type);
7124 if (initialized)
7125 /* Is it valid for this decl to have an initializer at all?
7126 If not, set INITIALIZED to zero, which will indirectly
7127 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
7129 /* Don't allow initializations for incomplete types except for
7130 arrays which might be completed by the initialization. */
7131 if (COMPLETE_TYPE_P (complete_type (type)))
7132 ; /* A complete type is ok. */
7133 else if (TREE_CODE (type) != ARRAY_TYPE)
7135 error ("variable `%#D' has initializer but incomplete type",
7136 decl);
7137 initialized = 0;
7138 type = TREE_TYPE (decl) = error_mark_node;
7140 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7142 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7143 error ("elements of array `%#D' have incomplete type", decl);
7144 /* else we already gave an error in start_decl. */
7145 initialized = 0;
7149 if (!initialized
7150 && TREE_CODE (decl) != TYPE_DECL
7151 && TREE_CODE (decl) != TEMPLATE_DECL
7152 && type != error_mark_node
7153 && IS_AGGR_TYPE (type)
7154 && ! DECL_EXTERNAL (decl))
7156 if ((! processing_template_decl || ! uses_template_parms (type))
7157 && !COMPLETE_TYPE_P (complete_type (type)))
7159 error ("aggregate `%#D' has incomplete type and cannot be defined",
7160 decl);
7161 /* Change the type so that assemble_variable will give
7162 DECL an rtl we can live with: (mem (const_int 0)). */
7163 type = TREE_TYPE (decl) = error_mark_node;
7165 else
7167 /* If any base type in the hierarchy of TYPE needs a constructor,
7168 then we set initialized to 1. This way any nodes which are
7169 created for the purposes of initializing this aggregate
7170 will live as long as it does. This is necessary for global
7171 aggregates which do not have their initializers processed until
7172 the end of the file. */
7173 initialized = TYPE_NEEDS_CONSTRUCTING (type);
7177 if (! initialized)
7178 DECL_INITIAL (decl) = NULL_TREE;
7181 /* Handle initialization of references. DECL, TYPE, and INIT have the
7182 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
7183 but will be set to a new CLEANUP_STMT if a temporary is created
7184 that must be destroeyd subsequently.
7186 Returns an initializer expression to use to initialize DECL, or
7187 NULL if the initialization can be performed statically.
7189 Quotes on semantics can be found in ARM 8.4.3. */
7191 static tree
7192 grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
7194 tree tmp;
7196 if (init == NULL_TREE)
7198 if ((DECL_LANG_SPECIFIC (decl) == 0
7199 || DECL_IN_AGGR_P (decl) == 0)
7200 && ! DECL_THIS_EXTERN (decl))
7201 error ("`%D' declared as reference but not initialized", decl);
7202 return NULL_TREE;
7205 if (TREE_CODE (init) == CONSTRUCTOR)
7207 error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7208 return NULL_TREE;
7211 if (TREE_CODE (init) == TREE_LIST)
7212 init = build_x_compound_expr_from_list (init, "initializer");
7214 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7215 init = convert_from_reference (init);
7217 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7218 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7219 /* Note: default conversion is only called in very special cases. */
7220 init = decay_conversion (init);
7222 /* Convert INIT to the reference type TYPE. This may involve the
7223 creation of a temporary, whose lifetime must be the same as that
7224 of the reference. If so, a DECL_STMT for the temporary will be
7225 added just after the DECL_STMT for DECL. That's why we don't set
7226 DECL_INITIAL for local references (instead assigning to them
7227 explicitly); we need to allow the temporary to be initialized
7228 first. */
7229 tmp = initialize_reference (type, init, decl, cleanup);
7231 if (tmp == error_mark_node)
7232 return NULL_TREE;
7233 else if (tmp == NULL_TREE)
7235 error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7236 return NULL_TREE;
7239 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7240 return tmp;
7242 DECL_INITIAL (decl) = tmp;
7244 return NULL_TREE;
7247 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7248 array until we finish parsing the initializer. If that's the
7249 situation we're in, update DECL accordingly. */
7251 static void
7252 maybe_deduce_size_from_array_init (tree decl, tree init)
7254 tree type = TREE_TYPE (decl);
7256 if (TREE_CODE (type) == ARRAY_TYPE
7257 && TYPE_DOMAIN (type) == NULL_TREE
7258 && TREE_CODE (decl) != TYPE_DECL)
7260 /* do_default is really a C-ism to deal with tentative definitions.
7261 But let's leave it here to ease the eventual merge. */
7262 int do_default = !DECL_EXTERNAL (decl);
7263 tree initializer = init ? init : DECL_INITIAL (decl);
7264 int failure = complete_array_type (type, initializer, do_default);
7266 if (failure == 1)
7267 error ("initializer fails to determine size of `%D'", decl);
7269 if (failure == 2)
7271 if (do_default)
7272 error ("array size missing in `%D'", decl);
7273 /* If a `static' var's size isn't known, make it extern as
7274 well as static, so it does not get allocated. If it's not
7275 `static', then don't mark it extern; finish_incomplete_decl
7276 will give it a default size and it will get allocated. */
7277 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7278 DECL_EXTERNAL (decl) = 1;
7281 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7282 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7283 integer_zero_node))
7284 error ("zero-size array `%D'", decl);
7286 layout_decl (decl, 0);
7290 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7291 any appropriate error messages regarding the layout. */
7293 static void
7294 layout_var_decl (tree decl)
7296 tree type = TREE_TYPE (decl);
7297 #if 0
7298 tree ttype = target_type (type);
7299 #endif
7301 /* If we haven't already layed out this declaration, do so now.
7302 Note that we must not call complete type for an external object
7303 because it's type might involve templates that we are not
7304 supposed to instantiate yet. (And it's perfectly valid to say
7305 `extern X x' for some incomplete type `X'.) */
7306 if (!DECL_EXTERNAL (decl))
7307 complete_type (type);
7308 if (!DECL_SIZE (decl)
7309 && TREE_TYPE (decl) != error_mark_node
7310 && (COMPLETE_TYPE_P (type)
7311 || (TREE_CODE (type) == ARRAY_TYPE
7312 && !TYPE_DOMAIN (type)
7313 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7314 layout_decl (decl, 0);
7316 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7318 /* An automatic variable with an incomplete type: that is an error.
7319 Don't talk about array types here, since we took care of that
7320 message in grokdeclarator. */
7321 error ("storage size of `%D' isn't known", decl);
7322 TREE_TYPE (decl) = error_mark_node;
7324 #if 0
7325 /* Keep this code around in case we later want to control debug info
7326 based on whether a type is "used". (jason 1999-11-11) */
7328 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7329 /* Let debugger know it should output info for this type. */
7330 note_debug_info_needed (ttype);
7332 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7333 note_debug_info_needed (DECL_CONTEXT (decl));
7334 #endif
7336 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7337 && DECL_SIZE (decl) != NULL_TREE
7338 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7340 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7341 constant_expression_warning (DECL_SIZE (decl));
7342 else
7343 error ("storage size of `%D' isn't constant", decl);
7346 if (TREE_STATIC (decl)
7347 && !DECL_ARTIFICIAL (decl)
7348 && current_function_decl
7349 && DECL_CONTEXT (decl) == current_function_decl)
7350 push_local_name (decl);
7353 /* If a local static variable is declared in an inline function, or if
7354 we have a weak definition, we must endeavor to create only one
7355 instance of the variable at link-time. */
7357 static void
7358 maybe_commonize_var (tree decl)
7360 /* Static data in a function with comdat linkage also has comdat
7361 linkage. */
7362 if (TREE_STATIC (decl)
7363 /* Don't mess with __FUNCTION__. */
7364 && ! DECL_ARTIFICIAL (decl)
7365 && DECL_FUNCTION_SCOPE_P (decl)
7366 /* Unfortunately, import_export_decl has not always been called
7367 before the function is processed, so we cannot simply check
7368 DECL_COMDAT. */
7369 && (DECL_COMDAT (DECL_CONTEXT (decl))
7370 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
7371 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
7372 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
7374 if (flag_weak)
7376 /* With weak symbols, we simply make the variable COMDAT;
7377 that will cause copies in multiple translations units to
7378 be merged. */
7379 comdat_linkage (decl);
7381 else
7383 if (DECL_INITIAL (decl) == NULL_TREE
7384 || DECL_INITIAL (decl) == error_mark_node)
7386 /* Without weak symbols, we can use COMMON to merge
7387 uninitialized variables. */
7388 TREE_PUBLIC (decl) = 1;
7389 DECL_COMMON (decl) = 1;
7391 else
7393 /* While for initialized variables, we must use internal
7394 linkage -- which means that multiple copies will not
7395 be merged. */
7396 TREE_PUBLIC (decl) = 0;
7397 DECL_COMMON (decl) = 0;
7398 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7399 warning ("%H you can work around this by removing the initializer",
7400 &DECL_SOURCE_LOCATION (decl));
7404 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7405 /* Set it up again; we might have set DECL_INITIAL since the last
7406 time. */
7407 comdat_linkage (decl);
7410 /* Issue an error message if DECL is an uninitialized const variable. */
7412 static void
7413 check_for_uninitialized_const_var (tree decl)
7415 tree type = TREE_TYPE (decl);
7417 /* ``Unless explicitly declared extern, a const object does not have
7418 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7419 7.1.6 */
7420 if (TREE_CODE (decl) == VAR_DECL
7421 && TREE_CODE (type) != REFERENCE_TYPE
7422 && CP_TYPE_CONST_P (type)
7423 && !TYPE_NEEDS_CONSTRUCTING (type)
7424 && !DECL_INITIAL (decl))
7425 error ("uninitialized const `%D'", decl);
7428 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
7429 returned is the next FIELD_DECL (possibly FIELD itself) that can be
7430 initialized. If there are no more such fields, the return value
7431 will be NULL. */
7433 static tree
7434 next_initializable_field (tree field)
7436 while (field
7437 && (TREE_CODE (field) != FIELD_DECL
7438 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7439 || DECL_ARTIFICIAL (field)))
7440 field = TREE_CHAIN (field);
7442 return field;
7445 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7446 brace-enclosed aggregate initializer.
7448 *INITP is one of a list of initializers describing a brace-enclosed
7449 initializer for an entity of the indicated aggregate TYPE. It may
7450 not presently match the shape of the TYPE; for example:
7452 struct S { int a; int b; };
7453 struct S a[] = { 1, 2, 3, 4 };
7455 Here *INITP will point to TREE_LIST of four elements, rather than a
7456 list of two elements, each itself a list of two elements. This
7457 routine transforms INIT from the former form into the latter. The
7458 revised initializer is returned. */
7460 static tree
7461 reshape_init (tree type, tree *initp)
7463 tree inits;
7464 tree old_init;
7465 tree old_init_value;
7466 tree new_init;
7467 bool brace_enclosed_p;
7469 old_init = *initp;
7470 old_init_value = (TREE_CODE (*initp) == TREE_LIST
7471 ? TREE_VALUE (*initp) : old_init);
7473 my_friendly_assert (old_init_value, 20030723);
7475 /* If the initializer is brace-enclosed, pull initializers from the
7476 enclosed elements. Advance past the brace-enclosed initializer
7477 now. */
7478 if (TREE_CODE (old_init_value) == CONSTRUCTOR
7479 && TREE_TYPE (old_init_value) == NULL_TREE
7480 && TREE_HAS_CONSTRUCTOR (old_init_value))
7482 *initp = TREE_CHAIN (old_init);
7483 TREE_CHAIN (old_init) = NULL_TREE;
7484 inits = CONSTRUCTOR_ELTS (old_init_value);
7485 initp = &inits;
7486 brace_enclosed_p = true;
7488 else
7490 inits = NULL_TREE;
7491 brace_enclosed_p = false;
7494 /* A non-aggregate type is always initialized with a single
7495 initializer. */
7496 if (!CP_AGGREGATE_TYPE_P (type))
7498 *initp = TREE_CHAIN (old_init);
7499 TREE_CHAIN (old_init) = NULL_TREE;
7500 /* It is invalid to initialize a non-aggregate type with a
7501 brace-enclosed initializer. */
7502 if (brace_enclosed_p)
7504 error ("brace-enclosed initializer used to initialize `%T'",
7505 type);
7506 if (TREE_CODE (old_init) == TREE_LIST)
7507 TREE_VALUE (old_init) = error_mark_node;
7508 else
7509 old_init = error_mark_node;
7512 return old_init;
7515 /* [dcl.init.aggr]
7517 All implicit type conversions (clause _conv_) are considered when
7518 initializing the aggregate member with an initializer from an
7519 initializer-list. If the initializer can initialize a member,
7520 the member is initialized. Otherwise, if the member is itself a
7521 non-empty subaggregate, brace elision is assumed and the
7522 initializer is considered for the initialization of the first
7523 member of the subaggregate. */
7524 if (!brace_enclosed_p
7525 && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7527 *initp = TREE_CHAIN (old_init);
7528 TREE_CHAIN (old_init) = NULL_TREE;
7529 return old_init;
7532 if (TREE_CODE (old_init_value) == STRING_CST
7533 && TREE_CODE (type) == ARRAY_TYPE
7534 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7536 /* [dcl.init.string]
7538 A char array (whether plain char, signed char, or unsigned char)
7539 can be initialized by a string-literal (optionally enclosed in
7540 braces); a wchar_t array can be initialized by a wide
7541 string-literal (optionally enclosed in braces). */
7542 new_init = old_init;
7543 /* Move past the initializer. */
7544 *initp = TREE_CHAIN (old_init);
7545 TREE_CHAIN (old_init) = NULL_TREE;
7547 else
7549 /* Build a CONSTRUCTOR to hold the contents of the aggregate. */
7550 new_init = build_constructor (type, NULL_TREE);
7551 TREE_HAS_CONSTRUCTOR (new_init) = 1;
7553 if (CLASS_TYPE_P (type))
7555 tree field;
7557 field = next_initializable_field (TYPE_FIELDS (type));
7559 if (!field)
7561 /* [dcl.init.aggr]
7563 An initializer for an aggregate member that is an
7564 empty class shall have the form of an empty
7565 initializer-list {}. */
7566 if (!brace_enclosed_p)
7567 error ("initializer for `%T' must be brace-enclosed",
7568 type);
7570 else
7572 /* Loop through the initializable fields, gathering
7573 initializers. */
7574 while (*initp)
7576 tree field_init;
7578 /* Handle designated initializers, as an extension. */
7579 if (TREE_PURPOSE (*initp))
7581 if (pedantic)
7582 pedwarn ("ISO C++ does not allow designated initializers");
7583 field = lookup_field_1 (type, TREE_PURPOSE (*initp),
7584 /*want_type=*/false);
7585 if (!field || TREE_CODE (field) != FIELD_DECL)
7586 error ("`%T' has no non-static data member named `%D'",
7587 type, TREE_PURPOSE (*initp));
7589 if (!field)
7590 break;
7592 field_init = reshape_init (TREE_TYPE (field), initp);
7593 TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7594 CONSTRUCTOR_ELTS (new_init) = field_init;
7595 /* [dcl.init.aggr]
7597 When a union is initialized with a brace-enclosed
7598 initializer, the braces shall only contain an
7599 initializer for the first member of the union. */
7600 if (TREE_CODE (type) == UNION_TYPE)
7601 break;
7602 field = next_initializable_field (TREE_CHAIN (field));
7606 else if (TREE_CODE (type) == ARRAY_TYPE)
7608 tree index;
7609 tree max_index;
7611 /* If the bound of the array is known, take no more initializers
7612 than are allowed. */
7613 max_index = (TYPE_DOMAIN (type)
7614 ? array_type_nelts (type) : NULL_TREE);
7615 /* Loop through the array elements, gathering initializers. */
7616 for (index = size_zero_node;
7617 *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7618 index = size_binop (PLUS_EXPR, index, size_one_node))
7620 tree element_init;
7622 element_init = reshape_init (TREE_TYPE (type), initp);
7623 TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7624 CONSTRUCTOR_ELTS (new_init) = element_init;
7625 if (TREE_PURPOSE (element_init))
7626 index = TREE_PURPOSE (element_init);
7629 else
7630 abort ();
7632 /* The initializers were placed in reverse order in the
7633 CONSTRUCTOR. */
7634 CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7636 if (TREE_CODE (old_init) == TREE_LIST)
7637 new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7640 /* If this was a brace-enclosed initializer and all of the
7641 initializers were not used up, there is a problem. */
7642 if (brace_enclosed_p && *initp)
7643 error ("too many initializers for `%T'", type);
7645 return new_init;
7648 /* Verify INIT (the initializer for DECL), and record the
7649 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7650 grok_reference_init.
7652 If the return value is non-NULL, it is an expression that must be
7653 evaluated dynamically to initialize DECL. */
7655 static tree
7656 check_initializer (tree decl, tree init, int flags, tree *cleanup)
7658 tree type = TREE_TYPE (decl);
7660 /* If `start_decl' didn't like having an initialization, ignore it now. */
7661 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7662 init = NULL_TREE;
7664 /* If an initializer is present, DECL_INITIAL has been
7665 error_mark_node, to indicate that an as-of-yet unevaluated
7666 initialization will occur. From now on, DECL_INITIAL reflects
7667 the static initialization -- if any -- of DECL. */
7668 DECL_INITIAL (decl) = NULL_TREE;
7670 /* Things that are going to be initialized need to have complete
7671 type. */
7672 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7674 if (type == error_mark_node)
7675 /* We will have already complained. */
7676 init = NULL_TREE;
7677 else if (init && COMPLETE_TYPE_P (type)
7678 && !TREE_CONSTANT (TYPE_SIZE (type)))
7680 error ("variable-sized object `%D' may not be initialized", decl);
7681 init = NULL_TREE;
7683 else if (TREE_CODE (type) == ARRAY_TYPE
7684 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7686 error ("elements of array `%#D' have incomplete type", decl);
7687 init = NULL_TREE;
7689 else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7691 error ("`%D' has incomplete type", decl);
7692 TREE_TYPE (decl) = error_mark_node;
7693 init = NULL_TREE;
7696 if (TREE_CODE (decl) == CONST_DECL)
7698 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7700 DECL_INITIAL (decl) = init;
7702 my_friendly_assert (init != NULL_TREE, 149);
7703 init = NULL_TREE;
7705 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7706 init = grok_reference_init (decl, type, init, cleanup);
7707 else if (init)
7709 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7711 /* [dcl.init] paragraph 13,
7712 If T is a scalar type, then a declaration of the form
7713 T x = { a };
7714 is equivalent to
7715 T x = a;
7717 reshape_init will complain about the extra braces,
7718 and doesn't do anything useful in the case where TYPE is
7719 scalar, so just don't call it. */
7720 if (CP_AGGREGATE_TYPE_P (type))
7721 init = reshape_init (type, &init);
7723 if ((*targetm.vector_opaque_p) (type))
7725 error ("opaque vector types cannot be initialized");
7726 init = error_mark_node;
7730 /* If DECL has an array type without a specific bound, deduce the
7731 array size from the initializer. */
7732 maybe_deduce_size_from_array_init (decl, init);
7733 type = TREE_TYPE (decl);
7734 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7735 TREE_TYPE (init) = type;
7737 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7739 if (TREE_CODE (type) == ARRAY_TYPE)
7740 goto initialize_aggr;
7741 else if (TREE_CODE (init) == CONSTRUCTOR
7742 && TREE_HAS_CONSTRUCTOR (init))
7744 if (TYPE_NON_AGGREGATE_CLASS (type))
7746 error ("`%D' must be initialized by constructor, not by `{...}'",
7747 decl);
7748 init = error_mark_node;
7750 else
7751 goto dont_use_constructor;
7753 else
7755 int saved_stmts_are_full_exprs_p;
7757 initialize_aggr:
7758 saved_stmts_are_full_exprs_p = 0;
7759 if (building_stmt_tree ())
7761 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7762 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7764 init = build_aggr_init (decl, init, flags);
7765 if (building_stmt_tree ())
7766 current_stmt_tree ()->stmts_are_full_exprs_p =
7767 saved_stmts_are_full_exprs_p;
7768 return init;
7771 else
7773 dont_use_constructor:
7774 if (TREE_CODE (init) != TREE_VEC)
7775 init = store_init_value (decl, init);
7778 else if (DECL_EXTERNAL (decl))
7780 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7781 goto initialize_aggr;
7782 else if (IS_AGGR_TYPE (type))
7784 tree core_type = strip_array_types (type);
7786 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7787 error ("structure `%D' with uninitialized const members", decl);
7788 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7789 error ("structure `%D' with uninitialized reference members",
7790 decl);
7792 check_for_uninitialized_const_var (decl);
7794 else
7795 check_for_uninitialized_const_var (decl);
7797 if (init && init != error_mark_node)
7798 init = build (INIT_EXPR, type, decl, init);
7800 return init;
7803 /* If DECL is not a local variable, give it RTL. */
7805 static void
7806 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7808 int toplev = toplevel_bindings_p ();
7809 int defer_p;
7811 /* Handle non-variables up front. */
7812 if (TREE_CODE (decl) != VAR_DECL)
7814 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7815 return;
7818 /* If we see a class member here, it should be a static data
7819 member. */
7820 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7822 my_friendly_assert (TREE_STATIC (decl), 19990828);
7823 /* An in-class declaration of a static data member should be
7824 external; it is only a declaration, and not a definition. */
7825 if (init == NULL_TREE)
7826 my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7829 /* Set the DECL_ASSEMBLER_NAME for the variable. */
7830 if (asmspec)
7832 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7833 /* The `register' keyword, when used together with an
7834 asm-specification, indicates that the variable should be
7835 placed in a particular register. */
7836 if (DECL_REGISTER (decl))
7837 DECL_C_HARD_REGISTER (decl) = 1;
7840 /* We don't create any RTL for local variables. */
7841 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7842 return;
7844 /* We defer emission of local statics until the corresponding
7845 DECL_STMT is expanded. */
7846 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7848 /* We try to defer namespace-scope static constants so that they are
7849 not emitted into the object file unnecessarily. */
7850 if (!DECL_VIRTUAL_P (decl)
7851 && TREE_READONLY (decl)
7852 && DECL_INITIAL (decl) != NULL_TREE
7853 && DECL_INITIAL (decl) != error_mark_node
7854 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7855 && toplev
7856 && !TREE_PUBLIC (decl))
7858 /* Fool with the linkage of static consts according to #pragma
7859 interface. */
7860 if (!interface_unknown && !TREE_PUBLIC (decl))
7862 TREE_PUBLIC (decl) = 1;
7863 DECL_EXTERNAL (decl) = interface_only;
7866 defer_p = 1;
7868 /* Likewise for template instantiations. */
7869 else if (DECL_COMDAT (decl))
7870 defer_p = 1;
7872 /* If we're deferring the variable, we only need to make RTL if
7873 there's an ASMSPEC. Otherwise, we'll lazily create it later when
7874 we need it. (There's no way to lazily create RTL for things that
7875 have assembly specs because the information about the specifier
7876 isn't stored in the tree, yet) */
7877 if (defer_p && asmspec)
7878 make_decl_rtl (decl, asmspec);
7879 /* If we're not deferring, go ahead and assemble the variable. */
7880 else if (!defer_p)
7881 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7884 /* The old ARM scoping rules injected variables declared in the
7885 initialization statement of a for-statement into the surrounding
7886 scope. We support this usage, in order to be backward-compatible.
7887 DECL is a just-declared VAR_DECL; if necessary inject its
7888 declaration into the surrounding scope. */
7890 void
7891 maybe_inject_for_scope_var (tree decl)
7893 timevar_push (TV_NAME_LOOKUP);
7894 if (!DECL_NAME (decl))
7896 timevar_pop (TV_NAME_LOOKUP);
7897 return;
7900 /* Declarations of __FUNCTION__ and its ilk appear magically when
7901 the variable is first used. If that happens to be inside a
7902 for-loop, we don't want to do anything special. */
7903 if (DECL_PRETTY_FUNCTION_P (decl))
7905 timevar_pop (TV_NAME_LOOKUP);
7906 return;
7909 if (current_binding_level->is_for_scope)
7911 struct cp_binding_level *outer
7912 = current_binding_level->level_chain;
7914 /* Check to see if the same name is already bound at the outer
7915 level, either because it was directly declared, or because a
7916 dead for-decl got preserved. In either case, the code would
7917 not have been valid under the ARM scope rules, so clear
7918 is_for_scope for the current_binding_level.
7920 Otherwise, we need to preserve the temp slot for decl to last
7921 into the outer binding level. */
7923 cxx_binding *outer_binding
7924 = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
7926 if (outer_binding && BINDING_SCOPE (outer_binding) == outer
7927 && (TREE_CODE (BINDING_VALUE (outer_binding)) == VAR_DECL)
7928 && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7930 BINDING_VALUE (outer_binding)
7931 = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7932 current_binding_level->is_for_scope = 0;
7935 timevar_pop (TV_NAME_LOOKUP);
7938 /* Generate code to initialize DECL (a local variable). */
7940 static void
7941 initialize_local_var (tree decl, tree init)
7943 tree type = TREE_TYPE (decl);
7944 tree cleanup;
7946 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
7947 || TREE_CODE (decl) == RESULT_DECL,
7948 20021010);
7949 my_friendly_assert (!TREE_STATIC (decl), 20021010);
7951 if (DECL_SIZE (decl) == NULL_TREE)
7953 /* If we used it already as memory, it must stay in memory. */
7954 DECL_INITIAL (decl) = NULL_TREE;
7955 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7958 if (DECL_SIZE (decl) && type != error_mark_node)
7960 int already_used;
7962 /* Compute and store the initial value. */
7963 already_used = TREE_USED (decl) || TREE_USED (type);
7965 /* Perform the initialization. */
7966 if (init)
7968 int saved_stmts_are_full_exprs_p;
7970 my_friendly_assert (building_stmt_tree (), 20000906);
7971 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7972 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7973 finish_expr_stmt (init);
7974 current_stmt_tree ()->stmts_are_full_exprs_p =
7975 saved_stmts_are_full_exprs_p;
7978 /* Set this to 0 so we can tell whether an aggregate which was
7979 initialized was ever used. Don't do this if it has a
7980 destructor, so we don't complain about the 'resource
7981 allocation is initialization' idiom. Now set
7982 attribute((unused)) on types so decls of that type will be
7983 marked used. (see TREE_USED, above.) */
7984 if (TYPE_NEEDS_CONSTRUCTING (type)
7985 && ! already_used
7986 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7987 && DECL_NAME (decl))
7988 TREE_USED (decl) = 0;
7989 else if (already_used)
7990 TREE_USED (decl) = 1;
7993 /* Generate a cleanup, if necessary. */
7994 cleanup = cxx_maybe_build_cleanup (decl);
7995 if (DECL_SIZE (decl) && cleanup)
7996 finish_decl_cleanup (decl, cleanup);
7999 /* Finish processing of a declaration;
8000 install its line number and initial value.
8001 If the length of an array type is not known before,
8002 it must be determined now, from the initial value, or it is an error.
8004 INIT holds the value of an initializer that should be allowed to escape
8005 the normal rules.
8007 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8008 if the (init) syntax was used. */
8010 void
8011 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
8013 tree type;
8014 tree ttype = NULL_TREE;
8015 tree cleanup;
8016 const char *asmspec = NULL;
8017 int was_readonly = 0;
8019 if (! decl)
8021 if (init)
8022 error ("assignment (not initialization) in declaration");
8023 return;
8026 my_friendly_assert (TREE_CODE (decl) != RESULT_DECL, 20030619);
8028 /* Assume no cleanup is required. */
8029 cleanup = NULL_TREE;
8031 /* If a name was specified, get the string. */
8032 if (global_scope_p (current_binding_level))
8033 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8034 if (asmspec_tree)
8035 asmspec = TREE_STRING_POINTER (asmspec_tree);
8037 if (init && TREE_CODE (init) == NAMESPACE_DECL)
8039 error ("cannot initialize `%D' to namespace `%D'",
8040 decl, init);
8041 init = NULL_TREE;
8044 if (current_class_type
8045 && CP_DECL_CONTEXT (decl) == current_class_type
8046 && TYPE_BEING_DEFINED (current_class_type)
8047 && (DECL_INITIAL (decl) || init))
8048 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8050 if (TREE_CODE (decl) == VAR_DECL
8051 && DECL_CONTEXT (decl)
8052 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8053 && DECL_CONTEXT (decl) != current_namespace
8054 && init)
8056 /* Leave the namespace of the object. */
8057 pop_decl_namespace ();
8060 type = TREE_TYPE (decl);
8062 if (type == error_mark_node)
8063 goto finish_end0;
8065 if (TYPE_HAS_MUTABLE_P (type))
8066 TREE_READONLY (decl) = 0;
8068 if (processing_template_decl)
8070 /* Add this declaration to the statement-tree. */
8071 if (at_function_scope_p ())
8072 add_decl_stmt (decl);
8074 if (init && DECL_INITIAL (decl))
8075 DECL_INITIAL (decl) = init;
8076 goto finish_end0;
8079 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8080 my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8082 /* Take care of TYPE_DECLs up front. */
8083 if (TREE_CODE (decl) == TYPE_DECL)
8085 if (type != error_mark_node
8086 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8088 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8089 warning ("shadowing previous type declaration of `%#D'", decl);
8090 set_identifier_type_value (DECL_NAME (decl), decl);
8093 /* If we have installed this as the canonical typedef for this
8094 type, and that type has not been defined yet, delay emitting
8095 the debug information for it, as we will emit it later. */
8096 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8097 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8098 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8100 rest_of_decl_compilation (decl, NULL,
8101 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8102 goto finish_end;
8105 if (TREE_CODE (decl) != FUNCTION_DECL)
8106 ttype = target_type (type);
8108 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8109 && (TYPE_NEEDS_CONSTRUCTING (type)
8110 || TREE_CODE (type) == REFERENCE_TYPE))
8112 /* Currently, GNU C++ puts constants in text space, making them
8113 impossible to initialize. In the future, one would hope for
8114 an operating system which understood the difference between
8115 initialization and the running of a program. */
8116 was_readonly = 1;
8117 TREE_READONLY (decl) = 0;
8120 if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8122 /* This must override the asm specifier which was placed by
8123 grokclassfn. Lay this out fresh. */
8124 SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8125 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8126 make_decl_rtl (decl, asmspec);
8128 else if (TREE_CODE (decl) == VAR_DECL)
8130 /* Only PODs can have thread-local storage. Other types may require
8131 various kinds of non-trivial initialization. */
8132 if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8133 error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8134 decl, TREE_TYPE (decl));
8135 /* Convert the initializer to the type of DECL, if we have not
8136 already initialized DECL. */
8137 if (!DECL_INITIALIZED_P (decl)
8138 /* If !DECL_EXTERNAL then DECL is being defined. In the
8139 case of a static data member initialized inside the
8140 class-specifier, there can be an initializer even if DECL
8141 is *not* defined. */
8142 && (!DECL_EXTERNAL (decl) || init))
8144 init = check_initializer (decl, init, flags, &cleanup);
8145 /* Thread-local storage cannot be dynamically initialized. */
8146 if (DECL_THREAD_LOCAL (decl) && init)
8148 error ("`%D' is thread-local and so cannot be dynamically "
8149 "initialized", decl);
8150 init = NULL_TREE;
8152 /* Handle:
8154 [dcl.init]
8156 The memory occupied by any object of static storage
8157 duration is zero-initialized at program startup before
8158 any other initialization takes place.
8160 We cannot create an appropriate initializer until after
8161 the type of DECL is finalized. If DECL_INITIAL is set,
8162 then the DECL is statically initialized, and any
8163 necessary zero-initialization has already been performed. */
8164 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8165 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8166 /*nelts=*/NULL_TREE,
8167 /*static_storage_p=*/true);
8168 /* Remember that the initialization for this variable has
8169 taken place. */
8170 DECL_INITIALIZED_P (decl) = 1;
8172 /* If the variable has an array type, lay out the type, even if
8173 there is no initializer. It is valid to index through the
8174 array, and we must get TYPE_ALIGN set correctly on the array
8175 type. */
8176 else if (TREE_CODE (type) == ARRAY_TYPE)
8177 layout_type (type);
8180 /* Add this declaration to the statement-tree. This needs to happen
8181 after the call to check_initializer so that the DECL_STMT for a
8182 reference temp is added before the DECL_STMT for the reference itself. */
8183 if (at_function_scope_p ())
8184 add_decl_stmt (decl);
8186 if (TREE_CODE (decl) == VAR_DECL)
8187 layout_var_decl (decl);
8189 /* Output the assembler code and/or RTL code for variables and functions,
8190 unless the type is an undefined structure or union.
8191 If not, it will get done when the type is completed. */
8192 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
8194 if (TREE_CODE (decl) == VAR_DECL)
8195 maybe_commonize_var (decl);
8197 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8199 if (TREE_CODE (type) == FUNCTION_TYPE
8200 || TREE_CODE (type) == METHOD_TYPE)
8201 abstract_virtuals_error (decl,
8202 strip_array_types (TREE_TYPE (type)));
8203 else
8204 abstract_virtuals_error (decl, strip_array_types (type));
8206 if (TREE_CODE (decl) == FUNCTION_DECL
8207 || TREE_TYPE (decl) == error_mark_node)
8208 /* No initialization required. */
8210 else if (DECL_EXTERNAL (decl)
8211 && ! (DECL_LANG_SPECIFIC (decl)
8212 && DECL_NOT_REALLY_EXTERN (decl)))
8214 if (init)
8215 DECL_INITIAL (decl) = init;
8217 else
8219 /* A variable definition. */
8220 if (DECL_FUNCTION_SCOPE_P (decl))
8222 /* This is a local declaration. */
8223 if (doing_semantic_analysis_p ())
8224 maybe_inject_for_scope_var (decl);
8225 /* Initialize the local variable. */
8226 if (processing_template_decl)
8228 if (init || DECL_INITIAL (decl) == error_mark_node)
8229 DECL_INITIAL (decl) = init;
8231 else if (!TREE_STATIC (decl))
8232 initialize_local_var (decl, init);
8235 if (TREE_STATIC (decl))
8236 expand_static_init (decl, init);
8238 finish_end0:
8240 /* Undo call to `pushclass' that was done in `start_decl'
8241 due to initialization of qualified member variable.
8242 I.e., Foo::x = 10; */
8244 tree context = CP_DECL_CONTEXT (decl);
8245 if (context
8246 && TYPE_P (context)
8247 && (TREE_CODE (decl) == VAR_DECL
8248 /* We also have a pushclass done that we need to undo here
8249 if we're at top level and declare a method. */
8250 || TREE_CODE (decl) == FUNCTION_DECL)
8251 /* If size hasn't been set, we're still defining it,
8252 and therefore inside the class body; don't pop
8253 the binding level.. */
8254 && COMPLETE_TYPE_P (context)
8255 && context == current_class_type)
8256 pop_nested_class ();
8260 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
8261 reference, insert it in the statement-tree now. */
8262 if (cleanup)
8263 add_stmt (cleanup);
8265 finish_end:
8267 if (was_readonly)
8268 TREE_READONLY (decl) = 1;
8270 /* If this was marked 'used', be sure it will be output. */
8271 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
8272 mark_referenced (DECL_ASSEMBLER_NAME (decl));
8275 /* This is here for a midend callback from c-common.c */
8277 void
8278 finish_decl (tree decl, tree init, tree asmspec_tree)
8280 cp_finish_decl (decl, init, asmspec_tree, 0);
8283 /* Returns a declaration for a VAR_DECL as if:
8285 extern "C" TYPE NAME;
8287 had been seen. Used to create compiler-generated global
8288 variables. */
8290 tree
8291 declare_global_var (tree name, tree type)
8293 tree decl;
8295 push_to_top_level ();
8296 decl = build_decl (VAR_DECL, name, type);
8297 TREE_PUBLIC (decl) = 1;
8298 DECL_EXTERNAL (decl) = 1;
8299 DECL_ARTIFICIAL (decl) = 1;
8300 pushdecl (decl);
8301 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8302 pop_from_top_level ();
8304 return decl;
8307 /* Returns a pointer to the `atexit' function. Note that if
8308 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8309 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8311 static tree
8312 get_atexit_node (void)
8314 tree atexit_fndecl;
8315 tree arg_types;
8316 tree fn_type;
8317 tree fn_ptr_type;
8318 const char *name;
8320 if (atexit_node)
8321 return atexit_node;
8323 if (flag_use_cxa_atexit)
8325 /* The declaration for `__cxa_atexit' is:
8327 int __cxa_atexit (void (*)(void *), void *, void *)
8329 We build up the argument types and then then function type
8330 itself. */
8332 /* First, build the pointer-to-function type for the first
8333 argument. */
8334 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8335 fn_type = build_function_type (void_type_node, arg_types);
8336 fn_ptr_type = build_pointer_type (fn_type);
8337 /* Then, build the rest of the argument types. */
8338 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8339 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8340 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8341 /* And the final __cxa_atexit type. */
8342 fn_type = build_function_type (integer_type_node, arg_types);
8343 fn_ptr_type = build_pointer_type (fn_type);
8344 name = "__cxa_atexit";
8346 else
8348 /* The declaration for `atexit' is:
8350 int atexit (void (*)());
8352 We build up the argument types and then then function type
8353 itself. */
8354 fn_type = build_function_type (void_type_node, void_list_node);
8355 fn_ptr_type = build_pointer_type (fn_type);
8356 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8357 /* Build the final atexit type. */
8358 fn_type = build_function_type (integer_type_node, arg_types);
8359 name = "atexit";
8362 /* Now, build the function declaration. */
8363 push_lang_context (lang_name_c);
8364 atexit_fndecl = build_library_fn_ptr (name, fn_type);
8365 mark_used (atexit_fndecl);
8366 pop_lang_context ();
8367 atexit_node = decay_conversion (atexit_fndecl);
8369 return atexit_node;
8372 /* Returns the __dso_handle VAR_DECL. */
8374 static tree
8375 get_dso_handle_node (void)
8377 if (dso_handle_node)
8378 return dso_handle_node;
8380 /* Declare the variable. */
8381 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8382 ptr_type_node);
8384 return dso_handle_node;
8387 /* Begin a new function with internal linkage whose job will be simply
8388 to destroy some particular variable. */
8390 static GTY(()) int start_cleanup_cnt;
8392 static tree
8393 start_cleanup_fn (void)
8395 int old_interface_only = interface_only;
8396 int old_interface_unknown = interface_unknown;
8397 char name[32];
8398 tree parmtypes;
8399 tree fntype;
8400 tree fndecl;
8402 push_to_top_level ();
8404 /* No need to mangle this. */
8405 push_lang_context (lang_name_c);
8407 interface_only = 0;
8408 interface_unknown = 1;
8410 /* Build the parameter-types. */
8411 parmtypes = void_list_node;
8412 /* Functions passed to __cxa_atexit take an additional parameter.
8413 We'll just ignore it. After we implement the new calling
8414 convention for destructors, we can eliminate the use of
8415 additional cleanup functions entirely in the -fnew-abi case. */
8416 if (flag_use_cxa_atexit)
8417 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8418 /* Build the function type itself. */
8419 fntype = build_function_type (void_type_node, parmtypes);
8420 /* Build the name of the function. */
8421 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
8422 /* Build the function declaration. */
8423 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8424 /* It's a function with internal linkage, generated by the
8425 compiler. */
8426 TREE_PUBLIC (fndecl) = 0;
8427 DECL_ARTIFICIAL (fndecl) = 1;
8428 /* Make the function `inline' so that it is only emitted if it is
8429 actually needed. It is unlikely that it will be inlined, since
8430 it is only called via a function pointer, but we avoid unnecessary
8431 emissions this way. */
8432 DECL_INLINE (fndecl) = 1;
8433 /* Build the parameter. */
8434 if (flag_use_cxa_atexit)
8436 tree parmdecl;
8438 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8439 DECL_CONTEXT (parmdecl) = fndecl;
8440 TREE_USED (parmdecl) = 1;
8441 DECL_ARGUMENTS (fndecl) = parmdecl;
8444 pushdecl (fndecl);
8445 start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8447 interface_unknown = old_interface_unknown;
8448 interface_only = old_interface_only;
8450 pop_lang_context ();
8452 return current_function_decl;
8455 /* Finish the cleanup function begun by start_cleanup_fn. */
8457 static void
8458 end_cleanup_fn (void)
8460 expand_or_defer_fn (finish_function (0));
8462 pop_from_top_level ();
8465 /* Generate code to handle the destruction of DECL, an object with
8466 static storage duration. */
8468 void
8469 register_dtor_fn (tree decl)
8471 tree cleanup;
8472 tree compound_stmt;
8473 tree args;
8474 tree fcall;
8476 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8477 return;
8479 /* Call build_cleanup before we enter the anonymous function so that
8480 any access checks will be done relative to the current scope,
8481 rather than the scope of the anonymous function. */
8482 build_cleanup (decl);
8484 /* Now start the function. */
8485 cleanup = start_cleanup_fn ();
8487 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8488 to the original function, rather than the anonymous one. That
8489 will make the back-end think that nested functions are in use,
8490 which causes confusion. */
8492 push_deferring_access_checks (dk_no_check);
8493 fcall = build_cleanup (decl);
8494 pop_deferring_access_checks ();
8496 /* Create the body of the anonymous function. */
8497 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
8498 finish_expr_stmt (fcall);
8499 finish_compound_stmt (compound_stmt);
8500 end_cleanup_fn ();
8502 /* Call atexit with the cleanup function. */
8503 cxx_mark_addressable (cleanup);
8504 mark_used (cleanup);
8505 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8506 if (flag_use_cxa_atexit)
8508 args = tree_cons (NULL_TREE,
8509 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8510 NULL_TREE);
8511 args = tree_cons (NULL_TREE, null_pointer_node, args);
8512 args = tree_cons (NULL_TREE, cleanup, args);
8514 else
8515 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8516 finish_expr_stmt (build_function_call (get_atexit_node (), args));
8519 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8520 is its initializer. Generate code to handle the construction
8521 and destruction of DECL. */
8523 static void
8524 expand_static_init (tree decl, tree init)
8526 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8527 my_friendly_assert (TREE_STATIC (decl), 20021010);
8529 /* Some variables require no initialization. */
8530 if (!init
8531 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8532 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8533 return;
8535 if (! toplevel_bindings_p ())
8537 /* Emit code to perform this initialization but once. */
8538 tree if_stmt;
8539 tree then_clause;
8540 tree assignment;
8541 tree guard;
8542 tree guard_init;
8544 /* Emit code to perform this initialization but once. This code
8545 looks like:
8547 static int guard = 0;
8548 if (!guard) {
8549 // Do initialization.
8550 guard = 1;
8551 // Register variable for destruction at end of program.
8554 Note that the `temp' variable is only set to 1 *after* the
8555 initialization is complete. This ensures that an exception,
8556 thrown during the construction, will cause the variable to
8557 reinitialized when we pass through this code again, as per:
8559 [stmt.dcl]
8561 If the initialization exits by throwing an exception, the
8562 initialization is not complete, so it will be tried again
8563 the next time control enters the declaration.
8565 In theory, this process should be thread-safe, too; multiple
8566 threads should not be able to initialize the variable more
8567 than once. We don't yet attempt to ensure thread-safety. */
8569 /* Create the guard variable. */
8570 guard = get_guard (decl);
8572 /* Begin the conditional initialization. */
8573 if_stmt = begin_if_stmt ();
8574 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8575 then_clause = begin_compound_stmt (/*has_no_scope=*/false);
8577 /* Do the initialization itself. */
8578 assignment = init ? init : NULL_TREE;
8580 /* Once the assignment is complete, set TEMP to 1. Since the
8581 construction of the static object is complete at this point,
8582 we want to make sure TEMP is set to 1 even if a temporary
8583 constructed during the initialization throws an exception
8584 when it is destroyed. So, we combine the initialization and
8585 the assignment to TEMP into a single expression, ensuring
8586 that when we call finish_expr_stmt the cleanups will not be
8587 run until after TEMP is set to 1. */
8588 guard_init = set_guard (guard);
8589 if (assignment)
8590 assignment = build_compound_expr (assignment, guard_init);
8591 else
8592 assignment = guard_init;
8593 finish_expr_stmt (assignment);
8595 /* Use atexit to register a function for destroying this static
8596 variable. */
8597 register_dtor_fn (decl);
8599 finish_compound_stmt (then_clause);
8600 finish_then_clause (if_stmt);
8601 finish_if_stmt ();
8603 else
8604 static_aggregates = tree_cons (init, decl, static_aggregates);
8607 /* Finish the declaration of a catch-parameter. */
8609 tree
8610 start_handler_parms (tree declspecs, tree declarator)
8612 tree decl;
8613 if (declspecs)
8615 decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8616 1, NULL);
8617 if (decl == NULL_TREE)
8618 error ("invalid catch parameter");
8620 else
8621 decl = NULL_TREE;
8623 return decl;
8627 /* Make TYPE a complete type based on INITIAL_VALUE.
8628 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8629 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8632 complete_array_type (tree type, tree initial_value, int do_default)
8634 register tree maxindex = NULL_TREE;
8635 int value = 0;
8637 if (initial_value)
8639 /* An array of character type can be initialized from a
8640 brace-enclosed string constant. */
8641 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8642 && TREE_CODE (initial_value) == CONSTRUCTOR
8643 && CONSTRUCTOR_ELTS (initial_value)
8644 && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8645 == STRING_CST)
8646 && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8647 initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8649 /* Note MAXINDEX is really the maximum index, one less than the
8650 size. */
8651 if (TREE_CODE (initial_value) == STRING_CST)
8653 int eltsize
8654 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8655 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8656 / eltsize) - 1, 0);
8658 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8660 tree elts = CONSTRUCTOR_ELTS (initial_value);
8662 maxindex = ssize_int (-1);
8663 for (; elts; elts = TREE_CHAIN (elts))
8665 if (TREE_PURPOSE (elts))
8666 maxindex = TREE_PURPOSE (elts);
8667 else
8668 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8670 maxindex = copy_node (maxindex);
8672 else
8674 /* Make an error message unless that happened already. */
8675 if (initial_value != error_mark_node)
8676 value = 1;
8677 else
8678 initial_value = NULL_TREE;
8680 /* Prevent further error messages. */
8681 maxindex = build_int_2 (0, 0);
8685 if (!maxindex)
8687 if (do_default)
8688 maxindex = build_int_2 (0, 0);
8689 value = 2;
8692 if (maxindex)
8694 tree itype;
8695 tree domain;
8697 domain = build_index_type (maxindex);
8698 TYPE_DOMAIN (type) = domain;
8700 if (! TREE_TYPE (maxindex))
8701 TREE_TYPE (maxindex) = domain;
8702 if (initial_value)
8703 itype = TREE_TYPE (initial_value);
8704 else
8705 itype = NULL;
8706 if (itype && !TYPE_DOMAIN (itype))
8707 TYPE_DOMAIN (itype) = domain;
8708 /* The type of the main variant should never be used for arrays
8709 of different sizes. It should only ever be completed with the
8710 size of the array. */
8711 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8712 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8715 /* Lay out the type now that we can get the real answer. */
8717 layout_type (type);
8719 return value;
8722 /* Return zero if something is declared to be a member of type
8723 CTYPE when in the context of CUR_TYPE. STRING is the error
8724 message to print in that case. Otherwise, quietly return 1. */
8726 static int
8727 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8729 if (ctype && ctype != cur_type)
8731 if (flags == DTOR_FLAG)
8732 error ("destructor for alien class `%T' cannot be a member",
8733 ctype);
8734 else
8735 error ("constructor for alien class `%T' cannot be a member",
8736 ctype);
8737 return 0;
8739 return 1;
8742 /* Subroutine of `grokdeclarator'. */
8744 /* Generate errors possibly applicable for a given set of specifiers.
8745 This is for ARM $7.1.2. */
8747 static void
8748 bad_specifiers (tree object,
8749 const char* type,
8750 int virtualp,
8751 int quals,
8752 int inlinep,
8753 int friendp,
8754 int raises)
8756 if (virtualp)
8757 error ("`%D' declared as a `virtual' %s", object, type);
8758 if (inlinep)
8759 error ("`%D' declared as an `inline' %s", object, type);
8760 if (quals)
8761 error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8762 object, type);
8763 if (friendp)
8764 cp_error_at ("`%D' declared as a friend", object);
8765 if (raises
8766 && (TREE_CODE (object) == TYPE_DECL
8767 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8768 && !TYPE_REFFN_P (TREE_TYPE (object))
8769 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8770 cp_error_at ("`%D' declared with an exception specification", object);
8773 /* CTYPE is class type, or null if non-class.
8774 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8775 or METHOD_TYPE.
8776 DECLARATOR is the function's name.
8777 VIRTUALP is truthvalue of whether the function is virtual or not.
8778 FLAGS are to be passed through to `grokclassfn'.
8779 QUALS are qualifiers indicating whether the function is `const'
8780 or `volatile'.
8781 RAISES is a list of exceptions that this function can raise.
8782 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8783 not look, and -1 if we should not call `grokclassfn' at all.
8785 Returns `NULL_TREE' if something goes wrong, after issuing
8786 applicable error messages. */
8788 static tree
8789 grokfndecl (tree ctype,
8790 tree type,
8791 tree declarator,
8792 tree orig_declarator,
8793 int virtualp,
8794 enum overload_flags flags,
8795 tree quals,
8796 tree raises,
8797 int check,
8798 int friendp,
8799 int publicp,
8800 int inlinep,
8801 int funcdef_flag,
8802 int template_count,
8803 tree in_namespace)
8805 tree decl;
8806 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8807 int has_default_arg = 0;
8808 tree t;
8810 if (raises)
8811 type = build_exception_variant (type, raises);
8813 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8814 /* Propagate volatile out from type to decl. */
8815 if (TYPE_VOLATILE (type))
8816 TREE_THIS_VOLATILE (decl) = 1;
8818 /* If this decl has namespace scope, set that up. */
8819 if (in_namespace)
8820 set_decl_namespace (decl, in_namespace, friendp);
8821 else if (!ctype)
8822 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8824 /* `main' and builtins have implicit 'C' linkage. */
8825 if ((MAIN_NAME_P (declarator)
8826 || (IDENTIFIER_LENGTH (declarator) > 10
8827 && IDENTIFIER_POINTER (declarator)[0] == '_'
8828 && IDENTIFIER_POINTER (declarator)[1] == '_'
8829 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8830 && current_lang_name == lang_name_cplusplus
8831 && ctype == NULL_TREE
8832 /* NULL_TREE means global namespace. */
8833 && DECL_CONTEXT (decl) == NULL_TREE)
8834 SET_DECL_LANGUAGE (decl, lang_c);
8836 /* Should probably propagate const out from type to decl I bet (mrs). */
8837 if (staticp)
8839 DECL_STATIC_FUNCTION_P (decl) = 1;
8840 DECL_CONTEXT (decl) = ctype;
8843 if (ctype)
8844 DECL_CONTEXT (decl) = ctype;
8846 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8848 if (processing_template_decl)
8849 error ("cannot declare `::main' to be a template");
8850 if (inlinep)
8851 error ("cannot declare `::main' to be inline");
8852 if (!publicp)
8853 error ("cannot declare `::main' to be static");
8854 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8855 integer_type_node))
8856 error ("`main' must return `int'");
8857 inlinep = 0;
8858 publicp = 1;
8861 /* Members of anonymous types and local classes have no linkage; make
8862 them internal. */
8863 /* FIXME what if it gets a name from typedef? */
8864 if (ctype && (TYPE_ANONYMOUS_P (ctype)
8865 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8866 publicp = 0;
8868 if (publicp)
8870 /* [basic.link]: A name with no linkage (notably, the name of a class
8871 or enumeration declared in a local scope) shall not be used to
8872 declare an entity with linkage.
8874 Only check this for public decls for now. See core 319, 389. */
8875 t = no_linkage_check (TREE_TYPE (decl));
8876 if (t)
8878 if (TYPE_ANONYMOUS_P (t))
8880 if (DECL_EXTERN_C_P (decl))
8881 /* Allow this; it's pretty common in C. */;
8882 else
8884 pedwarn ("non-local function `%#D' uses anonymous type",
8885 decl);
8886 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8887 cp_pedwarn_at ("\
8888 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8889 TYPE_NAME (t));
8892 else
8893 pedwarn ("non-local function `%#D' uses local type `%T'",
8894 decl, t);
8898 TREE_PUBLIC (decl) = publicp;
8899 if (! publicp)
8901 DECL_INTERFACE_KNOWN (decl) = 1;
8902 DECL_NOT_REALLY_EXTERN (decl) = 1;
8905 /* If the declaration was declared inline, mark it as such. */
8906 if (inlinep)
8907 DECL_DECLARED_INLINE_P (decl) = 1;
8908 /* We inline functions that are explicitly declared inline, or, when
8909 the user explicitly asks us to, all functions. */
8910 if (DECL_DECLARED_INLINE_P (decl)
8911 || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag))
8912 DECL_INLINE (decl) = 1;
8914 DECL_EXTERNAL (decl) = 1;
8915 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8917 error ("%smember function `%D' cannot have `%T' method qualifier",
8918 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8919 quals = NULL_TREE;
8922 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8923 grok_op_properties (decl, friendp, /*complain=*/true);
8925 if (ctype && decl_function_context (decl))
8926 DECL_NO_STATIC_CHAIN (decl) = 1;
8928 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8929 if (TREE_PURPOSE (t)
8930 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8932 has_default_arg = 1;
8933 break;
8936 if (friendp
8937 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8939 if (funcdef_flag)
8940 error
8941 ("defining explicit specialization `%D' in friend declaration",
8942 orig_declarator);
8943 else
8945 tree fns = TREE_OPERAND (orig_declarator, 0);
8946 tree args = TREE_OPERAND (orig_declarator, 1);
8948 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8950 /* Something like `template <class T> friend void f<T>()'. */
8951 error ("invalid use of template-id `%D' in declaration of primary template",
8952 orig_declarator);
8953 return NULL_TREE;
8957 /* A friend declaration of the form friend void f<>(). Record
8958 the information in the TEMPLATE_ID_EXPR. */
8959 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8961 if (TREE_CODE (fns) == COMPONENT_REF)
8963 /* Due to bison parser ickiness, we will have already looked
8964 up an operator_name or PFUNCNAME within the current class
8965 (see template_id in parse.y). If the current class contains
8966 such a name, we'll get a COMPONENT_REF here. Undo that. */
8968 my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
8969 == current_class_type, 20001120);
8970 fns = TREE_OPERAND (fns, 1);
8972 my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
8973 || TREE_CODE (fns) == OVERLOAD, 20001120);
8974 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
8976 if (has_default_arg)
8978 error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8979 decl);
8980 return NULL_TREE;
8983 if (inlinep)
8985 error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8986 decl);
8987 return NULL_TREE;
8992 if (funcdef_flag)
8993 /* Make the init_value nonzero so pushdecl knows this is not
8994 tentative. error_mark_node is replaced later with the BLOCK. */
8995 DECL_INITIAL (decl) = error_mark_node;
8997 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8998 TREE_NOTHROW (decl) = 1;
9000 /* Caller will do the rest of this. */
9001 if (check < 0)
9002 return decl;
9004 if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
9005 DECL_CONSTRUCTOR_P (decl) = 1;
9007 /* Function gets the ugly name, field gets the nice one. This call
9008 may change the type of the function (because of default
9009 parameters)! */
9010 if (ctype != NULL_TREE)
9011 grokclassfn (ctype, decl, flags, quals);
9013 decl = check_explicit_specialization (orig_declarator, decl,
9014 template_count,
9015 2 * (funcdef_flag != 0) +
9016 4 * (friendp != 0));
9017 if (decl == error_mark_node)
9018 return NULL_TREE;
9020 if (ctype != NULL_TREE
9021 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9022 && check)
9024 tree old_decl;
9026 old_decl = check_classfn (ctype, decl);
9028 if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9029 /* Because grokfndecl is always supposed to return a
9030 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9031 here. We depend on our callers to figure out that its
9032 really a template that's being returned. */
9033 old_decl = DECL_TEMPLATE_RESULT (old_decl);
9035 if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9036 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9038 /* Remove the `this' parm added by grokclassfn.
9039 XXX Isn't this done in start_function, too? */
9040 revert_static_member_fn (decl);
9041 last_function_parms = TREE_CHAIN (last_function_parms);
9043 if (old_decl && DECL_ARTIFICIAL (old_decl))
9044 error ("definition of implicitly-declared `%D'", old_decl);
9046 if (old_decl)
9048 bool ok;
9050 /* Since we've smashed OLD_DECL to its
9051 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9052 if (TREE_CODE (decl) == TEMPLATE_DECL)
9053 decl = DECL_TEMPLATE_RESULT (decl);
9055 /* Attempt to merge the declarations. This can fail, in
9056 the case of some invalid specialization declarations. */
9057 push_scope (ctype);
9058 ok = duplicate_decls (decl, old_decl);
9059 pop_scope (ctype);
9060 if (!ok)
9062 error ("no `%#D' member function declared in class `%T'",
9063 decl, ctype);
9064 return NULL_TREE;
9066 return old_decl;
9070 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9071 return NULL_TREE;
9073 if (ctype == NULL_TREE || check)
9074 return decl;
9076 if (virtualp)
9077 DECL_VIRTUAL_P (decl) = 1;
9079 return decl;
9082 /* Create a VAR_DECL named NAME with the indicated TYPE.
9084 If SCOPE is non-NULL, it is the class type or namespace containing
9085 the variable. If SCOPE is NULL, the variable should is created in
9086 the innermost enclosings scope. */
9088 static tree
9089 grokvardecl (tree type,
9090 tree name,
9091 RID_BIT_TYPE * specbits_in,
9092 int initialized,
9093 int constp,
9094 tree scope)
9096 tree decl;
9097 RID_BIT_TYPE specbits;
9099 my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE,
9100 20020808);
9102 specbits = *specbits_in;
9104 /* Compute the scope in which to place the variable. */
9105 if (!scope)
9107 /* An explicit "extern" specifier indicates a namespace-scope
9108 variable. */
9109 if (RIDBIT_SETP (RID_EXTERN, specbits))
9110 scope = current_namespace;
9111 else if (!at_function_scope_p ())
9113 scope = current_scope ();
9114 if (!scope)
9115 scope = current_namespace;
9119 if (scope
9120 && (/* If the variable is a namespace-scope variable declared in a
9121 template, we need DECL_LANG_SPECIFIC. */
9122 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9123 /* Similarly for namespace-scope variables with language linkage
9124 other than C++. */
9125 || (TREE_CODE (scope) == NAMESPACE_DECL
9126 && current_lang_name != lang_name_cplusplus)
9127 /* Similarly for static data members. */
9128 || TYPE_P (scope)))
9129 decl = build_lang_decl (VAR_DECL, name, type);
9130 else
9131 decl = build_decl (VAR_DECL, name, type);
9133 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9134 set_decl_namespace (decl, scope, 0);
9135 else
9136 DECL_CONTEXT (decl) = scope;
9138 if (name && scope && current_lang_name != lang_name_c)
9139 /* We can't mangle lazily here because we don't have any
9140 way to recover whether or not a variable was `extern
9141 "C"' later. */
9142 mangle_decl (decl);
9144 if (RIDBIT_SETP (RID_EXTERN, specbits))
9146 DECL_THIS_EXTERN (decl) = 1;
9147 DECL_EXTERNAL (decl) = !initialized;
9150 /* In class context, static means one per class,
9151 public access, and static storage. */
9152 if (DECL_CLASS_SCOPE_P (decl))
9154 TREE_PUBLIC (decl) = 1;
9155 TREE_STATIC (decl) = 1;
9156 DECL_EXTERNAL (decl) = 0;
9158 /* At top level, either `static' or no s.c. makes a definition
9159 (perhaps tentative), and absence of `static' makes it public. */
9160 else if (toplevel_bindings_p ())
9162 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9163 && (DECL_THIS_EXTERN (decl) || ! constp));
9164 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9166 /* Not at top level, only `static' makes a static definition. */
9167 else
9169 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9170 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9173 if (RIDBIT_SETP (RID_THREAD, specbits))
9175 if (targetm.have_tls)
9176 DECL_THREAD_LOCAL (decl) = 1;
9177 else
9178 /* A mere warning is sure to result in improper semantics
9179 at runtime. Don't bother to allow this to compile. */
9180 error ("thread-local storage not supported for this target");
9183 if (TREE_PUBLIC (decl))
9185 /* [basic.link]: A name with no linkage (notably, the name of a class
9186 or enumeration declared in a local scope) shall not be used to
9187 declare an entity with linkage.
9189 Only check this for public decls for now. */
9190 tree t = no_linkage_check (TREE_TYPE (decl));
9191 if (t)
9193 if (TYPE_ANONYMOUS_P (t))
9194 /* Ignore for now; `enum { foo } e' is pretty common. */;
9195 else
9196 pedwarn ("non-local variable `%#D' uses local type `%T'",
9197 decl, t);
9201 return decl;
9204 /* Create and return a canonical pointer to member function type, for
9205 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
9207 tree
9208 build_ptrmemfunc_type (tree type)
9210 tree field, fields;
9211 tree t;
9212 tree unqualified_variant = NULL_TREE;
9214 if (type == error_mark_node)
9215 return type;
9217 /* If a canonical type already exists for this type, use it. We use
9218 this method instead of type_hash_canon, because it only does a
9219 simple equality check on the list of field members. */
9221 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9222 return t;
9224 /* Make sure that we always have the unqualified pointer-to-member
9225 type first. */
9226 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9227 unqualified_variant
9228 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9230 t = make_aggr_type (RECORD_TYPE);
9231 /* Let the front-end know this is a pointer to member function... */
9232 TYPE_PTRMEMFUNC_FLAG (t) = 1;
9233 /* ... and not really an aggregate. */
9234 SET_IS_AGGR_TYPE (t, 0);
9236 field = build_decl (FIELD_DECL, pfn_identifier, type);
9237 fields = field;
9239 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9240 TREE_CHAIN (field) = fields;
9241 fields = field;
9243 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9245 /* Zap out the name so that the back-end will give us the debugging
9246 information for this anonymous RECORD_TYPE. */
9247 TYPE_NAME (t) = NULL_TREE;
9249 /* If this is not the unqualified form of this pointer-to-member
9250 type, set the TYPE_MAIN_VARIANT for this type to be the
9251 unqualified type. Since they are actually RECORD_TYPEs that are
9252 not variants of each other, we must do this manually. */
9253 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9255 t = build_qualified_type (t, cp_type_quals (type));
9256 TYPE_MAIN_VARIANT (t) = unqualified_variant;
9257 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9258 TYPE_NEXT_VARIANT (unqualified_variant) = t;
9261 /* Cache this pointer-to-member type so that we can find it again
9262 later. */
9263 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9265 return t;
9268 /* Create and return a pointer to data member type. */
9270 tree
9271 build_ptrmem_type (tree class_type, tree member_type)
9273 if (TREE_CODE (member_type) == METHOD_TYPE)
9275 tree arg_types;
9277 arg_types = TYPE_ARG_TYPES (member_type);
9278 class_type = (cp_build_qualified_type
9279 (class_type,
9280 cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types)))));
9281 member_type
9282 = build_method_type_directly (class_type,
9283 TREE_TYPE (member_type),
9284 TREE_CHAIN (arg_types));
9285 return build_ptrmemfunc_type (build_pointer_type (member_type));
9287 else
9289 my_friendly_assert (TREE_CODE (member_type) != FUNCTION_TYPE,
9290 20030716);
9291 return build_offset_type (class_type, member_type);
9295 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9296 Check to see that the definition is valid. Issue appropriate error
9297 messages. Return 1 if the definition is particularly bad, or 0
9298 otherwise. */
9301 check_static_variable_definition (tree decl, tree type)
9303 /* Motion 10 at San Diego: If a static const integral data member is
9304 initialized with an integral constant expression, the initializer
9305 may appear either in the declaration (within the class), or in
9306 the definition, but not both. If it appears in the class, the
9307 member is a member constant. The file-scope definition is always
9308 required. */
9309 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9311 error ("invalid in-class initialization of static data member of non-integral type `%T'",
9312 type);
9313 /* If we just return the declaration, crashes will sometimes
9314 occur. We therefore return void_type_node, as if this was a
9315 friend declaration, to cause callers to completely ignore
9316 this declaration. */
9317 return 1;
9319 else if (!CP_TYPE_CONST_P (type))
9320 error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9321 decl);
9322 else if (pedantic && !INTEGRAL_TYPE_P (type))
9323 pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9325 return 0;
9328 /* Given the SIZE (i.e., number of elements) in an array, compute an
9329 appropriate index type for the array. If non-NULL, NAME is the
9330 name of the thing being declared. */
9332 tree
9333 compute_array_index_type (tree name, tree size)
9335 tree itype;
9337 /* If this involves a template parameter, it will be a constant at
9338 instantiation time, but we don't know what the value is yet.
9339 Even if no template parameters are involved, we may an expression
9340 that is not a constant; we don't even simplify `1 + 2' when
9341 processing a template. */
9342 if (processing_template_decl)
9344 /* Resolve a qualified reference to an enumerator or static
9345 const data member of ours. */
9346 if (TREE_CODE (size) == SCOPE_REF
9347 && TREE_OPERAND (size, 0) == current_class_type)
9349 tree t = lookup_field (current_class_type,
9350 TREE_OPERAND (size, 1), 0, false);
9351 if (t)
9352 size = t;
9355 return build_index_type (build_min (MINUS_EXPR, sizetype,
9356 size, integer_one_node));
9359 /* The size might be the result of a cast. */
9360 STRIP_TYPE_NOPS (size);
9362 /* It might be a const variable or enumeration constant. */
9363 size = decl_constant_value (size);
9365 /* The array bound must be an integer type. */
9366 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9367 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9368 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9370 if (name)
9371 error ("size of array `%D' has non-integer type", name);
9372 else
9373 error ("size of array has non-integer type");
9374 size = integer_one_node;
9377 /* Normally, the array-bound will be a constant. */
9378 if (TREE_CODE (size) == INTEGER_CST)
9380 /* Check to see if the array bound overflowed. Make that an
9381 error, no matter how generous we're being. */
9382 int old_flag_pedantic_errors = flag_pedantic_errors;
9383 int old_pedantic = pedantic;
9384 pedantic = flag_pedantic_errors = 1;
9385 constant_expression_warning (size);
9386 pedantic = old_pedantic;
9387 flag_pedantic_errors = old_flag_pedantic_errors;
9389 /* An array must have a positive number of elements. */
9390 if (INT_CST_LT (size, integer_zero_node))
9392 if (name)
9393 error ("size of array `%D' is negative", name);
9394 else
9395 error ("size of array is negative");
9396 size = integer_one_node;
9398 /* Except that an extension we allow zero-sized arrays. We
9399 always allow them in system headers because glibc uses
9400 them. */
9401 else if (integer_zerop (size) && pedantic && !in_system_header)
9403 if (name)
9404 pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9405 else
9406 pedwarn ("ISO C++ forbids zero-size array");
9409 else if (TREE_CONSTANT (size))
9411 /* `(int) &fn' is not a valid array bound. */
9412 if (name)
9413 error ("size of array `%D' is not an integral constant-expression",
9414 name);
9415 else
9416 error ("size of array is not an integral constant-expression");
9419 /* Compute the index of the largest element in the array. It is
9420 one less than the number of elements in the array. */
9421 itype
9422 = fold (cp_build_binary_op (MINUS_EXPR,
9423 cp_convert (ssizetype, size),
9424 cp_convert (ssizetype,
9425 integer_one_node)));
9427 /* Check for variable-sized arrays. We allow such things as an
9428 extension, even though they are not allowed in ANSI/ISO C++. */
9429 if (!TREE_CONSTANT (itype))
9431 if (pedantic)
9433 if (name)
9434 pedwarn ("ISO C++ forbids variable-size array `%D'",
9435 name);
9436 else
9437 pedwarn ("ISO C++ forbids variable-size array");
9440 /* Create a variable-sized array index type. */
9441 itype = variable_size (itype);
9443 /* Make sure that there was no overflow when creating to a signed
9444 index type. (For example, on a 32-bit machine, an array with
9445 size 2^32 - 1 is too big.) */
9446 else if (TREE_OVERFLOW (itype))
9448 error ("overflow in array dimension");
9449 TREE_OVERFLOW (itype) = 0;
9452 /* Create and return the appropriate index type. */
9453 return build_index_type (itype);
9456 /* Returns the scope (if any) in which the entity declared by
9457 DECLARATOR will be located. If the entity was declared with an
9458 unqualified name, NULL_TREE is returned. */
9460 tree
9461 get_scope_of_declarator (tree declarator)
9463 if (!declarator)
9464 return NULL_TREE;
9466 switch (TREE_CODE (declarator))
9468 case CALL_EXPR:
9469 case ARRAY_REF:
9470 case INDIRECT_REF:
9471 case ADDR_EXPR:
9472 /* For any of these, the main declarator is the first operand. */
9473 return get_scope_of_declarator (TREE_OPERAND
9474 (declarator, 0));
9476 case SCOPE_REF:
9477 /* For a pointer-to-member, continue descending. */
9478 if (TREE_CODE (TREE_OPERAND (declarator, 1))
9479 == INDIRECT_REF)
9480 return get_scope_of_declarator (TREE_OPERAND
9481 (declarator, 1));
9482 /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9483 which the declaration occurs is the first operand. */
9484 return TREE_OPERAND (declarator, 0);
9486 case TREE_LIST:
9487 /* Attributes to be applied. The declarator is TREE_VALUE. */
9488 return get_scope_of_declarator (TREE_VALUE (declarator));
9490 default:
9491 /* Otherwise, we have a declarator-id which is not a qualified
9492 name; the entity will be declared in the current scope. */
9493 return NULL_TREE;
9497 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9498 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9499 with this type. */
9501 static tree
9502 create_array_type_for_decl (tree name, tree type, tree size)
9504 tree itype = NULL_TREE;
9505 const char* error_msg;
9507 /* If things have already gone awry, bail now. */
9508 if (type == error_mark_node || size == error_mark_node)
9509 return error_mark_node;
9511 /* Assume that everything will go OK. */
9512 error_msg = NULL;
9514 /* There are some types which cannot be array elements. */
9515 switch (TREE_CODE (type))
9517 case VOID_TYPE:
9518 error_msg = "array of void";
9519 break;
9521 case FUNCTION_TYPE:
9522 error_msg = "array of functions";
9523 break;
9525 case REFERENCE_TYPE:
9526 error_msg = "array of references";
9527 break;
9529 case METHOD_TYPE:
9530 error_msg = "array of function members";
9531 break;
9533 default:
9534 break;
9537 /* If something went wrong, issue an error-message and return. */
9538 if (error_msg)
9540 if (name)
9541 error ("declaration of `%D' as %s", name, error_msg);
9542 else
9543 error ("creating %s", error_msg);
9545 return error_mark_node;
9548 /* [dcl.array]
9550 The constant expressions that specify the bounds of the arrays
9551 can be omitted only for the first member of the sequence. */
9552 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9554 if (name)
9555 error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9556 name);
9557 else
9558 error ("multidimensional array must have bounds for all dimensions except the first");
9560 return error_mark_node;
9563 /* Figure out the index type for the array. */
9564 if (size)
9565 itype = compute_array_index_type (name, size);
9567 return build_cplus_array_type (type, itype);
9570 /* Check that it's OK to declare a function with the indicated TYPE.
9571 SFK indicates the kind of special function (if any) that this
9572 function is. OPTYPE is the type given in a conversion operator
9573 declaration. Returns the actual return type of the function; that
9574 may be different than TYPE if an error occurs, or for certain
9575 special functions. */
9577 static tree
9578 check_special_function_return_type (special_function_kind sfk,
9579 tree type,
9580 tree optype)
9582 switch (sfk)
9584 case sfk_constructor:
9585 if (type)
9586 error ("return type specification for constructor invalid");
9588 type = void_type_node;
9589 break;
9591 case sfk_destructor:
9592 if (type)
9593 error ("return type specification for destructor invalid");
9594 type = void_type_node;
9595 break;
9597 case sfk_conversion:
9598 if (type && !same_type_p (type, optype))
9599 error ("operator `%T' declared to return `%T'", optype, type);
9600 else if (type)
9601 pedwarn ("return type specified for `operator %T'", optype);
9602 type = optype;
9603 break;
9605 default:
9606 abort ();
9607 break;
9610 return type;
9613 /* Given declspecs and a declarator (abstract or otherwise), determine
9614 the name and type of the object declared and construct a DECL node
9615 for it.
9617 DECLSPECS is a chain of tree_list nodes whose value fields
9618 are the storage classes and type specifiers.
9620 DECL_CONTEXT says which syntactic context this declaration is in:
9621 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9622 FUNCDEF for a function definition. Like NORMAL but a few different
9623 error messages in each case. Return value may be zero meaning
9624 this definition is too screwy to try to parse.
9625 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9626 handle member functions (which have FIELD context).
9627 Return value may be zero meaning this definition is too screwy to
9628 try to parse.
9629 PARM for a parameter declaration (either within a function prototype
9630 or before a function body). Make a PARM_DECL, or return void_type_node.
9631 CATCHPARM for a parameter declaration before a catch clause.
9632 TYPENAME if for a typename (in a cast or sizeof).
9633 Don't make a DECL node; just return the ..._TYPE node.
9634 FIELD for a struct or union field; make a FIELD_DECL.
9635 BITFIELD for a field with specified width.
9636 INITIALIZED is 1 if the decl has an initializer.
9638 ATTRLIST is a pointer to the list of attributes, which may be NULL
9639 if there are none; *ATTRLIST may be modified if attributes from inside
9640 the declarator should be applied to the declaration.
9642 When this function is called, scoping variables (such as
9643 CURRENT_CLASS_TYPE) should reflect the scope in which the
9644 declaration occurs, not the scope in which the new declaration will
9645 be placed. For example, on:
9647 void S::f() { ... }
9649 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9650 should not be `S'. */
9652 tree
9653 grokdeclarator (tree declarator,
9654 tree declspecs,
9655 enum decl_context decl_context,
9656 int initialized,
9657 tree* attrlist)
9659 RID_BIT_TYPE specbits;
9660 int nclasses = 0;
9661 tree spec;
9662 tree type = NULL_TREE;
9663 int longlong = 0;
9664 int type_quals;
9665 int virtualp, explicitp, friendp, inlinep, staticp;
9666 int explicit_int = 0;
9667 int explicit_char = 0;
9668 int defaulted_int = 0;
9669 int extern_langp = 0;
9670 tree dependant_name = NULL_TREE;
9672 tree typedef_decl = NULL_TREE;
9673 const char *name;
9674 tree typedef_type = NULL_TREE;
9675 int funcdef_flag = 0;
9676 enum tree_code innermost_code = ERROR_MARK;
9677 int bitfield = 0;
9678 #if 0
9679 /* See the code below that used this. */
9680 tree decl_attr = NULL_TREE;
9681 #endif
9683 /* Keep track of what sort of function is being processed
9684 so that we can warn about default return values, or explicit
9685 return values which do not match prescribed defaults. */
9686 special_function_kind sfk = sfk_none;
9688 tree dname = NULL_TREE;
9689 tree ctype = current_class_type;
9690 tree ctor_return_type = NULL_TREE;
9691 enum overload_flags flags = NO_SPECIAL;
9692 tree quals = NULL_TREE;
9693 tree raises = NULL_TREE;
9694 int template_count = 0;
9695 tree in_namespace = NULL_TREE;
9696 tree returned_attrs = NULL_TREE;
9697 tree scope = NULL_TREE;
9699 RIDBIT_RESET_ALL (specbits);
9700 if (decl_context == FUNCDEF)
9701 funcdef_flag = 1, decl_context = NORMAL;
9702 else if (decl_context == MEMFUNCDEF)
9703 funcdef_flag = -1, decl_context = FIELD;
9704 else if (decl_context == BITFIELD)
9705 bitfield = 1, decl_context = FIELD;
9707 /* Look inside a declarator for the name being declared
9708 and get it as a string, for an error message. */
9710 tree *next = &declarator;
9711 register tree decl;
9712 name = NULL;
9714 while (next && *next)
9716 decl = *next;
9717 switch (TREE_CODE (decl))
9719 case TREE_LIST:
9720 /* For attributes. */
9721 next = &TREE_VALUE (decl);
9722 break;
9724 case COND_EXPR:
9725 ctype = NULL_TREE;
9726 next = &TREE_OPERAND (decl, 0);
9727 break;
9729 case BIT_NOT_EXPR: /* For C++ destructors! */
9731 tree name = TREE_OPERAND (decl, 0);
9732 tree rename = NULL_TREE;
9734 my_friendly_assert (flags == NO_SPECIAL, 152);
9735 flags = DTOR_FLAG;
9736 sfk = sfk_destructor;
9737 if (TYPE_P (name))
9738 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9739 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9740 if (ctype == NULL_TREE)
9742 if (current_class_type == NULL_TREE)
9744 error ("destructors must be member functions");
9745 flags = NO_SPECIAL;
9747 else
9749 tree t = constructor_name (current_class_type);
9750 if (t != name)
9751 rename = t;
9754 else
9756 tree t = constructor_name (ctype);
9757 if (t != name)
9758 rename = t;
9761 if (rename)
9763 error ("destructor `%T' must match class name `%T'",
9764 name, rename);
9765 TREE_OPERAND (decl, 0) = rename;
9767 next = &name;
9769 break;
9771 case ADDR_EXPR: /* C++ reference declaration */
9772 /* Fall through. */
9773 case ARRAY_REF:
9774 case INDIRECT_REF:
9775 ctype = NULL_TREE;
9776 innermost_code = TREE_CODE (decl);
9777 next = &TREE_OPERAND (decl, 0);
9778 break;
9780 case CALL_EXPR:
9781 innermost_code = TREE_CODE (decl);
9782 if (decl_context == FIELD && ctype == NULL_TREE)
9783 ctype = current_class_type;
9784 if (ctype
9785 && TREE_OPERAND (decl, 0)
9786 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9787 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9788 ctype)))
9789 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9790 next = &TREE_OPERAND (decl, 0);
9791 decl = *next;
9792 if (ctype != NULL_TREE
9793 && decl != NULL_TREE && flags != DTOR_FLAG
9794 && constructor_name_p (decl, ctype))
9796 sfk = sfk_constructor;
9797 ctor_return_type = ctype;
9799 ctype = NULL_TREE;
9800 break;
9802 case TEMPLATE_ID_EXPR:
9804 tree fns = TREE_OPERAND (decl, 0);
9806 dname = fns;
9807 if (TREE_CODE (dname) == COMPONENT_REF)
9808 dname = TREE_OPERAND (dname, 1);
9809 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9811 my_friendly_assert (is_overloaded_fn (dname),
9812 19990331);
9813 dname = DECL_NAME (get_first_fn (dname));
9816 /* Fall through. */
9818 case IDENTIFIER_NODE:
9819 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9820 dname = decl;
9822 next = 0;
9824 if (C_IS_RESERVED_WORD (dname))
9826 error ("declarator-id missing; using reserved word `%D'",
9827 dname);
9828 name = IDENTIFIER_POINTER (dname);
9830 else if (!IDENTIFIER_TYPENAME_P (dname))
9831 name = IDENTIFIER_POINTER (dname);
9832 else
9834 my_friendly_assert (flags == NO_SPECIAL, 154);
9835 flags = TYPENAME_FLAG;
9836 ctor_return_type = TREE_TYPE (dname);
9837 sfk = sfk_conversion;
9838 if (IDENTIFIER_GLOBAL_VALUE (dname)
9839 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9840 == TYPE_DECL))
9841 name = IDENTIFIER_POINTER (dname);
9842 else
9843 name = "<invalid operator>";
9845 break;
9847 /* C++ extension */
9848 case SCOPE_REF:
9850 /* Perform error checking, and decide on a ctype. */
9851 tree cname = TREE_OPERAND (decl, 0);
9852 if (cname == NULL_TREE)
9853 ctype = NULL_TREE;
9854 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9856 ctype = NULL_TREE;
9857 in_namespace = TREE_OPERAND (decl, 0);
9859 else if (! is_aggr_type (cname, 1))
9860 ctype = NULL_TREE;
9861 /* Must test TREE_OPERAND (decl, 1), in case user gives
9862 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9863 else if (TREE_OPERAND (decl, 1)
9864 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9865 ctype = cname;
9866 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9867 || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9869 /* This might be declaring a member of a template
9870 parm to be a friend. */
9871 ctype = cname;
9872 dependant_name = TREE_OPERAND (decl, 1);
9874 else if (ctype == NULL_TREE)
9875 ctype = cname;
9876 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9878 else
9880 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9882 error ("type `%T' is not derived from type `%T'",
9883 cname, ctype);
9884 ctype = NULL_TREE;
9886 else
9887 ctype = cname;
9890 /* It is valid to write:
9892 class C { void f(); };
9893 typedef C D;
9894 void D::f();
9896 The standard is not clear about whether `typedef const C D' is
9897 legal; as of 2002-09-15 the committee is considering
9898 that question. EDG 3.0 allows that syntax.
9899 Therefore, we do as well. */
9900 if (ctype)
9901 ctype = TYPE_MAIN_VARIANT (ctype);
9902 /* Update the declarator so that when we process it
9903 again the correct type is present. */
9904 TREE_OPERAND (decl, 0) = ctype;
9906 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9907 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9908 ctype))
9909 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9910 next = &TREE_OPERAND (decl, 1);
9911 decl = *next;
9912 if (ctype)
9914 tree name = decl;
9916 if (TREE_CODE (name) == BIT_NOT_EXPR)
9917 name = TREE_OPERAND (name, 0);
9919 if (!constructor_name_p (decl, ctype))
9921 else if (decl == name)
9923 sfk = sfk_constructor;
9924 ctor_return_type = ctype;
9926 else
9928 sfk = sfk_destructor;
9929 ctor_return_type = ctype;
9930 flags = DTOR_FLAG;
9931 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9932 next = &TREE_OPERAND (decl, 0);
9936 break;
9938 case ERROR_MARK:
9939 next = 0;
9940 break;
9942 case TYPE_DECL:
9943 /* Parse error puts this typespec where
9944 a declarator should go. */
9945 error ("`%T' specified as declarator-id", DECL_NAME (decl));
9946 if (TREE_TYPE (decl) == current_class_type)
9947 error (" perhaps you want `%T' for a constructor",
9948 current_class_name);
9949 dname = DECL_NAME (decl);
9950 name = IDENTIFIER_POINTER (dname);
9952 /* Avoid giving two errors for this. */
9953 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9955 declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
9956 *next = dname;
9957 next = 0;
9958 break;
9960 case BASELINK:
9961 next = &BASELINK_FUNCTIONS (decl);
9962 break;
9964 case TEMPLATE_DECL:
9965 /* Sometimes, we see a template-name used as part of a
9966 decl-specifier like in
9967 std::allocator alloc;
9968 Handle that gracefully. */
9969 error ("invalid use of template-name '%E' in a declarator", decl);
9970 return error_mark_node;
9971 break;
9973 default:
9974 my_friendly_assert (0, 20020917);
9979 /* A function definition's declarator must have the form of
9980 a function declarator. */
9982 if (funcdef_flag && innermost_code != CALL_EXPR)
9983 return 0;
9985 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9986 && innermost_code != CALL_EXPR
9987 && ! (ctype && declspecs == NULL_TREE))
9989 error ("declaration of `%D' as non-function", dname);
9990 return void_type_node;
9993 /* Anything declared one level down from the top level
9994 must be one of the parameters of a function
9995 (because the body is at least two levels down). */
9997 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9998 by not allowing C++ class definitions to specify their parameters
9999 with xdecls (must be spec.d in the parmlist).
10001 Since we now wait to push a class scope until we are sure that
10002 we are in a legitimate method context, we must set oldcname
10003 explicitly (since current_class_name is not yet alive).
10005 We also want to avoid calling this a PARM if it is in a namespace. */
10007 if (decl_context == NORMAL && !toplevel_bindings_p ())
10009 struct cp_binding_level *b = current_binding_level;
10010 current_binding_level = b->level_chain;
10011 if (current_binding_level != 0 && toplevel_bindings_p ())
10012 decl_context = PARM;
10013 current_binding_level = b;
10016 if (name == NULL)
10017 name = decl_context == PARM ? "parameter" : "type name";
10019 /* Look through the decl specs and record which ones appear.
10020 Some typespecs are defined as built-in typenames.
10021 Others, the ones that are modifiers of other types,
10022 are represented by bits in SPECBITS: set the bits for
10023 the modifiers that appear. Storage class keywords are also in SPECBITS.
10025 If there is a typedef name or a type, store the type in TYPE.
10026 This includes builtin typedefs such as `int'.
10028 Set EXPLICIT_INT if the type is `int' or `char' and did not
10029 come from a user typedef.
10031 Set LONGLONG if `long' is mentioned twice.
10033 For C++, constructors and destructors have their own fast treatment. */
10035 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10037 register int i;
10038 register tree id;
10040 /* Certain parse errors slip through. For example,
10041 `int class;' is not caught by the parser. Try
10042 weakly to recover here. */
10043 if (TREE_CODE (spec) != TREE_LIST)
10044 return 0;
10046 id = TREE_VALUE (spec);
10048 /* If the entire declaration is itself tagged as deprecated then
10049 suppress reports of deprecated items. */
10050 if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10052 if (deprecated_state != DEPRECATED_SUPPRESS)
10053 warn_deprecated_use (id);
10056 if (TREE_CODE (id) == IDENTIFIER_NODE)
10058 if (id == ridpointers[(int) RID_INT]
10059 || id == ridpointers[(int) RID_CHAR]
10060 || id == ridpointers[(int) RID_BOOL]
10061 || id == ridpointers[(int) RID_WCHAR])
10063 if (type)
10065 if (id == ridpointers[(int) RID_BOOL])
10066 error ("`bool' is now a keyword");
10067 else
10068 error ("extraneous `%T' ignored", id);
10070 else
10072 if (id == ridpointers[(int) RID_INT])
10073 explicit_int = 1;
10074 else if (id == ridpointers[(int) RID_CHAR])
10075 explicit_char = 1;
10076 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10078 goto found;
10080 /* C++ aggregate types. */
10081 if (IDENTIFIER_HAS_TYPE_VALUE (id))
10083 if (type)
10084 error ("multiple declarations `%T' and `%T'", type, id);
10085 else
10086 type = IDENTIFIER_TYPE_VALUE (id);
10087 goto found;
10090 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10092 if (ridpointers[i] == id)
10094 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10096 if (pedantic && ! in_system_header && warn_long_long)
10097 pedwarn ("ISO C++ does not support `long long'");
10098 if (longlong)
10099 error ("`long long long' is too long for GCC");
10100 else
10101 longlong = 1;
10103 else if (RIDBIT_SETP (i, specbits))
10104 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10106 /* Diagnose "__thread extern" or "__thread static". */
10107 if (RIDBIT_SETP (RID_THREAD, specbits))
10109 if (i == (int)RID_EXTERN)
10110 error ("`__thread' before `extern'");
10111 else if (i == (int)RID_STATIC)
10112 error ("`__thread' before `static'");
10115 if (i == (int)RID_EXTERN
10116 && TREE_PURPOSE (spec) == error_mark_node)
10117 /* This extern was part of a language linkage. */
10118 extern_langp = 1;
10120 RIDBIT_SET (i, specbits);
10121 goto found;
10125 else if (TREE_CODE (id) == TYPE_DECL)
10127 if (type)
10128 error ("multiple declarations `%T' and `%T'", type,
10129 TREE_TYPE (id));
10130 else
10132 type = TREE_TYPE (id);
10133 TREE_VALUE (spec) = type;
10134 typedef_decl = id;
10136 goto found;
10138 if (type)
10139 error ("two or more data types in declaration of `%s'", name);
10140 else if (TREE_CODE (id) == IDENTIFIER_NODE)
10142 register tree t = lookup_name (id, 1);
10143 if (!t || TREE_CODE (t) != TYPE_DECL)
10144 error ("`%s' fails to be a typedef or built in type",
10145 IDENTIFIER_POINTER (id));
10146 else
10148 type = TREE_TYPE (t);
10149 typedef_decl = t;
10152 else if (id != error_mark_node)
10153 /* Can't change CLASS nodes into RECORD nodes here! */
10154 type = id;
10156 found: ;
10159 #if 0
10160 /* See the code below that used this. */
10161 if (typedef_decl)
10162 decl_attr = DECL_ATTRIBUTES (typedef_decl);
10163 #endif
10164 typedef_type = type;
10166 /* No type at all: default to `int', and set DEFAULTED_INT
10167 because it was not a user-defined typedef. */
10169 if (type == NULL_TREE
10170 && (RIDBIT_SETP (RID_SIGNED, specbits)
10171 || RIDBIT_SETP (RID_UNSIGNED, specbits)
10172 || RIDBIT_SETP (RID_LONG, specbits)
10173 || RIDBIT_SETP (RID_SHORT, specbits)))
10175 /* These imply 'int'. */
10176 type = integer_type_node;
10177 defaulted_int = 1;
10180 if (sfk != sfk_none)
10181 type = check_special_function_return_type (sfk, type,
10182 ctor_return_type);
10183 else if (type == NULL_TREE)
10185 int is_main;
10187 explicit_int = -1;
10189 /* We handle `main' specially here, because 'main () { }' is so
10190 common. With no options, it is allowed. With -Wreturn-type,
10191 it is a warning. It is only an error with -pedantic-errors. */
10192 is_main = (funcdef_flag
10193 && dname && MAIN_NAME_P (dname)
10194 && ctype == NULL_TREE
10195 && in_namespace == NULL_TREE
10196 && current_namespace == global_namespace);
10198 if (in_system_header || flag_ms_extensions)
10199 /* Allow it, sigh. */;
10200 else if (pedantic || ! is_main)
10201 pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10202 name);
10203 else if (warn_return_type)
10204 warning ("ISO C++ forbids declaration of `%s' with no type",
10205 name);
10207 type = integer_type_node;
10210 ctype = NULL_TREE;
10212 /* Now process the modifiers that were specified
10213 and check for invalid combinations. */
10215 /* Long double is a special combination. */
10217 if (RIDBIT_SETP (RID_LONG, specbits)
10218 && TYPE_MAIN_VARIANT (type) == double_type_node)
10220 RIDBIT_RESET (RID_LONG, specbits);
10221 type = build_qualified_type (long_double_type_node,
10222 cp_type_quals (type));
10225 /* Check all other uses of type modifiers. */
10227 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10228 || RIDBIT_SETP (RID_SIGNED, specbits)
10229 || RIDBIT_SETP (RID_LONG, specbits)
10230 || RIDBIT_SETP (RID_SHORT, specbits))
10232 int ok = 0;
10234 if (TREE_CODE (type) == REAL_TYPE)
10235 error ("short, signed or unsigned invalid for `%s'", name);
10236 else if (TREE_CODE (type) != INTEGER_TYPE)
10237 error ("long, short, signed or unsigned invalid for `%s'", name);
10238 else if (RIDBIT_SETP (RID_LONG, specbits)
10239 && RIDBIT_SETP (RID_SHORT, specbits))
10240 error ("long and short specified together for `%s'", name);
10241 else if ((RIDBIT_SETP (RID_LONG, specbits)
10242 || RIDBIT_SETP (RID_SHORT, specbits))
10243 && explicit_char)
10244 error ("long or short specified with char for `%s'", name);
10245 else if ((RIDBIT_SETP (RID_LONG, specbits)
10246 || RIDBIT_SETP (RID_SHORT, specbits))
10247 && TREE_CODE (type) == REAL_TYPE)
10248 error ("long or short specified with floating type for `%s'", name);
10249 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10250 && RIDBIT_SETP (RID_UNSIGNED, specbits))
10251 error ("signed and unsigned given together for `%s'", name);
10252 else
10254 ok = 1;
10255 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10257 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10258 name);
10259 if (flag_pedantic_errors)
10260 ok = 0;
10264 /* Discard the type modifiers if they are invalid. */
10265 if (! ok)
10267 RIDBIT_RESET (RID_UNSIGNED, specbits);
10268 RIDBIT_RESET (RID_SIGNED, specbits);
10269 RIDBIT_RESET (RID_LONG, specbits);
10270 RIDBIT_RESET (RID_SHORT, specbits);
10271 longlong = 0;
10275 if (RIDBIT_SETP (RID_COMPLEX, specbits)
10276 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10278 error ("complex invalid for `%s'", name);
10279 RIDBIT_RESET (RID_COMPLEX, specbits);
10282 /* Decide whether an integer type is signed or not.
10283 Optionally treat bitfields as signed by default. */
10284 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10285 /* [class.bit]
10287 It is implementation-defined whether a plain (neither
10288 explicitly signed or unsigned) char, short, int, or long
10289 bit-field is signed or unsigned.
10291 Naturally, we extend this to long long as well. Note that
10292 this does not include wchar_t. */
10293 || (bitfield && !flag_signed_bitfields
10294 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10295 /* A typedef for plain `int' without `signed' can be
10296 controlled just like plain `int', but a typedef for
10297 `signed int' cannot be so controlled. */
10298 && !(typedef_decl
10299 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10300 && (TREE_CODE (type) == INTEGER_TYPE
10301 || TREE_CODE (type) == CHAR_TYPE)
10302 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10304 if (longlong)
10305 type = long_long_unsigned_type_node;
10306 else if (RIDBIT_SETP (RID_LONG, specbits))
10307 type = long_unsigned_type_node;
10308 else if (RIDBIT_SETP (RID_SHORT, specbits))
10309 type = short_unsigned_type_node;
10310 else if (type == char_type_node)
10311 type = unsigned_char_type_node;
10312 else if (typedef_decl)
10313 type = c_common_unsigned_type (type);
10314 else
10315 type = unsigned_type_node;
10317 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10318 && type == char_type_node)
10319 type = signed_char_type_node;
10320 else if (longlong)
10321 type = long_long_integer_type_node;
10322 else if (RIDBIT_SETP (RID_LONG, specbits))
10323 type = long_integer_type_node;
10324 else if (RIDBIT_SETP (RID_SHORT, specbits))
10325 type = short_integer_type_node;
10327 if (RIDBIT_SETP (RID_COMPLEX, specbits))
10329 /* If we just have "complex", it is equivalent to
10330 "complex double", but if any modifiers at all are specified it is
10331 the complex form of TYPE. E.g, "complex short" is
10332 "complex short int". */
10334 if (defaulted_int && ! longlong
10335 && ! (RIDBIT_SETP (RID_LONG, specbits)
10336 || RIDBIT_SETP (RID_SHORT, specbits)
10337 || RIDBIT_SETP (RID_SIGNED, specbits)
10338 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10339 type = complex_double_type_node;
10340 else if (type == integer_type_node)
10341 type = complex_integer_type_node;
10342 else if (type == float_type_node)
10343 type = complex_float_type_node;
10344 else if (type == double_type_node)
10345 type = complex_double_type_node;
10346 else if (type == long_double_type_node)
10347 type = complex_long_double_type_node;
10348 else
10349 type = build_complex_type (type);
10352 type_quals = TYPE_UNQUALIFIED;
10353 if (RIDBIT_SETP (RID_CONST, specbits))
10354 type_quals |= TYPE_QUAL_CONST;
10355 if (RIDBIT_SETP (RID_VOLATILE, specbits))
10356 type_quals |= TYPE_QUAL_VOLATILE;
10357 if (RIDBIT_SETP (RID_RESTRICT, specbits))
10358 type_quals |= TYPE_QUAL_RESTRICT;
10359 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10360 error ("qualifiers are not allowed on declaration of `operator %T'",
10361 ctor_return_type);
10363 type_quals |= cp_type_quals (type);
10364 type = cp_build_qualified_type_real
10365 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10366 ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10367 /* We might have ignored or rejected some of the qualifiers. */
10368 type_quals = cp_type_quals (type);
10370 staticp = 0;
10371 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10372 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10373 RIDBIT_RESET (RID_VIRTUAL, specbits);
10374 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10375 RIDBIT_RESET (RID_EXPLICIT, specbits);
10377 if (RIDBIT_SETP (RID_STATIC, specbits))
10378 staticp = 1 + (decl_context == FIELD);
10380 if (virtualp && staticp == 2)
10382 error ("member `%D' cannot be declared both virtual and static",
10383 dname);
10384 staticp = 0;
10386 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10387 RIDBIT_RESET (RID_FRIEND, specbits);
10389 if (dependant_name && !friendp)
10391 error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10392 return void_type_node;
10395 /* Warn if two storage classes are given. Default to `auto'. */
10397 if (RIDBIT_ANY_SET (specbits))
10399 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10400 if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10401 if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10402 if (decl_context == PARM && nclasses > 0)
10403 error ("storage class specifiers invalid in parameter declarations");
10404 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10406 if (decl_context == PARM)
10407 error ("typedef declaration invalid in parameter declaration");
10408 nclasses++;
10410 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10411 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10412 if (!nclasses && !friendp && extern_langp)
10413 nclasses++;
10416 /* Give error if `virtual' is used outside of class declaration. */
10417 if (virtualp
10418 && (current_class_name == NULL_TREE || decl_context != FIELD))
10420 error ("virtual outside class declaration");
10421 virtualp = 0;
10424 /* Static anonymous unions are dealt with here. */
10425 if (staticp && decl_context == TYPENAME
10426 && TREE_CODE (declspecs) == TREE_LIST
10427 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10428 decl_context = FIELD;
10430 /* Warn about storage classes that are invalid for certain
10431 kinds of declarations (parameters, typenames, etc.). */
10433 /* "static __thread" and "extern __thread" are allowed. */
10434 if (nclasses == 2
10435 && RIDBIT_SETP (RID_THREAD, specbits)
10436 && (RIDBIT_SETP (RID_EXTERN, specbits)
10437 || RIDBIT_SETP (RID_STATIC, specbits)))
10438 nclasses = 1;
10440 if (nclasses > 1)
10441 error ("multiple storage classes in declaration of `%s'", name);
10442 else if (decl_context != NORMAL && nclasses > 0)
10444 if ((decl_context == PARM || decl_context == CATCHPARM)
10445 && (RIDBIT_SETP (RID_REGISTER, specbits)
10446 || RIDBIT_SETP (RID_AUTO, specbits)))
10448 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10450 else if (decl_context == FIELD
10451 /* C++ allows static class elements */
10452 && RIDBIT_SETP (RID_STATIC, specbits))
10453 /* C++ also allows inlines and signed and unsigned elements,
10454 but in those cases we don't come in here. */
10456 else
10458 if (decl_context == FIELD)
10460 tree tmp = NULL_TREE;
10461 register int op = 0;
10463 if (declarator)
10465 /* Avoid trying to get an operand off an identifier node. */
10466 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10467 tmp = declarator;
10468 else
10469 tmp = TREE_OPERAND (declarator, 0);
10470 op = IDENTIFIER_OPNAME_P (tmp);
10471 if (IDENTIFIER_TYPENAME_P (tmp))
10473 if (IDENTIFIER_GLOBAL_VALUE (tmp)
10474 && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10475 == TYPE_DECL))
10476 name = IDENTIFIER_POINTER (tmp);
10477 else
10478 name = "<invalid operator>";
10481 error ("storage class specified for %s `%s'",
10482 op ? "member operator" : "field",
10483 name);
10485 else
10487 if (decl_context == PARM || decl_context == CATCHPARM)
10488 error ("storage class specified for parameter `%s'", name);
10489 else
10490 error ("storage class specified for typename");
10492 RIDBIT_RESET (RID_REGISTER, specbits);
10493 RIDBIT_RESET (RID_AUTO, specbits);
10494 RIDBIT_RESET (RID_EXTERN, specbits);
10495 RIDBIT_RESET (RID_THREAD, specbits);
10498 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10500 if (toplevel_bindings_p ())
10502 /* It's common practice (and completely valid) to have a const
10503 be initialized and declared extern. */
10504 if (!(type_quals & TYPE_QUAL_CONST))
10505 warning ("`%s' initialized and declared `extern'", name);
10507 else
10508 error ("`%s' has both `extern' and initializer", name);
10510 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10511 && ! toplevel_bindings_p ())
10512 error ("nested function `%s' declared `extern'", name);
10513 else if (toplevel_bindings_p ())
10515 if (RIDBIT_SETP (RID_AUTO, specbits))
10516 error ("top-level declaration of `%s' specifies `auto'", name);
10518 else if (RIDBIT_SETP (RID_THREAD, specbits)
10519 && !RIDBIT_SETP (RID_EXTERN, specbits)
10520 && !RIDBIT_SETP (RID_STATIC, specbits))
10522 error ("function-scope `%s' implicitly auto and declared `__thread'",
10523 name);
10524 RIDBIT_RESET (RID_THREAD, specbits);
10527 if (nclasses > 0 && friendp)
10528 error ("storage class specifiers invalid in friend function declarations");
10530 scope = get_scope_of_declarator (declarator);
10532 /* Now figure out the structure of the declarator proper.
10533 Descend through it, creating more complex types, until we reach
10534 the declared identifier (or NULL_TREE, in an abstract declarator). */
10536 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10537 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10539 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10540 an INDIRECT_REF (for *...),
10541 a CALL_EXPR (for ...(...)),
10542 an identifier (for the name being declared)
10543 or a null pointer (for the place in an absolute declarator
10544 where the name was omitted).
10545 For the last two cases, we have just exited the loop.
10547 For C++ it could also be
10548 a SCOPE_REF (for class :: ...). In this case, we have converted
10549 sensible names to types, and those are the values we use to
10550 qualify the member name.
10551 an ADDR_EXPR (for &...),
10552 a BIT_NOT_EXPR (for destructors)
10554 At this point, TYPE is the type of elements of an array,
10555 or for a function to return, or for a pointer to point to.
10556 After this sequence of ifs, TYPE is the type of the
10557 array or function or pointer, and DECLARATOR has had its
10558 outermost layer removed. */
10560 if (type == error_mark_node)
10562 if (declarator == error_mark_node)
10563 return error_mark_node;
10564 else if (TREE_CODE (declarator) == SCOPE_REF)
10565 declarator = TREE_OPERAND (declarator, 1);
10566 else
10567 declarator = TREE_OPERAND (declarator, 0);
10568 continue;
10570 if (quals != NULL_TREE
10571 && (declarator == NULL_TREE
10572 || TREE_CODE (declarator) != SCOPE_REF))
10574 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10575 ctype = TYPE_METHOD_BASETYPE (type);
10576 if (ctype != NULL_TREE)
10578 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10579 grok_method_quals (ctype, dummy, quals);
10580 type = TREE_TYPE (dummy);
10581 quals = NULL_TREE;
10585 switch (TREE_CODE (declarator))
10587 case TREE_LIST:
10589 /* We encode a declarator with embedded attributes using
10590 a TREE_LIST. */
10591 tree attrs = TREE_PURPOSE (declarator);
10592 tree inner_decl;
10593 int attr_flags;
10595 declarator = TREE_VALUE (declarator);
10596 inner_decl = declarator;
10597 while (inner_decl != NULL_TREE
10598 && TREE_CODE (inner_decl) == TREE_LIST)
10599 inner_decl = TREE_VALUE (inner_decl);
10600 attr_flags = 0;
10601 if (inner_decl == NULL_TREE
10602 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10603 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10604 if (TREE_CODE (inner_decl) == CALL_EXPR)
10605 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10606 if (TREE_CODE (inner_decl) == ARRAY_REF)
10607 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10608 returned_attrs = decl_attributes (&type,
10609 chainon (returned_attrs, attrs),
10610 attr_flags);
10612 break;
10614 case ARRAY_REF:
10616 tree size = TREE_OPERAND (declarator, 1);
10617 declarator = TREE_OPERAND (declarator, 0);
10619 type = create_array_type_for_decl (dname, type, size);
10621 ctype = NULL_TREE;
10623 break;
10625 case CALL_EXPR:
10627 tree arg_types;
10628 int funcdecl_p;
10629 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10630 tree inner_decl = TREE_OPERAND (declarator, 0);
10632 /* Declaring a function type.
10633 Make sure we have a valid type for the function to return. */
10635 /* We now know that the TYPE_QUALS don't apply to the
10636 decl, but to its return type. */
10637 type_quals = TYPE_UNQUALIFIED;
10639 /* Warn about some types functions can't return. */
10641 if (TREE_CODE (type) == FUNCTION_TYPE)
10643 error ("`%s' declared as function returning a function", name);
10644 type = integer_type_node;
10646 if (TREE_CODE (type) == ARRAY_TYPE)
10648 error ("`%s' declared as function returning an array", name);
10649 type = integer_type_node;
10652 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10653 inner_decl = TREE_OPERAND (inner_decl, 1);
10655 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10656 inner_decl = dname;
10658 /* Pick up type qualifiers which should be applied to `this'. */
10659 quals = CALL_DECLARATOR_QUALS (declarator);
10661 /* Pick up the exception specifications. */
10662 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10664 /* Say it's a definition only for the CALL_EXPR
10665 closest to the identifier. */
10666 funcdecl_p
10667 = inner_decl
10668 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10669 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10670 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10672 if (ctype == NULL_TREE
10673 && decl_context == FIELD
10674 && funcdecl_p
10675 && (friendp == 0 || dname == current_class_name))
10676 ctype = current_class_type;
10678 if (ctype && sfk == sfk_conversion)
10679 TYPE_HAS_CONVERSION (ctype) = 1;
10680 if (ctype && constructor_name_p (dname, ctype))
10682 /* We are within a class's scope. If our declarator name
10683 is the same as the class name, and we are defining
10684 a function, then it is a constructor/destructor, and
10685 therefore returns a void type. */
10687 if (flags == DTOR_FLAG)
10689 /* ISO C++ 12.4/2. A destructor may not be
10690 declared const or volatile. A destructor may
10691 not be static. */
10692 if (staticp == 2)
10693 error ("destructor cannot be static member function");
10694 if (quals)
10696 error ("destructors may not be `%s'",
10697 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10698 quals = NULL_TREE;
10700 if (decl_context == FIELD)
10702 if (! member_function_or_else (ctype,
10703 current_class_type,
10704 flags))
10705 return void_type_node;
10708 else /* It's a constructor. */
10710 if (explicitp == 1)
10711 explicitp = 2;
10712 /* ISO C++ 12.1. A constructor may not be
10713 declared const or volatile. A constructor may
10714 not be virtual. A constructor may not be
10715 static. */
10716 if (staticp == 2)
10717 error ("constructor cannot be static member function");
10718 if (virtualp)
10720 pedwarn ("constructors cannot be declared virtual");
10721 virtualp = 0;
10723 if (quals)
10725 error ("constructors may not be `%s'",
10726 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10727 quals = NULL_TREE;
10730 RID_BIT_TYPE tmp_bits;
10731 memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10732 RIDBIT_RESET (RID_INLINE, tmp_bits);
10733 RIDBIT_RESET (RID_STATIC, tmp_bits);
10734 if (RIDBIT_ANY_SET (tmp_bits))
10735 error ("return value type specifier for constructor ignored");
10737 if (decl_context == FIELD)
10739 if (! member_function_or_else (ctype,
10740 current_class_type,
10741 flags))
10742 return void_type_node;
10743 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10744 if (sfk != sfk_constructor)
10745 return NULL_TREE;
10748 if (decl_context == FIELD)
10749 staticp = 0;
10751 else if (friendp)
10753 if (initialized)
10754 error ("can't initialize friend function `%s'", name);
10755 if (virtualp)
10757 /* Cannot be both friend and virtual. */
10758 error ("virtual functions cannot be friends");
10759 RIDBIT_RESET (RID_FRIEND, specbits);
10760 friendp = 0;
10762 if (decl_context == NORMAL)
10763 error ("friend declaration not in class definition");
10764 if (current_function_decl && funcdef_flag)
10765 error ("can't define friend function `%s' in a local class definition",
10766 name);
10769 /* Construct the function type and go to the next
10770 inner layer of declarator. */
10772 declarator = TREE_OPERAND (declarator, 0);
10774 arg_types = grokparms (inner_parms);
10776 if (declarator && flags == DTOR_FLAG)
10778 /* A destructor declared in the body of a class will
10779 be represented as a BIT_NOT_EXPR. But, we just
10780 want the underlying IDENTIFIER. */
10781 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10782 declarator = TREE_OPERAND (declarator, 0);
10784 if (arg_types != void_list_node)
10786 error ("destructors may not have parameters");
10787 arg_types = void_list_node;
10788 last_function_parms = NULL_TREE;
10792 /* ANSI says that `const int foo ();'
10793 does not make the function foo const. */
10794 type = build_function_type (type, arg_types);
10796 break;
10798 case ADDR_EXPR:
10799 case INDIRECT_REF:
10800 /* Filter out pointers-to-references and references-to-references.
10801 We can get these if a TYPE_DECL is used. */
10803 if (TREE_CODE (type) == REFERENCE_TYPE)
10805 error (TREE_CODE (declarator) == ADDR_EXPR
10806 ? "cannot declare reference to `%#T'"
10807 : "cannot declare pointer to `%#T'", type);
10808 type = TREE_TYPE (type);
10810 else if (VOID_TYPE_P (type)
10811 && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10812 error (ctype ? "cannot declare pointer to `%#T' member"
10813 : "cannot declare reference to `%#T'", type);
10815 /* Merge any constancy or volatility into the target type
10816 for the pointer. */
10818 /* We now know that the TYPE_QUALS don't apply to the decl,
10819 but to the target of the pointer. */
10820 type_quals = TYPE_UNQUALIFIED;
10822 if (TREE_CODE (declarator) == ADDR_EXPR)
10824 if (!VOID_TYPE_P (type))
10825 type = build_reference_type (type);
10827 else if (TREE_CODE (type) == METHOD_TYPE)
10828 type = build_ptrmemfunc_type (build_pointer_type (type));
10829 else if (ctype)
10830 type = build_ptrmem_type (ctype, type);
10831 else
10832 type = build_pointer_type (type);
10834 /* Process a list of type modifier keywords (such as
10835 const or volatile) that were given inside the `*' or `&'. */
10837 if (TREE_TYPE (declarator))
10839 register tree typemodlist;
10840 int erred = 0;
10841 int constp = 0;
10842 int volatilep = 0;
10843 int restrictp = 0;
10845 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10846 typemodlist = TREE_CHAIN (typemodlist))
10848 tree qualifier = TREE_VALUE (typemodlist);
10850 if (qualifier == ridpointers[(int) RID_CONST])
10852 constp++;
10853 type_quals |= TYPE_QUAL_CONST;
10855 else if (qualifier == ridpointers[(int) RID_VOLATILE])
10857 volatilep++;
10858 type_quals |= TYPE_QUAL_VOLATILE;
10860 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10862 restrictp++;
10863 type_quals |= TYPE_QUAL_RESTRICT;
10865 else if (!erred)
10867 erred = 1;
10868 error ("invalid type modifier within pointer declarator");
10871 if (constp > 1)
10872 pedwarn ("duplicate `const'");
10873 if (volatilep > 1)
10874 pedwarn ("duplicate `volatile'");
10875 if (restrictp > 1)
10876 pedwarn ("duplicate `restrict'");
10877 type = cp_build_qualified_type (type, type_quals);
10878 type_quals = cp_type_quals (type);
10880 declarator = TREE_OPERAND (declarator, 0);
10881 ctype = NULL_TREE;
10882 break;
10884 case SCOPE_REF:
10886 /* We have converted type names to NULL_TREE if the
10887 name was bogus, or to a _TYPE node, if not.
10889 The variable CTYPE holds the type we will ultimately
10890 resolve to. The code here just needs to build
10891 up appropriate member types. */
10892 tree sname = TREE_OPERAND (declarator, 1);
10893 tree t;
10895 /* Destructors can have their visibilities changed as well. */
10896 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10897 sname = TREE_OPERAND (sname, 0);
10899 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10901 /* We had a reference to a global decl, or
10902 perhaps we were given a non-aggregate typedef,
10903 in which case we cleared this out, and should just
10904 keep going as though it wasn't there. */
10905 declarator = sname;
10906 continue;
10908 ctype = TREE_OPERAND (declarator, 0);
10910 t = ctype;
10911 while (t != NULL_TREE && CLASS_TYPE_P (t))
10913 /* You're supposed to have one `template <...>'
10914 for every template class, but you don't need one
10915 for a full specialization. For example:
10917 template <class T> struct S{};
10918 template <> struct S<int> { void f(); };
10919 void S<int>::f () {}
10921 is correct; there shouldn't be a `template <>' for
10922 the definition of `S<int>::f'. */
10923 if (CLASSTYPE_TEMPLATE_INFO (t)
10924 && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
10925 || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10926 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
10927 template_count += 1;
10929 t = TYPE_MAIN_DECL (t);
10930 t = DECL_CONTEXT (t);
10933 if (sname == NULL_TREE)
10934 goto done_scoping;
10936 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10938 /* This is the `standard' use of the scoping operator:
10939 basetype :: member . */
10941 if (ctype == current_class_type)
10943 /* class A {
10944 void A::f ();
10947 Is this ill-formed? */
10949 if (pedantic)
10950 pedwarn ("extra qualification `%T::' on member `%s' ignored",
10951 ctype, name);
10953 else if (TREE_CODE (type) == FUNCTION_TYPE)
10955 if (current_class_type == NULL_TREE || friendp)
10956 type
10957 = build_method_type_directly (ctype,
10958 TREE_TYPE (type),
10959 TYPE_ARG_TYPES (type));
10960 else
10962 error ("cannot declare member function `%T::%s' within `%T'",
10963 ctype, name, current_class_type);
10964 return error_mark_node;
10967 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10968 || COMPLETE_TYPE_P (complete_type (ctype)))
10970 /* Have to move this code elsewhere in this function.
10971 this code is used for i.e., typedef int A::M; M *pm;
10973 It is? How? jason 10/2/94 */
10975 if (current_class_type)
10977 error ("cannot declare member `%T::%s' within `%T'",
10978 ctype, name, current_class_type);
10979 return void_type_node;
10982 else
10984 cxx_incomplete_type_error (NULL_TREE, ctype);
10985 return error_mark_node;
10988 declarator = sname;
10990 else if (TREE_CODE (sname) == SCOPE_REF)
10991 abort ();
10992 else
10994 done_scoping:
10995 declarator = TREE_OPERAND (declarator, 1);
10996 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10997 /* In this case, we will deal with it later. */
10999 else if (TREE_CODE (type) == FUNCTION_TYPE)
11000 type = build_method_type_directly (ctype,
11001 TREE_TYPE (type),
11002 TYPE_ARG_TYPES (type));
11005 break;
11007 case BIT_NOT_EXPR:
11008 declarator = TREE_OPERAND (declarator, 0);
11009 break;
11011 case BASELINK:
11012 declarator = BASELINK_FUNCTIONS (declarator);
11013 break;
11015 case RECORD_TYPE:
11016 case UNION_TYPE:
11017 case ENUMERAL_TYPE:
11018 declarator = NULL_TREE;
11019 break;
11021 case ERROR_MARK:
11022 declarator = NULL_TREE;
11023 break;
11025 default:
11026 abort ();
11030 if (returned_attrs)
11032 if (attrlist)
11033 *attrlist = chainon (returned_attrs, *attrlist);
11034 else
11035 attrlist = &returned_attrs;
11038 /* Now TYPE has the actual type. */
11040 /* Did array size calculations overflow? */
11042 if (TREE_CODE (type) == ARRAY_TYPE
11043 && COMPLETE_TYPE_P (type)
11044 && TREE_OVERFLOW (TYPE_SIZE (type)))
11046 error ("size of array `%s' is too large", name);
11047 /* If we proceed with the array type as it is, we'll eventually
11048 crash in tree_low_cst(). */
11049 type = error_mark_node;
11052 if ((decl_context == FIELD || decl_context == PARM)
11053 && !processing_template_decl
11054 && variably_modified_type_p (type))
11056 if (decl_context == FIELD)
11057 error ("data member may not have variably modified type `%T'", type);
11058 else
11059 error ("parameter may not have variably modified type `%T'", type);
11060 type = error_mark_node;
11063 if (explicitp == 1 || (explicitp && friendp))
11065 /* [dcl.fct.spec] The explicit specifier shall only be used in
11066 declarations of constructors within a class definition. */
11067 error ("only declarations of constructors can be `explicit'");
11068 explicitp = 0;
11071 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11073 if (decl_context != FIELD || friendp)
11075 error ("non-member `%s' cannot be declared `mutable'", name);
11076 RIDBIT_RESET (RID_MUTABLE, specbits);
11078 else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11080 error ("non-object member `%s' cannot be declared `mutable'", name);
11081 RIDBIT_RESET (RID_MUTABLE, specbits);
11083 else if (TREE_CODE (type) == FUNCTION_TYPE
11084 || TREE_CODE (type) == METHOD_TYPE)
11086 error ("function `%s' cannot be declared `mutable'", name);
11087 RIDBIT_RESET (RID_MUTABLE, specbits);
11089 else if (staticp)
11091 error ("static `%s' cannot be declared `mutable'", name);
11092 RIDBIT_RESET (RID_MUTABLE, specbits);
11094 else if (type_quals & TYPE_QUAL_CONST)
11096 error ("const `%s' cannot be declared `mutable'", name);
11097 RIDBIT_RESET (RID_MUTABLE, specbits);
11101 if (declarator == NULL_TREE
11102 || TREE_CODE (declarator) == IDENTIFIER_NODE
11103 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11104 && (TREE_CODE (type) == FUNCTION_TYPE
11105 || TREE_CODE (type) == METHOD_TYPE)))
11106 /* OK */;
11107 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11109 error ("template-id `%D' used as a declarator", declarator);
11110 declarator = dname;
11112 else
11113 /* Unexpected declarator format. */
11114 abort ();
11116 /* If this is declaring a typedef name, return a TYPE_DECL. */
11118 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11120 tree decl;
11122 /* Note that the grammar rejects storage classes
11123 in typenames, fields or parameters. */
11124 if (current_lang_name == lang_name_java)
11125 TYPE_FOR_JAVA (type) = 1;
11127 if (decl_context == FIELD)
11129 if (constructor_name_p (declarator, current_class_type))
11130 pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11131 declarator);
11132 decl = build_lang_decl (TYPE_DECL, declarator, type);
11134 else
11136 decl = build_decl (TYPE_DECL, declarator, type);
11137 if (in_namespace || ctype)
11138 error ("%Htypedef name may not be a nested-name-specifier",
11139 &DECL_SOURCE_LOCATION (decl));
11140 if (!current_function_decl)
11141 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11144 /* If the user declares "typedef struct {...} foo" then the
11145 struct will have an anonymous name. Fill that name in now.
11146 Nothing can refer to it, so nothing needs know about the name
11147 change. */
11148 if (type != error_mark_node
11149 && declarator
11150 && TYPE_NAME (type)
11151 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11152 && TYPE_ANONYMOUS_P (type)
11153 /* Don't do this if there are attributes. */
11154 && (!attrlist || !*attrlist)
11155 && cp_type_quals (type) == TYPE_UNQUALIFIED)
11157 tree oldname = TYPE_NAME (type);
11158 tree t;
11160 /* Replace the anonymous name with the real name everywhere. */
11161 lookup_tag_reverse (type, declarator);
11162 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11163 if (TYPE_NAME (t) == oldname)
11164 TYPE_NAME (t) = decl;
11166 if (TYPE_LANG_SPECIFIC (type))
11167 TYPE_WAS_ANONYMOUS (type) = 1;
11169 /* If this is a typedef within a template class, the nested
11170 type is a (non-primary) template. The name for the
11171 template needs updating as well. */
11172 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11173 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11174 = TYPE_IDENTIFIER (type);
11176 /* FIXME remangle member functions; member functions of a
11177 type with external linkage have external linkage. */
11180 if (quals)
11182 if (ctype == NULL_TREE)
11184 if (TREE_CODE (type) != METHOD_TYPE)
11185 error ("%Hinvalid type qualifier for non-member function type",
11186 &DECL_SOURCE_LOCATION (decl));
11187 else
11188 ctype = TYPE_METHOD_BASETYPE (type);
11190 if (ctype != NULL_TREE)
11191 grok_method_quals (ctype, decl, quals);
11194 if (RIDBIT_SETP (RID_SIGNED, specbits)
11195 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11196 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11198 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11199 inlinep, friendp, raises != NULL_TREE);
11201 return decl;
11204 /* Detect the case of an array type of unspecified size
11205 which came, as such, direct from a typedef name.
11206 We must copy the type, so that the array's domain can be
11207 individually set by the object's initializer. */
11209 if (type && typedef_type
11210 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11211 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11212 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11214 /* Detect where we're using a typedef of function type to declare a
11215 function. last_function_parms will not be set, so we must create
11216 it now. */
11218 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11220 tree decls = NULL_TREE;
11221 tree args;
11223 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11225 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11227 TREE_CHAIN (decl) = decls;
11228 decls = decl;
11231 last_function_parms = nreverse (decls);
11234 /* If this is a type name (such as, in a cast or sizeof),
11235 compute the type and return it now. */
11237 if (decl_context == TYPENAME)
11239 /* Note that the grammar rejects storage classes
11240 in typenames, fields or parameters. */
11241 if (type_quals != TYPE_UNQUALIFIED)
11242 type_quals = TYPE_UNQUALIFIED;
11244 /* Special case: "friend class foo" looks like a TYPENAME context. */
11245 if (friendp)
11247 if (type_quals != TYPE_UNQUALIFIED)
11249 error ("type qualifiers specified for friend class declaration");
11250 type_quals = TYPE_UNQUALIFIED;
11252 if (inlinep)
11254 error ("`inline' specified for friend class declaration");
11255 inlinep = 0;
11258 if (!current_aggr)
11260 /* Don't allow friend declaration without a class-key. */
11261 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11262 pedwarn ("template parameters cannot be friends");
11263 else if (TREE_CODE (type) == TYPENAME_TYPE)
11264 pedwarn ("friend declaration requires class-key, "
11265 "i.e. `friend class %T::%D'",
11266 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11267 else
11268 pedwarn ("friend declaration requires class-key, "
11269 "i.e. `friend %#T'",
11270 type);
11273 /* Only try to do this stuff if we didn't already give up. */
11274 if (type != integer_type_node)
11276 /* A friendly class? */
11277 if (current_class_type)
11278 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
11279 /*complain=*/true);
11280 else
11281 error ("trying to make class `%T' a friend of global scope",
11282 type);
11284 type = void_type_node;
11287 else if (quals)
11289 if (ctype == NULL_TREE)
11291 if (TREE_CODE (type) != METHOD_TYPE)
11292 error ("invalid qualifiers on non-member function type");
11293 else
11294 ctype = TYPE_METHOD_BASETYPE (type);
11296 if (ctype)
11298 tree dummy = build_decl (TYPE_DECL, declarator, type);
11299 grok_method_quals (ctype, dummy, quals);
11300 type = TREE_TYPE (dummy);
11304 return type;
11306 else if (declarator == NULL_TREE && decl_context != PARM
11307 && decl_context != CATCHPARM
11308 && TREE_CODE (type) != UNION_TYPE
11309 && ! bitfield)
11311 error ("abstract declarator `%T' used as declaration", type);
11312 declarator = make_anon_name ();
11315 /* `void' at top level (not within pointer)
11316 is allowed only in typedefs or type names.
11317 We don't complain about parms either, but that is because
11318 a better error message can be made later. */
11320 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11322 if (! declarator)
11323 error ("unnamed variable or field declared void");
11324 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11326 if (IDENTIFIER_OPNAME_P (declarator))
11327 abort ();
11328 else
11329 error ("variable or field `%s' declared void", name);
11331 else
11332 error ("variable or field declared void");
11333 type = integer_type_node;
11336 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11337 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11339 if (decl_context == PARM || decl_context == CATCHPARM)
11341 if (ctype || in_namespace)
11342 error ("cannot use `::' in parameter declaration");
11344 /* A parameter declared as an array of T is really a pointer to T.
11345 One declared as a function is really a pointer to a function.
11346 One declared as a member is really a pointer to member. */
11348 if (TREE_CODE (type) == ARRAY_TYPE)
11350 /* Transfer const-ness of array into that of type pointed to. */
11351 type = build_pointer_type (TREE_TYPE (type));
11352 type_quals = TYPE_UNQUALIFIED;
11354 else if (TREE_CODE (type) == FUNCTION_TYPE)
11355 type = build_pointer_type (type);
11359 register tree decl;
11361 if (decl_context == PARM)
11363 decl = cp_build_parm_decl (declarator, type);
11365 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11366 inlinep, friendp, raises != NULL_TREE);
11368 else if (decl_context == FIELD)
11370 /* The C99 flexible array extension. */
11371 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
11372 && TYPE_DOMAIN (type) == NULL_TREE)
11374 tree itype = compute_array_index_type (dname, integer_zero_node);
11375 type = build_cplus_array_type (TREE_TYPE (type), itype);
11378 if (type == error_mark_node)
11380 /* Happens when declaring arrays of sizes which
11381 are error_mark_node, for example. */
11382 decl = NULL_TREE;
11384 else if (in_namespace && !friendp)
11386 /* Something like struct S { int N::j; }; */
11387 error ("invalid use of `::'");
11388 decl = NULL_TREE;
11390 else if (TREE_CODE (type) == FUNCTION_TYPE)
11392 int publicp = 0;
11393 tree function_context;
11395 /* We catch the others as conflicts with the builtin
11396 typedefs. */
11397 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11399 error ("function `%D' cannot be declared friend",
11400 declarator);
11401 friendp = 0;
11404 if (friendp == 0)
11406 if (ctype == NULL_TREE)
11407 ctype = current_class_type;
11409 if (ctype == NULL_TREE)
11411 error ("can't make `%D' into a method -- not in a class",
11412 declarator);
11413 return void_type_node;
11416 /* ``A union may [ ... ] not [ have ] virtual functions.''
11417 ARM 9.5 */
11418 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11420 error ("function `%D' declared virtual inside a union",
11421 declarator);
11422 return void_type_node;
11425 if (declarator == ansi_opname (NEW_EXPR)
11426 || declarator == ansi_opname (VEC_NEW_EXPR)
11427 || declarator == ansi_opname (DELETE_EXPR)
11428 || declarator == ansi_opname (VEC_DELETE_EXPR))
11430 if (virtualp)
11432 error ("`%D' cannot be declared virtual, since it is always static",
11433 declarator);
11434 virtualp = 0;
11437 else if (staticp < 2)
11438 type = build_method_type_directly (ctype,
11439 TREE_TYPE (type),
11440 TYPE_ARG_TYPES (type));
11443 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11444 function_context = (ctype != NULL_TREE) ?
11445 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11446 publicp = (! friendp || ! staticp)
11447 && function_context == NULL_TREE;
11448 decl = grokfndecl (ctype, type,
11449 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11450 ? declarator : dname,
11451 declarator,
11452 virtualp, flags, quals, raises,
11453 friendp ? -1 : 0, friendp, publicp, inlinep,
11454 funcdef_flag, template_count, in_namespace);
11455 if (decl == NULL_TREE)
11456 return decl;
11457 #if 0
11458 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11459 /* The decl and setting of decl_attr is also turned off. */
11460 decl = build_decl_attribute_variant (decl, decl_attr);
11461 #endif
11463 /* [class.conv.ctor]
11465 A constructor declared without the function-specifier
11466 explicit that can be called with a single parameter
11467 specifies a conversion from the type of its first
11468 parameter to the type of its class. Such a constructor
11469 is called a converting constructor. */
11470 if (explicitp == 2)
11471 DECL_NONCONVERTING_P (decl) = 1;
11472 else if (DECL_CONSTRUCTOR_P (decl))
11474 /* The constructor can be called with exactly one
11475 parameter if there is at least one parameter, and
11476 any subsequent parameters have default arguments.
11477 Ignore any compiler-added parms. */
11478 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11480 if (arg_types == void_list_node
11481 || (arg_types
11482 && TREE_CHAIN (arg_types)
11483 && TREE_CHAIN (arg_types) != void_list_node
11484 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11485 DECL_NONCONVERTING_P (decl) = 1;
11488 else if (TREE_CODE (type) == METHOD_TYPE)
11490 /* We only get here for friend declarations of
11491 members of other classes. */
11492 /* All method decls are public, so tell grokfndecl to set
11493 TREE_PUBLIC, also. */
11494 decl = grokfndecl (ctype, type,
11495 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11496 ? declarator : dname,
11497 declarator,
11498 virtualp, flags, quals, raises,
11499 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11500 template_count, in_namespace);
11501 if (decl == NULL_TREE)
11502 return NULL_TREE;
11504 else if (!staticp && ! processing_template_decl
11505 && !COMPLETE_TYPE_P (complete_type (type))
11506 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11508 if (declarator)
11509 error ("field `%D' has incomplete type", declarator);
11510 else
11511 error ("name `%T' has incomplete type", type);
11513 /* If we're instantiating a template, tell them which
11514 instantiation made the field's type be incomplete. */
11515 if (current_class_type
11516 && TYPE_NAME (current_class_type)
11517 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11518 && declspecs && TREE_VALUE (declspecs)
11519 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11520 error (" in instantiation of template `%T'",
11521 current_class_type);
11523 type = error_mark_node;
11524 decl = NULL_TREE;
11526 else
11528 if (friendp)
11530 error ("`%s' is neither function nor member function; cannot be declared friend",
11531 IDENTIFIER_POINTER (declarator));
11532 friendp = 0;
11534 decl = NULL_TREE;
11537 if (friendp)
11539 /* Friends are treated specially. */
11540 if (ctype == current_class_type)
11541 warning ("member functions are implicitly friends of their class");
11542 else if (decl && DECL_NAME (decl))
11544 if (template_class_depth (current_class_type) == 0)
11546 decl = check_explicit_specialization
11547 (declarator, decl, template_count,
11548 2 * (funcdef_flag != 0) + 4);
11549 if (decl == error_mark_node)
11550 return error_mark_node;
11553 decl = do_friend (ctype, declarator, decl,
11554 last_function_parms, *attrlist,
11555 flags, quals, funcdef_flag);
11556 return decl;
11558 else
11559 return void_type_node;
11562 /* Structure field. It may not be a function, except for C++ */
11564 if (decl == NULL_TREE)
11566 if (initialized)
11568 if (!staticp)
11570 /* An attempt is being made to initialize a non-static
11571 member. But, from [class.mem]:
11573 4 A member-declarator can contain a
11574 constant-initializer only if it declares a static
11575 member (_class.static_) of integral or enumeration
11576 type, see _class.static.data_.
11578 This used to be relatively common practice, but
11579 the rest of the compiler does not correctly
11580 handle the initialization unless the member is
11581 static so we make it static below. */
11582 pedwarn ("ISO C++ forbids initialization of member `%D'",
11583 declarator);
11584 pedwarn ("making `%D' static", declarator);
11585 staticp = 1;
11588 if (uses_template_parms (type))
11589 /* We'll check at instantiation time. */
11591 else if (check_static_variable_definition (declarator,
11592 type))
11593 /* If we just return the declaration, crashes
11594 will sometimes occur. We therefore return
11595 void_type_node, as if this was a friend
11596 declaration, to cause callers to completely
11597 ignore this declaration. */
11598 return void_type_node;
11601 if (staticp)
11603 /* [class.mem] forbids static data members with the
11604 same name as the enclosing class. Non-static data
11605 members are checked in check_field_decls. */
11606 if (constructor_name_p (declarator, current_class_type))
11607 pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11608 declarator);
11610 /* C++ allows static class members. All other work
11611 for this is done by grokfield. */
11612 decl = build_lang_decl (VAR_DECL, declarator, type);
11613 TREE_STATIC (decl) = 1;
11614 /* In class context, 'static' means public access. */
11615 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11617 else
11619 decl = build_decl (FIELD_DECL, declarator, type);
11620 DECL_NONADDRESSABLE_P (decl) = bitfield;
11621 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11623 DECL_MUTABLE_P (decl) = 1;
11624 RIDBIT_RESET (RID_MUTABLE, specbits);
11628 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11629 inlinep, friendp, raises != NULL_TREE);
11632 else if (TREE_CODE (type) == FUNCTION_TYPE
11633 || TREE_CODE (type) == METHOD_TYPE)
11635 tree original_name;
11636 int publicp = 0;
11638 if (! declarator)
11639 return NULL_TREE;
11641 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11642 original_name = dname;
11643 else
11644 original_name = declarator;
11646 if (RIDBIT_SETP (RID_AUTO, specbits))
11647 error ("storage class `auto' invalid for function `%s'", name);
11648 else if (RIDBIT_SETP (RID_REGISTER, specbits))
11649 error ("storage class `register' invalid for function `%s'", name);
11650 else if (RIDBIT_SETP (RID_THREAD, specbits))
11651 error ("storage class `__thread' invalid for function `%s'", name);
11653 /* Function declaration not at top level.
11654 Storage classes other than `extern' are not allowed
11655 and `extern' makes no difference. */
11656 if (! toplevel_bindings_p ()
11657 && (RIDBIT_SETP (RID_STATIC, specbits)
11658 || RIDBIT_SETP (RID_INLINE, specbits))
11659 && pedantic)
11661 if (RIDBIT_SETP (RID_STATIC, specbits))
11662 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11663 else
11664 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11667 if (ctype == NULL_TREE)
11669 if (virtualp)
11671 error ("virtual non-class function `%s'", name);
11672 virtualp = 0;
11675 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11676 type = build_method_type_directly (ctype,
11677 TREE_TYPE (type),
11678 TYPE_ARG_TYPES (type));
11680 /* Record presence of `static'. */
11681 publicp = (ctype != NULL_TREE
11682 || RIDBIT_SETP (RID_EXTERN, specbits)
11683 || !RIDBIT_SETP (RID_STATIC, specbits));
11685 decl = grokfndecl (ctype, type, original_name, declarator,
11686 virtualp, flags, quals, raises,
11687 1, friendp,
11688 publicp, inlinep, funcdef_flag,
11689 template_count, in_namespace);
11690 if (decl == NULL_TREE)
11691 return NULL_TREE;
11693 if (staticp == 1)
11695 int invalid_static = 0;
11697 /* Don't allow a static member function in a class, and forbid
11698 declaring main to be static. */
11699 if (TREE_CODE (type) == METHOD_TYPE)
11701 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11702 invalid_static = 1;
11704 else if (current_function_decl)
11706 /* FIXME need arm citation */
11707 error ("cannot declare static function inside another function");
11708 invalid_static = 1;
11711 if (invalid_static)
11713 staticp = 0;
11714 RIDBIT_RESET (RID_STATIC, specbits);
11718 else
11720 /* It's a variable. */
11722 /* An uninitialized decl with `extern' is a reference. */
11723 decl = grokvardecl (type, declarator, &specbits,
11724 initialized,
11725 (type_quals & TYPE_QUAL_CONST) != 0,
11726 ctype ? ctype : in_namespace);
11727 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11728 inlinep, friendp, raises != NULL_TREE);
11730 if (ctype)
11732 DECL_CONTEXT (decl) = ctype;
11733 if (staticp == 1)
11735 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11736 staticp = 0;
11737 RIDBIT_RESET (RID_STATIC, specbits);
11739 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11741 error ("static member `%D' declared `register'", decl);
11742 RIDBIT_RESET (RID_REGISTER, specbits);
11744 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11746 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11747 decl);
11748 RIDBIT_RESET (RID_EXTERN, specbits);
11753 my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11755 /* Record `register' declaration for warnings on &
11756 and in case doing stupid register allocation. */
11758 if (RIDBIT_SETP (RID_REGISTER, specbits))
11759 DECL_REGISTER (decl) = 1;
11761 if (RIDBIT_SETP (RID_EXTERN, specbits))
11762 DECL_THIS_EXTERN (decl) = 1;
11764 if (RIDBIT_SETP (RID_STATIC, specbits))
11765 DECL_THIS_STATIC (decl) = 1;
11767 /* Record constancy and volatility. There's no need to do this
11768 when processing a template; we'll do this for the instantiated
11769 declaration based on the type of DECL. */
11770 if (!processing_template_decl)
11771 c_apply_type_quals_to_decl (type_quals, decl);
11773 return decl;
11777 /* Subroutine of start_function. Ensure that each of the parameter
11778 types (as listed in PARMS) is complete, as is required for a
11779 function definition. */
11781 static void
11782 require_complete_types_for_parms (tree parms)
11784 for (; parms; parms = TREE_CHAIN (parms))
11786 if (VOID_TYPE_P (TREE_TYPE (parms)))
11787 /* grokparms will have already issued an error */
11788 TREE_TYPE (parms) = error_mark_node;
11789 else if (complete_type_or_else (TREE_TYPE (parms), parms))
11791 layout_decl (parms, 0);
11792 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11794 else
11795 TREE_TYPE (parms) = error_mark_node;
11799 /* Returns nonzero if T is a local variable. */
11802 local_variable_p (tree t)
11804 if ((TREE_CODE (t) == VAR_DECL
11805 /* A VAR_DECL with a context that is a _TYPE is a static data
11806 member. */
11807 && !TYPE_P (CP_DECL_CONTEXT (t))
11808 /* Any other non-local variable must be at namespace scope. */
11809 && !DECL_NAMESPACE_SCOPE_P (t))
11810 || (TREE_CODE (t) == PARM_DECL))
11811 return 1;
11813 return 0;
11816 /* Returns nonzero if T is an automatic local variable or a label.
11817 (These are the declarations that need to be remapped when the code
11818 containing them is duplicated.) */
11821 nonstatic_local_decl_p (tree t)
11823 return ((local_variable_p (t) && !TREE_STATIC (t))
11824 || TREE_CODE (t) == LABEL_DECL
11825 || TREE_CODE (t) == RESULT_DECL);
11828 /* Like local_variable_p, but suitable for use as a tree-walking
11829 function. */
11831 static tree
11832 local_variable_p_walkfn (tree* tp,
11833 int* walk_subtrees ATTRIBUTE_UNUSED ,
11834 void* data ATTRIBUTE_UNUSED )
11836 return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11837 ? *tp : NULL_TREE);
11840 /* Check that ARG, which is a default-argument expression for a
11841 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11842 something goes wrong. DECL may also be a _TYPE node, rather than a
11843 DECL, if there is no DECL available. */
11845 tree
11846 check_default_argument (tree decl, tree arg)
11848 tree var;
11849 tree decl_type;
11851 if (TREE_CODE (arg) == DEFAULT_ARG)
11852 /* We get a DEFAULT_ARG when looking at an in-class declaration
11853 with a default argument. Ignore the argument for now; we'll
11854 deal with it after the class is complete. */
11855 return arg;
11857 if (processing_template_decl || uses_template_parms (arg))
11858 /* We don't do anything checking until instantiation-time. Note
11859 that there may be uninstantiated arguments even for an
11860 instantiated function, since default arguments are not
11861 instantiated until they are needed. */
11862 return arg;
11864 if (TYPE_P (decl))
11866 decl_type = decl;
11867 decl = NULL_TREE;
11869 else
11870 decl_type = TREE_TYPE (decl);
11872 if (arg == error_mark_node
11873 || decl == error_mark_node
11874 || TREE_TYPE (arg) == error_mark_node
11875 || decl_type == error_mark_node)
11876 /* Something already went wrong. There's no need to check
11877 further. */
11878 return error_mark_node;
11880 /* [dcl.fct.default]
11882 A default argument expression is implicitly converted to the
11883 parameter type. */
11884 if (!TREE_TYPE (arg)
11885 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11887 if (decl)
11888 error ("default argument for `%#D' has type `%T'",
11889 decl, TREE_TYPE (arg));
11890 else
11891 error ("default argument for parameter of type `%T' has type `%T'",
11892 decl_type, TREE_TYPE (arg));
11894 return error_mark_node;
11897 /* [dcl.fct.default]
11899 Local variables shall not be used in default argument
11900 expressions.
11902 The keyword `this' shall not be used in a default argument of a
11903 member function. */
11904 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11905 NULL);
11906 if (var)
11908 error ("default argument `%E' uses local variable `%D'",
11909 arg, var);
11910 return error_mark_node;
11913 /* All is well. */
11914 return arg;
11917 /* Decode the list of parameter types for a function type.
11918 Given the list of things declared inside the parens,
11919 return a list of types.
11921 We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11922 flag. If unset, we append void_list_node. A parmlist declared
11923 as `(void)' is accepted as the empty parmlist.
11925 Also set last_function_parms to the chain of PARM_DECLs. */
11927 static tree
11928 grokparms (tree first_parm)
11930 tree result = NULL_TREE;
11931 tree decls = NULL_TREE;
11932 int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11933 tree parm, chain;
11934 int any_error = 0;
11936 my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11938 for (parm = first_parm; parm != NULL_TREE; parm = chain)
11940 tree type = NULL_TREE;
11941 tree decl = TREE_VALUE (parm);
11942 tree init = TREE_PURPOSE (parm);
11943 tree specs, attrs;
11945 chain = TREE_CHAIN (parm);
11946 /* @@ weak defense against parse errors. */
11947 if (TREE_CODE (decl) != VOID_TYPE
11948 && TREE_CODE (decl) != TREE_LIST)
11950 /* Give various messages as the need arises. */
11951 if (TREE_CODE (decl) == STRING_CST)
11952 error ("invalid string constant `%E'", decl);
11953 else if (TREE_CODE (decl) == INTEGER_CST)
11954 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11955 continue;
11958 if (parm == void_list_node)
11959 break;
11961 split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
11962 decl = grokdeclarator (TREE_VALUE (decl), specs,
11963 PARM, init != NULL_TREE, &attrs);
11964 if (! decl || TREE_TYPE (decl) == error_mark_node)
11965 continue;
11967 if (attrs)
11968 cplus_decl_attributes (&decl, attrs, 0);
11970 type = TREE_TYPE (decl);
11971 if (VOID_TYPE_P (type))
11973 if (same_type_p (type, void_type_node)
11974 && !DECL_NAME (decl) && !result && !chain && !ellipsis)
11975 /* this is a parmlist of `(void)', which is ok. */
11976 break;
11977 cxx_incomplete_type_error (decl, type);
11978 /* It's not a good idea to actually create parameters of
11979 type `void'; other parts of the compiler assume that a
11980 void type terminates the parameter list. */
11981 type = error_mark_node;
11982 TREE_TYPE (decl) = error_mark_node;
11985 if (type != error_mark_node)
11987 /* Top-level qualifiers on the parameters are
11988 ignored for function types. */
11989 type = cp_build_qualified_type (type, 0);
11990 if (TREE_CODE (type) == METHOD_TYPE)
11992 error ("parameter `%D' invalidly declared method type", decl);
11993 type = build_pointer_type (type);
11994 TREE_TYPE (decl) = type;
11996 else if (abstract_virtuals_error (decl, type))
11997 any_error = 1; /* Seems like a good idea. */
11998 else if (POINTER_TYPE_P (type))
12000 /* [dcl.fct]/6, parameter types cannot contain pointers
12001 (references) to arrays of unknown bound. */
12002 tree t = TREE_TYPE (type);
12003 int ptr = TYPE_PTR_P (type);
12005 while (1)
12007 if (TYPE_PTR_P (t))
12008 ptr = 1;
12009 else if (TREE_CODE (t) != ARRAY_TYPE)
12010 break;
12011 else if (!TYPE_DOMAIN (t))
12012 break;
12013 t = TREE_TYPE (t);
12015 if (TREE_CODE (t) == ARRAY_TYPE)
12016 error ("parameter `%D' includes %s to array of unknown bound `%T'",
12017 decl, ptr ? "pointer" : "reference", t);
12020 if (!any_error && init)
12021 init = check_default_argument (decl, init);
12022 else
12023 init = NULL_TREE;
12026 TREE_CHAIN (decl) = decls;
12027 decls = decl;
12028 result = tree_cons (init, type, result);
12030 decls = nreverse (decls);
12031 result = nreverse (result);
12032 if (!ellipsis)
12033 result = chainon (result, void_list_node);
12034 last_function_parms = decls;
12036 return result;
12040 /* D is a constructor or overloaded `operator='.
12042 Let T be the class in which D is declared. Then, this function
12043 returns:
12045 -1 if D's is an ill-formed constructor or copy assignment operator
12046 whose first parameter is of type `T'.
12047 0 if D is not a copy constructor or copy assignment
12048 operator.
12049 1 if D is a copy constructor or copy assignment operator whose
12050 first parameter is a reference to const qualified T.
12051 2 if D is a copy constructor or copy assignment operator whose
12052 first parameter is a reference to non-const qualified T.
12054 This function can be used as a predicate. Positive values indicate
12055 a copy constructor and nonzero values indicate a copy assignment
12056 operator. */
12059 copy_fn_p (tree d)
12061 tree args;
12062 tree arg_type;
12063 int result = 1;
12065 my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12067 if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12068 /* Instantiations of template member functions are never copy
12069 functions. Note that member functions of templated classes are
12070 represented as template functions internally, and we must
12071 accept those as copy functions. */
12072 return 0;
12074 args = FUNCTION_FIRST_USER_PARMTYPE (d);
12075 if (!args)
12076 return 0;
12078 arg_type = TREE_VALUE (args);
12080 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12082 /* Pass by value copy assignment operator. */
12083 result = -1;
12085 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12086 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12088 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12089 result = 2;
12091 else
12092 return 0;
12094 args = TREE_CHAIN (args);
12096 if (args && args != void_list_node && !TREE_PURPOSE (args))
12097 /* There are more non-optional args. */
12098 return 0;
12100 return result;
12103 /* Remember any special properties of member function DECL. */
12105 void grok_special_member_properties (tree decl)
12107 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12108 ; /* Not special. */
12109 else if (DECL_CONSTRUCTOR_P (decl))
12111 int ctor = copy_fn_p (decl);
12113 if (ctor > 0)
12115 /* [class.copy]
12117 A non-template constructor for class X is a copy
12118 constructor if its first parameter is of type X&, const
12119 X&, volatile X& or const volatile X&, and either there
12120 are no other parameters or else all other parameters have
12121 default arguments. */
12122 TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12123 if (ctor > 1)
12124 TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12126 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12127 TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12129 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12131 /* [class.copy]
12133 A non-template assignment operator for class X is a copy
12134 assignment operator if its parameter is of type X, X&, const
12135 X&, volatile X& or const volatile X&. */
12137 int assop = copy_fn_p (decl);
12139 if (assop)
12141 TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12142 if (assop != 1)
12143 TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12144 if (DECL_PURE_VIRTUAL_P (decl))
12145 TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12150 /* Check a constructor DECL has the correct form. Complains
12151 if the class has a constructor of the form X(X). */
12154 grok_ctor_properties (tree ctype, tree decl)
12156 int ctor_parm = copy_fn_p (decl);
12158 if (ctor_parm < 0)
12160 /* [class.copy]
12162 A declaration of a constructor for a class X is ill-formed if
12163 its first parameter is of type (optionally cv-qualified) X
12164 and either there are no other parameters or else all other
12165 parameters have default arguments.
12167 We *don't* complain about member template instantiations that
12168 have this form, though; they can occur as we try to decide
12169 what constructor to use during overload resolution. Since
12170 overload resolution will never prefer such a constructor to
12171 the non-template copy constructor (which is either explicitly
12172 or implicitly defined), there's no need to worry about their
12173 existence. Theoretically, they should never even be
12174 instantiated, but that's hard to forestall. */
12175 error ("invalid constructor; you probably meant `%T (const %T&)'",
12176 ctype, ctype);
12177 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12178 return 0;
12181 return 1;
12184 /* An operator with this code is unary, but can also be binary. */
12186 static int
12187 ambi_op_p (enum tree_code code)
12189 return (code == INDIRECT_REF
12190 || code == ADDR_EXPR
12191 || code == CONVERT_EXPR
12192 || code == NEGATE_EXPR
12193 || code == PREINCREMENT_EXPR
12194 || code == PREDECREMENT_EXPR);
12197 /* An operator with this name can only be unary. */
12199 static int
12200 unary_op_p (enum tree_code code)
12202 return (code == TRUTH_NOT_EXPR
12203 || code == BIT_NOT_EXPR
12204 || code == COMPONENT_REF
12205 || code == TYPE_EXPR);
12208 /* DECL is a declaration for an overloaded operator. Returns true if
12209 the declaration is valid; false otherwise. If COMPLAIN is true,
12210 errors are issued for invalid declarations. */
12212 bool
12213 grok_op_properties (tree decl, int friendp, bool complain)
12215 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12216 tree argtype;
12217 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12218 tree name = DECL_NAME (decl);
12219 enum tree_code operator_code;
12220 int arity;
12221 bool ok;
12223 /* Assume that the declaration is valid. */
12224 ok = true;
12226 /* Count the number of arguments. */
12227 for (argtype = argtypes, arity = 0;
12228 argtype && argtype != void_list_node;
12229 argtype = TREE_CHAIN (argtype))
12230 ++arity;
12232 if (current_class_type == NULL_TREE)
12233 friendp = 1;
12235 if (DECL_CONV_FN_P (decl))
12236 operator_code = TYPE_EXPR;
12237 else
12240 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12241 if (ansi_opname (CODE) == name) \
12243 operator_code = (CODE); \
12244 break; \
12246 else if (ansi_assopname (CODE) == name) \
12248 operator_code = (CODE); \
12249 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12250 break; \
12253 #include "operators.def"
12254 #undef DEF_OPERATOR
12256 abort ();
12258 while (0);
12259 my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12260 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12262 if (! friendp)
12264 switch (operator_code)
12266 case NEW_EXPR:
12267 TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12268 break;
12270 case DELETE_EXPR:
12271 TYPE_GETS_DELETE (current_class_type) |= 1;
12272 break;
12274 case VEC_NEW_EXPR:
12275 TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12276 break;
12278 case VEC_DELETE_EXPR:
12279 TYPE_GETS_DELETE (current_class_type) |= 2;
12280 break;
12282 default:
12283 break;
12287 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12289 /* When the compiler encounters the definition of A::operator new, it
12290 doesn't look at the class declaration to find out if it's static. */
12291 if (methodp)
12292 revert_static_member_fn (decl);
12294 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12296 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12298 if (methodp)
12299 revert_static_member_fn (decl);
12301 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12303 else
12305 /* An operator function must either be a non-static member function
12306 or have at least one parameter of a class, a reference to a class,
12307 an enumeration, or a reference to an enumeration. 13.4.0.6 */
12308 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12310 if (operator_code == TYPE_EXPR
12311 || operator_code == CALL_EXPR
12312 || operator_code == COMPONENT_REF
12313 || operator_code == ARRAY_REF
12314 || operator_code == NOP_EXPR)
12315 error ("`%D' must be a nonstatic member function", decl);
12316 else
12318 tree p;
12320 if (DECL_STATIC_FUNCTION_P (decl))
12321 error ("`%D' must be either a non-static member function or a non-member function", decl);
12323 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
12325 tree arg = non_reference (TREE_VALUE (p));
12326 /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used
12327 because these checks are performed even on
12328 template functions. */
12329 if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE)
12330 break;
12333 if (!p || p == void_list_node)
12335 if (!complain)
12336 return false;
12338 error ("`%D' must have an argument of class or "
12339 "enumerated type",
12340 decl);
12341 ok = false;
12346 /* There are no restrictions on the arguments to an overloaded
12347 "operator ()". */
12348 if (operator_code == CALL_EXPR)
12349 return ok;
12351 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12353 tree t = TREE_TYPE (name);
12354 if (! friendp)
12356 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12357 const char *what = 0;
12359 if (ref)
12360 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12362 if (TREE_CODE (t) == VOID_TYPE)
12363 what = "void";
12364 else if (t == current_class_type)
12365 what = "the same type";
12366 /* Don't force t to be complete here. */
12367 else if (IS_AGGR_TYPE (t)
12368 && COMPLETE_TYPE_P (t)
12369 && DERIVED_FROM_P (t, current_class_type))
12370 what = "a base class";
12372 if (what && warn_conversion)
12373 warning ("conversion to %s%s will never use a type conversion operator",
12374 ref ? "a reference to " : "", what);
12377 if (operator_code == COND_EXPR)
12379 /* 13.4.0.3 */
12380 error ("ISO C++ prohibits overloading operator ?:");
12382 else if (ambi_op_p (operator_code))
12384 if (arity == 1)
12385 /* We pick the one-argument operator codes by default, so
12386 we don't have to change anything. */
12388 else if (arity == 2)
12390 /* If we thought this was a unary operator, we now know
12391 it to be a binary operator. */
12392 switch (operator_code)
12394 case INDIRECT_REF:
12395 operator_code = MULT_EXPR;
12396 break;
12398 case ADDR_EXPR:
12399 operator_code = BIT_AND_EXPR;
12400 break;
12402 case CONVERT_EXPR:
12403 operator_code = PLUS_EXPR;
12404 break;
12406 case NEGATE_EXPR:
12407 operator_code = MINUS_EXPR;
12408 break;
12410 case PREINCREMENT_EXPR:
12411 operator_code = POSTINCREMENT_EXPR;
12412 break;
12414 case PREDECREMENT_EXPR:
12415 operator_code = POSTDECREMENT_EXPR;
12416 break;
12418 default:
12419 abort ();
12422 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12424 if ((operator_code == POSTINCREMENT_EXPR
12425 || operator_code == POSTDECREMENT_EXPR)
12426 && ! processing_template_decl
12427 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12429 if (methodp)
12430 error ("postfix `%D' must take `int' as its argument",
12431 decl);
12432 else
12433 error
12434 ("postfix `%D' must take `int' as its second argument",
12435 decl);
12438 else
12440 if (methodp)
12441 error ("`%D' must take either zero or one argument", decl);
12442 else
12443 error ("`%D' must take either one or two arguments", decl);
12446 /* More Effective C++ rule 6. */
12447 if (warn_ecpp
12448 && (operator_code == POSTINCREMENT_EXPR
12449 || operator_code == POSTDECREMENT_EXPR
12450 || operator_code == PREINCREMENT_EXPR
12451 || operator_code == PREDECREMENT_EXPR))
12453 tree arg = TREE_VALUE (argtypes);
12454 tree ret = TREE_TYPE (TREE_TYPE (decl));
12455 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12456 arg = TREE_TYPE (arg);
12457 arg = TYPE_MAIN_VARIANT (arg);
12458 if (operator_code == PREINCREMENT_EXPR
12459 || operator_code == PREDECREMENT_EXPR)
12461 if (TREE_CODE (ret) != REFERENCE_TYPE
12462 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12463 arg))
12464 warning ("prefix `%D' should return `%T'", decl,
12465 build_reference_type (arg));
12467 else
12469 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12470 warning ("postfix `%D' should return `%T'", decl, arg);
12474 else if (unary_op_p (operator_code))
12476 if (arity != 1)
12478 if (methodp)
12479 error ("`%D' must take `void'", decl);
12480 else
12481 error ("`%D' must take exactly one argument", decl);
12484 else /* if (binary_op_p (operator_code)) */
12486 if (arity != 2)
12488 if (methodp)
12489 error ("`%D' must take exactly one argument", decl);
12490 else
12491 error ("`%D' must take exactly two arguments", decl);
12494 /* More Effective C++ rule 7. */
12495 if (warn_ecpp
12496 && (operator_code == TRUTH_ANDIF_EXPR
12497 || operator_code == TRUTH_ORIF_EXPR
12498 || operator_code == COMPOUND_EXPR))
12499 warning ("user-defined `%D' always evaluates both arguments",
12500 decl);
12503 /* Effective C++ rule 23. */
12504 if (warn_ecpp
12505 && arity == 2
12506 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12507 && (operator_code == PLUS_EXPR
12508 || operator_code == MINUS_EXPR
12509 || operator_code == TRUNC_DIV_EXPR
12510 || operator_code == MULT_EXPR
12511 || operator_code == TRUNC_MOD_EXPR)
12512 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12513 warning ("`%D' should return by value", decl);
12515 /* [over.oper]/8 */
12516 for (; argtypes && argtypes != void_list_node;
12517 argtypes = TREE_CHAIN (argtypes))
12518 if (TREE_PURPOSE (argtypes))
12520 TREE_PURPOSE (argtypes) = NULL_TREE;
12521 if (operator_code == POSTINCREMENT_EXPR
12522 || operator_code == POSTDECREMENT_EXPR)
12524 if (pedantic)
12525 pedwarn ("`%D' cannot have default arguments", decl);
12527 else
12528 error ("`%D' cannot have default arguments", decl);
12533 return ok;
12536 static const char *
12537 tag_name (enum tag_types code)
12539 switch (code)
12541 case record_type:
12542 return "struct";
12543 case class_type:
12544 return "class";
12545 case union_type:
12546 return "union ";
12547 case enum_type:
12548 return "enum";
12549 default:
12550 abort ();
12554 /* Name lookup in an elaborated-type-specifier (after the keyword
12555 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
12556 elaborated-type-specifier is invalid, issue a diagnostic and return
12557 error_mark_node; otherwise, return the *_TYPE to which it referred.
12558 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
12560 tree
12561 check_elaborated_type_specifier (enum tag_types tag_code,
12562 tree decl,
12563 bool allow_template_p)
12565 tree type;
12567 /* In the case of:
12569 struct S { struct S *p; };
12571 name lookup will find the TYPE_DECL for the implicit "S::S"
12572 typedef. Adjust for that here. */
12573 if (DECL_SELF_REFERENCE_P (decl))
12574 decl = TYPE_NAME (TREE_TYPE (decl));
12576 type = TREE_TYPE (decl);
12578 /* [dcl.type.elab]
12580 If the identifier resolves to a typedef-name or a template
12581 type-parameter, the elaborated-type-specifier is ill-formed.
12583 In other words, the only legitimate declaration to use in the
12584 elaborated type specifier is the implicit typedef created when
12585 the type is declared. */
12586 if (!DECL_IMPLICIT_TYPEDEF_P (decl))
12588 error ("using typedef-name `%D' after `%s'", decl, tag_name (tag_code));
12589 return IS_AGGR_TYPE (type) ? type : error_mark_node;
12592 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12594 error ("using template type parameter `%T' after `%s'",
12595 type, tag_name (tag_code));
12596 return error_mark_node;
12598 else if (TREE_CODE (type) != RECORD_TYPE
12599 && TREE_CODE (type) != UNION_TYPE
12600 && tag_code != enum_type)
12602 error ("`%T' referred to as `%s'", type, tag_name (tag_code));
12603 return error_mark_node;
12605 else if (TREE_CODE (type) != ENUMERAL_TYPE
12606 && tag_code == enum_type)
12608 error ("`%T' referred to as enum", type);
12609 return error_mark_node;
12611 else if (!allow_template_p
12612 && TREE_CODE (type) == RECORD_TYPE
12613 && CLASSTYPE_IS_TEMPLATE (type))
12615 /* If a class template appears as elaborated type specifier
12616 without a template header such as:
12618 template <class T> class C {};
12619 void f(class C); // No template header here
12621 then the required template argument is missing. */
12623 error ("template argument required for `%s %T'",
12624 tag_name (tag_code),
12625 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
12626 return error_mark_node;
12629 return type;
12632 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
12633 Define the tag as a forward-reference if it is not defined.
12635 If a declaration is given, process it here, and report an error if
12636 multiple declarations are not identical. ATTRIBUTE is the attribute
12637 appeared in this declaration.
12639 GLOBALIZE is false when this is also a definition. Only look in
12640 the current frame for the name (since C++ allows new names in any
12641 scope.)
12643 TEMPLATE_HEADER_P is true when this declaration is preceded by
12644 a set of template parameters. */
12646 tree
12647 xref_tag (enum tag_types tag_code, tree name, tree attributes,
12648 bool globalize, bool template_header_p)
12650 enum tree_code code;
12651 register tree t;
12652 struct cp_binding_level *b = current_binding_level;
12653 tree context = NULL_TREE;
12655 timevar_push (TV_NAME_LOOKUP);
12657 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
12659 switch (tag_code)
12661 case record_type:
12662 case class_type:
12663 code = RECORD_TYPE;
12664 break;
12665 case union_type:
12666 code = UNION_TYPE;
12667 break;
12668 case enum_type:
12669 code = ENUMERAL_TYPE;
12670 break;
12671 default:
12672 abort ();
12675 if (! globalize)
12677 /* If we know we are defining this tag, only look it up in
12678 this scope and don't try to find it as a type. */
12679 t = lookup_tag (code, name, b, 1);
12681 else
12683 tree decl = lookup_name (name, 1);
12685 if (decl && DECL_CLASS_TEMPLATE_P (decl))
12686 decl = DECL_TEMPLATE_RESULT (decl);
12688 if (decl && TREE_CODE (decl) == TYPE_DECL)
12690 /* Two cases we need to consider when deciding if a class
12691 template is allowed as an elaborated type specifier:
12692 1. It is a self reference to its own class.
12693 2. It comes with a template header.
12695 For example:
12697 template <class T> class C {
12698 class C *c1; // DECL_SELF_REFERENCE_P is true
12699 class D;
12701 template <class U> class C; // template_header_p is true
12702 template <class T> class C<T>::D {
12703 class C *c2; // DECL_SELF_REFERENCE_P is true
12704 }; */
12706 t = check_elaborated_type_specifier (tag_code,
12707 decl,
12708 template_header_p
12709 | DECL_SELF_REFERENCE_P (decl));
12710 if (t == error_mark_node)
12711 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12713 else
12714 t = NULL_TREE;
12716 if (t && current_class_type
12717 && template_class_depth (current_class_type)
12718 && template_header_p)
12720 /* Since GLOBALIZE is nonzero, we are not looking at a
12721 definition of this tag. Since, in addition, we are currently
12722 processing a (member) template declaration of a template
12723 class, we must be very careful; consider:
12725 template <class X>
12726 struct S1
12728 template <class U>
12729 struct S2
12730 { template <class V>
12731 friend struct S1; };
12733 Here, the S2::S1 declaration should not be confused with the
12734 outer declaration. In particular, the inner version should
12735 have a template parameter of level 2, not level 1. This
12736 would be particularly important if the member declaration
12737 were instead:
12739 template <class V = U> friend struct S1;
12741 say, when we should tsubst into `U' when instantiating
12742 S2. On the other hand, when presented with:
12744 template <class T>
12745 struct S1 {
12746 template <class U>
12747 struct S2 {};
12748 template <class U>
12749 friend struct S2;
12752 we must find the inner binding eventually. We
12753 accomplish this by making sure that the new type we
12754 create to represent this declaration has the right
12755 TYPE_CONTEXT. */
12756 context = TYPE_CONTEXT (t);
12757 t = NULL_TREE;
12761 if (! t)
12763 /* If no such tag is yet defined, create a forward-reference node
12764 and record it as the "definition".
12765 When a real declaration of this type is found,
12766 the forward-reference will be altered into a real type. */
12767 if (code == ENUMERAL_TYPE)
12769 error ("use of enum `%#D' without previous declaration", name);
12771 t = make_node (ENUMERAL_TYPE);
12773 /* Give the type a default layout like unsigned int
12774 to avoid crashing if it does not get defined. */
12775 TYPE_MODE (t) = TYPE_MODE (unsigned_type_node);
12776 TYPE_ALIGN (t) = TYPE_ALIGN (unsigned_type_node);
12777 TYPE_USER_ALIGN (t) = 0;
12778 TREE_UNSIGNED (t) = 1;
12779 TYPE_PRECISION (t) = TYPE_PRECISION (unsigned_type_node);
12780 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (unsigned_type_node);
12781 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (unsigned_type_node);
12783 /* Enable us to recognize when a type is created in class context.
12784 To do nested classes correctly, this should probably be cleared
12785 out when we leave this classes scope. Currently this in only
12786 done in `start_enum'. */
12788 pushtag (name, t, globalize);
12790 else
12792 t = make_aggr_type (code);
12793 TYPE_CONTEXT (t) = context;
12794 pushtag (name, t, globalize);
12797 else
12799 if (!globalize && processing_template_decl && IS_AGGR_TYPE (t))
12800 redeclare_class_template (t, current_template_parms);
12803 TYPE_ATTRIBUTES (t) = attributes;
12805 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
12808 tree
12809 xref_tag_from_type (tree old, tree id, int globalize)
12811 enum tag_types tag_kind;
12813 if (TREE_CODE (old) == RECORD_TYPE)
12814 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12815 else
12816 tag_kind = union_type;
12818 if (id == NULL_TREE)
12819 id = TYPE_IDENTIFIER (old);
12821 return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize, false);
12824 /* REF is a type (named NAME), for which we have just seen some
12825 baseclasses. BASE_LIST is a list of those baseclasses; the
12826 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12827 the base-class. TREE_VIA_VIRTUAL indicates virtual
12828 inheritance. CODE_TYPE_NODE indicates whether REF is a class,
12829 struct, or union. */
12831 void
12832 xref_basetypes (tree ref, tree base_list)
12834 /* In the declaration `A : X, Y, ... Z' we mark all the types
12835 (A, X, Y, ..., Z) so we can check for duplicates. */
12836 tree *basep;
12838 int i;
12839 enum tag_types tag_code;
12841 if (ref == error_mark_node)
12842 return;
12844 if (TREE_CODE (ref) == UNION_TYPE)
12846 error ("derived union `%T' invalid", ref);
12847 return;
12850 tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12852 /* First, make sure that any templates in base-classes are
12853 instantiated. This ensures that if we call ourselves recursively
12854 we do not get confused about which classes are marked and which
12855 are not. */
12856 basep = &base_list;
12857 while (*basep)
12859 tree basetype = TREE_VALUE (*basep);
12860 if (!(processing_template_decl && uses_template_parms (basetype))
12861 && !complete_type_or_else (basetype, NULL))
12862 /* An incomplete type. Remove it from the list. */
12863 *basep = TREE_CHAIN (*basep);
12864 else
12865 basep = &TREE_CHAIN (*basep);
12868 SET_CLASSTYPE_MARKED (ref);
12869 i = list_length (base_list);
12870 if (i)
12872 tree binfo = TYPE_BINFO (ref);
12873 tree binfos = make_tree_vec (i);
12874 tree accesses = make_tree_vec (i);
12876 BINFO_BASETYPES (binfo) = binfos;
12877 BINFO_BASEACCESSES (binfo) = accesses;
12879 for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
12881 tree access = TREE_PURPOSE (base_list);
12882 int via_virtual = TREE_VIA_VIRTUAL (base_list);
12883 tree basetype = TREE_VALUE (base_list);
12884 tree base_binfo;
12886 if (access == access_default_node)
12887 /* The base of a derived struct is public by default. */
12888 access = (tag_code == class_type
12889 ? access_private_node : access_public_node);
12891 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12892 basetype = TREE_TYPE (basetype);
12893 if (!basetype
12894 || (TREE_CODE (basetype) != RECORD_TYPE
12895 && TREE_CODE (basetype) != TYPENAME_TYPE
12896 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12897 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12899 error ("base type `%T' fails to be a struct or class type",
12900 basetype);
12901 continue;
12904 if (CLASSTYPE_MARKED (basetype))
12906 if (basetype == ref)
12907 error ("recursive type `%T' undefined", basetype);
12908 else
12909 error ("duplicate base type `%T' invalid", basetype);
12910 continue;
12913 if (TYPE_FOR_JAVA (basetype)
12914 && (current_lang_depth () == 0))
12915 TYPE_FOR_JAVA (ref) = 1;
12917 if (CLASS_TYPE_P (basetype))
12919 base_binfo = TYPE_BINFO (basetype);
12920 /* This flag will be in the binfo of the base type, we must
12921 clear it after copying the base binfos. */
12922 BINFO_DEPENDENT_BASE_P (base_binfo)
12923 = dependent_type_p (basetype);
12925 else
12926 base_binfo = make_binfo (size_zero_node, basetype,
12927 NULL_TREE, NULL_TREE);
12929 TREE_VEC_ELT (binfos, i) = base_binfo;
12930 TREE_VEC_ELT (accesses, i) = access;
12931 /* This flag will be in the binfo of the base type, we must
12932 clear it after copying the base binfos. */
12933 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12935 SET_CLASSTYPE_MARKED (basetype);
12937 /* We are free to modify these bits because they are meaningless
12938 at top level, and BASETYPE is a top-level type. */
12939 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12941 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12942 /* Converting to a virtual base class requires looking
12943 up the offset of the virtual base. */
12944 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12947 if (CLASS_TYPE_P (basetype))
12949 TYPE_HAS_NEW_OPERATOR (ref)
12950 |= TYPE_HAS_NEW_OPERATOR (basetype);
12951 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12952 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12953 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12954 /* If the base-class uses multiple inheritance, so do we. */
12955 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12956 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12957 /* Likewise, if converting to a base of the base may require
12958 code, then we may need to generate code to convert to a
12959 base as well. */
12960 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12961 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12963 i++;
12965 if (i)
12966 TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
12967 else
12968 BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
12970 if (i > 1)
12972 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12973 /* If there is more than one non-empty they cannot be at the same
12974 address. */
12975 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12979 /* Copy the base binfos, collect the virtual bases and set the
12980 inheritance order chain. */
12981 copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
12982 CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
12984 /* Unmark all the types. */
12985 while (i--)
12987 tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
12989 CLEAR_CLASSTYPE_MARKED (basetype);
12990 if (CLASS_TYPE_P (basetype))
12992 TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
12993 BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
12996 CLEAR_CLASSTYPE_MARKED (ref);
13000 /* Begin compiling the definition of an enumeration type.
13001 NAME is its name (or null if anonymous).
13002 Returns the type object, as yet incomplete.
13003 Also records info about it so that build_enumerator
13004 may be used to declare the individual values as they are read. */
13006 tree
13007 start_enum (tree name)
13009 register tree enumtype = NULL_TREE;
13010 struct cp_binding_level *b = current_binding_level;
13012 /* If this is the real definition for a previous forward reference,
13013 fill in the contents in the same object that used to be the
13014 forward reference. */
13016 if (name != NULL_TREE)
13017 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
13019 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13021 error ("multiple definition of `%#T'", enumtype);
13022 error ("%Hprevious definition here",
13023 &DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)));
13024 /* Clear out TYPE_VALUES, and start again. */
13025 TYPE_VALUES (enumtype) = NULL_TREE;
13027 else
13029 enumtype = make_node (ENUMERAL_TYPE);
13030 pushtag (name, enumtype, 0);
13033 return enumtype;
13036 /* After processing and defining all the values of an enumeration type,
13037 install their decls in the enumeration type and finish it off.
13038 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
13040 void
13041 finish_enum (tree enumtype)
13043 tree values;
13044 tree decl;
13045 tree value;
13046 tree minnode;
13047 tree maxnode;
13048 tree t;
13049 bool unsignedp;
13050 int lowprec;
13051 int highprec;
13052 int precision;
13053 integer_type_kind itk;
13054 tree underlying_type = NULL_TREE;
13056 /* We built up the VALUES in reverse order. */
13057 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13059 /* For an enum defined in a template, just set the type of the values;
13060 all further processing is postponed until the template is
13061 instantiated. We need to set the type so that tsubst of a CONST_DECL
13062 works. */
13063 if (processing_template_decl)
13065 for (values = TYPE_VALUES (enumtype);
13066 values;
13067 values = TREE_CHAIN (values))
13068 TREE_TYPE (TREE_VALUE (values)) = enumtype;
13069 if (at_function_scope_p ())
13070 add_stmt (build_min (TAG_DEFN, enumtype));
13071 return;
13074 /* Determine the minimum and maximum values of the enumerators. */
13075 if (TYPE_VALUES (enumtype))
13077 minnode = maxnode = NULL_TREE;
13079 for (values = TYPE_VALUES (enumtype);
13080 values;
13081 values = TREE_CHAIN (values))
13083 decl = TREE_VALUE (values);
13085 /* [dcl.enum]: Following the closing brace of an enum-specifier,
13086 each enumerator has the type of its enumeration. Prior to the
13087 closing brace, the type of each enumerator is the type of its
13088 initializing value. */
13089 TREE_TYPE (decl) = enumtype;
13091 /* Update the minimum and maximum values, if appropriate. */
13092 value = DECL_INITIAL (decl);
13093 /* Figure out what the minimum and maximum values of the
13094 enumerators are. */
13095 if (!minnode)
13096 minnode = maxnode = value;
13097 else if (tree_int_cst_lt (maxnode, value))
13098 maxnode = value;
13099 else if (tree_int_cst_lt (value, minnode))
13100 minnode = value;
13102 /* Set the TREE_TYPE for the values as well. That's so that when
13103 we call decl_constant_value we get an entity of the right type
13104 (but with the constant value). But first make a copy so we
13105 don't clobber shared INTEGER_CSTs. */
13106 if (TREE_TYPE (value) != enumtype)
13108 value = DECL_INITIAL (decl) = copy_node (value);
13109 TREE_TYPE (value) = enumtype;
13113 else
13114 /* [dcl.enum]
13116 If the enumerator-list is empty, the underlying type is as if
13117 the enumeration had a single enumerator with value 0. */
13118 minnode = maxnode = integer_zero_node;
13120 /* Compute the number of bits require to represent all values of the
13121 enumeration. We must do this before the type of MINNODE and
13122 MAXNODE are transformed, since min_precision relies on the
13123 TREE_TYPE of the value it is passed. */
13124 unsignedp = tree_int_cst_sgn (minnode) >= 0;
13125 lowprec = min_precision (minnode, unsignedp);
13126 highprec = min_precision (maxnode, unsignedp);
13127 precision = MAX (lowprec, highprec);
13129 /* Determine the underlying type of the enumeration.
13131 [dcl.enum]
13133 The underlying type of an enumeration is an integral type that
13134 can represent all the enumerator values defined in the
13135 enumeration. It is implementation-defined which integral type is
13136 used as the underlying type for an enumeration except that the
13137 underlying type shall not be larger than int unless the value of
13138 an enumerator cannot fit in an int or unsigned int.
13140 We use "int" or an "unsigned int" as the underlying type, even if
13141 a smaller integral type would work, unless the user has
13142 explicitly requested that we use the smallest possible type. */
13143 for (itk = (flag_short_enums ? itk_char : itk_int);
13144 itk != itk_none;
13145 itk++)
13147 underlying_type = integer_types[itk];
13148 if (TYPE_PRECISION (underlying_type) >= precision
13149 && TREE_UNSIGNED (underlying_type) == unsignedp)
13150 break;
13152 if (itk == itk_none)
13154 /* DR 377
13156 IF no integral type can represent all the enumerator values, the
13157 enumeration is ill-formed. */
13158 error ("no integral type can represent all of the enumerator values "
13159 "for `%T'", enumtype);
13160 precision = TYPE_PRECISION (long_long_integer_type_node);
13161 underlying_type = integer_types[itk_unsigned_long_long];
13164 /* Compute the minium and maximum values for the type.
13166 [dcl.enum]
13168 For an enumeration where emin is the smallest enumerator and emax
13169 is the largest, the values of the enumeration are the values of the
13170 underlying type in the range bmin to bmax, where bmin and bmax are,
13171 respectively, the smallest and largest values of the smallest bit-
13172 field that can store emin and emax. */
13173 TYPE_PRECISION (enumtype) = precision;
13174 set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp);
13176 /* [dcl.enum]
13178 The value of sizeof() applied to an enumeration type, an object
13179 of an enumeration type, or an enumerator, is the value of sizeof()
13180 applied to the underlying type. */
13181 TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
13182 TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
13183 TYPE_MODE (enumtype) = TYPE_MODE (underlying_type);
13184 TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
13185 TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
13186 TREE_UNSIGNED (enumtype) = TREE_UNSIGNED (underlying_type);
13188 /* Convert each of the enumerators to the type of the underlying
13189 type of the enumeration. */
13190 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
13192 decl = TREE_VALUE (values);
13193 value = perform_implicit_conversion (underlying_type,
13194 DECL_INITIAL (decl));
13195 TREE_TYPE (value) = enumtype;
13196 DECL_INITIAL (decl) = value;
13197 TREE_VALUE (values) = value;
13200 /* Fix up all variant types of this enum type. */
13201 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13203 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13204 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13205 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13206 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13207 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13208 TYPE_MODE (t) = TYPE_MODE (enumtype);
13209 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13210 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13211 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13212 TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13215 /* Finish debugging output for this type. */
13216 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13219 /* Build and install a CONST_DECL for an enumeration constant of the
13220 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13221 Assignment of sequential values by default is handled here. */
13223 void
13224 build_enumerator (tree name, tree value, tree enumtype)
13226 tree decl;
13227 tree context;
13228 tree type;
13230 /* Remove no-op casts from the value. */
13231 if (value)
13232 STRIP_TYPE_NOPS (value);
13234 if (! processing_template_decl)
13236 /* Validate and default VALUE. */
13237 if (value != NULL_TREE)
13239 value = decl_constant_value (value);
13241 if (TREE_CODE (value) == INTEGER_CST)
13243 value = perform_integral_promotions (value);
13244 constant_expression_warning (value);
13246 else
13248 error ("enumerator value for `%D' not integer constant", name);
13249 value = NULL_TREE;
13253 /* Default based on previous value. */
13254 if (value == NULL_TREE)
13256 tree prev_value;
13258 if (TYPE_VALUES (enumtype))
13260 /* The next value is the previous value ... */
13261 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13262 /* ... plus one. */
13263 value = cp_build_binary_op (PLUS_EXPR,
13264 prev_value,
13265 integer_one_node);
13267 if (tree_int_cst_lt (value, prev_value))
13268 error ("overflow in enumeration values at `%D'", name);
13270 else
13271 value = integer_zero_node;
13274 /* Remove no-op casts from the value. */
13275 STRIP_TYPE_NOPS (value);
13278 /* C++ associates enums with global, function, or class declarations. */
13279 context = current_scope ();
13280 if (!context)
13281 context = current_namespace;
13283 /* Build the actual enumeration constant. Note that the enumeration
13284 constants have the type of their initializers until the
13285 enumeration is complete:
13287 [ dcl.enum ]
13289 Following the closing brace of an enum-specifier, each enumer-
13290 ator has the type of its enumeration. Prior to the closing
13291 brace, the type of each enumerator is the type of its
13292 initializing value.
13294 In finish_enum we will reset the type. Of course, if we're
13295 processing a template, there may be no value. */
13296 type = value ? TREE_TYPE (value) : NULL_TREE;
13298 if (context && context == current_class_type)
13299 /* This enum declaration is local to the class. We need the full
13300 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
13301 decl = build_lang_decl (CONST_DECL, name, type);
13302 else
13303 /* It's a global enum, or it's local to a function. (Note local to
13304 a function could mean local to a class method. */
13305 decl = build_decl (CONST_DECL, name, type);
13307 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13308 TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
13309 DECL_INITIAL (decl) = value;
13311 if (context && context == current_class_type)
13312 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13313 on the TYPE_FIELDS list for `S'. (That's so that you can say
13314 things like `S::i' later.) */
13315 finish_member_declaration (decl);
13316 else
13317 pushdecl (decl);
13319 /* Add this enumeration constant to the list for this type. */
13320 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13324 /* We're defining DECL. Make sure that it's type is OK. */
13326 static void
13327 check_function_type (tree decl, tree current_function_parms)
13329 tree fntype = TREE_TYPE (decl);
13330 tree return_type = complete_type (TREE_TYPE (fntype));
13332 /* In a function definition, arg types must be complete. */
13333 require_complete_types_for_parms (current_function_parms);
13335 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13337 error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13339 /* Make it return void instead, but don't change the
13340 type of the DECL_RESULT, in case we have a named return value. */
13341 if (TREE_CODE (fntype) == METHOD_TYPE)
13343 tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13344 TREE_TYPE (decl)
13345 = build_method_type_directly (ctype,
13346 void_type_node,
13347 FUNCTION_ARG_CHAIN (decl));
13349 else
13350 TREE_TYPE (decl)
13351 = build_function_type (void_type_node,
13352 TYPE_ARG_TYPES (TREE_TYPE (decl)));
13353 TREE_TYPE (decl)
13354 = build_exception_variant (fntype,
13355 TYPE_RAISES_EXCEPTIONS (fntype));
13357 else
13358 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13361 /* Create the FUNCTION_DECL for a function definition.
13362 DECLSPECS and DECLARATOR are the parts of the declaration;
13363 they describe the function's name and the type it returns,
13364 but twisted together in a fashion that parallels the syntax of C.
13366 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13367 DECLARATOR is really the DECL for the function we are about to
13368 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13369 indicating that the function is an inline defined in-class.
13371 This function creates a binding context for the function body
13372 as well as setting up the FUNCTION_DECL in current_function_decl.
13374 Returns 1 on success. If the DECLARATOR is not suitable for a function
13375 (it defines a datum instead), we return 0, which tells
13376 yyparse to report a parse error.
13378 For C++, we must first check whether that datum makes any sense.
13379 For example, "class A local_a(1,2);" means that variable local_a
13380 is an aggregate of type A, which should have a constructor
13381 applied to it with the argument list [1, 2]. */
13384 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13386 tree decl1;
13387 tree ctype = NULL_TREE;
13388 tree fntype;
13389 tree restype;
13390 int doing_friend = 0;
13391 struct cp_binding_level *bl;
13392 tree current_function_parms;
13394 /* Sanity check. */
13395 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13396 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13398 /* This should only be done once on the top most decl. */
13399 if (have_extern_spec)
13401 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13402 have_extern_spec = false;
13405 if (flags & SF_PRE_PARSED)
13407 decl1 = declarator;
13409 fntype = TREE_TYPE (decl1);
13410 if (TREE_CODE (fntype) == METHOD_TYPE)
13411 ctype = TYPE_METHOD_BASETYPE (fntype);
13413 /* ISO C++ 11.4/5. A friend function defined in a class is in
13414 the (lexical) scope of the class in which it is defined. */
13415 if (!ctype && DECL_FRIEND_P (decl1))
13417 ctype = DECL_FRIEND_CONTEXT (decl1);
13419 /* CTYPE could be null here if we're dealing with a template;
13420 for example, `inline friend float foo()' inside a template
13421 will have no CTYPE set. */
13422 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13423 ctype = NULL_TREE;
13424 else
13425 doing_friend = 1;
13428 last_function_parms = DECL_ARGUMENTS (decl1);
13430 else
13432 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13433 /* If the declarator is not suitable for a function definition,
13434 cause a syntax error. */
13435 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13436 return 0;
13438 cplus_decl_attributes (&decl1, attrs, 0);
13440 /* If #pragma weak was used, mark the decl weak now. */
13441 if (global_scope_p (current_binding_level))
13442 maybe_apply_pragma_weak (decl1);
13444 fntype = TREE_TYPE (decl1);
13446 restype = TREE_TYPE (fntype);
13448 if (TREE_CODE (fntype) == METHOD_TYPE)
13449 ctype = TYPE_METHOD_BASETYPE (fntype);
13450 else if (DECL_MAIN_P (decl1))
13452 /* If this doesn't return integer_type, or a typedef to
13453 integer_type, complain. */
13454 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl1)), integer_type_node))
13456 if (pedantic || warn_return_type)
13457 pedwarn ("return type for `main' changed to `int'");
13458 TREE_TYPE (decl1) = fntype = default_function_type;
13463 if (DECL_DECLARED_INLINE_P (decl1)
13464 && lookup_attribute ("noinline", attrs))
13465 warning ("%Hinline function '%D' given attribute noinline",
13466 &DECL_SOURCE_LOCATION (decl1), decl1);
13468 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13469 /* This is a constructor, we must ensure that any default args
13470 introduced by this definition are propagated to the clones
13471 now. The clones are used directly in overload resolution. */
13472 adjust_clone_args (decl1);
13474 /* Sometimes we don't notice that a function is a static member, and
13475 build a METHOD_TYPE for it. Fix that up now. */
13476 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13477 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13479 revert_static_member_fn (decl1);
13480 last_function_parms = TREE_CHAIN (last_function_parms);
13481 ctype = NULL_TREE;
13484 /* Warn if function was previously implicitly declared
13485 (but not if we warned then). */
13486 if (! warn_implicit
13487 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13488 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13490 /* Set up current_class_type, and enter the scope of the class, if
13491 appropriate. */
13492 if (ctype)
13493 push_nested_class (ctype);
13494 else if (DECL_STATIC_FUNCTION_P (decl1))
13495 push_nested_class (DECL_CONTEXT (decl1));
13497 /* Now that we have entered the scope of the class, we must restore
13498 the bindings for any template parameters surrounding DECL1, if it
13499 is an inline member template. (Order is important; consider the
13500 case where a template parameter has the same name as a field of
13501 the class.) It is not until after this point that
13502 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13503 if (flags & SF_INCLASS_INLINE)
13504 maybe_begin_member_template_processing (decl1);
13506 /* Effective C++ rule 15. */
13507 if (warn_ecpp
13508 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13509 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13510 warning ("`operator=' should return a reference to `*this'");
13512 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13513 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13514 if (!DECL_INITIAL (decl1))
13515 DECL_INITIAL (decl1) = error_mark_node;
13517 /* This function exists in static storage.
13518 (This does not mean `static' in the C sense!) */
13519 TREE_STATIC (decl1) = 1;
13521 /* We must call push_template_decl after current_class_type is set
13522 up. (If we are processing inline definitions after exiting a
13523 class scope, current_class_type will be NULL_TREE until set above
13524 by push_nested_class.) */
13525 if (processing_template_decl)
13526 decl1 = push_template_decl (decl1);
13528 /* We are now in the scope of the function being defined. */
13529 current_function_decl = decl1;
13531 /* Save the parm names or decls from this function's declarator
13532 where store_parm_decls will find them. */
13533 current_function_parms = last_function_parms;
13535 /* Make sure the parameter and return types are reasonable. When
13536 you declare a function, these types can be incomplete, but they
13537 must be complete when you define the function. */
13538 if (! processing_template_decl)
13539 check_function_type (decl1, current_function_parms);
13541 /* Build the return declaration for the function. */
13542 restype = TREE_TYPE (fntype);
13543 /* Promote the value to int before returning it. */
13544 if (c_promoting_integer_type_p (restype))
13545 restype = type_promotes_to (restype);
13546 if (DECL_RESULT (decl1) == NULL_TREE)
13548 DECL_RESULT (decl1)
13549 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13550 c_apply_type_quals_to_decl (cp_type_quals (restype),
13551 DECL_RESULT (decl1));
13554 /* Initialize RTL machinery. We cannot do this until
13555 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13556 even when processing a template; this is how we get
13557 CFUN set up, and our per-function variables initialized.
13558 FIXME factor out the non-RTL stuff. */
13559 bl = current_binding_level;
13560 allocate_struct_function (decl1);
13561 current_binding_level = bl;
13563 /* Even though we're inside a function body, we still don't want to
13564 call expand_expr to calculate the size of a variable-sized array.
13565 We haven't necessarily assigned RTL to all variables yet, so it's
13566 not safe to try to expand expressions involving them. */
13567 immediate_size_expand = 0;
13568 cfun->x_dont_save_pending_sizes_p = 1;
13570 /* Start the statement-tree, start the tree now. */
13571 begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13573 /* Don't double-count statements in templates. */
13574 DECL_ESTIMATED_INSNS (decl1) = 0;
13576 /* Let the user know we're compiling this function. */
13577 announce_function (decl1);
13579 /* Record the decl so that the function name is defined.
13580 If we already have a decl for this name, and it is a FUNCTION_DECL,
13581 use the old decl. */
13582 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13584 /* A specialization is not used to guide overload resolution. */
13585 if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13586 && ! DECL_FUNCTION_MEMBER_P (decl1))
13587 decl1 = pushdecl (decl1);
13588 else
13590 /* We need to set the DECL_CONTEXT. */
13591 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13592 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13593 /* And make sure we have enough default args. */
13594 check_default_args (decl1);
13596 fntype = TREE_TYPE (decl1);
13599 /* Reset these in case the call to pushdecl changed them. */
13600 current_function_decl = decl1;
13601 cfun->decl = decl1;
13603 /* If we are (erroneously) defining a function that we have already
13604 defined before, wipe out what we knew before. */
13605 if (!DECL_PENDING_INLINE_P (decl1))
13606 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13608 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13610 /* We know that this was set up by `grokclassfn'. We do not
13611 wait until `store_parm_decls', since evil parse errors may
13612 never get us to that point. Here we keep the consistency
13613 between `current_class_type' and `current_class_ptr'. */
13614 tree t = DECL_ARGUMENTS (decl1);
13616 my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13617 162);
13618 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13619 19990811);
13621 cp_function_chain->x_current_class_ref
13622 = build_indirect_ref (t, NULL);
13623 cp_function_chain->x_current_class_ptr = t;
13625 /* Constructors and destructors need to know whether they're "in
13626 charge" of initializing virtual base classes. */
13627 t = TREE_CHAIN (t);
13628 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13630 current_in_charge_parm = t;
13631 t = TREE_CHAIN (t);
13633 if (DECL_HAS_VTT_PARM_P (decl1))
13635 if (DECL_NAME (t) != vtt_parm_identifier)
13636 abort ();
13637 current_vtt_parm = t;
13641 if (DECL_INTERFACE_KNOWN (decl1))
13643 tree ctx = decl_function_context (decl1);
13645 if (DECL_NOT_REALLY_EXTERN (decl1))
13646 DECL_EXTERNAL (decl1) = 0;
13648 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13649 && TREE_PUBLIC (ctx))
13650 /* This is a function in a local class in an extern inline
13651 function. */
13652 comdat_linkage (decl1);
13654 /* If this function belongs to an interface, it is public.
13655 If it belongs to someone else's interface, it is also external.
13656 This only affects inlines and template instantiations. */
13657 else if (interface_unknown == 0
13658 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13659 || flag_alt_external_templates))
13661 if (DECL_DECLARED_INLINE_P (decl1)
13662 || DECL_TEMPLATE_INSTANTIATION (decl1)
13663 || processing_template_decl)
13665 DECL_EXTERNAL (decl1)
13666 = (interface_only
13667 || (DECL_DECLARED_INLINE_P (decl1)
13668 && ! flag_implement_inlines
13669 && !DECL_VINDEX (decl1)));
13671 /* For WIN32 we also want to put these in linkonce sections. */
13672 maybe_make_one_only (decl1);
13674 else
13675 DECL_EXTERNAL (decl1) = 0;
13676 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13677 DECL_INTERFACE_KNOWN (decl1) = 1;
13679 else if (interface_unknown && interface_only
13680 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13681 || flag_alt_external_templates))
13683 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13684 interface, we will have interface_only set but not
13685 interface_known. In that case, we don't want to use the normal
13686 heuristics because someone will supply a #pragma implementation
13687 elsewhere, and deducing it here would produce a conflict. */
13688 comdat_linkage (decl1);
13689 DECL_EXTERNAL (decl1) = 0;
13690 DECL_INTERFACE_KNOWN (decl1) = 1;
13691 DECL_DEFER_OUTPUT (decl1) = 1;
13693 else
13695 /* This is a definition, not a reference.
13696 So clear DECL_EXTERNAL. */
13697 DECL_EXTERNAL (decl1) = 0;
13699 if ((DECL_DECLARED_INLINE_P (decl1)
13700 || DECL_TEMPLATE_INSTANTIATION (decl1))
13701 && ! DECL_INTERFACE_KNOWN (decl1)
13702 /* Don't try to defer nested functions for now. */
13703 && ! decl_function_context (decl1))
13704 DECL_DEFER_OUTPUT (decl1) = 1;
13705 else
13706 DECL_INTERFACE_KNOWN (decl1) = 1;
13709 pushlevel (0);
13710 current_binding_level->parm_flag = 1;
13712 ++function_depth;
13714 if (DECL_DESTRUCTOR_P (decl1))
13716 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13717 DECL_CONTEXT (dtor_label) = current_function_decl;
13720 start_fname_decls ();
13722 store_parm_decls (current_function_parms);
13724 return 1;
13727 /* Store the parameter declarations into the current function declaration.
13728 This is called after parsing the parameter declarations, before
13729 digesting the body of the function.
13731 Also install to binding contour return value identifier, if any. */
13733 static void
13734 store_parm_decls (tree current_function_parms)
13736 register tree fndecl = current_function_decl;
13737 register tree parm;
13739 /* This is a chain of any other decls that came in among the parm
13740 declarations. If a parm is declared with enum {foo, bar} x;
13741 then CONST_DECLs for foo and bar are put here. */
13742 tree nonparms = NULL_TREE;
13744 if (current_function_parms)
13746 /* This case is when the function was defined with an ANSI prototype.
13747 The parms already have decls, so we need not do anything here
13748 except record them as in effect
13749 and complain if any redundant old-style parm decls were written. */
13751 tree specparms = current_function_parms;
13752 tree next;
13754 /* Must clear this because it might contain TYPE_DECLs declared
13755 at class level. */
13756 storedecls (NULL_TREE);
13758 /* If we're doing semantic analysis, then we'll call pushdecl
13759 for each of these. We must do them in reverse order so that
13760 they end in the correct forward order. */
13761 specparms = nreverse (specparms);
13763 for (parm = specparms; parm; parm = next)
13765 next = TREE_CHAIN (parm);
13766 if (TREE_CODE (parm) == PARM_DECL)
13768 if (DECL_NAME (parm) == NULL_TREE
13769 || TREE_CODE (parm) != VOID_TYPE)
13770 pushdecl (parm);
13771 else
13772 error ("parameter `%D' declared void", parm);
13774 else
13776 /* If we find an enum constant or a type tag,
13777 put it aside for the moment. */
13778 TREE_CHAIN (parm) = NULL_TREE;
13779 nonparms = chainon (nonparms, parm);
13783 /* Get the decls in their original chain order and record in the
13784 function. This is all and only the PARM_DECLs that were
13785 pushed into scope by the loop above. */
13786 DECL_ARGUMENTS (fndecl) = getdecls ();
13788 else
13789 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13791 /* Now store the final chain of decls for the arguments
13792 as the decl-chain of the current lexical scope.
13793 Put the enumerators in as well, at the front so that
13794 DECL_ARGUMENTS is not modified. */
13795 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13797 /* Do the starting of the exception specifications, if we have any. */
13798 if (flag_exceptions && !processing_template_decl
13799 && flag_enforce_eh_specs
13800 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13801 current_eh_spec_block = begin_eh_spec_block ();
13805 /* We have finished doing semantic analysis on DECL, but have not yet
13806 generated RTL for its body. Save away our current state, so that
13807 when we want to generate RTL later we know what to do. */
13809 static void
13810 save_function_data (tree decl)
13812 struct language_function *f;
13814 /* Save the language-specific per-function data so that we can
13815 get it back when we really expand this function. */
13816 my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13817 19990908);
13819 /* Make a copy. */
13820 f = ggc_alloc (sizeof (struct language_function));
13821 memcpy (f, cp_function_chain, sizeof (struct language_function));
13822 DECL_SAVED_FUNCTION_DATA (decl) = f;
13824 /* Clear out the bits we don't need. */
13825 f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13826 f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13827 f->x_named_label_uses = NULL;
13828 f->bindings = NULL;
13829 f->x_local_names = NULL;
13831 /* When we get back here again, we will be expanding. */
13832 f->x_expanding_p = 1;
13834 /* If we've already decided that we cannot inline this function, we
13835 must remember that fact when we actually go to expand the
13836 function. */
13837 if (current_function_cannot_inline)
13839 f->cannot_inline = current_function_cannot_inline;
13840 DECL_INLINE (decl) = 0;
13844 /* Add a note to mark the beginning of the main body of the constructor.
13845 This is used to set up the data structures for the cleanup regions for
13846 fully-constructed bases and members. */
13848 static void
13849 begin_constructor_body (void)
13853 /* Add a note to mark the end of the main body of the constructor. This is
13854 used to end the cleanup regions for fully-constructed bases and
13855 members. */
13857 static void
13858 finish_constructor_body (void)
13862 /* Do all the processing for the beginning of a destructor; set up the
13863 vtable pointers and cleanups for bases and members. */
13865 static void
13866 begin_destructor_body (void)
13868 tree if_stmt;
13869 tree compound_stmt;
13871 /* If the dtor is empty, and we know there is not any possible
13872 way we could use any vtable entries, before they are possibly
13873 set by a base class dtor, we don't have to setup the vtables,
13874 as we know that any base class dtor will set up any vtables
13875 it needs. We avoid MI, because one base class dtor can do a
13876 virtual dispatch to an overridden function that would need to
13877 have a non-related vtable set up, we cannot avoid setting up
13878 vtables in that case. We could change this to see if there
13879 is just one vtable.
13881 ??? In the destructor for a class, the vtables are set
13882 appropriately for that class. There will be no non-related
13883 vtables. jason 2001-12-11. */
13884 if_stmt = begin_if_stmt ();
13886 /* If it is not safe to avoid setting up the vtables, then
13887 someone will change the condition to be boolean_true_node.
13888 (Actually, for now, we do not have code to set the condition
13889 appropriately, so we just assume that we always need to
13890 initialize the vtables.) */
13891 finish_if_stmt_cond (boolean_true_node, if_stmt);
13893 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
13895 /* Make all virtual function table pointers in non-virtual base
13896 classes point to CURRENT_CLASS_TYPE's virtual function
13897 tables. */
13898 initialize_vtbl_ptrs (current_class_ptr);
13900 finish_compound_stmt (compound_stmt);
13901 finish_then_clause (if_stmt);
13902 finish_if_stmt ();
13904 /* And insert cleanups for our bases and members so that they
13905 will be properly destroyed if we throw. */
13906 push_base_cleanups ();
13909 /* At the end of every destructor we generate code to delete the object if
13910 necessary. Do that now. */
13912 static void
13913 finish_destructor_body (void)
13915 tree exprstmt;
13917 /* Any return from a destructor will end up here; that way all base
13918 and member cleanups will be run when the function returns. */
13919 add_stmt (build_stmt (LABEL_STMT, dtor_label));
13921 /* In a virtual destructor, we must call delete. */
13922 if (DECL_VIRTUAL_P (current_function_decl))
13924 tree if_stmt;
13925 tree virtual_size = cxx_sizeof (current_class_type);
13927 /* [class.dtor]
13929 At the point of definition of a virtual destructor (including
13930 an implicit definition), non-placement operator delete shall
13931 be looked up in the scope of the destructor's class and if
13932 found shall be accessible and unambiguous. */
13933 exprstmt = build_op_delete_call
13934 (DELETE_EXPR, current_class_ptr, virtual_size,
13935 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13937 if_stmt = begin_if_stmt ();
13938 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13939 current_in_charge_parm,
13940 integer_one_node),
13941 if_stmt);
13942 finish_expr_stmt (exprstmt);
13943 finish_then_clause (if_stmt);
13944 finish_if_stmt ();
13948 /* Do the necessary processing for the beginning of a function body, which
13949 in this case includes member-initializers, but not the catch clauses of
13950 a function-try-block. Currently, this means opening a binding level
13951 for the member-initializers (in a ctor) and member cleanups (in a dtor).
13952 In other functions, this isn't necessary, but it doesn't hurt. */
13954 tree
13955 begin_function_body (void)
13957 tree stmt;
13959 if (processing_template_decl)
13960 /* Do nothing now. */;
13961 else
13962 /* Always keep the BLOCK node associated with the outermost pair of
13963 curly braces of a function. These are needed for correct
13964 operation of dwarfout.c. */
13965 keep_next_level (1);
13967 stmt = begin_compound_stmt (/*has_no_scope=*/false);
13968 COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13970 if (processing_template_decl)
13971 /* Do nothing now. */;
13972 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13973 begin_constructor_body ();
13974 else if (DECL_DESTRUCTOR_P (current_function_decl))
13975 begin_destructor_body ();
13977 return stmt;
13980 /* Do the processing for the end of a function body. Currently, this means
13981 closing out the cleanups for fully-constructed bases and members, and in
13982 the case of the destructor, deleting the object if desired. Again, this
13983 is only meaningful for [cd]tors, since they are the only functions where
13984 there is a significant distinction between the main body and any
13985 function catch clauses. Handling, say, main() return semantics here
13986 would be wrong, as flowing off the end of a function catch clause for
13987 main() would also need to return 0. */
13989 void
13990 finish_function_body (tree compstmt)
13992 /* Close the block. */
13993 finish_compound_stmt (compstmt);
13995 if (processing_template_decl)
13996 /* Do nothing now. */;
13997 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13998 finish_constructor_body ();
13999 else if (DECL_DESTRUCTOR_P (current_function_decl))
14000 finish_destructor_body ();
14003 /* Finish up a function declaration and compile that function
14004 all the way to assembler language output. The free the storage
14005 for the function definition.
14007 FLAGS is a bitwise or of the following values:
14008 2 - INCLASS_INLINE
14009 We just finished processing the body of an in-class inline
14010 function definition. (This processing will have taken place
14011 after the class definition is complete.) */
14013 tree
14014 finish_function (int flags)
14016 register tree fndecl = current_function_decl;
14017 tree fntype, ctype = NULL_TREE;
14018 int inclass_inline = (flags & 2) != 0;
14019 int nested;
14021 /* When we get some parse errors, we can end up without a
14022 current_function_decl, so cope. */
14023 if (fndecl == NULL_TREE)
14024 return error_mark_node;
14026 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14027 && DECL_VIRTUAL_P (fndecl)
14028 && !processing_template_decl)
14030 tree fnclass = DECL_CONTEXT (fndecl);
14031 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14032 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14035 nested = function_depth > 1;
14036 fntype = TREE_TYPE (fndecl);
14038 /* TREE_READONLY (fndecl) = 1;
14039 This caused &foo to be of type ptr-to-const-function
14040 which then got a warning when stored in a ptr-to-function variable. */
14042 my_friendly_assert (building_stmt_tree (), 20000911);
14044 finish_fname_decls ();
14046 /* For a cloned function, we've already got all the code we need;
14047 there's no need to add any extra bits. */
14048 if (!DECL_CLONED_FUNCTION_P (fndecl))
14050 if (DECL_MAIN_P (current_function_decl))
14052 /* Make it so that `main' always returns 0 by default. */
14053 #if VMS_TARGET
14054 finish_return_stmt (integer_one_node);
14055 #else
14056 finish_return_stmt (integer_zero_node);
14057 #endif
14060 /* Finish dealing with exception specifiers. */
14061 if (flag_exceptions && !processing_template_decl
14062 && flag_enforce_eh_specs
14063 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
14064 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14065 (TREE_TYPE (current_function_decl)),
14066 current_eh_spec_block);
14069 /* If we're saving up tree structure, tie off the function now. */
14070 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
14072 /* If this function can't throw any exceptions, remember that. */
14073 if (!processing_template_decl
14074 && !cp_function_chain->can_throw
14075 && !flag_non_call_exceptions)
14076 TREE_NOTHROW (fndecl) = 1;
14078 /* This must come after expand_function_end because cleanups might
14079 have declarations (from inline functions) that need to go into
14080 this function's blocks. */
14082 /* If the current binding level isn't the outermost binding level
14083 for this function, either there is a bug, or we have experienced
14084 syntax errors and the statement tree is malformed. */
14085 if (current_binding_level->parm_flag != 1)
14087 /* Make sure we have already experienced errors. */
14088 if (errorcount == 0)
14089 abort ();
14091 /* Throw away the broken statement tree and extra binding
14092 levels. */
14093 DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14095 while (current_binding_level->parm_flag != 1)
14097 if (current_binding_level->parm_flag == 2)
14098 pop_nested_class ();
14099 else
14100 poplevel (0, 0, 0);
14103 poplevel (1, 0, 1);
14105 /* Statements should always be full-expressions at the outermost set
14106 of curly braces for a function. */
14107 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
14109 /* Set up the named return value optimization, if we can. Here, we
14110 eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14111 for the nrv. genrtl_start_function and declare_return_variable
14112 handle making the nrv and RESULT_DECL share space. */
14113 if (current_function_return_value)
14115 tree r = current_function_return_value;
14116 tree outer;
14118 if (r != error_mark_node
14119 /* This is only worth doing for fns that return in memory--and
14120 simpler, since we don't have to worry about promoted modes. */
14121 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)))
14122 /* Only allow this for variables declared in the outer scope of
14123 the function so we know that their lifetime always ends with a
14124 return; see g++.dg/opt/nrv6.C. We could be more flexible if
14125 we were to do this optimization in tree-ssa. */
14126 /* Skip the artificial function body block. */
14127 && (outer = BLOCK_SUBBLOCKS (BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl))),
14128 chain_member (r, BLOCK_VARS (outer))))
14131 DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14132 walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14133 nullify_returns_r, r);
14135 else
14136 /* Clear it so genrtl_start_function and declare_return_variable
14137 know we're not optimizing. */
14138 current_function_return_value = NULL_TREE;
14141 /* Remember that we were in class scope. */
14142 if (current_class_name)
14143 ctype = current_class_type;
14145 /* Must mark the RESULT_DECL as being in this function. */
14146 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14148 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14149 to the FUNCTION_DECL node itself. */
14150 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14152 /* Save away current state, if appropriate. */
14153 if (!processing_template_decl)
14154 save_function_data (fndecl);
14156 /* If this function calls `setjmp' it cannot be inlined. When
14157 `longjmp' is called it is not guaranteed to restore the value of
14158 local variables that have been modified since the call to
14159 `setjmp'. So, if were to inline this function into some caller
14160 `c', then when we `longjmp', we might not restore all variables
14161 in `c'. (It might seem, at first blush, that there's no way for
14162 this function to modify local variables in `c', but their
14163 addresses may have been stored somewhere accessible to this
14164 function.) */
14165 if (!processing_template_decl && calls_setjmp_p (fndecl))
14166 DECL_UNINLINABLE (fndecl) = 1;
14168 /* Complain if there's just no return statement. */
14169 if (warn_return_type
14170 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14171 && !current_function_returns_value && !current_function_returns_null
14172 /* Don't complain if we abort or throw. */
14173 && !current_function_returns_abnormally
14174 && !DECL_NAME (DECL_RESULT (fndecl))
14175 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
14176 inline function, as we might never be compiled separately. */
14177 && (DECL_INLINE (fndecl) || processing_template_decl))
14178 warning ("no return statement in function returning non-void");
14180 /* We're leaving the context of this function, so zap cfun. It's still in
14181 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
14182 cfun = NULL;
14184 /* If this is an in-class inline definition, we may have to pop the
14185 bindings for the template parameters that we added in
14186 maybe_begin_member_template_processing when start_function was
14187 called. */
14188 if (inclass_inline)
14189 maybe_end_member_template_processing ();
14191 /* Leave the scope of the class. */
14192 if (ctype)
14193 pop_nested_class ();
14195 --function_depth;
14197 /* Clean up. */
14198 if (! nested)
14199 /* Let the error reporting routines know that we're outside a
14200 function. For a nested function, this value is used in
14201 cxx_pop_function_context and then reset via pop_function_context. */
14202 current_function_decl = NULL_TREE;
14204 return fndecl;
14207 /* Create the FUNCTION_DECL for a function definition.
14208 DECLSPECS and DECLARATOR are the parts of the declaration;
14209 they describe the return type and the name of the function,
14210 but twisted together in a fashion that parallels the syntax of C.
14212 This function creates a binding context for the function body
14213 as well as setting up the FUNCTION_DECL in current_function_decl.
14215 Returns a FUNCTION_DECL on success.
14217 If the DECLARATOR is not suitable for a function (it defines a datum
14218 instead), we return 0, which tells yyparse to report a parse error.
14220 May return void_type_node indicating that this method is actually
14221 a friend. See grokfield for more details.
14223 Came here with a `.pushlevel' .
14225 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14226 CHANGES TO CODE IN `grokfield'. */
14228 tree
14229 start_method (tree declspecs, tree declarator, tree attrlist)
14231 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14232 &attrlist);
14234 if (fndecl == error_mark_node)
14235 return error_mark_node;
14237 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14239 error ("invalid member function declaration");
14240 return error_mark_node;
14243 if (attrlist)
14244 cplus_decl_attributes (&fndecl, attrlist, 0);
14246 /* Pass friends other than inline friend functions back. */
14247 if (fndecl == void_type_node)
14248 return fndecl;
14250 if (DECL_IN_AGGR_P (fndecl))
14252 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14254 if (DECL_CONTEXT (fndecl)
14255 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14256 error ("`%D' is already defined in class `%T'", fndecl,
14257 DECL_CONTEXT (fndecl));
14259 return void_type_node;
14262 check_template_shadow (fndecl);
14264 DECL_DECLARED_INLINE_P (fndecl) = 1;
14265 if (flag_default_inline)
14266 DECL_INLINE (fndecl) = 1;
14268 /* We process method specializations in finish_struct_1. */
14269 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14271 fndecl = push_template_decl (fndecl);
14272 if (fndecl == error_mark_node)
14273 return fndecl;
14276 if (! DECL_FRIEND_P (fndecl))
14278 if (TREE_CHAIN (fndecl))
14280 fndecl = copy_node (fndecl);
14281 TREE_CHAIN (fndecl) = NULL_TREE;
14283 grok_special_member_properties (fndecl);
14286 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14288 /* Make a place for the parms */
14289 pushlevel (0);
14290 current_binding_level->parm_flag = 1;
14292 DECL_IN_AGGR_P (fndecl) = 1;
14293 return fndecl;
14296 /* Go through the motions of finishing a function definition.
14297 We don't compile this method until after the whole class has
14298 been processed.
14300 FINISH_METHOD must return something that looks as though it
14301 came from GROKFIELD (since we are defining a method, after all).
14303 This is called after parsing the body of the function definition.
14304 STMTS is the chain of statements that makes up the function body.
14306 DECL is the ..._DECL that `start_method' provided. */
14308 tree
14309 finish_method (tree decl)
14311 register tree fndecl = decl;
14312 tree old_initial;
14314 register tree link;
14316 if (decl == void_type_node)
14317 return decl;
14319 old_initial = DECL_INITIAL (fndecl);
14321 /* Undo the level for the parms (from start_method).
14322 This is like poplevel, but it causes nothing to be
14323 saved. Saving information here confuses symbol-table
14324 output routines. Besides, this information will
14325 be correctly output when this method is actually
14326 compiled. */
14328 /* Clear out the meanings of the local variables of this level;
14329 also record in each decl which block it belongs to. */
14331 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14333 if (DECL_NAME (link) != NULL_TREE)
14334 pop_binding (DECL_NAME (link), link);
14335 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14336 DECL_CONTEXT (link) = NULL_TREE;
14339 poplevel (0, 0, 0);
14341 DECL_INITIAL (fndecl) = old_initial;
14343 /* We used to check if the context of FNDECL was different from
14344 current_class_type as another way to get inside here. This didn't work
14345 for String.cc in libg++. */
14346 if (DECL_FRIEND_P (fndecl))
14348 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14349 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14350 decl = void_type_node;
14353 return decl;
14357 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14358 we can lay it out later, when and if its type becomes complete. */
14360 void
14361 maybe_register_incomplete_var (tree var)
14363 my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14365 /* Keep track of variables with incomplete types. */
14366 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14367 && DECL_EXTERNAL (var))
14369 tree inner_type = TREE_TYPE (var);
14371 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14372 inner_type = TREE_TYPE (inner_type);
14373 inner_type = TYPE_MAIN_VARIANT (inner_type);
14375 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14376 /* RTTI TD entries are created while defining the type_info. */
14377 || (TYPE_LANG_SPECIFIC (inner_type)
14378 && TYPE_BEING_DEFINED (inner_type)))
14379 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14383 /* Called when a class type (given by TYPE) is defined. If there are
14384 any existing VAR_DECLs whose type hsa been completed by this
14385 declaration, update them now. */
14387 void
14388 complete_vars (tree type)
14390 tree *list = &incomplete_vars;
14392 my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14393 while (*list)
14395 if (same_type_p (type, TREE_PURPOSE (*list)))
14397 tree var = TREE_VALUE (*list);
14398 /* Complete the type of the variable. The VAR_DECL itself
14399 will be laid out in expand_expr. */
14400 complete_type (TREE_TYPE (var));
14401 /* Remove this entry from the list. */
14402 *list = TREE_CHAIN (*list);
14404 else
14405 list = &TREE_CHAIN (*list);
14409 /* If DECL is of a type which needs a cleanup, build that cleanup
14410 here. */
14412 tree
14413 cxx_maybe_build_cleanup (tree decl)
14415 tree type = TREE_TYPE (decl);
14417 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14419 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14420 tree rval;
14422 if (TREE_CODE (type) == ARRAY_TYPE)
14423 rval = decl;
14424 else
14426 cxx_mark_addressable (decl);
14427 rval = build_unary_op (ADDR_EXPR, decl, 0);
14430 /* Optimize for space over speed here. */
14431 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14432 || flag_expensive_optimizations)
14433 flags |= LOOKUP_NONVIRTUAL;
14435 rval = build_delete (TREE_TYPE (rval), rval,
14436 sfk_complete_destructor, flags, 0);
14438 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14439 && ! TYPE_HAS_DESTRUCTOR (type))
14440 rval = build_compound_expr (rval, build_vbase_delete (type, decl));
14442 return rval;
14444 return NULL_TREE;
14447 /* When a stmt has been parsed, this function is called. */
14449 void
14450 finish_stmt (void)
14452 /* Always assume this statement was not an expression statement. If
14453 it actually was an expression statement, its our callers
14454 responsibility to fix this up. */
14455 last_expr_type = NULL_TREE;
14458 /* DECL was originally constructed as a non-static member function,
14459 but turned out to be static. Update it accordingly. */
14461 void
14462 revert_static_member_fn (tree decl)
14464 tree tmp;
14465 tree function = TREE_TYPE (decl);
14466 tree args = TYPE_ARG_TYPES (function);
14468 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14469 != TYPE_UNQUALIFIED)
14470 error ("static member function `%#D' declared with type qualifiers",
14471 decl);
14473 args = TREE_CHAIN (args);
14474 tmp = build_function_type (TREE_TYPE (function), args);
14475 tmp = build_qualified_type (tmp, cp_type_quals (function));
14476 tmp = build_exception_variant (tmp,
14477 TYPE_RAISES_EXCEPTIONS (function));
14478 TREE_TYPE (decl) = tmp;
14479 if (DECL_ARGUMENTS (decl))
14480 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14481 DECL_STATIC_FUNCTION_P (decl) = 1;
14484 /* Initialize the variables used during compilation of a C++
14485 function. */
14487 void
14488 cxx_push_function_context (struct function * f)
14490 struct language_function *p
14491 = ggc_alloc_cleared (sizeof (struct language_function));
14492 f->language = p;
14494 /* It takes an explicit call to expand_body to generate RTL for a
14495 function. */
14496 expanding_p = 0;
14498 /* Whenever we start a new function, we destroy temporaries in the
14499 usual way. */
14500 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14502 if (f->decl)
14504 tree fn = f->decl;
14506 current_function_is_thunk = DECL_THUNK_P (fn);
14508 if (DECL_SAVED_FUNCTION_DATA (fn))
14510 /* If we already parsed this function, and we're just expanding it
14511 now, restore saved state. */
14512 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
14514 /* If we decided that we didn't want to inline this function,
14515 make sure the back-end knows that. */
14516 if (!current_function_cannot_inline)
14517 current_function_cannot_inline = cp_function_chain->cannot_inline;
14519 /* We don't need the saved data anymore. Unless this is an inline
14520 function; we need the named return value info for
14521 cp_copy_res_decl_for_inlining. */
14522 if (! DECL_INLINE (fn))
14523 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
14528 /* Free the language-specific parts of F, now that we've finished
14529 compiling the function. */
14531 void
14532 cxx_pop_function_context (struct function * f)
14534 f->language = 0;
14537 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14538 one of the language-independent trees. */
14540 enum cp_tree_node_structure_enum
14541 cp_tree_node_structure (union lang_tree_node * t)
14543 switch (TREE_CODE (&t->generic))
14545 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
14546 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
14547 case OVERLOAD: return TS_CP_OVERLOAD;
14548 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14549 case PTRMEM_CST: return TS_CP_PTRMEM;
14550 case BASELINK: return TS_CP_BASELINK;
14551 case WRAPPER: return TS_CP_WRAPPER;
14552 default: return TS_CP_GENERIC;
14556 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14557 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
14559 tree
14560 identifier_global_value (tree t)
14562 return IDENTIFIER_GLOBAL_VALUE (t);
14565 /* Build the void_list_node (void_type_node having been created). */
14566 tree
14567 build_void_list_node (void)
14569 tree t = build_tree_list (NULL_TREE, void_type_node);
14570 TREE_PARMLIST (t) = 1;
14571 return t;
14574 static int
14575 cp_missing_noreturn_ok_p (tree decl)
14577 /* A missing noreturn is ok for the `main' function. */
14578 return DECL_MAIN_P (decl);
14581 #include "gt-cp-decl.h"
14582 #include "gtype-cp.h"