PR c++/10147
[official-gcc.git] / gcc / cp / decl.c
blob4b23ee286989c07449b6c2b5c94b79a01b990c07
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 resume_binding_level (struct cp_binding_level *);
59 static int decl_jump_unsafe (tree);
60 static void storedecls (tree);
61 static void require_complete_types_for_parms (tree);
62 static int ambi_op_p (enum tree_code);
63 static int unary_op_p (enum tree_code);
64 static cxx_saved_binding *store_bindings (tree, cxx_saved_binding *);
65 static tree lookup_tag_reverse (tree, tree);
66 static void push_local_name (tree);
67 static void warn_extern_redeclared_static (tree, tree);
68 static tree grok_reference_init (tree, tree, tree, tree *);
69 static tree grokfndecl (tree, tree, tree, tree, int,
70 enum overload_flags, tree,
71 tree, int, int, int, int, int, int, tree);
72 static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
73 static tree follow_tag_typedef (tree);
74 static tree lookup_tag (enum tree_code, tree,
75 struct cp_binding_level *, int);
76 static void set_identifier_type_value_with_scope
77 (tree, tree, struct cp_binding_level *);
78 static void record_unknown_type (tree, const char *);
79 static tree builtin_function_1 (const char *, tree, tree, int,
80 enum built_in_class, const char *,
81 tree);
82 static tree build_library_fn_1 (tree, enum tree_code, tree);
83 static int member_function_or_else (tree, tree, enum overload_flags);
84 static void bad_specifiers (tree, const char *, int, int, int, int,
85 int);
86 static tree maybe_process_template_type_declaration
87 (tree, int, struct cp_binding_level*);
88 static void check_for_uninitialized_const_var (tree);
89 static hashval_t typename_hash (const void *);
90 static int typename_compare (const void *, const void *);
91 static void push_binding (tree, tree, struct cp_binding_level*);
92 static void pop_binding (tree, tree);
93 static tree local_variable_p_walkfn (tree *, int *, void *);
94 static tree select_decl (cxx_binding *, int);
95 static int lookup_flags (int, int);
96 static tree qualify_lookup (tree, int);
97 static tree record_builtin_java_type (const char *, int);
98 static const char *tag_name (enum tag_types code);
99 static struct cp_binding_level *innermost_nonclass_level (void);
100 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
101 static int walk_globals_r (tree, void*);
102 static int walk_vtables_r (tree, void*);
103 static void add_decl_to_level (tree, struct cp_binding_level *);
104 static tree make_label_decl (tree, int);
105 static void use_label (tree);
106 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
107 const location_t *);
108 static void check_previous_goto (struct named_label_use_list *);
109 static void check_switch_goto (struct cp_binding_level *);
110 static void check_previous_gotos (tree);
111 static void pop_label (tree, tree);
112 static void pop_labels (tree);
113 static void maybe_deduce_size_from_array_init (tree, tree);
114 static void layout_var_decl (tree);
115 static void maybe_commonize_var (tree);
116 static tree check_initializer (tree, tree, int, tree *);
117 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
118 static void save_function_data (tree);
119 static void check_function_type (tree, tree);
120 static void begin_constructor_body (void);
121 static void finish_constructor_body (void);
122 static void begin_destructor_body (void);
123 static void finish_destructor_body (void);
124 static tree create_array_type_for_decl (tree, tree, tree);
125 static tree get_atexit_node (void);
126 static tree get_dso_handle_node (void);
127 static tree start_cleanup_fn (void);
128 static void end_cleanup_fn (void);
129 static tree cp_make_fname_decl (tree, int);
130 static void initialize_predefined_identifiers (void);
131 static tree check_special_function_return_type
132 (special_function_kind, tree, tree);
133 static tree push_cp_library_fn (enum tree_code, tree);
134 static tree build_cp_library_fn (tree, enum tree_code, tree);
135 static void store_parm_decls (tree);
136 static int cp_missing_noreturn_ok_p (tree);
137 static void initialize_local_var (tree, tree);
138 static void expand_static_init (tree, tree);
139 static tree next_initializable_field (tree);
140 static tree reshape_init (tree, tree *);
141 static tree build_typename_type (tree, tree, tree);
143 /* Erroneous argument lists can use this *IFF* they do not modify it. */
144 tree error_mark_list;
146 /* The following symbols are subsumed in the cp_global_trees array, and
147 listed here individually for documentation purposes.
149 C++ extensions
150 tree wchar_decl_node;
152 tree vtable_entry_type;
153 tree delta_type_node;
154 tree __t_desc_type_node;
155 tree ti_desc_type_node;
156 tree bltn_desc_type_node, ptr_desc_type_node;
157 tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
158 tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
159 tree ptm_desc_type_node;
160 tree base_desc_type_node;
162 tree class_type_node, record_type_node, union_type_node, enum_type_node;
163 tree unknown_type_node;
165 Array type `vtable_entry_type[]'
167 tree vtbl_type_node;
168 tree vtbl_ptr_type_node;
170 Namespaces,
172 tree std_node;
173 tree abi_node;
175 A FUNCTION_DECL which can call `abort'. Not necessarily the
176 one that the user will declare, but sufficient to be called
177 by routines that want to abort the program.
179 tree abort_fndecl;
181 The FUNCTION_DECL for the default `::operator delete'.
183 tree global_delete_fndecl;
185 Used by RTTI
186 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
187 tree tinfo_var_id;
191 tree cp_global_trees[CPTI_MAX];
193 /* Indicates that there is a type value in some namespace, although
194 that is not necessarily in scope at the moment. */
196 static GTY(()) tree global_type_node;
198 /* The node that holds the "name" of the global scope. */
199 static GTY(()) tree global_scope_name;
201 /* Used only for jumps to as-yet undefined labels, since jumps to
202 defined labels can have their validity checked immediately. */
204 struct named_label_use_list GTY(())
206 struct cp_binding_level *binding_level;
207 tree names_in_scope;
208 tree label_decl;
209 location_t o_goto_locus;
210 struct named_label_use_list *next;
213 #define named_label_uses cp_function_chain->x_named_label_uses
215 #define local_names cp_function_chain->x_local_names
217 /* A list of objects which have constructors or destructors
218 which reside in the global scope. The decl is stored in
219 the TREE_VALUE slot and the initializer is stored
220 in the TREE_PURPOSE slot. */
221 tree static_aggregates;
223 /* -- end of C++ */
225 /* A node for the integer constants 2, and 3. */
227 tree integer_two_node, integer_three_node;
229 /* Similar, for last_function_parm_tags. */
230 tree last_function_parms;
232 /* A list of all LABEL_DECLs in the function that have names. Here so
233 we can clear out their names' definitions at the end of the
234 function, and so we can check the validity of jumps to these labels. */
236 struct named_label_list GTY(())
238 struct cp_binding_level *binding_level;
239 tree names_in_scope;
240 tree old_value;
241 tree label_decl;
242 tree bad_decls;
243 struct named_label_list *next;
244 unsigned int in_try_scope : 1;
245 unsigned int in_catch_scope : 1;
248 #define named_labels cp_function_chain->x_named_labels
250 /* The name of the anonymous namespace, throughout this translation
251 unit. */
252 tree anonymous_namespace_name;
254 /* The number of function bodies which we are currently processing.
255 (Zero if we are at namespace scope, one inside the body of a
256 function, two inside the body of a function in a local class, etc.) */
257 int function_depth;
259 /* States indicating how grokdeclarator() should handle declspecs marked
260 with __attribute__((deprecated)). An object declared as
261 __attribute__((deprecated)) suppresses warnings of uses of other
262 deprecated items. */
264 enum deprecated_states {
265 DEPRECATED_NORMAL,
266 DEPRECATED_SUPPRESS
269 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
271 /* Set by add_implicitly_declared_members() to keep those members from
272 being flagged as deprecated or reported as using deprecated
273 types. */
274 int adding_implicit_members = 0;
276 /* True if a declaration with an `extern' linkage specifier is being
277 processed. */
278 bool have_extern_spec;
281 /* A chain of binding_level structures awaiting reuse. */
283 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
285 /* true means unconditionally make a BLOCK for the next level pushed. */
287 static bool keep_next_level_flag;
289 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
290 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
291 time the VAR_DECL was declared, the type was incomplete. */
293 static GTY(()) tree incomplete_vars;
295 #ifndef ENABLE_SCOPE_CHECKING
296 # define ENABLE_SCOPE_CHECKING 0
297 #else
298 # define ENABLE_SCOPE_CHECKING 1
299 #endif
301 static int binding_depth = 0;
302 static int is_class_level = 0;
304 static void
305 indent (int depth)
307 int i;
309 for (i = 0; i < depth * 2; i++)
310 putc (' ', stderr);
313 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
315 /* Return a string describing the kind of SCOPE we have. */
316 static const char *
317 cxx_scope_descriptor (cxx_scope *scope)
319 /* The order of this table must match the "scope_kind"
320 enumerators. */
321 static const char* scope_kind_names[] = {
322 "block-scope",
323 "cleanup-scope",
324 "try-scope",
325 "catch-scope",
326 "for-scope",
327 "function-parameter-scope",
328 "class-scope",
329 "namespace-scope",
330 "template-parameter-scope",
331 "template-explicit-spec-scope"
333 const scope_kind kind = scope->explicit_spec_p
334 ? sk_template_spec : scope->kind;
336 return scope_kind_names[kind];
339 /* Output a debugging information about SCOPE when performning
340 ACTION at LINE. */
341 static void
342 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
344 const char *desc = cxx_scope_descriptor (scope);
345 if (scope->this_entity)
346 verbatim ("%s %s(%E) %p %d\n", action, desc,
347 scope->this_entity, (void *) scope, line);
348 else
349 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
352 /* Return the estimated initial size of the hashtable of a NAMESPACE
353 scope. */
355 static inline size_t
356 namespace_scope_ht_size (tree ns)
358 tree name = DECL_NAME (ns);
360 return name == std_identifier
361 ? NAMESPACE_STD_HT_SIZE
362 : (name == global_scope_name
363 ? GLOBAL_SCOPE_HT_SIZE
364 : NAMESPACE_ORDINARY_HT_SIZE);
367 /* Create a new KIND scope and make it the top of the active scopes stack.
368 ENTITY is the scope of the associated C++ entity (namespace, class,
369 function); it is NULL otherwise. */
371 cxx_scope *
372 begin_scope (scope_kind kind, tree entity)
374 cxx_scope *scope;
376 /* Reuse or create a struct for this binding level. */
377 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
379 scope = free_binding_level;
380 free_binding_level = scope->level_chain;
382 else
383 scope = ggc_alloc (sizeof (cxx_scope));
384 memset (scope, 0, sizeof (cxx_scope));
386 scope->this_entity = entity;
387 scope->more_cleanups_ok = true;
388 switch (kind)
390 case sk_cleanup:
391 scope->keep = true;
392 break;
394 case sk_template_spec:
395 scope->explicit_spec_p = true;
396 kind = sk_template_parms;
397 /* fall through */
398 case sk_template_parms:
399 case sk_block:
400 case sk_try:
401 case sk_catch:
402 case sk_for:
403 case sk_class:
404 case sk_function_parms:
405 scope->keep = keep_next_level_flag;
406 break;
408 case sk_namespace:
409 scope->type_decls = binding_table_new (namespace_scope_ht_size (entity));
410 NAMESPACE_LEVEL (entity) = scope;
411 VARRAY_TREE_INIT (scope->static_decls,
412 DECL_NAME (entity) == std_identifier
413 || DECL_NAME (entity) == global_scope_name
414 ? 200 : 10,
415 "Static declarations");
416 break;
418 default:
419 /* Should not happen. */
420 my_friendly_assert (false, 20030922);
421 break;
423 scope->kind = kind;
425 /* Add it to the front of currently active scopes stack. */
426 scope->level_chain = current_binding_level;
427 current_binding_level = scope;
428 keep_next_level_flag = false;
430 if (ENABLE_SCOPE_CHECKING)
432 scope->binding_depth = binding_depth;
433 indent (binding_depth);
434 cxx_scope_debug (scope, input_location.line, "push");
435 is_class_level = 0;
436 binding_depth++;
439 return scope;
442 /* We're about to leave current scope. Pop the top of the stack of
443 currently active scopes. Return the enclosing scope, now active. */
445 static cxx_scope *
446 leave_scope (void)
448 cxx_scope *scope = current_binding_level;
450 if (scope->kind == sk_namespace && class_binding_level)
451 current_binding_level = class_binding_level;
453 /* We cannot leave a scope, if there are none left. */
454 if (NAMESPACE_LEVEL (global_namespace))
455 my_friendly_assert (!global_scope_p (scope), 20030527);
457 if (ENABLE_SCOPE_CHECKING)
459 indent (--binding_depth);
460 cxx_scope_debug (scope, input_location.line, "leave");
461 if (is_class_level != (scope == class_binding_level))
463 indent (binding_depth);
464 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
466 is_class_level = 0;
469 /* Move one nesting level up. */
470 current_binding_level = scope->level_chain;
472 /* Namespace-scopes are left most probably temporarily, not completely;
473 they can be reopen later, e.g. in namespace-extension or any name
474 binding acttivity that requires us to resume a namespace. For other
475 scopes, we just make the structure available for reuse. */
476 if (scope->kind != sk_namespace)
478 scope->level_chain = free_binding_level;
479 if (scope->kind == sk_class)
480 scope->type_decls = NULL;
481 else
482 binding_table_free (scope->type_decls);
483 my_friendly_assert (!ENABLE_SCOPE_CHECKING
484 || scope->binding_depth == binding_depth,
485 20030529);
486 free_binding_level = scope;
489 /* Find the innermost enclosing class scope, and reset
490 CLASS_BINDING_LEVEL appropriately. */
491 for (scope = current_binding_level;
492 scope && scope->kind != sk_class;
493 scope = scope->level_chain)
495 class_binding_level = scope && scope->kind == sk_class ? scope : NULL;
497 return current_binding_level;
500 static void
501 resume_binding_level (struct cp_binding_level* b)
503 /* Resuming binding levels is meant only for namespaces,
504 and those cannot nest into classes. */
505 my_friendly_assert(!class_binding_level, 386);
506 /* Also, resuming a non-directly nested namespace is a no-no. */
507 my_friendly_assert(b->level_chain == current_binding_level, 386);
508 current_binding_level = b;
509 if (ENABLE_SCOPE_CHECKING)
511 b->binding_depth = binding_depth;
512 indent (binding_depth);
513 cxx_scope_debug (b, input_location.line, "resume");
514 is_class_level = 0;
515 binding_depth++;
519 /* Nonzero if we are currently in the global binding level. */
522 global_bindings_p (void)
524 return global_scope_p (current_binding_level);
527 /* Return the innermost binding level that is not for a class scope. */
529 static struct cp_binding_level *
530 innermost_nonclass_level (void)
532 struct cp_binding_level *b;
534 b = current_binding_level;
535 while (b->kind == sk_class)
536 b = b->level_chain;
538 return b;
541 /* Nonzero if we are currently in a toplevel binding level. This
542 means either the global binding level or a namespace in a toplevel
543 binding level. Since there are no non-toplevel namespace levels,
544 this really means any namespace or template parameter level. We
545 also include a class whose context is toplevel. */
548 toplevel_bindings_p (void)
550 struct cp_binding_level *b = innermost_nonclass_level ();
552 return b->kind == sk_namespace || b->kind == sk_template_parms;
555 /* Nonzero if this is a namespace scope, or if we are defining a class
556 which is itself at namespace scope, or whose enclosing class is
557 such a class, etc. */
560 namespace_bindings_p (void)
562 struct cp_binding_level *b = innermost_nonclass_level ();
564 return b->kind == sk_namespace;
567 /* If KEEP is true, make a BLOCK node for the next binding level,
568 unconditionally. Otherwise, use the normal logic to decide whether
569 or not to create a BLOCK. */
571 void
572 keep_next_level (bool keep)
574 keep_next_level_flag = keep;
577 /* Nonzero if the current level needs to have a BLOCK made. */
580 kept_level_p (void)
582 return (current_binding_level->blocks != NULL_TREE
583 || current_binding_level->keep
584 || current_binding_level->kind == sk_cleanup
585 || current_binding_level->names != NULL_TREE
586 || current_binding_level->type_decls != NULL);
589 /* Returns the kind of the innermost scope. */
591 scope_kind
592 innermost_scope_kind (void)
594 return current_binding_level->kind;
597 /* Returns nonzero if this scope was created to store template
598 parameters. */
601 template_parm_scope_p (void)
603 return innermost_scope_kind () == sk_template_parms;
606 /* Returns the kind of template specialization we are currently
607 processing, given that it's declaration contained N_CLASS_SCOPES
608 explicit scope qualifications. */
610 tmpl_spec_kind
611 current_tmpl_spec_kind (int n_class_scopes)
613 int n_template_parm_scopes = 0;
614 int seen_specialization_p = 0;
615 int innermost_specialization_p = 0;
616 struct cp_binding_level *b;
618 /* Scan through the template parameter scopes. */
619 for (b = current_binding_level;
620 b->kind == sk_template_parms;
621 b = b->level_chain)
623 /* If we see a specialization scope inside a parameter scope,
624 then something is wrong. That corresponds to a declaration
625 like:
627 template <class T> template <> ...
629 which is always invalid since [temp.expl.spec] forbids the
630 specialization of a class member template if the enclosing
631 class templates are not explicitly specialized as well. */
632 if (b->explicit_spec_p)
634 if (n_template_parm_scopes == 0)
635 innermost_specialization_p = 1;
636 else
637 seen_specialization_p = 1;
639 else if (seen_specialization_p == 1)
640 return tsk_invalid_member_spec;
642 ++n_template_parm_scopes;
645 /* Handle explicit instantiations. */
646 if (processing_explicit_instantiation)
648 if (n_template_parm_scopes != 0)
649 /* We've seen a template parameter list during an explicit
650 instantiation. For example:
652 template <class T> template void f(int);
654 This is erroneous. */
655 return tsk_invalid_expl_inst;
656 else
657 return tsk_expl_inst;
660 if (n_template_parm_scopes < n_class_scopes)
661 /* We've not seen enough template headers to match all the
662 specialized classes present. For example:
664 template <class T> void R<T>::S<T>::f(int);
666 This is invalid; there needs to be one set of template
667 parameters for each class. */
668 return tsk_insufficient_parms;
669 else if (n_template_parm_scopes == n_class_scopes)
670 /* We're processing a non-template declaration (even though it may
671 be a member of a template class.) For example:
673 template <class T> void S<T>::f(int);
675 The `class T' maches the `S<T>', leaving no template headers
676 corresponding to the `f'. */
677 return tsk_none;
678 else if (n_template_parm_scopes > n_class_scopes + 1)
679 /* We've got too many template headers. For example:
681 template <> template <class T> void f (T);
683 There need to be more enclosing classes. */
684 return tsk_excessive_parms;
685 else
686 /* This must be a template. It's of the form:
688 template <class T> template <class U> void S<T>::f(U);
690 This is a specialization if the innermost level was a
691 specialization; otherwise it's just a definition of the
692 template. */
693 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
696 void
697 set_class_shadows (tree shadows)
699 class_binding_level->class_shadowed = shadows;
702 /* We're defining an object of type TYPE. If it needs a cleanup, but
703 we're not allowed to add any more objects with cleanups to the current
704 scope, create a new binding level. */
706 void
707 maybe_push_cleanup_level (tree type)
709 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
710 && current_binding_level->more_cleanups_ok == 0)
712 begin_scope (sk_cleanup, NULL);
713 clear_last_expr ();
714 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
718 /* Exit the current scope. */
720 void
721 finish_scope (void)
723 poplevel (0, 0, 0);
726 /* Make DECL the innermost binding for ID. The LEVEL is the binding
727 level at which this declaration is being bound. */
729 static void
730 push_binding (tree id, tree decl, cxx_scope* level)
732 cxx_binding *binding = cxx_binding_make (decl, NULL);
734 /* Now, fill in the binding information. */
735 binding->previous = IDENTIFIER_BINDING (id);
736 binding->scope = level;
737 INHERITED_VALUE_BINDING_P (binding) = 0;
738 LOCAL_BINDING_P (binding) = (level != class_binding_level);
740 /* And put it on the front of the list of bindings for ID. */
741 IDENTIFIER_BINDING (id) = binding;
744 /* Add DECL to the list of things declared in B. */
746 static void
747 add_decl_to_level (tree decl,
748 struct cp_binding_level* b)
750 if (TREE_CODE (decl) == NAMESPACE_DECL
751 && !DECL_NAMESPACE_ALIAS (decl))
753 TREE_CHAIN (decl) = b->namespaces;
754 b->namespaces = decl;
756 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
758 TREE_CHAIN (decl) = b->vtables;
759 b->vtables = decl;
761 else
763 /* We build up the list in reverse order, and reverse it later if
764 necessary. */
765 TREE_CHAIN (decl) = b->names;
766 b->names = decl;
767 b->names_size++;
769 /* If appropriate, add decl to separate list of statics */
770 if (b->kind == sk_namespace)
771 if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
772 || (TREE_CODE (decl) == FUNCTION_DECL
773 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
774 VARRAY_PUSH_TREE (b->static_decls, decl);
778 /* Bind DECL to ID in the current_binding_level, assumed to be a local
779 binding level. If PUSH_USING is set in FLAGS, we know that DECL
780 doesn't really belong to this binding level, that it got here
781 through a using-declaration. */
783 void
784 push_local_binding (tree id, tree decl, int flags)
786 struct cp_binding_level *b;
788 /* Skip over any local classes. This makes sense if we call
789 push_local_binding with a friend decl of a local class. */
790 b = innermost_nonclass_level ();
792 if (lookup_name_current_level (id))
794 /* Supplement the existing binding. */
795 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
796 /* It didn't work. Something else must be bound at this
797 level. Do not add DECL to the list of things to pop
798 later. */
799 return;
801 else
802 /* Create a new binding. */
803 push_binding (id, decl, b);
805 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
806 /* We must put the OVERLOAD into a TREE_LIST since the
807 TREE_CHAIN of an OVERLOAD is already used. Similarly for
808 decls that got here through a using-declaration. */
809 decl = build_tree_list (NULL_TREE, decl);
811 /* And put DECL on the list of things declared by the current
812 binding level. */
813 add_decl_to_level (decl, b);
816 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
817 binding was successful. */
820 push_class_binding (tree id, tree decl)
822 int result = 1;
823 cxx_binding *binding = IDENTIFIER_BINDING (id);
824 tree context;
826 timevar_push (TV_NAME_LOOKUP);
827 /* Note that we declared this value so that we can issue an error if
828 this is an invalid redeclaration of a name already used for some
829 other purpose. */
830 note_name_declared_in_class (id, decl);
832 if (binding && binding->scope == class_binding_level)
833 /* Supplement the existing binding. */
834 result = supplement_binding (IDENTIFIER_BINDING (id), decl);
835 else
836 /* Create a new binding. */
837 push_binding (id, decl, class_binding_level);
839 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
840 class-level declaration. Note that we do not use DECL here
841 because of the possibility of the `struct stat' hack; if DECL is
842 a class-name or enum-name we might prefer a field-name, or some
843 such. */
844 IDENTIFIER_CLASS_VALUE (id) = IDENTIFIER_BINDING (id)->value;
846 /* If this is a binding from a base class, mark it as such. */
847 binding = IDENTIFIER_BINDING (id);
848 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
850 if (TREE_CODE (decl) == OVERLOAD)
851 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
852 else
854 my_friendly_assert (DECL_P (decl), 0);
855 context = context_for_name_lookup (decl);
858 if (is_properly_derived_from (current_class_type, context))
859 INHERITED_VALUE_BINDING_P (binding) = 1;
860 else
861 INHERITED_VALUE_BINDING_P (binding) = 0;
863 else if (binding->value == decl)
864 /* We only encounter a TREE_LIST when push_class_decls detects an
865 ambiguity. Such an ambiguity can be overridden by a definition
866 in this class. */
867 INHERITED_VALUE_BINDING_P (binding) = 1;
869 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
872 /* Remove the binding for DECL which should be the innermost binding
873 for ID. */
875 static void
876 pop_binding (tree id, tree decl)
878 cxx_binding *binding;
880 if (id == NULL_TREE)
881 /* It's easiest to write the loops that call this function without
882 checking whether or not the entities involved have names. We
883 get here for such an entity. */
884 return;
886 /* Get the innermost binding for ID. */
887 binding = IDENTIFIER_BINDING (id);
889 /* The name should be bound. */
890 my_friendly_assert (binding != NULL, 0);
892 /* The DECL will be either the ordinary binding or the type
893 binding for this identifier. Remove that binding. */
894 if (binding->value == decl)
895 binding->value = NULL_TREE;
896 else if (binding->type == decl)
897 binding->type = NULL_TREE;
898 else
899 abort ();
901 if (!binding->value && !binding->type)
903 /* We're completely done with the innermost binding for this
904 identifier. Unhook it from the list of bindings. */
905 IDENTIFIER_BINDING (id) = binding->previous;
907 /* Add it to the free list. */
908 cxx_binding_free (binding);
912 /* When a label goes out of scope, check to see if that label was used
913 in a valid manner, and issue any appropriate warnings or errors. */
915 static void
916 pop_label (tree label, tree old_value)
918 if (!processing_template_decl)
920 if (DECL_INITIAL (label) == NULL_TREE)
922 location_t location;
924 cp_error_at ("label `%D' used but not defined", label);
925 location.file = input_filename;
926 location.line = 0;
927 /* Avoid crashing later. */
928 define_label (location, DECL_NAME (label));
930 else if (warn_unused_label && !TREE_USED (label))
931 cp_warning_at ("label `%D' defined but not used", label);
934 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
937 /* At the end of a function, all labels declared within the function
938 go out of scope. BLOCK is the top-level block for the
939 function. */
941 static void
942 pop_labels (tree block)
944 struct named_label_list *link;
946 /* Clear out the definitions of all label names, since their scopes
947 end here. */
948 for (link = named_labels; link; link = link->next)
950 pop_label (link->label_decl, link->old_value);
951 /* Put the labels into the "variables" of the top-level block,
952 so debugger can see them. */
953 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
954 BLOCK_VARS (block) = link->label_decl;
957 named_labels = NULL;
960 /* Exit a binding level.
961 Pop the level off, and restore the state of the identifier-decl mappings
962 that were in effect when this level was entered.
964 If KEEP == 1, this level had explicit declarations, so
965 and create a "block" (a BLOCK node) for the level
966 to record its declarations and subblocks for symbol table output.
968 If FUNCTIONBODY is nonzero, this level is the body of a function,
969 so create a block as if KEEP were set and also clear out all
970 label names.
972 If REVERSE is nonzero, reverse the order of decls before putting
973 them into the BLOCK. */
975 tree
976 poplevel (int keep, int reverse, int functionbody)
978 register tree link;
979 /* The chain of decls was accumulated in reverse order.
980 Put it into forward order, just for cleanliness. */
981 tree decls;
982 int tmp = functionbody;
983 int real_functionbody;
984 tree subblocks;
985 tree block = NULL_TREE;
986 tree decl;
987 int leaving_for_scope;
988 scope_kind kind;
990 timevar_push (TV_NAME_LOOKUP);
992 my_friendly_assert (current_binding_level->kind != sk_class, 19990916);
994 real_functionbody = (current_binding_level->kind == sk_cleanup
995 ? ((functionbody = 0), tmp) : functionbody);
996 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
998 my_friendly_assert (!current_binding_level->class_shadowed,
999 19990414);
1001 /* We used to use KEEP == 2 to indicate that the new block should go
1002 at the beginning of the list of blocks at this binding level,
1003 rather than the end. This hack is no longer used. */
1004 my_friendly_assert (keep == 0 || keep == 1, 0);
1006 if (current_binding_level->keep)
1007 keep = 1;
1009 /* Any uses of undefined labels, and any defined labels, now operate
1010 under constraints of next binding contour. */
1011 if (cfun && !functionbody)
1013 struct cp_binding_level *level_chain;
1014 level_chain = current_binding_level->level_chain;
1015 if (level_chain)
1017 struct named_label_use_list *uses;
1018 struct named_label_list *labels;
1019 for (labels = named_labels; labels; labels = labels->next)
1020 if (labels->binding_level == current_binding_level)
1022 tree decl;
1023 if (current_binding_level->kind == sk_try)
1024 labels->in_try_scope = 1;
1025 if (current_binding_level->kind == sk_catch)
1026 labels->in_catch_scope = 1;
1027 for (decl = labels->names_in_scope; decl;
1028 decl = TREE_CHAIN (decl))
1029 if (decl_jump_unsafe (decl))
1030 labels->bad_decls = tree_cons (NULL_TREE, decl,
1031 labels->bad_decls);
1032 labels->binding_level = level_chain;
1033 labels->names_in_scope = level_chain->names;
1036 for (uses = named_label_uses; uses; uses = uses->next)
1037 if (uses->binding_level == current_binding_level)
1039 uses->binding_level = level_chain;
1040 uses->names_in_scope = level_chain->names;
1045 /* Get the decls in the order they were written.
1046 Usually current_binding_level->names is in reverse order.
1047 But parameter decls were previously put in forward order. */
1049 if (reverse)
1050 current_binding_level->names
1051 = decls = nreverse (current_binding_level->names);
1052 else
1053 decls = current_binding_level->names;
1055 /* Output any nested inline functions within this block
1056 if they weren't already output. */
1057 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1058 if (TREE_CODE (decl) == FUNCTION_DECL
1059 && ! TREE_ASM_WRITTEN (decl)
1060 && DECL_INITIAL (decl) != NULL_TREE
1061 && TREE_ADDRESSABLE (decl)
1062 && decl_function_context (decl) == current_function_decl)
1064 /* If this decl was copied from a file-scope decl
1065 on account of a block-scope extern decl,
1066 propagate TREE_ADDRESSABLE to the file-scope decl. */
1067 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1068 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1069 else
1071 push_function_context ();
1072 output_inline_function (decl);
1073 pop_function_context ();
1077 /* When not in function-at-a-time mode, expand_end_bindings will
1078 warn about unused variables. But, in function-at-a-time mode
1079 expand_end_bindings is not passed the list of variables in the
1080 current scope, and therefore no warning is emitted. So, we
1081 explicitly warn here. */
1082 if (!processing_template_decl)
1083 warn_about_unused_variables (getdecls ());
1085 /* If there were any declarations or structure tags in that level,
1086 or if this level is a function body,
1087 create a BLOCK to record them for the life of this function. */
1088 block = NULL_TREE;
1089 if (keep == 1 || functionbody)
1090 block = make_node (BLOCK);
1091 if (block != NULL_TREE)
1093 BLOCK_VARS (block) = decls;
1094 BLOCK_SUBBLOCKS (block) = subblocks;
1097 /* In each subblock, record that this is its superior. */
1098 if (keep >= 0)
1099 for (link = subblocks; link; link = TREE_CHAIN (link))
1100 BLOCK_SUPERCONTEXT (link) = block;
1102 /* We still support the old for-scope rules, whereby the variables
1103 in a for-init statement were in scope after the for-statement
1104 ended. We only use the new rules if flag_new_for_scope is
1105 nonzero. */
1106 leaving_for_scope
1107 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
1109 /* Remove declarations for all the DECLs in this level. */
1110 for (link = decls; link; link = TREE_CHAIN (link))
1112 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1113 && DECL_NAME (link))
1115 cxx_binding *outer_binding
1116 = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
1117 tree ns_binding;
1119 if (!outer_binding)
1120 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1121 else
1122 ns_binding = NULL_TREE;
1124 if (outer_binding
1125 && outer_binding->scope == current_binding_level->level_chain)
1126 /* We have something like:
1128 int i;
1129 for (int i; ;);
1131 and we are leaving the `for' scope. There's no reason to
1132 keep the binding of the inner `i' in this case. */
1133 pop_binding (DECL_NAME (link), link);
1134 else if ((outer_binding
1135 && (TREE_CODE (outer_binding->value) == TYPE_DECL))
1136 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
1137 /* Here, we have something like:
1139 typedef int I;
1141 void f () {
1142 for (int I; ;);
1145 We must pop the for-scope binding so we know what's a
1146 type and what isn't. */
1147 pop_binding (DECL_NAME (link), link);
1148 else
1150 /* Mark this VAR_DECL as dead so that we can tell we left it
1151 there only for backward compatibility. */
1152 DECL_DEAD_FOR_LOCAL (link) = 1;
1154 /* Keep track of what should have happened when we
1155 popped the binding. */
1156 if (outer_binding && outer_binding->value)
1157 DECL_SHADOWED_FOR_VAR (link) = outer_binding->value;
1159 /* Add it to the list of dead variables in the next
1160 outermost binding to that we can remove these when we
1161 leave that binding. */
1162 current_binding_level->level_chain->dead_vars_from_for
1163 = tree_cons (NULL_TREE, link,
1164 current_binding_level->level_chain->
1165 dead_vars_from_for);
1167 /* Although we don't pop the cxx_binding, we do clear
1168 its SCOPE since the scope is going away now. */
1169 IDENTIFIER_BINDING (DECL_NAME (link))->scope = NULL;
1172 else
1174 /* Remove the binding. */
1175 decl = link;
1176 if (TREE_CODE (decl) == TREE_LIST)
1177 decl = TREE_VALUE (decl);
1178 if (DECL_P (decl))
1179 pop_binding (DECL_NAME (decl), decl);
1180 else if (TREE_CODE (decl) == OVERLOAD)
1181 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1182 else
1183 abort ();
1187 /* Remove declarations for any `for' variables from inner scopes
1188 that we kept around. */
1189 for (link = current_binding_level->dead_vars_from_for;
1190 link; link = TREE_CHAIN (link))
1191 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1193 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1194 for (link = current_binding_level->type_shadowed;
1195 link; link = TREE_CHAIN (link))
1196 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1198 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1199 for (link = current_binding_level->shadowed_labels;
1200 link;
1201 link = TREE_CHAIN (link))
1202 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1204 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1205 list if a `using' declaration put them there. The debugging
1206 back-ends won't understand OVERLOAD, so we remove them here.
1207 Because the BLOCK_VARS are (temporarily) shared with
1208 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1209 popped all the bindings. */
1210 if (block)
1212 tree* d;
1214 for (d = &BLOCK_VARS (block); *d; )
1216 if (TREE_CODE (*d) == TREE_LIST)
1217 *d = TREE_CHAIN (*d);
1218 else
1219 d = &TREE_CHAIN (*d);
1223 /* If the level being exited is the top level of a function,
1224 check over all the labels. */
1225 if (functionbody)
1227 /* Since this is the top level block of a function, the vars are
1228 the function's parameters. Don't leave them in the BLOCK
1229 because they are found in the FUNCTION_DECL instead. */
1230 BLOCK_VARS (block) = 0;
1231 pop_labels (block);
1234 kind = current_binding_level->kind;
1236 leave_scope ();
1237 if (functionbody)
1238 DECL_INITIAL (current_function_decl) = block;
1239 else if (block)
1240 current_binding_level->blocks
1241 = chainon (current_binding_level->blocks, block);
1243 /* If we did not make a block for the level just exited,
1244 any blocks made for inner levels
1245 (since they cannot be recorded as subblocks in that level)
1246 must be carried forward so they will later become subblocks
1247 of something else. */
1248 else if (subblocks)
1249 current_binding_level->blocks
1250 = chainon (current_binding_level->blocks, subblocks);
1252 /* Each and every BLOCK node created here in `poplevel' is important
1253 (e.g. for proper debugging information) so if we created one
1254 earlier, mark it as "used". */
1255 if (block)
1256 TREE_USED (block) = 1;
1258 /* Take care of compiler's internal binding structures. */
1259 if (kind == sk_cleanup)
1261 tree scope_stmts;
1263 scope_stmts
1264 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1265 if (block)
1267 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1268 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1271 block = poplevel (keep, reverse, functionbody);
1274 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1277 /* Delete the node BLOCK from the current binding level.
1278 This is used for the block inside a stmt expr ({...})
1279 so that the block can be reinserted where appropriate. */
1281 void
1282 delete_block (tree block)
1284 tree t;
1285 if (current_binding_level->blocks == block)
1286 current_binding_level->blocks = TREE_CHAIN (block);
1287 for (t = current_binding_level->blocks; t;)
1289 if (TREE_CHAIN (t) == block)
1290 TREE_CHAIN (t) = TREE_CHAIN (block);
1291 else
1292 t = TREE_CHAIN (t);
1294 TREE_CHAIN (block) = NULL_TREE;
1295 /* Clear TREE_USED which is always set by poplevel.
1296 The flag is set again if insert_block is called. */
1297 TREE_USED (block) = 0;
1300 /* Insert BLOCK at the end of the list of subblocks of the
1301 current binding level. This is used when a BIND_EXPR is expanded,
1302 to handle the BLOCK node inside the BIND_EXPR. */
1304 void
1305 insert_block (tree block)
1307 TREE_USED (block) = 1;
1308 current_binding_level->blocks
1309 = chainon (current_binding_level->blocks, block);
1312 /* Set the BLOCK node for the innermost scope
1313 (the one we are currently in). */
1315 void
1316 set_block (tree block ATTRIBUTE_UNUSED )
1318 /* The RTL expansion machinery requires us to provide this callback,
1319 but it is not applicable in function-at-a-time mode. */
1322 /* Do a pushlevel for class declarations. */
1324 void
1325 pushlevel_class (void)
1327 if (ENABLE_SCOPE_CHECKING)
1328 is_class_level = 1;
1330 class_binding_level = begin_scope (sk_class, current_class_type);
1333 /* ...and a poplevel for class declarations. */
1335 void
1336 poplevel_class (void)
1338 register struct cp_binding_level *level = class_binding_level;
1339 tree shadowed;
1341 timevar_push (TV_NAME_LOOKUP);
1342 my_friendly_assert (level != 0, 354);
1344 /* If we're leaving a toplevel class, don't bother to do the setting
1345 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1346 shouldn't even be used when current_class_type isn't set, and second,
1347 if we don't touch it here, we're able to use the cache effect if the
1348 next time we're entering a class scope, it is the same class. */
1349 if (current_class_depth != 1)
1351 struct cp_binding_level* b;
1353 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1354 for (shadowed = level->class_shadowed;
1355 shadowed;
1356 shadowed = TREE_CHAIN (shadowed))
1357 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1359 /* Find the next enclosing class, and recreate
1360 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1361 b = level->level_chain;
1362 while (b && b->kind != sk_class)
1363 b = b->level_chain;
1365 if (b)
1366 for (shadowed = b->class_shadowed;
1367 shadowed;
1368 shadowed = TREE_CHAIN (shadowed))
1370 cxx_binding *binding;
1372 binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1373 while (binding && binding->scope != b)
1374 binding = binding->previous;
1376 if (binding)
1377 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1378 = binding->value;
1381 else
1382 /* Remember to save what IDENTIFIER's were bound in this scope so we
1383 can recover from cache misses. */
1385 previous_class_type = current_class_type;
1386 previous_class_values = class_binding_level->class_shadowed;
1388 for (shadowed = level->type_shadowed;
1389 shadowed;
1390 shadowed = TREE_CHAIN (shadowed))
1391 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1393 /* Remove the bindings for all of the class-level declarations. */
1394 for (shadowed = level->class_shadowed;
1395 shadowed;
1396 shadowed = TREE_CHAIN (shadowed))
1397 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1399 /* Now, pop out of the binding level which we created up in the
1400 `pushlevel_class' routine. */
1401 if (ENABLE_SCOPE_CHECKING)
1402 is_class_level = 1;
1404 leave_scope ();
1405 timevar_pop (TV_NAME_LOOKUP);
1408 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1409 for any names in enclosing classes. */
1411 void
1412 clear_identifier_class_values (void)
1414 tree t;
1416 if (!class_binding_level)
1417 return;
1419 for (t = class_binding_level->class_shadowed;
1421 t = TREE_CHAIN (t))
1422 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1425 /* Returns nonzero if T is a virtual function table. */
1428 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1430 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1433 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1434 functions. */
1437 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1439 return (TREE_CODE (t) == TYPE_DECL
1440 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1441 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1444 /* Return the declarations that are members of the namespace NS. */
1446 tree
1447 cp_namespace_decls (tree ns)
1449 return NAMESPACE_LEVEL (ns)->names;
1452 struct walk_globals_data {
1453 walk_globals_pred p;
1454 walk_globals_fn f;
1455 void *data;
1458 /* Walk the vtable declarations in NAMESPACE. Whenever one is found
1459 for which P returns nonzero, call F with its address. If any call
1460 to F returns a nonzero value, return a nonzero value. */
1462 static int
1463 walk_vtables_r (tree namespace, void* data)
1465 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1466 walk_globals_fn f = wgd->f;
1467 void *d = wgd->data;
1468 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1469 int result = 0;
1471 for (; decl ; decl = TREE_CHAIN (decl))
1472 result |= (*f) (&decl, d);
1474 return result;
1477 /* Walk the vtable declarations. Whenever one is found for which P
1478 returns nonzero, call F with its address. If any call to F
1479 returns a nonzero value, return a nonzero value. */
1480 bool
1481 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1483 struct walk_globals_data wgd;
1484 wgd.p = p;
1485 wgd.f = f;
1486 wgd.data = data;
1488 return walk_namespaces (walk_vtables_r, &wgd);
1491 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1492 itself, calling F for each. The DATA is passed to F as well. */
1494 static int
1495 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1497 int result = 0;
1498 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
1500 result |= (*f) (namespace, data);
1502 for (; current; current = TREE_CHAIN (current))
1503 result |= walk_namespaces_r (current, f, data);
1505 return result;
1508 /* Walk all the namespaces, calling F for each. The DATA is passed to
1509 F as well. */
1512 walk_namespaces (walk_namespaces_fn f, void* data)
1514 return walk_namespaces_r (global_namespace, f, data);
1517 /* Walk the global declarations in NAMESPACE. Whenever one is found
1518 for which P returns nonzero, call F with its address. If any call
1519 to F returns a nonzero value, return a nonzero value. */
1521 static int
1522 walk_globals_r (tree namespace, void* data)
1524 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1525 walk_globals_pred p = wgd->p;
1526 walk_globals_fn f = wgd->f;
1527 void *d = wgd->data;
1528 tree *t;
1529 int result = 0;
1531 t = &NAMESPACE_LEVEL (namespace)->names;
1533 while (*t)
1535 tree glbl = *t;
1537 if ((*p) (glbl, d))
1538 result |= (*f) (t, d);
1540 /* If F changed *T, then *T still points at the next item to
1541 examine. */
1542 if (*t == glbl)
1543 t = &TREE_CHAIN (*t);
1546 return result;
1549 /* Walk the global declarations. Whenever one is found for which P
1550 returns true, call F with its address. If any call to F
1551 returns true, return true. */
1553 bool
1554 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1556 struct walk_globals_data wgd;
1557 wgd.p = p;
1558 wgd.f = f;
1559 wgd.data = data;
1561 return walk_namespaces (walk_globals_r, &wgd);
1564 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1565 DATA is non-NULL, this is the last time we will call
1566 wrapup_global_declarations for this NAMESPACE. */
1569 wrapup_globals_for_namespace (tree namespace, void* data)
1571 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
1572 varray_type statics = level->static_decls;
1573 tree *vec = &VARRAY_TREE (statics, 0);
1574 int len = VARRAY_ACTIVE_SIZE (statics);
1575 int last_time = (data != 0);
1577 if (last_time)
1579 check_global_declarations (vec, len);
1580 return 0;
1583 /* Write out any globals that need to be output. */
1584 return wrapup_global_declarations (vec, len);
1588 /* For debugging. */
1589 static int no_print_functions = 0;
1590 static int no_print_builtins = 0;
1592 /* Called from print_binding_level through binding_table_foreach to
1593 print the content of binding ENTRY. DATA is a pointer to line offset
1594 marker. */
1595 static void
1596 bt_print_entry (binding_entry entry, void *data)
1598 int *p = (int *) data;
1599 int len;
1601 if (entry->name == NULL)
1602 len = 3;
1603 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1604 len = 2;
1605 else
1606 len = 4;
1607 len = 4;
1609 *p += len;
1611 if (*p > 5)
1613 fprintf (stderr, "\n\t");
1614 *p = len;
1616 if (entry->name == NULL)
1618 print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
1619 fprintf (stderr, ">");
1621 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1622 print_node_brief (stderr, "", entry->type, 0);
1623 else
1625 print_node_brief (stderr, "<typedef", entry->name, 0);
1626 print_node_brief (stderr, "", entry->type, 0);
1627 fprintf (stderr, ">");
1631 void
1632 print_binding_level (struct cp_binding_level* lvl)
1634 tree t;
1635 int i = 0, len;
1636 fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks);
1637 if (lvl->more_cleanups_ok)
1638 fprintf (stderr, " more-cleanups-ok");
1639 if (lvl->have_cleanups)
1640 fprintf (stderr, " have-cleanups");
1641 fprintf (stderr, "\n");
1642 if (lvl->names)
1644 fprintf (stderr, " names:\t");
1645 /* We can probably fit 3 names to a line? */
1646 for (t = lvl->names; t; t = TREE_CHAIN (t))
1648 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1649 continue;
1650 if (no_print_builtins
1651 && (TREE_CODE (t) == TYPE_DECL)
1652 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1653 continue;
1655 /* Function decls tend to have longer names. */
1656 if (TREE_CODE (t) == FUNCTION_DECL)
1657 len = 3;
1658 else
1659 len = 2;
1660 i += len;
1661 if (i > 6)
1663 fprintf (stderr, "\n\t");
1664 i = len;
1666 print_node_brief (stderr, "", t, 0);
1667 if (t == error_mark_node)
1668 break;
1670 if (i)
1671 fprintf (stderr, "\n");
1673 if (lvl->type_decls)
1675 fprintf (stderr, " tags:\t");
1676 i = 0;
1677 binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
1678 if (i)
1679 fprintf (stderr, "\n");
1681 if (lvl->class_shadowed)
1683 fprintf (stderr, " class-shadowed:");
1684 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1686 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1688 fprintf (stderr, "\n");
1690 if (lvl->type_shadowed)
1692 fprintf (stderr, " type-shadowed:");
1693 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1695 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1697 fprintf (stderr, "\n");
1701 void
1702 print_other_binding_stack (struct cp_binding_level *stack)
1704 struct cp_binding_level *level;
1705 for (level = stack; !global_scope_p (level); level = level->level_chain)
1707 fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level);
1708 print_binding_level (level);
1712 void
1713 print_binding_stack (void)
1715 struct cp_binding_level *b;
1716 fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF
1717 "\nclass_binding_level=" HOST_PTR_PRINTF
1718 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n",
1719 (void *) current_binding_level, (void *) class_binding_level,
1720 (void *) NAMESPACE_LEVEL (global_namespace));
1721 if (class_binding_level)
1723 for (b = class_binding_level; b; b = b->level_chain)
1724 if (b == current_binding_level)
1725 break;
1726 if (b)
1727 b = class_binding_level;
1728 else
1729 b = current_binding_level;
1731 else
1732 b = current_binding_level;
1733 print_other_binding_stack (b);
1734 fprintf (stderr, "global:\n");
1735 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1738 /* Namespace binding access routines: The namespace_bindings field of
1739 the identifier is polymorphic, with three possible values:
1740 NULL_TREE, a list of "cxx_binding"s. */
1742 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
1743 select a name that is unique to this compilation unit. */
1745 void
1746 push_namespace (tree name)
1748 tree d = NULL_TREE;
1749 int need_new = 1;
1750 int implicit_use = 0;
1752 timevar_push (TV_NAME_LOOKUP);
1754 /* We should not get here if the global_namespace is not yet constructed
1755 nor if NAME designates the global namespace: The global scope is
1756 constructed elsewhere. */
1757 my_friendly_assert (global_namespace != NULL && name != global_scope_name,
1758 20030531);
1760 if (!name)
1762 /* The name of anonymous namespace is unique for the translation
1763 unit. */
1764 if (!anonymous_namespace_name)
1765 anonymous_namespace_name = get_file_function_name ('N');
1766 name = anonymous_namespace_name;
1767 d = IDENTIFIER_NAMESPACE_VALUE (name);
1768 if (d)
1769 /* Reopening anonymous namespace. */
1770 need_new = 0;
1771 implicit_use = 1;
1773 else
1775 /* Check whether this is an extended namespace definition. */
1776 d = IDENTIFIER_NAMESPACE_VALUE (name);
1777 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
1779 need_new = 0;
1780 if (DECL_NAMESPACE_ALIAS (d))
1782 error ("namespace alias `%D' not allowed here, assuming `%D'",
1783 d, DECL_NAMESPACE_ALIAS (d));
1784 d = DECL_NAMESPACE_ALIAS (d);
1789 if (need_new)
1791 /* Make a new namespace, binding the name to it. */
1792 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
1793 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
1794 d = pushdecl (d);
1795 begin_scope (sk_namespace, d);
1797 else
1798 resume_binding_level (NAMESPACE_LEVEL (d));
1800 if (implicit_use)
1801 do_using_directive (d);
1802 /* Enter the name space. */
1803 current_namespace = d;
1805 timevar_pop (TV_NAME_LOOKUP);
1808 /* Pop from the scope of the current namespace. */
1810 void
1811 pop_namespace (void)
1813 my_friendly_assert (current_namespace != global_namespace, 20010801);
1814 current_namespace = CP_DECL_CONTEXT (current_namespace);
1815 /* The binding level is not popped, as it might be re-opened later. */
1816 leave_scope ();
1819 /* Push into the scope of the namespace NS, even if it is deeply
1820 nested within another namespace. */
1822 void
1823 push_nested_namespace (tree ns)
1825 if (ns == global_namespace)
1826 push_to_top_level ();
1827 else
1829 push_nested_namespace (CP_DECL_CONTEXT (ns));
1830 push_namespace (DECL_NAME (ns));
1834 /* Pop back from the scope of the namespace NS, which was previously
1835 entered with push_nested_namespace. */
1837 void
1838 pop_nested_namespace (tree ns)
1840 timevar_push (TV_NAME_LOOKUP);
1841 while (ns != global_namespace)
1843 pop_namespace ();
1844 ns = CP_DECL_CONTEXT (ns);
1847 pop_from_top_level ();
1848 timevar_pop (TV_NAME_LOOKUP);
1852 /* Allocate storage for saving a C++ binding. */
1853 #define cxx_saved_binding_make() \
1854 (ggc_alloc (sizeof (cxx_saved_binding)))
1856 struct cxx_saved_binding GTY(())
1858 /* Link that chains saved C++ bindings for a given name into a stack. */
1859 cxx_saved_binding *previous;
1860 /* The name of the current binding. */
1861 tree identifier;
1862 /* The binding we're saving. */
1863 cxx_binding *binding;
1864 tree class_value;
1865 tree real_type_value;
1868 /* Subroutines for reverting temporarily to top-level for instantiation
1869 of templates and such. We actually need to clear out the class- and
1870 local-value slots of all identifiers, so that only the global values
1871 are at all visible. Simply setting current_binding_level to the global
1872 scope isn't enough, because more binding levels may be pushed. */
1873 struct saved_scope *scope_chain;
1875 static cxx_saved_binding *
1876 store_bindings (tree names, cxx_saved_binding *old_bindings)
1878 tree t;
1879 cxx_saved_binding *search_bindings = old_bindings;
1881 timevar_push (TV_NAME_LOOKUP);
1882 for (t = names; t; t = TREE_CHAIN (t))
1884 tree id;
1885 cxx_saved_binding *saved;
1886 cxx_saved_binding *t1;
1888 if (TREE_CODE (t) == TREE_LIST)
1889 id = TREE_PURPOSE (t);
1890 else
1891 id = DECL_NAME (t);
1893 if (!id
1894 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
1895 we have no IDENTIFIER_BINDING if we have left the class
1896 scope, but cached the class-level declarations. */
1897 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
1898 continue;
1900 for (t1 = search_bindings; t1; t1 = t1->previous)
1901 if (t1->identifier == id)
1902 goto skip_it;
1904 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
1905 saved = cxx_saved_binding_make ();
1906 saved->previous = old_bindings;
1907 saved->identifier = id;
1908 saved->binding = IDENTIFIER_BINDING (id);
1909 saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
1910 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1911 IDENTIFIER_BINDING (id) = NULL;
1912 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
1913 old_bindings = saved;
1914 skip_it:
1917 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
1920 void
1921 maybe_push_to_top_level (int pseudo)
1923 struct saved_scope *s;
1924 struct cp_binding_level *b;
1925 cxx_saved_binding *old_bindings;
1926 int need_pop;
1928 timevar_push (TV_NAME_LOOKUP);
1929 s = ggc_alloc_cleared (sizeof (struct saved_scope));
1931 b = scope_chain ? current_binding_level : 0;
1933 /* If we're in the middle of some function, save our state. */
1934 if (cfun)
1936 need_pop = 1;
1937 push_function_context_to (NULL_TREE);
1939 else
1940 need_pop = 0;
1942 old_bindings = NULL;
1943 if (scope_chain && previous_class_type)
1944 old_bindings = store_bindings (previous_class_values, old_bindings);
1946 /* Have to include the global scope, because class-scope decls
1947 aren't listed anywhere useful. */
1948 for (; b; b = b->level_chain)
1950 tree t;
1952 /* Template IDs are inserted into the global level. If they were
1953 inserted into namespace level, finish_file wouldn't find them
1954 when doing pending instantiations. Therefore, don't stop at
1955 namespace level, but continue until :: . */
1956 if (global_scope_p (b) || (pseudo && b->kind == sk_template_parms))
1957 break;
1959 old_bindings = store_bindings (b->names, old_bindings);
1960 /* We also need to check class_shadowed to save class-level type
1961 bindings, since pushclass doesn't fill in b->names. */
1962 if (b->kind == sk_class)
1963 old_bindings = store_bindings (b->class_shadowed, old_bindings);
1965 /* Unwind type-value slots back to top level. */
1966 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
1967 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
1969 s->prev = scope_chain;
1970 s->old_bindings = old_bindings;
1971 s->bindings = b;
1972 s->need_pop_function_context = need_pop;
1973 s->function_decl = current_function_decl;
1974 s->last_parms = last_function_parms;
1976 scope_chain = s;
1977 current_function_decl = NULL_TREE;
1978 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
1979 current_lang_name = lang_name_cplusplus;
1980 current_namespace = global_namespace;
1981 timevar_pop (TV_NAME_LOOKUP);
1984 void
1985 push_to_top_level (void)
1987 maybe_push_to_top_level (0);
1990 void
1991 pop_from_top_level (void)
1993 struct saved_scope *s = scope_chain;
1994 cxx_saved_binding *saved;
1996 timevar_push (TV_NAME_LOOKUP);
1997 /* Clear out class-level bindings cache. */
1998 if (previous_class_type)
1999 invalidate_class_lookup_cache ();
2001 current_lang_base = 0;
2003 scope_chain = s->prev;
2004 for (saved = s->old_bindings; saved; saved = saved->previous)
2006 tree id = saved->identifier;
2008 IDENTIFIER_BINDING (id) = saved->binding;
2009 IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2010 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2013 /* If we were in the middle of compiling a function, restore our
2014 state. */
2015 if (s->need_pop_function_context)
2016 pop_function_context_from (NULL_TREE);
2017 current_function_decl = s->function_decl;
2018 last_function_parms = s->last_parms;
2019 timevar_pop (TV_NAME_LOOKUP);
2022 /* Push a definition of struct, union or enum tag named ID. into
2023 binding_level B. DECL is a TYPE_DECL for the type. We assume that
2024 the tag ID is not already defined. */
2026 static void
2027 set_identifier_type_value_with_scope (tree id,
2028 tree decl,
2029 struct cp_binding_level* b)
2031 tree type;
2033 if (b->kind != sk_namespace)
2035 /* Shadow the marker, not the real thing, so that the marker
2036 gets restored later. */
2037 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2038 b->type_shadowed
2039 = tree_cons (id, old_type_value, b->type_shadowed);
2040 type = decl ? TREE_TYPE (decl) : NULL_TREE;
2042 else
2044 cxx_binding *binding =
2045 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2046 if (decl)
2048 if (binding->value)
2049 supplement_binding (binding, decl);
2050 else
2051 binding->value = decl;
2053 else
2054 abort ();
2055 /* Store marker instead of real type. */
2056 type = global_type_node;
2058 SET_IDENTIFIER_TYPE_VALUE (id, type);
2061 /* As set_identifier_type_value_with_scope, but using current_binding_level. */
2063 void
2064 set_identifier_type_value (tree id, tree decl)
2066 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2069 /* Return the type associated with id. */
2071 tree
2072 identifier_type_value (tree id)
2074 timevar_push (TV_NAME_LOOKUP);
2075 /* There is no type with that name, anywhere. */
2076 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2077 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2078 /* This is not the type marker, but the real thing. */
2079 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2080 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2081 /* Have to search for it. It must be on the global level, now.
2082 Ask lookup_name not to return non-types. */
2083 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2084 if (id)
2085 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2086 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2089 /* Pop off extraneous binding levels left over due to syntax errors.
2091 We don't pop past namespaces, as they might be valid. */
2093 void
2094 pop_everything (void)
2096 if (ENABLE_SCOPE_CHECKING)
2097 verbatim ("XXX entering pop_everything ()\n");
2098 while (!toplevel_bindings_p ())
2100 if (current_binding_level->kind == sk_class)
2101 pop_nested_class ();
2102 else
2103 poplevel (0, 0, 0);
2105 if (ENABLE_SCOPE_CHECKING)
2106 verbatim ("XXX leaving pop_everything ()\n");
2109 /* The type TYPE is being declared. If it is a class template, or a
2110 specialization of a class template, do any processing required and
2111 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
2112 being declared a friend. B is the binding level at which this TYPE
2113 should be bound.
2115 Returns the TYPE_DECL for TYPE, which may have been altered by this
2116 processing. */
2118 static tree
2119 maybe_process_template_type_declaration (tree type,
2120 int globalize,
2121 struct cp_binding_level* b)
2123 tree decl = TYPE_NAME (type);
2125 if (processing_template_parmlist)
2126 /* You can't declare a new template type in a template parameter
2127 list. But, you can declare a non-template type:
2129 template <class A*> struct S;
2131 is a forward-declaration of `A'. */
2133 else
2135 maybe_check_template_type (type);
2137 my_friendly_assert (IS_AGGR_TYPE (type)
2138 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2141 if (processing_template_decl)
2143 /* This may change after the call to
2144 push_template_decl_real, but we want the original value. */
2145 tree name = DECL_NAME (decl);
2147 decl = push_template_decl_real (decl, globalize);
2148 /* If the current binding level is the binding level for the
2149 template parameters (see the comment in
2150 begin_template_parm_list) and the enclosing level is a class
2151 scope, and we're not looking at a friend, push the
2152 declaration of the member class into the class scope. In the
2153 friend case, push_template_decl will already have put the
2154 friend into global scope, if appropriate. */
2155 if (TREE_CODE (type) != ENUMERAL_TYPE
2156 && !globalize && b->kind == sk_template_parms
2157 && b->level_chain->kind == sk_class)
2159 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2160 /* Put this UDT in the table of UDTs for the class, since
2161 that won't happen below because B is not the class
2162 binding level, but is instead the pseudo-global level. */
2163 if (b->level_chain->type_decls == NULL)
2164 b->level_chain->type_decls =
2165 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2166 binding_table_insert (b->level_chain->type_decls, name, type);
2167 if (!COMPLETE_TYPE_P (current_class_type))
2169 maybe_add_class_template_decl_list (current_class_type,
2170 type, /*friend_p=*/0);
2171 CLASSTYPE_NESTED_UTDS (current_class_type) =
2172 b->level_chain->type_decls;
2178 return decl;
2181 /* In C++, you don't have to write `struct S' to refer to `S'; you
2182 can just use `S'. We accomplish this by creating a TYPE_DECL as
2183 if the user had written `typedef struct S S'. Create and return
2184 the TYPE_DECL for TYPE. */
2186 tree
2187 create_implicit_typedef (tree name, tree type)
2189 tree decl;
2191 decl = build_decl (TYPE_DECL, name, type);
2192 DECL_ARTIFICIAL (decl) = 1;
2193 /* There are other implicit type declarations, like the one *within*
2194 a class that allows you to write `S::S'. We must distinguish
2195 amongst these. */
2196 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2197 TYPE_NAME (type) = decl;
2199 return decl;
2202 /* Remember a local name for name-mangling purposes. */
2204 static void
2205 push_local_name (tree decl)
2207 size_t i, nelts;
2208 tree t, name;
2210 timevar_push (TV_NAME_LOOKUP);
2211 if (!local_names)
2212 VARRAY_TREE_INIT (local_names, 8, "local_names");
2214 name = DECL_NAME (decl);
2216 nelts = VARRAY_ACTIVE_SIZE (local_names);
2217 for (i = 0; i < nelts; i++)
2219 t = VARRAY_TREE (local_names, i);
2220 if (DECL_NAME (t) == name)
2222 if (!DECL_LANG_SPECIFIC (decl))
2223 retrofit_lang_decl (decl);
2224 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2225 if (DECL_LANG_SPECIFIC (t))
2226 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2227 else
2228 DECL_DISCRIMINATOR (decl) = 1;
2230 VARRAY_TREE (local_names, i) = decl;
2231 timevar_pop (TV_NAME_LOOKUP);
2232 return;
2236 VARRAY_PUSH_TREE (local_names, decl);
2237 timevar_pop (TV_NAME_LOOKUP);
2240 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2241 Normally put it into the inner-most non-sk_cleanup scope,
2242 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2243 The latter is needed for implicit declarations. */
2245 void
2246 pushtag (tree name, tree type, int globalize)
2248 register struct cp_binding_level *b;
2250 timevar_push (TV_NAME_LOOKUP);
2251 b = current_binding_level;
2252 while (b->kind == sk_cleanup
2253 || (b->kind == sk_class
2254 && (globalize
2255 /* We may be defining a new type in the initializer
2256 of a static member variable. We allow this when
2257 not pedantic, and it is particularly useful for
2258 type punning via an anonymous union. */
2259 || COMPLETE_TYPE_P (b->this_entity))))
2260 b = b->level_chain;
2262 if (b->type_decls == NULL)
2263 b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2264 binding_table_insert (b->type_decls, name, type);
2266 if (name)
2268 /* Do C++ gratuitous typedefing. */
2269 if (IDENTIFIER_TYPE_VALUE (name) != type)
2271 register tree d = NULL_TREE;
2272 int in_class = 0;
2273 tree context = TYPE_CONTEXT (type);
2275 if (! context)
2277 tree cs = current_scope ();
2279 if (! globalize)
2280 context = cs;
2281 else if (cs != NULL_TREE && TYPE_P (cs))
2282 /* When declaring a friend class of a local class, we want
2283 to inject the newly named class into the scope
2284 containing the local class, not the namespace scope. */
2285 context = decl_function_context (get_type_decl (cs));
2287 if (!context)
2288 context = current_namespace;
2290 if (b->kind == sk_class
2291 || (b->kind == sk_template_parms
2292 && b->level_chain->kind == sk_class))
2293 in_class = 1;
2295 if (current_lang_name == lang_name_java)
2296 TYPE_FOR_JAVA (type) = 1;
2298 d = create_implicit_typedef (name, type);
2299 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2300 if (! in_class)
2301 set_identifier_type_value_with_scope (name, d, b);
2303 d = maybe_process_template_type_declaration (type,
2304 globalize, b);
2306 if (b->kind == sk_class)
2308 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2309 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2310 class. But if it's a member template class, we
2311 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2312 is done later. */
2313 finish_member_declaration (d);
2314 else
2315 pushdecl_class_level (d);
2317 else
2318 d = pushdecl_with_scope (d, b);
2320 /* FIXME what if it gets a name from typedef? */
2321 if (ANON_AGGRNAME_P (name))
2322 DECL_IGNORED_P (d) = 1;
2324 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2326 /* If this is a local class, keep track of it. We need this
2327 information for name-mangling, and so that it is possible to find
2328 all function definitions in a translation unit in a convenient
2329 way. (It's otherwise tricky to find a member function definition
2330 it's only pointed to from within a local class.) */
2331 if (TYPE_CONTEXT (type)
2332 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2333 && !processing_template_decl)
2334 VARRAY_PUSH_TREE (local_classes, type);
2336 if (b->kind == sk_class
2337 && !COMPLETE_TYPE_P (current_class_type))
2339 maybe_add_class_template_decl_list (current_class_type,
2340 type, /*friend_p=*/0);
2341 CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
2345 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2346 /* Use the canonical TYPE_DECL for this node. */
2347 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2348 else
2350 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2351 will be the tagged type we just added to the current
2352 binding level. This fake NULL-named TYPE_DECL node helps
2353 dwarfout.c to know when it needs to output a
2354 representation of a tagged type, and it also gives us a
2355 convenient place to record the "scope start" address for
2356 the tagged type. */
2358 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2359 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2361 timevar_pop (TV_NAME_LOOKUP);
2364 /* Counter used to create anonymous type names. */
2366 static GTY(()) int anon_cnt;
2368 /* Return an IDENTIFIER which can be used as a name for
2369 anonymous structs and unions. */
2371 tree
2372 make_anon_name (void)
2374 char buf[32];
2376 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2377 return get_identifier (buf);
2380 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
2381 This keeps dbxout from getting confused. */
2383 void
2384 clear_anon_tags (void)
2386 register struct cp_binding_level *b;
2387 static int last_cnt = 0;
2389 /* Fast out if no new anon names were declared. */
2390 if (last_cnt == anon_cnt)
2391 return;
2393 b = current_binding_level;
2394 while (b->kind == sk_cleanup)
2395 b = b->level_chain;
2396 if (b->type_decls != NULL)
2397 binding_table_remove_anonymous_types (b->type_decls);
2398 last_cnt = anon_cnt;
2401 /* Subroutine of duplicate_decls: return truthvalue of whether
2402 or not types of these decls match.
2404 For C++, we must compare the parameter list so that `int' can match
2405 `int&' in a parameter position, but `int&' is not confused with
2406 `const int&'. */
2409 decls_match (tree newdecl, tree olddecl)
2411 int types_match;
2413 if (newdecl == olddecl)
2414 return 1;
2416 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2417 /* If the two DECLs are not even the same kind of thing, we're not
2418 interested in their types. */
2419 return 0;
2421 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2423 tree f1 = TREE_TYPE (newdecl);
2424 tree f2 = TREE_TYPE (olddecl);
2425 tree p1 = TYPE_ARG_TYPES (f1);
2426 tree p2 = TYPE_ARG_TYPES (f2);
2428 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2429 && ! (DECL_EXTERN_C_P (newdecl)
2430 && DECL_EXTERN_C_P (olddecl)))
2431 return 0;
2433 if (TREE_CODE (f1) != TREE_CODE (f2))
2434 return 0;
2436 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2438 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2439 && (DECL_BUILT_IN (olddecl)
2440 #ifndef NO_IMPLICIT_EXTERN_C
2441 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2442 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2443 #endif
2446 types_match = self_promoting_args_p (p1);
2447 if (p1 == void_list_node)
2448 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2450 #ifndef NO_IMPLICIT_EXTERN_C
2451 else if (p1 == NULL_TREE
2452 && (DECL_EXTERN_C_P (olddecl)
2453 && DECL_IN_SYSTEM_HEADER (olddecl)
2454 && !DECL_CLASS_SCOPE_P (olddecl))
2455 && (DECL_EXTERN_C_P (newdecl)
2456 && DECL_IN_SYSTEM_HEADER (newdecl)
2457 && !DECL_CLASS_SCOPE_P (newdecl)))
2459 types_match = self_promoting_args_p (p2);
2460 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2462 #endif
2463 else
2464 types_match = compparms (p1, p2);
2466 else
2467 types_match = 0;
2469 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2471 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2472 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2473 return 0;
2475 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2476 DECL_TEMPLATE_PARMS (olddecl)))
2477 return 0;
2479 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2480 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
2481 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
2482 else
2483 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2484 DECL_TEMPLATE_RESULT (newdecl));
2486 else
2488 if (TREE_TYPE (newdecl) == error_mark_node)
2489 types_match = TREE_TYPE (olddecl) == error_mark_node;
2490 else if (TREE_TYPE (olddecl) == NULL_TREE)
2491 types_match = TREE_TYPE (newdecl) == NULL_TREE;
2492 else if (TREE_TYPE (newdecl) == NULL_TREE)
2493 types_match = 0;
2494 else
2495 types_match = comptypes (TREE_TYPE (newdecl),
2496 TREE_TYPE (olddecl),
2497 COMPARE_REDECLARATION);
2500 return types_match;
2503 /* If NEWDECL is `static' and an `extern' was seen previously,
2504 warn about it. OLDDECL is the previous declaration.
2506 Note that this does not apply to the C++ case of declaring
2507 a variable `extern const' and then later `const'.
2509 Don't complain about built-in functions, since they are beyond
2510 the user's control. */
2512 static void
2513 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2515 static const char *const explicit_extern_static_warning
2516 = "`%D' was declared `extern' and later `static'";
2517 static const char *const implicit_extern_static_warning
2518 = "`%D' was declared implicitly `extern' and later `static'";
2520 tree name;
2522 if (TREE_CODE (newdecl) == TYPE_DECL
2523 || TREE_CODE (newdecl) == TEMPLATE_DECL
2524 || TREE_CODE (newdecl) == CONST_DECL
2525 || TREE_CODE (newdecl) == NAMESPACE_DECL)
2526 return;
2528 /* Don't get confused by static member functions; that's a different
2529 use of `static'. */
2530 if (TREE_CODE (newdecl) == FUNCTION_DECL
2531 && DECL_STATIC_FUNCTION_P (newdecl))
2532 return;
2534 /* If the old declaration was `static', or the new one isn't, then
2535 then everything is OK. */
2536 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2537 return;
2539 /* It's OK to declare a builtin function as `static'. */
2540 if (TREE_CODE (olddecl) == FUNCTION_DECL
2541 && DECL_ARTIFICIAL (olddecl))
2542 return;
2544 name = DECL_ASSEMBLER_NAME (newdecl);
2545 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2546 ? implicit_extern_static_warning
2547 : explicit_extern_static_warning, newdecl);
2548 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2551 /* Handle when a new declaration NEWDECL has the same name as an old
2552 one OLDDECL in the same binding contour. Prints an error message
2553 if appropriate.
2555 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2556 Otherwise, return 0. */
2559 duplicate_decls (tree newdecl, tree olddecl)
2561 unsigned olddecl_uid = DECL_UID (olddecl);
2562 int olddecl_friend = 0, types_match = 0;
2563 int new_defines_function = 0;
2565 if (newdecl == olddecl)
2566 return 1;
2568 types_match = decls_match (newdecl, olddecl);
2570 /* If either the type of the new decl or the type of the old decl is an
2571 error_mark_node, then that implies that we have already issued an
2572 error (earlier) for some bogus type specification, and in that case,
2573 it is rather pointless to harass the user with yet more error message
2574 about the same declaration, so just pretend the types match here. */
2575 if (TREE_TYPE (newdecl) == error_mark_node
2576 || TREE_TYPE (olddecl) == error_mark_node)
2577 types_match = 1;
2579 if (DECL_P (olddecl)
2580 && TREE_CODE (newdecl) == FUNCTION_DECL
2581 && TREE_CODE (olddecl) == FUNCTION_DECL
2582 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2584 if (DECL_DECLARED_INLINE_P (newdecl)
2585 && DECL_UNINLINABLE (newdecl)
2586 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2587 /* Already warned elsewhere. */;
2588 else if (DECL_DECLARED_INLINE_P (olddecl)
2589 && DECL_UNINLINABLE (olddecl)
2590 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2591 /* Already warned. */;
2592 else if (DECL_DECLARED_INLINE_P (newdecl)
2593 && DECL_UNINLINABLE (olddecl)
2594 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2596 warning ("%Jfunction '%D' redeclared as inline", newdecl, newdecl);
2597 warning ("%Jprevious declaration of '%D' with attribute noinline",
2598 olddecl, olddecl);
2600 else if (DECL_DECLARED_INLINE_P (olddecl)
2601 && DECL_UNINLINABLE (newdecl)
2602 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2604 warning ("%Jfunction '%D' redeclared with attribute noinline",
2605 newdecl, newdecl);
2606 warning ("%Jprevious declaration of '%D' was inline",
2607 olddecl, olddecl);
2611 /* Check for redeclaration and other discrepancies. */
2612 if (TREE_CODE (olddecl) == FUNCTION_DECL
2613 && DECL_ARTIFICIAL (olddecl))
2615 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2617 /* Avoid warnings redeclaring anticipated built-ins. */
2618 if (DECL_ANTICIPATED (olddecl))
2619 return 0;
2621 /* If you declare a built-in or predefined function name as static,
2622 the old definition is overridden, but optionally warn this was a
2623 bad choice of name. */
2624 if (! TREE_PUBLIC (newdecl))
2626 if (warn_shadow)
2627 warning ("shadowing %s function `%#D'",
2628 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2629 olddecl);
2630 /* Discard the old built-in function. */
2631 return 0;
2633 /* If the built-in is not ansi, then programs can override
2634 it even globally without an error. */
2635 else if (! DECL_BUILT_IN (olddecl))
2636 warning ("library function `%#D' redeclared as non-function `%#D'",
2637 olddecl, newdecl);
2638 else
2640 error ("declaration of `%#D'", newdecl);
2641 error ("conflicts with built-in declaration `%#D'",
2642 olddecl);
2644 return 0;
2646 else if (!types_match)
2648 /* Avoid warnings redeclaring anticipated built-ins. */
2649 if (DECL_ANTICIPATED (olddecl))
2650 ; /* Do nothing yet. */
2651 else if ((DECL_EXTERN_C_P (newdecl)
2652 && DECL_EXTERN_C_P (olddecl))
2653 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2654 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2656 /* A near match; override the builtin. */
2658 if (TREE_PUBLIC (newdecl))
2660 warning ("new declaration `%#D'", newdecl);
2661 warning ("ambiguates built-in declaration `%#D'",
2662 olddecl);
2664 else if (warn_shadow)
2665 warning ("shadowing %s function `%#D'",
2666 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2667 olddecl);
2669 else
2670 /* Discard the old built-in function. */
2671 return 0;
2673 /* Replace the old RTL to avoid problems with inlining. */
2674 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2676 /* Even if the types match, prefer the new declarations type
2677 for anticipated built-ins, for exception lists, etc... */
2678 else if (DECL_ANTICIPATED (olddecl))
2679 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2681 /* Whether or not the builtin can throw exceptions has no
2682 bearing on this declarator. */
2683 TREE_NOTHROW (olddecl) = 0;
2685 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2687 /* If a builtin function is redeclared as `static', merge
2688 the declarations, but make the original one static. */
2689 DECL_THIS_STATIC (olddecl) = 1;
2690 TREE_PUBLIC (olddecl) = 0;
2692 /* Make the old declaration consistent with the new one so
2693 that all remnants of the builtin-ness of this function
2694 will be banished. */
2695 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2696 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2699 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2701 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
2702 && TREE_CODE (newdecl) != TYPE_DECL
2703 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
2704 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
2705 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
2706 && TREE_CODE (olddecl) != TYPE_DECL
2707 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
2708 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2709 == TYPE_DECL))))
2711 /* We do nothing special here, because C++ does such nasty
2712 things with TYPE_DECLs. Instead, just let the TYPE_DECL
2713 get shadowed, and know that if we need to find a TYPE_DECL
2714 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2715 slot of the identifier. */
2716 return 0;
2719 if ((TREE_CODE (newdecl) == FUNCTION_DECL
2720 && DECL_FUNCTION_TEMPLATE_P (olddecl))
2721 || (TREE_CODE (olddecl) == FUNCTION_DECL
2722 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2723 return 0;
2725 error ("`%#D' redeclared as different kind of symbol", newdecl);
2726 if (TREE_CODE (olddecl) == TREE_LIST)
2727 olddecl = TREE_VALUE (olddecl);
2728 cp_error_at ("previous declaration of `%#D'", olddecl);
2730 /* New decl is completely inconsistent with the old one =>
2731 tell caller to replace the old one. */
2733 return 0;
2735 else if (!types_match)
2737 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
2738 /* These are certainly not duplicate declarations; they're
2739 from different scopes. */
2740 return 0;
2742 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2744 /* The name of a class template may not be declared to refer to
2745 any other template, class, function, object, namespace, value,
2746 or type in the same scope. */
2747 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
2748 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2750 error ("declaration of template `%#D'", newdecl);
2751 cp_error_at ("conflicts with previous declaration `%#D'",
2752 olddecl);
2754 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
2755 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
2756 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
2757 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
2758 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2759 DECL_TEMPLATE_PARMS (olddecl))
2760 /* Template functions can be disambiguated by
2761 return type. */
2762 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
2763 TREE_TYPE (TREE_TYPE (olddecl))))
2765 error ("new declaration `%#D'", newdecl);
2766 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
2768 return 0;
2770 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2772 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
2774 error ("declaration of C function `%#D' conflicts with",
2775 newdecl);
2776 cp_error_at ("previous declaration `%#D' here", olddecl);
2778 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2779 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2781 error ("new declaration `%#D'", newdecl);
2782 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
2784 else
2785 return 0;
2788 /* Already complained about this, so don't do so again. */
2789 else if (current_class_type == NULL_TREE
2790 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
2792 error ("conflicting declaration '%#D'", newdecl);
2793 cp_error_at ("'%D' has a previous declaration as `%#D'",
2794 olddecl, olddecl);
2795 return false;
2798 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2799 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2800 && (!DECL_TEMPLATE_INFO (newdecl)
2801 || (DECL_TI_TEMPLATE (newdecl)
2802 != DECL_TI_TEMPLATE (olddecl))))
2803 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2804 && (!DECL_TEMPLATE_INFO (olddecl)
2805 || (DECL_TI_TEMPLATE (olddecl)
2806 != DECL_TI_TEMPLATE (newdecl))))))
2807 /* It's OK to have a template specialization and a non-template
2808 with the same type, or to have specializations of two
2809 different templates with the same type. Note that if one is a
2810 specialization, and the other is an instantiation of the same
2811 template, that we do not exit at this point. That situation
2812 can occur if we instantiate a template class, and then
2813 specialize one of its methods. This situation is valid, but
2814 the declarations must be merged in the usual way. */
2815 return 0;
2816 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2817 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2818 && !DECL_USE_TEMPLATE (newdecl))
2819 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2820 && !DECL_USE_TEMPLATE (olddecl))))
2821 /* One of the declarations is a template instantiation, and the
2822 other is not a template at all. That's OK. */
2823 return 0;
2824 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
2825 && DECL_NAMESPACE_ALIAS (newdecl)
2826 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
2827 /* Redeclaration of namespace alias, ignore it. */
2828 return 1;
2829 else
2831 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2832 if (errmsg)
2834 error (errmsg, newdecl);
2835 if (DECL_NAME (olddecl) != NULL_TREE)
2836 cp_error_at ((DECL_INITIAL (olddecl)
2837 && namespace_bindings_p ())
2838 ? "`%#D' previously defined here"
2839 : "`%#D' previously declared here", olddecl);
2840 return 0;
2842 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2843 && DECL_INITIAL (olddecl) != NULL_TREE
2844 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
2845 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
2847 /* Prototype decl follows defn w/o prototype. */
2848 cp_warning_at ("prototype for `%#D'", newdecl);
2849 warning ("%Jfollows non-prototype definition here", olddecl);
2851 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2852 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2854 /* extern "C" int foo ();
2855 int foo () { bar (); }
2856 is OK. */
2857 if (current_lang_depth () == 0)
2858 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2859 else
2861 cp_error_at ("previous declaration of `%#D' with %L linkage",
2862 olddecl, DECL_LANGUAGE (olddecl));
2863 error ("conflicts with new declaration with %L linkage",
2864 DECL_LANGUAGE (newdecl));
2868 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2870 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2872 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
2873 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
2874 int i = 1;
2876 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
2877 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
2879 for (; t1 && t1 != void_list_node;
2880 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2881 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2883 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
2884 TREE_PURPOSE (t2)))
2886 pedwarn ("default argument given for parameter %d of `%#D'",
2887 i, newdecl);
2888 cp_pedwarn_at ("after previous specification in `%#D'",
2889 olddecl);
2891 else
2893 error ("default argument given for parameter %d of `%#D'",
2894 i, newdecl);
2895 cp_error_at ("after previous specification in `%#D'",
2896 olddecl);
2900 if (DECL_DECLARED_INLINE_P (newdecl)
2901 && ! DECL_DECLARED_INLINE_P (olddecl)
2902 && TREE_ADDRESSABLE (olddecl) && warn_inline)
2904 warning ("`%#D' was used before it was declared inline", newdecl);
2905 warning ("%Jprevious non-inline declaration here", olddecl);
2910 /* Do not merge an implicit typedef with an explicit one. In:
2912 class A;
2914 typedef class A A __attribute__ ((foo));
2916 the attribute should apply only to the typedef. */
2917 if (TREE_CODE (olddecl) == TYPE_DECL
2918 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2919 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2920 return 0;
2922 /* If new decl is `static' and an `extern' was seen previously,
2923 warn about it. */
2924 warn_extern_redeclared_static (newdecl, olddecl);
2926 /* We have committed to returning 1 at this point. */
2927 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2929 /* Now that functions must hold information normally held
2930 by field decls, there is extra work to do so that
2931 declaration information does not get destroyed during
2932 definition. */
2933 if (DECL_VINDEX (olddecl))
2934 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2935 if (DECL_CONTEXT (olddecl))
2936 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2937 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2938 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2939 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2940 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2941 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
2942 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2943 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
2944 SET_OVERLOADED_OPERATOR_CODE
2945 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
2946 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2948 /* Optionally warn about more than one declaration for the same
2949 name, but don't warn about a function declaration followed by a
2950 definition. */
2951 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2952 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2953 /* Don't warn about extern decl followed by definition. */
2954 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2955 /* Don't warn about friends, let add_friend take care of it. */
2956 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
2958 warning ("redundant redeclaration of `%D' in same scope", newdecl);
2959 cp_warning_at ("previous declaration of `%D'", olddecl);
2963 /* Deal with C++: must preserve virtual function table size. */
2964 if (TREE_CODE (olddecl) == TYPE_DECL)
2966 register tree newtype = TREE_TYPE (newdecl);
2967 register tree oldtype = TREE_TYPE (olddecl);
2969 if (newtype != error_mark_node && oldtype != error_mark_node
2970 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2971 CLASSTYPE_FRIEND_CLASSES (newtype)
2972 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2974 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2977 /* Copy all the DECL_... slots specified in the new decl
2978 except for any that we copy here from the old type. */
2979 DECL_ATTRIBUTES (newdecl)
2980 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2982 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2984 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
2985 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
2986 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
2987 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2989 /* If the new declaration is a definition, update the file and
2990 line information on the declaration. */
2991 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
2992 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
2994 DECL_SOURCE_LOCATION (olddecl)
2995 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
2996 = DECL_SOURCE_LOCATION (newdecl);
2999 return 1;
3002 if (types_match)
3004 /* Automatically handles default parameters. */
3005 tree oldtype = TREE_TYPE (olddecl);
3006 tree newtype;
3008 /* Merge the data types specified in the two decls. */
3009 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3011 /* If merge_types produces a non-typedef type, just use the old type. */
3012 if (TREE_CODE (newdecl) == TYPE_DECL
3013 && newtype == DECL_ORIGINAL_TYPE (newdecl))
3014 newtype = oldtype;
3016 if (TREE_CODE (newdecl) == VAR_DECL)
3018 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3019 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3020 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
3021 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
3024 /* Do this after calling `merge_types' so that default
3025 parameters don't confuse us. */
3026 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3027 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3028 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3030 TREE_TYPE (newdecl) = build_exception_variant (newtype,
3031 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3032 TREE_TYPE (olddecl) = build_exception_variant (newtype,
3033 TYPE_RAISES_EXCEPTIONS (oldtype));
3035 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3036 && DECL_SOURCE_LINE (olddecl) != 0
3037 && flag_exceptions
3038 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3039 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3041 error ("declaration of `%F' throws different exceptions",
3042 newdecl);
3043 cp_error_at ("than previous declaration `%F'", olddecl);
3046 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3048 /* Lay the type out, unless already done. */
3049 if (! same_type_p (newtype, oldtype)
3050 && TREE_TYPE (newdecl) != error_mark_node
3051 && !(processing_template_decl && uses_template_parms (newdecl)))
3052 layout_type (TREE_TYPE (newdecl));
3054 if ((TREE_CODE (newdecl) == VAR_DECL
3055 || TREE_CODE (newdecl) == PARM_DECL
3056 || TREE_CODE (newdecl) == RESULT_DECL
3057 || TREE_CODE (newdecl) == FIELD_DECL
3058 || TREE_CODE (newdecl) == TYPE_DECL)
3059 && !(processing_template_decl && uses_template_parms (newdecl)))
3060 layout_decl (newdecl, 0);
3062 /* Merge the type qualifiers. */
3063 if (TREE_READONLY (newdecl))
3064 TREE_READONLY (olddecl) = 1;
3065 if (TREE_THIS_VOLATILE (newdecl))
3066 TREE_THIS_VOLATILE (olddecl) = 1;
3068 /* Merge the initialization information. */
3069 if (DECL_INITIAL (newdecl) == NULL_TREE
3070 && DECL_INITIAL (olddecl) != NULL_TREE)
3072 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3073 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3074 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3075 && DECL_LANG_SPECIFIC (newdecl)
3076 && DECL_LANG_SPECIFIC (olddecl))
3078 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3079 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
3083 /* Merge the section attribute.
3084 We want to issue an error if the sections conflict but that must be
3085 done later in decl_attributes since we are called before attributes
3086 are assigned. */
3087 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3088 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3090 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3092 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3093 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3094 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
3095 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
3096 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
3097 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
3098 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
3099 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
3100 /* Keep the old RTL. */
3101 COPY_DECL_RTL (olddecl, newdecl);
3103 else if (TREE_CODE (newdecl) == VAR_DECL
3104 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3106 /* Keep the old RTL. We cannot keep the old RTL if the old
3107 declaration was for an incomplete object and the new
3108 declaration is not since many attributes of the RTL will
3109 change. */
3110 COPY_DECL_RTL (olddecl, newdecl);
3113 /* If cannot merge, then use the new type and qualifiers,
3114 and don't preserve the old rtl. */
3115 else
3117 /* Clean out any memory we had of the old declaration. */
3118 tree oldstatic = value_member (olddecl, static_aggregates);
3119 if (oldstatic)
3120 TREE_VALUE (oldstatic) = error_mark_node;
3122 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3123 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3124 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3125 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3128 /* Merge the storage class information. */
3129 merge_weak (newdecl, olddecl);
3131 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3132 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3133 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3134 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3135 if (! DECL_EXTERNAL (olddecl))
3136 DECL_EXTERNAL (newdecl) = 0;
3138 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3140 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3141 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3142 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3143 DECL_TEMPLATE_INSTANTIATED (newdecl)
3144 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3145 /* Don't really know how much of the language-specific
3146 values we should copy from old to new. */
3147 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3148 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
3149 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3150 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3151 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3152 DECL_INITIALIZED_IN_CLASS_P (newdecl)
3153 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3154 olddecl_friend = DECL_FRIEND_P (olddecl);
3156 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3157 if (TREE_CODE (newdecl) == FUNCTION_DECL
3158 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3160 DECL_BEFRIENDING_CLASSES (newdecl)
3161 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3162 DECL_BEFRIENDING_CLASSES (olddecl));
3163 /* DECL_THUNKS is only valid for virtual functions,
3164 otherwise it is a DECL_FRIEND_CONTEXT. */
3165 if (DECL_VIRTUAL_P (newdecl))
3166 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3170 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3172 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3173 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3175 /* If newdecl is not a specialization, then it is not a
3176 template-related function at all. And that means that we
3177 should have exited above, returning 0. */
3178 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3181 if (TREE_USED (olddecl))
3182 /* From [temp.expl.spec]:
3184 If a template, a member template or the member of a class
3185 template is explicitly specialized then that
3186 specialization shall be declared before the first use of
3187 that specialization that would cause an implicit
3188 instantiation to take place, in every translation unit in
3189 which such a use occurs. */
3190 error ("explicit specialization of %D after first use",
3191 olddecl);
3193 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3195 /* [temp.expl.spec/14] We don't inline explicit specialization
3196 just because the primary template says so. */
3198 else
3200 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3201 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3203 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3205 /* If either decl says `inline', this fn is inline, unless
3206 its definition was passed already. */
3207 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3208 DECL_INLINE (olddecl) = 1;
3209 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3211 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3212 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3215 /* Preserve abstractness on cloned [cd]tors. */
3216 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3218 if (! types_match)
3220 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3221 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3222 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3224 if (! types_match || new_defines_function)
3226 /* These need to be copied so that the names are available.
3227 Note that if the types do match, we'll preserve inline
3228 info and other bits, but if not, we won't. */
3229 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3230 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3232 if (new_defines_function)
3233 /* If defining a function declared with other language
3234 linkage, use the previously declared language linkage. */
3235 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3236 else if (types_match)
3238 /* If redeclaring a builtin function, and not a definition,
3239 it stays built in. */
3240 if (DECL_BUILT_IN (olddecl))
3242 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3243 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3244 /* If we're keeping the built-in definition, keep the rtl,
3245 regardless of declaration matches. */
3246 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3248 else
3249 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
3251 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3252 /* Don't clear out the arguments if we're redefining a function. */
3253 if (DECL_ARGUMENTS (olddecl))
3254 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3257 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3258 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3260 /* Now preserve various other info from the definition. */
3261 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3262 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3263 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3264 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3266 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3268 int function_size;
3270 function_size = sizeof (struct tree_decl);
3272 memcpy ((char *) olddecl + sizeof (struct tree_common),
3273 (char *) newdecl + sizeof (struct tree_common),
3274 function_size - sizeof (struct tree_common));
3276 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3277 /* If newdecl is a template instantiation, it is possible that
3278 the following sequence of events has occurred:
3280 o A friend function was declared in a class template. The
3281 class template was instantiated.
3283 o The instantiation of the friend declaration was
3284 recorded on the instantiation list, and is newdecl.
3286 o Later, however, instantiate_class_template called pushdecl
3287 on the newdecl to perform name injection. But, pushdecl in
3288 turn called duplicate_decls when it discovered that another
3289 declaration of a global function with the same name already
3290 existed.
3292 o Here, in duplicate_decls, we decided to clobber newdecl.
3294 If we're going to do that, we'd better make sure that
3295 olddecl, and not newdecl, is on the list of
3296 instantiations so that if we try to do the instantiation
3297 again we won't get the clobbered declaration. */
3298 reregister_specialization (newdecl,
3299 DECL_TI_TEMPLATE (newdecl),
3300 olddecl);
3302 else
3304 memcpy ((char *) olddecl + sizeof (struct tree_common),
3305 (char *) newdecl + sizeof (struct tree_common),
3306 sizeof (struct tree_decl) - sizeof (struct tree_common)
3307 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3310 DECL_UID (olddecl) = olddecl_uid;
3311 if (olddecl_friend)
3312 DECL_FRIEND_P (olddecl) = 1;
3314 /* NEWDECL contains the merged attribute lists.
3315 Update OLDDECL to be the same. */
3316 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3318 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3319 so that encode_section_info has a chance to look at the new decl
3320 flags and attributes. */
3321 if (DECL_RTL_SET_P (olddecl)
3322 && (TREE_CODE (olddecl) == FUNCTION_DECL
3323 || (TREE_CODE (olddecl) == VAR_DECL
3324 && TREE_STATIC (olddecl))))
3325 make_decl_rtl (olddecl, NULL);
3327 return 1;
3330 /* Record a decl-node X as belonging to the current lexical scope.
3331 Check for errors (such as an incompatible declaration for the same
3332 name already seen in the same scope).
3334 Returns either X or an old decl for the same name.
3335 If an old decl is returned, it may have been smashed
3336 to agree with what X says. */
3338 tree
3339 pushdecl (tree x)
3341 register tree t;
3342 register tree name;
3343 int need_new_binding;
3345 timevar_push (TV_NAME_LOOKUP);
3347 need_new_binding = 1;
3349 if (DECL_TEMPLATE_PARM_P (x))
3350 /* Template parameters have no context; they are not X::T even
3351 when declared within a class or namespace. */
3353 else
3355 if (current_function_decl && x != current_function_decl
3356 /* A local declaration for a function doesn't constitute
3357 nesting. */
3358 && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3359 /* A local declaration for an `extern' variable is in the
3360 scope of the current namespace, not the current
3361 function. */
3362 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3363 && !DECL_CONTEXT (x))
3364 DECL_CONTEXT (x) = current_function_decl;
3366 /* If this is the declaration for a namespace-scope function,
3367 but the declaration itself is in a local scope, mark the
3368 declaration. */
3369 if (TREE_CODE (x) == FUNCTION_DECL
3370 && DECL_NAMESPACE_SCOPE_P (x)
3371 && current_function_decl
3372 && x != current_function_decl)
3373 DECL_LOCAL_FUNCTION_P (x) = 1;
3376 name = DECL_NAME (x);
3377 if (name)
3379 int different_binding_level = 0;
3381 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3382 name = TREE_OPERAND (name, 0);
3384 /* In case this decl was explicitly namespace-qualified, look it
3385 up in its namespace context. */
3386 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3387 && namespace_bindings_p ())
3388 t = namespace_binding (name, DECL_CONTEXT (x));
3389 else
3390 t = lookup_name_current_level (name);
3392 /* [basic.link] If there is a visible declaration of an entity
3393 with linkage having the same name and type, ignoring entities
3394 declared outside the innermost enclosing namespace scope, the
3395 block scope declaration declares that same entity and
3396 receives the linkage of the previous declaration. */
3397 if (! t && current_function_decl && x != current_function_decl
3398 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3399 && DECL_EXTERNAL (x))
3401 /* Look in block scope. */
3402 t = IDENTIFIER_VALUE (name);
3403 /* Or in the innermost namespace. */
3404 if (! t)
3405 t = namespace_binding (name, DECL_CONTEXT (x));
3406 /* Does it have linkage? Note that if this isn't a DECL, it's an
3407 OVERLOAD, which is OK. */
3408 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3409 t = NULL_TREE;
3410 if (t)
3411 different_binding_level = 1;
3414 /* If we are declaring a function, and the result of name-lookup
3415 was an OVERLOAD, look for an overloaded instance that is
3416 actually the same as the function we are declaring. (If
3417 there is one, we have to merge our declaration with the
3418 previous declaration.) */
3419 if (t && TREE_CODE (t) == OVERLOAD)
3421 tree match;
3423 if (TREE_CODE (x) == FUNCTION_DECL)
3424 for (match = t; match; match = OVL_NEXT (match))
3426 if (decls_match (OVL_CURRENT (match), x))
3427 break;
3429 else
3430 /* Just choose one. */
3431 match = t;
3433 if (match)
3434 t = OVL_CURRENT (match);
3435 else
3436 t = NULL_TREE;
3439 if (t == error_mark_node)
3441 /* error_mark_node is 0 for a while during initialization! */
3442 t = NULL_TREE;
3443 cp_error_at ("`%#D' used prior to declaration", x);
3445 else if (t != NULL_TREE)
3447 if (different_binding_level)
3449 if (decls_match (x, t))
3450 /* The standard only says that the local extern
3451 inherits linkage from the previous decl; in
3452 particular, default args are not shared. It would
3453 be nice to propagate inlining info, though. FIXME. */
3454 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3456 else if (TREE_CODE (t) == PARM_DECL)
3458 if (DECL_CONTEXT (t) == NULL_TREE)
3459 /* This is probably caused by too many errors, but calling
3460 abort will say that if errors have occurred. */
3461 abort ();
3463 /* Check for duplicate params. */
3464 if (duplicate_decls (x, t))
3465 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3467 else if ((DECL_EXTERN_C_FUNCTION_P (x)
3468 || DECL_FUNCTION_TEMPLATE_P (x))
3469 && is_overloaded_fn (t))
3470 /* Don't do anything just yet. */;
3471 else if (t == wchar_decl_node)
3473 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3474 pedwarn ("redeclaration of `wchar_t' as `%T'",
3475 TREE_TYPE (x));
3477 /* Throw away the redeclaration. */
3478 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3480 else if (TREE_CODE (t) != TREE_CODE (x))
3482 if (duplicate_decls (x, t))
3483 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3485 else if (duplicate_decls (x, t))
3487 if (TREE_CODE (t) == TYPE_DECL)
3488 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3489 else if (TREE_CODE (t) == FUNCTION_DECL)
3490 check_default_args (t);
3492 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3494 else if (DECL_MAIN_P (x))
3496 /* A redeclaration of main, but not a duplicate of the
3497 previous one.
3499 [basic.start.main]
3501 This function shall not be overloaded. */
3502 cp_error_at ("invalid redeclaration of `%D'", t);
3503 error ("as `%D'", x);
3504 /* We don't try to push this declaration since that
3505 causes a crash. */
3506 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3510 check_template_shadow (x);
3512 /* If this is a function conjured up by the backend, massage it
3513 so it looks friendly. */
3514 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3516 retrofit_lang_decl (x);
3517 SET_DECL_LANGUAGE (x, lang_c);
3520 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3522 t = push_overloaded_decl (x, PUSH_LOCAL);
3523 if (t != x)
3524 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3525 if (!namespace_bindings_p ())
3526 /* We do not need to create a binding for this name;
3527 push_overloaded_decl will have already done so if
3528 necessary. */
3529 need_new_binding = 0;
3531 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3533 t = push_overloaded_decl (x, PUSH_GLOBAL);
3534 if (t == x)
3535 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3536 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3539 /* If declaring a type as a typedef, copy the type (unless we're
3540 at line 0), and install this TYPE_DECL as the new type's typedef
3541 name. See the extensive comment in ../c-decl.c (pushdecl). */
3542 if (TREE_CODE (x) == TYPE_DECL)
3544 tree type = TREE_TYPE (x);
3545 if (DECL_SOURCE_LINE (x) == 0)
3547 if (TYPE_NAME (type) == 0)
3548 TYPE_NAME (type) = x;
3550 else if (type != error_mark_node && TYPE_NAME (type) != x
3551 /* We don't want to copy the type when all we're
3552 doing is making a TYPE_DECL for the purposes of
3553 inlining. */
3554 && (!TYPE_NAME (type)
3555 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3557 DECL_ORIGINAL_TYPE (x) = type;
3558 type = build_type_copy (type);
3559 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3560 TYPE_NAME (type) = x;
3561 TREE_TYPE (x) = type;
3564 if (type != error_mark_node
3565 && TYPE_NAME (type)
3566 && TYPE_IDENTIFIER (type))
3567 set_identifier_type_value (DECL_NAME (x), x);
3570 /* Multiple external decls of the same identifier ought to match.
3572 We get warnings about inline functions where they are defined.
3573 We get warnings about other functions from push_overloaded_decl.
3575 Avoid duplicate warnings where they are used. */
3576 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3578 tree decl;
3580 decl = IDENTIFIER_NAMESPACE_VALUE (name);
3581 if (decl && TREE_CODE (decl) == OVERLOAD)
3582 decl = OVL_FUNCTION (decl);
3584 if (decl && decl != error_mark_node
3585 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3586 /* If different sort of thing, we already gave an error. */
3587 && TREE_CODE (decl) == TREE_CODE (x)
3588 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3590 pedwarn ("type mismatch with previous external decl of `%#D'", x);
3591 cp_pedwarn_at ("previous external decl of `%#D'", decl);
3595 /* This name is new in its binding level.
3596 Install the new declaration and return it. */
3597 if (namespace_bindings_p ())
3599 /* Install a global value. */
3601 /* If the first global decl has external linkage,
3602 warn if we later see static one. */
3603 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3604 TREE_PUBLIC (name) = 1;
3606 /* Bind the name for the entity. */
3607 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3608 && t != NULL_TREE)
3609 && (TREE_CODE (x) == TYPE_DECL
3610 || TREE_CODE (x) == VAR_DECL
3611 || TREE_CODE (x) == ALIAS_DECL
3612 || TREE_CODE (x) == NAMESPACE_DECL
3613 || TREE_CODE (x) == CONST_DECL
3614 || TREE_CODE (x) == TEMPLATE_DECL))
3615 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3617 /* Don't forget if the function was used via an implicit decl. */
3618 if (IDENTIFIER_IMPLICIT_DECL (name)
3619 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3620 TREE_USED (x) = 1;
3622 /* Don't forget if its address was taken in that way. */
3623 if (IDENTIFIER_IMPLICIT_DECL (name)
3624 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3625 TREE_ADDRESSABLE (x) = 1;
3627 /* Warn about mismatches against previous implicit decl. */
3628 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3629 /* If this real decl matches the implicit, don't complain. */
3630 && ! (TREE_CODE (x) == FUNCTION_DECL
3631 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3632 warning
3633 ("`%D' was previously implicitly declared to return `int'", x);
3635 /* If new decl is `static' and an `extern' was seen previously,
3636 warn about it. */
3637 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3638 warn_extern_redeclared_static (x, t);
3640 else
3642 /* Here to install a non-global value. */
3643 tree oldlocal = IDENTIFIER_VALUE (name);
3644 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3646 if (need_new_binding)
3648 push_local_binding (name, x, 0);
3649 /* Because push_local_binding will hook X on to the
3650 current_binding_level's name list, we don't want to
3651 do that again below. */
3652 need_new_binding = 0;
3655 /* If this is a TYPE_DECL, push it into the type value slot. */
3656 if (TREE_CODE (x) == TYPE_DECL)
3657 set_identifier_type_value (name, x);
3659 /* Clear out any TYPE_DECL shadowed by a namespace so that
3660 we won't think this is a type. The C struct hack doesn't
3661 go through namespaces. */
3662 if (TREE_CODE (x) == NAMESPACE_DECL)
3663 set_identifier_type_value (name, NULL_TREE);
3665 if (oldlocal)
3667 tree d = oldlocal;
3669 while (oldlocal
3670 && TREE_CODE (oldlocal) == VAR_DECL
3671 && DECL_DEAD_FOR_LOCAL (oldlocal))
3672 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
3674 if (oldlocal == NULL_TREE)
3675 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
3678 /* If this is an extern function declaration, see if we
3679 have a global definition or declaration for the function. */
3680 if (oldlocal == NULL_TREE
3681 && DECL_EXTERNAL (x)
3682 && oldglobal != NULL_TREE
3683 && TREE_CODE (x) == FUNCTION_DECL
3684 && TREE_CODE (oldglobal) == FUNCTION_DECL)
3686 /* We have one. Their types must agree. */
3687 if (decls_match (x, oldglobal))
3688 /* OK */;
3689 else
3691 warning ("extern declaration of `%#D' doesn't match", x);
3692 cp_warning_at ("global declaration `%#D'", oldglobal);
3695 /* If we have a local external declaration,
3696 and no file-scope declaration has yet been seen,
3697 then if we later have a file-scope decl it must not be static. */
3698 if (oldlocal == NULL_TREE
3699 && oldglobal == NULL_TREE
3700 && DECL_EXTERNAL (x)
3701 && TREE_PUBLIC (x))
3702 TREE_PUBLIC (name) = 1;
3704 /* Warn if shadowing an argument at the top level of the body. */
3705 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3706 /* Inline decls shadow nothing. */
3707 && !DECL_FROM_INLINE (x)
3708 && TREE_CODE (oldlocal) == PARM_DECL
3709 /* Don't check the `this' parameter. */
3710 && !DECL_ARTIFICIAL (oldlocal))
3712 bool err = false;
3714 /* Don't complain if it's from an enclosing function. */
3715 if (DECL_CONTEXT (oldlocal) == current_function_decl
3716 && TREE_CODE (x) != PARM_DECL)
3718 /* Go to where the parms should be and see if we find
3719 them there. */
3720 struct cp_binding_level *b = current_binding_level->level_chain;
3722 /* Skip the ctor/dtor cleanup level. */
3723 b = b->level_chain;
3725 /* ARM $8.3 */
3726 if (b->kind == sk_function_parms)
3728 error ("declaration of `%#D' shadows a parameter",
3729 name);
3730 err = true;
3734 if (warn_shadow && !err)
3735 shadow_warning (SW_PARAM,
3736 IDENTIFIER_POINTER (name), oldlocal);
3739 /* Maybe warn if shadowing something else. */
3740 else if (warn_shadow && !DECL_EXTERNAL (x)
3741 /* No shadow warnings for internally generated vars. */
3742 && ! DECL_ARTIFICIAL (x)
3743 /* No shadow warnings for vars made for inlining. */
3744 && ! DECL_FROM_INLINE (x))
3746 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
3747 && current_class_ptr
3748 && !TREE_STATIC (name))
3749 warning ("declaration of `%s' shadows a member of `this'",
3750 IDENTIFIER_POINTER (name));
3751 else if (oldlocal != NULL_TREE
3752 && TREE_CODE (oldlocal) == VAR_DECL)
3753 shadow_warning (SW_LOCAL,
3754 IDENTIFIER_POINTER (name), oldlocal);
3755 else if (oldglobal != NULL_TREE
3756 && TREE_CODE (oldglobal) == VAR_DECL)
3757 /* XXX shadow warnings in outer-more namespaces */
3758 shadow_warning (SW_GLOBAL,
3759 IDENTIFIER_POINTER (name), oldglobal);
3763 if (TREE_CODE (x) == FUNCTION_DECL)
3764 check_default_args (x);
3766 if (TREE_CODE (x) == VAR_DECL)
3767 maybe_register_incomplete_var (x);
3770 if (need_new_binding)
3771 add_decl_to_level (x,
3772 DECL_NAMESPACE_SCOPE_P (x)
3773 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
3774 : current_binding_level);
3776 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3779 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3780 caller to set DECL_CONTEXT properly. */
3782 static tree
3783 pushdecl_with_scope (tree x, struct cp_binding_level* level)
3785 register struct cp_binding_level *b;
3786 tree function_decl = current_function_decl;
3788 timevar_push (TV_NAME_LOOKUP);
3789 current_function_decl = NULL_TREE;
3790 if (level->kind == sk_class)
3792 b = class_binding_level;
3793 class_binding_level = level;
3794 pushdecl_class_level (x);
3795 class_binding_level = b;
3797 else
3799 b = current_binding_level;
3800 current_binding_level = level;
3801 x = pushdecl (x);
3802 current_binding_level = b;
3804 current_function_decl = function_decl;
3805 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3808 /* Like pushdecl, only it places X in the current namespace,
3809 if appropriate. */
3811 tree
3812 pushdecl_namespace_level (tree x)
3814 register struct cp_binding_level *b = current_binding_level;
3815 register tree t;
3817 timevar_push (TV_NAME_LOOKUP);
3818 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
3820 /* Now, the type_shadowed stack may screw us. Munge it so it does
3821 what we want. */
3822 if (TREE_CODE (x) == TYPE_DECL)
3824 tree name = DECL_NAME (x);
3825 tree newval;
3826 tree *ptr = (tree *)0;
3827 for (; !global_scope_p (b); b = b->level_chain)
3829 tree shadowed = b->type_shadowed;
3830 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3831 if (TREE_PURPOSE (shadowed) == name)
3833 ptr = &TREE_VALUE (shadowed);
3834 /* Can't break out of the loop here because sometimes
3835 a binding level will have duplicate bindings for
3836 PT names. It's gross, but I haven't time to fix it. */
3839 newval = TREE_TYPE (x);
3840 if (ptr == (tree *)0)
3842 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3843 up here if this is changed to an assertion. --KR */
3844 SET_IDENTIFIER_TYPE_VALUE (name, x);
3846 else
3848 *ptr = newval;
3851 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3854 /* Like pushdecl, only it places X in the global scope if appropriate.
3855 Calls cp_finish_decl to register the variable, initializing it with
3856 *INIT, if INIT is non-NULL. */
3858 static tree
3859 pushdecl_top_level_1 (tree x, tree *init)
3861 timevar_push (TV_NAME_LOOKUP);
3862 push_to_top_level ();
3863 x = pushdecl_namespace_level (x);
3864 if (init)
3865 cp_finish_decl (x, *init, NULL_TREE, 0);
3866 pop_from_top_level ();
3867 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3870 /* Like pushdecl, only it places X in the global scope if appropriate. */
3872 tree
3873 pushdecl_top_level (tree x)
3875 return pushdecl_top_level_1 (x, NULL);
3878 /* Like pushdecl, only it places X in the global scope if
3879 appropriate. Calls cp_finish_decl to register the variable,
3880 initializing it with INIT. */
3882 tree
3883 pushdecl_top_level_and_finish (tree x, tree init)
3885 return pushdecl_top_level_1 (x, &init);
3888 /* Make the declaration of X appear in CLASS scope. */
3890 bool
3891 pushdecl_class_level (tree x)
3893 tree name;
3894 bool is_valid = true;
3896 timevar_push (TV_NAME_LOOKUP);
3897 /* Get the name of X. */
3898 if (TREE_CODE (x) == OVERLOAD)
3899 name = DECL_NAME (get_first_fn (x));
3900 else
3901 name = DECL_NAME (x);
3903 if (name)
3905 is_valid = push_class_level_binding (name, x);
3906 if (TREE_CODE (x) == TYPE_DECL)
3907 set_identifier_type_value (name, x);
3909 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3911 /* If X is an anonymous aggregate, all of its members are
3912 treated as if they were members of the class containing the
3913 aggregate, for naming purposes. */
3914 tree f;
3916 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
3918 location_t save_location = input_location;
3919 input_location = DECL_SOURCE_LOCATION (f);
3920 if (!pushdecl_class_level (f))
3921 is_valid = false;
3922 input_location = save_location;
3925 timevar_pop (TV_NAME_LOOKUP);
3927 return is_valid;
3930 /* Enter DECL into the symbol table, if that's appropriate. Returns
3931 DECL, or a modified version thereof. */
3933 tree
3934 maybe_push_decl (tree decl)
3936 tree type = TREE_TYPE (decl);
3938 /* Add this decl to the current binding level, but not if it comes
3939 from another scope, e.g. a static member variable. TEM may equal
3940 DECL or it may be a previous decl of the same name. */
3941 if (decl == error_mark_node
3942 || (TREE_CODE (decl) != PARM_DECL
3943 && DECL_CONTEXT (decl) != NULL_TREE
3944 /* Definitions of namespace members outside their namespace are
3945 possible. */
3946 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
3947 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3948 || TREE_CODE (type) == UNKNOWN_TYPE
3949 /* The declaration of a template specialization does not affect
3950 the functions available for overload resolution, so we do not
3951 call pushdecl. */
3952 || (TREE_CODE (decl) == FUNCTION_DECL
3953 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3954 return decl;
3955 else
3956 return pushdecl (decl);
3959 /* Make the declaration(s) of X appear in CLASS scope under the name
3960 NAME. Returns true if the binding is valid. */
3962 bool
3963 push_class_level_binding (tree name, tree x)
3965 cxx_binding *binding;
3967 timevar_push (TV_NAME_LOOKUP);
3968 /* The class_binding_level will be NULL if x is a template
3969 parameter name in a member template. */
3970 if (!class_binding_level)
3971 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
3973 /* Make sure that this new member does not have the same name
3974 as a template parameter. */
3975 if (TYPE_BEING_DEFINED (current_class_type))
3976 check_template_shadow (x);
3978 /* If this declaration shadows a declaration from an enclosing
3979 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
3980 we leave this class. Record the shadowed declaration here. */
3981 binding = IDENTIFIER_BINDING (name);
3982 if (binding && binding->value)
3984 tree bval = binding->value;
3985 tree old_decl = NULL_TREE;
3987 if (INHERITED_VALUE_BINDING_P (binding))
3989 /* If the old binding was from a base class, and was for a
3990 tag name, slide it over to make room for the new binding.
3991 The old binding is still visible if explicitly qualified
3992 with a class-key. */
3993 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
3994 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
3996 old_decl = binding->type;
3997 binding->type = bval;
3998 binding->value = NULL_TREE;
3999 INHERITED_VALUE_BINDING_P (binding) = 0;
4001 else
4002 old_decl = bval;
4004 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
4005 old_decl = bval;
4006 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
4007 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4008 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
4009 old_decl = bval;
4010 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
4011 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4013 if (old_decl)
4015 tree shadow;
4017 /* Find the previous binding of name on the class-shadowed
4018 list, and update it. */
4019 for (shadow = class_binding_level->class_shadowed;
4020 shadow;
4021 shadow = TREE_CHAIN (shadow))
4022 if (TREE_PURPOSE (shadow) == name
4023 && TREE_TYPE (shadow) == old_decl)
4025 binding->value = x;
4026 INHERITED_VALUE_BINDING_P (binding) = 0;
4027 TREE_TYPE (shadow) = x;
4028 IDENTIFIER_CLASS_VALUE (name) = x;
4029 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4034 /* If we didn't replace an existing binding, put the binding on the
4035 stack of bindings for the identifier, and update the shadowed list. */
4036 if (push_class_binding (name, x))
4038 class_binding_level->class_shadowed
4039 = tree_cons (name, NULL,
4040 class_binding_level->class_shadowed);
4041 /* Record the value we are binding NAME to so that we can know
4042 what to pop later. */
4043 TREE_TYPE (class_binding_level->class_shadowed) = x;
4044 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4047 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
4050 /* Insert another USING_DECL into the current binding level, returning
4051 this declaration. If this is a redeclaration, do nothing, and
4052 return NULL_TREE if this not in namespace scope (in namespace
4053 scope, a using decl might extend any previous bindings). */
4055 tree
4056 push_using_decl (tree scope, tree name)
4058 tree decl;
4060 timevar_push (TV_NAME_LOOKUP);
4061 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4062 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4063 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4064 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4065 break;
4066 if (decl)
4067 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4068 namespace_bindings_p () ? decl : NULL_TREE);
4069 decl = build_lang_decl (USING_DECL, name, void_type_node);
4070 DECL_INITIAL (decl) = scope;
4071 TREE_CHAIN (decl) = current_binding_level->usings;
4072 current_binding_level->usings = decl;
4073 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4076 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4077 changed (i.e. there was already a directive), or the fresh
4078 TREE_LIST otherwise. */
4080 tree
4081 push_using_directive (tree used)
4083 tree ud = current_binding_level->using_directives;
4084 tree iter, ancestor;
4086 timevar_push (TV_NAME_LOOKUP);
4087 /* Check if we already have this. */
4088 if (purpose_member (used, ud) != NULL_TREE)
4089 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4091 ancestor = namespace_ancestor (current_decl_namespace (), used);
4092 ud = current_binding_level->using_directives;
4093 ud = tree_cons (used, ancestor, ud);
4094 current_binding_level->using_directives = ud;
4096 /* Recursively add all namespaces used. */
4097 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4098 push_using_directive (TREE_PURPOSE (iter));
4100 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4103 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4104 other definitions already in place. We get around this by making
4105 the value of the identifier point to a list of all the things that
4106 want to be referenced by that name. It is then up to the users of
4107 that name to decide what to do with that list.
4109 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4110 DECL_TEMPLATE_RESULT. It is dealt with the same way.
4112 FLAGS is a bitwise-or of the following values:
4113 PUSH_LOCAL: Bind DECL in the current scope, rather than at
4114 namespace scope.
4115 PUSH_USING: DECL is being pushed as the result of a using
4116 declaration.
4118 The value returned may be a previous declaration if we guessed wrong
4119 about what language DECL should belong to (C or C++). Otherwise,
4120 it's always DECL (and never something that's not a _DECL). */
4122 tree
4123 push_overloaded_decl (tree decl, int flags)
4125 tree name = DECL_NAME (decl);
4126 tree old;
4127 tree new_binding;
4128 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4130 timevar_push (TV_NAME_LOOKUP);
4131 if (doing_global)
4132 old = namespace_binding (name, DECL_CONTEXT (decl));
4133 else
4134 old = lookup_name_current_level (name);
4136 if (old)
4138 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4140 tree t = TREE_TYPE (old);
4141 if (IS_AGGR_TYPE (t) && warn_shadow
4142 && (! DECL_IN_SYSTEM_HEADER (decl)
4143 || ! DECL_IN_SYSTEM_HEADER (old)))
4144 warning ("`%#D' hides constructor for `%#T'", decl, t);
4145 old = NULL_TREE;
4147 else if (is_overloaded_fn (old))
4149 tree tmp;
4151 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4153 tree fn = OVL_CURRENT (tmp);
4155 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4156 && !(flags & PUSH_USING)
4157 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4158 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4159 error ("`%#D' conflicts with previous using declaration `%#D'",
4160 decl, fn);
4162 if (duplicate_decls (decl, fn))
4163 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4166 else if (old == error_mark_node)
4167 /* Ignore the undefined symbol marker. */
4168 old = NULL_TREE;
4169 else
4171 cp_error_at ("previous non-function declaration `%#D'", old);
4172 error ("conflicts with function declaration `%#D'", decl);
4173 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4177 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4179 if (old && TREE_CODE (old) != OVERLOAD)
4180 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4181 else
4182 new_binding = ovl_cons (decl, old);
4183 if (flags & PUSH_USING)
4184 OVL_USED (new_binding) = 1;
4186 else
4187 /* NAME is not ambiguous. */
4188 new_binding = decl;
4190 if (doing_global)
4191 set_namespace_binding (name, current_namespace, new_binding);
4192 else
4194 /* We only create an OVERLOAD if there was a previous binding at
4195 this level, or if decl is a template. In the former case, we
4196 need to remove the old binding and replace it with the new
4197 binding. We must also run through the NAMES on the binding
4198 level where the name was bound to update the chain. */
4200 if (TREE_CODE (new_binding) == OVERLOAD && old)
4202 tree *d;
4204 for (d = &IDENTIFIER_BINDING (name)->scope->names;
4206 d = &TREE_CHAIN (*d))
4207 if (*d == old
4208 || (TREE_CODE (*d) == TREE_LIST
4209 && TREE_VALUE (*d) == old))
4211 if (TREE_CODE (*d) == TREE_LIST)
4212 /* Just replace the old binding with the new. */
4213 TREE_VALUE (*d) = new_binding;
4214 else
4215 /* Build a TREE_LIST to wrap the OVERLOAD. */
4216 *d = tree_cons (NULL_TREE, new_binding,
4217 TREE_CHAIN (*d));
4219 /* And update the cxx_binding node. */
4220 IDENTIFIER_BINDING (name)->value = new_binding;
4221 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4224 /* We should always find a previous binding in this case. */
4225 abort ();
4228 /* Install the new binding. */
4229 push_local_binding (name, new_binding, flags);
4232 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4235 /* Generate an implicit declaration for identifier FUNCTIONID
4236 as a function of type int (). Print a warning if appropriate. */
4238 tree
4239 implicitly_declare (tree functionid)
4241 register tree decl;
4243 /* We used to reuse an old implicit decl here,
4244 but this loses with inline functions because it can clobber
4245 the saved decl chains. */
4246 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4248 DECL_EXTERNAL (decl) = 1;
4249 TREE_PUBLIC (decl) = 1;
4251 /* ISO standard says implicit declarations are in the innermost block.
4252 So we record the decl in the standard fashion. */
4253 pushdecl (decl);
4254 rest_of_decl_compilation (decl, NULL, 0, 0);
4256 if (warn_implicit
4257 /* Only one warning per identifier. */
4258 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4260 pedwarn ("implicit declaration of function `%#D'", decl);
4263 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4265 return decl;
4268 /* Return zero if the declaration NEWDECL is valid
4269 when the declaration OLDDECL (assumed to be for the same name)
4270 has already been seen.
4271 Otherwise return an error message format string with a %s
4272 where the identifier should go. */
4274 static const char *
4275 redeclaration_error_message (tree newdecl, tree olddecl)
4277 if (TREE_CODE (newdecl) == TYPE_DECL)
4279 /* Because C++ can put things into name space for free,
4280 constructs like "typedef struct foo { ... } foo"
4281 would look like an erroneous redeclaration. */
4282 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4283 return 0;
4284 else
4285 return "redefinition of `%#D'";
4287 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4289 /* If this is a pure function, its olddecl will actually be
4290 the original initialization to `0' (which we force to call
4291 abort()). Don't complain about redefinition in this case. */
4292 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4293 return 0;
4295 /* If both functions come from different namespaces, this is not
4296 a redeclaration - this is a conflict with a used function. */
4297 if (DECL_NAMESPACE_SCOPE_P (olddecl)
4298 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4299 return "`%D' conflicts with used function";
4301 /* We'll complain about linkage mismatches in
4302 warn_extern_redeclared_static. */
4304 /* Defining the same name twice is no good. */
4305 if (DECL_INITIAL (olddecl) != NULL_TREE
4306 && DECL_INITIAL (newdecl) != NULL_TREE)
4308 if (DECL_NAME (olddecl) == NULL_TREE)
4309 return "`%#D' not declared in class";
4310 else
4311 return "redefinition of `%#D'";
4313 return 0;
4315 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4317 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4318 && (DECL_TEMPLATE_RESULT (newdecl)
4319 != DECL_TEMPLATE_RESULT (olddecl))
4320 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4321 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4322 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4323 && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4324 && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4325 return "redefinition of `%#D'";
4326 return 0;
4328 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4330 /* Objects declared at top level: */
4331 /* If at least one is a reference, it's ok. */
4332 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4333 return 0;
4334 /* Reject two definitions. */
4335 return "redefinition of `%#D'";
4337 else
4339 /* Objects declared with block scope: */
4340 /* Reject two definitions, and reject a definition
4341 together with an external reference. */
4342 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4343 return "redeclaration of `%#D'";
4344 return 0;
4348 /* Create a new label, named ID. */
4350 static tree
4351 make_label_decl (tree id, int local_p)
4353 tree decl;
4355 decl = build_decl (LABEL_DECL, id, void_type_node);
4357 DECL_CONTEXT (decl) = current_function_decl;
4358 DECL_MODE (decl) = VOIDmode;
4359 C_DECLARED_LABEL_FLAG (decl) = local_p;
4361 /* Say where one reference is to the label, for the sake of the
4362 error if it is not defined. */
4363 DECL_SOURCE_LOCATION (decl) = input_location;
4365 /* Record the fact that this identifier is bound to this label. */
4366 SET_IDENTIFIER_LABEL_VALUE (id, decl);
4368 return decl;
4371 /* Record this label on the list of used labels so that we can check
4372 at the end of the function to see whether or not the label was
4373 actually defined, and so we can check when the label is defined whether
4374 this use is valid. */
4376 static void
4377 use_label (tree decl)
4379 if (named_label_uses == NULL
4380 || named_label_uses->names_in_scope != current_binding_level->names
4381 || named_label_uses->label_decl != decl)
4383 struct named_label_use_list *new_ent;
4384 new_ent = ggc_alloc (sizeof (struct named_label_use_list));
4385 new_ent->label_decl = decl;
4386 new_ent->names_in_scope = current_binding_level->names;
4387 new_ent->binding_level = current_binding_level;
4388 new_ent->o_goto_locus = input_location;
4389 new_ent->next = named_label_uses;
4390 named_label_uses = new_ent;
4394 /* Look for a label named ID in the current function. If one cannot
4395 be found, create one. (We keep track of used, but undefined,
4396 labels, and complain about them at the end of a function.) */
4398 tree
4399 lookup_label (tree id)
4401 tree decl;
4402 struct named_label_list *ent;
4404 timevar_push (TV_NAME_LOOKUP);
4405 /* You can't use labels at global scope. */
4406 if (current_function_decl == NULL_TREE)
4408 error ("label `%s' referenced outside of any function",
4409 IDENTIFIER_POINTER (id));
4410 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4413 /* See if we've already got this label. */
4414 decl = IDENTIFIER_LABEL_VALUE (id);
4415 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4416 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4418 /* Record this label on the list of labels used in this function.
4419 We do this before calling make_label_decl so that we get the
4420 IDENTIFIER_LABEL_VALUE before the new label is declared. */
4421 ent = ggc_alloc_cleared (sizeof (struct named_label_list));
4422 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4423 ent->next = named_labels;
4424 named_labels = ent;
4426 /* We need a new label. */
4427 decl = make_label_decl (id, /*local_p=*/0);
4429 /* Now fill in the information we didn't have before. */
4430 ent->label_decl = decl;
4432 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4435 /* Declare a local label named ID. */
4437 tree
4438 declare_local_label (tree id)
4440 tree decl;
4442 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4443 this scope we can restore the old value of
4444 IDENTIFIER_TYPE_VALUE. */
4445 current_binding_level->shadowed_labels
4446 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4447 current_binding_level->shadowed_labels);
4448 /* Look for the label. */
4449 decl = make_label_decl (id, /*local_p=*/1);
4450 /* Now fill in the information we didn't have before. */
4451 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4453 return decl;
4456 /* Returns nonzero if it is ill-formed to jump past the declaration of
4457 DECL. Returns 2 if it's also a real problem. */
4459 static int
4460 decl_jump_unsafe (tree decl)
4462 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4463 return 0;
4465 if (DECL_INITIAL (decl) == NULL_TREE
4466 && pod_type_p (TREE_TYPE (decl)))
4467 return 0;
4469 /* This is really only important if we're crossing an initialization.
4470 The POD stuff is just pedantry; why should it matter if the class
4471 contains a field of pointer to member type? */
4472 if (DECL_INITIAL (decl)
4473 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4474 return 2;
4475 return 1;
4478 /* Check that a single previously seen jump to a newly defined label
4479 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4480 the jump context; NAMES are the names in scope in LEVEL at the jump
4481 context; FILE and LINE are the source position of the jump or 0. */
4483 static void
4484 check_previous_goto_1 (tree decl,
4485 struct cp_binding_level* level,
4486 tree names, const location_t *locus)
4488 int identified = 0;
4489 int saw_eh = 0;
4490 struct cp_binding_level *b = current_binding_level;
4491 for (; b; b = b->level_chain)
4493 tree new_decls = b->names;
4494 tree old_decls = (b == level ? names : NULL_TREE);
4495 for (; new_decls != old_decls;
4496 new_decls = TREE_CHAIN (new_decls))
4498 int problem = decl_jump_unsafe (new_decls);
4499 if (! problem)
4500 continue;
4502 if (! identified)
4504 if (decl)
4505 pedwarn ("jump to label `%D'", decl);
4506 else
4507 pedwarn ("jump to case label");
4509 if (locus)
4510 pedwarn ("%H from here", locus);
4511 identified = 1;
4514 if (problem > 1)
4515 cp_error_at (" crosses initialization of `%#D'",
4516 new_decls);
4517 else
4518 cp_pedwarn_at (" enters scope of non-POD `%#D'",
4519 new_decls);
4522 if (b == level)
4523 break;
4524 if ((b->kind == sk_try || b->kind == sk_catch) && ! saw_eh)
4526 if (! identified)
4528 if (decl)
4529 pedwarn ("jump to label `%D'", decl);
4530 else
4531 pedwarn ("jump to case label");
4533 if (locus)
4534 pedwarn ("%H from here", locus);
4535 identified = 1;
4537 if (b->kind == sk_try)
4538 error (" enters try block");
4539 else
4540 error (" enters catch block");
4541 saw_eh = 1;
4546 static void
4547 check_previous_goto (struct named_label_use_list* use)
4549 check_previous_goto_1 (use->label_decl, use->binding_level,
4550 use->names_in_scope, &use->o_goto_locus);
4553 static void
4554 check_switch_goto (struct cp_binding_level* level)
4556 check_previous_goto_1 (NULL_TREE, level, level->names, NULL);
4559 /* Check that any previously seen jumps to a newly defined label DECL
4560 are OK. Called by define_label. */
4562 static void
4563 check_previous_gotos (tree decl)
4565 struct named_label_use_list **usep;
4567 if (! TREE_USED (decl))
4568 return;
4570 for (usep = &named_label_uses; *usep; )
4572 struct named_label_use_list *use = *usep;
4573 if (use->label_decl == decl)
4575 check_previous_goto (use);
4576 *usep = use->next;
4578 else
4579 usep = &(use->next);
4583 /* Check that a new jump to a label DECL is OK. Called by
4584 finish_goto_stmt. */
4586 void
4587 check_goto (tree decl)
4589 int identified = 0;
4590 tree bad;
4591 struct named_label_list *lab;
4593 /* We can't know where a computed goto is jumping. So we assume
4594 that it's OK. */
4595 if (! DECL_P (decl))
4596 return;
4598 /* If the label hasn't been defined yet, defer checking. */
4599 if (! DECL_INITIAL (decl))
4601 use_label (decl);
4602 return;
4605 for (lab = named_labels; lab; lab = lab->next)
4606 if (decl == lab->label_decl)
4607 break;
4609 /* If the label is not on named_labels it's a gcc local label, so
4610 it must be in an outer scope, so jumping to it is always OK. */
4611 if (lab == 0)
4612 return;
4614 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4615 && !identified)
4617 cp_pedwarn_at ("jump to label `%D'", decl);
4618 pedwarn (" from here");
4619 identified = 1;
4622 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4624 tree b = TREE_VALUE (bad);
4625 int u = decl_jump_unsafe (b);
4627 if (u > 1 && DECL_ARTIFICIAL (b))
4628 /* Can't skip init of __exception_info. */
4629 error ("%J enters catch block", b);
4630 else if (u > 1)
4631 cp_error_at (" skips initialization of `%#D'", b);
4632 else
4633 cp_pedwarn_at (" enters scope of non-POD `%#D'", b);
4636 if (lab->in_try_scope)
4637 error (" enters try block");
4638 else if (lab->in_catch_scope)
4639 error (" enters catch block");
4642 /* Define a label, specifying the location in the source file.
4643 Return the LABEL_DECL node for the label. */
4645 tree
4646 define_label (location_t location, tree name)
4648 tree decl = lookup_label (name);
4649 struct named_label_list *ent;
4650 register struct cp_binding_level *p;
4652 timevar_push (TV_NAME_LOOKUP);
4653 for (ent = named_labels; ent; ent = ent->next)
4654 if (ent->label_decl == decl)
4655 break;
4657 /* After labels, make any new cleanups in the function go into their
4658 own new (temporary) binding contour. */
4659 for (p = current_binding_level;
4660 p->kind != sk_function_parms;
4661 p = p->level_chain)
4662 p->more_cleanups_ok = 0;
4664 if (name == get_identifier ("wchar_t"))
4665 pedwarn ("label named wchar_t");
4667 if (DECL_INITIAL (decl) != NULL_TREE)
4668 error ("duplicate label `%D'", decl);
4669 else
4671 /* Mark label as having been defined. */
4672 DECL_INITIAL (decl) = error_mark_node;
4673 /* Say where in the source. */
4674 DECL_SOURCE_LOCATION (decl) = location;
4675 if (ent)
4677 ent->names_in_scope = current_binding_level->names;
4678 ent->binding_level = current_binding_level;
4680 check_previous_gotos (decl);
4683 timevar_pop (TV_NAME_LOOKUP);
4684 return decl;
4687 struct cp_switch
4689 struct cp_binding_level *level;
4690 struct cp_switch *next;
4691 /* The SWITCH_STMT being built. */
4692 tree switch_stmt;
4693 /* A splay-tree mapping the low element of a case range to the high
4694 element, or NULL_TREE if there is no high element. Used to
4695 determine whether or not a new case label duplicates an old case
4696 label. We need a tree, rather than simply a hash table, because
4697 of the GNU case range extension. */
4698 splay_tree cases;
4701 /* A stack of the currently active switch statements. The innermost
4702 switch statement is on the top of the stack. There is no need to
4703 mark the stack for garbage collection because it is only active
4704 during the processing of the body of a function, and we never
4705 collect at that point. */
4707 static struct cp_switch *switch_stack;
4709 /* Called right after a switch-statement condition is parsed.
4710 SWITCH_STMT is the switch statement being parsed. */
4712 void
4713 push_switch (tree switch_stmt)
4715 struct cp_switch *p = xmalloc (sizeof (struct cp_switch));
4716 p->level = current_binding_level;
4717 p->next = switch_stack;
4718 p->switch_stmt = switch_stmt;
4719 p->cases = splay_tree_new (case_compare, NULL, NULL);
4720 switch_stack = p;
4723 void
4724 pop_switch (void)
4726 struct cp_switch *cs;
4728 cs = switch_stack;
4729 splay_tree_delete (cs->cases);
4730 switch_stack = switch_stack->next;
4731 free (cs);
4734 /* Note that we've seen a definition of a case label, and complain if this
4735 is a bad place for one. */
4737 tree
4738 finish_case_label (tree low_value, tree high_value)
4740 tree cond, r;
4741 register struct cp_binding_level *p;
4743 if (! switch_stack)
4745 if (high_value)
4746 error ("case label not within a switch statement");
4747 else if (low_value)
4748 error ("case label `%E' not within a switch statement",
4749 low_value);
4750 else
4751 error ("`default' label not within a switch statement");
4752 return NULL_TREE;
4755 if (processing_template_decl)
4757 tree label;
4759 /* For templates, just add the case label; we'll do semantic
4760 analysis at instantiation-time. */
4761 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4762 return add_stmt (build_case_label (low_value, high_value, label));
4765 /* Find the condition on which this switch statement depends. */
4766 cond = SWITCH_COND (switch_stack->switch_stmt);
4767 if (cond && TREE_CODE (cond) == TREE_LIST)
4768 cond = TREE_VALUE (cond);
4770 r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
4772 check_switch_goto (switch_stack->level);
4774 /* After labels, make any new cleanups in the function go into their
4775 own new (temporary) binding contour. */
4776 for (p = current_binding_level;
4777 p->kind != sk_function_parms;
4778 p = p->level_chain)
4779 p->more_cleanups_ok = 0;
4781 return r;
4784 /* Return the list of declarations of the current level.
4785 Note that this list is in reverse order unless/until
4786 you nreverse it; and when you do nreverse it, you must
4787 store the result back using `storedecls' or you will lose. */
4789 tree
4790 getdecls (void)
4792 return current_binding_level->names;
4795 /* Store the list of declarations of the current level.
4796 This is done for the parameter declarations of a function being defined,
4797 after they are modified in the light of any missing parameters. */
4799 static void
4800 storedecls (tree decls)
4802 current_binding_level->names = decls;
4805 /* Set the current binding TABLE for type declarations.. This is a
4806 temporary workaround of the fact that the data structure classtypes
4807 does not currently carry its allocated cxx_scope structure. */
4808 void
4809 cxx_remember_type_decls (binding_table table)
4811 current_binding_level->type_decls = table;
4814 /* Return the type that should be used when TYPE's name is preceded
4815 by a tag such as 'struct' or 'union', or null if the name cannot
4816 be used in this way.
4818 For example, when processing the third line of:
4820 struct A;
4821 typedef struct A A;
4822 struct A;
4824 lookup of A will find the typedef. Given A's typedef, this function
4825 will return the type associated with "struct A". For the tag to be
4826 anything other than TYPE, TYPE must be a typedef whose original type
4827 has the same name and context as TYPE itself.
4829 It is not valid for a typedef of an anonymous type to be used with
4830 an explicit tag:
4832 typedef struct { ... } B;
4833 struct B;
4835 Return null for this case. */
4837 static tree
4838 follow_tag_typedef (tree type)
4840 tree original;
4842 original = original_type (type);
4843 if (! TYPE_NAME (original))
4844 return NULL_TREE;
4845 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
4846 && (CP_DECL_CONTEXT (TYPE_NAME (original))
4847 == CP_DECL_CONTEXT (TYPE_NAME (type)))
4848 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
4849 return original;
4850 else
4851 return NULL_TREE;
4854 /* Given NAME, an IDENTIFIER_NODE,
4855 return the structure (or union or enum) definition for that name.
4856 Searches binding levels from its SCOPE up to the global level.
4857 If THISLEVEL_ONLY is nonzero, searches only the specified context
4858 (but skips any sk_cleanup contexts to find one that is
4859 meaningful for tags).
4860 FORM says which kind of type the caller wants;
4861 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4862 If the wrong kind of type is found, and it's not a template, an error is
4863 reported. */
4865 static tree
4866 lookup_tag (enum tree_code form, tree name,
4867 struct cp_binding_level* binding_level, int thislevel_only)
4869 register struct cp_binding_level *level;
4870 /* Nonzero if, we should look past a template parameter level, even
4871 if THISLEVEL_ONLY. */
4872 int allow_template_parms_p = 1;
4873 bool type_is_anonymous = ANON_AGGRNAME_P (name);
4875 timevar_push (TV_NAME_LOOKUP);
4876 for (level = binding_level; level; level = level->level_chain)
4878 register tree tail;
4879 if (type_is_anonymous && level->type_decls != NULL)
4881 tree type = binding_table_find_anon_type (level->type_decls, name);
4882 /* There is no need for error checking here, because
4883 anon names are unique throughout the compilation. */
4884 if (type != NULL)
4885 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
4887 else if (level->kind == sk_namespace)
4888 /* Do namespace lookup. */
4889 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
4891 cxx_binding *binding =
4892 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
4893 tree old;
4895 /* If we just skipped past a template parameter level,
4896 even though THISLEVEL_ONLY, and we find a template
4897 class declaration, then we use the _TYPE node for the
4898 template. See the example below. */
4899 if (thislevel_only && !allow_template_parms_p
4900 && binding && binding->value
4901 && DECL_CLASS_TEMPLATE_P (binding->value))
4902 old = binding->value;
4903 else if (binding)
4904 old = select_decl (binding, LOOKUP_PREFER_TYPES);
4905 else
4906 old = NULL_TREE;
4908 if (old)
4910 /* We've found something at this binding level. If it is
4911 a typedef, extract the tag it refers to. Lookup fails
4912 if the typedef doesn't refer to a taggable type. */
4913 old = TREE_TYPE (old);
4914 old = follow_tag_typedef (old);
4915 if (!old)
4916 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4917 if (TREE_CODE (old) != form
4918 && (form == ENUMERAL_TYPE
4919 || TREE_CODE (old) == ENUMERAL_TYPE))
4921 error ("`%#D' redeclared as %C", old, form);
4922 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4924 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
4926 if (thislevel_only || tail == global_namespace)
4927 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4929 else if (level->type_decls != NULL)
4931 binding_entry entry = binding_table_find (level->type_decls, name);
4932 if (entry != NULL)
4934 enum tree_code code = TREE_CODE (entry->type);
4936 if (code != form
4937 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
4939 /* Definition isn't the kind we were looking for. */
4940 error ("`%#D' redeclared as %C", entry->type, form);
4941 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4943 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
4946 if (thislevel_only && level->kind != sk_cleanup)
4948 if (level->kind == sk_template_parms && allow_template_parms_p)
4950 /* We must deal with cases like this:
4952 template <class T> struct S;
4953 template <class T> struct S {};
4955 When looking up `S', for the second declaration, we
4956 would like to find the first declaration. But, we
4957 are in the pseudo-global level created for the
4958 template parameters, rather than the (surrounding)
4959 namespace level. Thus, we keep going one more level,
4960 even though THISLEVEL_ONLY is nonzero. */
4961 allow_template_parms_p = 0;
4962 continue;
4964 else
4965 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4968 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4971 /* Given a type, find the tag that was defined for it and return the tag name.
4972 Otherwise return 0. However, the value can never be 0
4973 in the cases in which this is used.
4975 C++: If NAME is nonzero, this is the new name to install. This is
4976 done when replacing anonymous tags with real tag names. */
4978 static tree
4979 lookup_tag_reverse (tree type, tree name)
4981 register struct cp_binding_level *level;
4983 timevar_push (TV_NAME_LOOKUP);
4984 for (level = current_binding_level; level; level = level->level_chain)
4986 binding_entry entry = level->type_decls == NULL
4987 ? NULL
4988 : binding_table_reverse_maybe_remap (level->type_decls, type, name);
4989 if (entry)
4990 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
4992 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4995 /* Look up NAME in the NAMESPACE. */
4997 tree
4998 lookup_namespace_name (tree namespace, tree name)
5000 tree val;
5001 tree template_id = NULL_TREE;
5002 cxx_binding binding;
5004 timevar_push (TV_NAME_LOOKUP);
5005 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5007 if (TREE_CODE (name) == NAMESPACE_DECL)
5008 /* This happens for A::B<int> when B is a namespace. */
5009 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5010 else if (TREE_CODE (name) == TEMPLATE_DECL)
5012 /* This happens for A::B where B is a template, and there are no
5013 template arguments. */
5014 error ("invalid use of `%D'", name);
5015 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5018 namespace = ORIGINAL_NAMESPACE (namespace);
5020 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5022 template_id = name;
5023 name = TREE_OPERAND (name, 0);
5024 if (TREE_CODE (name) == OVERLOAD)
5025 name = DECL_NAME (OVL_CURRENT (name));
5026 else if (DECL_P (name))
5027 name = DECL_NAME (name);
5030 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5032 cxx_binding_clear (&binding);
5033 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
5034 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5036 if (binding.value)
5038 val = binding.value;
5040 if (template_id)
5042 if (DECL_CLASS_TEMPLATE_P (val))
5043 val = lookup_template_class (val,
5044 TREE_OPERAND (template_id, 1),
5045 /*in_decl=*/NULL_TREE,
5046 /*context=*/NULL_TREE,
5047 /*entering_scope=*/0,
5048 tf_error | tf_warning);
5049 else if (DECL_FUNCTION_TEMPLATE_P (val)
5050 || TREE_CODE (val) == OVERLOAD)
5051 val = lookup_template_function (val,
5052 TREE_OPERAND (template_id, 1));
5053 else
5055 error ("`%D::%D' is not a template",
5056 namespace, name);
5057 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5061 /* If we have a single function from a using decl, pull it out. */
5062 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5063 val = OVL_FUNCTION (val);
5065 /* Ignore built-in functions that haven't been prototyped yet. */
5066 if (!val || !DECL_P(val)
5067 || !DECL_LANG_SPECIFIC(val)
5068 || !DECL_ANTICIPATED (val))
5069 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5072 error ("`%D' undeclared in namespace `%D'", name, namespace);
5073 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5076 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
5078 static hashval_t
5079 typename_hash (const void* k)
5081 hashval_t hash;
5082 tree t = (tree) k;
5084 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5085 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5087 return hash;
5090 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
5092 static int
5093 typename_compare (const void * k1, const void * k2)
5095 tree t1;
5096 tree t2;
5097 tree d1;
5098 tree d2;
5100 t1 = (tree) k1;
5101 t2 = (tree) k2;
5102 d1 = TYPE_NAME (t1);
5103 d2 = TYPE_NAME (t2);
5105 return (DECL_NAME (d1) == DECL_NAME (d2)
5106 && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5107 && ((TREE_TYPE (t1) != NULL_TREE)
5108 == (TREE_TYPE (t2) != NULL_TREE))
5109 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5110 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5113 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5114 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
5115 is non-NULL, this type is being created by the implicit typename
5116 extension, and BASE_TYPE is a type named `t' in some base class of
5117 `T' which depends on template parameters.
5119 Returns the new TYPENAME_TYPE. */
5121 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5123 static tree
5124 build_typename_type (tree context, tree name, tree fullname)
5126 tree t;
5127 tree d;
5128 void **e;
5130 if (typename_htab == NULL)
5132 typename_htab = htab_create_ggc (61, &typename_hash,
5133 &typename_compare, NULL);
5136 /* Build the TYPENAME_TYPE. */
5137 t = make_aggr_type (TYPENAME_TYPE);
5138 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5139 TYPENAME_TYPE_FULLNAME (t) = fullname;
5141 /* Build the corresponding TYPE_DECL. */
5142 d = build_decl (TYPE_DECL, name, t);
5143 TYPE_NAME (TREE_TYPE (d)) = d;
5144 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5145 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5146 DECL_ARTIFICIAL (d) = 1;
5148 /* See if we already have this type. */
5149 e = htab_find_slot (typename_htab, t, INSERT);
5150 if (*e)
5151 t = (tree) *e;
5152 else
5153 *e = t;
5155 return t;
5158 /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type,
5159 unless an error occurs, in which case error_mark_node is returned.
5160 If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5161 set, we return that, rather than the _TYPE it corresponds to, in
5162 other cases we look through the type decl. If TF_ERROR is set,
5163 complain about errors, otherwise be quiet. */
5165 tree
5166 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5168 tree fullname;
5170 if (name == error_mark_node
5171 || context == NULL_TREE
5172 || context == error_mark_node)
5173 return error_mark_node;
5175 if (TYPE_P (name))
5177 if (!(TYPE_LANG_SPECIFIC (name)
5178 && (CLASSTYPE_IS_TEMPLATE (name)
5179 || CLASSTYPE_USE_TEMPLATE (name))))
5180 name = TYPE_IDENTIFIER (name);
5181 else
5182 /* Create a TEMPLATE_ID_EXPR for the type. */
5183 name = build_nt (TEMPLATE_ID_EXPR,
5184 CLASSTYPE_TI_TEMPLATE (name),
5185 CLASSTYPE_TI_ARGS (name));
5187 else if (TREE_CODE (name) == TYPE_DECL)
5188 name = DECL_NAME (name);
5190 fullname = name;
5192 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5194 name = TREE_OPERAND (name, 0);
5195 if (TREE_CODE (name) == TEMPLATE_DECL)
5196 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5198 if (TREE_CODE (name) == TEMPLATE_DECL)
5200 error ("`%D' used without template parameters", name);
5201 return error_mark_node;
5203 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 20030802);
5205 if (TREE_CODE (context) == NAMESPACE_DECL)
5207 /* We can get here from typename_sub0 in the explicit_template_type
5208 expansion. Just fail. */
5209 if (complain & tf_error)
5210 error ("no class template named `%#T' in `%#T'",
5211 name, context);
5212 return error_mark_node;
5215 if (! uses_template_parms (context)
5216 || currently_open_class (context))
5218 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5220 tree tmpl = NULL_TREE;
5221 if (IS_AGGR_TYPE (context))
5222 tmpl = lookup_field (context, name, 0, false);
5223 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5225 if (complain & tf_error)
5226 error ("no class template named `%#T' in `%#T'",
5227 name, context);
5228 return error_mark_node;
5231 if (complain & tf_error)
5232 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
5234 return lookup_template_class (tmpl,
5235 TREE_OPERAND (fullname, 1),
5236 NULL_TREE, context,
5237 /*entering_scope=*/0,
5238 tf_error | tf_warning | tf_user);
5240 else
5242 tree t;
5244 if (!IS_AGGR_TYPE (context))
5246 if (complain & tf_error)
5247 error ("no type named `%#T' in `%#T'", name, context);
5248 return error_mark_node;
5251 t = lookup_field (context, name, 0, true);
5252 if (t)
5254 if (TREE_CODE (t) != TYPE_DECL)
5256 if (complain & tf_error)
5257 error ("no type named `%#T' in `%#T'", name, context);
5258 return error_mark_node;
5261 if (complain & tf_error)
5262 perform_or_defer_access_check (TYPE_BINFO (context), t);
5264 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5265 t = TREE_TYPE (t);
5267 return t;
5272 /* If the CONTEXT is not a template type, then either the field is
5273 there now or its never going to be. */
5274 if (!uses_template_parms (context))
5276 if (complain & tf_error)
5277 error ("no type named `%#T' in `%#T'", name, context);
5278 return error_mark_node;
5281 return build_typename_type (context, name, fullname);
5284 /* Resolve `CONTEXT::template NAME'. Returns an appropriate type,
5285 unless an error occurs, in which case error_mark_node is returned.
5286 If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5287 corresponds to. If COMPLAIN zero, don't complain about any errors
5288 that occur. */
5290 tree
5291 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5293 tree t;
5294 tree d;
5296 if (TYPE_P (name))
5297 name = TYPE_IDENTIFIER (name);
5298 else if (DECL_P (name))
5299 name = DECL_NAME (name);
5300 if (TREE_CODE (name) != IDENTIFIER_NODE)
5301 abort ();
5303 if (!uses_template_parms (context)
5304 || currently_open_class (context))
5306 tree tmpl = NULL_TREE;
5308 if (IS_AGGR_TYPE (context))
5309 tmpl = lookup_field (context, name, 0, false);
5311 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5313 if (complain & tf_error)
5314 error ("no class template named `%#T' in `%#T'", name, context);
5315 return error_mark_node;
5318 if (complain & tf_error)
5319 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
5321 return tmpl;
5324 /* Build the UNBOUND_CLASS_TEMPLATE. */
5325 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5326 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5327 TREE_TYPE (t) = NULL_TREE;
5329 /* Build the corresponding TEMPLATE_DECL. */
5330 d = build_decl (TEMPLATE_DECL, name, t);
5331 TYPE_NAME (TREE_TYPE (d)) = d;
5332 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5333 DECL_CONTEXT (d) = FROB_CONTEXT (context);
5334 DECL_ARTIFICIAL (d) = 1;
5336 return t;
5339 /* Select the right _DECL from multiple choices. */
5341 static tree
5342 select_decl (cxx_binding *binding, int flags)
5344 tree val;
5345 val = binding->value;
5347 timevar_push (TV_NAME_LOOKUP);
5348 if (LOOKUP_NAMESPACES_ONLY (flags))
5350 /* We are not interested in types. */
5351 if (val && TREE_CODE (val) == NAMESPACE_DECL)
5352 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5353 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5356 /* If looking for a type, or if there is no non-type binding, select
5357 the value binding. */
5358 if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
5359 val = binding->type;
5360 /* Don't return non-types if we really prefer types. */
5361 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
5362 && (TREE_CODE (val) != TEMPLATE_DECL
5363 || !DECL_CLASS_TEMPLATE_P (val)))
5364 val = NULL_TREE;
5366 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5369 /* Unscoped lookup of a global: iterate over current namespaces,
5370 considering using-directives. If SPACESP is non-NULL, store a list
5371 of the namespaces we've considered in it. */
5373 tree
5374 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5376 tree initial = current_decl_namespace ();
5377 tree scope = initial;
5378 tree siter;
5379 struct cp_binding_level *level;
5380 tree val = NULL_TREE;
5381 cxx_binding binding;
5383 timevar_push (TV_NAME_LOOKUP);
5384 cxx_binding_clear (&binding);
5385 if (spacesp)
5386 *spacesp = NULL_TREE;
5388 for (; !val; scope = CP_DECL_CONTEXT (scope))
5390 cxx_binding *b =
5391 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
5392 if (spacesp)
5393 *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5395 /* Ignore anticipated built-in functions. */
5396 if (b && b->value && DECL_P (b->value)
5397 && DECL_LANG_SPECIFIC (b->value) && DECL_ANTICIPATED (b->value))
5398 /* Keep binding cleared. */;
5399 else if (b)
5401 /* Initialize binding for this context. */
5402 binding.value = b->value;
5403 binding.type = b->type;
5406 /* Add all _DECLs seen through local using-directives. */
5407 for (level = current_binding_level;
5408 level->kind != sk_namespace;
5409 level = level->level_chain)
5410 if (!lookup_using_namespace (name, &binding, level->using_directives,
5411 scope, flags, spacesp))
5412 /* Give up because of error. */
5413 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5415 /* Add all _DECLs seen through global using-directives. */
5416 /* XXX local and global using lists should work equally. */
5417 siter = initial;
5418 while (1)
5420 if (!lookup_using_namespace (name, &binding,
5421 DECL_NAMESPACE_USING (siter),
5422 scope, flags, spacesp))
5423 /* Give up because of error. */
5424 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5425 if (siter == scope) break;
5426 siter = CP_DECL_CONTEXT (siter);
5429 val = select_decl (&binding, flags);
5430 if (scope == global_namespace)
5431 break;
5433 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5436 /* Combine prefer_type and namespaces_only into flags. */
5438 static int
5439 lookup_flags (int prefer_type, int namespaces_only)
5441 if (namespaces_only)
5442 return LOOKUP_PREFER_NAMESPACES;
5443 if (prefer_type > 1)
5444 return LOOKUP_PREFER_TYPES;
5445 if (prefer_type > 0)
5446 return LOOKUP_PREFER_BOTH;
5447 return 0;
5450 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5451 ignore it or not. Subroutine of lookup_name_real. */
5453 static tree
5454 qualify_lookup (tree val, int flags)
5456 if (val == NULL_TREE)
5457 return val;
5458 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5459 return val;
5460 if ((flags & LOOKUP_PREFER_TYPES)
5461 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
5462 return val;
5463 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5464 return NULL_TREE;
5465 return val;
5468 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5469 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
5470 bindings.
5472 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5473 declaration found. If no suitable declaration can be found,
5474 ERROR_MARK_NODE is returned. Iif COMPLAIN is true and SCOPE is
5475 neither a class-type nor a namespace a diagnostic is issued. */
5477 tree
5478 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
5480 int flags = 0;
5482 if (TREE_CODE (scope) == NAMESPACE_DECL)
5484 cxx_binding binding;
5486 cxx_binding_clear (&binding);
5487 flags |= LOOKUP_COMPLAIN;
5488 if (is_type_p)
5489 flags |= LOOKUP_PREFER_TYPES;
5490 if (qualified_lookup_using_namespace (name, scope, &binding,
5491 flags))
5492 return select_decl (&binding, flags);
5494 else if (is_aggr_type (scope, complain))
5496 tree t;
5497 t = lookup_member (scope, name, 0, is_type_p);
5498 if (t)
5499 return t;
5502 return error_mark_node;
5505 /* Check to see whether or not DECL is a variable that would have been
5506 in scope under the ARM, but is not in scope under the ANSI/ISO
5507 standard. If so, issue an error message. If name lookup would
5508 work in both cases, but return a different result, this function
5509 returns the result of ANSI/ISO lookup. Otherwise, it returns
5510 DECL. */
5512 tree
5513 check_for_out_of_scope_variable (tree decl)
5515 tree shadowed;
5517 /* We only care about out of scope variables. */
5518 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5519 return decl;
5521 shadowed = DECL_SHADOWED_FOR_VAR (decl);
5522 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5523 && DECL_DEAD_FOR_LOCAL (shadowed))
5524 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5525 if (!shadowed)
5526 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5527 if (shadowed)
5529 if (!DECL_ERROR_REPORTED (decl))
5531 warning ("name lookup of `%D' changed",
5532 DECL_NAME (decl));
5533 cp_warning_at (" matches this `%D' under ISO standard rules",
5534 shadowed);
5535 cp_warning_at (" matches this `%D' under old rules", decl);
5536 DECL_ERROR_REPORTED (decl) = 1;
5538 return shadowed;
5541 /* If we have already complained about this declaration, there's no
5542 need to do it again. */
5543 if (DECL_ERROR_REPORTED (decl))
5544 return decl;
5546 DECL_ERROR_REPORTED (decl) = 1;
5547 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5549 error ("name lookup of `%D' changed for new ISO `for' scoping",
5550 DECL_NAME (decl));
5551 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl);
5552 return error_mark_node;
5554 else
5556 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5557 DECL_NAME (decl));
5558 cp_pedwarn_at (" using obsolete binding at `%D'", decl);
5561 return decl;
5564 /* Look up NAME in the current binding level and its superiors in the
5565 namespace of variables, functions and typedefs. Return a ..._DECL
5566 node of some kind representing its definition if there is only one
5567 such declaration, or return a TREE_LIST with all the overloaded
5568 definitions if there are many, or return 0 if it is undefined.
5570 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5571 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5572 Otherwise we prefer non-TYPE_DECLs.
5574 If NONCLASS is nonzero, we don't look for the NAME in class scope,
5575 using IDENTIFIER_CLASS_VALUE. */
5577 tree
5578 lookup_name_real (tree name, int prefer_type, int nonclass,
5579 int namespaces_only, int flags)
5581 cxx_binding *iter;
5582 tree val = NULL_TREE;
5584 timevar_push (TV_NAME_LOOKUP);
5585 /* Conversion operators are handled specially because ordinary
5586 unqualified name lookup will not find template conversion
5587 operators. */
5588 if (IDENTIFIER_TYPENAME_P (name))
5590 struct cp_binding_level *level;
5592 for (level = current_binding_level;
5593 level && level->kind != sk_namespace;
5594 level = level->level_chain)
5596 tree class_type;
5597 tree operators;
5599 /* A conversion operator can only be declared in a class
5600 scope. */
5601 if (level->kind != sk_class)
5602 continue;
5604 /* Lookup the conversion operator in the class. */
5605 class_type = level->this_entity;
5606 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5607 if (operators)
5608 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
5611 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5614 flags |= lookup_flags (prefer_type, namespaces_only);
5616 /* First, look in non-namespace scopes. */
5618 if (current_class_type == NULL_TREE)
5619 nonclass = 1;
5621 for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
5623 tree binding;
5625 if (!LOCAL_BINDING_P (iter) && nonclass)
5626 /* We're not looking for class-scoped bindings, so keep going. */
5627 continue;
5629 /* If this is the kind of thing we're looking for, we're done. */
5630 if (qualify_lookup (iter->value, flags))
5631 binding = iter->value;
5632 else if ((flags & LOOKUP_PREFER_TYPES)
5633 && qualify_lookup (iter->type, flags))
5634 binding = iter->type;
5635 else
5636 binding = NULL_TREE;
5638 if (binding)
5640 val = binding;
5641 break;
5645 /* Now lookup in namespace scopes. */
5646 if (!val)
5648 tree t = unqualified_namespace_lookup (name, flags, 0);
5649 if (t)
5650 val = t;
5653 if (val)
5655 /* If we have a single function from a using decl, pull it out. */
5656 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5657 val = OVL_FUNCTION (val);
5660 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5663 tree
5664 lookup_name_nonclass (tree name)
5666 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
5669 tree
5670 lookup_function_nonclass (tree name, tree args)
5672 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5675 tree
5676 lookup_name (tree name, int prefer_type)
5678 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
5681 /* Similar to `lookup_name' but look only in the innermost non-class
5682 binding level. */
5684 tree
5685 lookup_name_current_level (tree name)
5687 struct cp_binding_level *b;
5688 tree t = NULL_TREE;
5690 timevar_push (TV_NAME_LOOKUP);
5691 b = innermost_nonclass_level ();
5693 if (b->kind == sk_namespace)
5695 t = IDENTIFIER_NAMESPACE_VALUE (name);
5697 /* extern "C" function() */
5698 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5699 t = TREE_VALUE (t);
5701 else if (IDENTIFIER_BINDING (name)
5702 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5704 while (1)
5706 if (IDENTIFIER_BINDING (name)->scope == b)
5707 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
5709 if (b->kind == sk_cleanup)
5710 b = b->level_chain;
5711 else
5712 break;
5716 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5719 /* Like lookup_name_current_level, but for types. */
5721 tree
5722 lookup_type_current_level (tree name)
5724 register tree t = NULL_TREE;
5726 timevar_push (TV_NAME_LOOKUP);
5727 my_friendly_assert (current_binding_level->kind != sk_namespace,
5728 980716);
5730 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5731 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5733 struct cp_binding_level *b = current_binding_level;
5734 while (1)
5736 if (purpose_member (name, b->type_shadowed))
5737 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
5738 REAL_IDENTIFIER_TYPE_VALUE (name));
5739 if (b->kind == sk_cleanup)
5740 b = b->level_chain;
5741 else
5742 break;
5746 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5751 /* A chain of TYPE_DECLs for the builtin types. */
5753 static GTY(()) tree builtin_type_decls;
5755 /* Return a chain of TYPE_DECLs for the builtin types. */
5757 tree
5758 cxx_builtin_type_decls (void)
5760 return builtin_type_decls;
5763 /* Push the declarations of builtin types into the namespace.
5764 RID_INDEX is the index of the builtin type in the array
5765 RID_POINTERS. NAME is the name used when looking up the builtin
5766 type. TYPE is the _TYPE node for the builtin type. */
5768 void
5769 record_builtin_type (enum rid rid_index,
5770 const char* name,
5771 tree type)
5773 tree rname = NULL_TREE, tname = NULL_TREE;
5774 tree tdecl = NULL_TREE;
5776 if ((int) rid_index < (int) RID_MAX)
5777 rname = ridpointers[(int) rid_index];
5778 if (name)
5779 tname = get_identifier (name);
5781 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
5782 eliminated. Built-in types should not be looked up name; their
5783 names are keywords that the parser can recognize. However, there
5784 is code in c-common.c that uses identifier_global_value to look
5785 up built-in types by name. */
5786 if (tname)
5788 tdecl = build_decl (TYPE_DECL, tname, type);
5789 DECL_ARTIFICIAL (tdecl) = 1;
5790 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
5792 if (rname)
5794 if (!tdecl)
5796 tdecl = build_decl (TYPE_DECL, rname, type);
5797 DECL_ARTIFICIAL (tdecl) = 1;
5799 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
5802 if (!TYPE_NAME (type))
5803 TYPE_NAME (type) = tdecl;
5805 if (tdecl)
5807 TREE_CHAIN (tdecl) = builtin_type_decls;
5808 builtin_type_decls = tdecl;
5812 /* Record one of the standard Java types.
5813 * Declare it as having the given NAME.
5814 * If SIZE > 0, it is the size of one of the integral types;
5815 * otherwise it is the negative of the size of one of the other types. */
5817 static tree
5818 record_builtin_java_type (const char* name, int size)
5820 tree type, decl;
5821 if (size > 0)
5822 type = make_signed_type (size);
5823 else if (size > -32)
5824 { /* "__java_char" or ""__java_boolean". */
5825 type = make_unsigned_type (-size);
5826 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
5828 else
5829 { /* "__java_float" or ""__java_double". */
5830 type = make_node (REAL_TYPE);
5831 TYPE_PRECISION (type) = - size;
5832 layout_type (type);
5834 record_builtin_type (RID_MAX, name, type);
5835 decl = TYPE_NAME (type);
5837 /* Suppress generate debug symbol entries for these types,
5838 since for normal C++ they are just clutter.
5839 However, push_lang_context undoes this if extern "Java" is seen. */
5840 DECL_IGNORED_P (decl) = 1;
5842 TYPE_FOR_JAVA (type) = 1;
5843 return type;
5846 /* Push a type into the namespace so that the back-ends ignore it. */
5848 static void
5849 record_unknown_type (tree type, const char* name)
5851 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
5852 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5853 DECL_IGNORED_P (decl) = 1;
5854 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5855 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5856 TYPE_ALIGN (type) = 1;
5857 TYPE_USER_ALIGN (type) = 0;
5858 TYPE_MODE (type) = TYPE_MODE (void_type_node);
5861 /* An string for which we should create an IDENTIFIER_NODE at
5862 startup. */
5864 typedef struct predefined_identifier
5866 /* The name of the identifier. */
5867 const char *const name;
5868 /* The place where the IDENTIFIER_NODE should be stored. */
5869 tree *const node;
5870 /* Nonzero if this is the name of a constructor or destructor. */
5871 const int ctor_or_dtor_p;
5872 } predefined_identifier;
5874 /* Create all the predefined identifiers. */
5876 static void
5877 initialize_predefined_identifiers (void)
5879 const predefined_identifier *pid;
5881 /* A table of identifiers to create at startup. */
5882 static const predefined_identifier predefined_identifiers[] = {
5883 { "C++", &lang_name_cplusplus, 0 },
5884 { "C", &lang_name_c, 0 },
5885 { "Java", &lang_name_java, 0 },
5886 { CTOR_NAME, &ctor_identifier, 1 },
5887 { "__base_ctor", &base_ctor_identifier, 1 },
5888 { "__comp_ctor", &complete_ctor_identifier, 1 },
5889 { DTOR_NAME, &dtor_identifier, 1 },
5890 { "__comp_dtor", &complete_dtor_identifier, 1 },
5891 { "__base_dtor", &base_dtor_identifier, 1 },
5892 { "__deleting_dtor", &deleting_dtor_identifier, 1 },
5893 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
5894 { "nelts", &nelts_identifier, 0 },
5895 { THIS_NAME, &this_identifier, 0 },
5896 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
5897 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
5898 { "_vptr", &vptr_identifier, 0 },
5899 { "__vtt_parm", &vtt_parm_identifier, 0 },
5900 { "::", &global_scope_name, 0 },
5901 { "std", &std_identifier, 0 },
5902 { NULL, NULL, 0 }
5905 for (pid = predefined_identifiers; pid->name; ++pid)
5907 *pid->node = get_identifier (pid->name);
5908 if (pid->ctor_or_dtor_p)
5909 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
5913 /* Create the predefined scalar types of C,
5914 and some nodes representing standard constants (0, 1, (void *)0).
5915 Initialize the global binding level.
5916 Make definitions for built-in primitive functions. */
5918 void
5919 cxx_init_decl_processing (void)
5921 tree void_ftype;
5922 tree void_ftype_ptr;
5924 /* Create all the identifiers we need. */
5925 initialize_predefined_identifiers ();
5927 /* Fill in back-end hooks. */
5928 lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
5930 /* Create the global variables. */
5931 push_to_top_level ();
5933 current_function_decl = NULL_TREE;
5934 current_binding_level = NULL;
5935 /* Enter the global namespace. */
5936 my_friendly_assert (global_namespace == NULL_TREE, 375);
5937 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
5938 void_type_node);
5939 begin_scope (sk_namespace, global_namespace);
5941 current_lang_name = NULL_TREE;
5943 /* Adjust various flags based on command-line settings. */
5944 if (!flag_permissive)
5945 flag_pedantic_errors = 1;
5946 if (!flag_no_inline)
5948 flag_inline_trees = 1;
5949 flag_no_inline = 1;
5951 if (flag_inline_functions)
5953 flag_inline_trees = 2;
5954 flag_inline_functions = 0;
5957 /* Force minimum function alignment if using the least significant
5958 bit of function pointers to store the virtual bit. */
5959 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
5960 && force_align_functions_log < 1)
5961 force_align_functions_log = 1;
5963 /* Initially, C. */
5964 current_lang_name = lang_name_c;
5966 build_common_tree_nodes (flag_signed_char);
5968 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
5969 TREE_TYPE (error_mark_list) = error_mark_node;
5971 /* Create the `std' namespace. */
5972 push_namespace (std_identifier);
5973 std_node = current_namespace;
5974 pop_namespace ();
5976 c_common_nodes_and_builtins ();
5978 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
5979 java_short_type_node = record_builtin_java_type ("__java_short", 16);
5980 java_int_type_node = record_builtin_java_type ("__java_int", 32);
5981 java_long_type_node = record_builtin_java_type ("__java_long", 64);
5982 java_float_type_node = record_builtin_java_type ("__java_float", -32);
5983 java_double_type_node = record_builtin_java_type ("__java_double", -64);
5984 java_char_type_node = record_builtin_java_type ("__java_char", -16);
5985 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
5987 integer_two_node = build_int_2 (2, 0);
5988 TREE_TYPE (integer_two_node) = integer_type_node;
5989 integer_three_node = build_int_2 (3, 0);
5990 TREE_TYPE (integer_three_node) = integer_type_node;
5992 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
5993 truthvalue_type_node = boolean_type_node;
5994 truthvalue_false_node = boolean_false_node;
5995 truthvalue_true_node = boolean_true_node;
5997 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5999 #if 0
6000 record_builtin_type (RID_MAX, NULL, string_type_node);
6001 #endif
6003 delta_type_node = ptrdiff_type_node;
6004 vtable_index_type = ptrdiff_type_node;
6006 vtt_parm_type = build_pointer_type (const_ptr_type_node);
6007 void_ftype = build_function_type (void_type_node, void_list_node);
6008 void_ftype_ptr = build_function_type (void_type_node,
6009 tree_cons (NULL_TREE,
6010 ptr_type_node,
6011 void_list_node));
6012 void_ftype_ptr
6013 = build_exception_variant (void_ftype_ptr, empty_except_spec);
6015 /* C++ extensions */
6017 unknown_type_node = make_node (UNKNOWN_TYPE);
6018 record_unknown_type (unknown_type_node, "unknown type");
6020 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
6021 TREE_TYPE (unknown_type_node) = unknown_type_node;
6023 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6024 result. */
6025 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6026 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6029 /* Make sure we get a unique function type, so we can give
6030 its pointer type a name. (This wins for gdb.) */
6031 tree vfunc_type = make_node (FUNCTION_TYPE);
6032 TREE_TYPE (vfunc_type) = integer_type_node;
6033 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6034 layout_type (vfunc_type);
6036 vtable_entry_type = build_pointer_type (vfunc_type);
6038 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6040 vtbl_type_node
6041 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6042 layout_type (vtbl_type_node);
6043 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6044 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6045 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6046 layout_type (vtbl_ptr_type_node);
6047 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6049 push_namespace (get_identifier ("__cxxabiv1"));
6050 abi_node = current_namespace;
6051 pop_namespace ();
6053 global_type_node = make_node (LANG_TYPE);
6054 record_unknown_type (global_type_node, "global type");
6056 /* Now, C++. */
6057 current_lang_name = lang_name_cplusplus;
6060 tree bad_alloc_id;
6061 tree bad_alloc_type_node;
6062 tree bad_alloc_decl;
6063 tree newtype, deltype;
6064 tree ptr_ftype_sizetype;
6066 push_namespace (std_identifier);
6067 bad_alloc_id = get_identifier ("bad_alloc");
6068 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
6069 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
6070 bad_alloc_decl
6071 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
6072 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
6073 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
6074 pop_namespace ();
6076 ptr_ftype_sizetype
6077 = build_function_type (ptr_type_node,
6078 tree_cons (NULL_TREE,
6079 size_type_node,
6080 void_list_node));
6081 newtype = build_exception_variant
6082 (ptr_ftype_sizetype, add_exception_specifier
6083 (NULL_TREE, bad_alloc_type_node, -1));
6084 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6085 push_cp_library_fn (NEW_EXPR, newtype);
6086 push_cp_library_fn (VEC_NEW_EXPR, newtype);
6087 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6088 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6091 abort_fndecl
6092 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6094 /* Perform other language dependent initializations. */
6095 init_class_processing ();
6096 init_search_processing ();
6097 init_rtti_processing ();
6099 if (flag_exceptions)
6100 init_exception_processing ();
6102 if (! supports_one_only ())
6103 flag_weak = 0;
6105 make_fname_decl = cp_make_fname_decl;
6106 start_fname_decls ();
6108 /* Show we use EH for cleanups. */
6109 using_eh_for_cleanups ();
6111 /* Maintain consistency. Perhaps we should just complain if they
6112 say -fwritable-strings? */
6113 if (flag_writable_strings)
6114 flag_const_strings = 0;
6117 /* Generate an initializer for a function naming variable from
6118 NAME. NAME may be NULL, in which case we generate a special
6119 ERROR_MARK node which should be replaced later. */
6121 tree
6122 cp_fname_init (const char* name)
6124 tree domain = NULL_TREE;
6125 tree type;
6126 tree init = NULL_TREE;
6127 size_t length = 0;
6129 if (name)
6131 length = strlen (name);
6132 domain = build_index_type (size_int (length));
6133 init = build_string (length + 1, name);
6136 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6137 type = build_cplus_array_type (type, domain);
6139 if (init)
6140 TREE_TYPE (init) = type;
6141 else
6142 /* We don't know the value until instantiation time. Make
6143 something which will be digested now, but replaced later. */
6144 init = build (ERROR_MARK, type);
6146 return init;
6149 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6150 decl, NAME is the initialization string and TYPE_DEP indicates whether
6151 NAME depended on the type of the function. We make use of that to detect
6152 __PRETTY_FUNCTION__ inside a template fn. This is being done
6153 lazily at the point of first use, so we musn't push the decl now. */
6155 static tree
6156 cp_make_fname_decl (tree id, int type_dep)
6158 const char *const name = (type_dep && processing_template_decl
6159 ? NULL : fname_as_string (type_dep));
6160 tree init = cp_fname_init (name);
6161 tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6163 /* As we're using pushdecl_with_scope, we must set the context. */
6164 DECL_CONTEXT (decl) = current_function_decl;
6165 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6167 TREE_STATIC (decl) = 1;
6168 TREE_READONLY (decl) = 1;
6169 DECL_ARTIFICIAL (decl) = 1;
6170 DECL_INITIAL (decl) = init;
6172 TREE_USED (decl) = 1;
6174 if (current_function_decl)
6176 struct cp_binding_level *b = current_binding_level;
6177 while (b->level_chain->kind != sk_function_parms)
6178 b = b->level_chain;
6179 pushdecl_with_scope (decl, b);
6182 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6184 return decl;
6187 /* Make a definition for a builtin function named NAME in the current
6188 namespace, whose data type is TYPE and whose context is CONTEXT.
6189 TYPE should be a function type with argument types.
6191 CLASS and CODE tell later passes how to compile calls to this function.
6192 See tree.h for possible values.
6194 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6195 the name to be called if we can't opencode the function.
6196 If ATTRS is nonzero, use that for the function's attribute
6197 list. */
6199 static tree
6200 builtin_function_1 (const char* name,
6201 tree type,
6202 tree context,
6203 int code,
6204 enum built_in_class class,
6205 const char* libname,
6206 tree attrs)
6208 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6209 DECL_BUILT_IN_CLASS (decl) = class;
6210 DECL_FUNCTION_CODE (decl) = code;
6211 DECL_CONTEXT (decl) = context;
6213 pushdecl (decl);
6215 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6216 we cannot change DECL_ASSEMBLER_NAME until we have installed this
6217 function in the namespace. */
6218 if (libname)
6219 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6220 make_decl_rtl (decl, NULL);
6222 /* Warn if a function in the namespace for users
6223 is used without an occasion to consider it declared. */
6224 if (name[0] != '_' || name[1] != '_')
6225 DECL_ANTICIPATED (decl) = 1;
6227 /* Possibly apply some default attributes to this built-in function. */
6228 if (attrs)
6229 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6230 else
6231 decl_attributes (&decl, NULL_TREE, 0);
6233 return decl;
6236 /* Entry point for the benefit of c_common_nodes_and_builtins.
6238 Make a definition for a builtin function named NAME and whose data type
6239 is TYPE. TYPE should be a function type with argument types. This
6240 function places the anticipated declaration in the global namespace
6241 and additionally in the std namespace if appropriate.
6243 CLASS and CODE tell later passes how to compile calls to this function.
6244 See tree.h for possible values.
6246 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6247 the name to be called if we can't opencode the function.
6249 If ATTRS is nonzero, use that for the function's attribute
6250 list. */
6252 tree
6253 builtin_function (const char* name,
6254 tree type,
6255 int code,
6256 enum built_in_class class,
6257 const char* libname,
6258 tree attrs)
6260 /* All builtins that don't begin with an '_' should additionally
6261 go in the 'std' namespace. */
6262 if (name[0] != '_')
6264 push_namespace (std_identifier);
6265 builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6266 pop_namespace ();
6269 return builtin_function_1 (name, type, NULL_TREE, code,
6270 class, libname, attrs);
6273 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6274 function. Not called directly. */
6276 static tree
6277 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6279 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6280 DECL_EXTERNAL (fn) = 1;
6281 TREE_PUBLIC (fn) = 1;
6282 DECL_ARTIFICIAL (fn) = 1;
6283 TREE_NOTHROW (fn) = 1;
6284 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6285 SET_DECL_LANGUAGE (fn, lang_c);
6286 return fn;
6289 /* Returns the _DECL for a library function with C linkage.
6290 We assume that such functions never throw; if this is incorrect,
6291 callers should unset TREE_NOTHROW. */
6293 tree
6294 build_library_fn (tree name, tree type)
6296 return build_library_fn_1 (name, ERROR_MARK, type);
6299 /* Returns the _DECL for a library function with C++ linkage. */
6301 static tree
6302 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6304 tree fn = build_library_fn_1 (name, operator_code, type);
6305 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6306 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6307 SET_DECL_LANGUAGE (fn, lang_cplusplus);
6308 set_mangled_name_for_decl (fn);
6309 return fn;
6312 /* Like build_library_fn, but takes a C string instead of an
6313 IDENTIFIER_NODE. */
6315 tree
6316 build_library_fn_ptr (const char* name, tree type)
6318 return build_library_fn (get_identifier (name), type);
6321 /* Like build_cp_library_fn, but takes a C string instead of an
6322 IDENTIFIER_NODE. */
6324 tree
6325 build_cp_library_fn_ptr (const char* name, tree type)
6327 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6330 /* Like build_library_fn, but also pushes the function so that we will
6331 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
6333 tree
6334 push_library_fn (tree name, tree type)
6336 tree fn = build_library_fn (name, type);
6337 pushdecl_top_level (fn);
6338 return fn;
6341 /* Like build_cp_library_fn, but also pushes the function so that it
6342 will be found by normal lookup. */
6344 static tree
6345 push_cp_library_fn (enum tree_code operator_code, tree type)
6347 tree fn = build_cp_library_fn (ansi_opname (operator_code),
6348 operator_code,
6349 type);
6350 pushdecl (fn);
6351 return fn;
6354 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6355 a FUNCTION_TYPE. */
6357 tree
6358 push_void_library_fn (tree name, tree parmtypes)
6360 tree type = build_function_type (void_type_node, parmtypes);
6361 return push_library_fn (name, type);
6364 /* Like push_library_fn, but also note that this function throws
6365 and does not return. Used for __throw_foo and the like. */
6367 tree
6368 push_throw_library_fn (tree name, tree type)
6370 tree fn = push_library_fn (name, type);
6371 TREE_THIS_VOLATILE (fn) = 1;
6372 TREE_NOTHROW (fn) = 0;
6373 return fn;
6376 /* When we call finish_struct for an anonymous union, we create
6377 default copy constructors and such. But, an anonymous union
6378 shouldn't have such things; this function undoes the damage to the
6379 anonymous union type T.
6381 (The reason that we create the synthesized methods is that we don't
6382 distinguish `union { int i; }' from `typedef union { int i; } U'.
6383 The first is an anonymous union; the second is just an ordinary
6384 union type.) */
6386 void
6387 fixup_anonymous_aggr (tree t)
6389 tree *q;
6391 /* Wipe out memory of synthesized methods */
6392 TYPE_HAS_CONSTRUCTOR (t) = 0;
6393 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6394 TYPE_HAS_INIT_REF (t) = 0;
6395 TYPE_HAS_CONST_INIT_REF (t) = 0;
6396 TYPE_HAS_ASSIGN_REF (t) = 0;
6397 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6399 /* Splice the implicitly generated functions out of the TYPE_METHODS
6400 list. */
6401 q = &TYPE_METHODS (t);
6402 while (*q)
6404 if (DECL_ARTIFICIAL (*q))
6405 *q = TREE_CHAIN (*q);
6406 else
6407 q = &TREE_CHAIN (*q);
6410 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
6411 if (TYPE_METHODS (t))
6412 error ("%Jan anonymous union cannot have function members",
6413 TYPE_MAIN_DECL (t));
6415 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6416 assignment operators (because they cannot have these methods themselves).
6417 For anonymous unions this is already checked because they are not allowed
6418 in any union, otherwise we have to check it. */
6419 if (TREE_CODE (t) != UNION_TYPE)
6421 tree field, type;
6423 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6424 if (TREE_CODE (field) == FIELD_DECL)
6426 type = TREE_TYPE (field);
6427 if (CLASS_TYPE_P (type))
6429 if (TYPE_NEEDS_CONSTRUCTING (type))
6430 cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6431 field);
6432 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6433 cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6434 field);
6435 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6436 cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6437 field);
6443 /* Make sure that a declaration with no declarator is well-formed, i.e.
6444 just declares a tagged type or anonymous union.
6446 Returns the type declared; or NULL_TREE if none. */
6448 tree
6449 check_tag_decl (tree declspecs)
6451 int found_type = 0;
6452 int saw_friend = 0;
6453 int saw_typedef = 0;
6454 tree ob_modifier = NULL_TREE;
6455 register tree link;
6456 /* If a class, struct, or enum type is declared by the DECLSPECS
6457 (i.e, if a class-specifier, enum-specifier, or non-typename
6458 elaborated-type-specifier appears in the DECLSPECS),
6459 DECLARED_TYPE is set to the corresponding type. */
6460 tree declared_type = NULL_TREE;
6461 bool error_p = false;
6463 for (link = declspecs; link; link = TREE_CHAIN (link))
6465 tree value = TREE_VALUE (link);
6467 if (TYPE_P (value) || TREE_CODE (value) == TYPE_DECL
6468 || (TREE_CODE (value) == IDENTIFIER_NODE
6469 && is_typename_at_global_scope (value)))
6471 ++found_type;
6473 if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6475 if (! in_system_header)
6476 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6477 return NULL_TREE;
6480 if (TYPE_P (value)
6481 && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6482 || TREE_CODE (value) == ENUMERAL_TYPE))
6484 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6485 declared_type = value;
6488 else if (value == ridpointers[(int) RID_TYPEDEF])
6489 saw_typedef = 1;
6490 else if (value == ridpointers[(int) RID_FRIEND])
6492 if (current_class_type == NULL_TREE
6493 || current_scope () != current_class_type)
6494 ob_modifier = value;
6495 else
6496 saw_friend = 1;
6498 else if (value == ridpointers[(int) RID_STATIC]
6499 || value == ridpointers[(int) RID_EXTERN]
6500 || value == ridpointers[(int) RID_AUTO]
6501 || value == ridpointers[(int) RID_REGISTER]
6502 || value == ridpointers[(int) RID_INLINE]
6503 || value == ridpointers[(int) RID_VIRTUAL]
6504 || value == ridpointers[(int) RID_CONST]
6505 || value == ridpointers[(int) RID_VOLATILE]
6506 || value == ridpointers[(int) RID_EXPLICIT]
6507 || value == ridpointers[(int) RID_THREAD])
6508 ob_modifier = value;
6509 else if (value == error_mark_node)
6510 error_p = true;
6513 if (found_type > 1)
6514 error ("multiple types in one declaration");
6516 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6517 pedwarn ("declaration does not declare anything");
6518 /* Check for an anonymous union. */
6519 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6520 && TYPE_ANONYMOUS_P (declared_type))
6522 /* 7/3 In a simple-declaration, the optional init-declarator-list
6523 can be omitted only when declaring a class (clause 9) or
6524 enumeration (7.2), that is, when the decl-specifier-seq contains
6525 either a class-specifier, an elaborated-type-specifier with
6526 a class-key (9.1), or an enum-specifier. In these cases and
6527 whenever a class-specifier or enum-specifier is present in the
6528 decl-specifier-seq, the identifiers in these specifiers are among
6529 the names being declared by the declaration (as class-name,
6530 enum-names, or enumerators, depending on the syntax). In such
6531 cases, and except for the declaration of an unnamed bit-field (9.6),
6532 the decl-specifier-seq shall introduce one or more names into the
6533 program, or shall redeclare a name introduced by a previous
6534 declaration. [Example:
6535 enum { }; // ill-formed
6536 typedef class { }; // ill-formed
6537 --end example] */
6538 if (saw_typedef)
6540 error ("missing type-name in typedef-declaration");
6541 return NULL_TREE;
6543 /* Anonymous unions are objects, so they can have specifiers. */;
6544 SET_ANON_AGGR_TYPE_P (declared_type);
6546 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
6547 && !in_system_header)
6548 pedwarn ("ISO C++ prohibits anonymous structs");
6551 else if (ob_modifier)
6553 if (ob_modifier == ridpointers[(int) RID_INLINE]
6554 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6555 error ("`%D' can only be specified for functions", ob_modifier);
6556 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6557 error ("`%D' can only be specified inside a class", ob_modifier);
6558 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6559 error ("`%D' can only be specified for constructors",
6560 ob_modifier);
6561 else
6562 error ("`%D' can only be specified for objects and functions",
6563 ob_modifier);
6566 return declared_type;
6569 /* Called when a declaration is seen that contains no names to declare.
6570 If its type is a reference to a structure, union or enum inherited
6571 from a containing scope, shadow that tag name for the current scope
6572 with a forward reference.
6573 If its type defines a new named structure or union
6574 or defines an enum, it is valid but we need not do anything here.
6575 Otherwise, it is an error.
6577 C++: may have to grok the declspecs to learn about static,
6578 complain for anonymous unions.
6580 Returns the TYPE declared -- or NULL_TREE if none. */
6582 tree
6583 shadow_tag (tree declspecs)
6585 tree t = check_tag_decl (declspecs);
6587 if (!t)
6588 return NULL_TREE;
6590 maybe_process_partial_specialization (t);
6592 /* This is where the variables in an anonymous union are
6593 declared. An anonymous union declaration looks like:
6594 union { ... } ;
6595 because there is no declarator after the union, the parser
6596 sends that declaration here. */
6597 if (ANON_AGGR_TYPE_P (t))
6599 fixup_anonymous_aggr (t);
6601 if (TYPE_FIELDS (t))
6603 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6604 NULL);
6605 finish_anon_union (decl);
6609 return t;
6612 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6614 tree
6615 groktypename (tree typename)
6617 tree specs, attrs;
6618 tree type;
6619 if (TREE_CODE (typename) != TREE_LIST)
6620 return typename;
6621 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6622 type = grokdeclarator (TREE_VALUE (typename), specs,
6623 TYPENAME, 0, &attrs);
6624 if (attrs)
6625 cplus_decl_attributes (&type, attrs, 0);
6626 return type;
6629 /* Decode a declarator in an ordinary declaration or data definition.
6630 This is called as soon as the type information and variable name
6631 have been parsed, before parsing the initializer if any.
6632 Here we create the ..._DECL node, fill in its type,
6633 and put it on the list of decls for the current context.
6634 The ..._DECL node is returned as the value.
6636 Exception: for arrays where the length is not specified,
6637 the type is left null, to be filled in by `cp_finish_decl'.
6639 Function definitions do not come here; they go to start_function
6640 instead. However, external and forward declarations of functions
6641 do go through here. Structure field declarations are done by
6642 grokfield and not through here. */
6644 tree
6645 start_decl (tree declarator,
6646 tree declspecs,
6647 int initialized,
6648 tree attributes,
6649 tree prefix_attributes)
6651 tree decl;
6652 register tree type, tem;
6653 tree context;
6655 /* This should only be done once on the top most decl. */
6656 if (have_extern_spec)
6658 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
6659 declspecs);
6660 have_extern_spec = false;
6663 /* An object declared as __attribute__((deprecated)) suppresses
6664 warnings of uses of other deprecated items. */
6665 if (lookup_attribute ("deprecated", attributes))
6666 deprecated_state = DEPRECATED_SUPPRESS;
6668 attributes = chainon (attributes, prefix_attributes);
6670 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6671 &attributes);
6673 deprecated_state = DEPRECATED_NORMAL;
6675 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
6676 return NULL_TREE;
6678 type = TREE_TYPE (decl);
6680 if (type == error_mark_node)
6681 return NULL_TREE;
6683 context = DECL_CONTEXT (decl);
6685 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6686 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6688 /* When parsing the initializer, lookup should use the object's
6689 namespace. */
6690 push_decl_namespace (context);
6693 /* We are only interested in class contexts, later. */
6694 if (context && TREE_CODE (context) == NAMESPACE_DECL)
6695 context = NULL_TREE;
6697 if (initialized)
6698 /* Is it valid for this decl to have an initializer at all?
6699 If not, set INITIALIZED to zero, which will indirectly
6700 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
6701 switch (TREE_CODE (decl))
6703 case TYPE_DECL:
6704 error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
6705 initialized = 0;
6706 break;
6708 case FUNCTION_DECL:
6709 error ("function `%#D' is initialized like a variable", decl);
6710 initialized = 0;
6711 break;
6713 default:
6714 break;
6717 if (initialized)
6719 if (! toplevel_bindings_p ()
6720 && DECL_EXTERNAL (decl))
6721 warning ("declaration of `%#D' has `extern' and is initialized",
6722 decl);
6723 DECL_EXTERNAL (decl) = 0;
6724 if (toplevel_bindings_p ())
6725 TREE_STATIC (decl) = 1;
6727 /* Tell `pushdecl' this is an initialized decl
6728 even though we don't yet have the initializer expression.
6729 Also tell `cp_finish_decl' it may store the real initializer. */
6730 DECL_INITIAL (decl) = error_mark_node;
6733 /* Set attributes here so if duplicate decl, will have proper attributes. */
6734 cplus_decl_attributes (&decl, attributes, 0);
6736 /* If #pragma weak was used, mark the decl weak now. */
6737 if (global_scope_p (current_binding_level))
6738 maybe_apply_pragma_weak (decl);
6740 if (TREE_CODE (decl) == FUNCTION_DECL
6741 && DECL_DECLARED_INLINE_P (decl)
6742 && DECL_UNINLINABLE (decl)
6743 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6744 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
6746 if (context && COMPLETE_TYPE_P (complete_type (context)))
6748 push_nested_class (context);
6750 if (TREE_CODE (decl) == VAR_DECL)
6752 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6753 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
6754 error ("`%#D' is not a static member of `%#T'", decl, context);
6755 else
6757 if (DECL_CONTEXT (field) != context)
6759 if (!same_type_p (DECL_CONTEXT (field), context))
6760 pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
6761 DECL_CONTEXT (field), DECL_NAME (decl),
6762 context, DECL_NAME (decl));
6763 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6765 /* Static data member are tricky; an in-class initialization
6766 still doesn't provide a definition, so the in-class
6767 declaration will have DECL_EXTERNAL set, but will have an
6768 initialization. Thus, duplicate_decls won't warn
6769 about this situation, and so we check here. */
6770 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
6771 error ("duplicate initialization of %D", decl);
6772 if (duplicate_decls (decl, field))
6773 decl = field;
6776 else
6778 tree field = check_classfn (context, decl);
6779 if (field && duplicate_decls (decl, field))
6780 decl = field;
6783 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6784 DECL_IN_AGGR_P (decl) = 0;
6785 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6786 || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
6788 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6789 /* [temp.expl.spec] An explicit specialization of a static data
6790 member of a template is a definition if the declaration
6791 includes an initializer; otherwise, it is a declaration.
6793 We check for processing_specialization so this only applies
6794 to the new specialization syntax. */
6795 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
6796 DECL_EXTERNAL (decl) = 1;
6799 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
6800 pedwarn ("declaration of `%#D' outside of class is not definition",
6801 decl);
6804 /* Enter this declaration into the symbol table. */
6805 tem = maybe_push_decl (decl);
6807 if (processing_template_decl)
6808 tem = push_template_decl (tem);
6810 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
6811 /* Tell the back-end to use or not use .common as appropriate. If we say
6812 -fconserve-space, we want this to save .data space, at the expense of
6813 wrong semantics. If we say -fno-conserve-space, we want this to
6814 produce errors about redefs; to do this we force variables into the
6815 data segment. */
6816 DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
6817 || !DECL_THREAD_LOCAL (tem))
6818 && (flag_conserve_space || ! TREE_PUBLIC (tem)));
6819 #endif
6821 if (! processing_template_decl)
6822 start_decl_1 (tem);
6824 return tem;
6827 void
6828 start_decl_1 (tree decl)
6830 tree type = TREE_TYPE (decl);
6831 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
6833 if (type == error_mark_node)
6834 return;
6836 maybe_push_cleanup_level (type);
6838 if (initialized)
6839 /* Is it valid for this decl to have an initializer at all?
6840 If not, set INITIALIZED to zero, which will indirectly
6841 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
6843 /* Don't allow initializations for incomplete types except for
6844 arrays which might be completed by the initialization. */
6845 if (COMPLETE_TYPE_P (complete_type (type)))
6846 ; /* A complete type is ok. */
6847 else if (TREE_CODE (type) != ARRAY_TYPE)
6849 error ("variable `%#D' has initializer but incomplete type",
6850 decl);
6851 initialized = 0;
6852 type = TREE_TYPE (decl) = error_mark_node;
6854 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6856 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6857 error ("elements of array `%#D' have incomplete type", decl);
6858 /* else we already gave an error in start_decl. */
6859 initialized = 0;
6863 if (!initialized
6864 && TREE_CODE (decl) != TYPE_DECL
6865 && TREE_CODE (decl) != TEMPLATE_DECL
6866 && type != error_mark_node
6867 && IS_AGGR_TYPE (type)
6868 && ! DECL_EXTERNAL (decl))
6870 if ((! processing_template_decl || ! uses_template_parms (type))
6871 && !COMPLETE_TYPE_P (complete_type (type)))
6873 error ("aggregate `%#D' has incomplete type and cannot be defined",
6874 decl);
6875 /* Change the type so that assemble_variable will give
6876 DECL an rtl we can live with: (mem (const_int 0)). */
6877 type = TREE_TYPE (decl) = error_mark_node;
6879 else
6881 /* If any base type in the hierarchy of TYPE needs a constructor,
6882 then we set initialized to 1. This way any nodes which are
6883 created for the purposes of initializing this aggregate
6884 will live as long as it does. This is necessary for global
6885 aggregates which do not have their initializers processed until
6886 the end of the file. */
6887 initialized = TYPE_NEEDS_CONSTRUCTING (type);
6891 if (! initialized)
6892 DECL_INITIAL (decl) = NULL_TREE;
6895 /* Handle initialization of references. DECL, TYPE, and INIT have the
6896 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6897 but will be set to a new CLEANUP_STMT if a temporary is created
6898 that must be destroeyd subsequently.
6900 Returns an initializer expression to use to initialize DECL, or
6901 NULL if the initialization can be performed statically.
6903 Quotes on semantics can be found in ARM 8.4.3. */
6905 static tree
6906 grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
6908 tree tmp;
6910 if (init == NULL_TREE)
6912 if ((DECL_LANG_SPECIFIC (decl) == 0
6913 || DECL_IN_AGGR_P (decl) == 0)
6914 && ! DECL_THIS_EXTERN (decl))
6915 error ("`%D' declared as reference but not initialized", decl);
6916 return NULL_TREE;
6919 if (TREE_CODE (init) == CONSTRUCTOR)
6921 error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
6922 return NULL_TREE;
6925 if (TREE_CODE (init) == TREE_LIST)
6926 init = build_x_compound_expr_from_list (init, "initializer");
6928 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
6929 init = convert_from_reference (init);
6931 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
6932 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6933 /* Note: default conversion is only called in very special cases. */
6934 init = decay_conversion (init);
6936 /* Convert INIT to the reference type TYPE. This may involve the
6937 creation of a temporary, whose lifetime must be the same as that
6938 of the reference. If so, a DECL_STMT for the temporary will be
6939 added just after the DECL_STMT for DECL. That's why we don't set
6940 DECL_INITIAL for local references (instead assigning to them
6941 explicitly); we need to allow the temporary to be initialized
6942 first. */
6943 tmp = initialize_reference (type, init, decl, cleanup);
6945 if (tmp == error_mark_node)
6946 return NULL_TREE;
6947 else if (tmp == NULL_TREE)
6949 error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
6950 return NULL_TREE;
6953 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
6954 return tmp;
6956 DECL_INITIAL (decl) = tmp;
6958 return NULL_TREE;
6961 /* When parsing `int a[] = {1, 2};' we don't know the size of the
6962 array until we finish parsing the initializer. If that's the
6963 situation we're in, update DECL accordingly. */
6965 static void
6966 maybe_deduce_size_from_array_init (tree decl, tree init)
6968 tree type = TREE_TYPE (decl);
6970 if (TREE_CODE (type) == ARRAY_TYPE
6971 && TYPE_DOMAIN (type) == NULL_TREE
6972 && TREE_CODE (decl) != TYPE_DECL)
6974 /* do_default is really a C-ism to deal with tentative definitions.
6975 But let's leave it here to ease the eventual merge. */
6976 int do_default = !DECL_EXTERNAL (decl);
6977 tree initializer = init ? init : DECL_INITIAL (decl);
6978 int failure = complete_array_type (type, initializer, do_default);
6980 if (failure == 1)
6981 error ("initializer fails to determine size of `%D'", decl);
6983 if (failure == 2)
6985 if (do_default)
6986 error ("array size missing in `%D'", decl);
6987 /* If a `static' var's size isn't known, make it extern as
6988 well as static, so it does not get allocated. If it's not
6989 `static', then don't mark it extern; finish_incomplete_decl
6990 will give it a default size and it will get allocated. */
6991 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6992 DECL_EXTERNAL (decl) = 1;
6995 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
6996 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
6997 integer_zero_node))
6998 error ("zero-size array `%D'", decl);
7000 layout_decl (decl, 0);
7004 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7005 any appropriate error messages regarding the layout. */
7007 static void
7008 layout_var_decl (tree decl)
7010 tree type = TREE_TYPE (decl);
7011 #if 0
7012 tree ttype = target_type (type);
7013 #endif
7015 /* If we haven't already layed out this declaration, do so now.
7016 Note that we must not call complete type for an external object
7017 because it's type might involve templates that we are not
7018 supposed to instantiate yet. (And it's perfectly valid to say
7019 `extern X x' for some incomplete type `X'.) */
7020 if (!DECL_EXTERNAL (decl))
7021 complete_type (type);
7022 if (!DECL_SIZE (decl)
7023 && TREE_TYPE (decl) != error_mark_node
7024 && (COMPLETE_TYPE_P (type)
7025 || (TREE_CODE (type) == ARRAY_TYPE
7026 && !TYPE_DOMAIN (type)
7027 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7028 layout_decl (decl, 0);
7030 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7032 /* An automatic variable with an incomplete type: that is an error.
7033 Don't talk about array types here, since we took care of that
7034 message in grokdeclarator. */
7035 error ("storage size of `%D' isn't known", decl);
7036 TREE_TYPE (decl) = error_mark_node;
7038 #if 0
7039 /* Keep this code around in case we later want to control debug info
7040 based on whether a type is "used". (jason 1999-11-11) */
7042 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7043 /* Let debugger know it should output info for this type. */
7044 note_debug_info_needed (ttype);
7046 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7047 note_debug_info_needed (DECL_CONTEXT (decl));
7048 #endif
7050 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7051 && DECL_SIZE (decl) != NULL_TREE
7052 && ! TREE_CONSTANT (DECL_SIZE (decl)))
7054 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7055 constant_expression_warning (DECL_SIZE (decl));
7056 else
7057 error ("storage size of `%D' isn't constant", decl);
7060 if (TREE_STATIC (decl)
7061 && !DECL_ARTIFICIAL (decl)
7062 && current_function_decl
7063 && DECL_CONTEXT (decl) == current_function_decl)
7064 push_local_name (decl);
7067 /* If a local static variable is declared in an inline function, or if
7068 we have a weak definition, we must endeavor to create only one
7069 instance of the variable at link-time. */
7071 static void
7072 maybe_commonize_var (tree decl)
7074 /* Static data in a function with comdat linkage also has comdat
7075 linkage. */
7076 if (TREE_STATIC (decl)
7077 /* Don't mess with __FUNCTION__. */
7078 && ! DECL_ARTIFICIAL (decl)
7079 && DECL_FUNCTION_SCOPE_P (decl)
7080 /* Unfortunately, import_export_decl has not always been called
7081 before the function is processed, so we cannot simply check
7082 DECL_COMDAT. */
7083 && (DECL_COMDAT (DECL_CONTEXT (decl))
7084 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
7085 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
7086 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
7088 if (flag_weak)
7090 /* With weak symbols, we simply make the variable COMDAT;
7091 that will cause copies in multiple translations units to
7092 be merged. */
7093 comdat_linkage (decl);
7095 else
7097 if (DECL_INITIAL (decl) == NULL_TREE
7098 || DECL_INITIAL (decl) == error_mark_node)
7100 /* Without weak symbols, we can use COMMON to merge
7101 uninitialized variables. */
7102 TREE_PUBLIC (decl) = 1;
7103 DECL_COMMON (decl) = 1;
7105 else
7107 /* While for initialized variables, we must use internal
7108 linkage -- which means that multiple copies will not
7109 be merged. */
7110 TREE_PUBLIC (decl) = 0;
7111 DECL_COMMON (decl) = 0;
7112 cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7113 warning ("%J you can work around this by removing the initializer",
7114 decl);
7118 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7119 /* Set it up again; we might have set DECL_INITIAL since the last
7120 time. */
7121 comdat_linkage (decl);
7124 /* Issue an error message if DECL is an uninitialized const variable. */
7126 static void
7127 check_for_uninitialized_const_var (tree decl)
7129 tree type = TREE_TYPE (decl);
7131 /* ``Unless explicitly declared extern, a const object does not have
7132 external linkage and must be initialized. ($8.4; $12.1)'' ARM
7133 7.1.6 */
7134 if (TREE_CODE (decl) == VAR_DECL
7135 && TREE_CODE (type) != REFERENCE_TYPE
7136 && CP_TYPE_CONST_P (type)
7137 && !TYPE_NEEDS_CONSTRUCTING (type)
7138 && !DECL_INITIAL (decl))
7139 error ("uninitialized const `%D'", decl);
7142 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
7143 returned is the next FIELD_DECL (possibly FIELD itself) that can be
7144 initialized. If there are no more such fields, the return value
7145 will be NULL. */
7147 static tree
7148 next_initializable_field (tree field)
7150 while (field
7151 && (TREE_CODE (field) != FIELD_DECL
7152 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7153 || DECL_ARTIFICIAL (field)))
7154 field = TREE_CHAIN (field);
7156 return field;
7159 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7160 brace-enclosed aggregate initializer.
7162 *INITP is one of a list of initializers describing a brace-enclosed
7163 initializer for an entity of the indicated aggregate TYPE. It may
7164 not presently match the shape of the TYPE; for example:
7166 struct S { int a; int b; };
7167 struct S a[] = { 1, 2, 3, 4 };
7169 Here *INITP will point to TREE_LIST of four elements, rather than a
7170 list of two elements, each itself a list of two elements. This
7171 routine transforms INIT from the former form into the latter. The
7172 revised initializer is returned. */
7174 static tree
7175 reshape_init (tree type, tree *initp)
7177 tree inits;
7178 tree old_init;
7179 tree old_init_value;
7180 tree new_init;
7181 bool brace_enclosed_p;
7183 old_init = *initp;
7184 old_init_value = (TREE_CODE (*initp) == TREE_LIST
7185 ? TREE_VALUE (*initp) : old_init);
7187 my_friendly_assert (old_init_value, 20030723);
7189 /* If the initializer is brace-enclosed, pull initializers from the
7190 enclosed elements. Advance past the brace-enclosed initializer
7191 now. */
7192 if (TREE_CODE (old_init_value) == CONSTRUCTOR
7193 && TREE_TYPE (old_init_value) == NULL_TREE
7194 && TREE_HAS_CONSTRUCTOR (old_init_value))
7196 *initp = TREE_CHAIN (old_init);
7197 TREE_CHAIN (old_init) = NULL_TREE;
7198 inits = CONSTRUCTOR_ELTS (old_init_value);
7199 initp = &inits;
7200 brace_enclosed_p = true;
7202 else
7204 inits = NULL_TREE;
7205 brace_enclosed_p = false;
7208 /* A non-aggregate type is always initialized with a single
7209 initializer. */
7210 if (!CP_AGGREGATE_TYPE_P (type))
7212 *initp = TREE_CHAIN (old_init);
7213 TREE_CHAIN (old_init) = NULL_TREE;
7214 /* It is invalid to initialize a non-aggregate type with a
7215 brace-enclosed initializer. */
7216 if (brace_enclosed_p)
7218 error ("brace-enclosed initializer used to initialize `%T'",
7219 type);
7220 if (TREE_CODE (old_init) == TREE_LIST)
7221 TREE_VALUE (old_init) = error_mark_node;
7222 else
7223 old_init = error_mark_node;
7226 return old_init;
7229 /* [dcl.init.aggr]
7231 All implicit type conversions (clause _conv_) are considered when
7232 initializing the aggregate member with an initializer from an
7233 initializer-list. If the initializer can initialize a member,
7234 the member is initialized. Otherwise, if the member is itself a
7235 non-empty subaggregate, brace elision is assumed and the
7236 initializer is considered for the initialization of the first
7237 member of the subaggregate. */
7238 if (!brace_enclosed_p
7239 && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7241 *initp = TREE_CHAIN (old_init);
7242 TREE_CHAIN (old_init) = NULL_TREE;
7243 return old_init;
7246 if (TREE_CODE (old_init_value) == STRING_CST
7247 && TREE_CODE (type) == ARRAY_TYPE
7248 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7250 /* [dcl.init.string]
7252 A char array (whether plain char, signed char, or unsigned char)
7253 can be initialized by a string-literal (optionally enclosed in
7254 braces); a wchar_t array can be initialized by a wide
7255 string-literal (optionally enclosed in braces). */
7256 new_init = old_init;
7257 /* Move past the initializer. */
7258 *initp = TREE_CHAIN (old_init);
7259 TREE_CHAIN (old_init) = NULL_TREE;
7261 else
7263 /* Build a CONSTRUCTOR to hold the contents of the aggregate. */
7264 new_init = build_constructor (type, NULL_TREE);
7265 TREE_HAS_CONSTRUCTOR (new_init) = 1;
7267 if (CLASS_TYPE_P (type))
7269 tree field;
7271 field = next_initializable_field (TYPE_FIELDS (type));
7273 if (!field)
7275 /* [dcl.init.aggr]
7277 An initializer for an aggregate member that is an
7278 empty class shall have the form of an empty
7279 initializer-list {}. */
7280 if (!brace_enclosed_p)
7281 error ("initializer for `%T' must be brace-enclosed",
7282 type);
7284 else
7286 /* Loop through the initializable fields, gathering
7287 initializers. */
7288 while (*initp)
7290 tree field_init;
7292 /* Handle designated initializers, as an extension. */
7293 if (TREE_PURPOSE (*initp))
7295 if (pedantic)
7296 pedwarn ("ISO C++ does not allow designated initializers");
7297 field = lookup_field_1 (type, TREE_PURPOSE (*initp),
7298 /*want_type=*/false);
7299 if (!field || TREE_CODE (field) != FIELD_DECL)
7300 error ("`%T' has no non-static data member named `%D'",
7301 type, TREE_PURPOSE (*initp));
7303 if (!field)
7304 break;
7306 field_init = reshape_init (TREE_TYPE (field), initp);
7307 TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7308 CONSTRUCTOR_ELTS (new_init) = field_init;
7309 /* [dcl.init.aggr]
7311 When a union is initialized with a brace-enclosed
7312 initializer, the braces shall only contain an
7313 initializer for the first member of the union. */
7314 if (TREE_CODE (type) == UNION_TYPE)
7315 break;
7316 field = next_initializable_field (TREE_CHAIN (field));
7320 else if (TREE_CODE (type) == ARRAY_TYPE)
7322 tree index;
7323 tree max_index;
7325 /* If the bound of the array is known, take no more initializers
7326 than are allowed. */
7327 max_index = (TYPE_DOMAIN (type)
7328 ? array_type_nelts (type) : NULL_TREE);
7329 /* Loop through the array elements, gathering initializers. */
7330 for (index = size_zero_node;
7331 *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7332 index = size_binop (PLUS_EXPR, index, size_one_node))
7334 tree element_init;
7336 element_init = reshape_init (TREE_TYPE (type), initp);
7337 TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7338 CONSTRUCTOR_ELTS (new_init) = element_init;
7339 if (TREE_PURPOSE (element_init))
7340 index = TREE_PURPOSE (element_init);
7343 else
7344 abort ();
7346 /* The initializers were placed in reverse order in the
7347 CONSTRUCTOR. */
7348 CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7350 if (TREE_CODE (old_init) == TREE_LIST)
7351 new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7354 /* If this was a brace-enclosed initializer and all of the
7355 initializers were not used up, there is a problem. */
7356 if (brace_enclosed_p && *initp)
7357 error ("too many initializers for `%T'", type);
7359 return new_init;
7362 /* Verify INIT (the initializer for DECL), and record the
7363 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7364 grok_reference_init.
7366 If the return value is non-NULL, it is an expression that must be
7367 evaluated dynamically to initialize DECL. */
7369 static tree
7370 check_initializer (tree decl, tree init, int flags, tree *cleanup)
7372 tree type = TREE_TYPE (decl);
7374 /* If `start_decl' didn't like having an initialization, ignore it now. */
7375 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7376 init = NULL_TREE;
7378 /* If an initializer is present, DECL_INITIAL has been
7379 error_mark_node, to indicate that an as-of-yet unevaluated
7380 initialization will occur. From now on, DECL_INITIAL reflects
7381 the static initialization -- if any -- of DECL. */
7382 DECL_INITIAL (decl) = NULL_TREE;
7384 /* Things that are going to be initialized need to have complete
7385 type. */
7386 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7388 if (type == error_mark_node)
7389 /* We will have already complained. */
7390 init = NULL_TREE;
7391 else if (init && COMPLETE_TYPE_P (type)
7392 && !TREE_CONSTANT (TYPE_SIZE (type)))
7394 error ("variable-sized object `%D' may not be initialized", decl);
7395 init = NULL_TREE;
7397 else if (TREE_CODE (type) == ARRAY_TYPE
7398 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7400 error ("elements of array `%#D' have incomplete type", decl);
7401 init = NULL_TREE;
7403 else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7405 error ("`%D' has incomplete type", decl);
7406 TREE_TYPE (decl) = error_mark_node;
7407 init = NULL_TREE;
7410 if (TREE_CODE (decl) == CONST_DECL)
7412 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7414 DECL_INITIAL (decl) = init;
7416 my_friendly_assert (init != NULL_TREE, 149);
7417 init = NULL_TREE;
7419 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7420 init = grok_reference_init (decl, type, init, cleanup);
7421 else if (init)
7423 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7425 /* [dcl.init] paragraph 13,
7426 If T is a scalar type, then a declaration of the form
7427 T x = { a };
7428 is equivalent to
7429 T x = a;
7431 reshape_init will complain about the extra braces,
7432 and doesn't do anything useful in the case where TYPE is
7433 scalar, so just don't call it. */
7434 if (CP_AGGREGATE_TYPE_P (type))
7435 init = reshape_init (type, &init);
7437 if ((*targetm.vector_opaque_p) (type))
7439 error ("opaque vector types cannot be initialized");
7440 init = error_mark_node;
7444 /* If DECL has an array type without a specific bound, deduce the
7445 array size from the initializer. */
7446 maybe_deduce_size_from_array_init (decl, init);
7447 type = TREE_TYPE (decl);
7448 if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7449 TREE_TYPE (init) = type;
7451 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7453 if (TREE_CODE (type) == ARRAY_TYPE)
7454 goto initialize_aggr;
7455 else if (TREE_CODE (init) == CONSTRUCTOR
7456 && TREE_HAS_CONSTRUCTOR (init))
7458 if (TYPE_NON_AGGREGATE_CLASS (type))
7460 error ("`%D' must be initialized by constructor, not by `{...}'",
7461 decl);
7462 init = error_mark_node;
7464 else
7465 goto dont_use_constructor;
7467 else
7469 int saved_stmts_are_full_exprs_p;
7471 initialize_aggr:
7472 saved_stmts_are_full_exprs_p = 0;
7473 if (building_stmt_tree ())
7475 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7476 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7478 init = build_aggr_init (decl, init, flags);
7479 if (building_stmt_tree ())
7480 current_stmt_tree ()->stmts_are_full_exprs_p =
7481 saved_stmts_are_full_exprs_p;
7482 return init;
7485 else
7487 dont_use_constructor:
7488 if (TREE_CODE (init) != TREE_VEC)
7489 init = store_init_value (decl, init);
7492 else if (DECL_EXTERNAL (decl))
7494 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7495 goto initialize_aggr;
7496 else if (IS_AGGR_TYPE (type))
7498 tree core_type = strip_array_types (type);
7500 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7501 error ("structure `%D' with uninitialized const members", decl);
7502 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7503 error ("structure `%D' with uninitialized reference members",
7504 decl);
7506 check_for_uninitialized_const_var (decl);
7508 else
7509 check_for_uninitialized_const_var (decl);
7511 if (init && init != error_mark_node)
7512 init = build (INIT_EXPR, type, decl, init);
7514 return init;
7517 /* If DECL is not a local variable, give it RTL. */
7519 static void
7520 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7522 int toplev = toplevel_bindings_p ();
7523 int defer_p;
7525 /* Handle non-variables up front. */
7526 if (TREE_CODE (decl) != VAR_DECL)
7528 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7529 return;
7532 /* If we see a class member here, it should be a static data
7533 member. */
7534 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7536 my_friendly_assert (TREE_STATIC (decl), 19990828);
7537 /* An in-class declaration of a static data member should be
7538 external; it is only a declaration, and not a definition. */
7539 if (init == NULL_TREE)
7540 my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7543 /* Set the DECL_ASSEMBLER_NAME for the variable. */
7544 if (asmspec)
7546 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7547 /* The `register' keyword, when used together with an
7548 asm-specification, indicates that the variable should be
7549 placed in a particular register. */
7550 if (DECL_REGISTER (decl))
7551 DECL_C_HARD_REGISTER (decl) = 1;
7554 /* We don't create any RTL for local variables. */
7555 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7556 return;
7558 /* We defer emission of local statics until the corresponding
7559 DECL_STMT is expanded. */
7560 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7562 /* We try to defer namespace-scope static constants so that they are
7563 not emitted into the object file unnecessarily. */
7564 if (!DECL_VIRTUAL_P (decl)
7565 && TREE_READONLY (decl)
7566 && DECL_INITIAL (decl) != NULL_TREE
7567 && DECL_INITIAL (decl) != error_mark_node
7568 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7569 && toplev
7570 && !TREE_PUBLIC (decl))
7572 /* Fool with the linkage of static consts according to #pragma
7573 interface. */
7574 if (!interface_unknown && !TREE_PUBLIC (decl))
7576 TREE_PUBLIC (decl) = 1;
7577 DECL_EXTERNAL (decl) = interface_only;
7580 defer_p = 1;
7582 /* Likewise for template instantiations. */
7583 else if (DECL_COMDAT (decl))
7584 defer_p = 1;
7586 /* If we're deferring the variable, we only need to make RTL if
7587 there's an ASMSPEC. Otherwise, we'll lazily create it later when
7588 we need it. (There's no way to lazily create RTL for things that
7589 have assembly specs because the information about the specifier
7590 isn't stored in the tree, yet) */
7591 if (defer_p && asmspec)
7592 make_decl_rtl (decl, asmspec);
7593 /* If we're not deferring, go ahead and assemble the variable. */
7594 else if (!defer_p)
7595 rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7598 /* The old ARM scoping rules injected variables declared in the
7599 initialization statement of a for-statement into the surrounding
7600 scope. We support this usage, in order to be backward-compatible.
7601 DECL is a just-declared VAR_DECL; if necessary inject its
7602 declaration into the surrounding scope. */
7604 void
7605 maybe_inject_for_scope_var (tree decl)
7607 timevar_push (TV_NAME_LOOKUP);
7608 if (!DECL_NAME (decl))
7610 timevar_pop (TV_NAME_LOOKUP);
7611 return;
7614 /* Declarations of __FUNCTION__ and its ilk appear magically when
7615 the variable is first used. If that happens to be inside a
7616 for-loop, we don't want to do anything special. */
7617 if (DECL_PRETTY_FUNCTION_P (decl))
7619 timevar_pop (TV_NAME_LOOKUP);
7620 return;
7623 if (current_binding_level->kind == sk_for)
7625 struct cp_binding_level *outer
7626 = current_binding_level->level_chain;
7628 /* Check to see if the same name is already bound at the outer
7629 level, either because it was directly declared, or because a
7630 dead for-decl got preserved. In either case, the code would
7631 not have been valid under the ARM scope rules, so clear
7632 is_for_scope for the current_binding_level.
7634 Otherwise, we need to preserve the temp slot for decl to last
7635 into the outer binding level. */
7637 cxx_binding *outer_binding
7638 = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
7640 if (outer_binding && outer_binding->scope == outer
7641 && (TREE_CODE (outer_binding->value) == VAR_DECL)
7642 && DECL_DEAD_FOR_LOCAL (outer_binding->value))
7644 outer_binding->value = DECL_SHADOWED_FOR_VAR (outer_binding->value);
7645 current_binding_level->kind = sk_block;
7648 timevar_pop (TV_NAME_LOOKUP);
7651 /* Generate code to initialize DECL (a local variable). */
7653 static void
7654 initialize_local_var (tree decl, tree init)
7656 tree type = TREE_TYPE (decl);
7657 tree cleanup;
7659 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
7660 || TREE_CODE (decl) == RESULT_DECL,
7661 20021010);
7662 my_friendly_assert (!TREE_STATIC (decl), 20021010);
7664 if (DECL_SIZE (decl) == NULL_TREE)
7666 /* If we used it already as memory, it must stay in memory. */
7667 DECL_INITIAL (decl) = NULL_TREE;
7668 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7671 if (DECL_SIZE (decl) && type != error_mark_node)
7673 int already_used;
7675 /* Compute and store the initial value. */
7676 already_used = TREE_USED (decl) || TREE_USED (type);
7678 /* Perform the initialization. */
7679 if (init)
7681 int saved_stmts_are_full_exprs_p;
7683 my_friendly_assert (building_stmt_tree (), 20000906);
7684 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7685 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7686 finish_expr_stmt (init);
7687 current_stmt_tree ()->stmts_are_full_exprs_p =
7688 saved_stmts_are_full_exprs_p;
7691 /* Set this to 0 so we can tell whether an aggregate which was
7692 initialized was ever used. Don't do this if it has a
7693 destructor, so we don't complain about the 'resource
7694 allocation is initialization' idiom. Now set
7695 attribute((unused)) on types so decls of that type will be
7696 marked used. (see TREE_USED, above.) */
7697 if (TYPE_NEEDS_CONSTRUCTING (type)
7698 && ! already_used
7699 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7700 && DECL_NAME (decl))
7701 TREE_USED (decl) = 0;
7702 else if (already_used)
7703 TREE_USED (decl) = 1;
7706 /* Generate a cleanup, if necessary. */
7707 cleanup = cxx_maybe_build_cleanup (decl);
7708 if (DECL_SIZE (decl) && cleanup)
7709 finish_decl_cleanup (decl, cleanup);
7712 /* Finish processing of a declaration;
7713 install its line number and initial value.
7714 If the length of an array type is not known before,
7715 it must be determined now, from the initial value, or it is an error.
7717 INIT holds the value of an initializer that should be allowed to escape
7718 the normal rules.
7720 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7721 if the (init) syntax was used. */
7723 void
7724 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
7726 tree type;
7727 tree ttype = NULL_TREE;
7728 tree cleanup;
7729 const char *asmspec = NULL;
7730 int was_readonly = 0;
7732 if (! decl)
7734 if (init)
7735 error ("assignment (not initialization) in declaration");
7736 return;
7739 my_friendly_assert (TREE_CODE (decl) != RESULT_DECL, 20030619);
7741 /* Assume no cleanup is required. */
7742 cleanup = NULL_TREE;
7744 /* If a name was specified, get the string. */
7745 if (global_scope_p (current_binding_level))
7746 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
7747 if (asmspec_tree)
7748 asmspec = TREE_STRING_POINTER (asmspec_tree);
7750 if (init && TREE_CODE (init) == NAMESPACE_DECL)
7752 error ("cannot initialize `%D' to namespace `%D'",
7753 decl, init);
7754 init = NULL_TREE;
7757 if (current_class_type
7758 && CP_DECL_CONTEXT (decl) == current_class_type
7759 && TYPE_BEING_DEFINED (current_class_type)
7760 && (DECL_INITIAL (decl) || init))
7761 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
7763 if (TREE_CODE (decl) == VAR_DECL
7764 && DECL_CONTEXT (decl)
7765 && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
7766 && DECL_CONTEXT (decl) != current_namespace
7767 && init)
7769 /* Leave the namespace of the object. */
7770 pop_decl_namespace ();
7773 type = TREE_TYPE (decl);
7775 if (type == error_mark_node)
7776 goto finish_end0;
7778 if (TYPE_HAS_MUTABLE_P (type))
7779 TREE_READONLY (decl) = 0;
7781 if (processing_template_decl)
7783 /* Add this declaration to the statement-tree. */
7784 if (at_function_scope_p ())
7785 add_decl_stmt (decl);
7787 if (init && DECL_INITIAL (decl))
7788 DECL_INITIAL (decl) = init;
7789 goto finish_end0;
7792 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7793 my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
7795 /* Take care of TYPE_DECLs up front. */
7796 if (TREE_CODE (decl) == TYPE_DECL)
7798 if (type != error_mark_node
7799 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
7801 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
7802 warning ("shadowing previous type declaration of `%#D'", decl);
7803 set_identifier_type_value (DECL_NAME (decl), decl);
7806 /* If we have installed this as the canonical typedef for this
7807 type, and that type has not been defined yet, delay emitting
7808 the debug information for it, as we will emit it later. */
7809 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
7810 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
7811 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7813 rest_of_decl_compilation (decl, NULL,
7814 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
7815 goto finish_end;
7818 if (TREE_CODE (decl) != FUNCTION_DECL)
7819 ttype = target_type (type);
7821 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
7822 && (TYPE_NEEDS_CONSTRUCTING (type)
7823 || TREE_CODE (type) == REFERENCE_TYPE))
7825 /* Currently, GNU C++ puts constants in text space, making them
7826 impossible to initialize. In the future, one would hope for
7827 an operating system which understood the difference between
7828 initialization and the running of a program. */
7829 was_readonly = 1;
7830 TREE_READONLY (decl) = 0;
7833 if (TREE_CODE (decl) == FIELD_DECL && asmspec)
7835 /* This must override the asm specifier which was placed by
7836 grokclassfn. Lay this out fresh. */
7837 SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
7838 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7839 make_decl_rtl (decl, asmspec);
7841 else if (TREE_CODE (decl) == VAR_DECL)
7843 /* Only PODs can have thread-local storage. Other types may require
7844 various kinds of non-trivial initialization. */
7845 if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
7846 error ("`%D' cannot be thread-local because it has non-POD type `%T'",
7847 decl, TREE_TYPE (decl));
7848 /* Convert the initializer to the type of DECL, if we have not
7849 already initialized DECL. */
7850 if (!DECL_INITIALIZED_P (decl)
7851 /* If !DECL_EXTERNAL then DECL is being defined. In the
7852 case of a static data member initialized inside the
7853 class-specifier, there can be an initializer even if DECL
7854 is *not* defined. */
7855 && (!DECL_EXTERNAL (decl) || init))
7857 init = check_initializer (decl, init, flags, &cleanup);
7858 /* Thread-local storage cannot be dynamically initialized. */
7859 if (DECL_THREAD_LOCAL (decl) && init)
7861 error ("`%D' is thread-local and so cannot be dynamically "
7862 "initialized", decl);
7863 init = NULL_TREE;
7865 /* Handle:
7867 [dcl.init]
7869 The memory occupied by any object of static storage
7870 duration is zero-initialized at program startup before
7871 any other initialization takes place.
7873 We cannot create an appropriate initializer until after
7874 the type of DECL is finalized. If DECL_INITIAL is set,
7875 then the DECL is statically initialized, and any
7876 necessary zero-initialization has already been performed. */
7877 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7878 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7879 /*nelts=*/NULL_TREE,
7880 /*static_storage_p=*/true);
7881 /* Remember that the initialization for this variable has
7882 taken place. */
7883 DECL_INITIALIZED_P (decl) = 1;
7885 /* If the variable has an array type, lay out the type, even if
7886 there is no initializer. It is valid to index through the
7887 array, and we must get TYPE_ALIGN set correctly on the array
7888 type. */
7889 else if (TREE_CODE (type) == ARRAY_TYPE)
7890 layout_type (type);
7893 /* Add this declaration to the statement-tree. This needs to happen
7894 after the call to check_initializer so that the DECL_STMT for a
7895 reference temp is added before the DECL_STMT for the reference itself. */
7896 if (at_function_scope_p ())
7897 add_decl_stmt (decl);
7899 if (TREE_CODE (decl) == VAR_DECL)
7900 layout_var_decl (decl);
7902 /* Output the assembler code and/or RTL code for variables and functions,
7903 unless the type is an undefined structure or union.
7904 If not, it will get done when the type is completed. */
7905 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
7907 if (TREE_CODE (decl) == VAR_DECL)
7908 maybe_commonize_var (decl);
7910 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7912 if (TREE_CODE (type) == FUNCTION_TYPE
7913 || TREE_CODE (type) == METHOD_TYPE)
7914 abstract_virtuals_error (decl,
7915 strip_array_types (TREE_TYPE (type)));
7916 else
7917 abstract_virtuals_error (decl, strip_array_types (type));
7919 if (TREE_CODE (decl) == FUNCTION_DECL
7920 || TREE_TYPE (decl) == error_mark_node)
7921 /* No initialization required. */
7923 else if (DECL_EXTERNAL (decl)
7924 && ! (DECL_LANG_SPECIFIC (decl)
7925 && DECL_NOT_REALLY_EXTERN (decl)))
7927 if (init)
7928 DECL_INITIAL (decl) = init;
7930 else
7932 /* A variable definition. */
7933 if (DECL_FUNCTION_SCOPE_P (decl))
7935 /* This is a local declaration. */
7936 maybe_inject_for_scope_var (decl);
7937 /* Initialize the local variable. */
7938 if (processing_template_decl)
7940 if (init || DECL_INITIAL (decl) == error_mark_node)
7941 DECL_INITIAL (decl) = init;
7943 else if (!TREE_STATIC (decl))
7944 initialize_local_var (decl, init);
7947 if (TREE_STATIC (decl))
7948 expand_static_init (decl, init);
7950 finish_end0:
7952 /* Undo call to `pushclass' that was done in `start_decl'
7953 due to initialization of qualified member variable.
7954 I.e., Foo::x = 10; */
7956 tree context = CP_DECL_CONTEXT (decl);
7957 if (context
7958 && TYPE_P (context)
7959 && (TREE_CODE (decl) == VAR_DECL
7960 /* We also have a pushclass done that we need to undo here
7961 if we're at top level and declare a method. */
7962 || TREE_CODE (decl) == FUNCTION_DECL)
7963 /* If size hasn't been set, we're still defining it,
7964 and therefore inside the class body; don't pop
7965 the binding level.. */
7966 && COMPLETE_TYPE_P (context)
7967 && context == current_class_type)
7968 pop_nested_class ();
7972 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7973 reference, insert it in the statement-tree now. */
7974 if (cleanup)
7975 add_stmt (cleanup);
7977 finish_end:
7979 if (was_readonly)
7980 TREE_READONLY (decl) = 1;
7982 /* If this was marked 'used', be sure it will be output. */
7983 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
7984 mark_referenced (DECL_ASSEMBLER_NAME (decl));
7987 /* This is here for a midend callback from c-common.c */
7989 void
7990 finish_decl (tree decl, tree init, tree asmspec_tree)
7992 cp_finish_decl (decl, init, asmspec_tree, 0);
7995 /* Returns a declaration for a VAR_DECL as if:
7997 extern "C" TYPE NAME;
7999 had been seen. Used to create compiler-generated global
8000 variables. */
8002 tree
8003 declare_global_var (tree name, tree type)
8005 tree decl;
8007 push_to_top_level ();
8008 decl = build_decl (VAR_DECL, name, type);
8009 TREE_PUBLIC (decl) = 1;
8010 DECL_EXTERNAL (decl) = 1;
8011 DECL_ARTIFICIAL (decl) = 1;
8012 pushdecl (decl);
8013 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8014 pop_from_top_level ();
8016 return decl;
8019 /* Returns a pointer to the `atexit' function. Note that if
8020 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8021 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8023 static tree
8024 get_atexit_node (void)
8026 tree atexit_fndecl;
8027 tree arg_types;
8028 tree fn_type;
8029 tree fn_ptr_type;
8030 const char *name;
8032 if (atexit_node)
8033 return atexit_node;
8035 if (flag_use_cxa_atexit)
8037 /* The declaration for `__cxa_atexit' is:
8039 int __cxa_atexit (void (*)(void *), void *, void *)
8041 We build up the argument types and then then function type
8042 itself. */
8044 /* First, build the pointer-to-function type for the first
8045 argument. */
8046 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8047 fn_type = build_function_type (void_type_node, arg_types);
8048 fn_ptr_type = build_pointer_type (fn_type);
8049 /* Then, build the rest of the argument types. */
8050 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8051 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8052 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8053 /* And the final __cxa_atexit type. */
8054 fn_type = build_function_type (integer_type_node, arg_types);
8055 fn_ptr_type = build_pointer_type (fn_type);
8056 name = "__cxa_atexit";
8058 else
8060 /* The declaration for `atexit' is:
8062 int atexit (void (*)());
8064 We build up the argument types and then then function type
8065 itself. */
8066 fn_type = build_function_type (void_type_node, void_list_node);
8067 fn_ptr_type = build_pointer_type (fn_type);
8068 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8069 /* Build the final atexit type. */
8070 fn_type = build_function_type (integer_type_node, arg_types);
8071 name = "atexit";
8074 /* Now, build the function declaration. */
8075 push_lang_context (lang_name_c);
8076 atexit_fndecl = build_library_fn_ptr (name, fn_type);
8077 mark_used (atexit_fndecl);
8078 pop_lang_context ();
8079 atexit_node = decay_conversion (atexit_fndecl);
8081 return atexit_node;
8084 /* Returns the __dso_handle VAR_DECL. */
8086 static tree
8087 get_dso_handle_node (void)
8089 if (dso_handle_node)
8090 return dso_handle_node;
8092 /* Declare the variable. */
8093 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8094 ptr_type_node);
8096 return dso_handle_node;
8099 /* Begin a new function with internal linkage whose job will be simply
8100 to destroy some particular variable. */
8102 static GTY(()) int start_cleanup_cnt;
8104 static tree
8105 start_cleanup_fn (void)
8107 int old_interface_only = interface_only;
8108 int old_interface_unknown = interface_unknown;
8109 char name[32];
8110 tree parmtypes;
8111 tree fntype;
8112 tree fndecl;
8114 push_to_top_level ();
8116 /* No need to mangle this. */
8117 push_lang_context (lang_name_c);
8119 interface_only = 0;
8120 interface_unknown = 1;
8122 /* Build the parameter-types. */
8123 parmtypes = void_list_node;
8124 /* Functions passed to __cxa_atexit take an additional parameter.
8125 We'll just ignore it. After we implement the new calling
8126 convention for destructors, we can eliminate the use of
8127 additional cleanup functions entirely in the -fnew-abi case. */
8128 if (flag_use_cxa_atexit)
8129 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8130 /* Build the function type itself. */
8131 fntype = build_function_type (void_type_node, parmtypes);
8132 /* Build the name of the function. */
8133 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
8134 /* Build the function declaration. */
8135 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8136 /* It's a function with internal linkage, generated by the
8137 compiler. */
8138 TREE_PUBLIC (fndecl) = 0;
8139 DECL_ARTIFICIAL (fndecl) = 1;
8140 /* Make the function `inline' so that it is only emitted if it is
8141 actually needed. It is unlikely that it will be inlined, since
8142 it is only called via a function pointer, but we avoid unnecessary
8143 emissions this way. */
8144 DECL_INLINE (fndecl) = 1;
8145 /* Build the parameter. */
8146 if (flag_use_cxa_atexit)
8148 tree parmdecl;
8150 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8151 DECL_CONTEXT (parmdecl) = fndecl;
8152 TREE_USED (parmdecl) = 1;
8153 DECL_ARGUMENTS (fndecl) = parmdecl;
8156 pushdecl (fndecl);
8157 start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8159 interface_unknown = old_interface_unknown;
8160 interface_only = old_interface_only;
8162 pop_lang_context ();
8164 return current_function_decl;
8167 /* Finish the cleanup function begun by start_cleanup_fn. */
8169 static void
8170 end_cleanup_fn (void)
8172 expand_or_defer_fn (finish_function (0));
8174 pop_from_top_level ();
8177 /* Generate code to handle the destruction of DECL, an object with
8178 static storage duration. */
8180 void
8181 register_dtor_fn (tree decl)
8183 tree cleanup;
8184 tree compound_stmt;
8185 tree args;
8186 tree fcall;
8188 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8189 return;
8191 /* Call build_cleanup before we enter the anonymous function so that
8192 any access checks will be done relative to the current scope,
8193 rather than the scope of the anonymous function. */
8194 build_cleanup (decl);
8196 /* Now start the function. */
8197 cleanup = start_cleanup_fn ();
8199 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8200 to the original function, rather than the anonymous one. That
8201 will make the back-end think that nested functions are in use,
8202 which causes confusion. */
8204 push_deferring_access_checks (dk_no_check);
8205 fcall = build_cleanup (decl);
8206 pop_deferring_access_checks ();
8208 /* Create the body of the anonymous function. */
8209 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
8210 finish_expr_stmt (fcall);
8211 finish_compound_stmt (compound_stmt);
8212 end_cleanup_fn ();
8214 /* Call atexit with the cleanup function. */
8215 cxx_mark_addressable (cleanup);
8216 mark_used (cleanup);
8217 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8218 if (flag_use_cxa_atexit)
8220 args = tree_cons (NULL_TREE,
8221 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8222 NULL_TREE);
8223 args = tree_cons (NULL_TREE, null_pointer_node, args);
8224 args = tree_cons (NULL_TREE, cleanup, args);
8226 else
8227 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8228 finish_expr_stmt (build_function_call (get_atexit_node (), args));
8231 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8232 is its initializer. Generate code to handle the construction
8233 and destruction of DECL. */
8235 static void
8236 expand_static_init (tree decl, tree init)
8238 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8239 my_friendly_assert (TREE_STATIC (decl), 20021010);
8241 /* Some variables require no initialization. */
8242 if (!init
8243 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8244 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8245 return;
8247 if (! toplevel_bindings_p ())
8249 /* Emit code to perform this initialization but once. */
8250 tree if_stmt;
8251 tree then_clause;
8252 tree assignment;
8253 tree guard;
8254 tree guard_init;
8256 /* Emit code to perform this initialization but once. This code
8257 looks like:
8259 static int guard = 0;
8260 if (!guard) {
8261 // Do initialization.
8262 guard = 1;
8263 // Register variable for destruction at end of program.
8266 Note that the `temp' variable is only set to 1 *after* the
8267 initialization is complete. This ensures that an exception,
8268 thrown during the construction, will cause the variable to
8269 reinitialized when we pass through this code again, as per:
8271 [stmt.dcl]
8273 If the initialization exits by throwing an exception, the
8274 initialization is not complete, so it will be tried again
8275 the next time control enters the declaration.
8277 In theory, this process should be thread-safe, too; multiple
8278 threads should not be able to initialize the variable more
8279 than once. We don't yet attempt to ensure thread-safety. */
8281 /* Create the guard variable. */
8282 guard = get_guard (decl);
8284 /* Begin the conditional initialization. */
8285 if_stmt = begin_if_stmt ();
8286 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8287 then_clause = begin_compound_stmt (/*has_no_scope=*/false);
8289 /* Do the initialization itself. */
8290 assignment = init ? init : NULL_TREE;
8292 /* Once the assignment is complete, set TEMP to 1. Since the
8293 construction of the static object is complete at this point,
8294 we want to make sure TEMP is set to 1 even if a temporary
8295 constructed during the initialization throws an exception
8296 when it is destroyed. So, we combine the initialization and
8297 the assignment to TEMP into a single expression, ensuring
8298 that when we call finish_expr_stmt the cleanups will not be
8299 run until after TEMP is set to 1. */
8300 guard_init = set_guard (guard);
8301 if (assignment)
8302 assignment = build_compound_expr (assignment, guard_init);
8303 else
8304 assignment = guard_init;
8305 finish_expr_stmt (assignment);
8307 /* Use atexit to register a function for destroying this static
8308 variable. */
8309 register_dtor_fn (decl);
8311 finish_compound_stmt (then_clause);
8312 finish_then_clause (if_stmt);
8313 finish_if_stmt ();
8315 else
8316 static_aggregates = tree_cons (init, decl, static_aggregates);
8319 /* Finish the declaration of a catch-parameter. */
8321 tree
8322 start_handler_parms (tree declspecs, tree declarator)
8324 tree decl;
8325 if (declspecs)
8327 decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8328 1, NULL);
8329 if (decl == NULL_TREE)
8330 error ("invalid catch parameter");
8332 else
8333 decl = NULL_TREE;
8335 return decl;
8339 /* Make TYPE a complete type based on INITIAL_VALUE.
8340 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8341 2 if there was no information (in which case assume 0 if DO_DEFAULT). */
8344 complete_array_type (tree type, tree initial_value, int do_default)
8346 register tree maxindex = NULL_TREE;
8347 int value = 0;
8349 if (initial_value)
8351 /* An array of character type can be initialized from a
8352 brace-enclosed string constant. */
8353 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8354 && TREE_CODE (initial_value) == CONSTRUCTOR
8355 && CONSTRUCTOR_ELTS (initial_value)
8356 && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8357 == STRING_CST)
8358 && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8359 initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8361 /* Note MAXINDEX is really the maximum index, one less than the
8362 size. */
8363 if (TREE_CODE (initial_value) == STRING_CST)
8365 int eltsize
8366 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8367 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8368 / eltsize) - 1, 0);
8370 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8372 tree elts = CONSTRUCTOR_ELTS (initial_value);
8374 maxindex = ssize_int (-1);
8375 for (; elts; elts = TREE_CHAIN (elts))
8377 if (TREE_PURPOSE (elts))
8378 maxindex = TREE_PURPOSE (elts);
8379 else
8380 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8382 maxindex = copy_node (maxindex);
8384 else
8386 /* Make an error message unless that happened already. */
8387 if (initial_value != error_mark_node)
8388 value = 1;
8389 else
8390 initial_value = NULL_TREE;
8392 /* Prevent further error messages. */
8393 maxindex = build_int_2 (0, 0);
8397 if (!maxindex)
8399 if (do_default)
8400 maxindex = build_int_2 (0, 0);
8401 value = 2;
8404 if (maxindex)
8406 tree itype;
8407 tree domain;
8409 domain = build_index_type (maxindex);
8410 TYPE_DOMAIN (type) = domain;
8412 if (! TREE_TYPE (maxindex))
8413 TREE_TYPE (maxindex) = domain;
8414 if (initial_value)
8415 itype = TREE_TYPE (initial_value);
8416 else
8417 itype = NULL;
8418 if (itype && !TYPE_DOMAIN (itype))
8419 TYPE_DOMAIN (itype) = domain;
8420 /* The type of the main variant should never be used for arrays
8421 of different sizes. It should only ever be completed with the
8422 size of the array. */
8423 if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8424 TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8427 /* Lay out the type now that we can get the real answer. */
8429 layout_type (type);
8431 return value;
8434 /* Return zero if something is declared to be a member of type
8435 CTYPE when in the context of CUR_TYPE. STRING is the error
8436 message to print in that case. Otherwise, quietly return 1. */
8438 static int
8439 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8441 if (ctype && ctype != cur_type)
8443 if (flags == DTOR_FLAG)
8444 error ("destructor for alien class `%T' cannot be a member",
8445 ctype);
8446 else
8447 error ("constructor for alien class `%T' cannot be a member",
8448 ctype);
8449 return 0;
8451 return 1;
8454 /* Subroutine of `grokdeclarator'. */
8456 /* Generate errors possibly applicable for a given set of specifiers.
8457 This is for ARM $7.1.2. */
8459 static void
8460 bad_specifiers (tree object,
8461 const char* type,
8462 int virtualp,
8463 int quals,
8464 int inlinep,
8465 int friendp,
8466 int raises)
8468 if (virtualp)
8469 error ("`%D' declared as a `virtual' %s", object, type);
8470 if (inlinep)
8471 error ("`%D' declared as an `inline' %s", object, type);
8472 if (quals)
8473 error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8474 object, type);
8475 if (friendp)
8476 cp_error_at ("`%D' declared as a friend", object);
8477 if (raises
8478 && (TREE_CODE (object) == TYPE_DECL
8479 || (!TYPE_PTRFN_P (TREE_TYPE (object))
8480 && !TYPE_REFFN_P (TREE_TYPE (object))
8481 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8482 cp_error_at ("`%D' declared with an exception specification", object);
8485 /* CTYPE is class type, or null if non-class.
8486 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8487 or METHOD_TYPE.
8488 DECLARATOR is the function's name.
8489 VIRTUALP is truthvalue of whether the function is virtual or not.
8490 FLAGS are to be passed through to `grokclassfn'.
8491 QUALS are qualifiers indicating whether the function is `const'
8492 or `volatile'.
8493 RAISES is a list of exceptions that this function can raise.
8494 CHECK is 1 if we must find this method in CTYPE, 0 if we should
8495 not look, and -1 if we should not call `grokclassfn' at all.
8497 Returns `NULL_TREE' if something goes wrong, after issuing
8498 applicable error messages. */
8500 static tree
8501 grokfndecl (tree ctype,
8502 tree type,
8503 tree declarator,
8504 tree orig_declarator,
8505 int virtualp,
8506 enum overload_flags flags,
8507 tree quals,
8508 tree raises,
8509 int check,
8510 int friendp,
8511 int publicp,
8512 int inlinep,
8513 int funcdef_flag,
8514 int template_count,
8515 tree in_namespace)
8517 tree decl;
8518 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8519 int has_default_arg = 0;
8520 tree t;
8522 if (raises)
8523 type = build_exception_variant (type, raises);
8525 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8526 /* Propagate volatile out from type to decl. */
8527 if (TYPE_VOLATILE (type))
8528 TREE_THIS_VOLATILE (decl) = 1;
8530 /* If this decl has namespace scope, set that up. */
8531 if (in_namespace)
8532 set_decl_namespace (decl, in_namespace, friendp);
8533 else if (!ctype)
8534 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8536 /* `main' and builtins have implicit 'C' linkage. */
8537 if ((MAIN_NAME_P (declarator)
8538 || (IDENTIFIER_LENGTH (declarator) > 10
8539 && IDENTIFIER_POINTER (declarator)[0] == '_'
8540 && IDENTIFIER_POINTER (declarator)[1] == '_'
8541 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8542 && current_lang_name == lang_name_cplusplus
8543 && ctype == NULL_TREE
8544 /* NULL_TREE means global namespace. */
8545 && DECL_CONTEXT (decl) == NULL_TREE)
8546 SET_DECL_LANGUAGE (decl, lang_c);
8548 /* Should probably propagate const out from type to decl I bet (mrs). */
8549 if (staticp)
8551 DECL_STATIC_FUNCTION_P (decl) = 1;
8552 DECL_CONTEXT (decl) = ctype;
8555 if (ctype)
8556 DECL_CONTEXT (decl) = ctype;
8558 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8560 if (processing_template_decl)
8561 error ("cannot declare `::main' to be a template");
8562 if (inlinep)
8563 error ("cannot declare `::main' to be inline");
8564 if (!publicp)
8565 error ("cannot declare `::main' to be static");
8566 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8567 integer_type_node))
8568 error ("`main' must return `int'");
8569 inlinep = 0;
8570 publicp = 1;
8573 /* Members of anonymous types and local classes have no linkage; make
8574 them internal. */
8575 /* FIXME what if it gets a name from typedef? */
8576 if (ctype && (TYPE_ANONYMOUS_P (ctype)
8577 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8578 publicp = 0;
8580 if (publicp)
8582 /* [basic.link]: A name with no linkage (notably, the name of a class
8583 or enumeration declared in a local scope) shall not be used to
8584 declare an entity with linkage.
8586 Only check this for public decls for now. See core 319, 389. */
8587 t = no_linkage_check (TREE_TYPE (decl));
8588 if (t)
8590 if (TYPE_ANONYMOUS_P (t))
8592 if (DECL_EXTERN_C_P (decl))
8593 /* Allow this; it's pretty common in C. */;
8594 else
8596 pedwarn ("non-local function `%#D' uses anonymous type",
8597 decl);
8598 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8599 cp_pedwarn_at ("\
8600 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8601 TYPE_NAME (t));
8604 else
8605 pedwarn ("non-local function `%#D' uses local type `%T'",
8606 decl, t);
8610 TREE_PUBLIC (decl) = publicp;
8611 if (! publicp)
8613 DECL_INTERFACE_KNOWN (decl) = 1;
8614 DECL_NOT_REALLY_EXTERN (decl) = 1;
8617 /* If the declaration was declared inline, mark it as such. */
8618 if (inlinep)
8619 DECL_DECLARED_INLINE_P (decl) = 1;
8620 /* We inline functions that are explicitly declared inline, or, when
8621 the user explicitly asks us to, all functions. */
8622 if (DECL_DECLARED_INLINE_P (decl)
8623 || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag))
8624 DECL_INLINE (decl) = 1;
8626 DECL_EXTERNAL (decl) = 1;
8627 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8629 error ("%smember function `%D' cannot have `%T' method qualifier",
8630 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8631 quals = NULL_TREE;
8634 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8635 grok_op_properties (decl, friendp, /*complain=*/true);
8637 if (ctype && decl_function_context (decl))
8638 DECL_NO_STATIC_CHAIN (decl) = 1;
8640 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8641 if (TREE_PURPOSE (t)
8642 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8644 has_default_arg = 1;
8645 break;
8648 if (friendp
8649 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8651 if (funcdef_flag)
8652 error
8653 ("defining explicit specialization `%D' in friend declaration",
8654 orig_declarator);
8655 else
8657 tree fns = TREE_OPERAND (orig_declarator, 0);
8658 tree args = TREE_OPERAND (orig_declarator, 1);
8660 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8662 /* Something like `template <class T> friend void f<T>()'. */
8663 error ("invalid use of template-id `%D' in declaration of primary template",
8664 orig_declarator);
8665 return NULL_TREE;
8669 /* A friend declaration of the form friend void f<>(). Record
8670 the information in the TEMPLATE_ID_EXPR. */
8671 SET_DECL_IMPLICIT_INSTANTIATION (decl);
8673 if (TREE_CODE (fns) == COMPONENT_REF)
8675 /* Due to bison parser ickiness, we will have already looked
8676 up an operator_name or PFUNCNAME within the current class
8677 (see template_id in parse.y). If the current class contains
8678 such a name, we'll get a COMPONENT_REF here. Undo that. */
8680 my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
8681 == current_class_type, 20001120);
8682 fns = TREE_OPERAND (fns, 1);
8684 my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
8685 || TREE_CODE (fns) == OVERLOAD, 20001120);
8686 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
8688 if (has_default_arg)
8690 error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8691 decl);
8692 return NULL_TREE;
8695 if (inlinep)
8697 error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8698 decl);
8699 return NULL_TREE;
8704 if (funcdef_flag)
8705 /* Make the init_value nonzero so pushdecl knows this is not
8706 tentative. error_mark_node is replaced later with the BLOCK. */
8707 DECL_INITIAL (decl) = error_mark_node;
8709 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8710 TREE_NOTHROW (decl) = 1;
8712 /* Caller will do the rest of this. */
8713 if (check < 0)
8714 return decl;
8716 if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
8717 DECL_CONSTRUCTOR_P (decl) = 1;
8719 /* Function gets the ugly name, field gets the nice one. This call
8720 may change the type of the function (because of default
8721 parameters)! */
8722 if (ctype != NULL_TREE)
8723 grokclassfn (ctype, decl, flags, quals);
8725 decl = check_explicit_specialization (orig_declarator, decl,
8726 template_count,
8727 2 * (funcdef_flag != 0) +
8728 4 * (friendp != 0));
8729 if (decl == error_mark_node)
8730 return NULL_TREE;
8732 if (ctype != NULL_TREE
8733 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
8734 && check)
8736 tree old_decl;
8738 old_decl = check_classfn (ctype, decl);
8740 if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
8741 /* Because grokfndecl is always supposed to return a
8742 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8743 here. We depend on our callers to figure out that its
8744 really a template that's being returned. */
8745 old_decl = DECL_TEMPLATE_RESULT (old_decl);
8747 if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
8748 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8750 /* Remove the `this' parm added by grokclassfn.
8751 XXX Isn't this done in start_function, too? */
8752 revert_static_member_fn (decl);
8753 last_function_parms = TREE_CHAIN (last_function_parms);
8755 if (old_decl && DECL_ARTIFICIAL (old_decl))
8756 error ("definition of implicitly-declared `%D'", old_decl);
8758 if (old_decl)
8760 bool ok;
8762 /* Since we've smashed OLD_DECL to its
8763 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8764 if (TREE_CODE (decl) == TEMPLATE_DECL)
8765 decl = DECL_TEMPLATE_RESULT (decl);
8767 /* Attempt to merge the declarations. This can fail, in
8768 the case of some invalid specialization declarations. */
8769 push_scope (ctype);
8770 ok = duplicate_decls (decl, old_decl);
8771 pop_scope (ctype);
8772 if (!ok)
8774 error ("no `%#D' member function declared in class `%T'",
8775 decl, ctype);
8776 return NULL_TREE;
8778 return old_decl;
8782 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8783 return NULL_TREE;
8785 if (ctype == NULL_TREE || check)
8786 return decl;
8788 if (virtualp)
8789 DECL_VIRTUAL_P (decl) = 1;
8791 return decl;
8794 /* Create a VAR_DECL named NAME with the indicated TYPE.
8796 If SCOPE is non-NULL, it is the class type or namespace containing
8797 the variable. If SCOPE is NULL, the variable should is created in
8798 the innermost enclosings scope. */
8800 static tree
8801 grokvardecl (tree type,
8802 tree name,
8803 RID_BIT_TYPE * specbits_in,
8804 int initialized,
8805 int constp,
8806 tree scope)
8808 tree decl;
8809 RID_BIT_TYPE specbits;
8811 my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE,
8812 20020808);
8814 specbits = *specbits_in;
8816 /* Compute the scope in which to place the variable. */
8817 if (!scope)
8819 /* An explicit "extern" specifier indicates a namespace-scope
8820 variable. */
8821 if (RIDBIT_SETP (RID_EXTERN, specbits))
8822 scope = current_namespace;
8823 else if (!at_function_scope_p ())
8825 scope = current_scope ();
8826 if (!scope)
8827 scope = current_namespace;
8831 if (scope
8832 && (/* If the variable is a namespace-scope variable declared in a
8833 template, we need DECL_LANG_SPECIFIC. */
8834 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
8835 /* Similarly for namespace-scope variables with language linkage
8836 other than C++. */
8837 || (TREE_CODE (scope) == NAMESPACE_DECL
8838 && current_lang_name != lang_name_cplusplus)
8839 /* Similarly for static data members. */
8840 || TYPE_P (scope)))
8841 decl = build_lang_decl (VAR_DECL, name, type);
8842 else
8843 decl = build_decl (VAR_DECL, name, type);
8845 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8846 set_decl_namespace (decl, scope, 0);
8847 else
8848 DECL_CONTEXT (decl) = scope;
8850 if (name && scope && current_lang_name != lang_name_c)
8851 /* We can't mangle lazily here because we don't have any
8852 way to recover whether or not a variable was `extern
8853 "C"' later. */
8854 mangle_decl (decl);
8856 if (RIDBIT_SETP (RID_EXTERN, specbits))
8858 DECL_THIS_EXTERN (decl) = 1;
8859 DECL_EXTERNAL (decl) = !initialized;
8862 /* In class context, static means one per class,
8863 public access, and static storage. */
8864 if (DECL_CLASS_SCOPE_P (decl))
8866 TREE_PUBLIC (decl) = 1;
8867 TREE_STATIC (decl) = 1;
8868 DECL_EXTERNAL (decl) = 0;
8870 /* At top level, either `static' or no s.c. makes a definition
8871 (perhaps tentative), and absence of `static' makes it public. */
8872 else if (toplevel_bindings_p ())
8874 TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
8875 && (DECL_THIS_EXTERN (decl) || ! constp));
8876 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8878 /* Not at top level, only `static' makes a static definition. */
8879 else
8881 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
8882 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8885 if (RIDBIT_SETP (RID_THREAD, specbits))
8887 if (targetm.have_tls)
8888 DECL_THREAD_LOCAL (decl) = 1;
8889 else
8890 /* A mere warning is sure to result in improper semantics
8891 at runtime. Don't bother to allow this to compile. */
8892 error ("thread-local storage not supported for this target");
8895 if (TREE_PUBLIC (decl))
8897 /* [basic.link]: A name with no linkage (notably, the name of a class
8898 or enumeration declared in a local scope) shall not be used to
8899 declare an entity with linkage.
8901 Only check this for public decls for now. */
8902 tree t = no_linkage_check (TREE_TYPE (decl));
8903 if (t)
8905 if (TYPE_ANONYMOUS_P (t))
8906 /* Ignore for now; `enum { foo } e' is pretty common. */;
8907 else
8908 pedwarn ("non-local variable `%#D' uses local type `%T'",
8909 decl, t);
8913 return decl;
8916 /* Create and return a canonical pointer to member function type, for
8917 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8919 tree
8920 build_ptrmemfunc_type (tree type)
8922 tree field, fields;
8923 tree t;
8924 tree unqualified_variant = NULL_TREE;
8926 if (type == error_mark_node)
8927 return type;
8929 /* If a canonical type already exists for this type, use it. We use
8930 this method instead of type_hash_canon, because it only does a
8931 simple equality check on the list of field members. */
8933 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8934 return t;
8936 /* Make sure that we always have the unqualified pointer-to-member
8937 type first. */
8938 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
8939 unqualified_variant
8940 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
8942 t = make_aggr_type (RECORD_TYPE);
8943 /* Let the front-end know this is a pointer to member function... */
8944 TYPE_PTRMEMFUNC_FLAG (t) = 1;
8945 /* ... and not really an aggregate. */
8946 SET_IS_AGGR_TYPE (t, 0);
8948 field = build_decl (FIELD_DECL, pfn_identifier, type);
8949 fields = field;
8951 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
8952 TREE_CHAIN (field) = fields;
8953 fields = field;
8955 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
8957 /* Zap out the name so that the back-end will give us the debugging
8958 information for this anonymous RECORD_TYPE. */
8959 TYPE_NAME (t) = NULL_TREE;
8961 /* If this is not the unqualified form of this pointer-to-member
8962 type, set the TYPE_MAIN_VARIANT for this type to be the
8963 unqualified type. Since they are actually RECORD_TYPEs that are
8964 not variants of each other, we must do this manually. */
8965 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
8967 t = build_qualified_type (t, cp_type_quals (type));
8968 TYPE_MAIN_VARIANT (t) = unqualified_variant;
8969 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
8970 TYPE_NEXT_VARIANT (unqualified_variant) = t;
8973 /* Cache this pointer-to-member type so that we can find it again
8974 later. */
8975 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8977 return t;
8980 /* Create and return a pointer to data member type. */
8982 tree
8983 build_ptrmem_type (tree class_type, tree member_type)
8985 if (TREE_CODE (member_type) == METHOD_TYPE)
8987 tree arg_types;
8989 arg_types = TYPE_ARG_TYPES (member_type);
8990 class_type = (cp_build_qualified_type
8991 (class_type,
8992 cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types)))));
8993 member_type
8994 = build_method_type_directly (class_type,
8995 TREE_TYPE (member_type),
8996 TREE_CHAIN (arg_types));
8997 return build_ptrmemfunc_type (build_pointer_type (member_type));
8999 else
9001 my_friendly_assert (TREE_CODE (member_type) != FUNCTION_TYPE,
9002 20030716);
9003 return build_offset_type (class_type, member_type);
9007 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9008 Check to see that the definition is valid. Issue appropriate error
9009 messages. Return 1 if the definition is particularly bad, or 0
9010 otherwise. */
9013 check_static_variable_definition (tree decl, tree type)
9015 /* Motion 10 at San Diego: If a static const integral data member is
9016 initialized with an integral constant expression, the initializer
9017 may appear either in the declaration (within the class), or in
9018 the definition, but not both. If it appears in the class, the
9019 member is a member constant. The file-scope definition is always
9020 required. */
9021 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9023 error ("invalid in-class initialization of static data member of non-integral type `%T'",
9024 type);
9025 /* If we just return the declaration, crashes will sometimes
9026 occur. We therefore return void_type_node, as if this was a
9027 friend declaration, to cause callers to completely ignore
9028 this declaration. */
9029 return 1;
9031 else if (!CP_TYPE_CONST_P (type))
9032 error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9033 decl);
9034 else if (pedantic && !INTEGRAL_TYPE_P (type))
9035 pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9037 return 0;
9040 /* Given the SIZE (i.e., number of elements) in an array, compute an
9041 appropriate index type for the array. If non-NULL, NAME is the
9042 name of the thing being declared. */
9044 tree
9045 compute_array_index_type (tree name, tree size)
9047 tree itype;
9049 /* If this involves a template parameter, it will be a constant at
9050 instantiation time, but we don't know what the value is yet.
9051 Even if no template parameters are involved, we may an expression
9052 that is not a constant; we don't even simplify `1 + 2' when
9053 processing a template. */
9054 if (processing_template_decl)
9056 /* Resolve a qualified reference to an enumerator or static
9057 const data member of ours. */
9058 if (TREE_CODE (size) == SCOPE_REF
9059 && TREE_OPERAND (size, 0) == current_class_type)
9061 tree t = lookup_field (current_class_type,
9062 TREE_OPERAND (size, 1), 0, false);
9063 if (t)
9064 size = t;
9067 return build_index_type (build_min (MINUS_EXPR, sizetype,
9068 size, integer_one_node));
9071 /* The size might be the result of a cast. */
9072 STRIP_TYPE_NOPS (size);
9074 /* It might be a const variable or enumeration constant. */
9075 size = decl_constant_value (size);
9077 /* The array bound must be an integer type. */
9078 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9079 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9080 && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9082 if (name)
9083 error ("size of array `%D' has non-integer type", name);
9084 else
9085 error ("size of array has non-integer type");
9086 size = integer_one_node;
9089 /* Normally, the array-bound will be a constant. */
9090 if (TREE_CODE (size) == INTEGER_CST)
9092 /* Check to see if the array bound overflowed. Make that an
9093 error, no matter how generous we're being. */
9094 int old_flag_pedantic_errors = flag_pedantic_errors;
9095 int old_pedantic = pedantic;
9096 pedantic = flag_pedantic_errors = 1;
9097 constant_expression_warning (size);
9098 pedantic = old_pedantic;
9099 flag_pedantic_errors = old_flag_pedantic_errors;
9101 /* An array must have a positive number of elements. */
9102 if (INT_CST_LT (size, integer_zero_node))
9104 if (name)
9105 error ("size of array `%D' is negative", name);
9106 else
9107 error ("size of array is negative");
9108 size = integer_one_node;
9110 /* Except that an extension we allow zero-sized arrays. We
9111 always allow them in system headers because glibc uses
9112 them. */
9113 else if (integer_zerop (size) && pedantic && !in_system_header)
9115 if (name)
9116 pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9117 else
9118 pedwarn ("ISO C++ forbids zero-size array");
9121 else if (TREE_CONSTANT (size))
9123 /* `(int) &fn' is not a valid array bound. */
9124 if (name)
9125 error ("size of array `%D' is not an integral constant-expression",
9126 name);
9127 else
9128 error ("size of array is not an integral constant-expression");
9131 /* Compute the index of the largest element in the array. It is
9132 one less than the number of elements in the array. */
9133 itype
9134 = fold (cp_build_binary_op (MINUS_EXPR,
9135 cp_convert (ssizetype, size),
9136 cp_convert (ssizetype,
9137 integer_one_node)));
9139 /* Check for variable-sized arrays. We allow such things as an
9140 extension, even though they are not allowed in ANSI/ISO C++. */
9141 if (!TREE_CONSTANT (itype))
9143 if (pedantic)
9145 if (name)
9146 pedwarn ("ISO C++ forbids variable-size array `%D'",
9147 name);
9148 else
9149 pedwarn ("ISO C++ forbids variable-size array");
9152 /* Create a variable-sized array index type. */
9153 itype = variable_size (itype);
9155 /* Make sure that there was no overflow when creating to a signed
9156 index type. (For example, on a 32-bit machine, an array with
9157 size 2^32 - 1 is too big.) */
9158 else if (TREE_OVERFLOW (itype))
9160 error ("overflow in array dimension");
9161 TREE_OVERFLOW (itype) = 0;
9164 /* Create and return the appropriate index type. */
9165 return build_index_type (itype);
9168 /* Returns the scope (if any) in which the entity declared by
9169 DECLARATOR will be located. If the entity was declared with an
9170 unqualified name, NULL_TREE is returned. */
9172 tree
9173 get_scope_of_declarator (tree declarator)
9175 if (!declarator)
9176 return NULL_TREE;
9178 switch (TREE_CODE (declarator))
9180 case CALL_EXPR:
9181 case ARRAY_REF:
9182 case INDIRECT_REF:
9183 case ADDR_EXPR:
9184 /* For any of these, the main declarator is the first operand. */
9185 return get_scope_of_declarator (TREE_OPERAND
9186 (declarator, 0));
9188 case SCOPE_REF:
9189 /* For a pointer-to-member, continue descending. */
9190 if (TREE_CODE (TREE_OPERAND (declarator, 1))
9191 == INDIRECT_REF)
9192 return get_scope_of_declarator (TREE_OPERAND
9193 (declarator, 1));
9194 /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9195 which the declaration occurs is the first operand. */
9196 return TREE_OPERAND (declarator, 0);
9198 case TREE_LIST:
9199 /* Attributes to be applied. The declarator is TREE_VALUE. */
9200 return get_scope_of_declarator (TREE_VALUE (declarator));
9202 default:
9203 /* Otherwise, we have a declarator-id which is not a qualified
9204 name; the entity will be declared in the current scope. */
9205 return NULL_TREE;
9209 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9210 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
9211 with this type. */
9213 static tree
9214 create_array_type_for_decl (tree name, tree type, tree size)
9216 tree itype = NULL_TREE;
9217 const char* error_msg;
9219 /* If things have already gone awry, bail now. */
9220 if (type == error_mark_node || size == error_mark_node)
9221 return error_mark_node;
9223 /* Assume that everything will go OK. */
9224 error_msg = NULL;
9226 /* There are some types which cannot be array elements. */
9227 switch (TREE_CODE (type))
9229 case VOID_TYPE:
9230 error_msg = "array of void";
9231 break;
9233 case FUNCTION_TYPE:
9234 error_msg = "array of functions";
9235 break;
9237 case REFERENCE_TYPE:
9238 error_msg = "array of references";
9239 break;
9241 case METHOD_TYPE:
9242 error_msg = "array of function members";
9243 break;
9245 default:
9246 break;
9249 /* If something went wrong, issue an error-message and return. */
9250 if (error_msg)
9252 if (name)
9253 error ("declaration of `%D' as %s", name, error_msg);
9254 else
9255 error ("creating %s", error_msg);
9257 return error_mark_node;
9260 /* [dcl.array]
9262 The constant expressions that specify the bounds of the arrays
9263 can be omitted only for the first member of the sequence. */
9264 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9266 if (name)
9267 error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9268 name);
9269 else
9270 error ("multidimensional array must have bounds for all dimensions except the first");
9272 return error_mark_node;
9275 /* Figure out the index type for the array. */
9276 if (size)
9277 itype = compute_array_index_type (name, size);
9279 return build_cplus_array_type (type, itype);
9282 /* Check that it's OK to declare a function with the indicated TYPE.
9283 SFK indicates the kind of special function (if any) that this
9284 function is. OPTYPE is the type given in a conversion operator
9285 declaration. Returns the actual return type of the function; that
9286 may be different than TYPE if an error occurs, or for certain
9287 special functions. */
9289 static tree
9290 check_special_function_return_type (special_function_kind sfk,
9291 tree type,
9292 tree optype)
9294 switch (sfk)
9296 case sfk_constructor:
9297 if (type)
9298 error ("return type specification for constructor invalid");
9300 type = void_type_node;
9301 break;
9303 case sfk_destructor:
9304 if (type)
9305 error ("return type specification for destructor invalid");
9306 type = void_type_node;
9307 break;
9309 case sfk_conversion:
9310 if (type && !same_type_p (type, optype))
9311 error ("operator `%T' declared to return `%T'", optype, type);
9312 else if (type)
9313 pedwarn ("return type specified for `operator %T'", optype);
9314 type = optype;
9315 break;
9317 default:
9318 abort ();
9319 break;
9322 return type;
9325 /* Given declspecs and a declarator (abstract or otherwise), determine
9326 the name and type of the object declared and construct a DECL node
9327 for it.
9329 DECLSPECS is a chain of tree_list nodes whose value fields
9330 are the storage classes and type specifiers.
9332 DECL_CONTEXT says which syntactic context this declaration is in:
9333 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9334 FUNCDEF for a function definition. Like NORMAL but a few different
9335 error messages in each case. Return value may be zero meaning
9336 this definition is too screwy to try to parse.
9337 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9338 handle member functions (which have FIELD context).
9339 Return value may be zero meaning this definition is too screwy to
9340 try to parse.
9341 PARM for a parameter declaration (either within a function prototype
9342 or before a function body). Make a PARM_DECL, or return void_type_node.
9343 CATCHPARM for a parameter declaration before a catch clause.
9344 TYPENAME if for a typename (in a cast or sizeof).
9345 Don't make a DECL node; just return the ..._TYPE node.
9346 FIELD for a struct or union field; make a FIELD_DECL.
9347 BITFIELD for a field with specified width.
9348 INITIALIZED is 1 if the decl has an initializer.
9350 ATTRLIST is a pointer to the list of attributes, which may be NULL
9351 if there are none; *ATTRLIST may be modified if attributes from inside
9352 the declarator should be applied to the declaration.
9354 When this function is called, scoping variables (such as
9355 CURRENT_CLASS_TYPE) should reflect the scope in which the
9356 declaration occurs, not the scope in which the new declaration will
9357 be placed. For example, on:
9359 void S::f() { ... }
9361 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9362 should not be `S'. */
9364 tree
9365 grokdeclarator (tree declarator,
9366 tree declspecs,
9367 enum decl_context decl_context,
9368 int initialized,
9369 tree* attrlist)
9371 RID_BIT_TYPE specbits;
9372 int nclasses = 0;
9373 tree spec;
9374 tree type = NULL_TREE;
9375 int longlong = 0;
9376 int type_quals;
9377 int virtualp, explicitp, friendp, inlinep, staticp;
9378 int explicit_int = 0;
9379 int explicit_char = 0;
9380 int defaulted_int = 0;
9381 int extern_langp = 0;
9382 tree dependant_name = NULL_TREE;
9384 tree typedef_decl = NULL_TREE;
9385 const char *name;
9386 tree typedef_type = NULL_TREE;
9387 int funcdef_flag = 0;
9388 enum tree_code innermost_code = ERROR_MARK;
9389 int bitfield = 0;
9390 #if 0
9391 /* See the code below that used this. */
9392 tree decl_attr = NULL_TREE;
9393 #endif
9395 /* Keep track of what sort of function is being processed
9396 so that we can warn about default return values, or explicit
9397 return values which do not match prescribed defaults. */
9398 special_function_kind sfk = sfk_none;
9400 tree dname = NULL_TREE;
9401 tree ctype = current_class_type;
9402 tree ctor_return_type = NULL_TREE;
9403 enum overload_flags flags = NO_SPECIAL;
9404 tree quals = NULL_TREE;
9405 tree raises = NULL_TREE;
9406 int template_count = 0;
9407 tree in_namespace = NULL_TREE;
9408 tree returned_attrs = NULL_TREE;
9409 tree scope = NULL_TREE;
9411 RIDBIT_RESET_ALL (specbits);
9412 if (decl_context == FUNCDEF)
9413 funcdef_flag = 1, decl_context = NORMAL;
9414 else if (decl_context == MEMFUNCDEF)
9415 funcdef_flag = -1, decl_context = FIELD;
9416 else if (decl_context == BITFIELD)
9417 bitfield = 1, decl_context = FIELD;
9419 /* Look inside a declarator for the name being declared
9420 and get it as a string, for an error message. */
9422 tree *next = &declarator;
9423 register tree decl;
9424 name = NULL;
9426 while (next && *next)
9428 decl = *next;
9429 switch (TREE_CODE (decl))
9431 case TREE_LIST:
9432 /* For attributes. */
9433 next = &TREE_VALUE (decl);
9434 break;
9436 case COND_EXPR:
9437 ctype = NULL_TREE;
9438 next = &TREE_OPERAND (decl, 0);
9439 break;
9441 case BIT_NOT_EXPR: /* For C++ destructors! */
9443 tree name = TREE_OPERAND (decl, 0);
9444 tree rename = NULL_TREE;
9446 my_friendly_assert (flags == NO_SPECIAL, 152);
9447 flags = DTOR_FLAG;
9448 sfk = sfk_destructor;
9449 if (TYPE_P (name))
9450 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9451 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9452 if (ctype == NULL_TREE)
9454 if (current_class_type == NULL_TREE)
9456 error ("destructors must be member functions");
9457 flags = NO_SPECIAL;
9459 else
9461 tree t = constructor_name (current_class_type);
9462 if (t != name)
9463 rename = t;
9466 else
9468 tree t = constructor_name (ctype);
9469 if (t != name)
9470 rename = t;
9473 if (rename)
9475 error ("destructor `%T' must match class name `%T'",
9476 name, rename);
9477 TREE_OPERAND (decl, 0) = rename;
9479 next = &name;
9481 break;
9483 case ADDR_EXPR: /* C++ reference declaration */
9484 /* Fall through. */
9485 case ARRAY_REF:
9486 case INDIRECT_REF:
9487 ctype = NULL_TREE;
9488 innermost_code = TREE_CODE (decl);
9489 next = &TREE_OPERAND (decl, 0);
9490 break;
9492 case CALL_EXPR:
9493 innermost_code = TREE_CODE (decl);
9494 if (decl_context == FIELD && ctype == NULL_TREE)
9495 ctype = current_class_type;
9496 if (ctype
9497 && TREE_OPERAND (decl, 0)
9498 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9499 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9500 ctype)))
9501 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9502 next = &TREE_OPERAND (decl, 0);
9503 decl = *next;
9504 if (ctype != NULL_TREE
9505 && decl != NULL_TREE && flags != DTOR_FLAG
9506 && constructor_name_p (decl, ctype))
9508 sfk = sfk_constructor;
9509 ctor_return_type = ctype;
9511 ctype = NULL_TREE;
9512 break;
9514 case TEMPLATE_ID_EXPR:
9516 tree fns = TREE_OPERAND (decl, 0);
9518 dname = fns;
9519 if (TREE_CODE (dname) == COMPONENT_REF)
9520 dname = TREE_OPERAND (dname, 1);
9521 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9523 my_friendly_assert (is_overloaded_fn (dname),
9524 19990331);
9525 dname = DECL_NAME (get_first_fn (dname));
9528 /* Fall through. */
9530 case IDENTIFIER_NODE:
9531 if (TREE_CODE (decl) == IDENTIFIER_NODE)
9532 dname = decl;
9534 next = 0;
9536 if (C_IS_RESERVED_WORD (dname))
9538 error ("declarator-id missing; using reserved word `%D'",
9539 dname);
9540 name = IDENTIFIER_POINTER (dname);
9542 else if (!IDENTIFIER_TYPENAME_P (dname))
9543 name = IDENTIFIER_POINTER (dname);
9544 else
9546 my_friendly_assert (flags == NO_SPECIAL, 154);
9547 flags = TYPENAME_FLAG;
9548 ctor_return_type = TREE_TYPE (dname);
9549 sfk = sfk_conversion;
9550 if (is_typename_at_global_scope (dname))
9551 name = IDENTIFIER_POINTER (dname);
9552 else
9553 name = "<invalid operator>";
9555 break;
9557 /* C++ extension */
9558 case SCOPE_REF:
9560 /* Perform error checking, and decide on a ctype. */
9561 tree cname = TREE_OPERAND (decl, 0);
9562 if (cname == NULL_TREE)
9563 ctype = NULL_TREE;
9564 else if (TREE_CODE (cname) == NAMESPACE_DECL)
9566 ctype = NULL_TREE;
9567 in_namespace = TREE_OPERAND (decl, 0);
9569 else if (! is_aggr_type (cname, 1))
9570 ctype = NULL_TREE;
9571 /* Must test TREE_OPERAND (decl, 1), in case user gives
9572 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
9573 else if (TREE_OPERAND (decl, 1)
9574 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9575 ctype = cname;
9576 else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9577 || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9579 /* This might be declaring a member of a template
9580 parm to be a friend. */
9581 ctype = cname;
9582 dependant_name = TREE_OPERAND (decl, 1);
9584 else if (ctype == NULL_TREE)
9585 ctype = cname;
9586 else if (TREE_COMPLEXITY (decl) == current_class_depth)
9588 else
9590 if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9592 error ("type `%T' is not derived from type `%T'",
9593 cname, ctype);
9594 ctype = NULL_TREE;
9596 else
9597 ctype = cname;
9600 /* It is valid to write:
9602 class C { void f(); };
9603 typedef C D;
9604 void D::f();
9606 The standard is not clear about whether `typedef const C D' is
9607 legal; as of 2002-09-15 the committee is considering
9608 that question. EDG 3.0 allows that syntax.
9609 Therefore, we do as well. */
9610 if (ctype)
9611 ctype = TYPE_MAIN_VARIANT (ctype);
9612 /* Update the declarator so that when we process it
9613 again the correct type is present. */
9614 TREE_OPERAND (decl, 0) = ctype;
9616 if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9617 && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9618 ctype))
9619 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9620 next = &TREE_OPERAND (decl, 1);
9621 decl = *next;
9622 if (ctype)
9624 tree name = decl;
9626 if (TREE_CODE (name) == BIT_NOT_EXPR)
9627 name = TREE_OPERAND (name, 0);
9629 if (!constructor_name_p (decl, ctype))
9631 else if (decl == name)
9633 sfk = sfk_constructor;
9634 ctor_return_type = ctype;
9636 else
9638 sfk = sfk_destructor;
9639 ctor_return_type = ctype;
9640 flags = DTOR_FLAG;
9641 TREE_OPERAND (decl, 0) = constructor_name (ctype);
9642 next = &TREE_OPERAND (decl, 0);
9646 break;
9648 case ERROR_MARK:
9649 next = 0;
9650 break;
9652 case TYPE_DECL:
9653 /* Parse error puts this typespec where
9654 a declarator should go. */
9655 error ("`%T' specified as declarator-id", DECL_NAME (decl));
9656 if (TREE_TYPE (decl) == current_class_type)
9657 error (" perhaps you want `%T' for a constructor",
9658 current_class_name);
9659 dname = DECL_NAME (decl);
9660 name = IDENTIFIER_POINTER (dname);
9662 /* Avoid giving two errors for this. */
9663 IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9665 declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
9666 *next = dname;
9667 next = 0;
9668 break;
9670 case BASELINK:
9671 next = &BASELINK_FUNCTIONS (decl);
9672 break;
9674 case TEMPLATE_DECL:
9675 /* Sometimes, we see a template-name used as part of a
9676 decl-specifier like in
9677 std::allocator alloc;
9678 Handle that gracefully. */
9679 error ("invalid use of template-name '%E' in a declarator", decl);
9680 return error_mark_node;
9681 break;
9683 default:
9684 my_friendly_assert (0, 20020917);
9689 /* A function definition's declarator must have the form of
9690 a function declarator. */
9692 if (funcdef_flag && innermost_code != CALL_EXPR)
9693 return 0;
9695 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9696 && innermost_code != CALL_EXPR
9697 && ! (ctype && declspecs == NULL_TREE))
9699 error ("declaration of `%D' as non-function", dname);
9700 return void_type_node;
9703 /* Anything declared one level down from the top level
9704 must be one of the parameters of a function
9705 (because the body is at least two levels down). */
9707 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9708 by not allowing C++ class definitions to specify their parameters
9709 with xdecls (must be spec.d in the parmlist).
9711 Since we now wait to push a class scope until we are sure that
9712 we are in a legitimate method context, we must set oldcname
9713 explicitly (since current_class_name is not yet alive).
9715 We also want to avoid calling this a PARM if it is in a namespace. */
9717 if (decl_context == NORMAL && !toplevel_bindings_p ())
9719 struct cp_binding_level *b = current_binding_level;
9720 current_binding_level = b->level_chain;
9721 if (current_binding_level != 0 && toplevel_bindings_p ())
9722 decl_context = PARM;
9723 current_binding_level = b;
9726 if (name == NULL)
9727 name = decl_context == PARM ? "parameter" : "type name";
9729 /* Look through the decl specs and record which ones appear.
9730 Some typespecs are defined as built-in typenames.
9731 Others, the ones that are modifiers of other types,
9732 are represented by bits in SPECBITS: set the bits for
9733 the modifiers that appear. Storage class keywords are also in SPECBITS.
9735 If there is a typedef name or a type, store the type in TYPE.
9736 This includes builtin typedefs such as `int'.
9738 Set EXPLICIT_INT if the type is `int' or `char' and did not
9739 come from a user typedef.
9741 Set LONGLONG if `long' is mentioned twice.
9743 For C++, constructors and destructors have their own fast treatment. */
9745 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9747 register int i;
9748 register tree id;
9750 /* Certain parse errors slip through. For example,
9751 `int class;' is not caught by the parser. Try
9752 weakly to recover here. */
9753 if (TREE_CODE (spec) != TREE_LIST)
9754 return 0;
9756 id = TREE_VALUE (spec);
9758 /* If the entire declaration is itself tagged as deprecated then
9759 suppress reports of deprecated items. */
9760 if (!adding_implicit_members && id && TREE_DEPRECATED (id))
9762 if (deprecated_state != DEPRECATED_SUPPRESS)
9763 warn_deprecated_use (id);
9766 if (TREE_CODE (id) == IDENTIFIER_NODE)
9768 if (id == ridpointers[(int) RID_INT]
9769 || id == ridpointers[(int) RID_CHAR]
9770 || id == ridpointers[(int) RID_BOOL]
9771 || id == ridpointers[(int) RID_WCHAR])
9773 if (type)
9775 if (id == ridpointers[(int) RID_BOOL])
9776 error ("`bool' is now a keyword");
9777 else
9778 error ("extraneous `%T' ignored", id);
9780 else
9782 if (id == ridpointers[(int) RID_INT])
9783 explicit_int = 1;
9784 else if (id == ridpointers[(int) RID_CHAR])
9785 explicit_char = 1;
9786 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
9788 goto found;
9790 /* C++ aggregate types. */
9791 if (IDENTIFIER_HAS_TYPE_VALUE (id))
9793 if (type)
9794 error ("multiple declarations `%T' and `%T'", type, id);
9795 else
9796 type = IDENTIFIER_TYPE_VALUE (id);
9797 goto found;
9800 for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
9802 if (ridpointers[i] == id)
9804 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
9806 if (pedantic && ! in_system_header && warn_long_long)
9807 pedwarn ("ISO C++ does not support `long long'");
9808 if (longlong)
9809 error ("`long long long' is too long for GCC");
9810 else
9811 longlong = 1;
9813 else if (RIDBIT_SETP (i, specbits))
9814 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
9816 /* Diagnose "__thread extern" or "__thread static". */
9817 if (RIDBIT_SETP (RID_THREAD, specbits))
9819 if (i == (int)RID_EXTERN)
9820 error ("`__thread' before `extern'");
9821 else if (i == (int)RID_STATIC)
9822 error ("`__thread' before `static'");
9825 if (i == (int)RID_EXTERN
9826 && TREE_PURPOSE (spec) == error_mark_node)
9827 /* This extern was part of a language linkage. */
9828 extern_langp = 1;
9830 RIDBIT_SET (i, specbits);
9831 goto found;
9835 else if (TREE_CODE (id) == TYPE_DECL)
9837 if (type)
9838 error ("multiple declarations `%T' and `%T'", type,
9839 TREE_TYPE (id));
9840 else
9842 type = TREE_TYPE (id);
9843 TREE_VALUE (spec) = type;
9844 typedef_decl = id;
9846 goto found;
9848 if (type)
9849 error ("two or more data types in declaration of `%s'", name);
9850 else if (TREE_CODE (id) == IDENTIFIER_NODE)
9852 register tree t = lookup_name (id, 1);
9853 if (!t || TREE_CODE (t) != TYPE_DECL)
9854 error ("`%s' fails to be a typedef or built in type",
9855 IDENTIFIER_POINTER (id));
9856 else
9858 type = TREE_TYPE (t);
9859 typedef_decl = t;
9862 else if (id != error_mark_node)
9863 /* Can't change CLASS nodes into RECORD nodes here! */
9864 type = id;
9866 found: ;
9869 #if 0
9870 /* See the code below that used this. */
9871 if (typedef_decl)
9872 decl_attr = DECL_ATTRIBUTES (typedef_decl);
9873 #endif
9874 typedef_type = type;
9876 /* No type at all: default to `int', and set DEFAULTED_INT
9877 because it was not a user-defined typedef. */
9879 if (type == NULL_TREE
9880 && (RIDBIT_SETP (RID_SIGNED, specbits)
9881 || RIDBIT_SETP (RID_UNSIGNED, specbits)
9882 || RIDBIT_SETP (RID_LONG, specbits)
9883 || RIDBIT_SETP (RID_SHORT, specbits)))
9885 /* These imply 'int'. */
9886 type = integer_type_node;
9887 defaulted_int = 1;
9890 if (sfk != sfk_none)
9891 type = check_special_function_return_type (sfk, type,
9892 ctor_return_type);
9893 else if (type == NULL_TREE)
9895 int is_main;
9897 explicit_int = -1;
9899 /* We handle `main' specially here, because 'main () { }' is so
9900 common. With no options, it is allowed. With -Wreturn-type,
9901 it is a warning. It is only an error with -pedantic-errors. */
9902 is_main = (funcdef_flag
9903 && dname && MAIN_NAME_P (dname)
9904 && ctype == NULL_TREE
9905 && in_namespace == NULL_TREE
9906 && current_namespace == global_namespace);
9908 if (in_system_header || flag_ms_extensions)
9909 /* Allow it, sigh. */;
9910 else if (pedantic || ! is_main)
9911 pedwarn ("ISO C++ forbids declaration of `%s' with no type",
9912 name);
9913 else if (warn_return_type)
9914 warning ("ISO C++ forbids declaration of `%s' with no type",
9915 name);
9917 type = integer_type_node;
9920 ctype = NULL_TREE;
9922 /* Now process the modifiers that were specified
9923 and check for invalid combinations. */
9925 /* Long double is a special combination. */
9927 if (RIDBIT_SETP (RID_LONG, specbits)
9928 && TYPE_MAIN_VARIANT (type) == double_type_node)
9930 RIDBIT_RESET (RID_LONG, specbits);
9931 type = build_qualified_type (long_double_type_node,
9932 cp_type_quals (type));
9935 /* Check all other uses of type modifiers. */
9937 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
9938 || RIDBIT_SETP (RID_SIGNED, specbits)
9939 || RIDBIT_SETP (RID_LONG, specbits)
9940 || RIDBIT_SETP (RID_SHORT, specbits))
9942 int ok = 0;
9944 if (TREE_CODE (type) == REAL_TYPE)
9945 error ("short, signed or unsigned invalid for `%s'", name);
9946 else if (TREE_CODE (type) != INTEGER_TYPE)
9947 error ("long, short, signed or unsigned invalid for `%s'", name);
9948 else if (RIDBIT_SETP (RID_LONG, specbits)
9949 && RIDBIT_SETP (RID_SHORT, specbits))
9950 error ("long and short specified together for `%s'", name);
9951 else if ((RIDBIT_SETP (RID_LONG, specbits)
9952 || RIDBIT_SETP (RID_SHORT, specbits))
9953 && explicit_char)
9954 error ("long or short specified with char for `%s'", name);
9955 else if ((RIDBIT_SETP (RID_LONG, specbits)
9956 || RIDBIT_SETP (RID_SHORT, specbits))
9957 && TREE_CODE (type) == REAL_TYPE)
9958 error ("long or short specified with floating type for `%s'", name);
9959 else if (RIDBIT_SETP (RID_SIGNED, specbits)
9960 && RIDBIT_SETP (RID_UNSIGNED, specbits))
9961 error ("signed and unsigned given together for `%s'", name);
9962 else
9964 ok = 1;
9965 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
9967 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
9968 name);
9969 if (flag_pedantic_errors)
9970 ok = 0;
9974 /* Discard the type modifiers if they are invalid. */
9975 if (! ok)
9977 RIDBIT_RESET (RID_UNSIGNED, specbits);
9978 RIDBIT_RESET (RID_SIGNED, specbits);
9979 RIDBIT_RESET (RID_LONG, specbits);
9980 RIDBIT_RESET (RID_SHORT, specbits);
9981 longlong = 0;
9985 if (RIDBIT_SETP (RID_COMPLEX, specbits)
9986 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9988 error ("complex invalid for `%s'", name);
9989 RIDBIT_RESET (RID_COMPLEX, specbits);
9992 /* Decide whether an integer type is signed or not.
9993 Optionally treat bitfields as signed by default. */
9994 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
9995 /* [class.bit]
9997 It is implementation-defined whether a plain (neither
9998 explicitly signed or unsigned) char, short, int, or long
9999 bit-field is signed or unsigned.
10001 Naturally, we extend this to long long as well. Note that
10002 this does not include wchar_t. */
10003 || (bitfield && !flag_signed_bitfields
10004 && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10005 /* A typedef for plain `int' without `signed' can be
10006 controlled just like plain `int', but a typedef for
10007 `signed int' cannot be so controlled. */
10008 && !(typedef_decl
10009 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10010 && (TREE_CODE (type) == INTEGER_TYPE
10011 || TREE_CODE (type) == CHAR_TYPE)
10012 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10014 if (longlong)
10015 type = long_long_unsigned_type_node;
10016 else if (RIDBIT_SETP (RID_LONG, specbits))
10017 type = long_unsigned_type_node;
10018 else if (RIDBIT_SETP (RID_SHORT, specbits))
10019 type = short_unsigned_type_node;
10020 else if (type == char_type_node)
10021 type = unsigned_char_type_node;
10022 else if (typedef_decl)
10023 type = c_common_unsigned_type (type);
10024 else
10025 type = unsigned_type_node;
10027 else if (RIDBIT_SETP (RID_SIGNED, specbits)
10028 && type == char_type_node)
10029 type = signed_char_type_node;
10030 else if (longlong)
10031 type = long_long_integer_type_node;
10032 else if (RIDBIT_SETP (RID_LONG, specbits))
10033 type = long_integer_type_node;
10034 else if (RIDBIT_SETP (RID_SHORT, specbits))
10035 type = short_integer_type_node;
10037 if (RIDBIT_SETP (RID_COMPLEX, specbits))
10039 /* If we just have "complex", it is equivalent to
10040 "complex double", but if any modifiers at all are specified it is
10041 the complex form of TYPE. E.g, "complex short" is
10042 "complex short int". */
10044 if (defaulted_int && ! longlong
10045 && ! (RIDBIT_SETP (RID_LONG, specbits)
10046 || RIDBIT_SETP (RID_SHORT, specbits)
10047 || RIDBIT_SETP (RID_SIGNED, specbits)
10048 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10049 type = complex_double_type_node;
10050 else if (type == integer_type_node)
10051 type = complex_integer_type_node;
10052 else if (type == float_type_node)
10053 type = complex_float_type_node;
10054 else if (type == double_type_node)
10055 type = complex_double_type_node;
10056 else if (type == long_double_type_node)
10057 type = complex_long_double_type_node;
10058 else
10059 type = build_complex_type (type);
10062 type_quals = TYPE_UNQUALIFIED;
10063 if (RIDBIT_SETP (RID_CONST, specbits))
10064 type_quals |= TYPE_QUAL_CONST;
10065 if (RIDBIT_SETP (RID_VOLATILE, specbits))
10066 type_quals |= TYPE_QUAL_VOLATILE;
10067 if (RIDBIT_SETP (RID_RESTRICT, specbits))
10068 type_quals |= TYPE_QUAL_RESTRICT;
10069 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10070 error ("qualifiers are not allowed on declaration of `operator %T'",
10071 ctor_return_type);
10073 type_quals |= cp_type_quals (type);
10074 type = cp_build_qualified_type_real
10075 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10076 ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10077 /* We might have ignored or rejected some of the qualifiers. */
10078 type_quals = cp_type_quals (type);
10080 staticp = 0;
10081 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10082 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10083 RIDBIT_RESET (RID_VIRTUAL, specbits);
10084 explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10085 RIDBIT_RESET (RID_EXPLICIT, specbits);
10087 if (RIDBIT_SETP (RID_STATIC, specbits))
10088 staticp = 1 + (decl_context == FIELD);
10090 if (virtualp && staticp == 2)
10092 error ("member `%D' cannot be declared both virtual and static",
10093 dname);
10094 staticp = 0;
10096 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10097 RIDBIT_RESET (RID_FRIEND, specbits);
10099 if (dependant_name && !friendp)
10101 error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10102 return void_type_node;
10105 /* Warn if two storage classes are given. Default to `auto'. */
10107 if (RIDBIT_ANY_SET (specbits))
10109 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10110 if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10111 if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10112 if (decl_context == PARM && nclasses > 0)
10113 error ("storage class specifiers invalid in parameter declarations");
10114 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10116 if (decl_context == PARM)
10117 error ("typedef declaration invalid in parameter declaration");
10118 nclasses++;
10120 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10121 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10122 if (!nclasses && !friendp && extern_langp)
10123 nclasses++;
10126 /* Give error if `virtual' is used outside of class declaration. */
10127 if (virtualp
10128 && (current_class_name == NULL_TREE || decl_context != FIELD))
10130 error ("virtual outside class declaration");
10131 virtualp = 0;
10134 /* Static anonymous unions are dealt with here. */
10135 if (staticp && decl_context == TYPENAME
10136 && TREE_CODE (declspecs) == TREE_LIST
10137 && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10138 decl_context = FIELD;
10140 /* Warn about storage classes that are invalid for certain
10141 kinds of declarations (parameters, typenames, etc.). */
10143 /* "static __thread" and "extern __thread" are allowed. */
10144 if (nclasses == 2
10145 && RIDBIT_SETP (RID_THREAD, specbits)
10146 && (RIDBIT_SETP (RID_EXTERN, specbits)
10147 || RIDBIT_SETP (RID_STATIC, specbits)))
10148 nclasses = 1;
10150 if (nclasses > 1)
10151 error ("multiple storage classes in declaration of `%s'", name);
10152 else if (decl_context != NORMAL && nclasses > 0)
10154 if ((decl_context == PARM || decl_context == CATCHPARM)
10155 && (RIDBIT_SETP (RID_REGISTER, specbits)
10156 || RIDBIT_SETP (RID_AUTO, specbits)))
10158 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10160 else if (decl_context == FIELD
10161 /* C++ allows static class elements */
10162 && RIDBIT_SETP (RID_STATIC, specbits))
10163 /* C++ also allows inlines and signed and unsigned elements,
10164 but in those cases we don't come in here. */
10166 else
10168 if (decl_context == FIELD)
10170 tree tmp = NULL_TREE;
10171 register int op = 0;
10173 if (declarator)
10175 /* Avoid trying to get an operand off an identifier node. */
10176 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10177 tmp = declarator;
10178 else
10179 tmp = TREE_OPERAND (declarator, 0);
10180 op = IDENTIFIER_OPNAME_P (tmp);
10181 if (IDENTIFIER_TYPENAME_P (tmp))
10183 if (is_typename_at_global_scope (tmp))
10184 name = IDENTIFIER_POINTER (tmp);
10185 else
10186 name = "<invalid operator>";
10189 error ("storage class specified for %s `%s'",
10190 op ? "member operator" : "field",
10191 name);
10193 else
10195 if (decl_context == PARM || decl_context == CATCHPARM)
10196 error ("storage class specified for parameter `%s'", name);
10197 else
10198 error ("storage class specified for typename");
10200 RIDBIT_RESET (RID_REGISTER, specbits);
10201 RIDBIT_RESET (RID_AUTO, specbits);
10202 RIDBIT_RESET (RID_EXTERN, specbits);
10203 RIDBIT_RESET (RID_THREAD, specbits);
10206 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10208 if (toplevel_bindings_p ())
10210 /* It's common practice (and completely valid) to have a const
10211 be initialized and declared extern. */
10212 if (!(type_quals & TYPE_QUAL_CONST))
10213 warning ("`%s' initialized and declared `extern'", name);
10215 else
10216 error ("`%s' has both `extern' and initializer", name);
10218 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10219 && ! toplevel_bindings_p ())
10220 error ("nested function `%s' declared `extern'", name);
10221 else if (toplevel_bindings_p ())
10223 if (RIDBIT_SETP (RID_AUTO, specbits))
10224 error ("top-level declaration of `%s' specifies `auto'", name);
10226 else if (RIDBIT_SETP (RID_THREAD, specbits)
10227 && !RIDBIT_SETP (RID_EXTERN, specbits)
10228 && !RIDBIT_SETP (RID_STATIC, specbits))
10230 error ("function-scope `%s' implicitly auto and declared `__thread'",
10231 name);
10232 RIDBIT_RESET (RID_THREAD, specbits);
10235 if (nclasses > 0 && friendp)
10236 error ("storage class specifiers invalid in friend function declarations");
10238 scope = get_scope_of_declarator (declarator);
10240 /* Now figure out the structure of the declarator proper.
10241 Descend through it, creating more complex types, until we reach
10242 the declared identifier (or NULL_TREE, in an abstract declarator). */
10244 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10245 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10247 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10248 an INDIRECT_REF (for *...),
10249 a CALL_EXPR (for ...(...)),
10250 an identifier (for the name being declared)
10251 or a null pointer (for the place in an absolute declarator
10252 where the name was omitted).
10253 For the last two cases, we have just exited the loop.
10255 For C++ it could also be
10256 a SCOPE_REF (for class :: ...). In this case, we have converted
10257 sensible names to types, and those are the values we use to
10258 qualify the member name.
10259 an ADDR_EXPR (for &...),
10260 a BIT_NOT_EXPR (for destructors)
10262 At this point, TYPE is the type of elements of an array,
10263 or for a function to return, or for a pointer to point to.
10264 After this sequence of ifs, TYPE is the type of the
10265 array or function or pointer, and DECLARATOR has had its
10266 outermost layer removed. */
10268 if (type == error_mark_node)
10270 if (declarator == error_mark_node)
10271 return error_mark_node;
10272 else if (TREE_CODE (declarator) == SCOPE_REF)
10273 declarator = TREE_OPERAND (declarator, 1);
10274 else
10275 declarator = TREE_OPERAND (declarator, 0);
10276 continue;
10278 if (quals != NULL_TREE
10279 && (declarator == NULL_TREE
10280 || TREE_CODE (declarator) != SCOPE_REF))
10282 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10283 ctype = TYPE_METHOD_BASETYPE (type);
10284 if (ctype != NULL_TREE)
10286 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10287 grok_method_quals (ctype, dummy, quals);
10288 type = TREE_TYPE (dummy);
10289 quals = NULL_TREE;
10293 switch (TREE_CODE (declarator))
10295 case TREE_LIST:
10297 /* We encode a declarator with embedded attributes using
10298 a TREE_LIST. */
10299 tree attrs = TREE_PURPOSE (declarator);
10300 tree inner_decl;
10301 int attr_flags;
10303 declarator = TREE_VALUE (declarator);
10304 inner_decl = declarator;
10305 while (inner_decl != NULL_TREE
10306 && TREE_CODE (inner_decl) == TREE_LIST)
10307 inner_decl = TREE_VALUE (inner_decl);
10308 attr_flags = 0;
10309 if (inner_decl == NULL_TREE
10310 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10311 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10312 if (TREE_CODE (inner_decl) == CALL_EXPR)
10313 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10314 if (TREE_CODE (inner_decl) == ARRAY_REF)
10315 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10316 returned_attrs = decl_attributes (&type,
10317 chainon (returned_attrs, attrs),
10318 attr_flags);
10320 break;
10322 case ARRAY_REF:
10324 tree size = TREE_OPERAND (declarator, 1);
10325 declarator = TREE_OPERAND (declarator, 0);
10327 type = create_array_type_for_decl (dname, type, size);
10329 ctype = NULL_TREE;
10331 break;
10333 case CALL_EXPR:
10335 tree arg_types;
10336 int funcdecl_p;
10337 tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10338 tree inner_decl = TREE_OPERAND (declarator, 0);
10340 /* Declaring a function type.
10341 Make sure we have a valid type for the function to return. */
10343 /* We now know that the TYPE_QUALS don't apply to the
10344 decl, but to its return type. */
10345 type_quals = TYPE_UNQUALIFIED;
10347 /* Warn about some types functions can't return. */
10349 if (TREE_CODE (type) == FUNCTION_TYPE)
10351 error ("`%s' declared as function returning a function", name);
10352 type = integer_type_node;
10354 if (TREE_CODE (type) == ARRAY_TYPE)
10356 error ("`%s' declared as function returning an array", name);
10357 type = integer_type_node;
10360 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10361 inner_decl = TREE_OPERAND (inner_decl, 1);
10363 if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10364 inner_decl = dname;
10366 /* Pick up type qualifiers which should be applied to `this'. */
10367 quals = CALL_DECLARATOR_QUALS (declarator);
10369 /* Pick up the exception specifications. */
10370 raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10372 /* Say it's a definition only for the CALL_EXPR
10373 closest to the identifier. */
10374 funcdecl_p
10375 = inner_decl
10376 && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10377 || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10378 || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10380 if (ctype == NULL_TREE
10381 && decl_context == FIELD
10382 && funcdecl_p
10383 && (friendp == 0 || dname == current_class_name))
10384 ctype = current_class_type;
10386 if (ctype && sfk == sfk_conversion)
10387 TYPE_HAS_CONVERSION (ctype) = 1;
10388 if (ctype && constructor_name_p (dname, ctype))
10390 /* We are within a class's scope. If our declarator name
10391 is the same as the class name, and we are defining
10392 a function, then it is a constructor/destructor, and
10393 therefore returns a void type. */
10395 if (flags == DTOR_FLAG)
10397 /* ISO C++ 12.4/2. A destructor may not be
10398 declared const or volatile. A destructor may
10399 not be static. */
10400 if (staticp == 2)
10401 error ("destructor cannot be static member function");
10402 if (quals)
10404 error ("destructors may not be `%s'",
10405 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10406 quals = NULL_TREE;
10408 if (decl_context == FIELD)
10410 if (! member_function_or_else (ctype,
10411 current_class_type,
10412 flags))
10413 return void_type_node;
10416 else /* It's a constructor. */
10418 if (explicitp == 1)
10419 explicitp = 2;
10420 /* ISO C++ 12.1. A constructor may not be
10421 declared const or volatile. A constructor may
10422 not be virtual. A constructor may not be
10423 static. */
10424 if (staticp == 2)
10425 error ("constructor cannot be static member function");
10426 if (virtualp)
10428 pedwarn ("constructors cannot be declared virtual");
10429 virtualp = 0;
10431 if (quals)
10433 error ("constructors may not be `%s'",
10434 IDENTIFIER_POINTER (TREE_VALUE (quals)));
10435 quals = NULL_TREE;
10438 RID_BIT_TYPE tmp_bits;
10439 memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10440 RIDBIT_RESET (RID_INLINE, tmp_bits);
10441 RIDBIT_RESET (RID_STATIC, tmp_bits);
10442 if (RIDBIT_ANY_SET (tmp_bits))
10443 error ("return value type specifier for constructor ignored");
10445 if (decl_context == FIELD)
10447 if (! member_function_or_else (ctype,
10448 current_class_type,
10449 flags))
10450 return void_type_node;
10451 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10452 if (sfk != sfk_constructor)
10453 return NULL_TREE;
10456 if (decl_context == FIELD)
10457 staticp = 0;
10459 else if (friendp)
10461 if (initialized)
10462 error ("can't initialize friend function `%s'", name);
10463 if (virtualp)
10465 /* Cannot be both friend and virtual. */
10466 error ("virtual functions cannot be friends");
10467 RIDBIT_RESET (RID_FRIEND, specbits);
10468 friendp = 0;
10470 if (decl_context == NORMAL)
10471 error ("friend declaration not in class definition");
10472 if (current_function_decl && funcdef_flag)
10473 error ("can't define friend function `%s' in a local class definition",
10474 name);
10477 /* Construct the function type and go to the next
10478 inner layer of declarator. */
10480 declarator = TREE_OPERAND (declarator, 0);
10482 arg_types = grokparms (inner_parms);
10484 if (declarator && flags == DTOR_FLAG)
10486 /* A destructor declared in the body of a class will
10487 be represented as a BIT_NOT_EXPR. But, we just
10488 want the underlying IDENTIFIER. */
10489 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10490 declarator = TREE_OPERAND (declarator, 0);
10492 if (arg_types != void_list_node)
10494 error ("destructors may not have parameters");
10495 arg_types = void_list_node;
10496 last_function_parms = NULL_TREE;
10500 /* ANSI says that `const int foo ();'
10501 does not make the function foo const. */
10502 type = build_function_type (type, arg_types);
10504 break;
10506 case ADDR_EXPR:
10507 case INDIRECT_REF:
10508 /* Filter out pointers-to-references and references-to-references.
10509 We can get these if a TYPE_DECL is used. */
10511 if (TREE_CODE (type) == REFERENCE_TYPE)
10513 error (TREE_CODE (declarator) == ADDR_EXPR
10514 ? "cannot declare reference to `%#T'"
10515 : "cannot declare pointer to `%#T'", type);
10516 type = TREE_TYPE (type);
10518 else if (VOID_TYPE_P (type)
10519 && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10520 error (ctype ? "cannot declare pointer to `%#T' member"
10521 : "cannot declare reference to `%#T'", type);
10523 /* Merge any constancy or volatility into the target type
10524 for the pointer. */
10526 /* We now know that the TYPE_QUALS don't apply to the decl,
10527 but to the target of the pointer. */
10528 type_quals = TYPE_UNQUALIFIED;
10530 if (TREE_CODE (declarator) == ADDR_EXPR)
10532 if (!VOID_TYPE_P (type))
10533 type = build_reference_type (type);
10535 else if (TREE_CODE (type) == METHOD_TYPE)
10536 type = build_ptrmemfunc_type (build_pointer_type (type));
10537 else if (ctype)
10538 type = build_ptrmem_type (ctype, type);
10539 else
10540 type = build_pointer_type (type);
10542 /* Process a list of type modifier keywords (such as
10543 const or volatile) that were given inside the `*' or `&'. */
10545 if (TREE_TYPE (declarator))
10547 register tree typemodlist;
10548 int erred = 0;
10549 int constp = 0;
10550 int volatilep = 0;
10551 int restrictp = 0;
10553 for (typemodlist = TREE_TYPE (declarator); typemodlist;
10554 typemodlist = TREE_CHAIN (typemodlist))
10556 tree qualifier = TREE_VALUE (typemodlist);
10558 if (qualifier == ridpointers[(int) RID_CONST])
10560 constp++;
10561 type_quals |= TYPE_QUAL_CONST;
10563 else if (qualifier == ridpointers[(int) RID_VOLATILE])
10565 volatilep++;
10566 type_quals |= TYPE_QUAL_VOLATILE;
10568 else if (qualifier == ridpointers[(int) RID_RESTRICT])
10570 restrictp++;
10571 type_quals |= TYPE_QUAL_RESTRICT;
10573 else if (!erred)
10575 erred = 1;
10576 error ("invalid type modifier within pointer declarator");
10579 if (constp > 1)
10580 pedwarn ("duplicate `const'");
10581 if (volatilep > 1)
10582 pedwarn ("duplicate `volatile'");
10583 if (restrictp > 1)
10584 pedwarn ("duplicate `restrict'");
10585 type = cp_build_qualified_type (type, type_quals);
10586 type_quals = cp_type_quals (type);
10588 declarator = TREE_OPERAND (declarator, 0);
10589 ctype = NULL_TREE;
10590 break;
10592 case SCOPE_REF:
10594 /* We have converted type names to NULL_TREE if the
10595 name was bogus, or to a _TYPE node, if not.
10597 The variable CTYPE holds the type we will ultimately
10598 resolve to. The code here just needs to build
10599 up appropriate member types. */
10600 tree sname = TREE_OPERAND (declarator, 1);
10601 tree t;
10603 /* Destructors can have their visibilities changed as well. */
10604 if (TREE_CODE (sname) == BIT_NOT_EXPR)
10605 sname = TREE_OPERAND (sname, 0);
10607 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10609 /* We had a reference to a global decl, or
10610 perhaps we were given a non-aggregate typedef,
10611 in which case we cleared this out, and should just
10612 keep going as though it wasn't there. */
10613 declarator = sname;
10614 continue;
10616 ctype = TREE_OPERAND (declarator, 0);
10618 t = ctype;
10619 while (t != NULL_TREE && CLASS_TYPE_P (t))
10621 /* You're supposed to have one `template <...>'
10622 for every template class, but you don't need one
10623 for a full specialization. For example:
10625 template <class T> struct S{};
10626 template <> struct S<int> { void f(); };
10627 void S<int>::f () {}
10629 is correct; there shouldn't be a `template <>' for
10630 the definition of `S<int>::f'. */
10631 if (CLASSTYPE_TEMPLATE_INFO (t)
10632 && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
10633 || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10634 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
10635 template_count += 1;
10637 t = TYPE_MAIN_DECL (t);
10638 t = DECL_CONTEXT (t);
10641 if (sname == NULL_TREE)
10642 goto done_scoping;
10644 if (TREE_CODE (sname) == IDENTIFIER_NODE)
10646 /* This is the `standard' use of the scoping operator:
10647 basetype :: member . */
10649 if (ctype == current_class_type)
10651 /* class A {
10652 void A::f ();
10655 Is this ill-formed? */
10657 if (pedantic)
10658 pedwarn ("extra qualification `%T::' on member `%s' ignored",
10659 ctype, name);
10661 else if (TREE_CODE (type) == FUNCTION_TYPE)
10663 if (current_class_type == NULL_TREE || friendp)
10664 type
10665 = build_method_type_directly (ctype,
10666 TREE_TYPE (type),
10667 TYPE_ARG_TYPES (type));
10668 else
10670 error ("cannot declare member function `%T::%s' within `%T'",
10671 ctype, name, current_class_type);
10672 return error_mark_node;
10675 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10676 || COMPLETE_TYPE_P (complete_type (ctype)))
10678 /* Have to move this code elsewhere in this function.
10679 this code is used for i.e., typedef int A::M; M *pm;
10681 It is? How? jason 10/2/94 */
10683 if (current_class_type)
10685 error ("cannot declare member `%T::%s' within `%T'",
10686 ctype, name, current_class_type);
10687 return void_type_node;
10690 else
10692 cxx_incomplete_type_error (NULL_TREE, ctype);
10693 return error_mark_node;
10696 declarator = sname;
10698 else if (TREE_CODE (sname) == SCOPE_REF)
10699 abort ();
10700 else
10702 done_scoping:
10703 declarator = TREE_OPERAND (declarator, 1);
10704 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10705 /* In this case, we will deal with it later. */
10707 else if (TREE_CODE (type) == FUNCTION_TYPE)
10708 type = build_method_type_directly (ctype,
10709 TREE_TYPE (type),
10710 TYPE_ARG_TYPES (type));
10713 break;
10715 case BIT_NOT_EXPR:
10716 declarator = TREE_OPERAND (declarator, 0);
10717 break;
10719 case BASELINK:
10720 declarator = BASELINK_FUNCTIONS (declarator);
10721 break;
10723 case RECORD_TYPE:
10724 case UNION_TYPE:
10725 case ENUMERAL_TYPE:
10726 declarator = NULL_TREE;
10727 break;
10729 case ERROR_MARK:
10730 declarator = NULL_TREE;
10731 break;
10733 default:
10734 abort ();
10738 if (returned_attrs)
10740 if (attrlist)
10741 *attrlist = chainon (returned_attrs, *attrlist);
10742 else
10743 attrlist = &returned_attrs;
10746 /* Now TYPE has the actual type. */
10748 /* Did array size calculations overflow? */
10750 if (TREE_CODE (type) == ARRAY_TYPE
10751 && COMPLETE_TYPE_P (type)
10752 && TREE_OVERFLOW (TYPE_SIZE (type)))
10754 error ("size of array `%s' is too large", name);
10755 /* If we proceed with the array type as it is, we'll eventually
10756 crash in tree_low_cst(). */
10757 type = error_mark_node;
10760 if ((decl_context == FIELD || decl_context == PARM)
10761 && !processing_template_decl
10762 && variably_modified_type_p (type))
10764 if (decl_context == FIELD)
10765 error ("data member may not have variably modified type `%T'", type);
10766 else
10767 error ("parameter may not have variably modified type `%T'", type);
10768 type = error_mark_node;
10771 if (explicitp == 1 || (explicitp && friendp))
10773 /* [dcl.fct.spec] The explicit specifier shall only be used in
10774 declarations of constructors within a class definition. */
10775 error ("only declarations of constructors can be `explicit'");
10776 explicitp = 0;
10779 if (RIDBIT_SETP (RID_MUTABLE, specbits))
10781 if (decl_context != FIELD || friendp)
10783 error ("non-member `%s' cannot be declared `mutable'", name);
10784 RIDBIT_RESET (RID_MUTABLE, specbits);
10786 else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
10788 error ("non-object member `%s' cannot be declared `mutable'", name);
10789 RIDBIT_RESET (RID_MUTABLE, specbits);
10791 else if (TREE_CODE (type) == FUNCTION_TYPE
10792 || TREE_CODE (type) == METHOD_TYPE)
10794 error ("function `%s' cannot be declared `mutable'", name);
10795 RIDBIT_RESET (RID_MUTABLE, specbits);
10797 else if (staticp)
10799 error ("static `%s' cannot be declared `mutable'", name);
10800 RIDBIT_RESET (RID_MUTABLE, specbits);
10802 else if (type_quals & TYPE_QUAL_CONST)
10804 error ("const `%s' cannot be declared `mutable'", name);
10805 RIDBIT_RESET (RID_MUTABLE, specbits);
10809 if (declarator == NULL_TREE
10810 || TREE_CODE (declarator) == IDENTIFIER_NODE
10811 || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
10812 && (TREE_CODE (type) == FUNCTION_TYPE
10813 || TREE_CODE (type) == METHOD_TYPE)))
10814 /* OK */;
10815 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
10817 error ("template-id `%D' used as a declarator", declarator);
10818 declarator = dname;
10820 else
10821 /* Unexpected declarator format. */
10822 abort ();
10824 /* If this is declaring a typedef name, return a TYPE_DECL. */
10826 if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
10828 tree decl;
10830 /* Note that the grammar rejects storage classes
10831 in typenames, fields or parameters. */
10832 if (current_lang_name == lang_name_java)
10833 TYPE_FOR_JAVA (type) = 1;
10835 if (decl_context == FIELD)
10837 if (constructor_name_p (declarator, current_class_type))
10838 pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
10839 declarator);
10840 decl = build_lang_decl (TYPE_DECL, declarator, type);
10842 else
10844 decl = build_decl (TYPE_DECL, declarator, type);
10845 if (in_namespace || ctype)
10846 error ("%Jtypedef name may not be a nested-name-specifier", decl);
10847 if (!current_function_decl)
10848 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10851 /* If the user declares "typedef struct {...} foo" then the
10852 struct will have an anonymous name. Fill that name in now.
10853 Nothing can refer to it, so nothing needs know about the name
10854 change. */
10855 if (type != error_mark_node
10856 && declarator
10857 && TYPE_NAME (type)
10858 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10859 && TYPE_ANONYMOUS_P (type)
10860 /* Don't do this if there are attributes. */
10861 && (!attrlist || !*attrlist)
10862 && cp_type_quals (type) == TYPE_UNQUALIFIED)
10864 tree oldname = TYPE_NAME (type);
10865 tree t;
10867 /* Replace the anonymous name with the real name everywhere. */
10868 lookup_tag_reverse (type, declarator);
10869 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10870 if (TYPE_NAME (t) == oldname)
10871 TYPE_NAME (t) = decl;
10873 if (TYPE_LANG_SPECIFIC (type))
10874 TYPE_WAS_ANONYMOUS (type) = 1;
10876 /* If this is a typedef within a template class, the nested
10877 type is a (non-primary) template. The name for the
10878 template needs updating as well. */
10879 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10880 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10881 = TYPE_IDENTIFIER (type);
10883 /* FIXME remangle member functions; member functions of a
10884 type with external linkage have external linkage. */
10887 if (quals)
10889 if (ctype == NULL_TREE)
10891 if (TREE_CODE (type) != METHOD_TYPE)
10892 error ("%Jinvalid type qualifier for non-member function type",
10893 decl);
10894 else
10895 ctype = TYPE_METHOD_BASETYPE (type);
10897 if (ctype != NULL_TREE)
10898 grok_method_quals (ctype, decl, quals);
10901 if (RIDBIT_SETP (RID_SIGNED, specbits)
10902 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10903 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10905 bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
10906 inlinep, friendp, raises != NULL_TREE);
10908 return decl;
10911 /* Detect the case of an array type of unspecified size
10912 which came, as such, direct from a typedef name.
10913 We must copy the type, so that the array's domain can be
10914 individually set by the object's initializer. */
10916 if (type && typedef_type
10917 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
10918 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
10919 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
10921 /* Detect where we're using a typedef of function type to declare a
10922 function. last_function_parms will not be set, so we must create
10923 it now. */
10925 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
10927 tree decls = NULL_TREE;
10928 tree args;
10930 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
10932 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
10934 TREE_CHAIN (decl) = decls;
10935 decls = decl;
10938 last_function_parms = nreverse (decls);
10941 /* If this is a type name (such as, in a cast or sizeof),
10942 compute the type and return it now. */
10944 if (decl_context == TYPENAME)
10946 /* Note that the grammar rejects storage classes
10947 in typenames, fields or parameters. */
10948 if (type_quals != TYPE_UNQUALIFIED)
10949 type_quals = TYPE_UNQUALIFIED;
10951 /* Special case: "friend class foo" looks like a TYPENAME context. */
10952 if (friendp)
10954 if (type_quals != TYPE_UNQUALIFIED)
10956 error ("type qualifiers specified for friend class declaration");
10957 type_quals = TYPE_UNQUALIFIED;
10959 if (inlinep)
10961 error ("`inline' specified for friend class declaration");
10962 inlinep = 0;
10965 if (!current_aggr)
10967 /* Don't allow friend declaration without a class-key. */
10968 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10969 pedwarn ("template parameters cannot be friends");
10970 else if (TREE_CODE (type) == TYPENAME_TYPE)
10971 pedwarn ("friend declaration requires class-key, "
10972 "i.e. `friend class %T::%D'",
10973 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
10974 else
10975 pedwarn ("friend declaration requires class-key, "
10976 "i.e. `friend %#T'",
10977 type);
10980 /* Only try to do this stuff if we didn't already give up. */
10981 if (type != integer_type_node)
10983 /* A friendly class? */
10984 if (current_class_type)
10985 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
10986 /*complain=*/true);
10987 else
10988 error ("trying to make class `%T' a friend of global scope",
10989 type);
10991 type = void_type_node;
10994 else if (quals)
10996 if (ctype == NULL_TREE)
10998 if (TREE_CODE (type) != METHOD_TYPE)
10999 error ("invalid qualifiers on non-member function type");
11000 else
11001 ctype = TYPE_METHOD_BASETYPE (type);
11003 if (ctype)
11005 tree dummy = build_decl (TYPE_DECL, declarator, type);
11006 grok_method_quals (ctype, dummy, quals);
11007 type = TREE_TYPE (dummy);
11011 return type;
11013 else if (declarator == NULL_TREE && decl_context != PARM
11014 && decl_context != CATCHPARM
11015 && TREE_CODE (type) != UNION_TYPE
11016 && ! bitfield)
11018 error ("abstract declarator `%T' used as declaration", type);
11019 declarator = make_anon_name ();
11022 /* `void' at top level (not within pointer)
11023 is allowed only in typedefs or type names.
11024 We don't complain about parms either, but that is because
11025 a better error message can be made later. */
11027 if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11029 if (! declarator)
11030 error ("unnamed variable or field declared void");
11031 else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11033 if (IDENTIFIER_OPNAME_P (declarator))
11034 abort ();
11035 else
11036 error ("variable or field `%s' declared void", name);
11038 else
11039 error ("variable or field declared void");
11040 type = integer_type_node;
11043 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11044 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
11046 if (decl_context == PARM || decl_context == CATCHPARM)
11048 if (ctype || in_namespace)
11049 error ("cannot use `::' in parameter declaration");
11051 /* A parameter declared as an array of T is really a pointer to T.
11052 One declared as a function is really a pointer to a function.
11053 One declared as a member is really a pointer to member. */
11055 if (TREE_CODE (type) == ARRAY_TYPE)
11057 /* Transfer const-ness of array into that of type pointed to. */
11058 type = build_pointer_type (TREE_TYPE (type));
11059 type_quals = TYPE_UNQUALIFIED;
11061 else if (TREE_CODE (type) == FUNCTION_TYPE)
11062 type = build_pointer_type (type);
11066 register tree decl;
11068 if (decl_context == PARM)
11070 decl = cp_build_parm_decl (declarator, type);
11072 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11073 inlinep, friendp, raises != NULL_TREE);
11075 else if (decl_context == FIELD)
11077 /* The C99 flexible array extension. */
11078 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
11079 && TYPE_DOMAIN (type) == NULL_TREE)
11081 tree itype = compute_array_index_type (dname, integer_zero_node);
11082 type = build_cplus_array_type (TREE_TYPE (type), itype);
11085 if (type == error_mark_node)
11087 /* Happens when declaring arrays of sizes which
11088 are error_mark_node, for example. */
11089 decl = NULL_TREE;
11091 else if (in_namespace && !friendp)
11093 /* Something like struct S { int N::j; }; */
11094 error ("invalid use of `::'");
11095 decl = NULL_TREE;
11097 else if (TREE_CODE (type) == FUNCTION_TYPE)
11099 int publicp = 0;
11100 tree function_context;
11102 /* We catch the others as conflicts with the builtin
11103 typedefs. */
11104 if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11106 error ("function `%D' cannot be declared friend",
11107 declarator);
11108 friendp = 0;
11111 if (friendp == 0)
11113 if (ctype == NULL_TREE)
11114 ctype = current_class_type;
11116 if (ctype == NULL_TREE)
11118 error ("can't make `%D' into a method -- not in a class",
11119 declarator);
11120 return void_type_node;
11123 /* ``A union may [ ... ] not [ have ] virtual functions.''
11124 ARM 9.5 */
11125 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11127 error ("function `%D' declared virtual inside a union",
11128 declarator);
11129 return void_type_node;
11132 if (declarator == ansi_opname (NEW_EXPR)
11133 || declarator == ansi_opname (VEC_NEW_EXPR)
11134 || declarator == ansi_opname (DELETE_EXPR)
11135 || declarator == ansi_opname (VEC_DELETE_EXPR))
11137 if (virtualp)
11139 error ("`%D' cannot be declared virtual, since it is always static",
11140 declarator);
11141 virtualp = 0;
11144 else if (staticp < 2)
11145 type = build_method_type_directly (ctype,
11146 TREE_TYPE (type),
11147 TYPE_ARG_TYPES (type));
11150 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11151 function_context = (ctype != NULL_TREE) ?
11152 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11153 publicp = (! friendp || ! staticp)
11154 && function_context == NULL_TREE;
11155 decl = grokfndecl (ctype, type,
11156 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11157 ? declarator : dname,
11158 declarator,
11159 virtualp, flags, quals, raises,
11160 friendp ? -1 : 0, friendp, publicp, inlinep,
11161 funcdef_flag, template_count, in_namespace);
11162 if (decl == NULL_TREE)
11163 return decl;
11164 #if 0
11165 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11166 /* The decl and setting of decl_attr is also turned off. */
11167 decl = build_decl_attribute_variant (decl, decl_attr);
11168 #endif
11170 /* [class.conv.ctor]
11172 A constructor declared without the function-specifier
11173 explicit that can be called with a single parameter
11174 specifies a conversion from the type of its first
11175 parameter to the type of its class. Such a constructor
11176 is called a converting constructor. */
11177 if (explicitp == 2)
11178 DECL_NONCONVERTING_P (decl) = 1;
11179 else if (DECL_CONSTRUCTOR_P (decl))
11181 /* The constructor can be called with exactly one
11182 parameter if there is at least one parameter, and
11183 any subsequent parameters have default arguments.
11184 Ignore any compiler-added parms. */
11185 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11187 if (arg_types == void_list_node
11188 || (arg_types
11189 && TREE_CHAIN (arg_types)
11190 && TREE_CHAIN (arg_types) != void_list_node
11191 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11192 DECL_NONCONVERTING_P (decl) = 1;
11195 else if (TREE_CODE (type) == METHOD_TYPE)
11197 /* We only get here for friend declarations of
11198 members of other classes. */
11199 /* All method decls are public, so tell grokfndecl to set
11200 TREE_PUBLIC, also. */
11201 decl = grokfndecl (ctype, type,
11202 TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11203 ? declarator : dname,
11204 declarator,
11205 virtualp, flags, quals, raises,
11206 friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11207 template_count, in_namespace);
11208 if (decl == NULL_TREE)
11209 return NULL_TREE;
11211 else if (!staticp && ! processing_template_decl
11212 && !COMPLETE_TYPE_P (complete_type (type))
11213 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11215 if (declarator)
11216 error ("field `%D' has incomplete type", declarator);
11217 else
11218 error ("name `%T' has incomplete type", type);
11220 /* If we're instantiating a template, tell them which
11221 instantiation made the field's type be incomplete. */
11222 if (current_class_type
11223 && TYPE_NAME (current_class_type)
11224 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11225 && declspecs && TREE_VALUE (declspecs)
11226 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11227 error (" in instantiation of template `%T'",
11228 current_class_type);
11230 type = error_mark_node;
11231 decl = NULL_TREE;
11233 else
11235 if (friendp)
11237 error ("`%s' is neither function nor member function; cannot be declared friend",
11238 IDENTIFIER_POINTER (declarator));
11239 friendp = 0;
11241 decl = NULL_TREE;
11244 if (friendp)
11246 /* Friends are treated specially. */
11247 if (ctype == current_class_type)
11248 warning ("member functions are implicitly friends of their class");
11249 else if (decl && DECL_NAME (decl))
11251 if (template_class_depth (current_class_type) == 0)
11253 decl = check_explicit_specialization
11254 (declarator, decl, template_count,
11255 2 * (funcdef_flag != 0) + 4);
11256 if (decl == error_mark_node)
11257 return error_mark_node;
11260 decl = do_friend (ctype, declarator, decl,
11261 last_function_parms, *attrlist,
11262 flags, quals, funcdef_flag);
11263 return decl;
11265 else
11266 return void_type_node;
11269 /* Structure field. It may not be a function, except for C++ */
11271 if (decl == NULL_TREE)
11273 if (initialized)
11275 if (!staticp)
11277 /* An attempt is being made to initialize a non-static
11278 member. But, from [class.mem]:
11280 4 A member-declarator can contain a
11281 constant-initializer only if it declares a static
11282 member (_class.static_) of integral or enumeration
11283 type, see _class.static.data_.
11285 This used to be relatively common practice, but
11286 the rest of the compiler does not correctly
11287 handle the initialization unless the member is
11288 static so we make it static below. */
11289 pedwarn ("ISO C++ forbids initialization of member `%D'",
11290 declarator);
11291 pedwarn ("making `%D' static", declarator);
11292 staticp = 1;
11295 if (uses_template_parms (type))
11296 /* We'll check at instantiation time. */
11298 else if (check_static_variable_definition (declarator,
11299 type))
11300 /* If we just return the declaration, crashes
11301 will sometimes occur. We therefore return
11302 void_type_node, as if this was a friend
11303 declaration, to cause callers to completely
11304 ignore this declaration. */
11305 return void_type_node;
11308 if (staticp)
11310 /* [class.mem] forbids static data members with the
11311 same name as the enclosing class. Non-static data
11312 members are checked in check_field_decls. */
11313 if (constructor_name_p (declarator, current_class_type))
11314 pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11315 declarator);
11317 /* C++ allows static class members. All other work
11318 for this is done by grokfield. */
11319 decl = build_lang_decl (VAR_DECL, declarator, type);
11320 TREE_STATIC (decl) = 1;
11321 /* In class context, 'static' means public access. */
11322 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11324 else
11326 decl = build_decl (FIELD_DECL, declarator, type);
11327 DECL_NONADDRESSABLE_P (decl) = bitfield;
11328 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11330 DECL_MUTABLE_P (decl) = 1;
11331 RIDBIT_RESET (RID_MUTABLE, specbits);
11335 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11336 inlinep, friendp, raises != NULL_TREE);
11339 else if (TREE_CODE (type) == FUNCTION_TYPE
11340 || TREE_CODE (type) == METHOD_TYPE)
11342 tree original_name;
11343 int publicp = 0;
11345 if (! declarator)
11346 return NULL_TREE;
11348 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11349 original_name = dname;
11350 else
11351 original_name = declarator;
11353 if (RIDBIT_SETP (RID_AUTO, specbits))
11354 error ("storage class `auto' invalid for function `%s'", name);
11355 else if (RIDBIT_SETP (RID_REGISTER, specbits))
11356 error ("storage class `register' invalid for function `%s'", name);
11357 else if (RIDBIT_SETP (RID_THREAD, specbits))
11358 error ("storage class `__thread' invalid for function `%s'", name);
11360 /* Function declaration not at top level.
11361 Storage classes other than `extern' are not allowed
11362 and `extern' makes no difference. */
11363 if (! toplevel_bindings_p ()
11364 && (RIDBIT_SETP (RID_STATIC, specbits)
11365 || RIDBIT_SETP (RID_INLINE, specbits))
11366 && pedantic)
11368 if (RIDBIT_SETP (RID_STATIC, specbits))
11369 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11370 else
11371 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11374 if (ctype == NULL_TREE)
11376 if (virtualp)
11378 error ("virtual non-class function `%s'", name);
11379 virtualp = 0;
11382 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11383 type = build_method_type_directly (ctype,
11384 TREE_TYPE (type),
11385 TYPE_ARG_TYPES (type));
11387 /* Record presence of `static'. */
11388 publicp = (ctype != NULL_TREE
11389 || RIDBIT_SETP (RID_EXTERN, specbits)
11390 || !RIDBIT_SETP (RID_STATIC, specbits));
11392 decl = grokfndecl (ctype, type, original_name, declarator,
11393 virtualp, flags, quals, raises,
11394 1, friendp,
11395 publicp, inlinep, funcdef_flag,
11396 template_count, in_namespace);
11397 if (decl == NULL_TREE)
11398 return NULL_TREE;
11400 if (staticp == 1)
11402 int invalid_static = 0;
11404 /* Don't allow a static member function in a class, and forbid
11405 declaring main to be static. */
11406 if (TREE_CODE (type) == METHOD_TYPE)
11408 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11409 invalid_static = 1;
11411 else if (current_function_decl)
11413 /* FIXME need arm citation */
11414 error ("cannot declare static function inside another function");
11415 invalid_static = 1;
11418 if (invalid_static)
11420 staticp = 0;
11421 RIDBIT_RESET (RID_STATIC, specbits);
11425 else
11427 /* It's a variable. */
11429 /* An uninitialized decl with `extern' is a reference. */
11430 decl = grokvardecl (type, declarator, &specbits,
11431 initialized,
11432 (type_quals & TYPE_QUAL_CONST) != 0,
11433 ctype ? ctype : in_namespace);
11434 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11435 inlinep, friendp, raises != NULL_TREE);
11437 if (ctype)
11439 DECL_CONTEXT (decl) = ctype;
11440 if (staticp == 1)
11442 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11443 staticp = 0;
11444 RIDBIT_RESET (RID_STATIC, specbits);
11446 if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11448 error ("static member `%D' declared `register'", decl);
11449 RIDBIT_RESET (RID_REGISTER, specbits);
11451 if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11453 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11454 decl);
11455 RIDBIT_RESET (RID_EXTERN, specbits);
11460 my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11462 /* Record `register' declaration for warnings on &
11463 and in case doing stupid register allocation. */
11465 if (RIDBIT_SETP (RID_REGISTER, specbits))
11466 DECL_REGISTER (decl) = 1;
11468 if (RIDBIT_SETP (RID_EXTERN, specbits))
11469 DECL_THIS_EXTERN (decl) = 1;
11471 if (RIDBIT_SETP (RID_STATIC, specbits))
11472 DECL_THIS_STATIC (decl) = 1;
11474 /* Record constancy and volatility. There's no need to do this
11475 when processing a template; we'll do this for the instantiated
11476 declaration based on the type of DECL. */
11477 if (!processing_template_decl)
11478 c_apply_type_quals_to_decl (type_quals, decl);
11480 return decl;
11484 /* Subroutine of start_function. Ensure that each of the parameter
11485 types (as listed in PARMS) is complete, as is required for a
11486 function definition. */
11488 static void
11489 require_complete_types_for_parms (tree parms)
11491 for (; parms; parms = TREE_CHAIN (parms))
11493 if (VOID_TYPE_P (TREE_TYPE (parms)))
11494 /* grokparms will have already issued an error */
11495 TREE_TYPE (parms) = error_mark_node;
11496 else if (complete_type_or_else (TREE_TYPE (parms), parms))
11498 layout_decl (parms, 0);
11499 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11501 else
11502 TREE_TYPE (parms) = error_mark_node;
11506 /* Returns nonzero if T is a local variable. */
11509 local_variable_p (tree t)
11511 if ((TREE_CODE (t) == VAR_DECL
11512 /* A VAR_DECL with a context that is a _TYPE is a static data
11513 member. */
11514 && !TYPE_P (CP_DECL_CONTEXT (t))
11515 /* Any other non-local variable must be at namespace scope. */
11516 && !DECL_NAMESPACE_SCOPE_P (t))
11517 || (TREE_CODE (t) == PARM_DECL))
11518 return 1;
11520 return 0;
11523 /* Returns nonzero if T is an automatic local variable or a label.
11524 (These are the declarations that need to be remapped when the code
11525 containing them is duplicated.) */
11528 nonstatic_local_decl_p (tree t)
11530 return ((local_variable_p (t) && !TREE_STATIC (t))
11531 || TREE_CODE (t) == LABEL_DECL
11532 || TREE_CODE (t) == RESULT_DECL);
11535 /* Like local_variable_p, but suitable for use as a tree-walking
11536 function. */
11538 static tree
11539 local_variable_p_walkfn (tree* tp,
11540 int* walk_subtrees ATTRIBUTE_UNUSED ,
11541 void* data ATTRIBUTE_UNUSED )
11543 return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11544 ? *tp : NULL_TREE);
11547 /* Check that ARG, which is a default-argument expression for a
11548 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11549 something goes wrong. DECL may also be a _TYPE node, rather than a
11550 DECL, if there is no DECL available. */
11552 tree
11553 check_default_argument (tree decl, tree arg)
11555 tree var;
11556 tree decl_type;
11558 if (TREE_CODE (arg) == DEFAULT_ARG)
11559 /* We get a DEFAULT_ARG when looking at an in-class declaration
11560 with a default argument. Ignore the argument for now; we'll
11561 deal with it after the class is complete. */
11562 return arg;
11564 if (processing_template_decl || uses_template_parms (arg))
11565 /* We don't do anything checking until instantiation-time. Note
11566 that there may be uninstantiated arguments even for an
11567 instantiated function, since default arguments are not
11568 instantiated until they are needed. */
11569 return arg;
11571 if (TYPE_P (decl))
11573 decl_type = decl;
11574 decl = NULL_TREE;
11576 else
11577 decl_type = TREE_TYPE (decl);
11579 if (arg == error_mark_node
11580 || decl == error_mark_node
11581 || TREE_TYPE (arg) == error_mark_node
11582 || decl_type == error_mark_node)
11583 /* Something already went wrong. There's no need to check
11584 further. */
11585 return error_mark_node;
11587 /* [dcl.fct.default]
11589 A default argument expression is implicitly converted to the
11590 parameter type. */
11591 if (!TREE_TYPE (arg)
11592 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11594 if (decl)
11595 error ("default argument for `%#D' has type `%T'",
11596 decl, TREE_TYPE (arg));
11597 else
11598 error ("default argument for parameter of type `%T' has type `%T'",
11599 decl_type, TREE_TYPE (arg));
11601 return error_mark_node;
11604 /* [dcl.fct.default]
11606 Local variables shall not be used in default argument
11607 expressions.
11609 The keyword `this' shall not be used in a default argument of a
11610 member function. */
11611 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11612 NULL);
11613 if (var)
11615 error ("default argument `%E' uses local variable `%D'",
11616 arg, var);
11617 return error_mark_node;
11620 /* All is well. */
11621 return arg;
11624 /* Decode the list of parameter types for a function type.
11625 Given the list of things declared inside the parens,
11626 return a list of types.
11628 We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11629 flag. If unset, we append void_list_node. A parmlist declared
11630 as `(void)' is accepted as the empty parmlist.
11632 Also set last_function_parms to the chain of PARM_DECLs. */
11634 static tree
11635 grokparms (tree first_parm)
11637 tree result = NULL_TREE;
11638 tree decls = NULL_TREE;
11639 int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11640 tree parm, chain;
11641 int any_error = 0;
11643 my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11645 for (parm = first_parm; parm != NULL_TREE; parm = chain)
11647 tree type = NULL_TREE;
11648 tree decl = TREE_VALUE (parm);
11649 tree init = TREE_PURPOSE (parm);
11650 tree specs, attrs;
11652 chain = TREE_CHAIN (parm);
11653 /* @@ weak defense against parse errors. */
11654 if (TREE_CODE (decl) != VOID_TYPE
11655 && TREE_CODE (decl) != TREE_LIST)
11657 /* Give various messages as the need arises. */
11658 if (TREE_CODE (decl) == STRING_CST)
11659 error ("invalid string constant `%E'", decl);
11660 else if (TREE_CODE (decl) == INTEGER_CST)
11661 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11662 continue;
11665 if (parm == void_list_node)
11666 break;
11668 split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
11669 decl = grokdeclarator (TREE_VALUE (decl), specs,
11670 PARM, init != NULL_TREE, &attrs);
11671 if (! decl || TREE_TYPE (decl) == error_mark_node)
11672 continue;
11674 if (attrs)
11675 cplus_decl_attributes (&decl, attrs, 0);
11677 type = TREE_TYPE (decl);
11678 if (VOID_TYPE_P (type))
11680 if (same_type_p (type, void_type_node)
11681 && !DECL_NAME (decl) && !result && !chain && !ellipsis)
11682 /* this is a parmlist of `(void)', which is ok. */
11683 break;
11684 cxx_incomplete_type_error (decl, type);
11685 /* It's not a good idea to actually create parameters of
11686 type `void'; other parts of the compiler assume that a
11687 void type terminates the parameter list. */
11688 type = error_mark_node;
11689 TREE_TYPE (decl) = error_mark_node;
11692 if (type != error_mark_node)
11694 /* Top-level qualifiers on the parameters are
11695 ignored for function types. */
11696 type = cp_build_qualified_type (type, 0);
11697 if (TREE_CODE (type) == METHOD_TYPE)
11699 error ("parameter `%D' invalidly declared method type", decl);
11700 type = build_pointer_type (type);
11701 TREE_TYPE (decl) = type;
11703 else if (abstract_virtuals_error (decl, type))
11704 any_error = 1; /* Seems like a good idea. */
11705 else if (POINTER_TYPE_P (type))
11707 /* [dcl.fct]/6, parameter types cannot contain pointers
11708 (references) to arrays of unknown bound. */
11709 tree t = TREE_TYPE (type);
11710 int ptr = TYPE_PTR_P (type);
11712 while (1)
11714 if (TYPE_PTR_P (t))
11715 ptr = 1;
11716 else if (TREE_CODE (t) != ARRAY_TYPE)
11717 break;
11718 else if (!TYPE_DOMAIN (t))
11719 break;
11720 t = TREE_TYPE (t);
11722 if (TREE_CODE (t) == ARRAY_TYPE)
11723 error ("parameter `%D' includes %s to array of unknown bound `%T'",
11724 decl, ptr ? "pointer" : "reference", t);
11727 if (!any_error && init)
11728 init = check_default_argument (decl, init);
11729 else
11730 init = NULL_TREE;
11733 TREE_CHAIN (decl) = decls;
11734 decls = decl;
11735 result = tree_cons (init, type, result);
11737 decls = nreverse (decls);
11738 result = nreverse (result);
11739 if (!ellipsis)
11740 result = chainon (result, void_list_node);
11741 last_function_parms = decls;
11743 return result;
11747 /* D is a constructor or overloaded `operator='.
11749 Let T be the class in which D is declared. Then, this function
11750 returns:
11752 -1 if D's is an ill-formed constructor or copy assignment operator
11753 whose first parameter is of type `T'.
11754 0 if D is not a copy constructor or copy assignment
11755 operator.
11756 1 if D is a copy constructor or copy assignment operator whose
11757 first parameter is a reference to const qualified T.
11758 2 if D is a copy constructor or copy assignment operator whose
11759 first parameter is a reference to non-const qualified T.
11761 This function can be used as a predicate. Positive values indicate
11762 a copy constructor and nonzero values indicate a copy assignment
11763 operator. */
11766 copy_fn_p (tree d)
11768 tree args;
11769 tree arg_type;
11770 int result = 1;
11772 my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
11774 if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
11775 /* Instantiations of template member functions are never copy
11776 functions. Note that member functions of templated classes are
11777 represented as template functions internally, and we must
11778 accept those as copy functions. */
11779 return 0;
11781 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11782 if (!args)
11783 return 0;
11785 arg_type = TREE_VALUE (args);
11787 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
11789 /* Pass by value copy assignment operator. */
11790 result = -1;
11792 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
11793 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
11795 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
11796 result = 2;
11798 else
11799 return 0;
11801 args = TREE_CHAIN (args);
11803 if (args && args != void_list_node && !TREE_PURPOSE (args))
11804 /* There are more non-optional args. */
11805 return 0;
11807 return result;
11810 /* Remember any special properties of member function DECL. */
11812 void grok_special_member_properties (tree decl)
11814 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
11815 ; /* Not special. */
11816 else if (DECL_CONSTRUCTOR_P (decl))
11818 int ctor = copy_fn_p (decl);
11820 if (ctor > 0)
11822 /* [class.copy]
11824 A non-template constructor for class X is a copy
11825 constructor if its first parameter is of type X&, const
11826 X&, volatile X& or const volatile X&, and either there
11827 are no other parameters or else all other parameters have
11828 default arguments. */
11829 TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
11830 if (ctor > 1)
11831 TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
11833 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
11834 TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
11836 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
11838 /* [class.copy]
11840 A non-template assignment operator for class X is a copy
11841 assignment operator if its parameter is of type X, X&, const
11842 X&, volatile X& or const volatile X&. */
11844 int assop = copy_fn_p (decl);
11846 if (assop)
11848 TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
11849 if (assop != 1)
11850 TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
11851 if (DECL_PURE_VIRTUAL_P (decl))
11852 TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
11857 /* Check a constructor DECL has the correct form. Complains
11858 if the class has a constructor of the form X(X). */
11861 grok_ctor_properties (tree ctype, tree decl)
11863 int ctor_parm = copy_fn_p (decl);
11865 if (ctor_parm < 0)
11867 /* [class.copy]
11869 A declaration of a constructor for a class X is ill-formed if
11870 its first parameter is of type (optionally cv-qualified) X
11871 and either there are no other parameters or else all other
11872 parameters have default arguments.
11874 We *don't* complain about member template instantiations that
11875 have this form, though; they can occur as we try to decide
11876 what constructor to use during overload resolution. Since
11877 overload resolution will never prefer such a constructor to
11878 the non-template copy constructor (which is either explicitly
11879 or implicitly defined), there's no need to worry about their
11880 existence. Theoretically, they should never even be
11881 instantiated, but that's hard to forestall. */
11882 error ("invalid constructor; you probably meant `%T (const %T&)'",
11883 ctype, ctype);
11884 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
11885 return 0;
11888 return 1;
11891 /* An operator with this code is unary, but can also be binary. */
11893 static int
11894 ambi_op_p (enum tree_code code)
11896 return (code == INDIRECT_REF
11897 || code == ADDR_EXPR
11898 || code == CONVERT_EXPR
11899 || code == NEGATE_EXPR
11900 || code == PREINCREMENT_EXPR
11901 || code == PREDECREMENT_EXPR);
11904 /* An operator with this name can only be unary. */
11906 static int
11907 unary_op_p (enum tree_code code)
11909 return (code == TRUTH_NOT_EXPR
11910 || code == BIT_NOT_EXPR
11911 || code == COMPONENT_REF
11912 || code == TYPE_EXPR);
11915 /* DECL is a declaration for an overloaded operator. Returns true if
11916 the declaration is valid; false otherwise. If COMPLAIN is true,
11917 errors are issued for invalid declarations. */
11919 bool
11920 grok_op_properties (tree decl, int friendp, bool complain)
11922 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11923 tree argtype;
11924 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
11925 tree name = DECL_NAME (decl);
11926 enum tree_code operator_code;
11927 int arity;
11928 bool ok;
11930 /* Assume that the declaration is valid. */
11931 ok = true;
11933 /* Count the number of arguments. */
11934 for (argtype = argtypes, arity = 0;
11935 argtype && argtype != void_list_node;
11936 argtype = TREE_CHAIN (argtype))
11937 ++arity;
11939 if (current_class_type == NULL_TREE)
11940 friendp = 1;
11942 if (DECL_CONV_FN_P (decl))
11943 operator_code = TYPE_EXPR;
11944 else
11947 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
11948 if (ansi_opname (CODE) == name) \
11950 operator_code = (CODE); \
11951 break; \
11953 else if (ansi_assopname (CODE) == name) \
11955 operator_code = (CODE); \
11956 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
11957 break; \
11960 #include "operators.def"
11961 #undef DEF_OPERATOR
11963 abort ();
11965 while (0);
11966 my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
11967 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
11969 if (! friendp)
11971 switch (operator_code)
11973 case NEW_EXPR:
11974 TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
11975 break;
11977 case DELETE_EXPR:
11978 TYPE_GETS_DELETE (current_class_type) |= 1;
11979 break;
11981 case VEC_NEW_EXPR:
11982 TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
11983 break;
11985 case VEC_DELETE_EXPR:
11986 TYPE_GETS_DELETE (current_class_type) |= 2;
11987 break;
11989 default:
11990 break;
11994 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
11996 /* When the compiler encounters the definition of A::operator new, it
11997 doesn't look at the class declaration to find out if it's static. */
11998 if (methodp)
11999 revert_static_member_fn (decl);
12001 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12003 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12005 if (methodp)
12006 revert_static_member_fn (decl);
12008 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12010 else
12012 /* An operator function must either be a non-static member function
12013 or have at least one parameter of a class, a reference to a class,
12014 an enumeration, or a reference to an enumeration. 13.4.0.6 */
12015 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12017 if (operator_code == TYPE_EXPR
12018 || operator_code == CALL_EXPR
12019 || operator_code == COMPONENT_REF
12020 || operator_code == ARRAY_REF
12021 || operator_code == NOP_EXPR)
12022 error ("`%D' must be a nonstatic member function", decl);
12023 else
12025 tree p;
12027 if (DECL_STATIC_FUNCTION_P (decl))
12028 error ("`%D' must be either a non-static member function or a non-member function", decl);
12030 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
12032 tree arg = non_reference (TREE_VALUE (p));
12033 /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used
12034 because these checks are performed even on
12035 template functions. */
12036 if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE)
12037 break;
12040 if (!p || p == void_list_node)
12042 if (!complain)
12043 return false;
12045 error ("`%D' must have an argument of class or "
12046 "enumerated type",
12047 decl);
12048 ok = false;
12053 /* There are no restrictions on the arguments to an overloaded
12054 "operator ()". */
12055 if (operator_code == CALL_EXPR)
12056 return ok;
12058 if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12060 tree t = TREE_TYPE (name);
12061 if (! friendp)
12063 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12064 const char *what = 0;
12066 if (ref)
12067 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12069 if (TREE_CODE (t) == VOID_TYPE)
12070 what = "void";
12071 else if (t == current_class_type)
12072 what = "the same type";
12073 /* Don't force t to be complete here. */
12074 else if (IS_AGGR_TYPE (t)
12075 && COMPLETE_TYPE_P (t)
12076 && DERIVED_FROM_P (t, current_class_type))
12077 what = "a base class";
12079 if (what && warn_conversion)
12080 warning ("conversion to %s%s will never use a type conversion operator",
12081 ref ? "a reference to " : "", what);
12084 if (operator_code == COND_EXPR)
12086 /* 13.4.0.3 */
12087 error ("ISO C++ prohibits overloading operator ?:");
12089 else if (ambi_op_p (operator_code))
12091 if (arity == 1)
12092 /* We pick the one-argument operator codes by default, so
12093 we don't have to change anything. */
12095 else if (arity == 2)
12097 /* If we thought this was a unary operator, we now know
12098 it to be a binary operator. */
12099 switch (operator_code)
12101 case INDIRECT_REF:
12102 operator_code = MULT_EXPR;
12103 break;
12105 case ADDR_EXPR:
12106 operator_code = BIT_AND_EXPR;
12107 break;
12109 case CONVERT_EXPR:
12110 operator_code = PLUS_EXPR;
12111 break;
12113 case NEGATE_EXPR:
12114 operator_code = MINUS_EXPR;
12115 break;
12117 case PREINCREMENT_EXPR:
12118 operator_code = POSTINCREMENT_EXPR;
12119 break;
12121 case PREDECREMENT_EXPR:
12122 operator_code = POSTDECREMENT_EXPR;
12123 break;
12125 default:
12126 abort ();
12129 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12131 if ((operator_code == POSTINCREMENT_EXPR
12132 || operator_code == POSTDECREMENT_EXPR)
12133 && ! processing_template_decl
12134 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12136 if (methodp)
12137 error ("postfix `%D' must take `int' as its argument",
12138 decl);
12139 else
12140 error
12141 ("postfix `%D' must take `int' as its second argument",
12142 decl);
12145 else
12147 if (methodp)
12148 error ("`%D' must take either zero or one argument", decl);
12149 else
12150 error ("`%D' must take either one or two arguments", decl);
12153 /* More Effective C++ rule 6. */
12154 if (warn_ecpp
12155 && (operator_code == POSTINCREMENT_EXPR
12156 || operator_code == POSTDECREMENT_EXPR
12157 || operator_code == PREINCREMENT_EXPR
12158 || operator_code == PREDECREMENT_EXPR))
12160 tree arg = TREE_VALUE (argtypes);
12161 tree ret = TREE_TYPE (TREE_TYPE (decl));
12162 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12163 arg = TREE_TYPE (arg);
12164 arg = TYPE_MAIN_VARIANT (arg);
12165 if (operator_code == PREINCREMENT_EXPR
12166 || operator_code == PREDECREMENT_EXPR)
12168 if (TREE_CODE (ret) != REFERENCE_TYPE
12169 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12170 arg))
12171 warning ("prefix `%D' should return `%T'", decl,
12172 build_reference_type (arg));
12174 else
12176 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12177 warning ("postfix `%D' should return `%T'", decl, arg);
12181 else if (unary_op_p (operator_code))
12183 if (arity != 1)
12185 if (methodp)
12186 error ("`%D' must take `void'", decl);
12187 else
12188 error ("`%D' must take exactly one argument", decl);
12191 else /* if (binary_op_p (operator_code)) */
12193 if (arity != 2)
12195 if (methodp)
12196 error ("`%D' must take exactly one argument", decl);
12197 else
12198 error ("`%D' must take exactly two arguments", decl);
12201 /* More Effective C++ rule 7. */
12202 if (warn_ecpp
12203 && (operator_code == TRUTH_ANDIF_EXPR
12204 || operator_code == TRUTH_ORIF_EXPR
12205 || operator_code == COMPOUND_EXPR))
12206 warning ("user-defined `%D' always evaluates both arguments",
12207 decl);
12210 /* Effective C++ rule 23. */
12211 if (warn_ecpp
12212 && arity == 2
12213 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12214 && (operator_code == PLUS_EXPR
12215 || operator_code == MINUS_EXPR
12216 || operator_code == TRUNC_DIV_EXPR
12217 || operator_code == MULT_EXPR
12218 || operator_code == TRUNC_MOD_EXPR)
12219 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12220 warning ("`%D' should return by value", decl);
12222 /* [over.oper]/8 */
12223 for (; argtypes && argtypes != void_list_node;
12224 argtypes = TREE_CHAIN (argtypes))
12225 if (TREE_PURPOSE (argtypes))
12227 TREE_PURPOSE (argtypes) = NULL_TREE;
12228 if (operator_code == POSTINCREMENT_EXPR
12229 || operator_code == POSTDECREMENT_EXPR)
12231 if (pedantic)
12232 pedwarn ("`%D' cannot have default arguments", decl);
12234 else
12235 error ("`%D' cannot have default arguments", decl);
12240 return ok;
12243 static const char *
12244 tag_name (enum tag_types code)
12246 switch (code)
12248 case record_type:
12249 return "struct";
12250 case class_type:
12251 return "class";
12252 case union_type:
12253 return "union ";
12254 case enum_type:
12255 return "enum";
12256 default:
12257 abort ();
12261 /* Name lookup in an elaborated-type-specifier (after the keyword
12262 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
12263 elaborated-type-specifier is invalid, issue a diagnostic and return
12264 error_mark_node; otherwise, return the *_TYPE to which it referred.
12265 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
12267 tree
12268 check_elaborated_type_specifier (enum tag_types tag_code,
12269 tree decl,
12270 bool allow_template_p)
12272 tree type;
12274 /* In the case of:
12276 struct S { struct S *p; };
12278 name lookup will find the TYPE_DECL for the implicit "S::S"
12279 typedef. Adjust for that here. */
12280 if (DECL_SELF_REFERENCE_P (decl))
12281 decl = TYPE_NAME (TREE_TYPE (decl));
12283 type = TREE_TYPE (decl);
12285 /* [dcl.type.elab]
12287 If the identifier resolves to a typedef-name or a template
12288 type-parameter, the elaborated-type-specifier is ill-formed.
12290 In other words, the only legitimate declaration to use in the
12291 elaborated type specifier is the implicit typedef created when
12292 the type is declared. */
12293 if (!DECL_IMPLICIT_TYPEDEF_P (decl))
12295 error ("using typedef-name `%D' after `%s'", decl, tag_name (tag_code));
12296 return IS_AGGR_TYPE (type) ? type : error_mark_node;
12299 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12301 error ("using template type parameter `%T' after `%s'",
12302 type, tag_name (tag_code));
12303 return error_mark_node;
12305 else if (TREE_CODE (type) != RECORD_TYPE
12306 && TREE_CODE (type) != UNION_TYPE
12307 && tag_code != enum_type)
12309 error ("`%T' referred to as `%s'", type, tag_name (tag_code));
12310 return error_mark_node;
12312 else if (TREE_CODE (type) != ENUMERAL_TYPE
12313 && tag_code == enum_type)
12315 error ("`%T' referred to as enum", type);
12316 return error_mark_node;
12318 else if (!allow_template_p
12319 && TREE_CODE (type) == RECORD_TYPE
12320 && CLASSTYPE_IS_TEMPLATE (type))
12322 /* If a class template appears as elaborated type specifier
12323 without a template header such as:
12325 template <class T> class C {};
12326 void f(class C); // No template header here
12328 then the required template argument is missing. */
12330 error ("template argument required for `%s %T'",
12331 tag_name (tag_code),
12332 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
12333 return error_mark_node;
12336 return type;
12339 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
12340 Define the tag as a forward-reference if it is not defined.
12342 If a declaration is given, process it here, and report an error if
12343 multiple declarations are not identical. ATTRIBUTE is the attribute
12344 appeared in this declaration.
12346 GLOBALIZE is false when this is also a definition. Only look in
12347 the current frame for the name (since C++ allows new names in any
12348 scope.)
12350 TEMPLATE_HEADER_P is true when this declaration is preceded by
12351 a set of template parameters. */
12353 tree
12354 xref_tag (enum tag_types tag_code, tree name, tree attributes,
12355 bool globalize, bool template_header_p)
12357 enum tree_code code;
12358 register tree t;
12359 struct cp_binding_level *b = current_binding_level;
12360 tree context = NULL_TREE;
12362 timevar_push (TV_NAME_LOOKUP);
12364 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
12366 switch (tag_code)
12368 case record_type:
12369 case class_type:
12370 code = RECORD_TYPE;
12371 break;
12372 case union_type:
12373 code = UNION_TYPE;
12374 break;
12375 case enum_type:
12376 code = ENUMERAL_TYPE;
12377 break;
12378 default:
12379 abort ();
12382 if (! globalize)
12384 /* If we know we are defining this tag, only look it up in
12385 this scope and don't try to find it as a type. */
12386 t = lookup_tag (code, name, b, 1);
12388 else
12390 tree decl = lookup_name (name, 1);
12392 if (decl && DECL_CLASS_TEMPLATE_P (decl))
12393 decl = DECL_TEMPLATE_RESULT (decl);
12395 if (decl && TREE_CODE (decl) == TYPE_DECL)
12397 /* Two cases we need to consider when deciding if a class
12398 template is allowed as an elaborated type specifier:
12399 1. It is a self reference to its own class.
12400 2. It comes with a template header.
12402 For example:
12404 template <class T> class C {
12405 class C *c1; // DECL_SELF_REFERENCE_P is true
12406 class D;
12408 template <class U> class C; // template_header_p is true
12409 template <class T> class C<T>::D {
12410 class C *c2; // DECL_SELF_REFERENCE_P is true
12411 }; */
12413 t = check_elaborated_type_specifier (tag_code,
12414 decl,
12415 template_header_p
12416 | DECL_SELF_REFERENCE_P (decl));
12417 if (t == error_mark_node)
12418 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12420 else
12421 t = NULL_TREE;
12423 if (t && current_class_type
12424 && template_class_depth (current_class_type)
12425 && template_header_p)
12427 /* Since GLOBALIZE is nonzero, we are not looking at a
12428 definition of this tag. Since, in addition, we are currently
12429 processing a (member) template declaration of a template
12430 class, we must be very careful; consider:
12432 template <class X>
12433 struct S1
12435 template <class U>
12436 struct S2
12437 { template <class V>
12438 friend struct S1; };
12440 Here, the S2::S1 declaration should not be confused with the
12441 outer declaration. In particular, the inner version should
12442 have a template parameter of level 2, not level 1. This
12443 would be particularly important if the member declaration
12444 were instead:
12446 template <class V = U> friend struct S1;
12448 say, when we should tsubst into `U' when instantiating
12449 S2. On the other hand, when presented with:
12451 template <class T>
12452 struct S1 {
12453 template <class U>
12454 struct S2 {};
12455 template <class U>
12456 friend struct S2;
12459 we must find the inner binding eventually. We
12460 accomplish this by making sure that the new type we
12461 create to represent this declaration has the right
12462 TYPE_CONTEXT. */
12463 context = TYPE_CONTEXT (t);
12464 t = NULL_TREE;
12468 if (! t)
12470 /* If no such tag is yet defined, create a forward-reference node
12471 and record it as the "definition".
12472 When a real declaration of this type is found,
12473 the forward-reference will be altered into a real type. */
12474 if (code == ENUMERAL_TYPE)
12476 error ("use of enum `%#D' without previous declaration", name);
12478 t = make_node (ENUMERAL_TYPE);
12480 /* Give the type a default layout like unsigned int
12481 to avoid crashing if it does not get defined. */
12482 TYPE_MODE (t) = TYPE_MODE (unsigned_type_node);
12483 TYPE_ALIGN (t) = TYPE_ALIGN (unsigned_type_node);
12484 TYPE_USER_ALIGN (t) = 0;
12485 TREE_UNSIGNED (t) = 1;
12486 TYPE_PRECISION (t) = TYPE_PRECISION (unsigned_type_node);
12487 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (unsigned_type_node);
12488 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (unsigned_type_node);
12490 /* Enable us to recognize when a type is created in class context.
12491 To do nested classes correctly, this should probably be cleared
12492 out when we leave this classes scope. Currently this in only
12493 done in `start_enum'. */
12495 pushtag (name, t, globalize);
12497 else
12499 t = make_aggr_type (code);
12500 TYPE_CONTEXT (t) = context;
12501 pushtag (name, t, globalize);
12504 else
12506 if (!globalize && processing_template_decl && IS_AGGR_TYPE (t))
12507 redeclare_class_template (t, current_template_parms);
12510 TYPE_ATTRIBUTES (t) = attributes;
12512 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
12515 tree
12516 xref_tag_from_type (tree old, tree id, int globalize)
12518 enum tag_types tag_kind;
12520 if (TREE_CODE (old) == RECORD_TYPE)
12521 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12522 else
12523 tag_kind = union_type;
12525 if (id == NULL_TREE)
12526 id = TYPE_IDENTIFIER (old);
12528 return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize, false);
12531 /* REF is a type (named NAME), for which we have just seen some
12532 baseclasses. BASE_LIST is a list of those baseclasses; the
12533 TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12534 the base-class. TREE_VIA_VIRTUAL indicates virtual
12535 inheritance. CODE_TYPE_NODE indicates whether REF is a class,
12536 struct, or union. */
12538 void
12539 xref_basetypes (tree ref, tree base_list)
12541 /* In the declaration `A : X, Y, ... Z' we mark all the types
12542 (A, X, Y, ..., Z) so we can check for duplicates. */
12543 tree *basep;
12545 int i;
12546 enum tag_types tag_code;
12548 if (ref == error_mark_node)
12549 return;
12551 if (TREE_CODE (ref) == UNION_TYPE)
12553 error ("derived union `%T' invalid", ref);
12554 return;
12557 tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12559 /* First, make sure that any templates in base-classes are
12560 instantiated. This ensures that if we call ourselves recursively
12561 we do not get confused about which classes are marked and which
12562 are not. */
12563 basep = &base_list;
12564 while (*basep)
12566 tree basetype = TREE_VALUE (*basep);
12567 if (!(processing_template_decl && uses_template_parms (basetype))
12568 && !complete_type_or_else (basetype, NULL))
12569 /* An incomplete type. Remove it from the list. */
12570 *basep = TREE_CHAIN (*basep);
12571 else
12572 basep = &TREE_CHAIN (*basep);
12575 SET_CLASSTYPE_MARKED (ref);
12576 i = list_length (base_list);
12577 if (i)
12579 tree binfo = TYPE_BINFO (ref);
12580 tree binfos = make_tree_vec (i);
12581 tree accesses = make_tree_vec (i);
12583 BINFO_BASETYPES (binfo) = binfos;
12584 BINFO_BASEACCESSES (binfo) = accesses;
12586 for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
12588 tree access = TREE_PURPOSE (base_list);
12589 int via_virtual = TREE_VIA_VIRTUAL (base_list);
12590 tree basetype = TREE_VALUE (base_list);
12591 tree base_binfo;
12593 if (access == access_default_node)
12594 /* The base of a derived struct is public by default. */
12595 access = (tag_code == class_type
12596 ? access_private_node : access_public_node);
12598 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12599 basetype = TREE_TYPE (basetype);
12600 if (!basetype
12601 || (TREE_CODE (basetype) != RECORD_TYPE
12602 && TREE_CODE (basetype) != TYPENAME_TYPE
12603 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12604 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12606 error ("base type `%T' fails to be a struct or class type",
12607 basetype);
12608 continue;
12611 if (CLASSTYPE_MARKED (basetype))
12613 if (basetype == ref)
12614 error ("recursive type `%T' undefined", basetype);
12615 else
12616 error ("duplicate base type `%T' invalid", basetype);
12617 continue;
12620 if (TYPE_FOR_JAVA (basetype)
12621 && (current_lang_depth () == 0))
12622 TYPE_FOR_JAVA (ref) = 1;
12624 if (CLASS_TYPE_P (basetype))
12626 base_binfo = TYPE_BINFO (basetype);
12627 /* This flag will be in the binfo of the base type, we must
12628 clear it after copying the base binfos. */
12629 BINFO_DEPENDENT_BASE_P (base_binfo)
12630 = dependent_type_p (basetype);
12632 else
12633 base_binfo = make_binfo (size_zero_node, basetype,
12634 NULL_TREE, NULL_TREE);
12636 TREE_VEC_ELT (binfos, i) = base_binfo;
12637 TREE_VEC_ELT (accesses, i) = access;
12638 /* This flag will be in the binfo of the base type, we must
12639 clear it after copying the base binfos. */
12640 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12642 SET_CLASSTYPE_MARKED (basetype);
12644 /* We are free to modify these bits because they are meaningless
12645 at top level, and BASETYPE is a top-level type. */
12646 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12648 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12649 /* Converting to a virtual base class requires looking
12650 up the offset of the virtual base. */
12651 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12654 if (CLASS_TYPE_P (basetype))
12656 TYPE_HAS_NEW_OPERATOR (ref)
12657 |= TYPE_HAS_NEW_OPERATOR (basetype);
12658 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12659 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12660 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12661 /* If the base-class uses multiple inheritance, so do we. */
12662 TYPE_USES_MULTIPLE_INHERITANCE (ref)
12663 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12664 /* Likewise, if converting to a base of the base may require
12665 code, then we may need to generate code to convert to a
12666 base as well. */
12667 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12668 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12670 i++;
12672 if (i)
12673 TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
12674 else
12675 BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
12677 if (i > 1)
12679 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12680 /* If there is more than one non-empty they cannot be at the same
12681 address. */
12682 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12686 /* Copy the base binfos, collect the virtual bases and set the
12687 inheritance order chain. */
12688 copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
12689 CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
12691 /* Unmark all the types. */
12692 while (i--)
12694 tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
12696 CLEAR_CLASSTYPE_MARKED (basetype);
12697 if (CLASS_TYPE_P (basetype))
12699 TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
12700 BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
12703 CLEAR_CLASSTYPE_MARKED (ref);
12707 /* Begin compiling the definition of an enumeration type.
12708 NAME is its name (or null if anonymous).
12709 Returns the type object, as yet incomplete.
12710 Also records info about it so that build_enumerator
12711 may be used to declare the individual values as they are read. */
12713 tree
12714 start_enum (tree name)
12716 register tree enumtype = NULL_TREE;
12717 struct cp_binding_level *b = current_binding_level;
12719 /* If this is the real definition for a previous forward reference,
12720 fill in the contents in the same object that used to be the
12721 forward reference. */
12723 if (name != NULL_TREE)
12724 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12726 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
12728 error ("multiple definition of `%#T'", enumtype);
12729 error ("%Jprevious definition here", TYPE_MAIN_DECL (enumtype));
12730 /* Clear out TYPE_VALUES, and start again. */
12731 TYPE_VALUES (enumtype) = NULL_TREE;
12733 else
12735 enumtype = make_node (ENUMERAL_TYPE);
12736 pushtag (name, enumtype, 0);
12739 return enumtype;
12742 /* After processing and defining all the values of an enumeration type,
12743 install their decls in the enumeration type and finish it off.
12744 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
12746 void
12747 finish_enum (tree enumtype)
12749 tree values;
12750 tree decl;
12751 tree value;
12752 tree minnode;
12753 tree maxnode;
12754 tree t;
12755 bool unsignedp;
12756 int lowprec;
12757 int highprec;
12758 int precision;
12759 integer_type_kind itk;
12760 tree underlying_type = NULL_TREE;
12762 /* We built up the VALUES in reverse order. */
12763 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
12765 /* For an enum defined in a template, just set the type of the values;
12766 all further processing is postponed until the template is
12767 instantiated. We need to set the type so that tsubst of a CONST_DECL
12768 works. */
12769 if (processing_template_decl)
12771 for (values = TYPE_VALUES (enumtype);
12772 values;
12773 values = TREE_CHAIN (values))
12774 TREE_TYPE (TREE_VALUE (values)) = enumtype;
12775 if (at_function_scope_p ())
12776 add_stmt (build_min (TAG_DEFN, enumtype));
12777 return;
12780 /* Determine the minimum and maximum values of the enumerators. */
12781 if (TYPE_VALUES (enumtype))
12783 minnode = maxnode = NULL_TREE;
12785 for (values = TYPE_VALUES (enumtype);
12786 values;
12787 values = TREE_CHAIN (values))
12789 decl = TREE_VALUE (values);
12791 /* [dcl.enum]: Following the closing brace of an enum-specifier,
12792 each enumerator has the type of its enumeration. Prior to the
12793 closing brace, the type of each enumerator is the type of its
12794 initializing value. */
12795 TREE_TYPE (decl) = enumtype;
12797 /* Update the minimum and maximum values, if appropriate. */
12798 value = DECL_INITIAL (decl);
12799 /* Figure out what the minimum and maximum values of the
12800 enumerators are. */
12801 if (!minnode)
12802 minnode = maxnode = value;
12803 else if (tree_int_cst_lt (maxnode, value))
12804 maxnode = value;
12805 else if (tree_int_cst_lt (value, minnode))
12806 minnode = value;
12808 /* Set the TREE_TYPE for the values as well. That's so that when
12809 we call decl_constant_value we get an entity of the right type
12810 (but with the constant value). But first make a copy so we
12811 don't clobber shared INTEGER_CSTs. */
12812 if (TREE_TYPE (value) != enumtype)
12814 value = DECL_INITIAL (decl) = copy_node (value);
12815 TREE_TYPE (value) = enumtype;
12819 else
12820 /* [dcl.enum]
12822 If the enumerator-list is empty, the underlying type is as if
12823 the enumeration had a single enumerator with value 0. */
12824 minnode = maxnode = integer_zero_node;
12826 /* Compute the number of bits require to represent all values of the
12827 enumeration. We must do this before the type of MINNODE and
12828 MAXNODE are transformed, since min_precision relies on the
12829 TREE_TYPE of the value it is passed. */
12830 unsignedp = tree_int_cst_sgn (minnode) >= 0;
12831 lowprec = min_precision (minnode, unsignedp);
12832 highprec = min_precision (maxnode, unsignedp);
12833 precision = MAX (lowprec, highprec);
12835 /* Determine the underlying type of the enumeration.
12837 [dcl.enum]
12839 The underlying type of an enumeration is an integral type that
12840 can represent all the enumerator values defined in the
12841 enumeration. It is implementation-defined which integral type is
12842 used as the underlying type for an enumeration except that the
12843 underlying type shall not be larger than int unless the value of
12844 an enumerator cannot fit in an int or unsigned int.
12846 We use "int" or an "unsigned int" as the underlying type, even if
12847 a smaller integral type would work, unless the user has
12848 explicitly requested that we use the smallest possible type. */
12849 for (itk = (flag_short_enums ? itk_char : itk_int);
12850 itk != itk_none;
12851 itk++)
12853 underlying_type = integer_types[itk];
12854 if (TYPE_PRECISION (underlying_type) >= precision
12855 && TREE_UNSIGNED (underlying_type) == unsignedp)
12856 break;
12858 if (itk == itk_none)
12860 /* DR 377
12862 IF no integral type can represent all the enumerator values, the
12863 enumeration is ill-formed. */
12864 error ("no integral type can represent all of the enumerator values "
12865 "for `%T'", enumtype);
12866 precision = TYPE_PRECISION (long_long_integer_type_node);
12867 underlying_type = integer_types[itk_unsigned_long_long];
12870 /* Compute the minium and maximum values for the type.
12872 [dcl.enum]
12874 For an enumeration where emin is the smallest enumerator and emax
12875 is the largest, the values of the enumeration are the values of the
12876 underlying type in the range bmin to bmax, where bmin and bmax are,
12877 respectively, the smallest and largest values of the smallest bit-
12878 field that can store emin and emax. */
12879 TYPE_PRECISION (enumtype) = precision;
12880 set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp);
12882 /* [dcl.enum]
12884 The value of sizeof() applied to an enumeration type, an object
12885 of an enumeration type, or an enumerator, is the value of sizeof()
12886 applied to the underlying type. */
12887 TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
12888 TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
12889 TYPE_MODE (enumtype) = TYPE_MODE (underlying_type);
12890 TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
12891 TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
12892 TREE_UNSIGNED (enumtype) = TREE_UNSIGNED (underlying_type);
12894 /* Convert each of the enumerators to the type of the underlying
12895 type of the enumeration. */
12896 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
12898 decl = TREE_VALUE (values);
12899 value = perform_implicit_conversion (underlying_type,
12900 DECL_INITIAL (decl));
12901 TREE_TYPE (value) = enumtype;
12902 DECL_INITIAL (decl) = value;
12903 TREE_VALUE (values) = value;
12906 /* Fix up all variant types of this enum type. */
12907 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
12909 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
12910 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
12911 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
12912 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
12913 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
12914 TYPE_MODE (t) = TYPE_MODE (enumtype);
12915 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
12916 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
12917 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
12918 TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
12921 /* Finish debugging output for this type. */
12922 rest_of_type_compilation (enumtype, namespace_bindings_p ());
12925 /* Build and install a CONST_DECL for an enumeration constant of the
12926 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
12927 Assignment of sequential values by default is handled here. */
12929 void
12930 build_enumerator (tree name, tree value, tree enumtype)
12932 tree decl;
12933 tree context;
12934 tree type;
12936 /* Remove no-op casts from the value. */
12937 if (value)
12938 STRIP_TYPE_NOPS (value);
12940 if (! processing_template_decl)
12942 /* Validate and default VALUE. */
12943 if (value != NULL_TREE)
12945 value = decl_constant_value (value);
12947 if (TREE_CODE (value) == INTEGER_CST)
12949 value = perform_integral_promotions (value);
12950 constant_expression_warning (value);
12952 else
12954 error ("enumerator value for `%D' not integer constant", name);
12955 value = NULL_TREE;
12959 /* Default based on previous value. */
12960 if (value == NULL_TREE)
12962 tree prev_value;
12964 if (TYPE_VALUES (enumtype))
12966 /* The next value is the previous value ... */
12967 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
12968 /* ... plus one. */
12969 value = cp_build_binary_op (PLUS_EXPR,
12970 prev_value,
12971 integer_one_node);
12973 if (tree_int_cst_lt (value, prev_value))
12974 error ("overflow in enumeration values at `%D'", name);
12976 else
12977 value = integer_zero_node;
12980 /* Remove no-op casts from the value. */
12981 STRIP_TYPE_NOPS (value);
12984 /* C++ associates enums with global, function, or class declarations. */
12985 context = current_scope ();
12986 if (!context)
12987 context = current_namespace;
12989 /* Build the actual enumeration constant. Note that the enumeration
12990 constants have the type of their initializers until the
12991 enumeration is complete:
12993 [ dcl.enum ]
12995 Following the closing brace of an enum-specifier, each enumer-
12996 ator has the type of its enumeration. Prior to the closing
12997 brace, the type of each enumerator is the type of its
12998 initializing value.
13000 In finish_enum we will reset the type. Of course, if we're
13001 processing a template, there may be no value. */
13002 type = value ? TREE_TYPE (value) : NULL_TREE;
13004 if (context && context == current_class_type)
13005 /* This enum declaration is local to the class. We need the full
13006 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
13007 decl = build_lang_decl (CONST_DECL, name, type);
13008 else
13009 /* It's a global enum, or it's local to a function. (Note local to
13010 a function could mean local to a class method. */
13011 decl = build_decl (CONST_DECL, name, type);
13013 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13014 TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
13015 DECL_INITIAL (decl) = value;
13017 if (context && context == current_class_type)
13018 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13019 on the TYPE_FIELDS list for `S'. (That's so that you can say
13020 things like `S::i' later.) */
13021 finish_member_declaration (decl);
13022 else
13023 pushdecl (decl);
13025 /* Add this enumeration constant to the list for this type. */
13026 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13030 /* We're defining DECL. Make sure that it's type is OK. */
13032 static void
13033 check_function_type (tree decl, tree current_function_parms)
13035 tree fntype = TREE_TYPE (decl);
13036 tree return_type = complete_type (TREE_TYPE (fntype));
13038 /* In a function definition, arg types must be complete. */
13039 require_complete_types_for_parms (current_function_parms);
13041 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13043 error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13045 /* Make it return void instead, but don't change the
13046 type of the DECL_RESULT, in case we have a named return value. */
13047 if (TREE_CODE (fntype) == METHOD_TYPE)
13049 tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13050 TREE_TYPE (decl)
13051 = build_method_type_directly (ctype,
13052 void_type_node,
13053 FUNCTION_ARG_CHAIN (decl));
13055 else
13056 TREE_TYPE (decl)
13057 = build_function_type (void_type_node,
13058 TYPE_ARG_TYPES (TREE_TYPE (decl)));
13059 TREE_TYPE (decl)
13060 = build_exception_variant (fntype,
13061 TYPE_RAISES_EXCEPTIONS (fntype));
13063 else
13064 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13067 /* Create the FUNCTION_DECL for a function definition.
13068 DECLSPECS and DECLARATOR are the parts of the declaration;
13069 they describe the function's name and the type it returns,
13070 but twisted together in a fashion that parallels the syntax of C.
13072 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13073 DECLARATOR is really the DECL for the function we are about to
13074 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13075 indicating that the function is an inline defined in-class.
13077 This function creates a binding context for the function body
13078 as well as setting up the FUNCTION_DECL in current_function_decl.
13080 Returns 1 on success. If the DECLARATOR is not suitable for a function
13081 (it defines a datum instead), we return 0, which tells
13082 yyparse to report a parse error.
13084 For C++, we must first check whether that datum makes any sense.
13085 For example, "class A local_a(1,2);" means that variable local_a
13086 is an aggregate of type A, which should have a constructor
13087 applied to it with the argument list [1, 2]. */
13090 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13092 tree decl1;
13093 tree ctype = NULL_TREE;
13094 tree fntype;
13095 tree restype;
13096 int doing_friend = 0;
13097 struct cp_binding_level *bl;
13098 tree current_function_parms;
13100 /* Sanity check. */
13101 my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13102 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13104 /* This should only be done once on the top most decl. */
13105 if (have_extern_spec)
13107 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13108 have_extern_spec = false;
13111 if (flags & SF_PRE_PARSED)
13113 decl1 = declarator;
13115 fntype = TREE_TYPE (decl1);
13116 if (TREE_CODE (fntype) == METHOD_TYPE)
13117 ctype = TYPE_METHOD_BASETYPE (fntype);
13119 /* ISO C++ 11.4/5. A friend function defined in a class is in
13120 the (lexical) scope of the class in which it is defined. */
13121 if (!ctype && DECL_FRIEND_P (decl1))
13123 ctype = DECL_FRIEND_CONTEXT (decl1);
13125 /* CTYPE could be null here if we're dealing with a template;
13126 for example, `inline friend float foo()' inside a template
13127 will have no CTYPE set. */
13128 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13129 ctype = NULL_TREE;
13130 else
13131 doing_friend = 1;
13134 last_function_parms = DECL_ARGUMENTS (decl1);
13136 else
13138 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
13139 /* If the declarator is not suitable for a function definition,
13140 cause a syntax error. */
13141 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13142 return 0;
13144 cplus_decl_attributes (&decl1, attrs, 0);
13146 /* If #pragma weak was used, mark the decl weak now. */
13147 if (global_scope_p (current_binding_level))
13148 maybe_apply_pragma_weak (decl1);
13150 fntype = TREE_TYPE (decl1);
13152 restype = TREE_TYPE (fntype);
13154 if (TREE_CODE (fntype) == METHOD_TYPE)
13155 ctype = TYPE_METHOD_BASETYPE (fntype);
13156 else if (DECL_MAIN_P (decl1))
13158 /* If this doesn't return integer_type, or a typedef to
13159 integer_type, complain. */
13160 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl1)), integer_type_node))
13162 if (pedantic || warn_return_type)
13163 pedwarn ("return type for `main' changed to `int'");
13164 TREE_TYPE (decl1) = fntype = default_function_type;
13169 if (DECL_DECLARED_INLINE_P (decl1)
13170 && lookup_attribute ("noinline", attrs))
13171 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
13173 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13174 /* This is a constructor, we must ensure that any default args
13175 introduced by this definition are propagated to the clones
13176 now. The clones are used directly in overload resolution. */
13177 adjust_clone_args (decl1);
13179 /* Sometimes we don't notice that a function is a static member, and
13180 build a METHOD_TYPE for it. Fix that up now. */
13181 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13182 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13184 revert_static_member_fn (decl1);
13185 last_function_parms = TREE_CHAIN (last_function_parms);
13186 ctype = NULL_TREE;
13189 /* Warn if function was previously implicitly declared
13190 (but not if we warned then). */
13191 if (! warn_implicit
13192 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13193 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13195 /* Set up current_class_type, and enter the scope of the class, if
13196 appropriate. */
13197 if (ctype)
13198 push_nested_class (ctype);
13199 else if (DECL_STATIC_FUNCTION_P (decl1))
13200 push_nested_class (DECL_CONTEXT (decl1));
13202 /* Now that we have entered the scope of the class, we must restore
13203 the bindings for any template parameters surrounding DECL1, if it
13204 is an inline member template. (Order is important; consider the
13205 case where a template parameter has the same name as a field of
13206 the class.) It is not until after this point that
13207 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13208 if (flags & SF_INCLASS_INLINE)
13209 maybe_begin_member_template_processing (decl1);
13211 /* Effective C++ rule 15. */
13212 if (warn_ecpp
13213 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13214 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13215 warning ("`operator=' should return a reference to `*this'");
13217 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13218 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13219 if (!DECL_INITIAL (decl1))
13220 DECL_INITIAL (decl1) = error_mark_node;
13222 /* This function exists in static storage.
13223 (This does not mean `static' in the C sense!) */
13224 TREE_STATIC (decl1) = 1;
13226 /* We must call push_template_decl after current_class_type is set
13227 up. (If we are processing inline definitions after exiting a
13228 class scope, current_class_type will be NULL_TREE until set above
13229 by push_nested_class.) */
13230 if (processing_template_decl)
13231 decl1 = push_template_decl (decl1);
13233 /* We are now in the scope of the function being defined. */
13234 current_function_decl = decl1;
13236 /* Save the parm names or decls from this function's declarator
13237 where store_parm_decls will find them. */
13238 current_function_parms = last_function_parms;
13240 /* Make sure the parameter and return types are reasonable. When
13241 you declare a function, these types can be incomplete, but they
13242 must be complete when you define the function. */
13243 if (! processing_template_decl)
13244 check_function_type (decl1, current_function_parms);
13246 /* Build the return declaration for the function. */
13247 restype = TREE_TYPE (fntype);
13248 /* Promote the value to int before returning it. */
13249 if (c_promoting_integer_type_p (restype))
13250 restype = type_promotes_to (restype);
13251 if (DECL_RESULT (decl1) == NULL_TREE)
13253 DECL_RESULT (decl1)
13254 = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13255 c_apply_type_quals_to_decl (cp_type_quals (restype),
13256 DECL_RESULT (decl1));
13259 /* Initialize RTL machinery. We cannot do this until
13260 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
13261 even when processing a template; this is how we get
13262 CFUN set up, and our per-function variables initialized.
13263 FIXME factor out the non-RTL stuff. */
13264 bl = current_binding_level;
13265 allocate_struct_function (decl1);
13266 current_binding_level = bl;
13268 /* Even though we're inside a function body, we still don't want to
13269 call expand_expr to calculate the size of a variable-sized array.
13270 We haven't necessarily assigned RTL to all variables yet, so it's
13271 not safe to try to expand expressions involving them. */
13272 immediate_size_expand = 0;
13273 cfun->x_dont_save_pending_sizes_p = 1;
13275 /* Start the statement-tree, start the tree now. */
13276 begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13278 /* Don't double-count statements in templates. */
13279 DECL_ESTIMATED_INSNS (decl1) = 0;
13281 /* Let the user know we're compiling this function. */
13282 announce_function (decl1);
13284 /* Record the decl so that the function name is defined.
13285 If we already have a decl for this name, and it is a FUNCTION_DECL,
13286 use the old decl. */
13287 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13289 /* A specialization is not used to guide overload resolution. */
13290 if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13291 && ! DECL_FUNCTION_MEMBER_P (decl1))
13292 decl1 = pushdecl (decl1);
13293 else
13295 /* We need to set the DECL_CONTEXT. */
13296 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13297 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13298 /* And make sure we have enough default args. */
13299 check_default_args (decl1);
13301 fntype = TREE_TYPE (decl1);
13304 /* Reset these in case the call to pushdecl changed them. */
13305 current_function_decl = decl1;
13306 cfun->decl = decl1;
13308 /* If we are (erroneously) defining a function that we have already
13309 defined before, wipe out what we knew before. */
13310 if (!DECL_PENDING_INLINE_P (decl1))
13311 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13313 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13315 /* We know that this was set up by `grokclassfn'. We do not
13316 wait until `store_parm_decls', since evil parse errors may
13317 never get us to that point. Here we keep the consistency
13318 between `current_class_type' and `current_class_ptr'. */
13319 tree t = DECL_ARGUMENTS (decl1);
13321 my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13322 162);
13323 my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13324 19990811);
13326 cp_function_chain->x_current_class_ref
13327 = build_indirect_ref (t, NULL);
13328 cp_function_chain->x_current_class_ptr = t;
13330 /* Constructors and destructors need to know whether they're "in
13331 charge" of initializing virtual base classes. */
13332 t = TREE_CHAIN (t);
13333 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13335 current_in_charge_parm = t;
13336 t = TREE_CHAIN (t);
13338 if (DECL_HAS_VTT_PARM_P (decl1))
13340 if (DECL_NAME (t) != vtt_parm_identifier)
13341 abort ();
13342 current_vtt_parm = t;
13346 if (DECL_INTERFACE_KNOWN (decl1))
13348 tree ctx = decl_function_context (decl1);
13350 if (DECL_NOT_REALLY_EXTERN (decl1))
13351 DECL_EXTERNAL (decl1) = 0;
13353 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13354 && TREE_PUBLIC (ctx))
13355 /* This is a function in a local class in an extern inline
13356 function. */
13357 comdat_linkage (decl1);
13359 /* If this function belongs to an interface, it is public.
13360 If it belongs to someone else's interface, it is also external.
13361 This only affects inlines and template instantiations. */
13362 else if (interface_unknown == 0
13363 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13364 || flag_alt_external_templates))
13366 if (DECL_DECLARED_INLINE_P (decl1)
13367 || DECL_TEMPLATE_INSTANTIATION (decl1)
13368 || processing_template_decl)
13370 DECL_EXTERNAL (decl1)
13371 = (interface_only
13372 || (DECL_DECLARED_INLINE_P (decl1)
13373 && ! flag_implement_inlines
13374 && !DECL_VINDEX (decl1)));
13376 /* For WIN32 we also want to put these in linkonce sections. */
13377 maybe_make_one_only (decl1);
13379 else
13380 DECL_EXTERNAL (decl1) = 0;
13381 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13382 DECL_INTERFACE_KNOWN (decl1) = 1;
13384 else if (interface_unknown && interface_only
13385 && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13386 || flag_alt_external_templates))
13388 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13389 interface, we will have interface_only set but not
13390 interface_known. In that case, we don't want to use the normal
13391 heuristics because someone will supply a #pragma implementation
13392 elsewhere, and deducing it here would produce a conflict. */
13393 comdat_linkage (decl1);
13394 DECL_EXTERNAL (decl1) = 0;
13395 DECL_INTERFACE_KNOWN (decl1) = 1;
13396 DECL_DEFER_OUTPUT (decl1) = 1;
13398 else
13400 /* This is a definition, not a reference.
13401 So clear DECL_EXTERNAL. */
13402 DECL_EXTERNAL (decl1) = 0;
13404 if ((DECL_DECLARED_INLINE_P (decl1)
13405 || DECL_TEMPLATE_INSTANTIATION (decl1))
13406 && ! DECL_INTERFACE_KNOWN (decl1)
13407 /* Don't try to defer nested functions for now. */
13408 && ! decl_function_context (decl1))
13409 DECL_DEFER_OUTPUT (decl1) = 1;
13410 else
13411 DECL_INTERFACE_KNOWN (decl1) = 1;
13414 begin_scope (sk_function_parms, decl1);
13416 ++function_depth;
13418 if (DECL_DESTRUCTOR_P (decl1))
13420 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13421 DECL_CONTEXT (dtor_label) = current_function_decl;
13424 start_fname_decls ();
13426 store_parm_decls (current_function_parms);
13428 return 1;
13431 /* Store the parameter declarations into the current function declaration.
13432 This is called after parsing the parameter declarations, before
13433 digesting the body of the function.
13435 Also install to binding contour return value identifier, if any. */
13437 static void
13438 store_parm_decls (tree current_function_parms)
13440 register tree fndecl = current_function_decl;
13441 register tree parm;
13443 /* This is a chain of any other decls that came in among the parm
13444 declarations. If a parm is declared with enum {foo, bar} x;
13445 then CONST_DECLs for foo and bar are put here. */
13446 tree nonparms = NULL_TREE;
13448 if (current_function_parms)
13450 /* This case is when the function was defined with an ANSI prototype.
13451 The parms already have decls, so we need not do anything here
13452 except record them as in effect
13453 and complain if any redundant old-style parm decls were written. */
13455 tree specparms = current_function_parms;
13456 tree next;
13458 /* Must clear this because it might contain TYPE_DECLs declared
13459 at class level. */
13460 storedecls (NULL_TREE);
13462 /* If we're doing semantic analysis, then we'll call pushdecl
13463 for each of these. We must do them in reverse order so that
13464 they end in the correct forward order. */
13465 specparms = nreverse (specparms);
13467 for (parm = specparms; parm; parm = next)
13469 next = TREE_CHAIN (parm);
13470 if (TREE_CODE (parm) == PARM_DECL)
13472 if (DECL_NAME (parm) == NULL_TREE
13473 || TREE_CODE (parm) != VOID_TYPE)
13474 pushdecl (parm);
13475 else
13476 error ("parameter `%D' declared void", parm);
13478 else
13480 /* If we find an enum constant or a type tag,
13481 put it aside for the moment. */
13482 TREE_CHAIN (parm) = NULL_TREE;
13483 nonparms = chainon (nonparms, parm);
13487 /* Get the decls in their original chain order and record in the
13488 function. This is all and only the PARM_DECLs that were
13489 pushed into scope by the loop above. */
13490 DECL_ARGUMENTS (fndecl) = getdecls ();
13492 else
13493 DECL_ARGUMENTS (fndecl) = NULL_TREE;
13495 /* Now store the final chain of decls for the arguments
13496 as the decl-chain of the current lexical scope.
13497 Put the enumerators in as well, at the front so that
13498 DECL_ARGUMENTS is not modified. */
13499 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13501 /* Do the starting of the exception specifications, if we have any. */
13502 if (flag_exceptions && !processing_template_decl
13503 && flag_enforce_eh_specs
13504 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13505 current_eh_spec_block = begin_eh_spec_block ();
13509 /* We have finished doing semantic analysis on DECL, but have not yet
13510 generated RTL for its body. Save away our current state, so that
13511 when we want to generate RTL later we know what to do. */
13513 static void
13514 save_function_data (tree decl)
13516 struct language_function *f;
13518 /* Save the language-specific per-function data so that we can
13519 get it back when we really expand this function. */
13520 my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13521 19990908);
13523 /* Make a copy. */
13524 f = ggc_alloc (sizeof (struct language_function));
13525 memcpy (f, cp_function_chain, sizeof (struct language_function));
13526 DECL_SAVED_FUNCTION_DATA (decl) = f;
13528 /* Clear out the bits we don't need. */
13529 f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13530 f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13531 f->x_named_label_uses = NULL;
13532 f->bindings = NULL;
13533 f->x_local_names = NULL;
13535 /* If we've already decided that we cannot inline this function, we
13536 must remember that fact when we actually go to expand the
13537 function. */
13538 if (current_function_cannot_inline)
13540 f->cannot_inline = current_function_cannot_inline;
13541 DECL_INLINE (decl) = 0;
13545 /* Add a note to mark the beginning of the main body of the constructor.
13546 This is used to set up the data structures for the cleanup regions for
13547 fully-constructed bases and members. */
13549 static void
13550 begin_constructor_body (void)
13554 /* Add a note to mark the end of the main body of the constructor. This is
13555 used to end the cleanup regions for fully-constructed bases and
13556 members. */
13558 static void
13559 finish_constructor_body (void)
13563 /* Do all the processing for the beginning of a destructor; set up the
13564 vtable pointers and cleanups for bases and members. */
13566 static void
13567 begin_destructor_body (void)
13569 tree if_stmt;
13570 tree compound_stmt;
13572 /* If the dtor is empty, and we know there is not any possible
13573 way we could use any vtable entries, before they are possibly
13574 set by a base class dtor, we don't have to setup the vtables,
13575 as we know that any base class dtor will set up any vtables
13576 it needs. We avoid MI, because one base class dtor can do a
13577 virtual dispatch to an overridden function that would need to
13578 have a non-related vtable set up, we cannot avoid setting up
13579 vtables in that case. We could change this to see if there
13580 is just one vtable.
13582 ??? In the destructor for a class, the vtables are set
13583 appropriately for that class. There will be no non-related
13584 vtables. jason 2001-12-11. */
13585 if_stmt = begin_if_stmt ();
13587 /* If it is not safe to avoid setting up the vtables, then
13588 someone will change the condition to be boolean_true_node.
13589 (Actually, for now, we do not have code to set the condition
13590 appropriately, so we just assume that we always need to
13591 initialize the vtables.) */
13592 finish_if_stmt_cond (boolean_true_node, if_stmt);
13594 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
13596 /* Make all virtual function table pointers in non-virtual base
13597 classes point to CURRENT_CLASS_TYPE's virtual function
13598 tables. */
13599 initialize_vtbl_ptrs (current_class_ptr);
13601 finish_compound_stmt (compound_stmt);
13602 finish_then_clause (if_stmt);
13603 finish_if_stmt ();
13605 /* And insert cleanups for our bases and members so that they
13606 will be properly destroyed if we throw. */
13607 push_base_cleanups ();
13610 /* At the end of every destructor we generate code to delete the object if
13611 necessary. Do that now. */
13613 static void
13614 finish_destructor_body (void)
13616 tree exprstmt;
13618 /* Any return from a destructor will end up here; that way all base
13619 and member cleanups will be run when the function returns. */
13620 add_stmt (build_stmt (LABEL_STMT, dtor_label));
13622 /* In a virtual destructor, we must call delete. */
13623 if (DECL_VIRTUAL_P (current_function_decl))
13625 tree if_stmt;
13626 tree virtual_size = cxx_sizeof (current_class_type);
13628 /* [class.dtor]
13630 At the point of definition of a virtual destructor (including
13631 an implicit definition), non-placement operator delete shall
13632 be looked up in the scope of the destructor's class and if
13633 found shall be accessible and unambiguous. */
13634 exprstmt = build_op_delete_call
13635 (DELETE_EXPR, current_class_ptr, virtual_size,
13636 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13638 if_stmt = begin_if_stmt ();
13639 finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13640 current_in_charge_parm,
13641 integer_one_node),
13642 if_stmt);
13643 finish_expr_stmt (exprstmt);
13644 finish_then_clause (if_stmt);
13645 finish_if_stmt ();
13649 /* Do the necessary processing for the beginning of a function body, which
13650 in this case includes member-initializers, but not the catch clauses of
13651 a function-try-block. Currently, this means opening a binding level
13652 for the member-initializers (in a ctor) and member cleanups (in a dtor).
13653 In other functions, this isn't necessary, but it doesn't hurt. */
13655 tree
13656 begin_function_body (void)
13658 tree stmt;
13660 if (processing_template_decl)
13661 /* Do nothing now. */;
13662 else
13663 /* Always keep the BLOCK node associated with the outermost pair of
13664 curly braces of a function. These are needed for correct
13665 operation of dwarfout.c. */
13666 keep_next_level (true);
13668 stmt = begin_compound_stmt (/*has_no_scope=*/false);
13669 COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13671 if (processing_template_decl)
13672 /* Do nothing now. */;
13673 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13674 begin_constructor_body ();
13675 else if (DECL_DESTRUCTOR_P (current_function_decl))
13676 begin_destructor_body ();
13678 return stmt;
13681 /* Do the processing for the end of a function body. Currently, this means
13682 closing out the cleanups for fully-constructed bases and members, and in
13683 the case of the destructor, deleting the object if desired. Again, this
13684 is only meaningful for [cd]tors, since they are the only functions where
13685 there is a significant distinction between the main body and any
13686 function catch clauses. Handling, say, main() return semantics here
13687 would be wrong, as flowing off the end of a function catch clause for
13688 main() would also need to return 0. */
13690 void
13691 finish_function_body (tree compstmt)
13693 /* Close the block. */
13694 finish_compound_stmt (compstmt);
13696 if (processing_template_decl)
13697 /* Do nothing now. */;
13698 else if (DECL_CONSTRUCTOR_P (current_function_decl))
13699 finish_constructor_body ();
13700 else if (DECL_DESTRUCTOR_P (current_function_decl))
13701 finish_destructor_body ();
13704 /* Finish up a function declaration and compile that function
13705 all the way to assembler language output. The free the storage
13706 for the function definition.
13708 FLAGS is a bitwise or of the following values:
13709 2 - INCLASS_INLINE
13710 We just finished processing the body of an in-class inline
13711 function definition. (This processing will have taken place
13712 after the class definition is complete.) */
13714 tree
13715 finish_function (int flags)
13717 register tree fndecl = current_function_decl;
13718 tree fntype, ctype = NULL_TREE;
13719 int inclass_inline = (flags & 2) != 0;
13720 int nested;
13722 /* When we get some parse errors, we can end up without a
13723 current_function_decl, so cope. */
13724 if (fndecl == NULL_TREE)
13725 return error_mark_node;
13727 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13728 && DECL_VIRTUAL_P (fndecl)
13729 && !processing_template_decl)
13731 tree fnclass = DECL_CONTEXT (fndecl);
13732 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
13733 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
13736 nested = function_depth > 1;
13737 fntype = TREE_TYPE (fndecl);
13739 /* TREE_READONLY (fndecl) = 1;
13740 This caused &foo to be of type ptr-to-const-function
13741 which then got a warning when stored in a ptr-to-function variable. */
13743 my_friendly_assert (building_stmt_tree (), 20000911);
13745 finish_fname_decls ();
13747 /* For a cloned function, we've already got all the code we need;
13748 there's no need to add any extra bits. */
13749 if (!DECL_CLONED_FUNCTION_P (fndecl))
13751 if (DECL_MAIN_P (current_function_decl))
13753 /* Make it so that `main' always returns 0 by default. */
13754 #if VMS_TARGET
13755 finish_return_stmt (integer_one_node);
13756 #else
13757 finish_return_stmt (integer_zero_node);
13758 #endif
13761 /* Finish dealing with exception specifiers. */
13762 if (flag_exceptions && !processing_template_decl
13763 && flag_enforce_eh_specs
13764 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13765 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
13766 (TREE_TYPE (current_function_decl)),
13767 current_eh_spec_block);
13770 /* If we're saving up tree structure, tie off the function now. */
13771 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
13773 /* If this function can't throw any exceptions, remember that. */
13774 if (!processing_template_decl
13775 && !cp_function_chain->can_throw
13776 && !flag_non_call_exceptions)
13777 TREE_NOTHROW (fndecl) = 1;
13779 /* This must come after expand_function_end because cleanups might
13780 have declarations (from inline functions) that need to go into
13781 this function's blocks. */
13783 /* If the current binding level isn't the outermost binding level
13784 for this function, either there is a bug, or we have experienced
13785 syntax errors and the statement tree is malformed. */
13786 if (current_binding_level->kind != sk_function_parms)
13788 /* Make sure we have already experienced errors. */
13789 if (errorcount == 0)
13790 abort ();
13792 /* Throw away the broken statement tree and extra binding
13793 levels. */
13794 DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
13796 while (current_binding_level->kind != sk_function_parms)
13798 if (current_binding_level->kind == sk_class)
13799 pop_nested_class ();
13800 else
13801 poplevel (0, 0, 0);
13804 poplevel (1, 0, 1);
13806 /* Statements should always be full-expressions at the outermost set
13807 of curly braces for a function. */
13808 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
13810 /* Set up the named return value optimization, if we can. Here, we
13811 eliminate the copy from the nrv into the RESULT_DECL and any cleanup
13812 for the nrv. genrtl_start_function and declare_return_variable
13813 handle making the nrv and RESULT_DECL share space. */
13814 if (current_function_return_value)
13816 tree r = current_function_return_value;
13817 tree outer;
13819 if (r != error_mark_node
13820 /* This is only worth doing for fns that return in memory--and
13821 simpler, since we don't have to worry about promoted modes. */
13822 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
13823 /* Only allow this for variables declared in the outer scope of
13824 the function so we know that their lifetime always ends with a
13825 return; see g++.dg/opt/nrv6.C. We could be more flexible if
13826 we were to do this optimization in tree-ssa. */
13827 /* Skip the artificial function body block. */
13828 && (outer = BLOCK_SUBBLOCKS (BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl))),
13829 chain_member (r, BLOCK_VARS (outer))))
13832 DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
13833 walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
13834 nullify_returns_r, r);
13836 else
13837 /* Clear it so genrtl_start_function and declare_return_variable
13838 know we're not optimizing. */
13839 current_function_return_value = NULL_TREE;
13842 /* Remember that we were in class scope. */
13843 if (current_class_name)
13844 ctype = current_class_type;
13846 /* Must mark the RESULT_DECL as being in this function. */
13847 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
13849 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
13850 to the FUNCTION_DECL node itself. */
13851 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
13853 /* Save away current state, if appropriate. */
13854 if (!processing_template_decl)
13855 save_function_data (fndecl);
13857 /* If this function calls `setjmp' it cannot be inlined. When
13858 `longjmp' is called it is not guaranteed to restore the value of
13859 local variables that have been modified since the call to
13860 `setjmp'. So, if were to inline this function into some caller
13861 `c', then when we `longjmp', we might not restore all variables
13862 in `c'. (It might seem, at first blush, that there's no way for
13863 this function to modify local variables in `c', but their
13864 addresses may have been stored somewhere accessible to this
13865 function.) */
13866 if (!processing_template_decl && calls_setjmp_p (fndecl))
13867 DECL_UNINLINABLE (fndecl) = 1;
13869 /* Complain if there's just no return statement. */
13870 if (warn_return_type
13871 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
13872 && !current_function_returns_value && !current_function_returns_null
13873 /* Don't complain if we abort or throw. */
13874 && !current_function_returns_abnormally
13875 && !DECL_NAME (DECL_RESULT (fndecl))
13876 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
13877 inline function, as we might never be compiled separately. */
13878 && (DECL_INLINE (fndecl) || processing_template_decl))
13879 warning ("no return statement in function returning non-void");
13881 /* We're leaving the context of this function, so zap cfun. It's still in
13882 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
13883 cfun = NULL;
13884 current_function_decl = NULL;
13886 /* If this is an in-class inline definition, we may have to pop the
13887 bindings for the template parameters that we added in
13888 maybe_begin_member_template_processing when start_function was
13889 called. */
13890 if (inclass_inline)
13891 maybe_end_member_template_processing ();
13893 /* Leave the scope of the class. */
13894 if (ctype)
13895 pop_nested_class ();
13897 --function_depth;
13899 /* Clean up. */
13900 if (! nested)
13901 /* Let the error reporting routines know that we're outside a
13902 function. For a nested function, this value is used in
13903 cxx_pop_function_context and then reset via pop_function_context. */
13904 current_function_decl = NULL_TREE;
13906 return fndecl;
13909 /* Create the FUNCTION_DECL for a function definition.
13910 DECLSPECS and DECLARATOR are the parts of the declaration;
13911 they describe the return type and the name of the function,
13912 but twisted together in a fashion that parallels the syntax of C.
13914 This function creates a binding context for the function body
13915 as well as setting up the FUNCTION_DECL in current_function_decl.
13917 Returns a FUNCTION_DECL on success.
13919 If the DECLARATOR is not suitable for a function (it defines a datum
13920 instead), we return 0, which tells yyparse to report a parse error.
13922 May return void_type_node indicating that this method is actually
13923 a friend. See grokfield for more details.
13925 Came here with a `.pushlevel' .
13927 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
13928 CHANGES TO CODE IN `grokfield'. */
13930 tree
13931 start_method (tree declspecs, tree declarator, tree attrlist)
13933 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
13934 &attrlist);
13936 if (fndecl == error_mark_node)
13937 return error_mark_node;
13939 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
13941 error ("invalid member function declaration");
13942 return error_mark_node;
13945 if (attrlist)
13946 cplus_decl_attributes (&fndecl, attrlist, 0);
13948 /* Pass friends other than inline friend functions back. */
13949 if (fndecl == void_type_node)
13950 return fndecl;
13952 if (DECL_IN_AGGR_P (fndecl))
13954 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
13956 if (DECL_CONTEXT (fndecl)
13957 && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
13958 error ("`%D' is already defined in class `%T'", fndecl,
13959 DECL_CONTEXT (fndecl));
13961 return void_type_node;
13964 check_template_shadow (fndecl);
13966 DECL_DECLARED_INLINE_P (fndecl) = 1;
13967 if (flag_default_inline)
13968 DECL_INLINE (fndecl) = 1;
13970 /* We process method specializations in finish_struct_1. */
13971 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
13973 fndecl = push_template_decl (fndecl);
13974 if (fndecl == error_mark_node)
13975 return fndecl;
13978 if (! DECL_FRIEND_P (fndecl))
13980 if (TREE_CHAIN (fndecl))
13982 fndecl = copy_node (fndecl);
13983 TREE_CHAIN (fndecl) = NULL_TREE;
13985 grok_special_member_properties (fndecl);
13988 cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
13990 /* Make a place for the parms */
13991 begin_scope (sk_function_parms, fndecl);
13993 DECL_IN_AGGR_P (fndecl) = 1;
13994 return fndecl;
13997 /* Go through the motions of finishing a function definition.
13998 We don't compile this method until after the whole class has
13999 been processed.
14001 FINISH_METHOD must return something that looks as though it
14002 came from GROKFIELD (since we are defining a method, after all).
14004 This is called after parsing the body of the function definition.
14005 STMTS is the chain of statements that makes up the function body.
14007 DECL is the ..._DECL that `start_method' provided. */
14009 tree
14010 finish_method (tree decl)
14012 register tree fndecl = decl;
14013 tree old_initial;
14015 register tree link;
14017 if (decl == void_type_node)
14018 return decl;
14020 old_initial = DECL_INITIAL (fndecl);
14022 /* Undo the level for the parms (from start_method).
14023 This is like poplevel, but it causes nothing to be
14024 saved. Saving information here confuses symbol-table
14025 output routines. Besides, this information will
14026 be correctly output when this method is actually
14027 compiled. */
14029 /* Clear out the meanings of the local variables of this level;
14030 also record in each decl which block it belongs to. */
14032 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14034 if (DECL_NAME (link) != NULL_TREE)
14035 pop_binding (DECL_NAME (link), link);
14036 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14037 DECL_CONTEXT (link) = NULL_TREE;
14040 poplevel (0, 0, 0);
14042 DECL_INITIAL (fndecl) = old_initial;
14044 /* We used to check if the context of FNDECL was different from
14045 current_class_type as another way to get inside here. This didn't work
14046 for String.cc in libg++. */
14047 if (DECL_FRIEND_P (fndecl))
14049 CLASSTYPE_INLINE_FRIENDS (current_class_type)
14050 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14051 decl = void_type_node;
14054 return decl;
14058 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14059 we can lay it out later, when and if its type becomes complete. */
14061 void
14062 maybe_register_incomplete_var (tree var)
14064 my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14066 /* Keep track of variables with incomplete types. */
14067 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14068 && DECL_EXTERNAL (var))
14070 tree inner_type = TREE_TYPE (var);
14072 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14073 inner_type = TREE_TYPE (inner_type);
14074 inner_type = TYPE_MAIN_VARIANT (inner_type);
14076 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14077 /* RTTI TD entries are created while defining the type_info. */
14078 || (TYPE_LANG_SPECIFIC (inner_type)
14079 && TYPE_BEING_DEFINED (inner_type)))
14080 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14084 /* Called when a class type (given by TYPE) is defined. If there are
14085 any existing VAR_DECLs whose type hsa been completed by this
14086 declaration, update them now. */
14088 void
14089 complete_vars (tree type)
14091 tree *list = &incomplete_vars;
14093 my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14094 while (*list)
14096 if (same_type_p (type, TREE_PURPOSE (*list)))
14098 tree var = TREE_VALUE (*list);
14099 /* Complete the type of the variable. The VAR_DECL itself
14100 will be laid out in expand_expr. */
14101 complete_type (TREE_TYPE (var));
14102 /* Remove this entry from the list. */
14103 *list = TREE_CHAIN (*list);
14105 else
14106 list = &TREE_CHAIN (*list);
14110 /* If DECL is of a type which needs a cleanup, build that cleanup
14111 here. */
14113 tree
14114 cxx_maybe_build_cleanup (tree decl)
14116 tree type = TREE_TYPE (decl);
14118 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14120 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14121 tree rval;
14123 if (TREE_CODE (type) == ARRAY_TYPE)
14124 rval = decl;
14125 else
14127 cxx_mark_addressable (decl);
14128 rval = build_unary_op (ADDR_EXPR, decl, 0);
14131 /* Optimize for space over speed here. */
14132 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14133 || flag_expensive_optimizations)
14134 flags |= LOOKUP_NONVIRTUAL;
14136 rval = build_delete (TREE_TYPE (rval), rval,
14137 sfk_complete_destructor, flags, 0);
14139 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14140 && ! TYPE_HAS_DESTRUCTOR (type))
14141 rval = build_compound_expr (rval, build_vbase_delete (type, decl));
14143 return rval;
14145 return NULL_TREE;
14148 /* When a stmt has been parsed, this function is called. */
14150 void
14151 finish_stmt (void)
14153 /* Always assume this statement was not an expression statement. If
14154 it actually was an expression statement, its our callers
14155 responsibility to fix this up. */
14156 last_expr_type = NULL_TREE;
14159 /* DECL was originally constructed as a non-static member function,
14160 but turned out to be static. Update it accordingly. */
14162 void
14163 revert_static_member_fn (tree decl)
14165 tree tmp;
14166 tree function = TREE_TYPE (decl);
14167 tree args = TYPE_ARG_TYPES (function);
14169 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14170 != TYPE_UNQUALIFIED)
14171 error ("static member function `%#D' declared with type qualifiers",
14172 decl);
14174 args = TREE_CHAIN (args);
14175 tmp = build_function_type (TREE_TYPE (function), args);
14176 tmp = build_qualified_type (tmp, cp_type_quals (function));
14177 tmp = build_exception_variant (tmp,
14178 TYPE_RAISES_EXCEPTIONS (function));
14179 TREE_TYPE (decl) = tmp;
14180 if (DECL_ARGUMENTS (decl))
14181 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14182 DECL_STATIC_FUNCTION_P (decl) = 1;
14185 /* Initialize the variables used during compilation of a C++
14186 function. */
14188 void
14189 cxx_push_function_context (struct function * f)
14191 struct language_function *p
14192 = ggc_alloc_cleared (sizeof (struct language_function));
14193 f->language = p;
14195 /* Whenever we start a new function, we destroy temporaries in the
14196 usual way. */
14197 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14199 if (f->decl)
14201 tree fn = f->decl;
14203 current_function_is_thunk = DECL_THUNK_P (fn);
14205 if (DECL_SAVED_FUNCTION_DATA (fn))
14207 /* If we already parsed this function, and we're just expanding it
14208 now, restore saved state. */
14209 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
14211 /* If we decided that we didn't want to inline this function,
14212 make sure the back-end knows that. */
14213 if (!current_function_cannot_inline)
14214 current_function_cannot_inline = cp_function_chain->cannot_inline;
14216 /* We don't need the saved data anymore. Unless this is an inline
14217 function; we need the named return value info for
14218 cp_copy_res_decl_for_inlining. */
14219 if (! DECL_INLINE (fn))
14220 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
14225 /* Free the language-specific parts of F, now that we've finished
14226 compiling the function. */
14228 void
14229 cxx_pop_function_context (struct function * f)
14231 f->language = 0;
14234 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14235 one of the language-independent trees. */
14237 enum cp_tree_node_structure_enum
14238 cp_tree_node_structure (union lang_tree_node * t)
14240 switch (TREE_CODE (&t->generic))
14242 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
14243 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
14244 case OVERLOAD: return TS_CP_OVERLOAD;
14245 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
14246 case PTRMEM_CST: return TS_CP_PTRMEM;
14247 case BASELINK: return TS_CP_BASELINK;
14248 case WRAPPER: return TS_CP_WRAPPER;
14249 default: return TS_CP_GENERIC;
14253 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14254 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
14256 tree
14257 identifier_global_value (tree t)
14259 return IDENTIFIER_GLOBAL_VALUE (t);
14262 /* Build the void_list_node (void_type_node having been created). */
14263 tree
14264 build_void_list_node (void)
14266 tree t = build_tree_list (NULL_TREE, void_type_node);
14267 TREE_PARMLIST (t) = 1;
14268 return t;
14271 static int
14272 cp_missing_noreturn_ok_p (tree decl)
14274 /* A missing noreturn is ok for the `main' function. */
14275 return DECL_MAIN_P (decl);
14278 #include "gt-cp-decl.h"
14279 #include "gtype-cp.h"